![](/media/cache/profiles/LogoEMCU_EU_2.png.50x50_q85.jpg)
Demo for Semtech Lora SX1272MB2xAS 868MHz an NUCLEO-F401RE (STM Cortex M4)
Fork of SX1272PingPong by
main.cpp@14:62abee5e269d, 2016-03-20 (annotated)
- Committer:
- emcu
- Date:
- Sun Mar 20 00:07:29 2016 +0000
- Revision:
- 14:62abee5e269d
- Parent:
- 13:edb9b443c1dd
- Child:
- 15:3365ba2777d2
Demo for Semtech Lora SX1272MB2xAS 868MHz an NUCLEO-F401RE (STM Cortex M4)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emcu | 14:62abee5e269d | 1 | /* |
emcu | 14:62abee5e269d | 2 | |
emcu | 14:62abee5e269d | 3 | Demo for Semtech Lora SX1272MB2xAS 868MHz an NUCLEO-F401RE (STM Cortex M4) |
emcu | 14:62abee5e269d | 4 | |
emcu | 14:62abee5e269d | 5 | by www.emcu.it |
emcu | 14:62abee5e269d | 6 | |
emcu | 14:62abee5e269d | 7 | Date: Mar.2016 |
emcu | 14:62abee5e269d | 8 | Ver.: 1.0.0 |
emcu | 14:62abee5e269d | 9 | |
emcu | 14:62abee5e269d | 10 | Note: |
emcu | 14:62abee5e269d | 11 | The original project is here: https://developer.mbed.org/components/SX1272MB2xAS/ |
emcu | 14:62abee5e269d | 12 | is: SX1272 Ping Pong Demo Application |
emcu | 14:62abee5e269d | 13 | |
emcu | 14:62abee5e269d | 14 | How to use: |
emcu | 14:62abee5e269d | 15 | We tested this SW on NUCLEO-F401RE |
emcu | 14:62abee5e269d | 16 | Program two kits (NUCLEO-F401RE + SX1272MB2xAS) with this SW. |
emcu | 14:62abee5e269d | 17 | Connect the KITs to the PC and open a terminal emulation (for example TeraTerm). |
emcu | 14:62abee5e269d | 18 | The TeraTerm configurations are shown below. |
emcu | 14:62abee5e269d | 19 | 9600 bauds, 8-bit data, no parity |
emcu | 14:62abee5e269d | 20 | On the TeraTerm you see the link informations. |
emcu | 14:62abee5e269d | 21 | On the NUCLEO-F401RE, if all is OK, you must see the GREEN led that flashing . This means that the two KITs are working properly. |
emcu | 14:62abee5e269d | 22 | If the GREEN led flash slowly means that is not present the link between the two KITs. |
emcu | 14:62abee5e269d | 23 | |
emcu | 14:62abee5e269d | 24 | |
emcu | 14:62abee5e269d | 25 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
emcu | 14:62abee5e269d | 26 | and associated documentation files (the "Software"), to deal in the Software without |
emcu | 14:62abee5e269d | 27 | restriction, including without limitation the rights to use, copy, modify, merge, publish, |
emcu | 14:62abee5e269d | 28 | distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
emcu | 14:62abee5e269d | 29 | Software is furnished to do so, subject to the following conditions: |
emcu | 14:62abee5e269d | 30 | |
emcu | 14:62abee5e269d | 31 | The above copyright notice and this permission notice shall be included in all copies or |
emcu | 14:62abee5e269d | 32 | substantial portions of the Software. |
emcu | 14:62abee5e269d | 33 | |
emcu | 14:62abee5e269d | 34 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
emcu | 14:62abee5e269d | 35 | BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
emcu | 14:62abee5e269d | 36 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
emcu | 14:62abee5e269d | 37 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
emcu | 14:62abee5e269d | 38 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
emcu | 14:62abee5e269d | 39 | |
emcu | 14:62abee5e269d | 40 | |
emcu | 14:62abee5e269d | 41 | */ |
emcu | 14:62abee5e269d | 42 | |
GregCr | 0:1ed39951ab7b | 43 | #include "mbed.h" |
GregCr | 4:5ece30264cd9 | 44 | #include "main.h" |
GregCr | 13:edb9b443c1dd | 45 | #include "sx1272-hal.h" |
GregCr | 8:f956dee63a56 | 46 | #include "debug.h" |
GregCr | 0:1ed39951ab7b | 47 | |
GregCr | 0:1ed39951ab7b | 48 | /* Set this flag to '1' to display debug messages on the console */ |
GregCr | 13:edb9b443c1dd | 49 | #define DEBUG_MESSAGE 1 |
GregCr | 0:1ed39951ab7b | 50 | |
GregCr | 0:1ed39951ab7b | 51 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ |
GregCr | 5:f2431c4fe3bb | 52 | #define USE_MODEM_LORA 1 |
GregCr | 0:1ed39951ab7b | 53 | #define USE_MODEM_FSK !USE_MODEM_LORA |
GregCr | 0:1ed39951ab7b | 54 | |
GregCr | 5:f2431c4fe3bb | 55 | #define RF_FREQUENCY 868000000 // Hz |
GregCr | 0:1ed39951ab7b | 56 | #define TX_OUTPUT_POWER 14 // 14 dBm |
GregCr | 0:1ed39951ab7b | 57 | |
GregCr | 0:1ed39951ab7b | 58 | #if USE_MODEM_LORA == 1 |
GregCr | 0:1ed39951ab7b | 59 | |
GregCr | 1:126d70d374f6 | 60 | #define LORA_BANDWIDTH 2 // [0: 125 kHz, |
GregCr | 0:1ed39951ab7b | 61 | // 1: 250 kHz, |
GregCr | 0:1ed39951ab7b | 62 | // 2: 500 kHz, |
GregCr | 0:1ed39951ab7b | 63 | // 3: Reserved] |
GregCr | 0:1ed39951ab7b | 64 | #define LORA_SPREADING_FACTOR 7 // [SF7..SF12] |
GregCr | 0:1ed39951ab7b | 65 | #define LORA_CODINGRATE 1 // [1: 4/5, |
GregCr | 0:1ed39951ab7b | 66 | // 2: 4/6, |
GregCr | 0:1ed39951ab7b | 67 | // 3: 4/7, |
GregCr | 0:1ed39951ab7b | 68 | // 4: 4/8] |
GregCr | 0:1ed39951ab7b | 69 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
GregCr | 0:1ed39951ab7b | 70 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
GregCr | 0:1ed39951ab7b | 71 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
GregCr | 3:8b9e2a4df4b5 | 72 | #define LORA_FHSS_ENABLED false |
GregCr | 3:8b9e2a4df4b5 | 73 | #define LORA_NB_SYMB_HOP 4 |
GregCr | 0:1ed39951ab7b | 74 | #define LORA_IQ_INVERSION_ON false |
GregCr | 3:8b9e2a4df4b5 | 75 | #define LORA_CRC_ENABLED true |
GregCr | 3:8b9e2a4df4b5 | 76 | |
GregCr | 0:1ed39951ab7b | 77 | #elif USE_MODEM_FSK == 1 |
GregCr | 0:1ed39951ab7b | 78 | |
GregCr | 2:59e108728d71 | 79 | #define FSK_FDEV 25000 // Hz |
GregCr | 2:59e108728d71 | 80 | #define FSK_DATARATE 19200 // bps |
GregCr | 2:59e108728d71 | 81 | #define FSK_BANDWIDTH 50000 // Hz |
GregCr | 2:59e108728d71 | 82 | #define FSK_AFC_BANDWIDTH 83333 // Hz |
GregCr | 0:1ed39951ab7b | 83 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx |
GregCr | 0:1ed39951ab7b | 84 | #define FSK_FIX_LENGTH_PAYLOAD_ON false |
GregCr | 3:8b9e2a4df4b5 | 85 | #define FSK_CRC_ENABLED true |
GregCr | 3:8b9e2a4df4b5 | 86 | |
GregCr | 0:1ed39951ab7b | 87 | #else |
GregCr | 0:1ed39951ab7b | 88 | #error "Please define a modem in the compiler options." |
GregCr | 0:1ed39951ab7b | 89 | #endif |
GregCr | 0:1ed39951ab7b | 90 | |
GregCr | 7:c1bbd6c56979 | 91 | #define RX_TIMEOUT_VALUE 3500000 // in us |
GregCr | 0:1ed39951ab7b | 92 | #define BUFFER_SIZE 32 // Define the payload size here |
GregCr | 0:1ed39951ab7b | 93 | |
GregCr | 8:f956dee63a56 | 94 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
GregCr | 3:8b9e2a4df4b5 | 95 | DigitalOut led(LED2); |
GregCr | 3:8b9e2a4df4b5 | 96 | #else |
GregCr | 3:8b9e2a4df4b5 | 97 | DigitalOut led(LED1); |
GregCr | 3:8b9e2a4df4b5 | 98 | #endif |
GregCr | 3:8b9e2a4df4b5 | 99 | |
emcu | 14:62abee5e269d | 100 | //------------------------------------ |
emcu | 14:62abee5e269d | 101 | // Hyperterminal configuration |
emcu | 14:62abee5e269d | 102 | // 9600 bauds, 8-bit data, no parity |
emcu | 14:62abee5e269d | 103 | //------------------------------------ |
emcu | 14:62abee5e269d | 104 | |
emcu | 14:62abee5e269d | 105 | Serial pc(SERIAL_TX, SERIAL_RX); |
emcu | 14:62abee5e269d | 106 | |
GregCr | 0:1ed39951ab7b | 107 | /* |
GregCr | 0:1ed39951ab7b | 108 | * Global variables declarations |
GregCr | 0:1ed39951ab7b | 109 | */ |
mluis | 10:7af820d1e1df | 110 | typedef enum |
mluis | 10:7af820d1e1df | 111 | { |
mluis | 10:7af820d1e1df | 112 | LOWPOWER = 0, |
mluis | 10:7af820d1e1df | 113 | IDLE, |
mluis | 10:7af820d1e1df | 114 | |
mluis | 10:7af820d1e1df | 115 | RX, |
mluis | 10:7af820d1e1df | 116 | RX_TIMEOUT, |
mluis | 10:7af820d1e1df | 117 | RX_ERROR, |
mluis | 10:7af820d1e1df | 118 | |
mluis | 10:7af820d1e1df | 119 | TX, |
mluis | 10:7af820d1e1df | 120 | TX_TIMEOUT, |
mluis | 10:7af820d1e1df | 121 | |
mluis | 10:7af820d1e1df | 122 | CAD, |
mluis | 10:7af820d1e1df | 123 | CAD_DONE |
mluis | 10:7af820d1e1df | 124 | }AppStates_t; |
GregCr | 0:1ed39951ab7b | 125 | |
mluis | 10:7af820d1e1df | 126 | volatile AppStates_t State = LOWPOWER; |
mluis | 10:7af820d1e1df | 127 | |
mluis | 10:7af820d1e1df | 128 | /*! |
mluis | 10:7af820d1e1df | 129 | * Radio events function pointer |
mluis | 10:7af820d1e1df | 130 | */ |
mluis | 10:7af820d1e1df | 131 | static RadioEvents_t RadioEvents; |
mluis | 10:7af820d1e1df | 132 | |
mluis | 10:7af820d1e1df | 133 | /* |
mluis | 10:7af820d1e1df | 134 | * Global variables declarations |
mluis | 10:7af820d1e1df | 135 | */ |
GregCr | 13:edb9b443c1dd | 136 | SX1272MB2xAS Radio( NULL ); |
GregCr | 0:1ed39951ab7b | 137 | |
GregCr | 0:1ed39951ab7b | 138 | const uint8_t PingMsg[] = "PING"; |
GregCr | 0:1ed39951ab7b | 139 | const uint8_t PongMsg[] = "PONG"; |
GregCr | 0:1ed39951ab7b | 140 | |
GregCr | 0:1ed39951ab7b | 141 | uint16_t BufferSize = BUFFER_SIZE; |
GregCr | 0:1ed39951ab7b | 142 | uint8_t Buffer[BUFFER_SIZE]; |
GregCr | 0:1ed39951ab7b | 143 | |
GregCr | 5:f2431c4fe3bb | 144 | int16_t RssiValue = 0.0; |
GregCr | 5:f2431c4fe3bb | 145 | int8_t SnrValue = 0.0; |
GregCr | 0:1ed39951ab7b | 146 | |
GregCr | 0:1ed39951ab7b | 147 | int main() |
GregCr | 0:1ed39951ab7b | 148 | { |
GregCr | 0:1ed39951ab7b | 149 | uint8_t i; |
GregCr | 0:1ed39951ab7b | 150 | bool isMaster = true; |
GregCr | 0:1ed39951ab7b | 151 | |
emcu | 14:62abee5e269d | 152 | pc.printf("\n\n\rModified by: http://www.wmcu.it \n\n\r" ); |
emcu | 14:62abee5e269d | 153 | pc.printf("\n\rSee this link: https://developer.mbed.org/components/SX1272MB2xAS/ \n\rfor original example.\n\r "); |
GregCr | 13:edb9b443c1dd | 154 | debug( "\n\n\r SX1272 Ping Pong Demo Application \n\n\r" ); |
mluis | 10:7af820d1e1df | 155 | |
mluis | 10:7af820d1e1df | 156 | // Initialize Radio driver |
mluis | 10:7af820d1e1df | 157 | RadioEvents.TxDone = OnTxDone; |
mluis | 10:7af820d1e1df | 158 | RadioEvents.RxDone = OnRxDone; |
mluis | 10:7af820d1e1df | 159 | RadioEvents.RxError = OnRxError; |
mluis | 10:7af820d1e1df | 160 | RadioEvents.TxTimeout = OnTxTimeout; |
mluis | 10:7af820d1e1df | 161 | RadioEvents.RxTimeout = OnRxTimeout; |
mluis | 10:7af820d1e1df | 162 | Radio.Init( &RadioEvents ); |
GregCr | 0:1ed39951ab7b | 163 | |
GregCr | 7:c1bbd6c56979 | 164 | // verify the connection with the board |
GregCr | 7:c1bbd6c56979 | 165 | while( Radio.Read( REG_VERSION ) == 0x00 ) |
GregCr | 2:59e108728d71 | 166 | { |
GregCr | 7:c1bbd6c56979 | 167 | debug( "Radio could not be detected!\n\r", NULL ); |
GregCr | 7:c1bbd6c56979 | 168 | wait( 1 ); |
GregCr | 2:59e108728d71 | 169 | } |
GregCr | 7:c1bbd6c56979 | 170 | |
GregCr | 13:edb9b443c1dd | 171 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" ); |
GregCr | 13:edb9b443c1dd | 172 | |
GregCr | 0:1ed39951ab7b | 173 | Radio.SetChannel( RF_FREQUENCY ); |
GregCr | 0:1ed39951ab7b | 174 | |
GregCr | 0:1ed39951ab7b | 175 | #if USE_MODEM_LORA == 1 |
GregCr | 3:8b9e2a4df4b5 | 176 | |
GregCr | 7:c1bbd6c56979 | 177 | debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); |
GregCr | 7:c1bbd6c56979 | 178 | debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); |
GregCr | 7:c1bbd6c56979 | 179 | |
GregCr | 0:1ed39951ab7b | 180 | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
GregCr | 0:1ed39951ab7b | 181 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
GregCr | 0:1ed39951ab7b | 182 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
GregCr | 3:8b9e2a4df4b5 | 183 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
GregCr | 7:c1bbd6c56979 | 184 | LORA_IQ_INVERSION_ON, 2000000 ); |
GregCr | 0:1ed39951ab7b | 185 | |
GregCr | 0:1ed39951ab7b | 186 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
GregCr | 0:1ed39951ab7b | 187 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
mluis | 9:e764990e45df | 188 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
GregCr | 3:8b9e2a4df4b5 | 189 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
GregCr | 3:8b9e2a4df4b5 | 190 | LORA_IQ_INVERSION_ON, true ); |
GregCr | 0:1ed39951ab7b | 191 | |
GregCr | 0:1ed39951ab7b | 192 | #elif USE_MODEM_FSK == 1 |
GregCr | 0:1ed39951ab7b | 193 | |
GregCr | 7:c1bbd6c56979 | 194 | debug("\n\n\r > FSK Mode < \n\n\r"); |
GregCr | 0:1ed39951ab7b | 195 | Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
GregCr | 0:1ed39951ab7b | 196 | FSK_DATARATE, 0, |
GregCr | 0:1ed39951ab7b | 197 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
GregCr | 7:c1bbd6c56979 | 198 | FSK_CRC_ENABLED, 0, 0, 0, 2000000 ); |
GregCr | 0:1ed39951ab7b | 199 | |
GregCr | 0:1ed39951ab7b | 200 | Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
GregCr | 0:1ed39951ab7b | 201 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
mluis | 9:e764990e45df | 202 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
GregCr | 3:8b9e2a4df4b5 | 203 | 0, 0, false, true ); |
GregCr | 0:1ed39951ab7b | 204 | |
GregCr | 0:1ed39951ab7b | 205 | #else |
GregCr | 0:1ed39951ab7b | 206 | |
GregCr | 0:1ed39951ab7b | 207 | #error "Please define a modem in the compiler options." |
GregCr | 0:1ed39951ab7b | 208 | |
GregCr | 0:1ed39951ab7b | 209 | #endif |
GregCr | 1:126d70d374f6 | 210 | |
GregCr | 7:c1bbd6c56979 | 211 | debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); |
GregCr | 0:1ed39951ab7b | 212 | |
GregCr | 3:8b9e2a4df4b5 | 213 | led = 0; |
GregCr | 3:8b9e2a4df4b5 | 214 | |
GregCr | 0:1ed39951ab7b | 215 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 216 | |
GregCr | 0:1ed39951ab7b | 217 | while( 1 ) |
GregCr | 0:1ed39951ab7b | 218 | { |
GregCr | 0:1ed39951ab7b | 219 | switch( State ) |
GregCr | 0:1ed39951ab7b | 220 | { |
GregCr | 0:1ed39951ab7b | 221 | case RX: |
GregCr | 0:1ed39951ab7b | 222 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 223 | { |
GregCr | 0:1ed39951ab7b | 224 | if( BufferSize > 0 ) |
GregCr | 0:1ed39951ab7b | 225 | { |
GregCr | 0:1ed39951ab7b | 226 | if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 227 | { |
GregCr | 3:8b9e2a4df4b5 | 228 | led = !led; |
GregCr | 2:59e108728d71 | 229 | debug( "...Pong\r\n" ); |
GregCr | 0:1ed39951ab7b | 230 | // Send the next PING frame |
GregCr | 7:c1bbd6c56979 | 231 | strcpy( ( char* )Buffer, ( char* )PingMsg ); |
GregCr | 0:1ed39951ab7b | 232 | // We fill the buffer with numbers for the payload |
GregCr | 0:1ed39951ab7b | 233 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 234 | { |
GregCr | 0:1ed39951ab7b | 235 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 236 | } |
GregCr | 0:1ed39951ab7b | 237 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 238 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 239 | } |
GregCr | 2:59e108728d71 | 240 | else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 241 | { // A master already exists then become a slave |
GregCr | 2:59e108728d71 | 242 | debug( "...Ping\r\n" ); |
GregCr | 3:8b9e2a4df4b5 | 243 | led = !led; |
GregCr | 0:1ed39951ab7b | 244 | isMaster = false; |
GregCr | 7:c1bbd6c56979 | 245 | // Send the next PONG frame |
GregCr | 7:c1bbd6c56979 | 246 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
GregCr | 2:59e108728d71 | 247 | // We fill the buffer with numbers for the payload |
GregCr | 2:59e108728d71 | 248 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 2:59e108728d71 | 249 | { |
GregCr | 2:59e108728d71 | 250 | Buffer[i] = i - 4; |
GregCr | 2:59e108728d71 | 251 | } |
GregCr | 3:8b9e2a4df4b5 | 252 | wait_ms( 10 ); |
GregCr | 2:59e108728d71 | 253 | Radio.Send( Buffer, BufferSize ); |
GregCr | 2:59e108728d71 | 254 | } |
GregCr | 2:59e108728d71 | 255 | else // valid reception but neither a PING or a PONG message |
GregCr | 2:59e108728d71 | 256 | { // Set device as master ans start again |
GregCr | 2:59e108728d71 | 257 | isMaster = true; |
GregCr | 0:1ed39951ab7b | 258 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 2:59e108728d71 | 259 | } |
GregCr | 0:1ed39951ab7b | 260 | } |
GregCr | 0:1ed39951ab7b | 261 | } |
GregCr | 0:1ed39951ab7b | 262 | else |
GregCr | 0:1ed39951ab7b | 263 | { |
GregCr | 0:1ed39951ab7b | 264 | if( BufferSize > 0 ) |
GregCr | 0:1ed39951ab7b | 265 | { |
GregCr | 0:1ed39951ab7b | 266 | if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) |
GregCr | 0:1ed39951ab7b | 267 | { |
GregCr | 3:8b9e2a4df4b5 | 268 | led = !led; |
GregCr | 2:59e108728d71 | 269 | debug( "...Ping\r\n" ); |
GregCr | 2:59e108728d71 | 270 | // Send the reply to the PING string |
GregCr | 7:c1bbd6c56979 | 271 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
GregCr | 0:1ed39951ab7b | 272 | // We fill the buffer with numbers for the payload |
GregCr | 0:1ed39951ab7b | 273 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 274 | { |
GregCr | 0:1ed39951ab7b | 275 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 276 | } |
GregCr | 0:1ed39951ab7b | 277 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 278 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 279 | } |
GregCr | 2:59e108728d71 | 280 | else // valid reception but not a PING as expected |
GregCr | 2:59e108728d71 | 281 | { // Set device as master and start again |
GregCr | 2:59e108728d71 | 282 | isMaster = true; |
GregCr | 2:59e108728d71 | 283 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 2:59e108728d71 | 284 | } |
GregCr | 0:1ed39951ab7b | 285 | } |
GregCr | 0:1ed39951ab7b | 286 | } |
GregCr | 0:1ed39951ab7b | 287 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 288 | break; |
GregCr | 3:8b9e2a4df4b5 | 289 | case TX: |
GregCr | 3:8b9e2a4df4b5 | 290 | led = !led; |
GregCr | 2:59e108728d71 | 291 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 292 | { |
GregCr | 2:59e108728d71 | 293 | debug( "Ping...\r\n" ); |
GregCr | 0:1ed39951ab7b | 294 | } |
GregCr | 0:1ed39951ab7b | 295 | else |
GregCr | 0:1ed39951ab7b | 296 | { |
GregCr | 2:59e108728d71 | 297 | debug( "Pong...\r\n" ); |
GregCr | 0:1ed39951ab7b | 298 | } |
GregCr | 0:1ed39951ab7b | 299 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 300 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 301 | break; |
GregCr | 0:1ed39951ab7b | 302 | case RX_TIMEOUT: |
GregCr | 0:1ed39951ab7b | 303 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 304 | { |
GregCr | 0:1ed39951ab7b | 305 | // Send the next PING frame |
GregCr | 7:c1bbd6c56979 | 306 | strcpy( ( char* )Buffer, ( char* )PingMsg ); |
GregCr | 0:1ed39951ab7b | 307 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 308 | { |
GregCr | 0:1ed39951ab7b | 309 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 310 | } |
GregCr | 0:1ed39951ab7b | 311 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 312 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 313 | } |
GregCr | 0:1ed39951ab7b | 314 | else |
GregCr | 0:1ed39951ab7b | 315 | { |
GregCr | 0:1ed39951ab7b | 316 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 317 | } |
GregCr | 0:1ed39951ab7b | 318 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 319 | break; |
GregCr | 0:1ed39951ab7b | 320 | case RX_ERROR: |
GregCr | 7:c1bbd6c56979 | 321 | // We have received a Packet with a CRC error, send reply as if packet was correct |
GregCr | 0:1ed39951ab7b | 322 | if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 323 | { |
GregCr | 0:1ed39951ab7b | 324 | // Send the next PING frame |
GregCr | 7:c1bbd6c56979 | 325 | strcpy( ( char* )Buffer, ( char* )PingMsg ); |
GregCr | 0:1ed39951ab7b | 326 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 327 | { |
GregCr | 0:1ed39951ab7b | 328 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 329 | } |
GregCr | 0:1ed39951ab7b | 330 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 331 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 332 | } |
GregCr | 0:1ed39951ab7b | 333 | else |
GregCr | 0:1ed39951ab7b | 334 | { |
GregCr | 2:59e108728d71 | 335 | // Send the next PONG frame |
GregCr | 7:c1bbd6c56979 | 336 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
GregCr | 2:59e108728d71 | 337 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 2:59e108728d71 | 338 | { |
GregCr | 2:59e108728d71 | 339 | Buffer[i] = i - 4; |
GregCr | 2:59e108728d71 | 340 | } |
GregCr | 2:59e108728d71 | 341 | wait_ms( 10 ); |
GregCr | 2:59e108728d71 | 342 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 343 | } |
GregCr | 0:1ed39951ab7b | 344 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 345 | break; |
GregCr | 0:1ed39951ab7b | 346 | case TX_TIMEOUT: |
GregCr | 0:1ed39951ab7b | 347 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 348 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 349 | break; |
GregCr | 0:1ed39951ab7b | 350 | case LOWPOWER: |
GregCr | 0:1ed39951ab7b | 351 | break; |
GregCr | 0:1ed39951ab7b | 352 | default: |
GregCr | 0:1ed39951ab7b | 353 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 354 | break; |
GregCr | 0:1ed39951ab7b | 355 | } |
GregCr | 0:1ed39951ab7b | 356 | } |
GregCr | 0:1ed39951ab7b | 357 | } |
GregCr | 0:1ed39951ab7b | 358 | |
GregCr | 0:1ed39951ab7b | 359 | void OnTxDone( void ) |
GregCr | 0:1ed39951ab7b | 360 | { |
GregCr | 5:f2431c4fe3bb | 361 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 362 | State = TX; |
GregCr | 7:c1bbd6c56979 | 363 | debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 364 | } |
GregCr | 0:1ed39951ab7b | 365 | |
GregCr | 4:5ece30264cd9 | 366 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
GregCr | 0:1ed39951ab7b | 367 | { |
GregCr | 0:1ed39951ab7b | 368 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 369 | BufferSize = size; |
GregCr | 0:1ed39951ab7b | 370 | memcpy( Buffer, payload, BufferSize ); |
GregCr | 0:1ed39951ab7b | 371 | RssiValue = rssi; |
GregCr | 0:1ed39951ab7b | 372 | SnrValue = snr; |
GregCr | 0:1ed39951ab7b | 373 | State = RX; |
GregCr | 7:c1bbd6c56979 | 374 | debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 375 | } |
GregCr | 0:1ed39951ab7b | 376 | |
GregCr | 0:1ed39951ab7b | 377 | void OnTxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 378 | { |
GregCr | 0:1ed39951ab7b | 379 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 380 | State = TX_TIMEOUT; |
GregCr | 7:c1bbd6c56979 | 381 | debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 382 | } |
GregCr | 0:1ed39951ab7b | 383 | |
GregCr | 0:1ed39951ab7b | 384 | void OnRxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 385 | { |
GregCr | 0:1ed39951ab7b | 386 | Radio.Sleep( ); |
GregCr | 1:126d70d374f6 | 387 | Buffer[ BufferSize ] = 0; |
GregCr | 0:1ed39951ab7b | 388 | State = RX_TIMEOUT; |
GregCr | 7:c1bbd6c56979 | 389 | debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 390 | } |
GregCr | 0:1ed39951ab7b | 391 | |
GregCr | 0:1ed39951ab7b | 392 | void OnRxError( void ) |
GregCr | 0:1ed39951ab7b | 393 | { |
GregCr | 0:1ed39951ab7b | 394 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 395 | State = RX_ERROR; |
GregCr | 7:c1bbd6c56979 | 396 | debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); |
GregCr | 0:1ed39951ab7b | 397 | } |
GregCr | 3:8b9e2a4df4b5 | 398 |