Vincenzo Canfora
/
SX1272PingPong
per emilio
Fork of SX1272PingPong by
main.cpp@14:c918b4f661ca, 2017-03-14 (annotated)
- Committer:
- cenzino91
- Date:
- Tue Mar 14 13:14:50 2017 +0000
- Revision:
- 14:c918b4f661ca
- Parent:
- 13:edb9b443c1dd
per emilio
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 | |
GregCr | 5:f2431c4fe3bb | 13 | #define RF_FREQUENCY 868000000 // Hz |
cenzino91 | 14:c918b4f661ca | 14 | #define TX_OUTPUT_POWER 20 // 14 dBm |
GregCr | 0:1ed39951ab7b | 15 | |
GregCr | 0:1ed39951ab7b | 16 | #if USE_MODEM_LORA == 1 |
GregCr | 0:1ed39951ab7b | 17 | |
GregCr | 1:126d70d374f6 | 18 | #define LORA_BANDWIDTH 2 // [0: 125 kHz, |
GregCr | 0:1ed39951ab7b | 19 | // 1: 250 kHz, |
GregCr | 0:1ed39951ab7b | 20 | // 2: 500 kHz, |
GregCr | 0:1ed39951ab7b | 21 | // 3: Reserved] |
GregCr | 0:1ed39951ab7b | 22 | #define LORA_SPREADING_FACTOR 7 // [SF7..SF12] |
GregCr | 0:1ed39951ab7b | 23 | #define LORA_CODINGRATE 1 // [1: 4/5, |
GregCr | 0:1ed39951ab7b | 24 | // 2: 4/6, |
GregCr | 0:1ed39951ab7b | 25 | // 3: 4/7, |
GregCr | 0:1ed39951ab7b | 26 | // 4: 4/8] |
GregCr | 0:1ed39951ab7b | 27 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
GregCr | 0:1ed39951ab7b | 28 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
GregCr | 0:1ed39951ab7b | 29 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
GregCr | 3:8b9e2a4df4b5 | 30 | #define LORA_FHSS_ENABLED false |
GregCr | 3:8b9e2a4df4b5 | 31 | #define LORA_NB_SYMB_HOP 4 |
GregCr | 0:1ed39951ab7b | 32 | #define LORA_IQ_INVERSION_ON false |
GregCr | 3:8b9e2a4df4b5 | 33 | #define LORA_CRC_ENABLED true |
GregCr | 3:8b9e2a4df4b5 | 34 | |
GregCr | 0:1ed39951ab7b | 35 | #elif USE_MODEM_FSK == 1 |
GregCr | 0:1ed39951ab7b | 36 | |
GregCr | 2:59e108728d71 | 37 | #define FSK_FDEV 25000 // Hz |
GregCr | 2:59e108728d71 | 38 | #define FSK_DATARATE 19200 // bps |
GregCr | 2:59e108728d71 | 39 | #define FSK_BANDWIDTH 50000 // Hz |
GregCr | 2:59e108728d71 | 40 | #define FSK_AFC_BANDWIDTH 83333 // Hz |
GregCr | 0:1ed39951ab7b | 41 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx |
GregCr | 0:1ed39951ab7b | 42 | #define FSK_FIX_LENGTH_PAYLOAD_ON false |
GregCr | 3:8b9e2a4df4b5 | 43 | #define FSK_CRC_ENABLED true |
GregCr | 3:8b9e2a4df4b5 | 44 | |
GregCr | 0:1ed39951ab7b | 45 | #else |
GregCr | 0:1ed39951ab7b | 46 | #error "Please define a modem in the compiler options." |
GregCr | 0:1ed39951ab7b | 47 | #endif |
GregCr | 0:1ed39951ab7b | 48 | |
GregCr | 7:c1bbd6c56979 | 49 | #define RX_TIMEOUT_VALUE 3500000 // in us |
GregCr | 0:1ed39951ab7b | 50 | #define BUFFER_SIZE 32 // Define the payload size here |
GregCr | 0:1ed39951ab7b | 51 | |
GregCr | 8:f956dee63a56 | 52 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
GregCr | 3:8b9e2a4df4b5 | 53 | DigitalOut led(LED2); |
GregCr | 3:8b9e2a4df4b5 | 54 | #else |
GregCr | 3:8b9e2a4df4b5 | 55 | DigitalOut led(LED1); |
GregCr | 3:8b9e2a4df4b5 | 56 | #endif |
GregCr | 3:8b9e2a4df4b5 | 57 | |
cenzino91 | 14:c918b4f661ca | 58 | Serial pc(USBTX, USBRX); |
cenzino91 | 14:c918b4f661ca | 59 | |
cenzino91 | 14:c918b4f661ca | 60 | |
cenzino91 | 14:c918b4f661ca | 61 | |
GregCr | 0:1ed39951ab7b | 62 | /* |
GregCr | 0:1ed39951ab7b | 63 | * Global variables declarations |
GregCr | 0:1ed39951ab7b | 64 | */ |
mluis | 10:7af820d1e1df | 65 | typedef enum |
mluis | 10:7af820d1e1df | 66 | { |
mluis | 10:7af820d1e1df | 67 | LOWPOWER = 0, |
mluis | 10:7af820d1e1df | 68 | IDLE, |
mluis | 10:7af820d1e1df | 69 | |
mluis | 10:7af820d1e1df | 70 | RX, |
mluis | 10:7af820d1e1df | 71 | RX_TIMEOUT, |
mluis | 10:7af820d1e1df | 72 | RX_ERROR, |
mluis | 10:7af820d1e1df | 73 | |
mluis | 10:7af820d1e1df | 74 | TX, |
mluis | 10:7af820d1e1df | 75 | TX_TIMEOUT, |
mluis | 10:7af820d1e1df | 76 | |
mluis | 10:7af820d1e1df | 77 | CAD, |
mluis | 10:7af820d1e1df | 78 | CAD_DONE |
mluis | 10:7af820d1e1df | 79 | }AppStates_t; |
GregCr | 0:1ed39951ab7b | 80 | |
mluis | 10:7af820d1e1df | 81 | volatile AppStates_t State = LOWPOWER; |
mluis | 10:7af820d1e1df | 82 | |
mluis | 10:7af820d1e1df | 83 | /*! |
mluis | 10:7af820d1e1df | 84 | * Radio events function pointer |
mluis | 10:7af820d1e1df | 85 | */ |
mluis | 10:7af820d1e1df | 86 | static RadioEvents_t RadioEvents; |
mluis | 10:7af820d1e1df | 87 | |
mluis | 10:7af820d1e1df | 88 | /* |
mluis | 10:7af820d1e1df | 89 | * Global variables declarations |
mluis | 10:7af820d1e1df | 90 | */ |
GregCr | 13:edb9b443c1dd | 91 | SX1272MB2xAS Radio( NULL ); |
GregCr | 0:1ed39951ab7b | 92 | |
GregCr | 0:1ed39951ab7b | 93 | const uint8_t PingMsg[] = "PING"; |
GregCr | 0:1ed39951ab7b | 94 | const uint8_t PongMsg[] = "PONG"; |
GregCr | 0:1ed39951ab7b | 95 | |
GregCr | 0:1ed39951ab7b | 96 | uint16_t BufferSize = BUFFER_SIZE; |
GregCr | 0:1ed39951ab7b | 97 | uint8_t Buffer[BUFFER_SIZE]; |
GregCr | 5:f2431c4fe3bb | 98 | int16_t RssiValue = 0.0; |
GregCr | 5:f2431c4fe3bb | 99 | int8_t SnrValue = 0.0; |
GregCr | 0:1ed39951ab7b | 100 | |
GregCr | 0:1ed39951ab7b | 101 | int main() |
GregCr | 0:1ed39951ab7b | 102 | { |
GregCr | 0:1ed39951ab7b | 103 | uint8_t i; |
GregCr | 0:1ed39951ab7b | 104 | bool isMaster = true; |
GregCr | 0:1ed39951ab7b | 105 | |
GregCr | 13:edb9b443c1dd | 106 | debug( "\n\n\r SX1272 Ping Pong Demo Application \n\n\r" ); |
mluis | 10:7af820d1e1df | 107 | |
mluis | 10:7af820d1e1df | 108 | // Initialize Radio driver |
mluis | 10:7af820d1e1df | 109 | RadioEvents.TxDone = OnTxDone; |
mluis | 10:7af820d1e1df | 110 | RadioEvents.RxDone = OnRxDone; |
mluis | 10:7af820d1e1df | 111 | RadioEvents.RxError = OnRxError; |
mluis | 10:7af820d1e1df | 112 | RadioEvents.TxTimeout = OnTxTimeout; |
mluis | 10:7af820d1e1df | 113 | RadioEvents.RxTimeout = OnRxTimeout; |
mluis | 10:7af820d1e1df | 114 | Radio.Init( &RadioEvents ); |
GregCr | 0:1ed39951ab7b | 115 | |
GregCr | 7:c1bbd6c56979 | 116 | // verify the connection with the board |
GregCr | 7:c1bbd6c56979 | 117 | while( Radio.Read( REG_VERSION ) == 0x00 ) |
GregCr | 2:59e108728d71 | 118 | { |
GregCr | 7:c1bbd6c56979 | 119 | debug( "Radio could not be detected!\n\r", NULL ); |
GregCr | 7:c1bbd6c56979 | 120 | wait( 1 ); |
GregCr | 2:59e108728d71 | 121 | } |
GregCr | 7:c1bbd6c56979 | 122 | |
GregCr | 13:edb9b443c1dd | 123 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" ); |
GregCr | 13:edb9b443c1dd | 124 | |
GregCr | 0:1ed39951ab7b | 125 | Radio.SetChannel( RF_FREQUENCY ); |
GregCr | 0:1ed39951ab7b | 126 | |
GregCr | 0:1ed39951ab7b | 127 | #if USE_MODEM_LORA == 1 |
GregCr | 3:8b9e2a4df4b5 | 128 | |
GregCr | 7:c1bbd6c56979 | 129 | debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); |
GregCr | 7:c1bbd6c56979 | 130 | debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); |
GregCr | 7:c1bbd6c56979 | 131 | |
GregCr | 0:1ed39951ab7b | 132 | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
GregCr | 0:1ed39951ab7b | 133 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
GregCr | 0:1ed39951ab7b | 134 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
GregCr | 3:8b9e2a4df4b5 | 135 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
GregCr | 7:c1bbd6c56979 | 136 | LORA_IQ_INVERSION_ON, 2000000 ); |
GregCr | 0:1ed39951ab7b | 137 | |
GregCr | 0:1ed39951ab7b | 138 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
GregCr | 0:1ed39951ab7b | 139 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
mluis | 9:e764990e45df | 140 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
GregCr | 3:8b9e2a4df4b5 | 141 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
GregCr | 3:8b9e2a4df4b5 | 142 | LORA_IQ_INVERSION_ON, true ); |
GregCr | 0:1ed39951ab7b | 143 | |
GregCr | 0:1ed39951ab7b | 144 | #elif USE_MODEM_FSK == 1 |
GregCr | 0:1ed39951ab7b | 145 | |
GregCr | 7:c1bbd6c56979 | 146 | debug("\n\n\r > FSK Mode < \n\n\r"); |
GregCr | 0:1ed39951ab7b | 147 | Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
GregCr | 0:1ed39951ab7b | 148 | FSK_DATARATE, 0, |
GregCr | 0:1ed39951ab7b | 149 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
GregCr | 7:c1bbd6c56979 | 150 | FSK_CRC_ENABLED, 0, 0, 0, 2000000 ); |
GregCr | 0:1ed39951ab7b | 151 | |
GregCr | 0:1ed39951ab7b | 152 | Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
GregCr | 0:1ed39951ab7b | 153 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
mluis | 9:e764990e45df | 154 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
GregCr | 3:8b9e2a4df4b5 | 155 | 0, 0, false, true ); |
GregCr | 0:1ed39951ab7b | 156 | |
GregCr | 0:1ed39951ab7b | 157 | #else |
GregCr | 0:1ed39951ab7b | 158 | |
GregCr | 0:1ed39951ab7b | 159 | #error "Please define a modem in the compiler options." |
GregCr | 0:1ed39951ab7b | 160 | |
GregCr | 0:1ed39951ab7b | 161 | #endif |
GregCr | 1:126d70d374f6 | 162 | |
GregCr | 7:c1bbd6c56979 | 163 | debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); |
GregCr | 0:1ed39951ab7b | 164 | |
GregCr | 3:8b9e2a4df4b5 | 165 | led = 0; |
GregCr | 3:8b9e2a4df4b5 | 166 | |
GregCr | 0:1ed39951ab7b | 167 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 168 | |
GregCr | 0:1ed39951ab7b | 169 | while( 1 ) |
GregCr | 0:1ed39951ab7b | 170 | { |
GregCr | 0:1ed39951ab7b | 171 | switch( State ) |
GregCr | 0:1ed39951ab7b | 172 | { |
GregCr | 0:1ed39951ab7b | 173 | case RX: |
GregCr | 0:1ed39951ab7b | 174 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 175 | { |
GregCr | 0:1ed39951ab7b | 176 | if( BufferSize > 0 ) |
GregCr | 0:1ed39951ab7b | 177 | { |
GregCr | 0:1ed39951ab7b | 178 | if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 179 | { |
GregCr | 3:8b9e2a4df4b5 | 180 | led = !led; |
GregCr | 2:59e108728d71 | 181 | debug( "...Pong\r\n" ); |
GregCr | 0:1ed39951ab7b | 182 | // Send the next PING frame |
GregCr | 7:c1bbd6c56979 | 183 | strcpy( ( char* )Buffer, ( char* )PingMsg ); |
GregCr | 0:1ed39951ab7b | 184 | // We fill the buffer with numbers for the payload |
GregCr | 0:1ed39951ab7b | 185 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 186 | { |
GregCr | 0:1ed39951ab7b | 187 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 188 | } |
GregCr | 0:1ed39951ab7b | 189 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 190 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 191 | } |
GregCr | 2:59e108728d71 | 192 | else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 193 | { // A master already exists then become a slave |
GregCr | 2:59e108728d71 | 194 | debug( "...Ping\r\n" ); |
GregCr | 3:8b9e2a4df4b5 | 195 | led = !led; |
GregCr | 0:1ed39951ab7b | 196 | isMaster = false; |
GregCr | 7:c1bbd6c56979 | 197 | // Send the next PONG frame |
GregCr | 7:c1bbd6c56979 | 198 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
GregCr | 2:59e108728d71 | 199 | // We fill the buffer with numbers for the payload |
GregCr | 2:59e108728d71 | 200 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 2:59e108728d71 | 201 | { |
GregCr | 2:59e108728d71 | 202 | Buffer[i] = i - 4; |
GregCr | 2:59e108728d71 | 203 | } |
GregCr | 3:8b9e2a4df4b5 | 204 | wait_ms( 10 ); |
GregCr | 2:59e108728d71 | 205 | Radio.Send( Buffer, BufferSize ); |
GregCr | 2:59e108728d71 | 206 | } |
GregCr | 2:59e108728d71 | 207 | else // valid reception but neither a PING or a PONG message |
GregCr | 2:59e108728d71 | 208 | { // Set device as master ans start again |
GregCr | 2:59e108728d71 | 209 | isMaster = true; |
GregCr | 0:1ed39951ab7b | 210 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 2:59e108728d71 | 211 | } |
GregCr | 0:1ed39951ab7b | 212 | } |
GregCr | 0:1ed39951ab7b | 213 | } |
GregCr | 0:1ed39951ab7b | 214 | else |
GregCr | 0:1ed39951ab7b | 215 | { |
GregCr | 0:1ed39951ab7b | 216 | if( BufferSize > 0 ) |
GregCr | 0:1ed39951ab7b | 217 | { |
GregCr | 0:1ed39951ab7b | 218 | if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 219 | { |
GregCr | 3:8b9e2a4df4b5 | 220 | led = !led; |
GregCr | 2:59e108728d71 | 221 | debug( "...Ping\r\n" ); |
GregCr | 2:59e108728d71 | 222 | // Send the reply to the PING string |
GregCr | 7:c1bbd6c56979 | 223 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
GregCr | 0:1ed39951ab7b | 224 | // We fill the buffer with numbers for the payload |
GregCr | 0:1ed39951ab7b | 225 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 226 | { |
GregCr | 0:1ed39951ab7b | 227 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 228 | } |
GregCr | 0:1ed39951ab7b | 229 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 230 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 231 | } |
GregCr | 2:59e108728d71 | 232 | else // valid reception but not a PING as expected |
GregCr | 2:59e108728d71 | 233 | { // Set device as master and start again |
GregCr | 2:59e108728d71 | 234 | isMaster = true; |
GregCr | 2:59e108728d71 | 235 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 2:59e108728d71 | 236 | } |
GregCr | 0:1ed39951ab7b | 237 | } |
GregCr | 0:1ed39951ab7b | 238 | } |
GregCr | 0:1ed39951ab7b | 239 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 240 | break; |
GregCr | 3:8b9e2a4df4b5 | 241 | case TX: |
GregCr | 3:8b9e2a4df4b5 | 242 | led = !led; |
GregCr | 2:59e108728d71 | 243 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 244 | { |
GregCr | 2:59e108728d71 | 245 | debug( "Ping...\r\n" ); |
GregCr | 0:1ed39951ab7b | 246 | } |
GregCr | 0:1ed39951ab7b | 247 | else |
GregCr | 0:1ed39951ab7b | 248 | { |
GregCr | 2:59e108728d71 | 249 | debug( "Pong...\r\n" ); |
GregCr | 0:1ed39951ab7b | 250 | } |
GregCr | 0:1ed39951ab7b | 251 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 252 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 253 | break; |
GregCr | 0:1ed39951ab7b | 254 | case RX_TIMEOUT: |
GregCr | 0:1ed39951ab7b | 255 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 256 | { |
GregCr | 0:1ed39951ab7b | 257 | // Send the next PING frame |
GregCr | 7:c1bbd6c56979 | 258 | strcpy( ( char* )Buffer, ( char* )PingMsg ); |
GregCr | 0:1ed39951ab7b | 259 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 260 | { |
GregCr | 0:1ed39951ab7b | 261 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 262 | } |
GregCr | 0:1ed39951ab7b | 263 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 264 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 265 | } |
GregCr | 0:1ed39951ab7b | 266 | else |
GregCr | 0:1ed39951ab7b | 267 | { |
GregCr | 0:1ed39951ab7b | 268 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 269 | } |
GregCr | 0:1ed39951ab7b | 270 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 271 | break; |
GregCr | 0:1ed39951ab7b | 272 | case RX_ERROR: |
GregCr | 7:c1bbd6c56979 | 273 | // We have received a Packet with a CRC error, send reply as if packet was correct |
GregCr | 0:1ed39951ab7b | 274 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 275 | { |
GregCr | 0:1ed39951ab7b | 276 | // Send the next PING frame |
GregCr | 7:c1bbd6c56979 | 277 | strcpy( ( char* )Buffer, ( char* )PingMsg ); |
GregCr | 0:1ed39951ab7b | 278 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 279 | { |
GregCr | 0:1ed39951ab7b | 280 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 281 | } |
GregCr | 0:1ed39951ab7b | 282 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 283 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 284 | } |
GregCr | 0:1ed39951ab7b | 285 | else |
GregCr | 0:1ed39951ab7b | 286 | { |
GregCr | 2:59e108728d71 | 287 | // Send the next PONG frame |
GregCr | 7:c1bbd6c56979 | 288 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
GregCr | 2:59e108728d71 | 289 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 2:59e108728d71 | 290 | { |
GregCr | 2:59e108728d71 | 291 | Buffer[i] = i - 4; |
GregCr | 2:59e108728d71 | 292 | } |
GregCr | 2:59e108728d71 | 293 | wait_ms( 10 ); |
GregCr | 2:59e108728d71 | 294 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 295 | } |
GregCr | 0:1ed39951ab7b | 296 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 297 | break; |
GregCr | 0:1ed39951ab7b | 298 | case TX_TIMEOUT: |
GregCr | 0:1ed39951ab7b | 299 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 300 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 301 | break; |
GregCr | 0:1ed39951ab7b | 302 | case LOWPOWER: |
GregCr | 0:1ed39951ab7b | 303 | break; |
GregCr | 0:1ed39951ab7b | 304 | default: |
GregCr | 0:1ed39951ab7b | 305 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 306 | break; |
GregCr | 0:1ed39951ab7b | 307 | } |
GregCr | 0:1ed39951ab7b | 308 | } |
GregCr | 0:1ed39951ab7b | 309 | } |
GregCr | 0:1ed39951ab7b | 310 | |
GregCr | 0:1ed39951ab7b | 311 | void OnTxDone( void ) |
GregCr | 0:1ed39951ab7b | 312 | { |
GregCr | 5:f2431c4fe3bb | 313 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 314 | State = TX; |
GregCr | 7:c1bbd6c56979 | 315 | debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 316 | } |
GregCr | 0:1ed39951ab7b | 317 | |
GregCr | 4:5ece30264cd9 | 318 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
GregCr | 0:1ed39951ab7b | 319 | { |
GregCr | 0:1ed39951ab7b | 320 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 321 | BufferSize = size; |
GregCr | 0:1ed39951ab7b | 322 | memcpy( Buffer, payload, BufferSize ); |
GregCr | 0:1ed39951ab7b | 323 | RssiValue = rssi; |
GregCr | 0:1ed39951ab7b | 324 | SnrValue = snr; |
GregCr | 0:1ed39951ab7b | 325 | State = RX; |
cenzino91 | 14:c918b4f661ca | 326 | pc.printf("Rssi: %d dbm\n\r",RssiValue); |
GregCr | 7:c1bbd6c56979 | 327 | debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 328 | } |
GregCr | 0:1ed39951ab7b | 329 | |
GregCr | 0:1ed39951ab7b | 330 | void OnTxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 331 | { |
GregCr | 0:1ed39951ab7b | 332 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 333 | State = TX_TIMEOUT; |
GregCr | 7:c1bbd6c56979 | 334 | debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 335 | } |
GregCr | 0:1ed39951ab7b | 336 | |
GregCr | 0:1ed39951ab7b | 337 | void OnRxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 338 | { |
GregCr | 0:1ed39951ab7b | 339 | Radio.Sleep( ); |
GregCr | 1:126d70d374f6 | 340 | Buffer[ BufferSize ] = 0; |
GregCr | 0:1ed39951ab7b | 341 | State = RX_TIMEOUT; |
GregCr | 7:c1bbd6c56979 | 342 | debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 343 | } |
GregCr | 0:1ed39951ab7b | 344 | |
GregCr | 0:1ed39951ab7b | 345 | void OnRxError( void ) |
GregCr | 0:1ed39951ab7b | 346 | { |
GregCr | 0:1ed39951ab7b | 347 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 348 | State = RX_ERROR; |
GregCr | 7:c1bbd6c56979 | 349 | debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); |
GregCr | 0:1ed39951ab7b | 350 | } |
GregCr | 3:8b9e2a4df4b5 | 351 |