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