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