TEST_LoRaSTboard

Dependencies:   BufferedSerial SX1276GenericLib mbed

Committer:
benpen10
Date:
Mon Jun 18 15:34:47 2018 +0000
Revision:
0:5f52ac52da6c
Child:
1:584a0952f885
The Base File

Who changed what in which revision?

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