Vincenzo Canfora
/
SX1272PingPong
per emilio
Fork of SX1272PingPong by
main.cpp@2:59e108728d71, 2014-08-21 (annotated)
- Committer:
- GregCr
- Date:
- Thu Aug 21 11:58:56 2014 +0000
- Revision:
- 2:59e108728d71
- Parent:
- 1:126d70d374f6
- Child:
- 3:8b9e2a4df4b5
Release Candidate V1.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GregCr | 0:1ed39951ab7b | 1 | #include "mbed.h" |
GregCr | 0:1ed39951ab7b | 2 | #include "sx1276-hal.h" |
GregCr | 0:1ed39951ab7b | 3 | #include "debug.h" |
GregCr | 0:1ed39951ab7b | 4 | |
GregCr | 0:1ed39951ab7b | 5 | /* Set this flag to '1' to display debug messages on the console */ |
GregCr | 0:1ed39951ab7b | 6 | #define DEBUG_MESSAGE 0 |
GregCr | 0:1ed39951ab7b | 7 | |
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 | 2:59e108728d71 | 10 | #define USE_MODEM_LORA 0 |
GregCr | 0:1ed39951ab7b | 11 | #define USE_MODEM_FSK !USE_MODEM_LORA |
GregCr | 0:1ed39951ab7b | 12 | |
GregCr | 2:59e108728d71 | 13 | #define RF_FREQUENCY 875000000 // Hz |
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 | |
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 | 0:1ed39951ab7b | 30 | #define LORA_IQ_INVERSION_ON false |
GregCr | 0:1ed39951ab7b | 31 | |
GregCr | 0:1ed39951ab7b | 32 | #elif USE_MODEM_FSK == 1 |
GregCr | 0:1ed39951ab7b | 33 | |
GregCr | 2:59e108728d71 | 34 | #define FSK_FDEV 25000 // Hz |
GregCr | 2:59e108728d71 | 35 | #define FSK_DATARATE 19200 // bps |
GregCr | 2:59e108728d71 | 36 | #define FSK_BANDWIDTH 50000 // Hz |
GregCr | 2:59e108728d71 | 37 | #define FSK_AFC_BANDWIDTH 83333 // Hz |
GregCr | 0:1ed39951ab7b | 38 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx |
GregCr | 0:1ed39951ab7b | 39 | #define FSK_FIX_LENGTH_PAYLOAD_ON false |
GregCr | 0:1ed39951ab7b | 40 | |
GregCr | 0:1ed39951ab7b | 41 | #else |
GregCr | 0:1ed39951ab7b | 42 | #error "Please define a modem in the compiler options." |
GregCr | 0:1ed39951ab7b | 43 | #endif |
GregCr | 0:1ed39951ab7b | 44 | |
GregCr | 0:1ed39951ab7b | 45 | #define RX_TIMEOUT_VALUE 3000000 // in us |
GregCr | 0:1ed39951ab7b | 46 | #define BUFFER_SIZE 32 // Define the payload size here |
GregCr | 0:1ed39951ab7b | 47 | |
GregCr | 0:1ed39951ab7b | 48 | /* |
GregCr | 0:1ed39951ab7b | 49 | * Callback functions prototypes |
GregCr | 0:1ed39951ab7b | 50 | */ |
GregCr | 0:1ed39951ab7b | 51 | /*! |
GregCr | 0:1ed39951ab7b | 52 | * @brief Function to be executed on Radio Tx Done event |
GregCr | 0:1ed39951ab7b | 53 | */ |
GregCr | 0:1ed39951ab7b | 54 | void OnTxDone( void ); |
GregCr | 0:1ed39951ab7b | 55 | |
GregCr | 0:1ed39951ab7b | 56 | /*! |
GregCr | 0:1ed39951ab7b | 57 | * @brief Function to be executed on Radio Rx Done event |
GregCr | 0:1ed39951ab7b | 58 | */ |
GregCr | 0:1ed39951ab7b | 59 | void OnRxDone( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr ); |
GregCr | 0:1ed39951ab7b | 60 | |
GregCr | 0:1ed39951ab7b | 61 | /*! |
GregCr | 0:1ed39951ab7b | 62 | * @brief Function executed on Radio Tx Timeout event |
GregCr | 0:1ed39951ab7b | 63 | */ |
GregCr | 0:1ed39951ab7b | 64 | void OnTxTimeout( void ); |
GregCr | 0:1ed39951ab7b | 65 | |
GregCr | 0:1ed39951ab7b | 66 | /*! |
GregCr | 0:1ed39951ab7b | 67 | * @brief Function executed on Radio Rx Timeout event |
GregCr | 0:1ed39951ab7b | 68 | */ |
GregCr | 0:1ed39951ab7b | 69 | void OnRxTimeout( void ); |
GregCr | 0:1ed39951ab7b | 70 | |
GregCr | 0:1ed39951ab7b | 71 | /*! |
GregCr | 0:1ed39951ab7b | 72 | * @brief Function executed on Radio Rx Error event |
GregCr | 0:1ed39951ab7b | 73 | */ |
GregCr | 0:1ed39951ab7b | 74 | void OnRxError( void ); |
GregCr | 0:1ed39951ab7b | 75 | |
GregCr | 0:1ed39951ab7b | 76 | /* |
GregCr | 0:1ed39951ab7b | 77 | * Global variables declarations |
GregCr | 0:1ed39951ab7b | 78 | */ |
GregCr | 0:1ed39951ab7b | 79 | typedef RadioState States_t; |
GregCr | 0:1ed39951ab7b | 80 | |
GregCr | 0:1ed39951ab7b | 81 | /* |
GregCr | 0:1ed39951ab7b | 82 | * Global variables declarations |
GregCr | 0:1ed39951ab7b | 83 | */ |
GregCr | 0:1ed39951ab7b | 84 | SX1276MB1xAS Radio( OnTxDone, OnTxTimeout, OnRxDone, OnRxTimeout, OnRxError ); |
GregCr | 0:1ed39951ab7b | 85 | |
GregCr | 0:1ed39951ab7b | 86 | const uint8_t PingMsg[] = "PING"; |
GregCr | 0:1ed39951ab7b | 87 | const uint8_t PongMsg[] = "PONG"; |
GregCr | 0:1ed39951ab7b | 88 | |
GregCr | 0:1ed39951ab7b | 89 | uint16_t BufferSize = BUFFER_SIZE; |
GregCr | 0:1ed39951ab7b | 90 | uint8_t Buffer[BUFFER_SIZE]; |
GregCr | 0:1ed39951ab7b | 91 | |
GregCr | 0:1ed39951ab7b | 92 | volatile States_t State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 93 | |
GregCr | 0:1ed39951ab7b | 94 | double RssiValue = 0.0; |
GregCr | 0:1ed39951ab7b | 95 | double SnrValue = 0.0; |
GregCr | 0:1ed39951ab7b | 96 | |
GregCr | 0:1ed39951ab7b | 97 | int main() |
GregCr | 0:1ed39951ab7b | 98 | { |
GregCr | 0:1ed39951ab7b | 99 | uint8_t i; |
GregCr | 0:1ed39951ab7b | 100 | bool isMaster = true; |
GregCr | 0:1ed39951ab7b | 101 | |
GregCr | 1:126d70d374f6 | 102 | debug( "\n\r\n\r SX1276 Ping Pong Demo Application \n\r" ); |
GregCr | 0:1ed39951ab7b | 103 | |
GregCr | 0:1ed39951ab7b | 104 | #if defined TARGET_NUCLEO_L152RE |
GregCr | 0:1ed39951ab7b | 105 | debug( DEBUG_MESSAGE, " > Nucleo-L152RE Platform <\r\n" ); |
GregCr | 0:1ed39951ab7b | 106 | #elif defined TARGET_KL25Z |
GregCr | 0:1ed39951ab7b | 107 | debug( DEBUG_MESSAGE, " > KL25Z Platform <\r\n" ); |
GregCr | 0:1ed39951ab7b | 108 | #elif defined TARGET_LPC11U6X |
GregCr | 0:1ed39951ab7b | 109 | debug( DEBUG_MESSAGE, " > LPC11U6X Platform <\r\n" ); |
GregCr | 0:1ed39951ab7b | 110 | #else |
GregCr | 0:1ed39951ab7b | 111 | debug( DEBUG_MESSAGE, " > Untested Platform <\r\n" ); |
GregCr | 0:1ed39951ab7b | 112 | #endif |
GregCr | 0:1ed39951ab7b | 113 | |
GregCr | 2:59e108728d71 | 114 | if( Radio.DetectBoardType( ) == SX1276MB1LAS ) |
GregCr | 2:59e108728d71 | 115 | { |
GregCr | 2:59e108728d71 | 116 | debug( DEBUG_MESSAGE, "\n\r > Board Type: SX1276MB1LAS < \n\r" ); |
GregCr | 2:59e108728d71 | 117 | } |
GregCr | 2:59e108728d71 | 118 | else |
GregCr | 2:59e108728d71 | 119 | { |
GregCr | 2:59e108728d71 | 120 | debug( DEBUG_MESSAGE, "\n\r > Board Type: SX1276MB1MAS < \n\r" ); |
GregCr | 2:59e108728d71 | 121 | } |
GregCr | 2:59e108728d71 | 122 | |
GregCr | 2:59e108728d71 | 123 | debug( DEBUG_MESSAGE, " > Chipset Version = 0x%x < \n\r", Radio.Read( REG_VERSION ) ); |
GregCr | 1:126d70d374f6 | 124 | |
GregCr | 0:1ed39951ab7b | 125 | Radio.SetChannel( RF_FREQUENCY ); |
GregCr | 0:1ed39951ab7b | 126 | |
GregCr | 0:1ed39951ab7b | 127 | #if USE_MODEM_LORA == 1 |
GregCr | 0:1ed39951ab7b | 128 | |
GregCr | 0:1ed39951ab7b | 129 | debug("\n\r\n\r > LORA Mode < \n\r\n\r"); |
GregCr | 0:1ed39951ab7b | 130 | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
GregCr | 0:1ed39951ab7b | 131 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
GregCr | 0:1ed39951ab7b | 132 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
GregCr | 0:1ed39951ab7b | 133 | true, LORA_IQ_INVERSION_ON, 3000000 ); |
GregCr | 0:1ed39951ab7b | 134 | |
GregCr | 0:1ed39951ab7b | 135 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
GregCr | 0:1ed39951ab7b | 136 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
GregCr | 0:1ed39951ab7b | 137 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, |
GregCr | 0:1ed39951ab7b | 138 | true, LORA_IQ_INVERSION_ON, true ); |
GregCr | 0:1ed39951ab7b | 139 | |
GregCr | 0:1ed39951ab7b | 140 | #elif USE_MODEM_FSK == 1 |
GregCr | 0:1ed39951ab7b | 141 | |
GregCr | 0:1ed39951ab7b | 142 | debug("\n\r\n\r > FSK Mode < \n\r\n\r"); |
GregCr | 0:1ed39951ab7b | 143 | Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
GregCr | 0:1ed39951ab7b | 144 | FSK_DATARATE, 0, |
GregCr | 0:1ed39951ab7b | 145 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
GregCr | 0:1ed39951ab7b | 146 | true, 0, 3000000 ); |
GregCr | 0:1ed39951ab7b | 147 | |
GregCr | 0:1ed39951ab7b | 148 | Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
GregCr | 0:1ed39951ab7b | 149 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
GregCr | 0:1ed39951ab7b | 150 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, true, |
GregCr | 0:1ed39951ab7b | 151 | false, true ); |
GregCr | 0:1ed39951ab7b | 152 | |
GregCr | 0:1ed39951ab7b | 153 | #else |
GregCr | 0:1ed39951ab7b | 154 | |
GregCr | 0:1ed39951ab7b | 155 | #error "Please define a modem in the compiler options." |
GregCr | 0:1ed39951ab7b | 156 | |
GregCr | 0:1ed39951ab7b | 157 | #endif |
GregCr | 1:126d70d374f6 | 158 | |
GregCr | 1:126d70d374f6 | 159 | debug( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); |
GregCr | 0:1ed39951ab7b | 160 | |
GregCr | 0:1ed39951ab7b | 161 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 162 | |
GregCr | 0:1ed39951ab7b | 163 | while( 1 ) |
GregCr | 0:1ed39951ab7b | 164 | { |
GregCr | 0:1ed39951ab7b | 165 | switch( State ) |
GregCr | 0:1ed39951ab7b | 166 | { |
GregCr | 0:1ed39951ab7b | 167 | case RX: |
GregCr | 0:1ed39951ab7b | 168 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 169 | { |
GregCr | 0:1ed39951ab7b | 170 | if( BufferSize > 0 ) |
GregCr | 0:1ed39951ab7b | 171 | { |
GregCr | 0:1ed39951ab7b | 172 | if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 173 | { |
GregCr | 2:59e108728d71 | 174 | debug( "...Pong\r\n" ); |
GregCr | 0:1ed39951ab7b | 175 | // Send the next PING frame |
GregCr | 0:1ed39951ab7b | 176 | Buffer[0] = 'P'; |
GregCr | 0:1ed39951ab7b | 177 | Buffer[1] = 'I'; |
GregCr | 0:1ed39951ab7b | 178 | Buffer[2] = 'N'; |
GregCr | 0:1ed39951ab7b | 179 | Buffer[3] = 'G'; |
GregCr | 0:1ed39951ab7b | 180 | // We fill the buffer with numbers for the payload |
GregCr | 0:1ed39951ab7b | 181 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 182 | { |
GregCr | 0:1ed39951ab7b | 183 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 184 | } |
GregCr | 0:1ed39951ab7b | 185 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 186 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 187 | } |
GregCr | 2:59e108728d71 | 188 | else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 189 | { // A master already exists then become a slave |
GregCr | 2:59e108728d71 | 190 | debug( "...Ping\r\n" ); |
GregCr | 0:1ed39951ab7b | 191 | isMaster = false; |
GregCr | 2:59e108728d71 | 192 | // Send the next PING frame |
GregCr | 2:59e108728d71 | 193 | Buffer[0] = 'P'; |
GregCr | 2:59e108728d71 | 194 | Buffer[1] = 'O'; |
GregCr | 2:59e108728d71 | 195 | Buffer[2] = 'N'; |
GregCr | 2:59e108728d71 | 196 | Buffer[3] = 'G'; |
GregCr | 2:59e108728d71 | 197 | // We fill the buffer with numbers for the payload |
GregCr | 2:59e108728d71 | 198 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 2:59e108728d71 | 199 | { |
GregCr | 2:59e108728d71 | 200 | Buffer[i] = i - 4; |
GregCr | 2:59e108728d71 | 201 | } |
GregCr | 2:59e108728d71 | 202 | // wait_ms( 10 ); |
GregCr | 2:59e108728d71 | 203 | Radio.Send( Buffer, BufferSize ); |
GregCr | 2:59e108728d71 | 204 | } |
GregCr | 2:59e108728d71 | 205 | else // valid reception but neither a PING or a PONG message |
GregCr | 2:59e108728d71 | 206 | { // Set device as master ans start again |
GregCr | 2:59e108728d71 | 207 | isMaster = true; |
GregCr | 0:1ed39951ab7b | 208 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 2:59e108728d71 | 209 | } |
GregCr | 0:1ed39951ab7b | 210 | } |
GregCr | 0:1ed39951ab7b | 211 | } |
GregCr | 0:1ed39951ab7b | 212 | else |
GregCr | 0:1ed39951ab7b | 213 | { |
GregCr | 0:1ed39951ab7b | 214 | if( BufferSize > 0 ) |
GregCr | 0:1ed39951ab7b | 215 | { |
GregCr | 0:1ed39951ab7b | 216 | if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 217 | { |
GregCr | 2:59e108728d71 | 218 | debug( "...Ping\r\n" ); |
GregCr | 2:59e108728d71 | 219 | // Send the reply to the PING string |
GregCr | 0:1ed39951ab7b | 220 | Buffer[0] = 'P'; |
GregCr | 0:1ed39951ab7b | 221 | Buffer[1] = 'O'; |
GregCr | 0:1ed39951ab7b | 222 | Buffer[2] = 'N'; |
GregCr | 0:1ed39951ab7b | 223 | Buffer[3] = 'G'; |
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 | 2:59e108728d71 | 241 | case TX: |
GregCr | 2:59e108728d71 | 242 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 243 | { |
GregCr | 2:59e108728d71 | 244 | debug( "Ping...\r\n" ); |
GregCr | 0:1ed39951ab7b | 245 | } |
GregCr | 0:1ed39951ab7b | 246 | else |
GregCr | 0:1ed39951ab7b | 247 | { |
GregCr | 2:59e108728d71 | 248 | debug( "Pong...\r\n" ); |
GregCr | 0:1ed39951ab7b | 249 | } |
GregCr | 0:1ed39951ab7b | 250 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 251 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 252 | break; |
GregCr | 0:1ed39951ab7b | 253 | case RX_TIMEOUT: |
GregCr | 0:1ed39951ab7b | 254 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 255 | { |
GregCr | 0:1ed39951ab7b | 256 | // Send the next PING frame |
GregCr | 0:1ed39951ab7b | 257 | Buffer[0] = 'P'; |
GregCr | 0:1ed39951ab7b | 258 | Buffer[1] = 'I'; |
GregCr | 0:1ed39951ab7b | 259 | Buffer[2] = 'N'; |
GregCr | 0:1ed39951ab7b | 260 | Buffer[3] = 'G'; |
GregCr | 0:1ed39951ab7b | 261 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 262 | { |
GregCr | 0:1ed39951ab7b | 263 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 264 | } |
GregCr | 0:1ed39951ab7b | 265 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 266 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 267 | } |
GregCr | 0:1ed39951ab7b | 268 | else |
GregCr | 0:1ed39951ab7b | 269 | { |
GregCr | 0:1ed39951ab7b | 270 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 271 | } |
GregCr | 0:1ed39951ab7b | 272 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 273 | break; |
GregCr | 0:1ed39951ab7b | 274 | case RX_ERROR: |
GregCr | 0:1ed39951ab7b | 275 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 276 | { |
GregCr | 0:1ed39951ab7b | 277 | // Send the next PING frame |
GregCr | 0:1ed39951ab7b | 278 | Buffer[0] = 'P'; |
GregCr | 0:1ed39951ab7b | 279 | Buffer[1] = 'I'; |
GregCr | 0:1ed39951ab7b | 280 | Buffer[2] = 'N'; |
GregCr | 0:1ed39951ab7b | 281 | Buffer[3] = 'G'; |
GregCr | 0:1ed39951ab7b | 282 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 283 | { |
GregCr | 0:1ed39951ab7b | 284 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 285 | } |
GregCr | 0:1ed39951ab7b | 286 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 287 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 288 | } |
GregCr | 0:1ed39951ab7b | 289 | else |
GregCr | 0:1ed39951ab7b | 290 | { |
GregCr | 2:59e108728d71 | 291 | // Send the next PONG frame |
GregCr | 2:59e108728d71 | 292 | Buffer[0] = 'P'; |
GregCr | 2:59e108728d71 | 293 | Buffer[1] = 'O'; |
GregCr | 2:59e108728d71 | 294 | Buffer[2] = 'N'; |
GregCr | 2:59e108728d71 | 295 | Buffer[3] = 'G'; |
GregCr | 2:59e108728d71 | 296 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 2:59e108728d71 | 297 | { |
GregCr | 2:59e108728d71 | 298 | Buffer[i] = i - 4; |
GregCr | 2:59e108728d71 | 299 | } |
GregCr | 2:59e108728d71 | 300 | wait_ms( 10 ); |
GregCr | 2:59e108728d71 | 301 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 302 | } |
GregCr | 0:1ed39951ab7b | 303 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 304 | break; |
GregCr | 0:1ed39951ab7b | 305 | case TX_TIMEOUT: |
GregCr | 0:1ed39951ab7b | 306 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 307 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 308 | break; |
GregCr | 0:1ed39951ab7b | 309 | case LOWPOWER: |
GregCr | 0:1ed39951ab7b | 310 | break; |
GregCr | 0:1ed39951ab7b | 311 | default: |
GregCr | 0:1ed39951ab7b | 312 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 313 | break; |
GregCr | 0:1ed39951ab7b | 314 | } |
GregCr | 0:1ed39951ab7b | 315 | } |
GregCr | 0:1ed39951ab7b | 316 | } |
GregCr | 0:1ed39951ab7b | 317 | |
GregCr | 0:1ed39951ab7b | 318 | void OnTxDone( void ) |
GregCr | 0:1ed39951ab7b | 319 | { |
GregCr | 2:59e108728d71 | 320 | debug( DEBUG_MESSAGE, "> OnTxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 321 | State = TX; |
GregCr | 0:1ed39951ab7b | 322 | } |
GregCr | 0:1ed39951ab7b | 323 | |
GregCr | 0:1ed39951ab7b | 324 | void OnRxDone( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr) |
GregCr | 0:1ed39951ab7b | 325 | { |
GregCr | 2:59e108728d71 | 326 | debug( DEBUG_MESSAGE, "> OnRxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 327 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 328 | BufferSize = size; |
GregCr | 0:1ed39951ab7b | 329 | memcpy( Buffer, payload, BufferSize ); |
GregCr | 0:1ed39951ab7b | 330 | RssiValue = rssi; |
GregCr | 0:1ed39951ab7b | 331 | SnrValue = snr; |
GregCr | 0:1ed39951ab7b | 332 | State = RX; |
GregCr | 0:1ed39951ab7b | 333 | } |
GregCr | 0:1ed39951ab7b | 334 | |
GregCr | 0:1ed39951ab7b | 335 | void OnTxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 336 | { |
GregCr | 2:59e108728d71 | 337 | debug( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 338 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 339 | State = TX_TIMEOUT; |
GregCr | 0:1ed39951ab7b | 340 | } |
GregCr | 0:1ed39951ab7b | 341 | |
GregCr | 0:1ed39951ab7b | 342 | void OnRxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 343 | { |
GregCr | 2:59e108728d71 | 344 | debug( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 345 | Radio.Sleep( ); |
GregCr | 1:126d70d374f6 | 346 | Buffer[ BufferSize ] = 0; |
GregCr | 0:1ed39951ab7b | 347 | State = RX_TIMEOUT; |
GregCr | 0:1ed39951ab7b | 348 | } |
GregCr | 0:1ed39951ab7b | 349 | |
GregCr | 0:1ed39951ab7b | 350 | void OnRxError( void ) |
GregCr | 0:1ed39951ab7b | 351 | { |
GregCr | 2:59e108728d71 | 352 | debug( DEBUG_MESSAGE, "> OnRxError\n\r" ); |
GregCr | 0:1ed39951ab7b | 353 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 354 | State = RX_ERROR; |
GregCr | 0:1ed39951ab7b | 355 | } |