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