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