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