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:
Sun Jun 03 19:25:37 2018 +0000
Revision:
20:a1029437adca
Parent:
19:992eda680d91
When USB is active we keep a Timer running to avoid deepsleep.

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 19:992eda680d91 120 #elif defined(HELTEC_STM32L4)
Helmut64 19:992eda680d91 121 Radio = new SX1276Generic(NULL, HELTEC_L4_1276,
Helmut64 19:992eda680d91 122 LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
Helmut64 19:992eda680d91 123 LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5,
Helmut64 19:992eda680d91 124 POWER_VEXT);
Helmut64 0:c43b6919ae15 125 #else // RFM95
Helmut64 0:c43b6919ae15 126 Radio = new SX1276Generic(NULL, RFM95_SX1276,
Helmut64 0:c43b6919ae15 127 LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
Helmut64 0:c43b6919ae15 128 LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5);
Helmut64 0:c43b6919ae15 129 #endif
Helmut64 0:c43b6919ae15 130
Helmut64 0:c43b6919ae15 131 uint8_t i;
Helmut64 0:c43b6919ae15 132 bool isMaster = true;
Helmut64 0:c43b6919ae15 133
Helmut64 0:c43b6919ae15 134 dprintf("SX1276 Ping Pong Demo Application" );
Helmut64 0:c43b6919ae15 135 dprintf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0);
Helmut64 0:c43b6919ae15 136 dprintf("TXPower: %d dBm", TX_OUTPUT_POWER);
Helmut64 0:c43b6919ae15 137 #if USE_MODEM_LORA == 1
Helmut64 8:3b0d7b4ff28f 138 dprintf("Bandwidth: %d Hz", LORA_BANDWIDTH);
Helmut64 0:c43b6919ae15 139 dprintf("Spreading factor: SF%d", LORA_SPREADING_FACTOR);
Helmut64 0:c43b6919ae15 140 #elif USE_MODEM_FSK == 1
Helmut64 0:c43b6919ae15 141 dprintf("Bandwidth: %d kHz", FSK_BANDWIDTH);
Helmut64 0:c43b6919ae15 142 dprintf("Baudrate: %d", FSK_DATARATE);
Helmut64 0:c43b6919ae15 143 #endif
Helmut64 0:c43b6919ae15 144 // Initialize Radio driver
Helmut64 0:c43b6919ae15 145 RadioEvents.TxDone = OnTxDone;
Helmut64 0:c43b6919ae15 146 RadioEvents.RxDone = OnRxDone;
Helmut64 0:c43b6919ae15 147 RadioEvents.RxError = OnRxError;
Helmut64 0:c43b6919ae15 148 RadioEvents.TxTimeout = OnTxTimeout;
Helmut64 6:1b598b0e52e4 149 RadioEvents.RxTimeout = OnRxTimeout;
Helmut64 6:1b598b0e52e4 150 if (Radio->Init( &RadioEvents ) == false) {
Helmut64 6:1b598b0e52e4 151 while(1) {
Helmut64 6:1b598b0e52e4 152 dprintf("Radio could not be detected!");
Helmut64 6:1b598b0e52e4 153 wait( 1 );
Helmut64 6:1b598b0e52e4 154 }
Helmut64 0:c43b6919ae15 155 }
Helmut64 6:1b598b0e52e4 156
Helmut64 0:c43b6919ae15 157
Helmut64 0:c43b6919ae15 158 switch(Radio->DetectBoardType()) {
Helmut64 0:c43b6919ae15 159 case SX1276MB1LAS:
Helmut64 0:c43b6919ae15 160 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 161 dprintf(" > Board Type: SX1276MB1LAS <");
Helmut64 0:c43b6919ae15 162 break;
Helmut64 0:c43b6919ae15 163 case SX1276MB1MAS:
Helmut64 0:c43b6919ae15 164 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 165 dprintf(" > Board Type: SX1276MB1LAS <");
Helmut64 0:c43b6919ae15 166 case MURATA_SX1276:
Helmut64 0:c43b6919ae15 167 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 168 dprintf(" > Board Type: MURATA_SX1276_STM32L0 <");
Helmut64 0:c43b6919ae15 169 break;
Helmut64 0:c43b6919ae15 170 case RFM95_SX1276:
Helmut64 0:c43b6919ae15 171 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 172 dprintf(" > HopeRF RFM95xx <");
Helmut64 0:c43b6919ae15 173 break;
Helmut64 0:c43b6919ae15 174 default:
Helmut64 0:c43b6919ae15 175 dprintf(" > Board Type: unknown <");
Helmut64 0:c43b6919ae15 176 }
Helmut64 0:c43b6919ae15 177
Helmut64 0:c43b6919ae15 178 Radio->SetChannel(RF_FREQUENCY );
Helmut64 0:c43b6919ae15 179
Helmut64 0:c43b6919ae15 180 #if USE_MODEM_LORA == 1
Helmut64 0:c43b6919ae15 181
Helmut64 0:c43b6919ae15 182 if (LORA_FHSS_ENABLED)
Helmut64 0:c43b6919ae15 183 dprintf(" > LORA FHSS Mode <");
Helmut64 0:c43b6919ae15 184 if (!LORA_FHSS_ENABLED)
Helmut64 0:c43b6919ae15 185 dprintf(" > LORA Mode <");
Helmut64 0:c43b6919ae15 186
Helmut64 0:c43b6919ae15 187 Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
Helmut64 0:c43b6919ae15 188 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
Helmut64 0:c43b6919ae15 189 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
Helmut64 0:c43b6919ae15 190 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
Helmut64 0:c43b6919ae15 191 LORA_IQ_INVERSION_ON, 2000 );
Helmut64 0:c43b6919ae15 192
Helmut64 0:c43b6919ae15 193 Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
Helmut64 0:c43b6919ae15 194 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
Helmut64 0:c43b6919ae15 195 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
Helmut64 0:c43b6919ae15 196 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
Helmut64 0:c43b6919ae15 197 LORA_IQ_INVERSION_ON, true );
Helmut64 0:c43b6919ae15 198
Helmut64 0:c43b6919ae15 199 #elif USE_MODEM_FSK == 1
Helmut64 0:c43b6919ae15 200
Helmut64 0:c43b6919ae15 201 dprintf(" > FSK Mode <");
Helmut64 0:c43b6919ae15 202 Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
Helmut64 0:c43b6919ae15 203 FSK_DATARATE, 0,
Helmut64 0:c43b6919ae15 204 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
Helmut64 0:c43b6919ae15 205 FSK_CRC_ENABLED, 0, 0, 0, 2000 );
Helmut64 0:c43b6919ae15 206
Helmut64 0:c43b6919ae15 207 Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
Helmut64 0:c43b6919ae15 208 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
Helmut64 0:c43b6919ae15 209 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
Helmut64 0:c43b6919ae15 210 0, 0, false, true );
Helmut64 0:c43b6919ae15 211
Helmut64 0:c43b6919ae15 212 #else
Helmut64 0:c43b6919ae15 213
Helmut64 0:c43b6919ae15 214 #error "Please define a modem in the compiler options."
Helmut64 0:c43b6919ae15 215
Helmut64 0:c43b6919ae15 216 #endif
Helmut64 0:c43b6919ae15 217
Helmut64 0:c43b6919ae15 218 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 219 dprintf("Starting Ping-Pong loop");
Helmut64 0:c43b6919ae15 220
Helmut64 0:c43b6919ae15 221
Helmut64 0:c43b6919ae15 222 Radio->Rx( RX_TIMEOUT_VALUE );
Helmut64 0:c43b6919ae15 223
Helmut64 0:c43b6919ae15 224 while( 1 )
Helmut64 0:c43b6919ae15 225 {
Helmut64 0:c43b6919ae15 226 #ifdef TARGET_STM32L4
Helmut64 16:675f4d0ee9e9 227 // WatchDogUpdate();
Helmut64 0:c43b6919ae15 228 #endif
Helmut64 0:c43b6919ae15 229
Helmut64 0:c43b6919ae15 230 switch( State )
Helmut64 0:c43b6919ae15 231 {
Helmut64 0:c43b6919ae15 232 case RX:
Helmut64 0:c43b6919ae15 233 *led3 = 0;
Helmut64 0:c43b6919ae15 234 if( isMaster == true )
Helmut64 0:c43b6919ae15 235 {
Helmut64 0:c43b6919ae15 236 if( BufferSize > 0 )
Helmut64 0:c43b6919ae15 237 {
Helmut64 0:c43b6919ae15 238 if( memcmp(Buffer, PongMsg, sizeof(PongMsg)) == 0 )
Helmut64 0:c43b6919ae15 239 {
Helmut64 0:c43b6919ae15 240 *led = !*led;
Helmut64 0:c43b6919ae15 241 dprintf( "...Pong" );
Helmut64 0:c43b6919ae15 242 // Send the next PING frame
Helmut64 0:c43b6919ae15 243 memcpy(Buffer, PingMsg, sizeof(PingMsg));
Helmut64 0:c43b6919ae15 244 // We fill the buffer with numbers for the payload
Helmut64 0:c43b6919ae15 245 for( i = sizeof(PingMsg); i < BufferSize; i++ )
Helmut64 0:c43b6919ae15 246 {
Helmut64 0:c43b6919ae15 247 Buffer[i] = i - sizeof(PingMsg);
Helmut64 0:c43b6919ae15 248 }
Helmut64 0:c43b6919ae15 249 wait_ms( 10 );
Helmut64 0:c43b6919ae15 250 Radio->Send( Buffer, BufferSize );
Helmut64 0:c43b6919ae15 251 }
Helmut64 0:c43b6919ae15 252 else if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 )
Helmut64 0:c43b6919ae15 253 { // A master already exists then become a slave
Helmut64 0:c43b6919ae15 254 dprintf( "...Ping" );
Helmut64 0:c43b6919ae15 255 *led = !*led;
Helmut64 0:c43b6919ae15 256 isMaster = false;
Helmut64 0:c43b6919ae15 257 // Send the next PONG frame
Helmut64 0:c43b6919ae15 258 memcpy(Buffer, PongMsg, sizeof(PongMsg));
Helmut64 0:c43b6919ae15 259 // We fill the buffer with numbers for the payload
Helmut64 0:c43b6919ae15 260 for( i = sizeof(PongMsg); i < BufferSize; i++ )
Helmut64 0:c43b6919ae15 261 {
Helmut64 0:c43b6919ae15 262 Buffer[i] = i - sizeof(PongMsg);
Helmut64 0:c43b6919ae15 263 }
Helmut64 0:c43b6919ae15 264 wait_ms( 10 );
Helmut64 0:c43b6919ae15 265 Radio->Send( Buffer, BufferSize );
Helmut64 0:c43b6919ae15 266 }
Helmut64 0:c43b6919ae15 267 else // valid reception but neither a PING or a PONG message
Helmut64 0:c43b6919ae15 268 { // Set device as master ans start again
Helmut64 0:c43b6919ae15 269 isMaster = true;
Helmut64 0:c43b6919ae15 270 Radio->Rx( RX_TIMEOUT_VALUE );
Helmut64 0:c43b6919ae15 271 }
Helmut64 0:c43b6919ae15 272 }
Helmut64 0:c43b6919ae15 273 }
Helmut64 0:c43b6919ae15 274 else
Helmut64 0:c43b6919ae15 275 {
Helmut64 0:c43b6919ae15 276 if( BufferSize > 0 )
Helmut64 0:c43b6919ae15 277 {
Helmut64 0:c43b6919ae15 278 if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 )
Helmut64 0:c43b6919ae15 279 {
Helmut64 0:c43b6919ae15 280 *led = !*led;
Helmut64 0:c43b6919ae15 281 dprintf( "...Ping" );
Helmut64 0:c43b6919ae15 282 // Send the reply to the PING string
Helmut64 0:c43b6919ae15 283 memcpy(Buffer, PongMsg, sizeof(PongMsg));
Helmut64 0:c43b6919ae15 284 // We fill the buffer with numbers for the payload
Helmut64 0:c43b6919ae15 285 for( i = sizeof(PongMsg); i < BufferSize; i++ )
Helmut64 0:c43b6919ae15 286 {
Helmut64 0:c43b6919ae15 287 Buffer[i] = i - sizeof(PongMsg);
Helmut64 0:c43b6919ae15 288 }
Helmut64 0:c43b6919ae15 289 wait_ms( 10 );
Helmut64 0:c43b6919ae15 290 Radio->Send( Buffer, BufferSize );
Helmut64 0:c43b6919ae15 291 }
Helmut64 0:c43b6919ae15 292 else // valid reception but not a PING as expected
Helmut64 0:c43b6919ae15 293 { // Set device as master and start again
Helmut64 0:c43b6919ae15 294 isMaster = true;
Helmut64 0:c43b6919ae15 295 Radio->Rx( RX_TIMEOUT_VALUE );
Helmut64 0:c43b6919ae15 296 }
Helmut64 0:c43b6919ae15 297 }
Helmut64 0:c43b6919ae15 298 }
Helmut64 0:c43b6919ae15 299 State = LOWPOWER;
Helmut64 0:c43b6919ae15 300 break;
Helmut64 0:c43b6919ae15 301 case TX:
Helmut64 0:c43b6919ae15 302 *led3 = 1;
Helmut64 0:c43b6919ae15 303 if( isMaster == true )
Helmut64 0:c43b6919ae15 304 {
Helmut64 0:c43b6919ae15 305 dprintf("Ping..." );
Helmut64 0:c43b6919ae15 306 }
Helmut64 0:c43b6919ae15 307 else
Helmut64 0:c43b6919ae15 308 {
Helmut64 0:c43b6919ae15 309 dprintf("Pong..." );
Helmut64 0:c43b6919ae15 310 }
Helmut64 0:c43b6919ae15 311 Radio->Rx( RX_TIMEOUT_VALUE );
Helmut64 0:c43b6919ae15 312 State = LOWPOWER;
Helmut64 0:c43b6919ae15 313 break;
Helmut64 0:c43b6919ae15 314 case RX_TIMEOUT:
Helmut64 0:c43b6919ae15 315 if( isMaster == true )
Helmut64 0:c43b6919ae15 316 {
Helmut64 0:c43b6919ae15 317 // Send the next PING frame
Helmut64 0:c43b6919ae15 318 memcpy(Buffer, PingMsg, sizeof(PingMsg));
Helmut64 0:c43b6919ae15 319 for( i = sizeof(PingMsg); i < BufferSize; i++ )
Helmut64 0:c43b6919ae15 320 {
Helmut64 0:c43b6919ae15 321 Buffer[i] = i - sizeof(PingMsg);
Helmut64 0:c43b6919ae15 322 }
Helmut64 0:c43b6919ae15 323 wait_ms( 10 );
Helmut64 0:c43b6919ae15 324 Radio->Send( Buffer, BufferSize );
Helmut64 0:c43b6919ae15 325 }
Helmut64 0:c43b6919ae15 326 else
Helmut64 0:c43b6919ae15 327 {
Helmut64 0:c43b6919ae15 328 Radio->Rx( RX_TIMEOUT_VALUE );
Helmut64 0:c43b6919ae15 329 }
Helmut64 0:c43b6919ae15 330 State = LOWPOWER;
Helmut64 0:c43b6919ae15 331 break;
Helmut64 0:c43b6919ae15 332 case RX_ERROR:
Helmut64 0:c43b6919ae15 333 // We have received a Packet with a CRC error, send reply as if packet was correct
Helmut64 0:c43b6919ae15 334 if( isMaster == true )
Helmut64 0:c43b6919ae15 335 {
Helmut64 0:c43b6919ae15 336 // Send the next PING frame
Helmut64 0:c43b6919ae15 337 memcpy(Buffer, PingMsg, sizeof(PingMsg));
Helmut64 0:c43b6919ae15 338 for( i = 4; i < BufferSize; i++ )
Helmut64 0:c43b6919ae15 339 {
Helmut64 0:c43b6919ae15 340 Buffer[i] = i - 4;
Helmut64 0:c43b6919ae15 341 }
Helmut64 0:c43b6919ae15 342 wait_ms( 10 );
Helmut64 0:c43b6919ae15 343 Radio->Send( Buffer, BufferSize );
Helmut64 0:c43b6919ae15 344 }
Helmut64 0:c43b6919ae15 345 else
Helmut64 0:c43b6919ae15 346 {
Helmut64 0:c43b6919ae15 347 // Send the next PONG frame
Helmut64 0:c43b6919ae15 348 memcpy(Buffer, PongMsg, sizeof(PongMsg));
Helmut64 0:c43b6919ae15 349 for( i = sizeof(PongMsg); i < BufferSize; i++ )
Helmut64 0:c43b6919ae15 350 {
Helmut64 0:c43b6919ae15 351 Buffer[i] = i - sizeof(PongMsg);
Helmut64 0:c43b6919ae15 352 }
Helmut64 0:c43b6919ae15 353 wait_ms( 10 );
Helmut64 0:c43b6919ae15 354 Radio->Send( Buffer, BufferSize );
Helmut64 0:c43b6919ae15 355 }
Helmut64 0:c43b6919ae15 356 State = LOWPOWER;
Helmut64 0:c43b6919ae15 357 break;
Helmut64 0:c43b6919ae15 358 case TX_TIMEOUT:
Helmut64 0:c43b6919ae15 359 Radio->Rx( RX_TIMEOUT_VALUE );
Helmut64 0:c43b6919ae15 360 State = LOWPOWER;
Helmut64 0:c43b6919ae15 361 break;
Helmut64 0:c43b6919ae15 362 case LOWPOWER:
Helmut64 1:65d602e2d746 363 sleep();
Helmut64 0:c43b6919ae15 364 break;
Helmut64 0:c43b6919ae15 365 default:
Helmut64 0:c43b6919ae15 366 State = LOWPOWER;
Helmut64 0:c43b6919ae15 367 break;
Helmut64 0:c43b6919ae15 368 }
Helmut64 0:c43b6919ae15 369 }
Helmut64 0:c43b6919ae15 370 }
Helmut64 0:c43b6919ae15 371
Helmut64 11:d3a591c20cd7 372 void OnTxDone(void *radio, void *userThisPtr, void *userData)
Helmut64 0:c43b6919ae15 373 {
Helmut64 0:c43b6919ae15 374 Radio->Sleep( );
Helmut64 0:c43b6919ae15 375 State = TX;
Helmut64 0:c43b6919ae15 376 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 377 dprintf("> OnTxDone");
Helmut64 0:c43b6919ae15 378 }
Helmut64 0:c43b6919ae15 379
Helmut64 11:d3a591c20cd7 380 void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
Helmut64 0:c43b6919ae15 381 {
Helmut64 0:c43b6919ae15 382 Radio->Sleep( );
Helmut64 0:c43b6919ae15 383 BufferSize = size;
Helmut64 0:c43b6919ae15 384 memcpy( Buffer, payload, BufferSize );
Helmut64 0:c43b6919ae15 385 State = RX;
Helmut64 0:c43b6919ae15 386 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 387 dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr);
Helmut64 0:c43b6919ae15 388 dump("Data:", payload, size);
Helmut64 0:c43b6919ae15 389 }
Helmut64 0:c43b6919ae15 390
Helmut64 11:d3a591c20cd7 391 void OnTxTimeout(void *radio, void *userThisPtr, void *userData)
Helmut64 0:c43b6919ae15 392 {
Helmut64 0:c43b6919ae15 393 *led3 = 0;
Helmut64 0:c43b6919ae15 394 Radio->Sleep( );
Helmut64 0:c43b6919ae15 395 State = TX_TIMEOUT;
Helmut64 0:c43b6919ae15 396 if(DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 397 dprintf("> OnTxTimeout");
Helmut64 0:c43b6919ae15 398 }
Helmut64 0:c43b6919ae15 399
Helmut64 11:d3a591c20cd7 400 void OnRxTimeout(void *radio, void *userThisPtr, void *userData)
Helmut64 0:c43b6919ae15 401 {
Helmut64 0:c43b6919ae15 402 *led3 = 0;
Helmut64 0:c43b6919ae15 403 Radio->Sleep( );
Helmut64 0:c43b6919ae15 404 Buffer[BufferSize-1] = 0;
Helmut64 0:c43b6919ae15 405 State = RX_TIMEOUT;
Helmut64 0:c43b6919ae15 406 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 407 dprintf("> OnRxTimeout");
Helmut64 0:c43b6919ae15 408 }
Helmut64 0:c43b6919ae15 409
Helmut64 11:d3a591c20cd7 410 void OnRxError(void *radio, void *userThisPtr, void *userData)
Helmut64 0:c43b6919ae15 411 {
Helmut64 0:c43b6919ae15 412 Radio->Sleep( );
Helmut64 0:c43b6919ae15 413 State = RX_ERROR;
Helmut64 0:c43b6919ae15 414 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 415 dprintf("> OnRxError");
Helmut64 0:c43b6919ae15 416 }
Helmut64 0:c43b6919ae15 417
Helmut64 0:c43b6919ae15 418 #endif