Simple example Ping Pong from Semtech, updated new lib
Fork of SX1276PingPong by
i'm using stm32 nucleo l46rg and stm32 nucleo l073 with sx1276mb1mas.
main.cpp@3:8b9e2a4df4b5, 2014-09-04 (annotated)
- Committer:
- GregCr
- Date:
- Thu Sep 04 14:04:52 2014 +0000
- Revision:
- 3:8b9e2a4df4b5
- Parent:
- 2:59e108728d71
- Child:
- 4:5ece30264cd9
Added support for FHSS
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 | 3:8b9e2a4df4b5 | 10 | #define USE_MODEM_LORA 1 |
GregCr | 0:1ed39951ab7b | 11 | #define USE_MODEM_FSK !USE_MODEM_LORA |
GregCr | 0:1ed39951ab7b | 12 | |
GregCr | 3:8b9e2a4df4b5 | 13 | #define RF_FREQUENCY 475000000 // 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 | 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 | 0:1ed39951ab7b | 49 | #define RX_TIMEOUT_VALUE 3000000 // in us |
GregCr | 0:1ed39951ab7b | 50 | #define BUFFER_SIZE 32 // Define the payload size here |
GregCr | 0:1ed39951ab7b | 51 | |
GregCr | 3:8b9e2a4df4b5 | 52 | #if( defined ( TARGET_KL25Z ) ) |
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 | |
GregCr | 0:1ed39951ab7b | 58 | /* |
GregCr | 0:1ed39951ab7b | 59 | * Callback functions prototypes |
GregCr | 0:1ed39951ab7b | 60 | */ |
GregCr | 0:1ed39951ab7b | 61 | /*! |
GregCr | 0:1ed39951ab7b | 62 | * @brief Function to be executed on Radio Tx Done event |
GregCr | 0:1ed39951ab7b | 63 | */ |
GregCr | 0:1ed39951ab7b | 64 | void OnTxDone( void ); |
GregCr | 0:1ed39951ab7b | 65 | |
GregCr | 0:1ed39951ab7b | 66 | /*! |
GregCr | 0:1ed39951ab7b | 67 | * @brief Function to be executed on Radio Rx Done event |
GregCr | 0:1ed39951ab7b | 68 | */ |
GregCr | 0:1ed39951ab7b | 69 | void OnRxDone( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr ); |
GregCr | 0:1ed39951ab7b | 70 | |
GregCr | 0:1ed39951ab7b | 71 | /*! |
GregCr | 0:1ed39951ab7b | 72 | * @brief Function executed on Radio Tx Timeout event |
GregCr | 0:1ed39951ab7b | 73 | */ |
GregCr | 0:1ed39951ab7b | 74 | void OnTxTimeout( void ); |
GregCr | 0:1ed39951ab7b | 75 | |
GregCr | 0:1ed39951ab7b | 76 | /*! |
GregCr | 0:1ed39951ab7b | 77 | * @brief Function executed on Radio Rx Timeout event |
GregCr | 0:1ed39951ab7b | 78 | */ |
GregCr | 0:1ed39951ab7b | 79 | void OnRxTimeout( void ); |
GregCr | 0:1ed39951ab7b | 80 | |
GregCr | 0:1ed39951ab7b | 81 | /*! |
GregCr | 0:1ed39951ab7b | 82 | * @brief Function executed on Radio Rx Error event |
GregCr | 0:1ed39951ab7b | 83 | */ |
GregCr | 0:1ed39951ab7b | 84 | void OnRxError( void ); |
GregCr | 0:1ed39951ab7b | 85 | |
GregCr | 3:8b9e2a4df4b5 | 86 | /*! |
GregCr | 3:8b9e2a4df4b5 | 87 | * @brief Function executed on Radio Fhss Change Channel event |
GregCr | 3:8b9e2a4df4b5 | 88 | */ |
GregCr | 3:8b9e2a4df4b5 | 89 | void OnFhssChangeChannel( uint8_t channelIndex ); |
GregCr | 3:8b9e2a4df4b5 | 90 | |
GregCr | 0:1ed39951ab7b | 91 | /* |
GregCr | 0:1ed39951ab7b | 92 | * Global variables declarations |
GregCr | 0:1ed39951ab7b | 93 | */ |
GregCr | 0:1ed39951ab7b | 94 | typedef RadioState States_t; |
GregCr | 0:1ed39951ab7b | 95 | |
GregCr | 0:1ed39951ab7b | 96 | /* |
GregCr | 0:1ed39951ab7b | 97 | * Global variables declarations |
GregCr | 0:1ed39951ab7b | 98 | */ |
GregCr | 3:8b9e2a4df4b5 | 99 | SX1276MB1xAS Radio( OnTxDone, OnTxTimeout, OnRxDone, OnRxTimeout, OnRxError, NULL ); |
GregCr | 0:1ed39951ab7b | 100 | |
GregCr | 0:1ed39951ab7b | 101 | const uint8_t PingMsg[] = "PING"; |
GregCr | 0:1ed39951ab7b | 102 | const uint8_t PongMsg[] = "PONG"; |
GregCr | 0:1ed39951ab7b | 103 | |
GregCr | 0:1ed39951ab7b | 104 | uint16_t BufferSize = BUFFER_SIZE; |
GregCr | 0:1ed39951ab7b | 105 | uint8_t Buffer[BUFFER_SIZE]; |
GregCr | 0:1ed39951ab7b | 106 | |
GregCr | 0:1ed39951ab7b | 107 | volatile States_t State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 108 | |
GregCr | 0:1ed39951ab7b | 109 | double RssiValue = 0.0; |
GregCr | 0:1ed39951ab7b | 110 | double SnrValue = 0.0; |
GregCr | 0:1ed39951ab7b | 111 | |
GregCr | 0:1ed39951ab7b | 112 | int main() |
GregCr | 0:1ed39951ab7b | 113 | { |
GregCr | 0:1ed39951ab7b | 114 | uint8_t i; |
GregCr | 0:1ed39951ab7b | 115 | bool isMaster = true; |
GregCr | 0:1ed39951ab7b | 116 | |
GregCr | 1:126d70d374f6 | 117 | debug( "\n\r\n\r SX1276 Ping Pong Demo Application \n\r" ); |
GregCr | 0:1ed39951ab7b | 118 | |
GregCr | 0:1ed39951ab7b | 119 | #if defined TARGET_NUCLEO_L152RE |
GregCr | 0:1ed39951ab7b | 120 | debug( DEBUG_MESSAGE, " > Nucleo-L152RE Platform <\r\n" ); |
GregCr | 0:1ed39951ab7b | 121 | #elif defined TARGET_KL25Z |
GregCr | 0:1ed39951ab7b | 122 | debug( DEBUG_MESSAGE, " > KL25Z Platform <\r\n" ); |
GregCr | 0:1ed39951ab7b | 123 | #elif defined TARGET_LPC11U6X |
GregCr | 0:1ed39951ab7b | 124 | debug( DEBUG_MESSAGE, " > LPC11U6X Platform <\r\n" ); |
GregCr | 0:1ed39951ab7b | 125 | #else |
GregCr | 0:1ed39951ab7b | 126 | debug( DEBUG_MESSAGE, " > Untested Platform <\r\n" ); |
GregCr | 0:1ed39951ab7b | 127 | #endif |
GregCr | 0:1ed39951ab7b | 128 | |
GregCr | 2:59e108728d71 | 129 | if( Radio.DetectBoardType( ) == SX1276MB1LAS ) |
GregCr | 2:59e108728d71 | 130 | { |
GregCr | 2:59e108728d71 | 131 | debug( DEBUG_MESSAGE, "\n\r > Board Type: SX1276MB1LAS < \n\r" ); |
GregCr | 2:59e108728d71 | 132 | } |
GregCr | 2:59e108728d71 | 133 | else |
GregCr | 2:59e108728d71 | 134 | { |
GregCr | 2:59e108728d71 | 135 | debug( DEBUG_MESSAGE, "\n\r > Board Type: SX1276MB1MAS < \n\r" ); |
GregCr | 2:59e108728d71 | 136 | } |
GregCr | 2:59e108728d71 | 137 | |
GregCr | 2:59e108728d71 | 138 | debug( DEBUG_MESSAGE, " > Chipset Version = 0x%x < \n\r", Radio.Read( REG_VERSION ) ); |
GregCr | 1:126d70d374f6 | 139 | |
GregCr | 0:1ed39951ab7b | 140 | Radio.SetChannel( RF_FREQUENCY ); |
GregCr | 0:1ed39951ab7b | 141 | |
GregCr | 0:1ed39951ab7b | 142 | #if USE_MODEM_LORA == 1 |
GregCr | 3:8b9e2a4df4b5 | 143 | |
GregCr | 3:8b9e2a4df4b5 | 144 | if( LORA_FHSS_ENABLED == true ) |
GregCr | 3:8b9e2a4df4b5 | 145 | { |
GregCr | 3:8b9e2a4df4b5 | 146 | debug("\n\r\n\r > LORA FHSS Mode < \n\r\n\r"); |
GregCr | 3:8b9e2a4df4b5 | 147 | } |
GregCr | 3:8b9e2a4df4b5 | 148 | else |
GregCr | 3:8b9e2a4df4b5 | 149 | { |
GregCr | 3:8b9e2a4df4b5 | 150 | debug("\n\r\n\r > LORA Mode < \n\r\n\r"); |
GregCr | 3:8b9e2a4df4b5 | 151 | } |
GregCr | 0:1ed39951ab7b | 152 | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
GregCr | 0:1ed39951ab7b | 153 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
GregCr | 0:1ed39951ab7b | 154 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
GregCr | 3:8b9e2a4df4b5 | 155 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
GregCr | 3:8b9e2a4df4b5 | 156 | LORA_IQ_INVERSION_ON, 3000000 ); |
GregCr | 0:1ed39951ab7b | 157 | |
GregCr | 0:1ed39951ab7b | 158 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
GregCr | 0:1ed39951ab7b | 159 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
GregCr | 0:1ed39951ab7b | 160 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, |
GregCr | 3:8b9e2a4df4b5 | 161 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
GregCr | 3:8b9e2a4df4b5 | 162 | LORA_IQ_INVERSION_ON, true ); |
GregCr | 0:1ed39951ab7b | 163 | |
GregCr | 0:1ed39951ab7b | 164 | #elif USE_MODEM_FSK == 1 |
GregCr | 0:1ed39951ab7b | 165 | |
GregCr | 0:1ed39951ab7b | 166 | debug("\n\r\n\r > FSK Mode < \n\r\n\r"); |
GregCr | 0:1ed39951ab7b | 167 | Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
GregCr | 0:1ed39951ab7b | 168 | FSK_DATARATE, 0, |
GregCr | 0:1ed39951ab7b | 169 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
GregCr | 3:8b9e2a4df4b5 | 170 | FSK_CRC_ENABLED, 0, 0, 0, 3000000 ); |
GregCr | 0:1ed39951ab7b | 171 | |
GregCr | 0:1ed39951ab7b | 172 | Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
GregCr | 0:1ed39951ab7b | 173 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
GregCr | 3:8b9e2a4df4b5 | 174 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, FSK_CRC_ENABLED, |
GregCr | 3:8b9e2a4df4b5 | 175 | 0, 0, false, true ); |
GregCr | 0:1ed39951ab7b | 176 | |
GregCr | 0:1ed39951ab7b | 177 | #else |
GregCr | 0:1ed39951ab7b | 178 | |
GregCr | 0:1ed39951ab7b | 179 | #error "Please define a modem in the compiler options." |
GregCr | 0:1ed39951ab7b | 180 | |
GregCr | 0:1ed39951ab7b | 181 | #endif |
GregCr | 1:126d70d374f6 | 182 | |
GregCr | 1:126d70d374f6 | 183 | debug( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); |
GregCr | 0:1ed39951ab7b | 184 | |
GregCr | 3:8b9e2a4df4b5 | 185 | led = 0; |
GregCr | 3:8b9e2a4df4b5 | 186 | |
GregCr | 0:1ed39951ab7b | 187 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 188 | |
GregCr | 0:1ed39951ab7b | 189 | while( 1 ) |
GregCr | 0:1ed39951ab7b | 190 | { |
GregCr | 0:1ed39951ab7b | 191 | switch( State ) |
GregCr | 0:1ed39951ab7b | 192 | { |
GregCr | 0:1ed39951ab7b | 193 | case RX: |
GregCr | 0:1ed39951ab7b | 194 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 195 | { |
GregCr | 0:1ed39951ab7b | 196 | if( BufferSize > 0 ) |
GregCr | 0:1ed39951ab7b | 197 | { |
GregCr | 0:1ed39951ab7b | 198 | if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 199 | { |
GregCr | 3:8b9e2a4df4b5 | 200 | led = !led; |
GregCr | 2:59e108728d71 | 201 | debug( "...Pong\r\n" ); |
GregCr | 0:1ed39951ab7b | 202 | // Send the next PING frame |
GregCr | 0:1ed39951ab7b | 203 | Buffer[0] = 'P'; |
GregCr | 0:1ed39951ab7b | 204 | Buffer[1] = 'I'; |
GregCr | 0:1ed39951ab7b | 205 | Buffer[2] = 'N'; |
GregCr | 0:1ed39951ab7b | 206 | Buffer[3] = 'G'; |
GregCr | 0:1ed39951ab7b | 207 | // We fill the buffer with numbers for the payload |
GregCr | 0:1ed39951ab7b | 208 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 209 | { |
GregCr | 0:1ed39951ab7b | 210 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 211 | } |
GregCr | 0:1ed39951ab7b | 212 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 213 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 214 | } |
GregCr | 2:59e108728d71 | 215 | else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 216 | { // A master already exists then become a slave |
GregCr | 2:59e108728d71 | 217 | debug( "...Ping\r\n" ); |
GregCr | 3:8b9e2a4df4b5 | 218 | led = !led; |
GregCr | 0:1ed39951ab7b | 219 | isMaster = false; |
GregCr | 2:59e108728d71 | 220 | // Send the next PING frame |
GregCr | 2:59e108728d71 | 221 | Buffer[0] = 'P'; |
GregCr | 2:59e108728d71 | 222 | Buffer[1] = 'O'; |
GregCr | 2:59e108728d71 | 223 | Buffer[2] = 'N'; |
GregCr | 2:59e108728d71 | 224 | Buffer[3] = 'G'; |
GregCr | 2:59e108728d71 | 225 | // We fill the buffer with numbers for the payload |
GregCr | 2:59e108728d71 | 226 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 2:59e108728d71 | 227 | { |
GregCr | 2:59e108728d71 | 228 | Buffer[i] = i - 4; |
GregCr | 2:59e108728d71 | 229 | } |
GregCr | 3:8b9e2a4df4b5 | 230 | wait_ms( 10 ); |
GregCr | 2:59e108728d71 | 231 | Radio.Send( Buffer, BufferSize ); |
GregCr | 2:59e108728d71 | 232 | } |
GregCr | 2:59e108728d71 | 233 | else // valid reception but neither a PING or a PONG message |
GregCr | 2:59e108728d71 | 234 | { // Set device as master ans start again |
GregCr | 2:59e108728d71 | 235 | isMaster = true; |
GregCr | 0:1ed39951ab7b | 236 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 2:59e108728d71 | 237 | } |
GregCr | 0:1ed39951ab7b | 238 | } |
GregCr | 0:1ed39951ab7b | 239 | } |
GregCr | 0:1ed39951ab7b | 240 | else |
GregCr | 0:1ed39951ab7b | 241 | { |
GregCr | 0:1ed39951ab7b | 242 | if( BufferSize > 0 ) |
GregCr | 0:1ed39951ab7b | 243 | { |
GregCr | 0:1ed39951ab7b | 244 | if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 245 | { |
GregCr | 3:8b9e2a4df4b5 | 246 | led = !led; |
GregCr | 2:59e108728d71 | 247 | debug( "...Ping\r\n" ); |
GregCr | 2:59e108728d71 | 248 | // Send the reply to the PING string |
GregCr | 0:1ed39951ab7b | 249 | Buffer[0] = 'P'; |
GregCr | 0:1ed39951ab7b | 250 | Buffer[1] = 'O'; |
GregCr | 0:1ed39951ab7b | 251 | Buffer[2] = 'N'; |
GregCr | 0:1ed39951ab7b | 252 | Buffer[3] = 'G'; |
GregCr | 0:1ed39951ab7b | 253 | // We fill the buffer with numbers for the payload |
GregCr | 0:1ed39951ab7b | 254 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 255 | { |
GregCr | 0:1ed39951ab7b | 256 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 257 | } |
GregCr | 0:1ed39951ab7b | 258 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 259 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 260 | } |
GregCr | 2:59e108728d71 | 261 | else // valid reception but not a PING as expected |
GregCr | 2:59e108728d71 | 262 | { // Set device as master and start again |
GregCr | 2:59e108728d71 | 263 | isMaster = true; |
GregCr | 2:59e108728d71 | 264 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 2:59e108728d71 | 265 | } |
GregCr | 0:1ed39951ab7b | 266 | } |
GregCr | 0:1ed39951ab7b | 267 | } |
GregCr | 0:1ed39951ab7b | 268 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 269 | break; |
GregCr | 3:8b9e2a4df4b5 | 270 | case TX: |
GregCr | 3:8b9e2a4df4b5 | 271 | led = !led; |
GregCr | 2:59e108728d71 | 272 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 273 | { |
GregCr | 2:59e108728d71 | 274 | debug( "Ping...\r\n" ); |
GregCr | 0:1ed39951ab7b | 275 | } |
GregCr | 0:1ed39951ab7b | 276 | else |
GregCr | 0:1ed39951ab7b | 277 | { |
GregCr | 2:59e108728d71 | 278 | debug( "Pong...\r\n" ); |
GregCr | 0:1ed39951ab7b | 279 | } |
GregCr | 0:1ed39951ab7b | 280 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 281 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 282 | break; |
GregCr | 0:1ed39951ab7b | 283 | case RX_TIMEOUT: |
GregCr | 0:1ed39951ab7b | 284 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 285 | { |
GregCr | 0:1ed39951ab7b | 286 | // Send the next PING frame |
GregCr | 0:1ed39951ab7b | 287 | Buffer[0] = 'P'; |
GregCr | 0:1ed39951ab7b | 288 | Buffer[1] = 'I'; |
GregCr | 0:1ed39951ab7b | 289 | Buffer[2] = 'N'; |
GregCr | 0:1ed39951ab7b | 290 | Buffer[3] = 'G'; |
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 ); |
GregCr | 0:1ed39951ab7b | 296 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 297 | } |
GregCr | 0:1ed39951ab7b | 298 | else |
GregCr | 0:1ed39951ab7b | 299 | { |
GregCr | 0:1ed39951ab7b | 300 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 301 | } |
GregCr | 0:1ed39951ab7b | 302 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 303 | break; |
GregCr | 0:1ed39951ab7b | 304 | case RX_ERROR: |
GregCr | 0:1ed39951ab7b | 305 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 306 | { |
GregCr | 0:1ed39951ab7b | 307 | // Send the next PING frame |
GregCr | 0:1ed39951ab7b | 308 | Buffer[0] = 'P'; |
GregCr | 0:1ed39951ab7b | 309 | Buffer[1] = 'I'; |
GregCr | 0:1ed39951ab7b | 310 | Buffer[2] = 'N'; |
GregCr | 0:1ed39951ab7b | 311 | Buffer[3] = 'G'; |
GregCr | 0:1ed39951ab7b | 312 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 313 | { |
GregCr | 0:1ed39951ab7b | 314 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 315 | } |
GregCr | 0:1ed39951ab7b | 316 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 317 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 318 | } |
GregCr | 0:1ed39951ab7b | 319 | else |
GregCr | 0:1ed39951ab7b | 320 | { |
GregCr | 2:59e108728d71 | 321 | // Send the next PONG frame |
GregCr | 2:59e108728d71 | 322 | Buffer[0] = 'P'; |
GregCr | 2:59e108728d71 | 323 | Buffer[1] = 'O'; |
GregCr | 2:59e108728d71 | 324 | Buffer[2] = 'N'; |
GregCr | 2:59e108728d71 | 325 | Buffer[3] = 'G'; |
GregCr | 2:59e108728d71 | 326 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 2:59e108728d71 | 327 | { |
GregCr | 2:59e108728d71 | 328 | Buffer[i] = i - 4; |
GregCr | 2:59e108728d71 | 329 | } |
GregCr | 2:59e108728d71 | 330 | wait_ms( 10 ); |
GregCr | 2:59e108728d71 | 331 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 332 | } |
GregCr | 0:1ed39951ab7b | 333 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 334 | break; |
GregCr | 0:1ed39951ab7b | 335 | case TX_TIMEOUT: |
GregCr | 0:1ed39951ab7b | 336 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 337 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 338 | break; |
GregCr | 0:1ed39951ab7b | 339 | case LOWPOWER: |
GregCr | 0:1ed39951ab7b | 340 | break; |
GregCr | 0:1ed39951ab7b | 341 | default: |
GregCr | 0:1ed39951ab7b | 342 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 343 | break; |
GregCr | 0:1ed39951ab7b | 344 | } |
GregCr | 0:1ed39951ab7b | 345 | } |
GregCr | 0:1ed39951ab7b | 346 | } |
GregCr | 0:1ed39951ab7b | 347 | |
GregCr | 0:1ed39951ab7b | 348 | void OnTxDone( void ) |
GregCr | 0:1ed39951ab7b | 349 | { |
GregCr | 2:59e108728d71 | 350 | debug( DEBUG_MESSAGE, "> OnTxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 351 | State = TX; |
GregCr | 0:1ed39951ab7b | 352 | } |
GregCr | 0:1ed39951ab7b | 353 | |
GregCr | 0:1ed39951ab7b | 354 | void OnRxDone( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr) |
GregCr | 0:1ed39951ab7b | 355 | { |
GregCr | 2:59e108728d71 | 356 | debug( DEBUG_MESSAGE, "> OnRxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 357 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 358 | BufferSize = size; |
GregCr | 0:1ed39951ab7b | 359 | memcpy( Buffer, payload, BufferSize ); |
GregCr | 0:1ed39951ab7b | 360 | RssiValue = rssi; |
GregCr | 0:1ed39951ab7b | 361 | SnrValue = snr; |
GregCr | 0:1ed39951ab7b | 362 | State = RX; |
GregCr | 0:1ed39951ab7b | 363 | } |
GregCr | 0:1ed39951ab7b | 364 | |
GregCr | 0:1ed39951ab7b | 365 | void OnTxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 366 | { |
GregCr | 2:59e108728d71 | 367 | debug( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 368 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 369 | State = TX_TIMEOUT; |
GregCr | 0:1ed39951ab7b | 370 | } |
GregCr | 0:1ed39951ab7b | 371 | |
GregCr | 0:1ed39951ab7b | 372 | void OnRxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 373 | { |
GregCr | 2:59e108728d71 | 374 | debug( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 375 | Radio.Sleep( ); |
GregCr | 1:126d70d374f6 | 376 | Buffer[ BufferSize ] = 0; |
GregCr | 0:1ed39951ab7b | 377 | State = RX_TIMEOUT; |
GregCr | 0:1ed39951ab7b | 378 | } |
GregCr | 0:1ed39951ab7b | 379 | |
GregCr | 0:1ed39951ab7b | 380 | void OnRxError( void ) |
GregCr | 0:1ed39951ab7b | 381 | { |
GregCr | 2:59e108728d71 | 382 | debug( DEBUG_MESSAGE, "> OnRxError\n\r" ); |
GregCr | 0:1ed39951ab7b | 383 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 384 | State = RX_ERROR; |
GregCr | 0:1ed39951ab7b | 385 | } |
GregCr | 3:8b9e2a4df4b5 | 386 |