Garn E
/
TestMisuraTx
Fork of SX1272PingPong by
main.cpp@16:0cf0cb508f19, 2018-06-01 (annotated)
- Committer:
- Viktor5
- Date:
- Fri Jun 01 09:39:00 2018 +0000
- Revision:
- 16:0cf0cb508f19
- Parent:
- 15:77a116408c4b
Who changed what in which revision?
User | Revision | Line number | New 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 | 16:0cf0cb508f19 | 188 | for(int count=0; count<misure; count++) |
Viktor5 | 16:0cf0cb508f19 | 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 | 16:0cf0cb508f19 | 203 | while (button1==1) |
Viktor5 | 16:0cf0cb508f19 | 204 | { |
Viktor5 | 16:0cf0cb508f19 | 205 | wait_ms (100); |
Viktor5 | 16:0cf0cb508f19 | 206 | } |
Viktor5 | 15:77a116408c4b | 207 | debug( "Start TX\r\n" ); |
Viktor5 | 15:77a116408c4b | 208 | t.start(); |
Viktor5 | 15:77a116408c4b | 209 | Radio.Send( Buffer, BufferSize ); //send |
Viktor5 | 15:77a116408c4b | 210 | |
Viktor5 | 15:77a116408c4b | 211 | //debug ("End TX\r\n"); |
Viktor5 | 15:77a116408c4b | 212 | |
Viktor5 | 15:77a116408c4b | 213 | //t.stop(); |
Viktor5 | 16:0cf0cb508f19 | 214 | //wait_ms( 10000 ); |
Viktor5 | 15:77a116408c4b | 215 | /*tmisura=t.read(); |
Viktor5 | 15:77a116408c4b | 216 | somma+=tmisura; |
Viktor5 | 15:77a116408c4b | 217 | t.reset(); |
Viktor5 | 15:77a116408c4b | 218 | |
Viktor5 | 15:77a116408c4b | 219 | |
Viktor5 | 15:77a116408c4b | 220 | debug("Tempo impiegato\r\n"); |
Viktor5 | 15:77a116408c4b | 221 | sprintf(tempo, "%f", tmisura); |
Viktor5 | 15:77a116408c4b | 222 | debug(tempo); |
Viktor5 | 15:77a116408c4b | 223 | debug("\r\n\n\n");*/ |
Viktor5 | 15:77a116408c4b | 224 | |
Viktor5 | 15:77a116408c4b | 225 | |
Viktor5 | 16:0cf0cb508f19 | 226 | } |
Viktor5 | 15:77a116408c4b | 227 | |
Viktor5 | 15:77a116408c4b | 228 | somma=somma/misure; |
Viktor5 | 15:77a116408c4b | 229 | debug("Tempo medio di TX\r\n"); |
Viktor5 | 15:77a116408c4b | 230 | sprintf(tempo, "%f", somma); |
Viktor5 | 15:77a116408c4b | 231 | debug(tempo); |
Viktor5 | 15:77a116408c4b | 232 | debug("\r\n\n\n"); |
Viktor5 | 15:77a116408c4b | 233 | |
Viktor5 | 15:77a116408c4b | 234 | |
Viktor5 | 15:77a116408c4b | 235 | /* switch( State ) |
GregCr | 0:1ed39951ab7b | 236 | { |
GregCr | 0:1ed39951ab7b | 237 | case RX: |
GregCr | 0:1ed39951ab7b | 238 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 239 | { |
GregCr | 0:1ed39951ab7b | 240 | if( BufferSize > 0 ) |
GregCr | 0:1ed39951ab7b | 241 | { |
Viktor5 | 15:77a116408c4b | 242 | if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) //se sono uguali vale 0 |
Viktor5 | 15:77a116408c4b | 243 | { //ho ricevuto un pong |
GregCr | 3:8b9e2a4df4b5 | 244 | led = !led; |
Viktor5 | 15:77a116408c4b | 245 | debug( "Master RX...Pong\r\n" ); |
mluis | 14:ab6eecd44b7a | 246 | // Send the next PING frame |
GregCr | 7:c1bbd6c56979 | 247 | strcpy( ( char* )Buffer, ( char* )PingMsg ); |
mluis | 14:ab6eecd44b7a | 248 | // We fill the buffer with numbers for the payload |
GregCr | 0:1ed39951ab7b | 249 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 250 | { |
GregCr | 0:1ed39951ab7b | 251 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 252 | } |
Viktor5 | 15:77a116408c4b | 253 | wait_ms( waitTime ); |
Viktor5 | 15:77a116408c4b | 254 | Radio.Send( Buffer, BufferSize ); //mando il ping |
GregCr | 0:1ed39951ab7b | 255 | } |
GregCr | 2:59e108728d71 | 256 | else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 257 | { // A master already exists then become a slave |
Viktor5 | 15:77a116408c4b | 258 | debug( "Master RX...Ping\r\n" ); //ho ricevuto un ping |
GregCr | 3:8b9e2a4df4b5 | 259 | led = !led; |
GregCr | 0:1ed39951ab7b | 260 | isMaster = false; |
mluis | 14:ab6eecd44b7a | 261 | // Send the next PONG frame |
GregCr | 7:c1bbd6c56979 | 262 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
mluis | 14:ab6eecd44b7a | 263 | // We fill the buffer with numbers for the payload |
GregCr | 2:59e108728d71 | 264 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 2:59e108728d71 | 265 | { |
GregCr | 2:59e108728d71 | 266 | Buffer[i] = i - 4; |
GregCr | 2:59e108728d71 | 267 | } |
Viktor5 | 15:77a116408c4b | 268 | wait_ms( waitTime ); |
GregCr | 2:59e108728d71 | 269 | Radio.Send( Buffer, BufferSize ); |
GregCr | 2:59e108728d71 | 270 | } |
GregCr | 2:59e108728d71 | 271 | else // valid reception but neither a PING or a PONG message |
GregCr | 2:59e108728d71 | 272 | { // Set device as master ans start again |
GregCr | 2:59e108728d71 | 273 | isMaster = true; |
Viktor5 | 15:77a116408c4b | 274 | Radio.Rx( RX_TIMEOUT_VALUE ); //re non ho ricevuto niente timeout |
mluis | 14:ab6eecd44b7a | 275 | } |
GregCr | 0:1ed39951ab7b | 276 | } |
GregCr | 0:1ed39951ab7b | 277 | } |
Viktor5 | 15:77a116408c4b | 278 | else //s sono lo slave |
GregCr | 0:1ed39951ab7b | 279 | { |
GregCr | 0:1ed39951ab7b | 280 | if( BufferSize > 0 ) |
GregCr | 0:1ed39951ab7b | 281 | { |
GregCr | 0:1ed39951ab7b | 282 | if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 283 | { |
GregCr | 3:8b9e2a4df4b5 | 284 | led = !led; |
Viktor5 | 15:77a116408c4b | 285 | debug( "Slave RX...Ping\r\n" ); |
GregCr | 2:59e108728d71 | 286 | // Send the reply to the PING string |
GregCr | 7:c1bbd6c56979 | 287 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
mluis | 14:ab6eecd44b7a | 288 | // We fill the buffer with numbers for the payload |
GregCr | 0:1ed39951ab7b | 289 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 290 | { |
GregCr | 0:1ed39951ab7b | 291 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 292 | } |
Viktor5 | 15:77a116408c4b | 293 | wait_ms( waitTime ); |
Viktor5 | 15:77a116408c4b | 294 | Radio.Send( Buffer, BufferSize ); //trasmetto un pong |
GregCr | 0:1ed39951ab7b | 295 | } |
GregCr | 2:59e108728d71 | 296 | else // valid reception but not a PING as expected |
GregCr | 2:59e108728d71 | 297 | { // Set device as master and start again |
Viktor5 | 15:77a116408c4b | 298 | isMaster = true; //divento master |
GregCr | 2:59e108728d71 | 299 | Radio.Rx( RX_TIMEOUT_VALUE ); |
mluis | 14:ab6eecd44b7a | 300 | } |
GregCr | 0:1ed39951ab7b | 301 | } |
GregCr | 0:1ed39951ab7b | 302 | } |
Viktor5 | 15:77a116408c4b | 303 | State = LOWPOWER; //vado nello stato lowpower |
GregCr | 0:1ed39951ab7b | 304 | break; |
mluis | 14:ab6eecd44b7a | 305 | case TX: |
mluis | 14:ab6eecd44b7a | 306 | led = !led; |
GregCr | 2:59e108728d71 | 307 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 308 | { |
Viktor5 | 15:77a116408c4b | 309 | debug( "Master TX Ping...\r\n" ); |
GregCr | 0:1ed39951ab7b | 310 | } |
GregCr | 0:1ed39951ab7b | 311 | else |
GregCr | 0:1ed39951ab7b | 312 | { |
Viktor5 | 15:77a116408c4b | 313 | debug( "Slave TX Pong...\r\n" ); |
GregCr | 0:1ed39951ab7b | 314 | } |
GregCr | 0:1ed39951ab7b | 315 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 316 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 317 | break; |
GregCr | 0:1ed39951ab7b | 318 | case RX_TIMEOUT: |
GregCr | 0:1ed39951ab7b | 319 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 320 | { |
GregCr | 0:1ed39951ab7b | 321 | // Send the next PING frame |
GregCr | 7:c1bbd6c56979 | 322 | strcpy( ( char* )Buffer, ( char* )PingMsg ); |
GregCr | 0:1ed39951ab7b | 323 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 324 | { |
GregCr | 0:1ed39951ab7b | 325 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 326 | } |
Viktor5 | 15:77a116408c4b | 327 | wait_ms( waitTime ); |
GregCr | 0:1ed39951ab7b | 328 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 329 | } |
GregCr | 0:1ed39951ab7b | 330 | else |
GregCr | 0:1ed39951ab7b | 331 | { |
mluis | 14:ab6eecd44b7a | 332 | Radio.Rx( RX_TIMEOUT_VALUE ); |
mluis | 14:ab6eecd44b7a | 333 | } |
GregCr | 0:1ed39951ab7b | 334 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 335 | break; |
GregCr | 0:1ed39951ab7b | 336 | case RX_ERROR: |
GregCr | 7:c1bbd6c56979 | 337 | // We have received a Packet with a CRC error, send reply as if packet was correct |
GregCr | 0:1ed39951ab7b | 338 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 339 | { |
GregCr | 0:1ed39951ab7b | 340 | // Send the next PING frame |
GregCr | 7:c1bbd6c56979 | 341 | strcpy( ( char* )Buffer, ( char* )PingMsg ); |
GregCr | 0:1ed39951ab7b | 342 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 343 | { |
GregCr | 0:1ed39951ab7b | 344 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 345 | } |
Viktor5 | 15:77a116408c4b | 346 | wait_ms( waitTime ); |
GregCr | 0:1ed39951ab7b | 347 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 348 | } |
GregCr | 0:1ed39951ab7b | 349 | else |
GregCr | 0:1ed39951ab7b | 350 | { |
GregCr | 2:59e108728d71 | 351 | // Send the next PONG frame |
GregCr | 7:c1bbd6c56979 | 352 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
GregCr | 2:59e108728d71 | 353 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 2:59e108728d71 | 354 | { |
GregCr | 2:59e108728d71 | 355 | Buffer[i] = i - 4; |
GregCr | 2:59e108728d71 | 356 | } |
Viktor5 | 15:77a116408c4b | 357 | wait_ms( waitTime ); |
GregCr | 2:59e108728d71 | 358 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 359 | } |
GregCr | 0:1ed39951ab7b | 360 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 361 | break; |
GregCr | 0:1ed39951ab7b | 362 | case TX_TIMEOUT: |
GregCr | 0:1ed39951ab7b | 363 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 364 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 365 | break; |
GregCr | 0:1ed39951ab7b | 366 | case LOWPOWER: |
GregCr | 0:1ed39951ab7b | 367 | break; |
GregCr | 0:1ed39951ab7b | 368 | default: |
GregCr | 0:1ed39951ab7b | 369 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 370 | break; |
mluis | 14:ab6eecd44b7a | 371 | } |
Viktor5 | 15:77a116408c4b | 372 | }*/ |
GregCr | 0:1ed39951ab7b | 373 | } |
GregCr | 0:1ed39951ab7b | 374 | |
GregCr | 0:1ed39951ab7b | 375 | void OnTxDone( void ) |
GregCr | 0:1ed39951ab7b | 376 | { |
GregCr | 5:f2431c4fe3bb | 377 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 378 | State = TX; |
Viktor5 | 15:77a116408c4b | 379 | //oktx=1; |
GregCr | 7:c1bbd6c56979 | 380 | debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); |
Viktor5 | 15:77a116408c4b | 381 | t.stop(); |
Viktor5 | 15:77a116408c4b | 382 | |
Viktor5 | 15:77a116408c4b | 383 | tmisura=t.read(); |
Viktor5 | 15:77a116408c4b | 384 | somma+=tmisura; |
Viktor5 | 15:77a116408c4b | 385 | t.reset(); |
Viktor5 | 15:77a116408c4b | 386 | |
Viktor5 | 15:77a116408c4b | 387 | debug("Tempo impiegato\r\n"); |
Viktor5 | 15:77a116408c4b | 388 | sprintf(tempo, "%f", tmisura); |
Viktor5 | 15:77a116408c4b | 389 | debug(tempo); |
Viktor5 | 15:77a116408c4b | 390 | debug("\r\n\n\n"); |
Viktor5 | 15:77a116408c4b | 391 | |
Viktor5 | 15:77a116408c4b | 392 | wait (10); |
Viktor5 | 15:77a116408c4b | 393 | |
Viktor5 | 16:0cf0cb508f19 | 394 | //Radio.Send( Buffer, BufferSize ); //send |
Viktor5 | 15:77a116408c4b | 395 | |
GregCr | 0:1ed39951ab7b | 396 | } |
GregCr | 0:1ed39951ab7b | 397 | |
mluis | 14:ab6eecd44b7a | 398 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) |
GregCr | 0:1ed39951ab7b | 399 | { |
GregCr | 0:1ed39951ab7b | 400 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 401 | BufferSize = size; |
GregCr | 0:1ed39951ab7b | 402 | memcpy( Buffer, payload, BufferSize ); |
GregCr | 0:1ed39951ab7b | 403 | RssiValue = rssi; |
GregCr | 0:1ed39951ab7b | 404 | SnrValue = snr; |
GregCr | 0:1ed39951ab7b | 405 | State = RX; |
GregCr | 7:c1bbd6c56979 | 406 | debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); |
Viktor5 | 15:77a116408c4b | 407 | oktx=1; |
GregCr | 0:1ed39951ab7b | 408 | } |
GregCr | 0:1ed39951ab7b | 409 | |
GregCr | 0:1ed39951ab7b | 410 | void OnTxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 411 | { |
GregCr | 0:1ed39951ab7b | 412 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 413 | State = TX_TIMEOUT; |
GregCr | 7:c1bbd6c56979 | 414 | debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 415 | } |
GregCr | 0:1ed39951ab7b | 416 | |
GregCr | 0:1ed39951ab7b | 417 | void OnRxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 418 | { |
GregCr | 0:1ed39951ab7b | 419 | Radio.Sleep( ); |
mluis | 14:ab6eecd44b7a | 420 | Buffer[BufferSize] = 0; |
GregCr | 0:1ed39951ab7b | 421 | State = RX_TIMEOUT; |
GregCr | 7:c1bbd6c56979 | 422 | debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 423 | } |
GregCr | 0:1ed39951ab7b | 424 | |
GregCr | 0:1ed39951ab7b | 425 | void OnRxError( void ) |
GregCr | 0:1ed39951ab7b | 426 | { |
GregCr | 0:1ed39951ab7b | 427 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 428 | State = RX_ERROR; |
GregCr | 7:c1bbd6c56979 | 429 | debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); |
GregCr | 0:1ed39951ab7b | 430 | } |