Demo for Semtech Lora SX1272MB2xAS 868MHz an NUCLEO-F401RE (STM Cortex M4)

Dependencies:   SX1272Lib mbed

Fork of SX1272PingPong by Semtech

Committer:
emcu
Date:
Sun Apr 03 17:40:50 2016 +0000
Revision:
15:3365ba2777d2
Parent:
14:62abee5e269d
Demo for Semtech Lora SX1272MB2xAS 868MHz and;     NUCLEO-F401RE (STM Cortex M4);     ;     by www.emcu.it;     ;     Date: Mar.2016;     Ver.: 1.0.1;     Project name: LORA-SX1272MB2xAS-PP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emcu 14:62abee5e269d 1 /*
emcu 14:62abee5e269d 2
emcu 14:62abee5e269d 3 Demo for Semtech Lora SX1272MB2xAS 868MHz an NUCLEO-F401RE (STM Cortex M4)
emcu 14:62abee5e269d 4
emcu 14:62abee5e269d 5 by www.emcu.it
emcu 14:62abee5e269d 6
emcu 14:62abee5e269d 7 Date: Mar.2016
emcu 15:3365ba2777d2 8 Ver.: 1.0.1
emcu 15:3365ba2777d2 9 Project name: LORA-SX1272MB2xAS-PP
emcu 14:62abee5e269d 10
emcu 14:62abee5e269d 11 Note:
emcu 14:62abee5e269d 12 The original project is here: https://developer.mbed.org/components/SX1272MB2xAS/
emcu 14:62abee5e269d 13 is: SX1272 Ping Pong Demo Application
emcu 14:62abee5e269d 14
emcu 14:62abee5e269d 15 How to use:
emcu 14:62abee5e269d 16 We tested this SW on NUCLEO-F401RE
emcu 14:62abee5e269d 17 Program two kits (NUCLEO-F401RE + SX1272MB2xAS) with this SW.
emcu 14:62abee5e269d 18 Connect the KITs to the PC and open a terminal emulation (for example TeraTerm).
emcu 14:62abee5e269d 19 The TeraTerm configurations are shown below.
emcu 14:62abee5e269d 20 9600 bauds, 8-bit data, no parity
emcu 14:62abee5e269d 21 On the TeraTerm you see the link informations.
emcu 14:62abee5e269d 22 On the NUCLEO-F401RE, if all is OK, you must see the GREEN led that flashing . This means that the two KITs are working properly.
emcu 14:62abee5e269d 23 If the GREEN led flash slowly means that is not present the link between the two KITs.
emcu 14:62abee5e269d 24
emcu 14:62abee5e269d 25
emcu 14:62abee5e269d 26 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
emcu 14:62abee5e269d 27 and associated documentation files (the "Software"), to deal in the Software without
emcu 14:62abee5e269d 28 restriction, including without limitation the rights to use, copy, modify, merge, publish,
emcu 14:62abee5e269d 29 distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
emcu 14:62abee5e269d 30 Software is furnished to do so, subject to the following conditions:
emcu 14:62abee5e269d 31
emcu 14:62abee5e269d 32 The above copyright notice and this permission notice shall be included in all copies or
emcu 14:62abee5e269d 33 substantial portions of the Software.
emcu 14:62abee5e269d 34
emcu 14:62abee5e269d 35 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
emcu 14:62abee5e269d 36 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
emcu 14:62abee5e269d 37 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
emcu 14:62abee5e269d 38 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
emcu 14:62abee5e269d 39 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
emcu 14:62abee5e269d 40
emcu 14:62abee5e269d 41
emcu 14:62abee5e269d 42 */
emcu 14:62abee5e269d 43
GregCr 0:1ed39951ab7b 44 #include "mbed.h"
GregCr 4:5ece30264cd9 45 #include "main.h"
GregCr 13:edb9b443c1dd 46 #include "sx1272-hal.h"
GregCr 8:f956dee63a56 47 #include "debug.h"
GregCr 0:1ed39951ab7b 48
GregCr 0:1ed39951ab7b 49 /* Set this flag to '1' to display debug messages on the console */
GregCr 13:edb9b443c1dd 50 #define DEBUG_MESSAGE 1
GregCr 0:1ed39951ab7b 51
GregCr 0:1ed39951ab7b 52 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
GregCr 5:f2431c4fe3bb 53 #define USE_MODEM_LORA 1
GregCr 0:1ed39951ab7b 54 #define USE_MODEM_FSK !USE_MODEM_LORA
GregCr 0:1ed39951ab7b 55
emcu 15:3365ba2777d2 56 /* NOTE
emcu 15:3365ba2777d2 57 LORA_BANDWIDTH=2 & LORA_SPREADING_FACTOR=7 == MaxSpeed 41Kbit
emcu 15:3365ba2777d2 58 LORA_BANDWIDTH=1 & LORA_SPREADING_FACTOR=12 == MediumSpeed
emcu 15:3365ba2777d2 59 */
emcu 15:3365ba2777d2 60
GregCr 5:f2431c4fe3bb 61 #define RF_FREQUENCY 868000000 // Hz
GregCr 0:1ed39951ab7b 62 #define TX_OUTPUT_POWER 14 // 14 dBm
GregCr 0:1ed39951ab7b 63
GregCr 0:1ed39951ab7b 64 #if USE_MODEM_LORA == 1
GregCr 0:1ed39951ab7b 65
emcu 15:3365ba2777d2 66 #define LORA_BANDWIDTH 0 //2 // [0: 125 kHz,
GregCr 0:1ed39951ab7b 67 // 1: 250 kHz,
GregCr 0:1ed39951ab7b 68 // 2: 500 kHz,
GregCr 0:1ed39951ab7b 69 // 3: Reserved]
emcu 15:3365ba2777d2 70 #define LORA_SPREADING_FACTOR 11 //7 // [SF7..SF12]
GregCr 0:1ed39951ab7b 71 #define LORA_CODINGRATE 1 // [1: 4/5,
GregCr 0:1ed39951ab7b 72 // 2: 4/6,
GregCr 0:1ed39951ab7b 73 // 3: 4/7,
GregCr 0:1ed39951ab7b 74 // 4: 4/8]
GregCr 0:1ed39951ab7b 75 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
GregCr 0:1ed39951ab7b 76 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
GregCr 0:1ed39951ab7b 77 #define LORA_FIX_LENGTH_PAYLOAD_ON false
GregCr 3:8b9e2a4df4b5 78 #define LORA_FHSS_ENABLED false
GregCr 3:8b9e2a4df4b5 79 #define LORA_NB_SYMB_HOP 4
GregCr 0:1ed39951ab7b 80 #define LORA_IQ_INVERSION_ON false
GregCr 3:8b9e2a4df4b5 81 #define LORA_CRC_ENABLED true
GregCr 3:8b9e2a4df4b5 82
GregCr 0:1ed39951ab7b 83 #elif USE_MODEM_FSK == 1
GregCr 0:1ed39951ab7b 84
GregCr 2:59e108728d71 85 #define FSK_FDEV 25000 // Hz
GregCr 2:59e108728d71 86 #define FSK_DATARATE 19200 // bps
GregCr 2:59e108728d71 87 #define FSK_BANDWIDTH 50000 // Hz
GregCr 2:59e108728d71 88 #define FSK_AFC_BANDWIDTH 83333 // Hz
GregCr 0:1ed39951ab7b 89 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
GregCr 0:1ed39951ab7b 90 #define FSK_FIX_LENGTH_PAYLOAD_ON false
GregCr 3:8b9e2a4df4b5 91 #define FSK_CRC_ENABLED true
GregCr 3:8b9e2a4df4b5 92
GregCr 0:1ed39951ab7b 93 #else
GregCr 0:1ed39951ab7b 94 #error "Please define a modem in the compiler options."
GregCr 0:1ed39951ab7b 95 #endif
GregCr 0:1ed39951ab7b 96
GregCr 7:c1bbd6c56979 97 #define RX_TIMEOUT_VALUE 3500000 // in us
GregCr 0:1ed39951ab7b 98 #define BUFFER_SIZE 32 // Define the payload size here
GregCr 0:1ed39951ab7b 99
GregCr 8:f956dee63a56 100 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
GregCr 3:8b9e2a4df4b5 101 DigitalOut led(LED2);
GregCr 3:8b9e2a4df4b5 102 #else
GregCr 3:8b9e2a4df4b5 103 DigitalOut led(LED1);
GregCr 3:8b9e2a4df4b5 104 #endif
GregCr 3:8b9e2a4df4b5 105
emcu 14:62abee5e269d 106 //------------------------------------
emcu 14:62abee5e269d 107 // Hyperterminal configuration
emcu 14:62abee5e269d 108 // 9600 bauds, 8-bit data, no parity
emcu 14:62abee5e269d 109 //------------------------------------
emcu 14:62abee5e269d 110
emcu 14:62abee5e269d 111 Serial pc(SERIAL_TX, SERIAL_RX);
emcu 14:62abee5e269d 112
GregCr 0:1ed39951ab7b 113 /*
GregCr 0:1ed39951ab7b 114 * Global variables declarations
GregCr 0:1ed39951ab7b 115 */
mluis 10:7af820d1e1df 116 typedef enum
mluis 10:7af820d1e1df 117 {
mluis 10:7af820d1e1df 118 LOWPOWER = 0,
mluis 10:7af820d1e1df 119 IDLE,
mluis 10:7af820d1e1df 120
mluis 10:7af820d1e1df 121 RX,
mluis 10:7af820d1e1df 122 RX_TIMEOUT,
mluis 10:7af820d1e1df 123 RX_ERROR,
mluis 10:7af820d1e1df 124
mluis 10:7af820d1e1df 125 TX,
mluis 10:7af820d1e1df 126 TX_TIMEOUT,
mluis 10:7af820d1e1df 127
mluis 10:7af820d1e1df 128 CAD,
mluis 10:7af820d1e1df 129 CAD_DONE
mluis 10:7af820d1e1df 130 }AppStates_t;
GregCr 0:1ed39951ab7b 131
mluis 10:7af820d1e1df 132 volatile AppStates_t State = LOWPOWER;
mluis 10:7af820d1e1df 133
mluis 10:7af820d1e1df 134 /*!
mluis 10:7af820d1e1df 135 * Radio events function pointer
mluis 10:7af820d1e1df 136 */
mluis 10:7af820d1e1df 137 static RadioEvents_t RadioEvents;
mluis 10:7af820d1e1df 138
mluis 10:7af820d1e1df 139 /*
mluis 10:7af820d1e1df 140 * Global variables declarations
mluis 10:7af820d1e1df 141 */
GregCr 13:edb9b443c1dd 142 SX1272MB2xAS Radio( NULL );
GregCr 0:1ed39951ab7b 143
emcu 15:3365ba2777d2 144 const uint8_t PingMsg[] = "em_PING";
emcu 15:3365ba2777d2 145 const uint8_t PongMsg[] = "em_PONG";
GregCr 0:1ed39951ab7b 146
GregCr 0:1ed39951ab7b 147 uint16_t BufferSize = BUFFER_SIZE;
GregCr 0:1ed39951ab7b 148 uint8_t Buffer[BUFFER_SIZE];
GregCr 0:1ed39951ab7b 149
GregCr 5:f2431c4fe3bb 150 int16_t RssiValue = 0.0;
GregCr 5:f2431c4fe3bb 151 int8_t SnrValue = 0.0;
GregCr 0:1ed39951ab7b 152
GregCr 0:1ed39951ab7b 153 int main()
GregCr 0:1ed39951ab7b 154 {
GregCr 0:1ed39951ab7b 155 uint8_t i;
GregCr 0:1ed39951ab7b 156 bool isMaster = true;
GregCr 0:1ed39951ab7b 157
emcu 14:62abee5e269d 158 pc.printf("\n\n\rModified by: http://www.wmcu.it \n\n\r" );
emcu 15:3365ba2777d2 159
emcu 15:3365ba2777d2 160 pc.printf("\n\r RF_FREQUENCY: %d Hz", RF_FREQUENCY);
emcu 15:3365ba2777d2 161 pc.printf("\n\r TX_OUTPUT_POWER: %d dBm", TX_OUTPUT_POWER);
emcu 15:3365ba2777d2 162 pc.printf("\n\r LORA_BANDWIDTH: %d -> 0==125KHz, 1==250KHz, 2==500KHz, 3==Reserved", LORA_BANDWIDTH);
emcu 15:3365ba2777d2 163 pc.printf("\n\r LORA_SPREADING_FACTOR: %d (SF7..SF12)", LORA_SPREADING_FACTOR);
emcu 15:3365ba2777d2 164 pc.printf("\n\r LORA_CODINGRATE: %d", LORA_CODINGRATE);
emcu 15:3365ba2777d2 165 pc.printf(" -> 1==4/5; 2==4/6; 3==4/7; 4==4/8\n\r");
emcu 15:3365ba2777d2 166
emcu 15:3365ba2777d2 167
emcu 14:62abee5e269d 168 pc.printf("\n\rSee this link: https://developer.mbed.org/components/SX1272MB2xAS/ \n\rfor original example.\n\r ");
GregCr 13:edb9b443c1dd 169 debug( "\n\n\r SX1272 Ping Pong Demo Application \n\n\r" );
mluis 10:7af820d1e1df 170
mluis 10:7af820d1e1df 171 // Initialize Radio driver
mluis 10:7af820d1e1df 172 RadioEvents.TxDone = OnTxDone;
mluis 10:7af820d1e1df 173 RadioEvents.RxDone = OnRxDone;
mluis 10:7af820d1e1df 174 RadioEvents.RxError = OnRxError;
mluis 10:7af820d1e1df 175 RadioEvents.TxTimeout = OnTxTimeout;
mluis 10:7af820d1e1df 176 RadioEvents.RxTimeout = OnRxTimeout;
mluis 10:7af820d1e1df 177 Radio.Init( &RadioEvents );
GregCr 0:1ed39951ab7b 178
GregCr 7:c1bbd6c56979 179 // verify the connection with the board
GregCr 7:c1bbd6c56979 180 while( Radio.Read( REG_VERSION ) == 0x00 )
GregCr 2:59e108728d71 181 {
GregCr 7:c1bbd6c56979 182 debug( "Radio could not be detected!\n\r", NULL );
GregCr 7:c1bbd6c56979 183 wait( 1 );
GregCr 2:59e108728d71 184 }
GregCr 7:c1bbd6c56979 185
GregCr 13:edb9b443c1dd 186 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" );
GregCr 13:edb9b443c1dd 187
GregCr 0:1ed39951ab7b 188 Radio.SetChannel( RF_FREQUENCY );
GregCr 0:1ed39951ab7b 189
GregCr 0:1ed39951ab7b 190 #if USE_MODEM_LORA == 1
GregCr 3:8b9e2a4df4b5 191
GregCr 7:c1bbd6c56979 192 debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r");
GregCr 7:c1bbd6c56979 193 debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r");
GregCr 7:c1bbd6c56979 194
GregCr 0:1ed39951ab7b 195 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
GregCr 0:1ed39951ab7b 196 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
GregCr 0:1ed39951ab7b 197 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
GregCr 3:8b9e2a4df4b5 198 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
GregCr 7:c1bbd6c56979 199 LORA_IQ_INVERSION_ON, 2000000 );
GregCr 0:1ed39951ab7b 200
GregCr 0:1ed39951ab7b 201 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
GregCr 0:1ed39951ab7b 202 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
mluis 9:e764990e45df 203 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
GregCr 3:8b9e2a4df4b5 204 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
GregCr 3:8b9e2a4df4b5 205 LORA_IQ_INVERSION_ON, true );
GregCr 0:1ed39951ab7b 206
GregCr 0:1ed39951ab7b 207 #elif USE_MODEM_FSK == 1
GregCr 0:1ed39951ab7b 208
GregCr 7:c1bbd6c56979 209 debug("\n\n\r > FSK Mode < \n\n\r");
GregCr 0:1ed39951ab7b 210 Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
GregCr 0:1ed39951ab7b 211 FSK_DATARATE, 0,
GregCr 0:1ed39951ab7b 212 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
GregCr 7:c1bbd6c56979 213 FSK_CRC_ENABLED, 0, 0, 0, 2000000 );
GregCr 0:1ed39951ab7b 214
GregCr 0:1ed39951ab7b 215 Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
GregCr 0:1ed39951ab7b 216 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
mluis 9:e764990e45df 217 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
GregCr 3:8b9e2a4df4b5 218 0, 0, false, true );
GregCr 0:1ed39951ab7b 219
GregCr 0:1ed39951ab7b 220 #else
GregCr 0:1ed39951ab7b 221
GregCr 0:1ed39951ab7b 222 #error "Please define a modem in the compiler options."
GregCr 0:1ed39951ab7b 223
GregCr 0:1ed39951ab7b 224 #endif
GregCr 1:126d70d374f6 225
GregCr 7:c1bbd6c56979 226 debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" );
GregCr 0:1ed39951ab7b 227
GregCr 3:8b9e2a4df4b5 228 led = 0;
GregCr 3:8b9e2a4df4b5 229
GregCr 0:1ed39951ab7b 230 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 231
GregCr 0:1ed39951ab7b 232 while( 1 )
GregCr 0:1ed39951ab7b 233 {
GregCr 0:1ed39951ab7b 234 switch( State )
GregCr 0:1ed39951ab7b 235 {
GregCr 0:1ed39951ab7b 236 case RX:
GregCr 0:1ed39951ab7b 237 if( isMaster == true )
GregCr 0:1ed39951ab7b 238 {
GregCr 0:1ed39951ab7b 239 if( BufferSize > 0 )
GregCr 0:1ed39951ab7b 240 {
GregCr 0:1ed39951ab7b 241 if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 )
GregCr 0:1ed39951ab7b 242 {
GregCr 3:8b9e2a4df4b5 243 led = !led;
emcu 15:3365ba2777d2 244 debug( "...em_Pong\r\n" );
GregCr 0:1ed39951ab7b 245 // Send the next PING frame
GregCr 7:c1bbd6c56979 246 strcpy( ( char* )Buffer, ( char* )PingMsg );
GregCr 0:1ed39951ab7b 247 // We fill the buffer with numbers for the payload
GregCr 0:1ed39951ab7b 248 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 249 {
GregCr 0:1ed39951ab7b 250 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 251 }
GregCr 0:1ed39951ab7b 252 wait_ms( 10 );
GregCr 0:1ed39951ab7b 253 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 254 }
GregCr 2:59e108728d71 255 else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
GregCr 0:1ed39951ab7b 256 { // A master already exists then become a slave
emcu 15:3365ba2777d2 257 debug( "...em_Ping\r\n" );
GregCr 3:8b9e2a4df4b5 258 led = !led;
GregCr 0:1ed39951ab7b 259 isMaster = false;
GregCr 7:c1bbd6c56979 260 // Send the next PONG frame
GregCr 7:c1bbd6c56979 261 strcpy( ( char* )Buffer, ( char* )PongMsg );
GregCr 2:59e108728d71 262 // We fill the buffer with numbers for the payload
GregCr 2:59e108728d71 263 for( i = 4; i < BufferSize; i++ )
GregCr 2:59e108728d71 264 {
GregCr 2:59e108728d71 265 Buffer[i] = i - 4;
GregCr 2:59e108728d71 266 }
GregCr 3:8b9e2a4df4b5 267 wait_ms( 10 );
GregCr 2:59e108728d71 268 Radio.Send( Buffer, BufferSize );
GregCr 2:59e108728d71 269 }
GregCr 2:59e108728d71 270 else // valid reception but neither a PING or a PONG message
GregCr 2:59e108728d71 271 { // Set device as master ans start again
GregCr 2:59e108728d71 272 isMaster = true;
GregCr 0:1ed39951ab7b 273 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 2:59e108728d71 274 }
GregCr 0:1ed39951ab7b 275 }
GregCr 0:1ed39951ab7b 276 }
GregCr 0:1ed39951ab7b 277 else
GregCr 0:1ed39951ab7b 278 {
GregCr 0:1ed39951ab7b 279 if( BufferSize > 0 )
GregCr 0:1ed39951ab7b 280 {
GregCr 0:1ed39951ab7b 281 if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
GregCr 0:1ed39951ab7b 282 {
GregCr 3:8b9e2a4df4b5 283 led = !led;
emcu 15:3365ba2777d2 284 debug( "...em_Ping\r\n" );
GregCr 2:59e108728d71 285 // Send the reply to the PING string
GregCr 7:c1bbd6c56979 286 strcpy( ( char* )Buffer, ( char* )PongMsg );
GregCr 0:1ed39951ab7b 287 // We fill the buffer with numbers for the payload
GregCr 0:1ed39951ab7b 288 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 289 {
GregCr 0:1ed39951ab7b 290 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 291 }
GregCr 0:1ed39951ab7b 292 wait_ms( 10 );
GregCr 0:1ed39951ab7b 293 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 294 }
GregCr 2:59e108728d71 295 else // valid reception but not a PING as expected
GregCr 2:59e108728d71 296 { // Set device as master and start again
GregCr 2:59e108728d71 297 isMaster = true;
GregCr 2:59e108728d71 298 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 2:59e108728d71 299 }
GregCr 0:1ed39951ab7b 300 }
GregCr 0:1ed39951ab7b 301 }
GregCr 0:1ed39951ab7b 302 State = LOWPOWER;
GregCr 0:1ed39951ab7b 303 break;
GregCr 3:8b9e2a4df4b5 304 case TX:
GregCr 3:8b9e2a4df4b5 305 led = !led;
GregCr 2:59e108728d71 306 if( isMaster == true )
GregCr 0:1ed39951ab7b 307 {
emcu 15:3365ba2777d2 308 debug( "em_Ping...\r\n" );
GregCr 0:1ed39951ab7b 309 }
GregCr 0:1ed39951ab7b 310 else
GregCr 0:1ed39951ab7b 311 {
GregCr 2:59e108728d71 312 debug( "Pong...\r\n" );
GregCr 0:1ed39951ab7b 313 }
GregCr 0:1ed39951ab7b 314 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 315 State = LOWPOWER;
GregCr 0:1ed39951ab7b 316 break;
GregCr 0:1ed39951ab7b 317 case RX_TIMEOUT:
GregCr 0:1ed39951ab7b 318 if( isMaster == true )
GregCr 0:1ed39951ab7b 319 {
GregCr 0:1ed39951ab7b 320 // Send the next PING frame
GregCr 7:c1bbd6c56979 321 strcpy( ( char* )Buffer, ( char* )PingMsg );
GregCr 0:1ed39951ab7b 322 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 323 {
GregCr 0:1ed39951ab7b 324 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 325 }
GregCr 0:1ed39951ab7b 326 wait_ms( 10 );
GregCr 0:1ed39951ab7b 327 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 328 }
GregCr 0:1ed39951ab7b 329 else
GregCr 0:1ed39951ab7b 330 {
GregCr 0:1ed39951ab7b 331 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 332 }
GregCr 0:1ed39951ab7b 333 State = LOWPOWER;
GregCr 0:1ed39951ab7b 334 break;
GregCr 0:1ed39951ab7b 335 case RX_ERROR:
GregCr 7:c1bbd6c56979 336 // We have received a Packet with a CRC error, send reply as if packet was correct
GregCr 0:1ed39951ab7b 337 if( isMaster == true )
GregCr 0:1ed39951ab7b 338 {
GregCr 0:1ed39951ab7b 339 // Send the next PING frame
GregCr 7:c1bbd6c56979 340 strcpy( ( char* )Buffer, ( char* )PingMsg );
GregCr 0:1ed39951ab7b 341 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 342 {
GregCr 0:1ed39951ab7b 343 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 344 }
GregCr 0:1ed39951ab7b 345 wait_ms( 10 );
GregCr 0:1ed39951ab7b 346 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 347 }
GregCr 0:1ed39951ab7b 348 else
GregCr 0:1ed39951ab7b 349 {
GregCr 2:59e108728d71 350 // Send the next PONG frame
GregCr 7:c1bbd6c56979 351 strcpy( ( char* )Buffer, ( char* )PongMsg );
GregCr 2:59e108728d71 352 for( i = 4; i < BufferSize; i++ )
GregCr 2:59e108728d71 353 {
GregCr 2:59e108728d71 354 Buffer[i] = i - 4;
GregCr 2:59e108728d71 355 }
GregCr 2:59e108728d71 356 wait_ms( 10 );
GregCr 2:59e108728d71 357 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 358 }
GregCr 0:1ed39951ab7b 359 State = LOWPOWER;
GregCr 0:1ed39951ab7b 360 break;
GregCr 0:1ed39951ab7b 361 case TX_TIMEOUT:
GregCr 0:1ed39951ab7b 362 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 363 State = LOWPOWER;
GregCr 0:1ed39951ab7b 364 break;
GregCr 0:1ed39951ab7b 365 case LOWPOWER:
GregCr 0:1ed39951ab7b 366 break;
GregCr 0:1ed39951ab7b 367 default:
GregCr 0:1ed39951ab7b 368 State = LOWPOWER;
GregCr 0:1ed39951ab7b 369 break;
GregCr 0:1ed39951ab7b 370 }
GregCr 0:1ed39951ab7b 371 }
GregCr 0:1ed39951ab7b 372 }
GregCr 0:1ed39951ab7b 373
GregCr 0:1ed39951ab7b 374 void OnTxDone( void )
GregCr 0:1ed39951ab7b 375 {
GregCr 5:f2431c4fe3bb 376 Radio.Sleep( );
GregCr 0:1ed39951ab7b 377 State = TX;
GregCr 7:c1bbd6c56979 378 debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
GregCr 0:1ed39951ab7b 379 }
GregCr 0:1ed39951ab7b 380
GregCr 4:5ece30264cd9 381 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
GregCr 0:1ed39951ab7b 382 {
GregCr 0:1ed39951ab7b 383 Radio.Sleep( );
GregCr 0:1ed39951ab7b 384 BufferSize = size;
GregCr 0:1ed39951ab7b 385 memcpy( Buffer, payload, BufferSize );
GregCr 0:1ed39951ab7b 386 RssiValue = rssi;
GregCr 0:1ed39951ab7b 387 SnrValue = snr;
GregCr 0:1ed39951ab7b 388 State = RX;
GregCr 7:c1bbd6c56979 389 debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
GregCr 0:1ed39951ab7b 390 }
GregCr 0:1ed39951ab7b 391
GregCr 0:1ed39951ab7b 392 void OnTxTimeout( void )
GregCr 0:1ed39951ab7b 393 {
GregCr 0:1ed39951ab7b 394 Radio.Sleep( );
GregCr 0:1ed39951ab7b 395 State = TX_TIMEOUT;
GregCr 7:c1bbd6c56979 396 debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
GregCr 0:1ed39951ab7b 397 }
GregCr 0:1ed39951ab7b 398
GregCr 0:1ed39951ab7b 399 void OnRxTimeout( void )
GregCr 0:1ed39951ab7b 400 {
GregCr 0:1ed39951ab7b 401 Radio.Sleep( );
GregCr 1:126d70d374f6 402 Buffer[ BufferSize ] = 0;
GregCr 0:1ed39951ab7b 403 State = RX_TIMEOUT;
GregCr 7:c1bbd6c56979 404 debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
GregCr 0:1ed39951ab7b 405 }
GregCr 0:1ed39951ab7b 406
GregCr 0:1ed39951ab7b 407 void OnRxError( void )
GregCr 0:1ed39951ab7b 408 {
GregCr 0:1ed39951ab7b 409 Radio.Sleep( );
GregCr 0:1ed39951ab7b 410 State = RX_ERROR;
GregCr 7:c1bbd6c56979 411 debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
GregCr 0:1ed39951ab7b 412 }
GregCr 3:8b9e2a4df4b5 413