This is code is part of a Technion course project in advanced IoT, implementing a device to receive and present sensors data from a Formula racing car built by students at Technion - Israel Institute of Technology.

Dependencies:   mbed Buffer

Fork of DISCO-L072CZ-LRWAN1_LoRa_PingPong by ST

This is code is part of a Technion course project in advanced IoT, implementing a device to receive sensors data from another L072CZ-LRWAN1 installed on a Formula racing car (built by students at Technion - Israel Institute of Technology), and sends it to a GUI presenting the data (GUI project: github.com/ward-mattar/TechnionFormulaGUI).

How to install

  • Create an account on Mbed: https://os.mbed.com/account/signup/
  • Import project into Compiler
  • In the Program Workspace select "Formula_Nucleo_Receiver"
  • Select a Platform like so:
  1. Click button at top-left
  2. Add Board
  3. Search "NUCLEO F103RB" and then "Add to your Mbed Compiler"
  • Finally click "Compile", if the build was successful, the binary would download automatically
  • To install it on device simply plug it in to a PC, open device drive and drag then drop binary file in it
Committer:
Helmut64
Date:
Fri May 19 09:54:10 2017 +0000
Revision:
6:1b598b0e52e4
Parent:
3:dc560d3e9070
Child:
7:6a8a82bfb0c6
Updated SX12GenerlicLib; Radio Init has now a return value true of a Radio chip is detected.

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