![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
LP Long Distance IR Vision Robot
Dependencies: max77650_charger_sample BufferedSerial SX1276GenericLib Adafruit-MotorShield NEO-6m-GPS MAX17055_EZconfig Adafruit_GFX USBDeviceHT Adafruit-PWM-Servo-Driver
SX1276GenericPingPong/GenericPingPong.cpp@28:0ed92c590607, 2018-07-23 (annotated)
- Committer:
- dev_alexander
- Date:
- Mon Jul 23 23:39:35 2018 +0000
- Revision:
- 28:0ed92c590607
- Parent:
- 27:6b549f838f0a
- Child:
- 32:b108ed6096b0
Reworked how the buffers are loaded and unloaded. Now prints off data received from the Slave on the Master. This is a stable release.
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" |
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 */ |
Helmut64 | 0:c43b6919ae15 | 18 | #define DEBUG_MESSAGE 1 |
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 | |
Helmut64 | 0:c43b6919ae15 | 55 | #define RX_TIMEOUT_VALUE 3500 // in ms |
Helmut64 | 0:c43b6919ae15 | 56 | |
dev_alexander | 24:e8d03912f303 | 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 | |
dev_alexander | 27:6b549f838f0a | 90 | |
dev_alexander | 28:0ed92c590607 | 91 | // Aliases for the buffers that are made in the main() |
dev_alexander | 28:0ed92c590607 | 92 | uint8_t *BufferTx; |
dev_alexander | 28:0ed92c590607 | 93 | uint8_t *BufferRx; |
Helmut64 | 0:c43b6919ae15 | 94 | |
Helmut64 | 0:c43b6919ae15 | 95 | DigitalOut *led3; |
Helmut64 | 0:c43b6919ae15 | 96 | |
Helmut64 | 0:c43b6919ae15 | 97 | |
dev_alexander | 28:0ed92c590607 | 98 | int SX1276PingPongSetup(uint8_t *BufferTxFromMain, uint8_t *BufferRxFromMain) |
Helmut64 | 0:c43b6919ae15 | 99 | { |
dev_alexander | 19:9f035b9e65ec | 100 | dprintf("TEST" ); |
Helmut64 | 0:c43b6919ae15 | 101 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
Helmut64 | 0:c43b6919ae15 | 102 | DigitalOut *led = new DigitalOut(LED2); |
Helmut64 | 13:5a32a1922fbc | 103 | #elif defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_DISCO_L072CZ_LRWAN1) |
Helmut64 | 0:c43b6919ae15 | 104 | DigitalOut *led = new DigitalOut(LED4); // RX red |
Helmut64 | 0:c43b6919ae15 | 105 | led3 = new DigitalOut(LED3); // TX blue |
Helmut64 | 0:c43b6919ae15 | 106 | #else |
Helmut64 | 0:c43b6919ae15 | 107 | DigitalOut *led = new DigitalOut(LED1); |
Helmut64 | 0:c43b6919ae15 | 108 | led3 = led; |
Helmut64 | 0:c43b6919ae15 | 109 | #endif |
Helmut64 | 0:c43b6919ae15 | 110 | |
dev_alexander | 28:0ed92c590607 | 111 | BufferTx = BufferTxFromMain; |
dev_alexander | 28:0ed92c590607 | 112 | BufferRx = BufferRxFromMain; |
Helmut64 | 0:c43b6919ae15 | 113 | *led3 = 1; |
Helmut64 | 0:c43b6919ae15 | 114 | |
Helmut64 | 0:c43b6919ae15 | 115 | #ifdef B_L072Z_LRWAN1_LORA |
Helmut64 | 0:c43b6919ae15 | 116 | Radio = new SX1276Generic(NULL, MURATA_SX1276, |
Helmut64 | 0:c43b6919ae15 | 117 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
Helmut64 | 0:c43b6919ae15 | 118 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, |
Helmut64 | 0:c43b6919ae15 | 119 | LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); |
Helmut64 | 0:c43b6919ae15 | 120 | #else // RFM95 |
Helmut64 | 0:c43b6919ae15 | 121 | Radio = new SX1276Generic(NULL, RFM95_SX1276, |
Helmut64 | 0:c43b6919ae15 | 122 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
Helmut64 | 0:c43b6919ae15 | 123 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5); |
Helmut64 | 0:c43b6919ae15 | 124 | |
Helmut64 | 0:c43b6919ae15 | 125 | #endif |
Helmut64 | 0:c43b6919ae15 | 126 | |
Helmut64 | 0:c43b6919ae15 | 127 | dprintf("SX1276 Ping Pong Demo Application" ); |
Helmut64 | 0:c43b6919ae15 | 128 | dprintf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0); |
Helmut64 | 0:c43b6919ae15 | 129 | dprintf("TXPower: %d dBm", TX_OUTPUT_POWER); |
Helmut64 | 0:c43b6919ae15 | 130 | #if USE_MODEM_LORA == 1 |
Helmut64 | 8:3b0d7b4ff28f | 131 | dprintf("Bandwidth: %d Hz", LORA_BANDWIDTH); |
Helmut64 | 0:c43b6919ae15 | 132 | dprintf("Spreading factor: SF%d", LORA_SPREADING_FACTOR); |
Helmut64 | 0:c43b6919ae15 | 133 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 134 | dprintf("Bandwidth: %d kHz", FSK_BANDWIDTH); |
Helmut64 | 0:c43b6919ae15 | 135 | dprintf("Baudrate: %d", FSK_DATARATE); |
Helmut64 | 0:c43b6919ae15 | 136 | #endif |
Helmut64 | 0:c43b6919ae15 | 137 | // Initialize Radio driver |
Helmut64 | 0:c43b6919ae15 | 138 | RadioEvents.TxDone = OnTxDone; |
Helmut64 | 0:c43b6919ae15 | 139 | RadioEvents.RxDone = OnRxDone; |
Helmut64 | 0:c43b6919ae15 | 140 | RadioEvents.RxError = OnRxError; |
Helmut64 | 0:c43b6919ae15 | 141 | RadioEvents.TxTimeout = OnTxTimeout; |
Helmut64 | 6:1b598b0e52e4 | 142 | RadioEvents.RxTimeout = OnRxTimeout; |
Helmut64 | 6:1b598b0e52e4 | 143 | if (Radio->Init( &RadioEvents ) == false) { |
Helmut64 | 6:1b598b0e52e4 | 144 | while(1) { |
Helmut64 | 6:1b598b0e52e4 | 145 | dprintf("Radio could not be detected!"); |
Helmut64 | 6:1b598b0e52e4 | 146 | wait( 1 ); |
Helmut64 | 6:1b598b0e52e4 | 147 | } |
Helmut64 | 0:c43b6919ae15 | 148 | } |
Helmut64 | 6:1b598b0e52e4 | 149 | |
Helmut64 | 0:c43b6919ae15 | 150 | |
Helmut64 | 0:c43b6919ae15 | 151 | switch(Radio->DetectBoardType()) { |
Helmut64 | 0:c43b6919ae15 | 152 | case SX1276MB1LAS: |
Helmut64 | 0:c43b6919ae15 | 153 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 154 | dprintf(" > Board Type: SX1276MB1LAS <"); |
Helmut64 | 0:c43b6919ae15 | 155 | break; |
Helmut64 | 0:c43b6919ae15 | 156 | case SX1276MB1MAS: |
Helmut64 | 0:c43b6919ae15 | 157 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 158 | dprintf(" > Board Type: SX1276MB1LAS <"); |
Helmut64 | 0:c43b6919ae15 | 159 | case MURATA_SX1276: |
Helmut64 | 0:c43b6919ae15 | 160 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 161 | dprintf(" > Board Type: MURATA_SX1276_STM32L0 <"); |
Helmut64 | 0:c43b6919ae15 | 162 | break; |
Helmut64 | 0:c43b6919ae15 | 163 | case RFM95_SX1276: |
Helmut64 | 0:c43b6919ae15 | 164 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 165 | dprintf(" > HopeRF RFM95xx <"); |
Helmut64 | 0:c43b6919ae15 | 166 | break; |
Helmut64 | 0:c43b6919ae15 | 167 | default: |
Helmut64 | 0:c43b6919ae15 | 168 | dprintf(" > Board Type: unknown <"); |
Helmut64 | 0:c43b6919ae15 | 169 | } |
Helmut64 | 0:c43b6919ae15 | 170 | |
Helmut64 | 0:c43b6919ae15 | 171 | Radio->SetChannel(RF_FREQUENCY ); |
Helmut64 | 0:c43b6919ae15 | 172 | |
Helmut64 | 0:c43b6919ae15 | 173 | #if USE_MODEM_LORA == 1 |
Helmut64 | 0:c43b6919ae15 | 174 | |
Helmut64 | 0:c43b6919ae15 | 175 | if (LORA_FHSS_ENABLED) |
Helmut64 | 0:c43b6919ae15 | 176 | dprintf(" > LORA FHSS Mode <"); |
Helmut64 | 0:c43b6919ae15 | 177 | if (!LORA_FHSS_ENABLED) |
Helmut64 | 0:c43b6919ae15 | 178 | dprintf(" > LORA Mode <"); |
dev_alexander | 25:1a031add188a | 179 | |
dev_alexander | 25:1a031add188a | 180 | |
dev_alexander | 25:1a031add188a | 181 | // Without this line, the default max payload length is only 64 bytes |
dev_alexander | 25:1a031add188a | 182 | Radio->SetMaxPayloadLength(MODEM_LORA, 255); |
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 ); |
dev_alexander | 22:abca9d17d13d | 220 | } |
dev_alexander | 22:abca9d17d13d | 221 | |
dev_alexander | 25:1a031add188a | 222 | /**************************************************************************************************************************************** |
dev_alexander | 25:1a031add188a | 223 | * |
dev_alexander | 25:1a031add188a | 224 | ****************************************************************************************************************************************/ |
dev_alexander | 22:abca9d17d13d | 225 | int SX1276PingPong(void) |
dev_alexander | 22:abca9d17d13d | 226 | { |
dev_alexander | 22:abca9d17d13d | 227 | #if defined(TARGET_MAX32630FTHR) // Master Device: Bluetooth Gateway |
dev_alexander | 22:abca9d17d13d | 228 | bool isMaster = true; |
dev_alexander | 23:f74a50977593 | 229 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: Robot |
dev_alexander | 22:abca9d17d13d | 230 | bool isMaster = false; |
Helmut64 | 0:c43b6919ae15 | 231 | #endif |
dev_alexander | 25:1a031add188a | 232 | |
dev_alexander | 22:abca9d17d13d | 233 | switch( State ) |
dev_alexander | 22:abca9d17d13d | 234 | { |
dev_alexander | 22:abca9d17d13d | 235 | case RX: |
dev_alexander | 28:0ed92c590607 | 236 | // *led3 = 0; |
dev_alexander | 22:abca9d17d13d | 237 | if( isMaster == true ) |
Helmut64 | 0:c43b6919ae15 | 238 | { |
dev_alexander | 24:e8d03912f303 | 239 | if( BufferSizeRx > 0 ) |
Helmut64 | 0:c43b6919ae15 | 240 | { |
dev_alexander | 28:0ed92c590607 | 241 | if( memcmp(&BufferRx[rx_idx_signature], PongMsg, sizeof(PongMsg)) == 0 ) |
Helmut64 | 0:c43b6919ae15 | 242 | { |
dev_alexander | 22:abca9d17d13d | 243 | // *led = !*led; |
dev_alexander | 22:abca9d17d13d | 244 | dprintf( "...Pong" ); |
dev_alexander | 22:abca9d17d13d | 245 | // Send the next PING frame |
dev_alexander | 25:1a031add188a | 246 | // memcpy(BufferTx, PingMsg, sizeof(PingMsg)); |
dev_alexander | 25:1a031add188a | 247 | /* |
dev_alexander | 22:abca9d17d13d | 248 | // We fill the buffer with numbers for the payload |
dev_alexander | 24:e8d03912f303 | 249 | for( i = sizeof(PingMsg); i < BufferSizeTx; i++ ) |
Helmut64 | 0:c43b6919ae15 | 250 | { |
dev_alexander | 24:e8d03912f303 | 251 | BufferTx[i] = i - sizeof(PingMsg); |
Helmut64 | 0:c43b6919ae15 | 252 | } |
dev_alexander | 25:1a031add188a | 253 | */ |
dev_alexander | 25:1a031add188a | 254 | /* Construct the payload buffer so data can be transmited. */ |
dev_alexander | 28:0ed92c590607 | 255 | // fillPayloadWithGlobalBufs(BufferTx); |
dev_alexander | 22:abca9d17d13d | 256 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 257 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 258 | } |
dev_alexander | 22:abca9d17d13d | 259 | /* else if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) |
dev_alexander | 22:abca9d17d13d | 260 | { // A master already exists then become a slave |
dev_alexander | 22:abca9d17d13d | 261 | dprintf( "...Ping" ); |
dev_alexander | 22:abca9d17d13d | 262 | // *led = !*led; |
dev_alexander | 22:abca9d17d13d | 263 | isMaster = false; |
dev_alexander | 22:abca9d17d13d | 264 | // Send the next PONG frame |
dev_alexander | 22:abca9d17d13d | 265 | memcpy(Buffer, PongMsg, sizeof(PongMsg)); |
dev_alexander | 22:abca9d17d13d | 266 | // We fill the buffer with numbers for the payload |
dev_alexander | 22:abca9d17d13d | 267 | for( i = sizeof(PongMsg); i < BufferSize; i++ ) |
dev_alexander | 22:abca9d17d13d | 268 | { |
dev_alexander | 22:abca9d17d13d | 269 | Buffer[i] = i - sizeof(PongMsg); |
Helmut64 | 0:c43b6919ae15 | 270 | } |
dev_alexander | 22:abca9d17d13d | 271 | wait_ms( 10 ); |
dev_alexander | 22:abca9d17d13d | 272 | Radio->Send( Buffer, BufferSize ); |
dev_alexander | 22:abca9d17d13d | 273 | } |
dev_alexander | 22:abca9d17d13d | 274 | */ |
dev_alexander | 22:abca9d17d13d | 275 | else // valid reception but neither a PING or a PONG message |
dev_alexander | 22:abca9d17d13d | 276 | { // Set device as master ans start again |
dev_alexander | 24:e8d03912f303 | 277 | //isMaster = true; |
dev_alexander | 22:abca9d17d13d | 278 | Radio->Rx( RX_TIMEOUT_VALUE ); |
Helmut64 | 0:c43b6919ae15 | 279 | } |
Helmut64 | 0:c43b6919ae15 | 280 | } |
dev_alexander | 22:abca9d17d13d | 281 | } |
dev_alexander | 22:abca9d17d13d | 282 | else |
dev_alexander | 22:abca9d17d13d | 283 | { |
dev_alexander | 24:e8d03912f303 | 284 | if( BufferSizeRx > 0 ) |
Helmut64 | 0:c43b6919ae15 | 285 | { |
dev_alexander | 28:0ed92c590607 | 286 | if( memcmp(BufferRx, PingMsg, sizeof(PingMsg)) == 0 ) |
Helmut64 | 0:c43b6919ae15 | 287 | { |
dev_alexander | 22:abca9d17d13d | 288 | // *led = !*led; |
dev_alexander | 22:abca9d17d13d | 289 | dprintf( "...Ping" ); |
dev_alexander | 22:abca9d17d13d | 290 | // Send the reply to the PING string |
dev_alexander | 25:1a031add188a | 291 | // memcpy(BufferTx, PongMsg, sizeof(PongMsg)); |
dev_alexander | 25:1a031add188a | 292 | /* |
dev_alexander | 22:abca9d17d13d | 293 | // We fill the buffer with numbers for the payload |
dev_alexander | 24:e8d03912f303 | 294 | for( i = sizeof(PongMsg); i < BufferSizeTx; i++ ) |
dev_alexander | 22:abca9d17d13d | 295 | { |
dev_alexander | 24:e8d03912f303 | 296 | BufferTx[i] = i - sizeof(PongMsg); |
dev_alexander | 22:abca9d17d13d | 297 | } |
dev_alexander | 25:1a031add188a | 298 | */ |
dev_alexander | 28:0ed92c590607 | 299 | // fillPayloadWithGlobalBufs(BufferTx); |
dev_alexander | 22:abca9d17d13d | 300 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 301 | Radio->Send( BufferTx, BufferSizeTx ); |
Helmut64 | 0:c43b6919ae15 | 302 | } |
dev_alexander | 22:abca9d17d13d | 303 | else // valid reception but not a PING as expected |
dev_alexander | 22:abca9d17d13d | 304 | { // Set device as master and start again |
dev_alexander | 22:abca9d17d13d | 305 | //isMaster = true; |
dev_alexander | 22:abca9d17d13d | 306 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 307 | } |
Helmut64 | 0:c43b6919ae15 | 308 | } |
dev_alexander | 22:abca9d17d13d | 309 | } |
dev_alexander | 22:abca9d17d13d | 310 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 311 | break; |
dev_alexander | 22:abca9d17d13d | 312 | case TX: |
dev_alexander | 28:0ed92c590607 | 313 | // *led3 = 1; |
dev_alexander | 22:abca9d17d13d | 314 | if( isMaster == true ) |
dev_alexander | 22:abca9d17d13d | 315 | { |
dev_alexander | 22:abca9d17d13d | 316 | dprintf("Ping..." ); |
dev_alexander | 22:abca9d17d13d | 317 | } |
dev_alexander | 22:abca9d17d13d | 318 | else |
dev_alexander | 22:abca9d17d13d | 319 | { |
dev_alexander | 22:abca9d17d13d | 320 | dprintf("Pong..." ); |
dev_alexander | 22:abca9d17d13d | 321 | } |
dev_alexander | 22:abca9d17d13d | 322 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 323 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 324 | break; |
dev_alexander | 22:abca9d17d13d | 325 | case RX_TIMEOUT: |
dev_alexander | 22:abca9d17d13d | 326 | if( isMaster == true ) |
dev_alexander | 22:abca9d17d13d | 327 | { |
dev_alexander | 22:abca9d17d13d | 328 | // Send the next PING frame |
dev_alexander | 25:1a031add188a | 329 | // memcpy(BufferTx, PingMsg, sizeof(PingMsg)); |
dev_alexander | 25:1a031add188a | 330 | /* |
dev_alexander | 24:e8d03912f303 | 331 | for( i = sizeof(PingMsg); i < BufferSizeTx; i++ ) |
Helmut64 | 0:c43b6919ae15 | 332 | { |
dev_alexander | 24:e8d03912f303 | 333 | BufferTx[i] = i - sizeof(PingMsg); |
Helmut64 | 0:c43b6919ae15 | 334 | } |
dev_alexander | 25:1a031add188a | 335 | */ |
dev_alexander | 28:0ed92c590607 | 336 | fillPayloadWithGlobalBufs(BufferTx); |
dev_alexander | 22:abca9d17d13d | 337 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 338 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 339 | } |
dev_alexander | 22:abca9d17d13d | 340 | else |
dev_alexander | 22:abca9d17d13d | 341 | { |
dev_alexander | 22:abca9d17d13d | 342 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 343 | } |
dev_alexander | 22:abca9d17d13d | 344 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 345 | break; |
dev_alexander | 22:abca9d17d13d | 346 | case RX_ERROR: |
dev_alexander | 22:abca9d17d13d | 347 | // We have received a Packet with a CRC error, send reply as if packet was correct |
dev_alexander | 22:abca9d17d13d | 348 | if( isMaster == true ) |
dev_alexander | 22:abca9d17d13d | 349 | { |
dev_alexander | 22:abca9d17d13d | 350 | // Send the next PING frame |
dev_alexander | 28:0ed92c590607 | 351 | memcpy(BufferTx, PingMsg, sizeof(PingMsg)); |
dev_alexander | 25:1a031add188a | 352 | /* |
dev_alexander | 24:e8d03912f303 | 353 | for( i = 4; i < BufferSizeTx; i++ ) |
Helmut64 | 0:c43b6919ae15 | 354 | { |
dev_alexander | 24:e8d03912f303 | 355 | BufferTx[i] = i - 4; |
Helmut64 | 0:c43b6919ae15 | 356 | } |
dev_alexander | 25:1a031add188a | 357 | */ |
dev_alexander | 28:0ed92c590607 | 358 | // fillPayloadWithGlobalBufs(BufferTx); |
dev_alexander | 22:abca9d17d13d | 359 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 360 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 361 | } |
dev_alexander | 22:abca9d17d13d | 362 | else |
dev_alexander | 22:abca9d17d13d | 363 | { |
dev_alexander | 22:abca9d17d13d | 364 | // Send the next PONG frame |
dev_alexander | 25:1a031add188a | 365 | // memcpy(BufferTx, PongMsg, sizeof(PongMsg)); |
dev_alexander | 25:1a031add188a | 366 | /* |
dev_alexander | 24:e8d03912f303 | 367 | for( i = sizeof(PongMsg); i < BufferSizeTx; i++ ) |
dev_alexander | 22:abca9d17d13d | 368 | { |
dev_alexander | 24:e8d03912f303 | 369 | BufferTx[i] = i - sizeof(PongMsg); |
dev_alexander | 22:abca9d17d13d | 370 | } |
dev_alexander | 25:1a031add188a | 371 | */ |
dev_alexander | 28:0ed92c590607 | 372 | // fillPayloadWithGlobalBufs(BufferTx); |
dev_alexander | 22:abca9d17d13d | 373 | wait_ms( 10 ); |
dev_alexander | 28:0ed92c590607 | 374 | Radio->Send( BufferTx, BufferSizeTx ); |
dev_alexander | 22:abca9d17d13d | 375 | } |
dev_alexander | 22:abca9d17d13d | 376 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 377 | break; |
dev_alexander | 22:abca9d17d13d | 378 | case TX_TIMEOUT: |
dev_alexander | 22:abca9d17d13d | 379 | Radio->Rx( RX_TIMEOUT_VALUE ); |
dev_alexander | 22:abca9d17d13d | 380 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 381 | break; |
dev_alexander | 22:abca9d17d13d | 382 | case LOWPOWER: |
dev_alexander | 22:abca9d17d13d | 383 | sleep(); |
dev_alexander | 22:abca9d17d13d | 384 | break; |
dev_alexander | 22:abca9d17d13d | 385 | default: |
dev_alexander | 22:abca9d17d13d | 386 | State = LOWPOWER; |
dev_alexander | 22:abca9d17d13d | 387 | break; |
dev_alexander | 22:abca9d17d13d | 388 | } |
Helmut64 | 0:c43b6919ae15 | 389 | } |
Helmut64 | 0:c43b6919ae15 | 390 | |
Helmut64 | 11:d3a591c20cd7 | 391 | void OnTxDone(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 392 | { |
Helmut64 | 0:c43b6919ae15 | 393 | Radio->Sleep( ); |
dev_alexander | 25:1a031add188a | 394 | State = TX; |
Helmut64 | 0:c43b6919ae15 | 395 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 396 | dprintf("> OnTxDone"); |
Helmut64 | 0:c43b6919ae15 | 397 | } |
Helmut64 | 0:c43b6919ae15 | 398 | |
Helmut64 | 11:d3a591c20cd7 | 399 | void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
Helmut64 | 0:c43b6919ae15 | 400 | { |
Helmut64 | 0:c43b6919ae15 | 401 | Radio->Sleep( ); |
dev_alexander | 28:0ed92c590607 | 402 | |
dev_alexander | 28:0ed92c590607 | 403 | if(BufferSizeRx != size) |
dev_alexander | 28:0ed92c590607 | 404 | memcpy( BufferRx, payload, size ); |
dev_alexander | 28:0ed92c590607 | 405 | else |
dev_alexander | 28:0ed92c590607 | 406 | memcpy( BufferRx, payload, BufferSizeRx ); |
Helmut64 | 0:c43b6919ae15 | 407 | State = RX; |
dev_alexander | 25:1a031add188a | 408 | |
dev_alexander | 25:1a031add188a | 409 | // Call function that deconstructs payload |
dev_alexander | 28:0ed92c590607 | 410 | // fillGlobalBufsWithPayload(payload); |
dev_alexander | 25:1a031add188a | 411 | |
Helmut64 | 0:c43b6919ae15 | 412 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 413 | dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr); |
Helmut64 | 0:c43b6919ae15 | 414 | dump("Data:", payload, size); |
Helmut64 | 0:c43b6919ae15 | 415 | } |
Helmut64 | 0:c43b6919ae15 | 416 | |
Helmut64 | 11:d3a591c20cd7 | 417 | void OnTxTimeout(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 418 | { |
dev_alexander | 28:0ed92c590607 | 419 | // *led3 = 0; |
Helmut64 | 0:c43b6919ae15 | 420 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 421 | State = TX_TIMEOUT; |
Helmut64 | 0:c43b6919ae15 | 422 | if(DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 423 | dprintf("> OnTxTimeout"); |
Helmut64 | 0:c43b6919ae15 | 424 | } |
Helmut64 | 0:c43b6919ae15 | 425 | |
Helmut64 | 11:d3a591c20cd7 | 426 | void OnRxTimeout(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 427 | { |
dev_alexander | 28:0ed92c590607 | 428 | // *led3 = 0; |
Helmut64 | 0:c43b6919ae15 | 429 | Radio->Sleep( ); |
dev_alexander | 28:0ed92c590607 | 430 | BufferRx[BufferSizeRx-1] = 0; |
Helmut64 | 0:c43b6919ae15 | 431 | State = RX_TIMEOUT; |
Helmut64 | 0:c43b6919ae15 | 432 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 433 | dprintf("> OnRxTimeout"); |
Helmut64 | 0:c43b6919ae15 | 434 | } |
Helmut64 | 0:c43b6919ae15 | 435 | |
Helmut64 | 11:d3a591c20cd7 | 436 | void OnRxError(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 437 | { |
Helmut64 | 0:c43b6919ae15 | 438 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 439 | State = RX_ERROR; |
Helmut64 | 0:c43b6919ae15 | 440 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 441 | dprintf("> OnRxError"); |
Helmut64 | 0:c43b6919ae15 | 442 | } |
Helmut64 | 0:c43b6919ae15 | 443 | |
Helmut64 | 0:c43b6919ae15 | 444 | #endif |