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