Dependencies:   SX1272Lib mbed

Fork of SX1272PingPong by Semtech

Committer:
Viktor5
Date:
Fri Jun 01 09:32:13 2018 +0000
Revision:
15:77a116408c4b
Parent:
14:ab6eecd44b7a
Child:
16:0cf0cb508f19
qw

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GregCr 0:1ed39951ab7b 1 #include "mbed.h"
GregCr 4:5ece30264cd9 2 #include "main.h"
GregCr 13:edb9b443c1dd 3 #include "sx1272-hal.h"
GregCr 8:f956dee63a56 4 #include "debug.h"
GregCr 0:1ed39951ab7b 5
GregCr 0:1ed39951ab7b 6 /* Set this flag to '1' to display debug messages on the console */
GregCr 13:edb9b443c1dd 7 #define DEBUG_MESSAGE 1
GregCr 0:1ed39951ab7b 8
GregCr 0:1ed39951ab7b 9 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
GregCr 5:f2431c4fe3bb 10 #define USE_MODEM_LORA 1
GregCr 0:1ed39951ab7b 11 #define USE_MODEM_FSK !USE_MODEM_LORA
GregCr 0:1ed39951ab7b 12
Viktor5 15:77a116408c4b 13 #define RF_FREQUENCY 850000000 // Hz frequenza centrale 868
GregCr 0:1ed39951ab7b 14 #define TX_OUTPUT_POWER 14 // 14 dBm
GregCr 0:1ed39951ab7b 15
GregCr 0:1ed39951ab7b 16 #if USE_MODEM_LORA == 1
GregCr 0:1ed39951ab7b 17
Viktor5 15:77a116408c4b 18 #define LORA_BANDWIDTH 2 //banda
Viktor5 15:77a116408c4b 19 // [0: 125 kHz,
GregCr 0:1ed39951ab7b 20 // 1: 250 kHz,
GregCr 0:1ed39951ab7b 21 // 2: 500 kHz,
GregCr 0:1ed39951ab7b 22 // 3: Reserved]
Viktor5 15:77a116408c4b 23 #define LORA_SPREADING_FACTOR 12 // [SF7..SF12]
GregCr 0:1ed39951ab7b 24 #define LORA_CODINGRATE 1 // [1: 4/5,
GregCr 0:1ed39951ab7b 25 // 2: 4/6,
GregCr 0:1ed39951ab7b 26 // 3: 4/7,
GregCr 0:1ed39951ab7b 27 // 4: 4/8]
GregCr 0:1ed39951ab7b 28 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
GregCr 0:1ed39951ab7b 29 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
GregCr 0:1ed39951ab7b 30 #define LORA_FIX_LENGTH_PAYLOAD_ON false
GregCr 3:8b9e2a4df4b5 31 #define LORA_FHSS_ENABLED false
GregCr 3:8b9e2a4df4b5 32 #define LORA_NB_SYMB_HOP 4
GregCr 0:1ed39951ab7b 33 #define LORA_IQ_INVERSION_ON false
GregCr 3:8b9e2a4df4b5 34 #define LORA_CRC_ENABLED true
mluis 14:ab6eecd44b7a 35
GregCr 0:1ed39951ab7b 36 #elif USE_MODEM_FSK == 1
GregCr 0:1ed39951ab7b 37
GregCr 2:59e108728d71 38 #define FSK_FDEV 25000 // Hz
GregCr 2:59e108728d71 39 #define FSK_DATARATE 19200 // bps
GregCr 2:59e108728d71 40 #define FSK_BANDWIDTH 50000 // Hz
GregCr 2:59e108728d71 41 #define FSK_AFC_BANDWIDTH 83333 // Hz
GregCr 0:1ed39951ab7b 42 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
GregCr 0:1ed39951ab7b 43 #define FSK_FIX_LENGTH_PAYLOAD_ON false
GregCr 3:8b9e2a4df4b5 44 #define FSK_CRC_ENABLED true
mluis 14:ab6eecd44b7a 45
GregCr 0:1ed39951ab7b 46 #else
GregCr 0:1ed39951ab7b 47 #error "Please define a modem in the compiler options."
GregCr 0:1ed39951ab7b 48 #endif
GregCr 0:1ed39951ab7b 49
mluis 14:ab6eecd44b7a 50 #define RX_TIMEOUT_VALUE 3500 // in ms
GregCr 0:1ed39951ab7b 51 #define BUFFER_SIZE 32 // Define the payload size here
GregCr 0:1ed39951ab7b 52
GregCr 8:f956dee63a56 53 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
mluis 14:ab6eecd44b7a 54 DigitalOut led( LED2 );
GregCr 3:8b9e2a4df4b5 55 #else
mluis 14:ab6eecd44b7a 56 DigitalOut led( LED1 );
GregCr 3:8b9e2a4df4b5 57 #endif
GregCr 3:8b9e2a4df4b5 58
Viktor5 15:77a116408c4b 59 #define waitTime 1000 //Wait Time
Viktor5 15:77a116408c4b 60
GregCr 0:1ed39951ab7b 61 /*
GregCr 0:1ed39951ab7b 62 * Global variables declarations
GregCr 0:1ed39951ab7b 63 */
mluis 10:7af820d1e1df 64 typedef enum
mluis 10:7af820d1e1df 65 {
mluis 10:7af820d1e1df 66 LOWPOWER = 0,
mluis 10:7af820d1e1df 67 IDLE,
mluis 14:ab6eecd44b7a 68
mluis 10:7af820d1e1df 69 RX,
mluis 10:7af820d1e1df 70 RX_TIMEOUT,
mluis 10:7af820d1e1df 71 RX_ERROR,
mluis 14:ab6eecd44b7a 72
mluis 10:7af820d1e1df 73 TX,
mluis 10:7af820d1e1df 74 TX_TIMEOUT,
mluis 14:ab6eecd44b7a 75
mluis 10:7af820d1e1df 76 CAD,
mluis 10:7af820d1e1df 77 CAD_DONE
mluis 10:7af820d1e1df 78 }AppStates_t;
GregCr 0:1ed39951ab7b 79
mluis 10:7af820d1e1df 80 volatile AppStates_t State = LOWPOWER;
mluis 10:7af820d1e1df 81
mluis 10:7af820d1e1df 82 /*!
mluis 10:7af820d1e1df 83 * Radio events function pointer
mluis 10:7af820d1e1df 84 */
mluis 10:7af820d1e1df 85 static RadioEvents_t RadioEvents;
mluis 10:7af820d1e1df 86
mluis 10:7af820d1e1df 87 /*
mluis 10:7af820d1e1df 88 * Global variables declarations
mluis 10:7af820d1e1df 89 */
GregCr 13:edb9b443c1dd 90 SX1272MB2xAS Radio( NULL );
GregCr 0:1ed39951ab7b 91
Viktor5 15:77a116408c4b 92 const uint8_t PingMsg[] = "alfa"; //messaggio Ping
Viktor5 15:77a116408c4b 93 const uint8_t PongMsg[] = "beta";
GregCr 0:1ed39951ab7b 94
GregCr 0:1ed39951ab7b 95 uint16_t BufferSize = BUFFER_SIZE;
GregCr 0:1ed39951ab7b 96 uint8_t Buffer[BUFFER_SIZE];
GregCr 0:1ed39951ab7b 97
GregCr 5:f2431c4fe3bb 98 int16_t RssiValue = 0.0;
GregCr 5:f2431c4fe3bb 99 int8_t SnrValue = 0.0;
GregCr 0:1ed39951ab7b 100
Viktor5 15:77a116408c4b 101 /************************/
Viktor5 15:77a116408c4b 102 DigitalIn button1(PC_13);
Viktor5 15:77a116408c4b 103 Timer t;
Viktor5 15:77a116408c4b 104 char tempo[5];
Viktor5 15:77a116408c4b 105 float somma, tmisura;
Viktor5 15:77a116408c4b 106 int misure=10;
Viktor5 15:77a116408c4b 107
Viktor5 15:77a116408c4b 108 bool oktx=1;
Viktor5 15:77a116408c4b 109 /************************/
Viktor5 15:77a116408c4b 110
Viktor5 15:77a116408c4b 111
mluis 14:ab6eecd44b7a 112 int main( void )
GregCr 0:1ed39951ab7b 113 {
GregCr 0:1ed39951ab7b 114 uint8_t i;
Viktor5 15:77a116408c4b 115 bool isMaster = true; //parte come master
mluis 14:ab6eecd44b7a 116
GregCr 13:edb9b443c1dd 117 debug( "\n\n\r SX1272 Ping Pong Demo Application \n\n\r" );
mluis 10:7af820d1e1df 118
mluis 10:7af820d1e1df 119 // Initialize Radio driver
mluis 10:7af820d1e1df 120 RadioEvents.TxDone = OnTxDone;
mluis 10:7af820d1e1df 121 RadioEvents.RxDone = OnRxDone;
mluis 10:7af820d1e1df 122 RadioEvents.RxError = OnRxError;
mluis 10:7af820d1e1df 123 RadioEvents.TxTimeout = OnTxTimeout;
mluis 10:7af820d1e1df 124 RadioEvents.RxTimeout = OnRxTimeout;
mluis 10:7af820d1e1df 125 Radio.Init( &RadioEvents );
mluis 14:ab6eecd44b7a 126
GregCr 7:c1bbd6c56979 127 // verify the connection with the board
GregCr 7:c1bbd6c56979 128 while( Radio.Read( REG_VERSION ) == 0x00 )
GregCr 2:59e108728d71 129 {
GregCr 7:c1bbd6c56979 130 debug( "Radio could not be detected!\n\r", NULL );
GregCr 7:c1bbd6c56979 131 wait( 1 );
GregCr 2:59e108728d71 132 }
mluis 14:ab6eecd44b7a 133
mluis 14:ab6eecd44b7a 134 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ), "\n\r > Board Type: SX1272MB2xAS < \n\r" );
mluis 14:ab6eecd44b7a 135
GregCr 0:1ed39951ab7b 136 Radio.SetChannel( RF_FREQUENCY );
GregCr 0:1ed39951ab7b 137
GregCr 0:1ed39951ab7b 138 #if USE_MODEM_LORA == 1
mluis 14:ab6eecd44b7a 139
mluis 14:ab6eecd44b7a 140 debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r" );
mluis 14:ab6eecd44b7a 141 debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r" );
GregCr 7:c1bbd6c56979 142
GregCr 0:1ed39951ab7b 143 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
GregCr 0:1ed39951ab7b 144 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
GregCr 0:1ed39951ab7b 145 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
mluis 14:ab6eecd44b7a 146 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
mluis 14:ab6eecd44b7a 147 LORA_IQ_INVERSION_ON, 2000 );
mluis 14:ab6eecd44b7a 148
GregCr 0:1ed39951ab7b 149 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
GregCr 0:1ed39951ab7b 150 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
mluis 9:e764990e45df 151 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
mluis 14:ab6eecd44b7a 152 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
GregCr 3:8b9e2a4df4b5 153 LORA_IQ_INVERSION_ON, true );
mluis 14:ab6eecd44b7a 154
GregCr 0:1ed39951ab7b 155 #elif USE_MODEM_FSK == 1
GregCr 0:1ed39951ab7b 156
mluis 14:ab6eecd44b7a 157 debug("\n\n\r > FSK Mode < \n\n\r" );
GregCr 0:1ed39951ab7b 158 Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
GregCr 0:1ed39951ab7b 159 FSK_DATARATE, 0,
GregCr 0:1ed39951ab7b 160 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
mluis 14:ab6eecd44b7a 161 FSK_CRC_ENABLED, 0, 0, 0, 2000 );
mluis 14:ab6eecd44b7a 162
GregCr 0:1ed39951ab7b 163 Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
GregCr 0:1ed39951ab7b 164 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
mluis 9:e764990e45df 165 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
GregCr 3:8b9e2a4df4b5 166 0, 0, false, true );
mluis 14:ab6eecd44b7a 167
GregCr 0:1ed39951ab7b 168 #else
GregCr 0:1ed39951ab7b 169
GregCr 0:1ed39951ab7b 170 #error "Please define a modem in the compiler options."
GregCr 0:1ed39951ab7b 171
GregCr 0:1ed39951ab7b 172 #endif
mluis 14:ab6eecd44b7a 173
mluis 14:ab6eecd44b7a 174 debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" );
mluis 14:ab6eecd44b7a 175
GregCr 3:8b9e2a4df4b5 176 led = 0;
mluis 14:ab6eecd44b7a 177
GregCr 0:1ed39951ab7b 178 Radio.Rx( RX_TIMEOUT_VALUE );
mluis 14:ab6eecd44b7a 179
Viktor5 15:77a116408c4b 180
Viktor5 15:77a116408c4b 181
Viktor5 15:77a116408c4b 182 while (button1==1)
GregCr 0:1ed39951ab7b 183 {
Viktor5 15:77a116408c4b 184 }
Viktor5 15:77a116408c4b 185
Viktor5 15:77a116408c4b 186
Viktor5 15:77a116408c4b 187
Viktor5 15:77a116408c4b 188 //for(int count=0; count<misure; count++)
Viktor5 15:77a116408c4b 189 //{
Viktor5 15:77a116408c4b 190 // Send the next PING frame
Viktor5 15:77a116408c4b 191 strcpy( ( char* )Buffer, ( char* )PingMsg );
Viktor5 15:77a116408c4b 192
Viktor5 15:77a116408c4b 193 /*// We fill the buffer with numbers for the payload
Viktor5 15:77a116408c4b 194 for( i = 4; i < BufferSize; i++ )
Viktor5 15:77a116408c4b 195 {
Viktor5 15:77a116408c4b 196 Buffer[i] = i - 4;
Viktor5 15:77a116408c4b 197 }*/
Viktor5 15:77a116408c4b 198 //wait_ms( waitTime );
Viktor5 15:77a116408c4b 199
Viktor5 15:77a116408c4b 200 /*while (oktx==0)
Viktor5 15:77a116408c4b 201 {}
Viktor5 15:77a116408c4b 202 oktx=0;*/
Viktor5 15:77a116408c4b 203
Viktor5 15:77a116408c4b 204 debug( "Start TX\r\n" );
Viktor5 15:77a116408c4b 205 t.start();
Viktor5 15:77a116408c4b 206 Radio.Send( Buffer, BufferSize ); //send
Viktor5 15:77a116408c4b 207
Viktor5 15:77a116408c4b 208 //debug ("End TX\r\n");
Viktor5 15:77a116408c4b 209
Viktor5 15:77a116408c4b 210 //t.stop();
Viktor5 15:77a116408c4b 211 wait_ms( 10000 );
Viktor5 15:77a116408c4b 212 /*tmisura=t.read();
Viktor5 15:77a116408c4b 213 somma+=tmisura;
Viktor5 15:77a116408c4b 214 t.reset();
Viktor5 15:77a116408c4b 215
Viktor5 15:77a116408c4b 216
Viktor5 15:77a116408c4b 217 debug("Tempo impiegato\r\n");
Viktor5 15:77a116408c4b 218 sprintf(tempo, "%f", tmisura);
Viktor5 15:77a116408c4b 219 debug(tempo);
Viktor5 15:77a116408c4b 220 debug("\r\n\n\n");*/
Viktor5 15:77a116408c4b 221
Viktor5 15:77a116408c4b 222
Viktor5 15:77a116408c4b 223 //}
Viktor5 15:77a116408c4b 224
Viktor5 15:77a116408c4b 225 somma=somma/misure;
Viktor5 15:77a116408c4b 226 debug("Tempo medio di TX\r\n");
Viktor5 15:77a116408c4b 227 sprintf(tempo, "%f", somma);
Viktor5 15:77a116408c4b 228 debug(tempo);
Viktor5 15:77a116408c4b 229 debug("\r\n\n\n");
Viktor5 15:77a116408c4b 230
Viktor5 15:77a116408c4b 231
Viktor5 15:77a116408c4b 232 /* switch( State )
GregCr 0:1ed39951ab7b 233 {
GregCr 0:1ed39951ab7b 234 case RX:
GregCr 0:1ed39951ab7b 235 if( isMaster == true )
GregCr 0:1ed39951ab7b 236 {
GregCr 0:1ed39951ab7b 237 if( BufferSize > 0 )
GregCr 0:1ed39951ab7b 238 {
Viktor5 15:77a116408c4b 239 if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) //se sono uguali vale 0
Viktor5 15:77a116408c4b 240 { //ho ricevuto un pong
GregCr 3:8b9e2a4df4b5 241 led = !led;
Viktor5 15:77a116408c4b 242 debug( "Master RX...Pong\r\n" );
mluis 14:ab6eecd44b7a 243 // Send the next PING frame
GregCr 7:c1bbd6c56979 244 strcpy( ( char* )Buffer, ( char* )PingMsg );
mluis 14:ab6eecd44b7a 245 // We fill the buffer with numbers for the payload
GregCr 0:1ed39951ab7b 246 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 247 {
GregCr 0:1ed39951ab7b 248 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 249 }
Viktor5 15:77a116408c4b 250 wait_ms( waitTime );
Viktor5 15:77a116408c4b 251 Radio.Send( Buffer, BufferSize ); //mando il ping
GregCr 0:1ed39951ab7b 252 }
GregCr 2:59e108728d71 253 else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
GregCr 0:1ed39951ab7b 254 { // A master already exists then become a slave
Viktor5 15:77a116408c4b 255 debug( "Master RX...Ping\r\n" ); //ho ricevuto un ping
GregCr 3:8b9e2a4df4b5 256 led = !led;
GregCr 0:1ed39951ab7b 257 isMaster = false;
mluis 14:ab6eecd44b7a 258 // Send the next PONG frame
GregCr 7:c1bbd6c56979 259 strcpy( ( char* )Buffer, ( char* )PongMsg );
mluis 14:ab6eecd44b7a 260 // We fill the buffer with numbers for the payload
GregCr 2:59e108728d71 261 for( i = 4; i < BufferSize; i++ )
GregCr 2:59e108728d71 262 {
GregCr 2:59e108728d71 263 Buffer[i] = i - 4;
GregCr 2:59e108728d71 264 }
Viktor5 15:77a116408c4b 265 wait_ms( waitTime );
GregCr 2:59e108728d71 266 Radio.Send( Buffer, BufferSize );
GregCr 2:59e108728d71 267 }
GregCr 2:59e108728d71 268 else // valid reception but neither a PING or a PONG message
GregCr 2:59e108728d71 269 { // Set device as master ans start again
GregCr 2:59e108728d71 270 isMaster = true;
Viktor5 15:77a116408c4b 271 Radio.Rx( RX_TIMEOUT_VALUE ); //re non ho ricevuto niente timeout
mluis 14:ab6eecd44b7a 272 }
GregCr 0:1ed39951ab7b 273 }
GregCr 0:1ed39951ab7b 274 }
Viktor5 15:77a116408c4b 275 else //s sono lo slave
GregCr 0:1ed39951ab7b 276 {
GregCr 0:1ed39951ab7b 277 if( BufferSize > 0 )
GregCr 0:1ed39951ab7b 278 {
GregCr 0:1ed39951ab7b 279 if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
GregCr 0:1ed39951ab7b 280 {
GregCr 3:8b9e2a4df4b5 281 led = !led;
Viktor5 15:77a116408c4b 282 debug( "Slave RX...Ping\r\n" );
GregCr 2:59e108728d71 283 // Send the reply to the PING string
GregCr 7:c1bbd6c56979 284 strcpy( ( char* )Buffer, ( char* )PongMsg );
mluis 14:ab6eecd44b7a 285 // We fill the buffer with numbers for the payload
GregCr 0:1ed39951ab7b 286 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 287 {
GregCr 0:1ed39951ab7b 288 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 289 }
Viktor5 15:77a116408c4b 290 wait_ms( waitTime );
Viktor5 15:77a116408c4b 291 Radio.Send( Buffer, BufferSize ); //trasmetto un pong
GregCr 0:1ed39951ab7b 292 }
GregCr 2:59e108728d71 293 else // valid reception but not a PING as expected
GregCr 2:59e108728d71 294 { // Set device as master and start again
Viktor5 15:77a116408c4b 295 isMaster = true; //divento master
GregCr 2:59e108728d71 296 Radio.Rx( RX_TIMEOUT_VALUE );
mluis 14:ab6eecd44b7a 297 }
GregCr 0:1ed39951ab7b 298 }
GregCr 0:1ed39951ab7b 299 }
Viktor5 15:77a116408c4b 300 State = LOWPOWER; //vado nello stato lowpower
GregCr 0:1ed39951ab7b 301 break;
mluis 14:ab6eecd44b7a 302 case TX:
mluis 14:ab6eecd44b7a 303 led = !led;
GregCr 2:59e108728d71 304 if( isMaster == true )
GregCr 0:1ed39951ab7b 305 {
Viktor5 15:77a116408c4b 306 debug( "Master TX Ping...\r\n" );
GregCr 0:1ed39951ab7b 307 }
GregCr 0:1ed39951ab7b 308 else
GregCr 0:1ed39951ab7b 309 {
Viktor5 15:77a116408c4b 310 debug( "Slave TX Pong...\r\n" );
GregCr 0:1ed39951ab7b 311 }
GregCr 0:1ed39951ab7b 312 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 313 State = LOWPOWER;
GregCr 0:1ed39951ab7b 314 break;
GregCr 0:1ed39951ab7b 315 case RX_TIMEOUT:
GregCr 0:1ed39951ab7b 316 if( isMaster == true )
GregCr 0:1ed39951ab7b 317 {
GregCr 0:1ed39951ab7b 318 // Send the next PING frame
GregCr 7:c1bbd6c56979 319 strcpy( ( char* )Buffer, ( char* )PingMsg );
GregCr 0:1ed39951ab7b 320 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 321 {
GregCr 0:1ed39951ab7b 322 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 323 }
Viktor5 15:77a116408c4b 324 wait_ms( waitTime );
GregCr 0:1ed39951ab7b 325 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 326 }
GregCr 0:1ed39951ab7b 327 else
GregCr 0:1ed39951ab7b 328 {
mluis 14:ab6eecd44b7a 329 Radio.Rx( RX_TIMEOUT_VALUE );
mluis 14:ab6eecd44b7a 330 }
GregCr 0:1ed39951ab7b 331 State = LOWPOWER;
GregCr 0:1ed39951ab7b 332 break;
GregCr 0:1ed39951ab7b 333 case RX_ERROR:
GregCr 7:c1bbd6c56979 334 // We have received a Packet with a CRC error, send reply as if packet was correct
GregCr 0:1ed39951ab7b 335 if( isMaster == true )
GregCr 0:1ed39951ab7b 336 {
GregCr 0:1ed39951ab7b 337 // Send the next PING frame
GregCr 7:c1bbd6c56979 338 strcpy( ( char* )Buffer, ( char* )PingMsg );
GregCr 0:1ed39951ab7b 339 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 340 {
GregCr 0:1ed39951ab7b 341 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 342 }
Viktor5 15:77a116408c4b 343 wait_ms( waitTime );
GregCr 0:1ed39951ab7b 344 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 345 }
GregCr 0:1ed39951ab7b 346 else
GregCr 0:1ed39951ab7b 347 {
GregCr 2:59e108728d71 348 // Send the next PONG frame
GregCr 7:c1bbd6c56979 349 strcpy( ( char* )Buffer, ( char* )PongMsg );
GregCr 2:59e108728d71 350 for( i = 4; i < BufferSize; i++ )
GregCr 2:59e108728d71 351 {
GregCr 2:59e108728d71 352 Buffer[i] = i - 4;
GregCr 2:59e108728d71 353 }
Viktor5 15:77a116408c4b 354 wait_ms( waitTime );
GregCr 2:59e108728d71 355 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 356 }
GregCr 0:1ed39951ab7b 357 State = LOWPOWER;
GregCr 0:1ed39951ab7b 358 break;
GregCr 0:1ed39951ab7b 359 case TX_TIMEOUT:
GregCr 0:1ed39951ab7b 360 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 361 State = LOWPOWER;
GregCr 0:1ed39951ab7b 362 break;
GregCr 0:1ed39951ab7b 363 case LOWPOWER:
GregCr 0:1ed39951ab7b 364 break;
GregCr 0:1ed39951ab7b 365 default:
GregCr 0:1ed39951ab7b 366 State = LOWPOWER;
GregCr 0:1ed39951ab7b 367 break;
mluis 14:ab6eecd44b7a 368 }
Viktor5 15:77a116408c4b 369 }*/
GregCr 0:1ed39951ab7b 370 }
GregCr 0:1ed39951ab7b 371
GregCr 0:1ed39951ab7b 372 void OnTxDone( void )
GregCr 0:1ed39951ab7b 373 {
GregCr 5:f2431c4fe3bb 374 Radio.Sleep( );
GregCr 0:1ed39951ab7b 375 State = TX;
Viktor5 15:77a116408c4b 376 //oktx=1;
GregCr 7:c1bbd6c56979 377 debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
Viktor5 15:77a116408c4b 378 t.stop();
Viktor5 15:77a116408c4b 379
Viktor5 15:77a116408c4b 380 tmisura=t.read();
Viktor5 15:77a116408c4b 381 somma+=tmisura;
Viktor5 15:77a116408c4b 382 t.reset();
Viktor5 15:77a116408c4b 383
Viktor5 15:77a116408c4b 384 debug("Tempo impiegato\r\n");
Viktor5 15:77a116408c4b 385 sprintf(tempo, "%f", tmisura);
Viktor5 15:77a116408c4b 386 debug(tempo);
Viktor5 15:77a116408c4b 387 debug("\r\n\n\n");
Viktor5 15:77a116408c4b 388
Viktor5 15:77a116408c4b 389 wait (10);
Viktor5 15:77a116408c4b 390
Viktor5 15:77a116408c4b 391 Radio.Send( Buffer, BufferSize ); //send
Viktor5 15:77a116408c4b 392
GregCr 0:1ed39951ab7b 393 }
GregCr 0:1ed39951ab7b 394
mluis 14:ab6eecd44b7a 395 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
GregCr 0:1ed39951ab7b 396 {
GregCr 0:1ed39951ab7b 397 Radio.Sleep( );
GregCr 0:1ed39951ab7b 398 BufferSize = size;
GregCr 0:1ed39951ab7b 399 memcpy( Buffer, payload, BufferSize );
GregCr 0:1ed39951ab7b 400 RssiValue = rssi;
GregCr 0:1ed39951ab7b 401 SnrValue = snr;
GregCr 0:1ed39951ab7b 402 State = RX;
GregCr 7:c1bbd6c56979 403 debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
Viktor5 15:77a116408c4b 404 oktx=1;
GregCr 0:1ed39951ab7b 405 }
GregCr 0:1ed39951ab7b 406
GregCr 0:1ed39951ab7b 407 void OnTxTimeout( void )
GregCr 0:1ed39951ab7b 408 {
GregCr 0:1ed39951ab7b 409 Radio.Sleep( );
GregCr 0:1ed39951ab7b 410 State = TX_TIMEOUT;
GregCr 7:c1bbd6c56979 411 debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
GregCr 0:1ed39951ab7b 412 }
GregCr 0:1ed39951ab7b 413
GregCr 0:1ed39951ab7b 414 void OnRxTimeout( void )
GregCr 0:1ed39951ab7b 415 {
GregCr 0:1ed39951ab7b 416 Radio.Sleep( );
mluis 14:ab6eecd44b7a 417 Buffer[BufferSize] = 0;
GregCr 0:1ed39951ab7b 418 State = RX_TIMEOUT;
GregCr 7:c1bbd6c56979 419 debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
GregCr 0:1ed39951ab7b 420 }
GregCr 0:1ed39951ab7b 421
GregCr 0:1ed39951ab7b 422 void OnRxError( void )
GregCr 0:1ed39951ab7b 423 {
GregCr 0:1ed39951ab7b 424 Radio.Sleep( );
GregCr 0:1ed39951ab7b 425 State = RX_ERROR;
GregCr 7:c1bbd6c56979 426 debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
GregCr 0:1ed39951ab7b 427 }