![](/media/cache/profiles/profile_pic_for_github_less_than_1MB.jpg.50x50_q85.jpg)
This is example code that can get you started with building your own IR vision robot that communicates over LoRa
Dependencies: Adafruit-MotorShield Adafruit-PWM-Servo-Driver Adafruit_GFX BufferedSerial MAX17055_EZconfig NEO-6m-GPS SX1276GenericLib USBDeviceHT max32630fthr max77650_charger_sample
Fork of MAX326xxFTHR_LoRa_Example_test by
SX1276GenericPingPong/GenericPingPong.cpp@42:911770814147, 2018-08-15 (annotated)
- Committer:
- dev_alexander
- Date:
- Wed Aug 15 04:06:09 2018 +0000
- Revision:
- 42:911770814147
- Parent:
- 36:09e8b51fc91d
This version has replaced the MAX77650 Library with a publicly available library.
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 | */ |
dev_alexander | 32:b108ed6096b0 | 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" |
dev_alexander | 24:e8d03912f303 | 13 | #include "global_buffers.h" |
Helmut64 | 0:c43b6919ae15 | 14 | |
Helmut64 | 0:c43b6919ae15 | 15 | #ifdef FEATURE_LORA |
Helmut64 | 0:c43b6919ae15 | 16 | |
Helmut64 | 0:c43b6919ae15 | 17 | /* Set this flag to '1' to display debug messages on the console */ |
dev_alexander | 32:b108ed6096b0 | 18 | #define DEBUG_MESSAGE 0 |
Helmut64 | 0:c43b6919ae15 | 19 | |
Helmut64 | 0:c43b6919ae15 | 20 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ |
Helmut64 | 0:c43b6919ae15 | 21 | #define USE_MODEM_LORA 1 |
Helmut64 | 0:c43b6919ae15 | 22 | #define USE_MODEM_FSK !USE_MODEM_LORA |
dev_alexander | 19:9f035b9e65ec | 23 | #define RF_FREQUENCY RF_FREQUENCY_915_0 // Hz |
Helmut64 | 0:c43b6919ae15 | 24 | #define TX_OUTPUT_POWER 14 // 14 dBm |
Helmut64 | 0:c43b6919ae15 | 25 | |
Helmut64 | 0:c43b6919ae15 | 26 | #if USE_MODEM_LORA == 1 |
Helmut64 | 0:c43b6919ae15 | 27 | |
Helmut64 | 7:6a8a82bfb0c6 | 28 | #define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap |
Helmut64 | 0:c43b6919ae15 | 29 | #define LORA_SPREADING_FACTOR LORA_SF7 |
Helmut64 | 0:c43b6919ae15 | 30 | #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5 |
Helmut64 | 0:c43b6919ae15 | 31 | |
Helmut64 | 0:c43b6919ae15 | 32 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
Helmut64 | 0:c43b6919ae15 | 33 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
Helmut64 | 0:c43b6919ae15 | 34 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
Helmut64 | 0:c43b6919ae15 | 35 | #define LORA_FHSS_ENABLED false |
Helmut64 | 0:c43b6919ae15 | 36 | #define LORA_NB_SYMB_HOP 4 |
Helmut64 | 0:c43b6919ae15 | 37 | #define LORA_IQ_INVERSION_ON false |
Helmut64 | 0:c43b6919ae15 | 38 | #define LORA_CRC_ENABLED true |
Helmut64 | 0:c43b6919ae15 | 39 | |
Helmut64 | 0:c43b6919ae15 | 40 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 41 | |
Helmut64 | 0:c43b6919ae15 | 42 | #define FSK_FDEV 25000 // Hz |
Helmut64 | 0:c43b6919ae15 | 43 | #define FSK_DATARATE 19200 // bps |
Helmut64 | 0:c43b6919ae15 | 44 | #define FSK_BANDWIDTH 50000 // Hz |
Helmut64 | 0:c43b6919ae15 | 45 | #define FSK_AFC_BANDWIDTH 83333 // Hz |
Helmut64 | 0:c43b6919ae15 | 46 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx |
Helmut64 | 0:c43b6919ae15 | 47 | #define FSK_FIX_LENGTH_PAYLOAD_ON false |
Helmut64 | 0:c43b6919ae15 | 48 | #define FSK_CRC_ENABLED true |
Helmut64 | 0:c43b6919ae15 | 49 | |
Helmut64 | 0:c43b6919ae15 | 50 | #else |
Helmut64 | 0:c43b6919ae15 | 51 | #error "Please define a modem in the compiler options." |
Helmut64 | 0:c43b6919ae15 | 52 | #endif |
Helmut64 | 0:c43b6919ae15 | 53 | |
Helmut64 | 0:c43b6919ae15 | 54 | |
dev_alexander | 35:bae9b236070b | 55 | //#define RX_TIMEOUT_VALUE 3500 // in ms |
dev_alexander | 36:09e8b51fc91d | 56 | #define RX_TIMEOUT_VALUE 333 // in ms |
dev_alexander | 36:09e8b51fc91d | 57 | |
Helmut64 | 0:c43b6919ae15 | 58 | |
dev_alexander | 24:e8d03912f303 | 59 | //#define BUFFER_SIZE 64 // Define the payload size here |
Helmut64 | 0:c43b6919ae15 | 60 | |
Helmut64 | 0:c43b6919ae15 | 61 | /* |
Helmut64 | 0:c43b6919ae15 | 62 | * Global variables declarations |
Helmut64 | 0:c43b6919ae15 | 63 | */ |
Helmut64 | 0:c43b6919ae15 | 64 | typedef enum |
Helmut64 | 0:c43b6919ae15 | 65 | { |
Helmut64 | 0:c43b6919ae15 | 66 | LOWPOWER = 0, |
Helmut64 | 0:c43b6919ae15 | 67 | IDLE, |
Helmut64 | 0:c43b6919ae15 | 68 | |
Helmut64 | 0:c43b6919ae15 | 69 | RX, |
Helmut64 | 0:c43b6919ae15 | 70 | RX_TIMEOUT, |
Helmut64 | 0:c43b6919ae15 | 71 | RX_ERROR, |
Helmut64 | 0:c43b6919ae15 | 72 | |
Helmut64 | 0:c43b6919ae15 | 73 | TX, |
Helmut64 | 0:c43b6919ae15 | 74 | TX_TIMEOUT, |
Helmut64 | 0:c43b6919ae15 | 75 | |
Helmut64 | 0:c43b6919ae15 | 76 | CAD, |
Helmut64 | 0:c43b6919ae15 | 77 | CAD_DONE |
Helmut64 | 0:c43b6919ae15 | 78 | } AppStates_t; |
Helmut64 | 0:c43b6919ae15 | 79 | |
Helmut64 | 0:c43b6919ae15 | 80 | volatile AppStates_t State = LOWPOWER; |
Helmut64 | 0:c43b6919ae15 | 81 | |
Helmut64 | 0:c43b6919ae15 | 82 | /*! |
Helmut64 | 0:c43b6919ae15 | 83 | * Radio events function pointer |
Helmut64 | 0:c43b6919ae15 | 84 | */ |
Helmut64 | 0:c43b6919ae15 | 85 | static RadioEvents_t RadioEvents; |
Helmut64 | 0:c43b6919ae15 | 86 | |
Helmut64 | 0:c43b6919ae15 | 87 | /* |
Helmut64 | 0:c43b6919ae15 | 88 | * Global variables declarations |
Helmut64 | 0:c43b6919ae15 | 89 | */ |
Helmut64 | 0:c43b6919ae15 | 90 | SX1276Generic *Radio; |
Helmut64 | 0:c43b6919ae15 | 91 | |
dev_alexander | 27:6b549f838f0a | 92 | |
dev_alexander | 28:0ed92c590607 | 93 | // Aliases for the buffers that are made in the main() |
dev_alexander | 28:0ed92c590607 | 94 | uint8_t *BufferTx; |
dev_alexander | 28:0ed92c590607 | 95 | uint8_t *BufferRx; |
Helmut64 | 0:c43b6919ae15 | 96 | |
Helmut64 | 0:c43b6919ae15 | 97 | DigitalOut *led3; |
Helmut64 | 0:c43b6919ae15 | 98 | |
Helmut64 | 0:c43b6919ae15 | 99 | |
dev_alexander | 28:0ed92c590607 | 100 | int SX1276PingPongSetup(uint8_t *BufferTxFromMain, uint8_t *BufferRxFromMain) |
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 | |
dev_alexander | 28:0ed92c590607 | 113 | BufferTx = BufferTxFromMain; |
dev_alexander | 28:0ed92c590607 | 114 | BufferRx = BufferRxFromMain; |
Helmut64 | 0:c43b6919ae15 | 115 | *led3 = 1; |
Helmut64 | 0:c43b6919ae15 | 116 | |
Helmut64 | 0:c43b6919ae15 | 117 | #ifdef B_L072Z_LRWAN1_LORA |
Helmut64 | 0:c43b6919ae15 | 118 | Radio = new SX1276Generic(NULL, MURATA_SX1276, |
Helmut64 | 0:c43b6919ae15 | 119 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
Helmut64 | 0:c43b6919ae15 | 120 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, |
Helmut64 | 0:c43b6919ae15 | 121 | LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); |
Helmut64 | 0:c43b6919ae15 | 122 | #else // RFM95 |
Helmut64 | 0:c43b6919ae15 | 123 | Radio = new SX1276Generic(NULL, RFM95_SX1276, |
Helmut64 | 0:c43b6919ae15 | 124 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
Helmut64 | 0:c43b6919ae15 | 125 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5); |
Helmut64 | 0:c43b6919ae15 | 126 | |
Helmut64 | 0:c43b6919ae15 | 127 | #endif |
Helmut64 | 0:c43b6919ae15 | 128 | |
Helmut64 | 0:c43b6919ae15 | 129 | dprintf("SX1276 Ping Pong Demo Application" ); |
Helmut64 | 0:c43b6919ae15 | 130 | dprintf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0); |
Helmut64 | 0:c43b6919ae15 | 131 | dprintf("TXPower: %d dBm", TX_OUTPUT_POWER); |
Helmut64 | 0:c43b6919ae15 | 132 | #if USE_MODEM_LORA == 1 |
Helmut64 | 8:3b0d7b4ff28f | 133 | dprintf("Bandwidth: %d Hz", LORA_BANDWIDTH); |
Helmut64 | 0:c43b6919ae15 | 134 | dprintf("Spreading factor: SF%d", LORA_SPREADING_FACTOR); |
Helmut64 | 0:c43b6919ae15 | 135 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 136 | dprintf("Bandwidth: %d kHz", FSK_BANDWIDTH); |
Helmut64 | 0:c43b6919ae15 | 137 | dprintf("Baudrate: %d", FSK_DATARATE); |
Helmut64 | 0:c43b6919ae15 | 138 | #endif |
Helmut64 | 0:c43b6919ae15 | 139 | // Initialize Radio driver |
Helmut64 | 0:c43b6919ae15 | 140 | RadioEvents.TxDone = OnTxDone; |
Helmut64 | 0:c43b6919ae15 | 141 | RadioEvents.RxDone = OnRxDone; |
Helmut64 | 0:c43b6919ae15 | 142 | RadioEvents.RxError = OnRxError; |
Helmut64 | 0:c43b6919ae15 | 143 | RadioEvents.TxTimeout = OnTxTimeout; |
Helmut64 | 6:1b598b0e52e4 | 144 | RadioEvents.RxTimeout = OnRxTimeout; |
Helmut64 | 6:1b598b0e52e4 | 145 | if (Radio->Init( &RadioEvents ) == false) { |
Helmut64 | 6:1b598b0e52e4 | 146 | while(1) { |
Helmut64 | 6:1b598b0e52e4 | 147 | dprintf("Radio could not be detected!"); |
Helmut64 | 6:1b598b0e52e4 | 148 | wait( 1 ); |
Helmut64 | 6:1b598b0e52e4 | 149 | } |
Helmut64 | 0:c43b6919ae15 | 150 | } |
Helmut64 | 6:1b598b0e52e4 | 151 | |
Helmut64 | 0:c43b6919ae15 | 152 | |
Helmut64 | 0:c43b6919ae15 | 153 | switch(Radio->DetectBoardType()) { |
Helmut64 | 0:c43b6919ae15 | 154 | case SX1276MB1LAS: |
Helmut64 | 0:c43b6919ae15 | 155 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 156 | dprintf(" > Board Type: SX1276MB1LAS <"); |
Helmut64 | 0:c43b6919ae15 | 157 | break; |
Helmut64 | 0:c43b6919ae15 | 158 | case SX1276MB1MAS: |
Helmut64 | 0:c43b6919ae15 | 159 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 160 | dprintf(" > Board Type: SX1276MB1LAS <"); |
Helmut64 | 0:c43b6919ae15 | 161 | case MURATA_SX1276: |
Helmut64 | 0:c43b6919ae15 | 162 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 163 | dprintf(" > Board Type: MURATA_SX1276_STM32L0 <"); |
Helmut64 | 0:c43b6919ae15 | 164 | break; |
Helmut64 | 0:c43b6919ae15 | 165 | case RFM95_SX1276: |
Helmut64 | 0:c43b6919ae15 | 166 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 167 | dprintf(" > HopeRF RFM95xx <"); |
Helmut64 | 0:c43b6919ae15 | 168 | break; |
Helmut64 | 0:c43b6919ae15 | 169 | default: |
Helmut64 | 0:c43b6919ae15 | 170 | dprintf(" > Board Type: unknown <"); |
Helmut64 | 0:c43b6919ae15 | 171 | } |
Helmut64 | 0:c43b6919ae15 | 172 | |
Helmut64 | 0:c43b6919ae15 | 173 | Radio->SetChannel(RF_FREQUENCY ); |
Helmut64 | 0:c43b6919ae15 | 174 | |
Helmut64 | 0:c43b6919ae15 | 175 | #if USE_MODEM_LORA == 1 |
Helmut64 | 0:c43b6919ae15 | 176 | |
Helmut64 | 0:c43b6919ae15 | 177 | if (LORA_FHSS_ENABLED) |
Helmut64 | 0:c43b6919ae15 | 178 | dprintf(" > LORA FHSS Mode <"); |
Helmut64 | 0:c43b6919ae15 | 179 | if (!LORA_FHSS_ENABLED) |
Helmut64 | 0:c43b6919ae15 | 180 | dprintf(" > LORA Mode <"); |
dev_alexander | 25:1a031add188a | 181 | |
dev_alexander | 25:1a031add188a | 182 | |
dev_alexander | 25:1a031add188a | 183 | // Without this line, the default max payload length is only 64 bytes |
dev_alexander | 25:1a031add188a | 184 | Radio->SetMaxPayloadLength(MODEM_LORA, 255); |
Helmut64 | 0:c43b6919ae15 | 185 | |
Helmut64 | 0:c43b6919ae15 | 186 | Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
Helmut64 | 0:c43b6919ae15 | 187 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
Helmut64 | 0:c43b6919ae15 | 188 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
Helmut64 | 0:c43b6919ae15 | 189 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
Helmut64 | 0:c43b6919ae15 | 190 | LORA_IQ_INVERSION_ON, 2000 ); |
Helmut64 | 0:c43b6919ae15 | 191 | |
Helmut64 | 0:c43b6919ae15 | 192 | Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
Helmut64 | 0:c43b6919ae15 | 193 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
Helmut64 | 0:c43b6919ae15 | 194 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
Helmut64 | 0:c43b6919ae15 | 195 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
Helmut64 | 0:c43b6919ae15 | 196 | LORA_IQ_INVERSION_ON, true ); |
Helmut64 | 0:c43b6919ae15 | 197 | |
Helmut64 | 0:c43b6919ae15 | 198 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 199 | |
Helmut64 | 0:c43b6919ae15 | 200 | dprintf(" > FSK Mode <"); |
Helmut64 | 0:c43b6919ae15 | 201 | Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
Helmut64 | 0:c43b6919ae15 | 202 | FSK_DATARATE, 0, |
Helmut64 | 0:c43b6919ae15 | 203 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
Helmut64 | 0:c43b6919ae15 | 204 | FSK_CRC_ENABLED, 0, 0, 0, 2000 ); |
Helmut64 | 0:c43b6919ae15 | 205 | |
Helmut64 | 0:c43b6919ae15 | 206 | Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
Helmut64 | 0:c43b6919ae15 | 207 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
Helmut64 | 0:c43b6919ae15 | 208 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
Helmut64 | 0:c43b6919ae15 | 209 | 0, 0, false, true ); |
Helmut64 | 0:c43b6919ae15 | 210 | |
Helmut64 | 0:c43b6919ae15 | 211 | #else |
Helmut64 | 0:c43b6919ae15 | 212 | |
Helmut64 | 0:c43b6919ae15 | 213 | #error "Please define a modem in the compiler options." |
Helmut64 | 0:c43b6919ae15 | 214 | |
Helmut64 | 0:c43b6919ae15 | 215 | #endif |
Helmut64 | 0:c43b6919ae15 | 216 | |
Helmut64 | 0:c43b6919ae15 | 217 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 218 | dprintf("Starting Ping-Pong loop"); |
Helmut64 | 0:c43b6919ae15 | 219 | |
Helmut64 | 0:c43b6919ae15 | 220 | |
Helmut64 | 0:c43b6919ae15 | 221 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 222 | } |
dev_alexander | 22:abca9d17d13d | 223 | |
dev_alexander | 25:1a031add188a | 224 | /**************************************************************************************************************************************** |
dev_alexander | 25:1a031add188a | 225 | * |
dev_alexander | 25:1a031add188a | 226 | ****************************************************************************************************************************************/ |
dev_alexander | 22:abca9d17d13d | 227 | int SX1276PingPong(void) |
dev_alexander | 22:abca9d17d13d | 228 | { |
dev_alexander | 22:abca9d17d13d | 229 | #if defined(TARGET_MAX32630FTHR) // Master Device: Bluetooth Gateway |
dev_alexander | 22:abca9d17d13d | 230 | bool isMaster = true; |
dev_alexander | 23:f74a50977593 | 231 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: Robot |
dev_alexander | 22:abca9d17d13d | 232 | bool isMaster = false; |
Helmut64 | 0:c43b6919ae15 | 233 | #endif |
dev_alexander | 25:1a031add188a | 234 | |
dev_alexander | 22:abca9d17d13d | 235 | switch( State ) |
dev_alexander | 22:abca9d17d13d | 236 | { |
dev_alexander | 22:abca9d17d13d | 237 | case RX: |
dev_alexander | 28:0ed92c590607 | 238 | // *led3 = 0; |
dev_alexander | 22:abca9d17d13d | 239 | if( isMaster == true ) |
Helmut64 | 0:c43b6919ae15 | 240 | { |
dev_alexander | 24:e8d03912f303 | 241 | if( BufferSizeRx > 0 ) |
Helmut64 | 0:c43b6919ae15 | 242 | { |
dev_alexander | 42:911770814147 | 243 | /* This checks if the ID of the received transaction is matching that of |
dev_alexander | 32:b108ed6096b0 | 244 | * the slave device's ID. This is Defined in the GenericPingPong.h |
dev_alexander | 32:b108ed6096b0 | 245 | */ |
dev_alexander | 28:0ed92c590607 | 246 | if( memcmp(&BufferRx[rx_idx_signature], PongMsg, sizeof(PongMsg)) == 0 ) |
Helmut64 | 0:c43b6919ae15 | 247 | { |
dev_alexander | 22:abca9d17d13d | 248 | // *led = !*led; |
dev_alexander | 32:b108ed6096b0 | 249 | if (DEBUG_MESSAGE) |
dev_alexander | 32:b108ed6096b0 | 250 | dprintf( "...Pong" ); |
dev_alexander | 22:abca9d17d13d | 251 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 252 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 253 | } |
dev_alexander | 22:abca9d17d13d | 254 | else // valid reception but neither a PING or a PONG message |
dev_alexander | 22:abca9d17d13d | 255 | { // Set device as master ans start again |
dev_alexander | 24:e8d03912f303 | 256 | //isMaster = true; |
dev_alexander | 22:abca9d17d13d | 257 | Radio->Rx( RX_TIMEOUT_VALUE ); |
Helmut64 | 0:c43b6919ae15 | 258 | } |
Helmut64 | 0:c43b6919ae15 | 259 | } |
dev_alexander | 22:abca9d17d13d | 260 | } |
dev_alexander | 22:abca9d17d13d | 261 | else |
dev_alexander | 22:abca9d17d13d | 262 | { |
dev_alexander | 24:e8d03912f303 | 263 | if( BufferSizeRx > 0 ) |
Helmut64 | 0:c43b6919ae15 | 264 | { |
dev_alexander | 28:0ed92c590607 | 265 | if( memcmp(BufferRx, PingMsg, sizeof(PingMsg)) == 0 ) |
Helmut64 | 0:c43b6919ae15 | 266 | { |
dev_alexander | 22:abca9d17d13d | 267 | // *led = !*led; |
dev_alexander | 32:b108ed6096b0 | 268 | if (DEBUG_MESSAGE) |
dev_alexander | 32:b108ed6096b0 | 269 | dprintf( "...Ping" ); |
dev_alexander | 22:abca9d17d13d | 270 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 271 | Radio->Send( BufferTx, BufferSizeTx ); |
Helmut64 | 0:c43b6919ae15 | 272 | } |
dev_alexander | 22:abca9d17d13d | 273 | else // valid reception but not a PING as expected |
dev_alexander | 22:abca9d17d13d | 274 | { // Set device as master and start again |
dev_alexander | 22:abca9d17d13d | 275 | //isMaster = true; |
dev_alexander | 22:abca9d17d13d | 276 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 277 | } |
Helmut64 | 0:c43b6919ae15 | 278 | } |
dev_alexander | 22:abca9d17d13d | 279 | } |
dev_alexander | 22:abca9d17d13d | 280 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 281 | break; |
dev_alexander | 22:abca9d17d13d | 282 | case TX: |
dev_alexander | 28:0ed92c590607 | 283 | // *led3 = 1; |
dev_alexander | 22:abca9d17d13d | 284 | if( isMaster == true ) |
dev_alexander | 22:abca9d17d13d | 285 | { |
dev_alexander | 32:b108ed6096b0 | 286 | if (DEBUG_MESSAGE) |
dev_alexander | 32:b108ed6096b0 | 287 | dprintf("Ping..." ); |
dev_alexander | 22:abca9d17d13d | 288 | } |
dev_alexander | 22:abca9d17d13d | 289 | else |
dev_alexander | 22:abca9d17d13d | 290 | { |
dev_alexander | 32:b108ed6096b0 | 291 | if (DEBUG_MESSAGE) |
dev_alexander | 32:b108ed6096b0 | 292 | dprintf("Pong..." ); |
dev_alexander | 22:abca9d17d13d | 293 | } |
dev_alexander | 22:abca9d17d13d | 294 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 295 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 296 | break; |
dev_alexander | 22:abca9d17d13d | 297 | case RX_TIMEOUT: |
dev_alexander | 22:abca9d17d13d | 298 | if( isMaster == true ) |
dev_alexander | 22:abca9d17d13d | 299 | { |
dev_alexander | 22:abca9d17d13d | 300 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 301 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 302 | } |
dev_alexander | 22:abca9d17d13d | 303 | else |
dev_alexander | 22:abca9d17d13d | 304 | { |
dev_alexander | 22:abca9d17d13d | 305 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 306 | } |
dev_alexander | 22:abca9d17d13d | 307 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 308 | break; |
dev_alexander | 22:abca9d17d13d | 309 | case RX_ERROR: |
dev_alexander | 22:abca9d17d13d | 310 | // We have received a Packet with a CRC error, send reply as if packet was correct |
dev_alexander | 22:abca9d17d13d | 311 | if( isMaster == true ) |
dev_alexander | 22:abca9d17d13d | 312 | { |
dev_alexander | 22:abca9d17d13d | 313 | // Send the next PING frame |
dev_alexander | 32:b108ed6096b0 | 314 | // memcpy(BufferTx, PingMsg, sizeof(PingMsg)); |
dev_alexander | 25:1a031add188a | 315 | /* |
dev_alexander | 24:e8d03912f303 | 316 | for( i = 4; i < BufferSizeTx; i++ ) |
Helmut64 | 0:c43b6919ae15 | 317 | { |
dev_alexander | 24:e8d03912f303 | 318 | BufferTx[i] = i - 4; |
Helmut64 | 0:c43b6919ae15 | 319 | } |
dev_alexander | 25:1a031add188a | 320 | */ |
dev_alexander | 28:0ed92c590607 | 321 | // fillPayloadWithGlobalBufs(BufferTx); |
dev_alexander | 22:abca9d17d13d | 322 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 323 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 324 | } |
dev_alexander | 22:abca9d17d13d | 325 | else |
dev_alexander | 22:abca9d17d13d | 326 | { |
dev_alexander | 22:abca9d17d13d | 327 | // Send the next PONG frame |
dev_alexander | 25:1a031add188a | 328 | // memcpy(BufferTx, PongMsg, sizeof(PongMsg)); |
dev_alexander | 25:1a031add188a | 329 | /* |
dev_alexander | 24:e8d03912f303 | 330 | for( i = sizeof(PongMsg); i < BufferSizeTx; i++ ) |
dev_alexander | 22:abca9d17d13d | 331 | { |
dev_alexander | 24:e8d03912f303 | 332 | BufferTx[i] = i - sizeof(PongMsg); |
dev_alexander | 22:abca9d17d13d | 333 | } |
dev_alexander | 25:1a031add188a | 334 | */ |
dev_alexander | 22:abca9d17d13d | 335 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 336 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 337 | } |
dev_alexander | 22:abca9d17d13d | 338 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 339 | break; |
dev_alexander | 22:abca9d17d13d | 340 | case TX_TIMEOUT: |
dev_alexander | 22:abca9d17d13d | 341 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 342 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 343 | break; |
dev_alexander | 22:abca9d17d13d | 344 | case LOWPOWER: |
dev_alexander | 22:abca9d17d13d | 345 | sleep(); |
dev_alexander | 22:abca9d17d13d | 346 | break; |
dev_alexander | 22:abca9d17d13d | 347 | default: |
dev_alexander | 22:abca9d17d13d | 348 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 349 | break; |
dev_alexander | 22:abca9d17d13d | 350 | } |
Helmut64 | 0:c43b6919ae15 | 351 | } |
Helmut64 | 0:c43b6919ae15 | 352 | |
Helmut64 | 11:d3a591c20cd7 | 353 | void OnTxDone(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 354 | { |
Helmut64 | 0:c43b6919ae15 | 355 | Radio->Sleep( ); |
dev_alexander | 25:1a031add188a | 356 | State = TX; |
Helmut64 | 0:c43b6919ae15 | 357 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 358 | dprintf("> OnTxDone"); |
Helmut64 | 0:c43b6919ae15 | 359 | } |
Helmut64 | 0:c43b6919ae15 | 360 | |
Helmut64 | 11:d3a591c20cd7 | 361 | void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
Helmut64 | 0:c43b6919ae15 | 362 | { |
Helmut64 | 0:c43b6919ae15 | 363 | Radio->Sleep( ); |
dev_alexander | 28:0ed92c590607 | 364 | |
dev_alexander | 28:0ed92c590607 | 365 | if(BufferSizeRx != size) |
dev_alexander | 28:0ed92c590607 | 366 | memcpy( BufferRx, payload, size ); |
dev_alexander | 28:0ed92c590607 | 367 | else |
dev_alexander | 28:0ed92c590607 | 368 | memcpy( BufferRx, payload, BufferSizeRx ); |
Helmut64 | 0:c43b6919ae15 | 369 | State = RX; |
dev_alexander | 42:911770814147 | 370 | |
dev_alexander | 42:911770814147 | 371 | // Reset the RxTimeoutCount |
dev_alexander | 42:911770814147 | 372 | RxTimeoutCount = 0; |
dev_alexander | 25:1a031add188a | 373 | |
dev_alexander | 32:b108ed6096b0 | 374 | if (DEBUG_MESSAGE) { |
Helmut64 | 0:c43b6919ae15 | 375 | dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr); |
dev_alexander | 32:b108ed6096b0 | 376 | dump("Data:", payload, size); |
dev_alexander | 32:b108ed6096b0 | 377 | } |
Helmut64 | 0:c43b6919ae15 | 378 | } |
Helmut64 | 0:c43b6919ae15 | 379 | |
Helmut64 | 11:d3a591c20cd7 | 380 | void OnTxTimeout(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 381 | { |
dev_alexander | 28:0ed92c590607 | 382 | // *led3 = 0; |
Helmut64 | 0:c43b6919ae15 | 383 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 384 | State = TX_TIMEOUT; |
Helmut64 | 0:c43b6919ae15 | 385 | if(DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 386 | dprintf("> OnTxTimeout"); |
Helmut64 | 0:c43b6919ae15 | 387 | } |
Helmut64 | 0:c43b6919ae15 | 388 | |
Helmut64 | 11:d3a591c20cd7 | 389 | void OnRxTimeout(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 390 | { |
dev_alexander | 28:0ed92c590607 | 391 | // *led3 = 0; |
Helmut64 | 0:c43b6919ae15 | 392 | Radio->Sleep( ); |
dev_alexander | 28:0ed92c590607 | 393 | BufferRx[BufferSizeRx-1] = 0; |
Helmut64 | 0:c43b6919ae15 | 394 | State = RX_TIMEOUT; |
Helmut64 | 0:c43b6919ae15 | 395 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 396 | dprintf("> OnRxTimeout"); |
dev_alexander | 42:911770814147 | 397 | |
dev_alexander | 42:911770814147 | 398 | // Increment how many times a transmission times out\ |
dev_alexander | 42:911770814147 | 399 | RxTimeoutCount = RxTimeoutCount + 1; |
Helmut64 | 0:c43b6919ae15 | 400 | } |
Helmut64 | 0:c43b6919ae15 | 401 | |
Helmut64 | 11:d3a591c20cd7 | 402 | void OnRxError(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 403 | { |
Helmut64 | 0:c43b6919ae15 | 404 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 405 | State = RX_ERROR; |
Helmut64 | 0:c43b6919ae15 | 406 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 407 | dprintf("> OnRxError"); |
dev_alexander | 42:911770814147 | 408 | |
dev_alexander | 42:911770814147 | 409 | // Increment how many times a transmission times out\ |
dev_alexander | 42:911770814147 | 410 | RxTimeoutCount = RxTimeoutCount + 1; |
dev_alexander | 42:911770814147 | 411 | } |
dev_alexander | 42:911770814147 | 412 | |
dev_alexander | 42:911770814147 | 413 | int numOfRxTimeouts() |
dev_alexander | 42:911770814147 | 414 | { |
dev_alexander | 42:911770814147 | 415 | return RxTimeoutCount; |
Helmut64 | 0:c43b6919ae15 | 416 | } |
Helmut64 | 0:c43b6919ae15 | 417 | |
Helmut64 | 0:c43b6919ae15 | 418 | #endif |