RadioShuttle Lib for the STM32 L4 Heltec Board

Dependents:   Turtle_RadioShuttle

Committer:
Helmut64
Date:
Wed Feb 06 15:26:48 2019 +0000
Revision:
0:0c31756924a2
Child:
1:0a46cba1bf2c
new lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Helmut64 0:0c31756924a2 1
Helmut64 0:0c31756924a2 2 #include "mbed.h"
Helmut64 0:0c31756924a2 3 #include "PinMap.h"
Helmut64 0:0c31756924a2 4 #include "GenericPingPong.h"
Helmut64 0:0c31756924a2 5 #ifdef DEVICE_LOWPOWERTIMER
Helmut64 0:0c31756924a2 6 #define MyTimeout LowPowerTimeout
Helmut64 0:0c31756924a2 7 #else
Helmut64 0:0c31756924a2 8 #define MyTimeout Timeout
Helmut64 0:0c31756924a2 9 #endif
Helmut64 0:0c31756924a2 10 #include "sx1276-mbed-hal.h"
Helmut64 0:0c31756924a2 11 #include "main.h"
Helmut64 0:0c31756924a2 12
Helmut64 0:0c31756924a2 13 #ifdef FEATURE_LORA
Helmut64 0:0c31756924a2 14
Helmut64 0:0c31756924a2 15 /* Set this flag to '1' to display debug messages on the console */
Helmut64 0:0c31756924a2 16 #define DEBUG_MESSAGE 1
Helmut64 0:0c31756924a2 17
Helmut64 0:0c31756924a2 18 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
Helmut64 0:0c31756924a2 19 #define USE_MODEM_LORA 1
Helmut64 0:0c31756924a2 20 #define USE_MODEM_FSK !USE_MODEM_LORA
Helmut64 0:0c31756924a2 21 #define RF_FREQUENCY RF_FREQUENCY_868_1 // Hz
Helmut64 0:0c31756924a2 22 #define TX_OUTPUT_POWER 14 // 14 dBm
Helmut64 0:0c31756924a2 23
Helmut64 0:0c31756924a2 24 #if USE_MODEM_LORA == 1
Helmut64 0:0c31756924a2 25
Helmut64 0:0c31756924a2 26 #define LORA_BANDWIDTH 125000 // see LoRa bandwidth map for details
Helmut64 0:0c31756924a2 27 #define LORA_SPREADING_FACTOR LORA_SF7
Helmut64 0:0c31756924a2 28 #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5
Helmut64 0:0c31756924a2 29
Helmut64 0:0c31756924a2 30 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
Helmut64 0:0c31756924a2 31 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
Helmut64 0:0c31756924a2 32 #define LORA_FIX_LENGTH_PAYLOAD_ON false
Helmut64 0:0c31756924a2 33 #define LORA_FHSS_ENABLED false
Helmut64 0:0c31756924a2 34 #define LORA_NB_SYMB_HOP 4
Helmut64 0:0c31756924a2 35 #define LORA_IQ_INVERSION_ON false
Helmut64 0:0c31756924a2 36 #define LORA_CRC_ENABLED true
Helmut64 0:0c31756924a2 37
Helmut64 0:0c31756924a2 38 #elif USE_MODEM_FSK == 1
Helmut64 0:0c31756924a2 39
Helmut64 0:0c31756924a2 40 #define FSK_FDEV 25000 // Hz
Helmut64 0:0c31756924a2 41 #define FSK_DATARATE 19200 // bps
Helmut64 0:0c31756924a2 42 #define FSK_BANDWIDTH 50000 // Hz
Helmut64 0:0c31756924a2 43 #define FSK_AFC_BANDWIDTH 83333 // Hz
Helmut64 0:0c31756924a2 44 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
Helmut64 0:0c31756924a2 45 #define FSK_FIX_LENGTH_PAYLOAD_ON false
Helmut64 0:0c31756924a2 46 #define FSK_CRC_ENABLED true
Helmut64 0:0c31756924a2 47
Helmut64 0:0c31756924a2 48 #else
Helmut64 0:0c31756924a2 49 #error "Please define a modem in the compiler options."
Helmut64 0:0c31756924a2 50 #endif
Helmut64 0:0c31756924a2 51
Helmut64 0:0c31756924a2 52
Helmut64 0:0c31756924a2 53 #define RX_TIMEOUT_VALUE 3500 // in ms
Helmut64 0:0c31756924a2 54
Helmut64 0:0c31756924a2 55 //#define BUFFER_SIZE 32 // Define the payload size here
Helmut64 0:0c31756924a2 56 #define BUFFER_SIZE 64 // Define the payload size here
Helmut64 0:0c31756924a2 57
Helmut64 0:0c31756924a2 58 /*
Helmut64 0:0c31756924a2 59 * Global variables declarations
Helmut64 0:0c31756924a2 60 */
Helmut64 0:0c31756924a2 61 typedef enum
Helmut64 0:0c31756924a2 62 {
Helmut64 0:0c31756924a2 63 LOWPOWER = 0,
Helmut64 0:0c31756924a2 64 IDLE,
Helmut64 0:0c31756924a2 65
Helmut64 0:0c31756924a2 66 RX,
Helmut64 0:0c31756924a2 67 RX_TIMEOUT,
Helmut64 0:0c31756924a2 68 RX_ERROR,
Helmut64 0:0c31756924a2 69
Helmut64 0:0c31756924a2 70 TX,
Helmut64 0:0c31756924a2 71 TX_TIMEOUT,
Helmut64 0:0c31756924a2 72
Helmut64 0:0c31756924a2 73 CAD,
Helmut64 0:0c31756924a2 74 CAD_DONE
Helmut64 0:0c31756924a2 75 } AppStates_t;
Helmut64 0:0c31756924a2 76
Helmut64 0:0c31756924a2 77 volatile AppStates_t State = LOWPOWER;
Helmut64 0:0c31756924a2 78
Helmut64 0:0c31756924a2 79 /*!
Helmut64 0:0c31756924a2 80 * Radio events function pointer
Helmut64 0:0c31756924a2 81 */
Helmut64 0:0c31756924a2 82 static RadioEvents_t RadioEvents;
Helmut64 0:0c31756924a2 83
Helmut64 0:0c31756924a2 84 /*
Helmut64 0:0c31756924a2 85 * Global variables declarations
Helmut64 0:0c31756924a2 86 */
Helmut64 0:0c31756924a2 87 SX1276Generic *Radio;
Helmut64 0:0c31756924a2 88
Helmut64 0:0c31756924a2 89
Helmut64 0:0c31756924a2 90 const uint8_t PingMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'I', 'N', 'G'};// "PING";
Helmut64 0:0c31756924a2 91 const uint8_t PongMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'O', 'N', 'G'};// "PONG";
Helmut64 0:0c31756924a2 92
Helmut64 0:0c31756924a2 93 uint16_t BufferSize = BUFFER_SIZE;
Helmut64 0:0c31756924a2 94 uint8_t *Buffer;
Helmut64 0:0c31756924a2 95
Helmut64 0:0c31756924a2 96 DigitalOut *led3;
Helmut64 0:0c31756924a2 97
Helmut64 0:0c31756924a2 98
Helmut64 0:0c31756924a2 99 int SX1276PingPong()
Helmut64 0:0c31756924a2 100 {
Helmut64 0:0c31756924a2 101 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
Helmut64 0:0c31756924a2 102 DigitalOut *led = new DigitalOut(LED2);
Helmut64 0:0c31756924a2 103 #elif defined(TARGET_NUCLEO_L073RZ) || defined (TARGET_DISCO_L072CZ_LRWAN1)
Helmut64 0:0c31756924a2 104 DigitalOut *led = new DigitalOut(LED4); // RX red
Helmut64 0:0c31756924a2 105 led3 = new DigitalOut(LED3); // TX blue
Helmut64 0:0c31756924a2 106 #else
Helmut64 0:0c31756924a2 107 DigitalOut *led = new DigitalOut(LED1);
Helmut64 0:0c31756924a2 108 led3 = led;
Helmut64 0:0c31756924a2 109 #endif
Helmut64 0:0c31756924a2 110
Helmut64 0:0c31756924a2 111 Buffer = new uint8_t[BUFFER_SIZE];
Helmut64 0:0c31756924a2 112 *led3 = 1;
Helmut64 0:0c31756924a2 113
Helmut64 0:0c31756924a2 114 #ifdef TARGET_DISCO_L072CZ_LRWAN1
Helmut64 0:0c31756924a2 115 Radio = new SX1276Generic(NULL, MURATA_SX1276,
Helmut64 0:0c31756924a2 116 LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
Helmut64 0:0c31756924a2 117 LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5,
Helmut64 0:0c31756924a2 118 LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO);
Helmut64 0:0c31756924a2 119 #else // RFM95
Helmut64 0:0c31756924a2 120 Radio = new SX1276Generic(NULL, RFM95_SX1276,
Helmut64 0:0c31756924a2 121 LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
Helmut64 0:0c31756924a2 122 LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5);
Helmut64 0:0c31756924a2 123
Helmut64 0:0c31756924a2 124 #endif
Helmut64 0:0c31756924a2 125
Helmut64 0:0c31756924a2 126 uint8_t i;
Helmut64 0:0c31756924a2 127 bool isMaster = true;
Helmut64 0:0c31756924a2 128
Helmut64 0:0c31756924a2 129 dprintf("SX1276 Ping Pong Demo Application" );
Helmut64 0:0c31756924a2 130 dprintf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0);
Helmut64 0:0c31756924a2 131 dprintf("TXPower: %d dBm", TX_OUTPUT_POWER);
Helmut64 0:0c31756924a2 132 #if USE_MODEM_LORA == 1
Helmut64 0:0c31756924a2 133 dprintf("Bandwidth: %d Hz", LORA_BANDWIDTH);
Helmut64 0:0c31756924a2 134 dprintf("Spreading factor: SF%d", LORA_SPREADING_FACTOR);
Helmut64 0:0c31756924a2 135 #elif USE_MODEM_FSK == 1
Helmut64 0:0c31756924a2 136 dprintf("Bandwidth: %d kHz", FSK_BANDWIDTH);
Helmut64 0:0c31756924a2 137 dprintf("Baudrate: %d", FSK_DATARATE);
Helmut64 0:0c31756924a2 138 #endif
Helmut64 0:0c31756924a2 139 // Initialize Radio driver
Helmut64 0:0c31756924a2 140 RadioEvents.TxDone = OnTxDone;
Helmut64 0:0c31756924a2 141 RadioEvents.RxDone = OnRxDone;
Helmut64 0:0c31756924a2 142 RadioEvents.RxError = OnRxError;
Helmut64 0:0c31756924a2 143 RadioEvents.TxTimeout = OnTxTimeout;
Helmut64 0:0c31756924a2 144 RadioEvents.RxTimeout = OnRxTimeout;
Helmut64 0:0c31756924a2 145 if (Radio->Init( &RadioEvents ) == false) {
Helmut64 0:0c31756924a2 146 while(1) {
Helmut64 0:0c31756924a2 147 dprintf("Radio could not be detected!");
Helmut64 0:0c31756924a2 148 wait( 1 );
Helmut64 0:0c31756924a2 149 }
Helmut64 0:0c31756924a2 150 }
Helmut64 0:0c31756924a2 151
Helmut64 0:0c31756924a2 152 switch(Radio->DetectBoardType()) {
Helmut64 0:0c31756924a2 153 case SX1276MB1LAS:
Helmut64 0:0c31756924a2 154 if (DEBUG_MESSAGE)
Helmut64 0:0c31756924a2 155 dprintf(" > Board Type: SX1276MB1LAS <");
Helmut64 0:0c31756924a2 156 break;
Helmut64 0:0c31756924a2 157 case SX1276MB1MAS:
Helmut64 0:0c31756924a2 158 if (DEBUG_MESSAGE)
Helmut64 0:0c31756924a2 159 dprintf(" > Board Type: SX1276MB1LAS <");
Helmut64 0:0c31756924a2 160 case MURATA_SX1276:
Helmut64 0:0c31756924a2 161 if (DEBUG_MESSAGE)
Helmut64 0:0c31756924a2 162 dprintf(" > Board Type: MURATA_SX1276_STM32L0 <");
Helmut64 0:0c31756924a2 163 break;
Helmut64 0:0c31756924a2 164 case RFM95_SX1276:
Helmut64 0:0c31756924a2 165 if (DEBUG_MESSAGE)
Helmut64 0:0c31756924a2 166 dprintf(" > HopeRF RFM95xx <");
Helmut64 0:0c31756924a2 167 break;
Helmut64 0:0c31756924a2 168 default:
Helmut64 0:0c31756924a2 169 dprintf(" > Board Type: unknown <");
Helmut64 0:0c31756924a2 170 }
Helmut64 0:0c31756924a2 171
Helmut64 0:0c31756924a2 172 Radio->SetChannel(RF_FREQUENCY );
Helmut64 0:0c31756924a2 173
Helmut64 0:0c31756924a2 174 #if USE_MODEM_LORA == 1
Helmut64 0:0c31756924a2 175
Helmut64 0:0c31756924a2 176 if (LORA_FHSS_ENABLED)
Helmut64 0:0c31756924a2 177 dprintf(" > LORA FHSS Mode <");
Helmut64 0:0c31756924a2 178 if (!LORA_FHSS_ENABLED)
Helmut64 0:0c31756924a2 179 dprintf(" > LORA Mode <");
Helmut64 0:0c31756924a2 180
Helmut64 0:0c31756924a2 181 Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
Helmut64 0:0c31756924a2 182 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
Helmut64 0:0c31756924a2 183 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
Helmut64 0:0c31756924a2 184 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
Helmut64 0:0c31756924a2 185 LORA_IQ_INVERSION_ON, 2000 );
Helmut64 0:0c31756924a2 186
Helmut64 0:0c31756924a2 187 Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
Helmut64 0:0c31756924a2 188 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
Helmut64 0:0c31756924a2 189 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
Helmut64 0:0c31756924a2 190 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
Helmut64 0:0c31756924a2 191 LORA_IQ_INVERSION_ON, true );
Helmut64 0:0c31756924a2 192
Helmut64 0:0c31756924a2 193 #elif USE_MODEM_FSK == 1
Helmut64 0:0c31756924a2 194
Helmut64 0:0c31756924a2 195 dprintf(" > FSK Mode <");
Helmut64 0:0c31756924a2 196 Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
Helmut64 0:0c31756924a2 197 FSK_DATARATE, 0,
Helmut64 0:0c31756924a2 198 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
Helmut64 0:0c31756924a2 199 FSK_CRC_ENABLED, 0, 0, 0, 2000 );
Helmut64 0:0c31756924a2 200
Helmut64 0:0c31756924a2 201 Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
Helmut64 0:0c31756924a2 202 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
Helmut64 0:0c31756924a2 203 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
Helmut64 0:0c31756924a2 204 0, 0, false, true );
Helmut64 0:0c31756924a2 205
Helmut64 0:0c31756924a2 206 #else
Helmut64 0:0c31756924a2 207
Helmut64 0:0c31756924a2 208 #error "Please define a modem in the compiler options."
Helmut64 0:0c31756924a2 209
Helmut64 0:0c31756924a2 210 #endif
Helmut64 0:0c31756924a2 211
Helmut64 0:0c31756924a2 212 if (DEBUG_MESSAGE)
Helmut64 0:0c31756924a2 213 dprintf("Starting Ping-Pong loop");
Helmut64 0:0c31756924a2 214
Helmut64 0:0c31756924a2 215
Helmut64 0:0c31756924a2 216 Radio->Rx( RX_TIMEOUT_VALUE );
Helmut64 0:0c31756924a2 217
Helmut64 0:0c31756924a2 218 while( 1 )
Helmut64 0:0c31756924a2 219 {
Helmut64 0:0c31756924a2 220 #ifdef TARGET_STM32L4
Helmut64 0:0c31756924a2 221 WatchDogUpdate();
Helmut64 0:0c31756924a2 222 #endif
Helmut64 0:0c31756924a2 223
Helmut64 0:0c31756924a2 224 switch( State )
Helmut64 0:0c31756924a2 225 {
Helmut64 0:0c31756924a2 226 case RX:
Helmut64 0:0c31756924a2 227 *led3 = 0;
Helmut64 0:0c31756924a2 228 if( isMaster == true )
Helmut64 0:0c31756924a2 229 {
Helmut64 0:0c31756924a2 230 if( BufferSize > 0 )
Helmut64 0:0c31756924a2 231 {
Helmut64 0:0c31756924a2 232 if( memcmp(Buffer, PongMsg, sizeof(PongMsg)) == 0 )
Helmut64 0:0c31756924a2 233 {
Helmut64 0:0c31756924a2 234 *led = !*led;
Helmut64 0:0c31756924a2 235 dprintf( "...Pong" );
Helmut64 0:0c31756924a2 236 // Send the next PING frame
Helmut64 0:0c31756924a2 237 memcpy(Buffer, PingMsg, sizeof(PingMsg));
Helmut64 0:0c31756924a2 238 // We fill the buffer with numbers for the payload
Helmut64 0:0c31756924a2 239 for( i = sizeof(PingMsg); i < BufferSize; i++ )
Helmut64 0:0c31756924a2 240 {
Helmut64 0:0c31756924a2 241 Buffer[i] = i - sizeof(PingMsg);
Helmut64 0:0c31756924a2 242 }
Helmut64 0:0c31756924a2 243 wait_ms( 10 );
Helmut64 0:0c31756924a2 244 Radio->Send( Buffer, BufferSize );
Helmut64 0:0c31756924a2 245 }
Helmut64 0:0c31756924a2 246 else if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 )
Helmut64 0:0c31756924a2 247 { // A master already exists then become a slave
Helmut64 0:0c31756924a2 248 dprintf( "...Ping" );
Helmut64 0:0c31756924a2 249 *led = !*led;
Helmut64 0:0c31756924a2 250 isMaster = false;
Helmut64 0:0c31756924a2 251 // Send the next PONG frame
Helmut64 0:0c31756924a2 252 memcpy(Buffer, PongMsg, sizeof(PongMsg));
Helmut64 0:0c31756924a2 253 // We fill the buffer with numbers for the payload
Helmut64 0:0c31756924a2 254 for( i = sizeof(PongMsg); i < BufferSize; i++ )
Helmut64 0:0c31756924a2 255 {
Helmut64 0:0c31756924a2 256 Buffer[i] = i - sizeof(PongMsg);
Helmut64 0:0c31756924a2 257 }
Helmut64 0:0c31756924a2 258 wait_ms( 10 );
Helmut64 0:0c31756924a2 259 Radio->Send( Buffer, BufferSize );
Helmut64 0:0c31756924a2 260 }
Helmut64 0:0c31756924a2 261 else // valid reception but neither a PING or a PONG message
Helmut64 0:0c31756924a2 262 { // Set device as master ans start again
Helmut64 0:0c31756924a2 263 isMaster = true;
Helmut64 0:0c31756924a2 264 Radio->Rx( RX_TIMEOUT_VALUE );
Helmut64 0:0c31756924a2 265 }
Helmut64 0:0c31756924a2 266 }
Helmut64 0:0c31756924a2 267 }
Helmut64 0:0c31756924a2 268 else
Helmut64 0:0c31756924a2 269 {
Helmut64 0:0c31756924a2 270 if( BufferSize > 0 )
Helmut64 0:0c31756924a2 271 {
Helmut64 0:0c31756924a2 272 if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 )
Helmut64 0:0c31756924a2 273 {
Helmut64 0:0c31756924a2 274 *led = !*led;
Helmut64 0:0c31756924a2 275 dprintf( "...Ping" );
Helmut64 0:0c31756924a2 276 // Send the reply to the PING string
Helmut64 0:0c31756924a2 277 memcpy(Buffer, PongMsg, sizeof(PongMsg));
Helmut64 0:0c31756924a2 278 // We fill the buffer with numbers for the payload
Helmut64 0:0c31756924a2 279 for( i = sizeof(PongMsg); i < BufferSize; i++ )
Helmut64 0:0c31756924a2 280 {
Helmut64 0:0c31756924a2 281 Buffer[i] = i - sizeof(PongMsg);
Helmut64 0:0c31756924a2 282 }
Helmut64 0:0c31756924a2 283 wait_ms( 10 );
Helmut64 0:0c31756924a2 284 Radio->Send( Buffer, BufferSize );
Helmut64 0:0c31756924a2 285 }
Helmut64 0:0c31756924a2 286 else // valid reception but not a PING as expected
Helmut64 0:0c31756924a2 287 { // Set device as master and start again
Helmut64 0:0c31756924a2 288 isMaster = true;
Helmut64 0:0c31756924a2 289 Radio->Rx( RX_TIMEOUT_VALUE );
Helmut64 0:0c31756924a2 290 }
Helmut64 0:0c31756924a2 291 }
Helmut64 0:0c31756924a2 292 }
Helmut64 0:0c31756924a2 293 State = LOWPOWER;
Helmut64 0:0c31756924a2 294 break;
Helmut64 0:0c31756924a2 295 case TX:
Helmut64 0:0c31756924a2 296 *led3 = 1;
Helmut64 0:0c31756924a2 297 if( isMaster == true )
Helmut64 0:0c31756924a2 298 {
Helmut64 0:0c31756924a2 299 dprintf("Ping..." );
Helmut64 0:0c31756924a2 300 }
Helmut64 0:0c31756924a2 301 else
Helmut64 0:0c31756924a2 302 {
Helmut64 0:0c31756924a2 303 dprintf("Pong..." );
Helmut64 0:0c31756924a2 304 }
Helmut64 0:0c31756924a2 305 Radio->Rx( RX_TIMEOUT_VALUE );
Helmut64 0:0c31756924a2 306 State = LOWPOWER;
Helmut64 0:0c31756924a2 307 break;
Helmut64 0:0c31756924a2 308 case RX_TIMEOUT:
Helmut64 0:0c31756924a2 309 if( isMaster == true )
Helmut64 0:0c31756924a2 310 {
Helmut64 0:0c31756924a2 311 // Send the next PING frame
Helmut64 0:0c31756924a2 312 memcpy(Buffer, PingMsg, sizeof(PingMsg));
Helmut64 0:0c31756924a2 313 for( i = sizeof(PingMsg); i < BufferSize; i++ )
Helmut64 0:0c31756924a2 314 {
Helmut64 0:0c31756924a2 315 Buffer[i] = i - sizeof(PingMsg);
Helmut64 0:0c31756924a2 316 }
Helmut64 0:0c31756924a2 317 wait_ms( 10 );
Helmut64 0:0c31756924a2 318 Radio->Send( Buffer, BufferSize );
Helmut64 0:0c31756924a2 319 }
Helmut64 0:0c31756924a2 320 else
Helmut64 0:0c31756924a2 321 {
Helmut64 0:0c31756924a2 322 Radio->Rx( RX_TIMEOUT_VALUE );
Helmut64 0:0c31756924a2 323 }
Helmut64 0:0c31756924a2 324 State = LOWPOWER;
Helmut64 0:0c31756924a2 325 break;
Helmut64 0:0c31756924a2 326 case RX_ERROR:
Helmut64 0:0c31756924a2 327 // We have received a Packet with a CRC error, send reply as if packet was correct
Helmut64 0:0c31756924a2 328 if( isMaster == true )
Helmut64 0:0c31756924a2 329 {
Helmut64 0:0c31756924a2 330 // Send the next PING frame
Helmut64 0:0c31756924a2 331 memcpy(Buffer, PingMsg, sizeof(PingMsg));
Helmut64 0:0c31756924a2 332 for( i = 4; i < BufferSize; i++ )
Helmut64 0:0c31756924a2 333 {
Helmut64 0:0c31756924a2 334 Buffer[i] = i - 4;
Helmut64 0:0c31756924a2 335 }
Helmut64 0:0c31756924a2 336 wait_ms( 10 );
Helmut64 0:0c31756924a2 337 Radio->Send( Buffer, BufferSize );
Helmut64 0:0c31756924a2 338 }
Helmut64 0:0c31756924a2 339 else
Helmut64 0:0c31756924a2 340 {
Helmut64 0:0c31756924a2 341 // Send the next PONG frame
Helmut64 0:0c31756924a2 342 memcpy(Buffer, PongMsg, sizeof(PongMsg));
Helmut64 0:0c31756924a2 343 for( i = sizeof(PongMsg); i < BufferSize; i++ )
Helmut64 0:0c31756924a2 344 {
Helmut64 0:0c31756924a2 345 Buffer[i] = i - sizeof(PongMsg);
Helmut64 0:0c31756924a2 346 }
Helmut64 0:0c31756924a2 347 wait_ms( 10 );
Helmut64 0:0c31756924a2 348 Radio->Send( Buffer, BufferSize );
Helmut64 0:0c31756924a2 349 }
Helmut64 0:0c31756924a2 350 State = LOWPOWER;
Helmut64 0:0c31756924a2 351 break;
Helmut64 0:0c31756924a2 352 case TX_TIMEOUT:
Helmut64 0:0c31756924a2 353 Radio->Rx( RX_TIMEOUT_VALUE );
Helmut64 0:0c31756924a2 354 State = LOWPOWER;
Helmut64 0:0c31756924a2 355 break;
Helmut64 0:0c31756924a2 356 case LOWPOWER:
Helmut64 0:0c31756924a2 357 break;
Helmut64 0:0c31756924a2 358 default:
Helmut64 0:0c31756924a2 359 State = LOWPOWER;
Helmut64 0:0c31756924a2 360 break;
Helmut64 0:0c31756924a2 361 }
Helmut64 0:0c31756924a2 362 }
Helmut64 0:0c31756924a2 363 }
Helmut64 0:0c31756924a2 364
Helmut64 0:0c31756924a2 365 void OnTxDone(void *radio, void *userThisPtr, void *userData)
Helmut64 0:0c31756924a2 366 {
Helmut64 0:0c31756924a2 367 SX1276Generic *r = (SX1276Generic *)radio;
Helmut64 0:0c31756924a2 368 r->Sleep( );
Helmut64 0:0c31756924a2 369 State = TX;
Helmut64 0:0c31756924a2 370 if (DEBUG_MESSAGE)
Helmut64 0:0c31756924a2 371 dprintf("> OnTxDone");
Helmut64 0:0c31756924a2 372 }
Helmut64 0:0c31756924a2 373
Helmut64 0:0c31756924a2 374 void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
Helmut64 0:0c31756924a2 375 {
Helmut64 0:0c31756924a2 376 SX1276Generic *r = (SX1276Generic *)radio;
Helmut64 0:0c31756924a2 377 r->Sleep( );
Helmut64 0:0c31756924a2 378 BufferSize = size;
Helmut64 0:0c31756924a2 379 memcpy( Buffer, payload, BufferSize );
Helmut64 0:0c31756924a2 380 State = RX;
Helmut64 0:0c31756924a2 381 if (DEBUG_MESSAGE)
Helmut64 0:0c31756924a2 382 dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr);
Helmut64 0:0c31756924a2 383 dump("Data:", payload, size);
Helmut64 0:0c31756924a2 384 }
Helmut64 0:0c31756924a2 385
Helmut64 0:0c31756924a2 386 void OnTxTimeout(void *radio, void *userThisPtr, void *userData)
Helmut64 0:0c31756924a2 387 {
Helmut64 0:0c31756924a2 388 SX1276Generic *r = (SX1276Generic *)radio;
Helmut64 0:0c31756924a2 389 *led3 = 0;
Helmut64 0:0c31756924a2 390 r->Sleep( );
Helmut64 0:0c31756924a2 391 State = TX_TIMEOUT;
Helmut64 0:0c31756924a2 392 if(DEBUG_MESSAGE)
Helmut64 0:0c31756924a2 393 dprintf("> OnTxTimeout");
Helmut64 0:0c31756924a2 394 }
Helmut64 0:0c31756924a2 395
Helmut64 0:0c31756924a2 396 void OnRxTimeout(void *radio, void *userThisPtr, void *userData)
Helmut64 0:0c31756924a2 397 {
Helmut64 0:0c31756924a2 398 SX1276Generic *r = (SX1276Generic *)radio;
Helmut64 0:0c31756924a2 399 *led3 = 0;
Helmut64 0:0c31756924a2 400 r->Sleep( );
Helmut64 0:0c31756924a2 401 Buffer[BufferSize-1] = 0;
Helmut64 0:0c31756924a2 402 State = RX_TIMEOUT;
Helmut64 0:0c31756924a2 403 if (DEBUG_MESSAGE)
Helmut64 0:0c31756924a2 404 dprintf("> OnRxTimeout");
Helmut64 0:0c31756924a2 405 }
Helmut64 0:0c31756924a2 406
Helmut64 0:0c31756924a2 407 void OnRxError(void *radio, void *userThisPtr, void *userData)
Helmut64 0:0c31756924a2 408 {
Helmut64 0:0c31756924a2 409 SX1276Generic *r = (SX1276Generic *)radio;
Helmut64 0:0c31756924a2 410 r->Sleep( );
Helmut64 0:0c31756924a2 411 State = RX_ERROR;
Helmut64 0:0c31756924a2 412 if (DEBUG_MESSAGE)
Helmut64 0:0c31756924a2 413 dprintf("> OnRxError");
Helmut64 0:0c31756924a2 414 }
Helmut64 0:0c31756924a2 415 #endif