Lora support for the STM B_L072Z_LRWAN1 board out of the box. Also supports HopeRF RFM95, Murata CMWX1ZZABZ and Semtech SX1276MB1MAS/SX1276MB1LAS modules.

Dependencies:   BufferedSerial SX1276GenericLib mbed USBDeviceHT

Committer:
Helmut64
Date:
Thu Feb 22 11:12:24 2018 +0000
Revision:
16:675f4d0ee9e9
Parent:
13:5a32a1922fbc
Child:
19:992eda680d91
Added proper PinMap for the Heltec Board; updated libs

Who changed what in which revision?

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