TEST_LoRaSTboard
Dependencies: BufferedSerial SX1276GenericLib mbed
SX1276GenericPingPong/GenericPingPong.cpp@1:584a0952f885, 2018-06-18 (annotated)
- Committer:
- benpen10
- Date:
- Mon Jun 18 19:41:23 2018 +0000
- Revision:
- 1:584a0952f885
- Parent:
- 0:5f52ac52da6c
- Child:
- 2:3eac2a81eba4
Attempt 1 at LoRa US standard
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benpen10 | 0:5f52ac52da6c | 1 | /* |
benpen10 | 0:5f52ac52da6c | 2 | * This file contains a copy of the master content sx1276PingPong |
benpen10 | 0:5f52ac52da6c | 3 | * with adaption for the SX1276Generic environment |
benpen10 | 0:5f52ac52da6c | 4 | * (c) 2017 Helmut Tschemernjak |
benpen10 | 0:5f52ac52da6c | 5 | * 30826 Garbsen (Hannover) Germany |
benpen10 | 0:5f52ac52da6c | 6 | */ |
benpen10 | 0:5f52ac52da6c | 7 | |
benpen10 | 0:5f52ac52da6c | 8 | #include "mbed.h" |
benpen10 | 0:5f52ac52da6c | 9 | #include "PinMap.h" |
benpen10 | 0:5f52ac52da6c | 10 | #include "GenericPingPong.h" |
benpen10 | 0:5f52ac52da6c | 11 | #include "sx1276-mbed-hal.h" |
benpen10 | 0:5f52ac52da6c | 12 | #include "main.h" |
benpen10 | 0:5f52ac52da6c | 13 | |
benpen10 | 0:5f52ac52da6c | 14 | #ifdef FEATURE_LORA |
benpen10 | 0:5f52ac52da6c | 15 | |
benpen10 | 0:5f52ac52da6c | 16 | /* Set this flag to '1' to display debug messages on the console */ |
benpen10 | 0:5f52ac52da6c | 17 | #define DEBUG_MESSAGE 1 |
benpen10 | 0:5f52ac52da6c | 18 | |
benpen10 | 0:5f52ac52da6c | 19 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ |
benpen10 | 0:5f52ac52da6c | 20 | #define USE_MODEM_LORA 1 |
benpen10 | 0:5f52ac52da6c | 21 | #define USE_MODEM_FSK !USE_MODEM_LORA |
benpen10 | 1:584a0952f885 | 22 | //#define RF_FREQUENCY RF_FREQUENCY_868_1 // Hz |
benpen10 | 1:584a0952f885 | 23 | #define RF_FREQUENCY RF_FREQUENCY_915 // Hz |
benpen10 | 0:5f52ac52da6c | 24 | #define TX_OUTPUT_POWER 14 // 14 dBm |
benpen10 | 0:5f52ac52da6c | 25 | |
benpen10 | 0:5f52ac52da6c | 26 | #if USE_MODEM_LORA == 1 |
benpen10 | 0:5f52ac52da6c | 27 | |
benpen10 | 0:5f52ac52da6c | 28 | #define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap |
benpen10 | 0:5f52ac52da6c | 29 | #define LORA_SPREADING_FACTOR LORA_SF7 |
benpen10 | 0:5f52ac52da6c | 30 | #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5 |
benpen10 | 0:5f52ac52da6c | 31 | |
benpen10 | 0:5f52ac52da6c | 32 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
benpen10 | 0:5f52ac52da6c | 33 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
benpen10 | 0:5f52ac52da6c | 34 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
benpen10 | 0:5f52ac52da6c | 35 | #define LORA_FHSS_ENABLED false |
benpen10 | 0:5f52ac52da6c | 36 | #define LORA_NB_SYMB_HOP 4 |
benpen10 | 0:5f52ac52da6c | 37 | #define LORA_IQ_INVERSION_ON false |
benpen10 | 0:5f52ac52da6c | 38 | #define LORA_CRC_ENABLED true |
benpen10 | 0:5f52ac52da6c | 39 | |
benpen10 | 0:5f52ac52da6c | 40 | #elif USE_MODEM_FSK == 1 |
benpen10 | 0:5f52ac52da6c | 41 | |
benpen10 | 0:5f52ac52da6c | 42 | #define FSK_FDEV 25000 // Hz |
benpen10 | 0:5f52ac52da6c | 43 | #define FSK_DATARATE 19200 // bps |
benpen10 | 0:5f52ac52da6c | 44 | #define FSK_BANDWIDTH 50000 // Hz |
benpen10 | 0:5f52ac52da6c | 45 | #define FSK_AFC_BANDWIDTH 83333 // Hz |
benpen10 | 0:5f52ac52da6c | 46 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx |
benpen10 | 0:5f52ac52da6c | 47 | #define FSK_FIX_LENGTH_PAYLOAD_ON false |
benpen10 | 0:5f52ac52da6c | 48 | #define FSK_CRC_ENABLED true |
benpen10 | 0:5f52ac52da6c | 49 | |
benpen10 | 0:5f52ac52da6c | 50 | #else |
benpen10 | 0:5f52ac52da6c | 51 | #error "Please define a modem in the compiler options." |
benpen10 | 0:5f52ac52da6c | 52 | #endif |
benpen10 | 0:5f52ac52da6c | 53 | |
benpen10 | 0:5f52ac52da6c | 54 | |
benpen10 | 0:5f52ac52da6c | 55 | #define RX_TIMEOUT_VALUE 3500 // in ms |
benpen10 | 0:5f52ac52da6c | 56 | |
benpen10 | 0:5f52ac52da6c | 57 | //#define BUFFER_SIZE 32 // Define the payload size here |
benpen10 | 0:5f52ac52da6c | 58 | #define BUFFER_SIZE 64 // Define the payload size here |
benpen10 | 0:5f52ac52da6c | 59 | |
benpen10 | 0:5f52ac52da6c | 60 | /* |
benpen10 | 0:5f52ac52da6c | 61 | * Global variables declarations |
benpen10 | 0:5f52ac52da6c | 62 | */ |
benpen10 | 0:5f52ac52da6c | 63 | typedef enum |
benpen10 | 0:5f52ac52da6c | 64 | { |
benpen10 | 0:5f52ac52da6c | 65 | LOWPOWER = 0, |
benpen10 | 0:5f52ac52da6c | 66 | IDLE, |
benpen10 | 0:5f52ac52da6c | 67 | |
benpen10 | 0:5f52ac52da6c | 68 | RX, |
benpen10 | 0:5f52ac52da6c | 69 | RX_TIMEOUT, |
benpen10 | 0:5f52ac52da6c | 70 | RX_ERROR, |
benpen10 | 0:5f52ac52da6c | 71 | |
benpen10 | 0:5f52ac52da6c | 72 | TX, |
benpen10 | 0:5f52ac52da6c | 73 | TX_TIMEOUT, |
benpen10 | 0:5f52ac52da6c | 74 | |
benpen10 | 0:5f52ac52da6c | 75 | CAD, |
benpen10 | 0:5f52ac52da6c | 76 | CAD_DONE |
benpen10 | 0:5f52ac52da6c | 77 | } AppStates_t; |
benpen10 | 0:5f52ac52da6c | 78 | |
benpen10 | 0:5f52ac52da6c | 79 | volatile AppStates_t State = LOWPOWER; |
benpen10 | 0:5f52ac52da6c | 80 | |
benpen10 | 0:5f52ac52da6c | 81 | /*! |
benpen10 | 0:5f52ac52da6c | 82 | * Radio events function pointer |
benpen10 | 0:5f52ac52da6c | 83 | */ |
benpen10 | 0:5f52ac52da6c | 84 | static RadioEvents_t RadioEvents; |
benpen10 | 0:5f52ac52da6c | 85 | |
benpen10 | 0:5f52ac52da6c | 86 | /* |
benpen10 | 0:5f52ac52da6c | 87 | * Global variables declarations |
benpen10 | 0:5f52ac52da6c | 88 | */ |
benpen10 | 0:5f52ac52da6c | 89 | SX1276Generic *Radio; |
benpen10 | 0:5f52ac52da6c | 90 | |
benpen10 | 0:5f52ac52da6c | 91 | |
benpen10 | 0:5f52ac52da6c | 92 | const uint8_t PingMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'I', 'N', 'G'};// "PING"; |
benpen10 | 0:5f52ac52da6c | 93 | const uint8_t PongMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'O', 'N', 'G'};// "PONG"; |
benpen10 | 0:5f52ac52da6c | 94 | |
benpen10 | 0:5f52ac52da6c | 95 | uint16_t BufferSize = BUFFER_SIZE; |
benpen10 | 0:5f52ac52da6c | 96 | uint8_t *Buffer; |
benpen10 | 0:5f52ac52da6c | 97 | |
benpen10 | 0:5f52ac52da6c | 98 | DigitalOut *led3; |
benpen10 | 0:5f52ac52da6c | 99 | |
benpen10 | 0:5f52ac52da6c | 100 | |
benpen10 | 0:5f52ac52da6c | 101 | int SX1276PingPong() |
benpen10 | 0:5f52ac52da6c | 102 | { |
benpen10 | 0:5f52ac52da6c | 103 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
benpen10 | 0:5f52ac52da6c | 104 | DigitalOut *led = new DigitalOut(LED2); |
benpen10 | 0:5f52ac52da6c | 105 | #elif defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_DISCO_L072CZ_LRWAN1) |
benpen10 | 0:5f52ac52da6c | 106 | DigitalOut *led = new DigitalOut(LED4); // RX red |
benpen10 | 0:5f52ac52da6c | 107 | led3 = new DigitalOut(LED3); // TX blue |
benpen10 | 0:5f52ac52da6c | 108 | #else |
benpen10 | 0:5f52ac52da6c | 109 | DigitalOut *led = new DigitalOut(LED1); |
benpen10 | 0:5f52ac52da6c | 110 | led3 = led; |
benpen10 | 0:5f52ac52da6c | 111 | #endif |
benpen10 | 0:5f52ac52da6c | 112 | |
benpen10 | 0:5f52ac52da6c | 113 | Buffer = new uint8_t[BUFFER_SIZE]; |
benpen10 | 0:5f52ac52da6c | 114 | *led3 = 1; |
benpen10 | 0:5f52ac52da6c | 115 | |
benpen10 | 0:5f52ac52da6c | 116 | #ifdef B_L072Z_LRWAN1_LORA |
benpen10 | 0:5f52ac52da6c | 117 | Radio = new SX1276Generic(NULL, MURATA_SX1276, |
benpen10 | 0:5f52ac52da6c | 118 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
benpen10 | 0:5f52ac52da6c | 119 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, |
benpen10 | 0:5f52ac52da6c | 120 | LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); |
benpen10 | 0:5f52ac52da6c | 121 | #else // RFM95 |
benpen10 | 0:5f52ac52da6c | 122 | Radio = new SX1276Generic(NULL, RFM95_SX1276, |
benpen10 | 0:5f52ac52da6c | 123 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
benpen10 | 0:5f52ac52da6c | 124 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5); |
benpen10 | 0:5f52ac52da6c | 125 | |
benpen10 | 0:5f52ac52da6c | 126 | #endif |
benpen10 | 0:5f52ac52da6c | 127 | |
benpen10 | 0:5f52ac52da6c | 128 | uint8_t i; |
benpen10 | 0:5f52ac52da6c | 129 | bool isMaster = true; |
benpen10 | 0:5f52ac52da6c | 130 | |
benpen10 | 0:5f52ac52da6c | 131 | dprintf("SX1276 Ping Pong Demo Application" ); |
benpen10 | 0:5f52ac52da6c | 132 | dprintf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0); |
benpen10 | 0:5f52ac52da6c | 133 | dprintf("TXPower: %d dBm", TX_OUTPUT_POWER); |
benpen10 | 0:5f52ac52da6c | 134 | #if USE_MODEM_LORA == 1 |
benpen10 | 0:5f52ac52da6c | 135 | dprintf("Bandwidth: %d Hz", LORA_BANDWIDTH); |
benpen10 | 0:5f52ac52da6c | 136 | dprintf("Spreading factor: SF%d", LORA_SPREADING_FACTOR); |
benpen10 | 0:5f52ac52da6c | 137 | #elif USE_MODEM_FSK == 1 |
benpen10 | 0:5f52ac52da6c | 138 | dprintf("Bandwidth: %d kHz", FSK_BANDWIDTH); |
benpen10 | 0:5f52ac52da6c | 139 | dprintf("Baudrate: %d", FSK_DATARATE); |
benpen10 | 0:5f52ac52da6c | 140 | #endif |
benpen10 | 0:5f52ac52da6c | 141 | // Initialize Radio driver |
benpen10 | 0:5f52ac52da6c | 142 | RadioEvents.TxDone = OnTxDone; |
benpen10 | 0:5f52ac52da6c | 143 | RadioEvents.RxDone = OnRxDone; |
benpen10 | 0:5f52ac52da6c | 144 | RadioEvents.RxError = OnRxError; |
benpen10 | 0:5f52ac52da6c | 145 | RadioEvents.TxTimeout = OnTxTimeout; |
benpen10 | 0:5f52ac52da6c | 146 | RadioEvents.RxTimeout = OnRxTimeout; |
benpen10 | 0:5f52ac52da6c | 147 | if (Radio->Init( &RadioEvents ) == false) { |
benpen10 | 0:5f52ac52da6c | 148 | while(1) { |
benpen10 | 0:5f52ac52da6c | 149 | dprintf("Radio could not be detected!"); |
benpen10 | 0:5f52ac52da6c | 150 | wait( 1 ); |
benpen10 | 0:5f52ac52da6c | 151 | } |
benpen10 | 0:5f52ac52da6c | 152 | } |
benpen10 | 0:5f52ac52da6c | 153 | |
benpen10 | 0:5f52ac52da6c | 154 | |
benpen10 | 0:5f52ac52da6c | 155 | switch(Radio->DetectBoardType()) { |
benpen10 | 0:5f52ac52da6c | 156 | case SX1276MB1LAS: |
benpen10 | 0:5f52ac52da6c | 157 | if (DEBUG_MESSAGE) |
benpen10 | 0:5f52ac52da6c | 158 | dprintf(" > Board Type: SX1276MB1LAS <"); |
benpen10 | 0:5f52ac52da6c | 159 | break; |
benpen10 | 0:5f52ac52da6c | 160 | case SX1276MB1MAS: |
benpen10 | 0:5f52ac52da6c | 161 | if (DEBUG_MESSAGE) |
benpen10 | 0:5f52ac52da6c | 162 | dprintf(" > Board Type: SX1276MB1LAS <"); |
benpen10 | 0:5f52ac52da6c | 163 | case MURATA_SX1276: |
benpen10 | 0:5f52ac52da6c | 164 | if (DEBUG_MESSAGE) |
benpen10 | 0:5f52ac52da6c | 165 | dprintf(" > Board Type: MURATA_SX1276_STM32L0 <"); |
benpen10 | 0:5f52ac52da6c | 166 | break; |
benpen10 | 0:5f52ac52da6c | 167 | case RFM95_SX1276: |
benpen10 | 0:5f52ac52da6c | 168 | if (DEBUG_MESSAGE) |
benpen10 | 0:5f52ac52da6c | 169 | dprintf(" > HopeRF RFM95xx <"); |
benpen10 | 0:5f52ac52da6c | 170 | break; |
benpen10 | 0:5f52ac52da6c | 171 | default: |
benpen10 | 0:5f52ac52da6c | 172 | dprintf(" > Board Type: unknown <"); |
benpen10 | 0:5f52ac52da6c | 173 | } |
benpen10 | 0:5f52ac52da6c | 174 | |
benpen10 | 0:5f52ac52da6c | 175 | Radio->SetChannel(RF_FREQUENCY ); |
benpen10 | 0:5f52ac52da6c | 176 | |
benpen10 | 0:5f52ac52da6c | 177 | #if USE_MODEM_LORA == 1 |
benpen10 | 0:5f52ac52da6c | 178 | |
benpen10 | 0:5f52ac52da6c | 179 | if (LORA_FHSS_ENABLED) |
benpen10 | 0:5f52ac52da6c | 180 | dprintf(" > LORA FHSS Mode <"); |
benpen10 | 0:5f52ac52da6c | 181 | if (!LORA_FHSS_ENABLED) |
benpen10 | 0:5f52ac52da6c | 182 | dprintf(" > LORA Mode <"); |
benpen10 | 0:5f52ac52da6c | 183 | |
benpen10 | 0:5f52ac52da6c | 184 | Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
benpen10 | 0:5f52ac52da6c | 185 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
benpen10 | 0:5f52ac52da6c | 186 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
benpen10 | 0:5f52ac52da6c | 187 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
benpen10 | 0:5f52ac52da6c | 188 | LORA_IQ_INVERSION_ON, 2000 ); |
benpen10 | 0:5f52ac52da6c | 189 | |
benpen10 | 0:5f52ac52da6c | 190 | Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
benpen10 | 0:5f52ac52da6c | 191 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
benpen10 | 0:5f52ac52da6c | 192 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
benpen10 | 0:5f52ac52da6c | 193 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
benpen10 | 0:5f52ac52da6c | 194 | LORA_IQ_INVERSION_ON, true ); |
benpen10 | 0:5f52ac52da6c | 195 | |
benpen10 | 0:5f52ac52da6c | 196 | #elif USE_MODEM_FSK == 1 |
benpen10 | 0:5f52ac52da6c | 197 | |
benpen10 | 0:5f52ac52da6c | 198 | dprintf(" > FSK Mode <"); |
benpen10 | 0:5f52ac52da6c | 199 | Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
benpen10 | 0:5f52ac52da6c | 200 | FSK_DATARATE, 0, |
benpen10 | 0:5f52ac52da6c | 201 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
benpen10 | 0:5f52ac52da6c | 202 | FSK_CRC_ENABLED, 0, 0, 0, 2000 ); |
benpen10 | 0:5f52ac52da6c | 203 | |
benpen10 | 0:5f52ac52da6c | 204 | Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
benpen10 | 0:5f52ac52da6c | 205 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
benpen10 | 0:5f52ac52da6c | 206 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
benpen10 | 0:5f52ac52da6c | 207 | 0, 0, false, true ); |
benpen10 | 0:5f52ac52da6c | 208 | |
benpen10 | 0:5f52ac52da6c | 209 | #else |
benpen10 | 0:5f52ac52da6c | 210 | |
benpen10 | 0:5f52ac52da6c | 211 | #error "Please define a modem in the compiler options." |
benpen10 | 0:5f52ac52da6c | 212 | |
benpen10 | 0:5f52ac52da6c | 213 | #endif |
benpen10 | 0:5f52ac52da6c | 214 | |
benpen10 | 0:5f52ac52da6c | 215 | if (DEBUG_MESSAGE) |
benpen10 | 0:5f52ac52da6c | 216 | dprintf("Starting Ping-Pong loop"); |
benpen10 | 0:5f52ac52da6c | 217 | |
benpen10 | 0:5f52ac52da6c | 218 | |
benpen10 | 0:5f52ac52da6c | 219 | Radio->Rx( RX_TIMEOUT_VALUE ); |
benpen10 | 0:5f52ac52da6c | 220 | |
benpen10 | 0:5f52ac52da6c | 221 | while( 1 ) |
benpen10 | 0:5f52ac52da6c | 222 | { |
benpen10 | 0:5f52ac52da6c | 223 | #ifdef TARGET_STM32L4 |
benpen10 | 0:5f52ac52da6c | 224 | WatchDogUpdate(); |
benpen10 | 0:5f52ac52da6c | 225 | #endif |
benpen10 | 0:5f52ac52da6c | 226 | |
benpen10 | 0:5f52ac52da6c | 227 | switch( State ) |
benpen10 | 0:5f52ac52da6c | 228 | { |
benpen10 | 0:5f52ac52da6c | 229 | case RX: |
benpen10 | 0:5f52ac52da6c | 230 | *led3 = 0; |
benpen10 | 0:5f52ac52da6c | 231 | if( isMaster == true ) |
benpen10 | 0:5f52ac52da6c | 232 | { |
benpen10 | 0:5f52ac52da6c | 233 | if( BufferSize > 0 ) |
benpen10 | 0:5f52ac52da6c | 234 | { |
benpen10 | 0:5f52ac52da6c | 235 | if( memcmp(Buffer, PongMsg, sizeof(PongMsg)) == 0 ) |
benpen10 | 0:5f52ac52da6c | 236 | { |
benpen10 | 0:5f52ac52da6c | 237 | *led = !*led; |
benpen10 | 0:5f52ac52da6c | 238 | dprintf( "...Pong" ); |
benpen10 | 0:5f52ac52da6c | 239 | // Send the next PING frame |
benpen10 | 0:5f52ac52da6c | 240 | memcpy(Buffer, PingMsg, sizeof(PingMsg)); |
benpen10 | 0:5f52ac52da6c | 241 | // We fill the buffer with numbers for the payload |
benpen10 | 0:5f52ac52da6c | 242 | for( i = sizeof(PingMsg); i < BufferSize; i++ ) |
benpen10 | 0:5f52ac52da6c | 243 | { |
benpen10 | 0:5f52ac52da6c | 244 | Buffer[i] = i - sizeof(PingMsg); |
benpen10 | 0:5f52ac52da6c | 245 | } |
benpen10 | 0:5f52ac52da6c | 246 | wait_ms( 10 ); |
benpen10 | 0:5f52ac52da6c | 247 | Radio->Send( Buffer, BufferSize ); |
benpen10 | 0:5f52ac52da6c | 248 | } |
benpen10 | 0:5f52ac52da6c | 249 | else if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) |
benpen10 | 0:5f52ac52da6c | 250 | { // A master already exists then become a slave |
benpen10 | 0:5f52ac52da6c | 251 | dprintf( "...Ping" ); |
benpen10 | 0:5f52ac52da6c | 252 | *led = !*led; |
benpen10 | 0:5f52ac52da6c | 253 | isMaster = false; |
benpen10 | 0:5f52ac52da6c | 254 | // Send the next PONG frame |
benpen10 | 0:5f52ac52da6c | 255 | memcpy(Buffer, PongMsg, sizeof(PongMsg)); |
benpen10 | 0:5f52ac52da6c | 256 | // We fill the buffer with numbers for the payload |
benpen10 | 0:5f52ac52da6c | 257 | for( i = sizeof(PongMsg); i < BufferSize; i++ ) |
benpen10 | 0:5f52ac52da6c | 258 | { |
benpen10 | 0:5f52ac52da6c | 259 | Buffer[i] = i - sizeof(PongMsg); |
benpen10 | 0:5f52ac52da6c | 260 | } |
benpen10 | 0:5f52ac52da6c | 261 | wait_ms( 10 ); |
benpen10 | 0:5f52ac52da6c | 262 | Radio->Send( Buffer, BufferSize ); |
benpen10 | 0:5f52ac52da6c | 263 | } |
benpen10 | 0:5f52ac52da6c | 264 | else // valid reception but neither a PING or a PONG message |
benpen10 | 0:5f52ac52da6c | 265 | { // Set device as master ans start again |
benpen10 | 0:5f52ac52da6c | 266 | isMaster = true; |
benpen10 | 0:5f52ac52da6c | 267 | Radio->Rx( RX_TIMEOUT_VALUE ); |
benpen10 | 0:5f52ac52da6c | 268 | } |
benpen10 | 0:5f52ac52da6c | 269 | } |
benpen10 | 0:5f52ac52da6c | 270 | } |
benpen10 | 0:5f52ac52da6c | 271 | else |
benpen10 | 0:5f52ac52da6c | 272 | { |
benpen10 | 0:5f52ac52da6c | 273 | if( BufferSize > 0 ) |
benpen10 | 0:5f52ac52da6c | 274 | { |
benpen10 | 0:5f52ac52da6c | 275 | if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) |
benpen10 | 0:5f52ac52da6c | 276 | { |
benpen10 | 0:5f52ac52da6c | 277 | *led = !*led; |
benpen10 | 0:5f52ac52da6c | 278 | dprintf( "...Ping" ); |
benpen10 | 0:5f52ac52da6c | 279 | // Send the reply to the PING string |
benpen10 | 0:5f52ac52da6c | 280 | memcpy(Buffer, PongMsg, sizeof(PongMsg)); |
benpen10 | 0:5f52ac52da6c | 281 | // We fill the buffer with numbers for the payload |
benpen10 | 0:5f52ac52da6c | 282 | for( i = sizeof(PongMsg); i < BufferSize; i++ ) |
benpen10 | 0:5f52ac52da6c | 283 | { |
benpen10 | 0:5f52ac52da6c | 284 | Buffer[i] = i - sizeof(PongMsg); |
benpen10 | 0:5f52ac52da6c | 285 | } |
benpen10 | 0:5f52ac52da6c | 286 | wait_ms( 10 ); |
benpen10 | 0:5f52ac52da6c | 287 | Radio->Send( Buffer, BufferSize ); |
benpen10 | 0:5f52ac52da6c | 288 | } |
benpen10 | 0:5f52ac52da6c | 289 | else // valid reception but not a PING as expected |
benpen10 | 0:5f52ac52da6c | 290 | { // Set device as master and start again |
benpen10 | 0:5f52ac52da6c | 291 | isMaster = true; |
benpen10 | 0:5f52ac52da6c | 292 | Radio->Rx( RX_TIMEOUT_VALUE ); |
benpen10 | 0:5f52ac52da6c | 293 | } |
benpen10 | 0:5f52ac52da6c | 294 | } |
benpen10 | 0:5f52ac52da6c | 295 | } |
benpen10 | 0:5f52ac52da6c | 296 | State = LOWPOWER; |
benpen10 | 0:5f52ac52da6c | 297 | break; |
benpen10 | 0:5f52ac52da6c | 298 | case TX: |
benpen10 | 0:5f52ac52da6c | 299 | *led3 = 1; |
benpen10 | 0:5f52ac52da6c | 300 | if( isMaster == true ) |
benpen10 | 0:5f52ac52da6c | 301 | { |
benpen10 | 0:5f52ac52da6c | 302 | dprintf("Ping..." ); |
benpen10 | 0:5f52ac52da6c | 303 | } |
benpen10 | 0:5f52ac52da6c | 304 | else |
benpen10 | 0:5f52ac52da6c | 305 | { |
benpen10 | 0:5f52ac52da6c | 306 | dprintf("Pong..." ); |
benpen10 | 0:5f52ac52da6c | 307 | } |
benpen10 | 0:5f52ac52da6c | 308 | Radio->Rx( RX_TIMEOUT_VALUE ); |
benpen10 | 0:5f52ac52da6c | 309 | State = LOWPOWER; |
benpen10 | 0:5f52ac52da6c | 310 | break; |
benpen10 | 0:5f52ac52da6c | 311 | case RX_TIMEOUT: |
benpen10 | 0:5f52ac52da6c | 312 | if( isMaster == true ) |
benpen10 | 0:5f52ac52da6c | 313 | { |
benpen10 | 0:5f52ac52da6c | 314 | // Send the next PING frame |
benpen10 | 0:5f52ac52da6c | 315 | memcpy(Buffer, PingMsg, sizeof(PingMsg)); |
benpen10 | 0:5f52ac52da6c | 316 | for( i = sizeof(PingMsg); i < BufferSize; i++ ) |
benpen10 | 0:5f52ac52da6c | 317 | { |
benpen10 | 0:5f52ac52da6c | 318 | Buffer[i] = i - sizeof(PingMsg); |
benpen10 | 0:5f52ac52da6c | 319 | } |
benpen10 | 0:5f52ac52da6c | 320 | wait_ms( 10 ); |
benpen10 | 0:5f52ac52da6c | 321 | Radio->Send( Buffer, BufferSize ); |
benpen10 | 0:5f52ac52da6c | 322 | } |
benpen10 | 0:5f52ac52da6c | 323 | else |
benpen10 | 0:5f52ac52da6c | 324 | { |
benpen10 | 0:5f52ac52da6c | 325 | Radio->Rx( RX_TIMEOUT_VALUE ); |
benpen10 | 0:5f52ac52da6c | 326 | } |
benpen10 | 0:5f52ac52da6c | 327 | State = LOWPOWER; |
benpen10 | 0:5f52ac52da6c | 328 | break; |
benpen10 | 0:5f52ac52da6c | 329 | case RX_ERROR: |
benpen10 | 0:5f52ac52da6c | 330 | // We have received a Packet with a CRC error, send reply as if packet was correct |
benpen10 | 0:5f52ac52da6c | 331 | if( isMaster == true ) |
benpen10 | 0:5f52ac52da6c | 332 | { |
benpen10 | 0:5f52ac52da6c | 333 | // Send the next PING frame |
benpen10 | 0:5f52ac52da6c | 334 | memcpy(Buffer, PingMsg, sizeof(PingMsg)); |
benpen10 | 0:5f52ac52da6c | 335 | for( i = 4; i < BufferSize; i++ ) |
benpen10 | 0:5f52ac52da6c | 336 | { |
benpen10 | 0:5f52ac52da6c | 337 | Buffer[i] = i - 4; |
benpen10 | 0:5f52ac52da6c | 338 | } |
benpen10 | 0:5f52ac52da6c | 339 | wait_ms( 10 ); |
benpen10 | 0:5f52ac52da6c | 340 | Radio->Send( Buffer, BufferSize ); |
benpen10 | 0:5f52ac52da6c | 341 | } |
benpen10 | 0:5f52ac52da6c | 342 | else |
benpen10 | 0:5f52ac52da6c | 343 | { |
benpen10 | 0:5f52ac52da6c | 344 | // Send the next PONG frame |
benpen10 | 0:5f52ac52da6c | 345 | memcpy(Buffer, PongMsg, sizeof(PongMsg)); |
benpen10 | 0:5f52ac52da6c | 346 | for( i = sizeof(PongMsg); i < BufferSize; i++ ) |
benpen10 | 0:5f52ac52da6c | 347 | { |
benpen10 | 0:5f52ac52da6c | 348 | Buffer[i] = i - sizeof(PongMsg); |
benpen10 | 0:5f52ac52da6c | 349 | } |
benpen10 | 0:5f52ac52da6c | 350 | wait_ms( 10 ); |
benpen10 | 0:5f52ac52da6c | 351 | Radio->Send( Buffer, BufferSize ); |
benpen10 | 0:5f52ac52da6c | 352 | } |
benpen10 | 0:5f52ac52da6c | 353 | State = LOWPOWER; |
benpen10 | 0:5f52ac52da6c | 354 | break; |
benpen10 | 0:5f52ac52da6c | 355 | case TX_TIMEOUT: |
benpen10 | 0:5f52ac52da6c | 356 | Radio->Rx( RX_TIMEOUT_VALUE ); |
benpen10 | 0:5f52ac52da6c | 357 | State = LOWPOWER; |
benpen10 | 0:5f52ac52da6c | 358 | break; |
benpen10 | 0:5f52ac52da6c | 359 | case LOWPOWER: |
benpen10 | 0:5f52ac52da6c | 360 | sleep(); |
benpen10 | 0:5f52ac52da6c | 361 | break; |
benpen10 | 0:5f52ac52da6c | 362 | default: |
benpen10 | 0:5f52ac52da6c | 363 | State = LOWPOWER; |
benpen10 | 0:5f52ac52da6c | 364 | break; |
benpen10 | 0:5f52ac52da6c | 365 | } |
benpen10 | 0:5f52ac52da6c | 366 | } |
benpen10 | 0:5f52ac52da6c | 367 | } |
benpen10 | 0:5f52ac52da6c | 368 | |
benpen10 | 0:5f52ac52da6c | 369 | void OnTxDone(void *radio, void *userThisPtr, void *userData) |
benpen10 | 0:5f52ac52da6c | 370 | { |
benpen10 | 0:5f52ac52da6c | 371 | Radio->Sleep( ); |
benpen10 | 0:5f52ac52da6c | 372 | State = TX; |
benpen10 | 0:5f52ac52da6c | 373 | if (DEBUG_MESSAGE) |
benpen10 | 0:5f52ac52da6c | 374 | dprintf("> OnTxDone"); |
benpen10 | 0:5f52ac52da6c | 375 | } |
benpen10 | 0:5f52ac52da6c | 376 | |
benpen10 | 0:5f52ac52da6c | 377 | void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
benpen10 | 0:5f52ac52da6c | 378 | { |
benpen10 | 0:5f52ac52da6c | 379 | Radio->Sleep( ); |
benpen10 | 0:5f52ac52da6c | 380 | BufferSize = size; |
benpen10 | 0:5f52ac52da6c | 381 | memcpy( Buffer, payload, BufferSize ); |
benpen10 | 0:5f52ac52da6c | 382 | State = RX; |
benpen10 | 0:5f52ac52da6c | 383 | if (DEBUG_MESSAGE) |
benpen10 | 0:5f52ac52da6c | 384 | dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr); |
benpen10 | 0:5f52ac52da6c | 385 | dump("Data:", payload, size); |
benpen10 | 0:5f52ac52da6c | 386 | } |
benpen10 | 0:5f52ac52da6c | 387 | |
benpen10 | 0:5f52ac52da6c | 388 | void OnTxTimeout(void *radio, void *userThisPtr, void *userData) |
benpen10 | 0:5f52ac52da6c | 389 | { |
benpen10 | 0:5f52ac52da6c | 390 | *led3 = 0; |
benpen10 | 0:5f52ac52da6c | 391 | Radio->Sleep( ); |
benpen10 | 0:5f52ac52da6c | 392 | State = TX_TIMEOUT; |
benpen10 | 0:5f52ac52da6c | 393 | if(DEBUG_MESSAGE) |
benpen10 | 0:5f52ac52da6c | 394 | dprintf("> OnTxTimeout"); |
benpen10 | 0:5f52ac52da6c | 395 | } |
benpen10 | 0:5f52ac52da6c | 396 | |
benpen10 | 0:5f52ac52da6c | 397 | void OnRxTimeout(void *radio, void *userThisPtr, void *userData) |
benpen10 | 0:5f52ac52da6c | 398 | { |
benpen10 | 0:5f52ac52da6c | 399 | *led3 = 0; |
benpen10 | 0:5f52ac52da6c | 400 | Radio->Sleep( ); |
benpen10 | 0:5f52ac52da6c | 401 | Buffer[BufferSize-1] = 0; |
benpen10 | 0:5f52ac52da6c | 402 | State = RX_TIMEOUT; |
benpen10 | 0:5f52ac52da6c | 403 | if (DEBUG_MESSAGE) |
benpen10 | 0:5f52ac52da6c | 404 | dprintf("> OnRxTimeout"); |
benpen10 | 0:5f52ac52da6c | 405 | } |
benpen10 | 0:5f52ac52da6c | 406 | |
benpen10 | 0:5f52ac52da6c | 407 | void OnRxError(void *radio, void *userThisPtr, void *userData) |
benpen10 | 0:5f52ac52da6c | 408 | { |
benpen10 | 0:5f52ac52da6c | 409 | Radio->Sleep( ); |
benpen10 | 0:5f52ac52da6c | 410 | State = RX_ERROR; |
benpen10 | 0:5f52ac52da6c | 411 | if (DEBUG_MESSAGE) |
benpen10 | 0:5f52ac52da6c | 412 | dprintf("> OnRxError"); |
benpen10 | 0:5f52ac52da6c | 413 | } |
benpen10 | 0:5f52ac52da6c | 414 | |
benpen10 | 0:5f52ac52da6c | 415 | #endif |