PingPong_REC
Dependencies: mbed BufferedSerial SX1276GenericLib
SX1276GenericPingPong/GenericPingPong.cpp@1:65d602e2d746, 2017-05-10 (annotated)
- Committer:
- Helmut64
- Date:
- Wed May 10 12:37:06 2017 +0000
- Revision:
- 1:65d602e2d746
- Parent:
- 0:c43b6919ae15
- Child:
- 3:dc560d3e9070
Added simple sleep to save power.
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 |
Helmut64 | 0:c43b6919ae15 | 22 | #define RF_FREQUENCY RF_FREQUENCY_868_1 // 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 | 0:c43b6919ae15 | 27 | #define LORA_BANDWIDTH LORA_BANKWIDTH_125kHz |
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 | |
Helmut64 | 0:c43b6919ae15 | 100 | int SX1276PingPong() |
Helmut64 | 0:c43b6919ae15 | 101 | { |
Helmut64 | 0:c43b6919ae15 | 102 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
Helmut64 | 0:c43b6919ae15 | 103 | DigitalOut *led = new DigitalOut(LED2); |
Helmut64 | 0:c43b6919ae15 | 104 | #elif defined(TARGET_NUCLEO_L073RZ) |
Helmut64 | 0:c43b6919ae15 | 105 | DigitalOut *led = new DigitalOut(LED4); // RX red |
Helmut64 | 0:c43b6919ae15 | 106 | led3 = new DigitalOut(LED3); // TX blue |
Helmut64 | 0:c43b6919ae15 | 107 | #else |
Helmut64 | 0:c43b6919ae15 | 108 | DigitalOut *led = new DigitalOut(LED1); |
Helmut64 | 0:c43b6919ae15 | 109 | led3 = led; |
Helmut64 | 0:c43b6919ae15 | 110 | #endif |
Helmut64 | 0:c43b6919ae15 | 111 | |
Helmut64 | 0:c43b6919ae15 | 112 | Buffer = new uint8_t[BUFFER_SIZE]; |
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 | uint8_t i; |
Helmut64 | 0:c43b6919ae15 | 128 | bool isMaster = true; |
Helmut64 | 0:c43b6919ae15 | 129 | |
Helmut64 | 0:c43b6919ae15 | 130 | dprintf("SX1276 Ping Pong Demo Application" ); |
Helmut64 | 0:c43b6919ae15 | 131 | dprintf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0); |
Helmut64 | 0:c43b6919ae15 | 132 | dprintf("TXPower: %d dBm", TX_OUTPUT_POWER); |
Helmut64 | 0:c43b6919ae15 | 133 | #if USE_MODEM_LORA == 1 |
Helmut64 | 0:c43b6919ae15 | 134 | if (LORA_BANDWIDTH == LORA_BANKWIDTH_125kHz) { |
Helmut64 | 0:c43b6919ae15 | 135 | dprintf("Bandwidth: %d kHz", 125); |
Helmut64 | 0:c43b6919ae15 | 136 | } else if (LORA_BANDWIDTH == LORA_BANKWIDTH_250kHz) { |
Helmut64 | 0:c43b6919ae15 | 137 | dprintf("Bandwidth: %d kHz", 250); |
Helmut64 | 0:c43b6919ae15 | 138 | } else if (LORA_BANDWIDTH == LORA_BANKWIDTH_500kHz) { |
Helmut64 | 0:c43b6919ae15 | 139 | dprintf("Bandwidth: %d kHz", 500); |
Helmut64 | 0:c43b6919ae15 | 140 | } else { |
Helmut64 | 0:c43b6919ae15 | 141 | dprintf("Unkown Bandwidth: %d kHz", LORA_BANDWIDTH); |
Helmut64 | 0:c43b6919ae15 | 142 | } |
Helmut64 | 0:c43b6919ae15 | 143 | dprintf("Spreading factor: SF%d", LORA_SPREADING_FACTOR); |
Helmut64 | 0:c43b6919ae15 | 144 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 145 | dprintf("Bandwidth: %d kHz", FSK_BANDWIDTH); |
Helmut64 | 0:c43b6919ae15 | 146 | dprintf("Baudrate: %d", FSK_DATARATE); |
Helmut64 | 0:c43b6919ae15 | 147 | #endif |
Helmut64 | 0:c43b6919ae15 | 148 | // Initialize Radio driver |
Helmut64 | 0:c43b6919ae15 | 149 | RadioEvents.TxDone = OnTxDone; |
Helmut64 | 0:c43b6919ae15 | 150 | RadioEvents.RxDone = OnRxDone; |
Helmut64 | 0:c43b6919ae15 | 151 | RadioEvents.RxError = OnRxError; |
Helmut64 | 0:c43b6919ae15 | 152 | RadioEvents.TxTimeout = OnTxTimeout; |
Helmut64 | 0:c43b6919ae15 | 153 | RadioEvents.RxTimeout = OnRxTimeout; |
Helmut64 | 0:c43b6919ae15 | 154 | Radio->Init( &RadioEvents ); |
Helmut64 | 0:c43b6919ae15 | 155 | |
Helmut64 | 0:c43b6919ae15 | 156 | // verify the connection with the board |
Helmut64 | 0:c43b6919ae15 | 157 | while( Radio->Read( REG_VERSION ) == 0x00 ) |
Helmut64 | 0:c43b6919ae15 | 158 | { |
Helmut64 | 0:c43b6919ae15 | 159 | dprintf("Radio could not be detected!"); |
Helmut64 | 0:c43b6919ae15 | 160 | wait( 1 ); |
Helmut64 | 0:c43b6919ae15 | 161 | } |
Helmut64 | 0:c43b6919ae15 | 162 | |
Helmut64 | 0:c43b6919ae15 | 163 | switch(Radio->DetectBoardType()) { |
Helmut64 | 0:c43b6919ae15 | 164 | case SX1276MB1LAS: |
Helmut64 | 0:c43b6919ae15 | 165 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 166 | dprintf(" > Board Type: SX1276MB1LAS <"); |
Helmut64 | 0:c43b6919ae15 | 167 | break; |
Helmut64 | 0:c43b6919ae15 | 168 | case SX1276MB1MAS: |
Helmut64 | 0:c43b6919ae15 | 169 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 170 | dprintf(" > Board Type: SX1276MB1LAS <"); |
Helmut64 | 0:c43b6919ae15 | 171 | case MURATA_SX1276: |
Helmut64 | 0:c43b6919ae15 | 172 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 173 | dprintf(" > Board Type: MURATA_SX1276_STM32L0 <"); |
Helmut64 | 0:c43b6919ae15 | 174 | break; |
Helmut64 | 0:c43b6919ae15 | 175 | case RFM95_SX1276: |
Helmut64 | 0:c43b6919ae15 | 176 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 177 | dprintf(" > HopeRF RFM95xx <"); |
Helmut64 | 0:c43b6919ae15 | 178 | break; |
Helmut64 | 0:c43b6919ae15 | 179 | default: |
Helmut64 | 0:c43b6919ae15 | 180 | dprintf(" > Board Type: unknown <"); |
Helmut64 | 0:c43b6919ae15 | 181 | } |
Helmut64 | 0:c43b6919ae15 | 182 | |
Helmut64 | 0:c43b6919ae15 | 183 | Radio->SetChannel(RF_FREQUENCY ); |
Helmut64 | 0:c43b6919ae15 | 184 | |
Helmut64 | 0:c43b6919ae15 | 185 | #if USE_MODEM_LORA == 1 |
Helmut64 | 0:c43b6919ae15 | 186 | |
Helmut64 | 0:c43b6919ae15 | 187 | if (LORA_FHSS_ENABLED) |
Helmut64 | 0:c43b6919ae15 | 188 | dprintf(" > LORA FHSS Mode <"); |
Helmut64 | 0:c43b6919ae15 | 189 | if (!LORA_FHSS_ENABLED) |
Helmut64 | 0:c43b6919ae15 | 190 | dprintf(" > LORA Mode <"); |
Helmut64 | 0:c43b6919ae15 | 191 | |
Helmut64 | 0:c43b6919ae15 | 192 | Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
Helmut64 | 0:c43b6919ae15 | 193 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
Helmut64 | 0:c43b6919ae15 | 194 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
Helmut64 | 0:c43b6919ae15 | 195 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
Helmut64 | 0:c43b6919ae15 | 196 | LORA_IQ_INVERSION_ON, 2000 ); |
Helmut64 | 0:c43b6919ae15 | 197 | |
Helmut64 | 0:c43b6919ae15 | 198 | Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
Helmut64 | 0:c43b6919ae15 | 199 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
Helmut64 | 0:c43b6919ae15 | 200 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
Helmut64 | 0:c43b6919ae15 | 201 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
Helmut64 | 0:c43b6919ae15 | 202 | LORA_IQ_INVERSION_ON, true ); |
Helmut64 | 0:c43b6919ae15 | 203 | |
Helmut64 | 0:c43b6919ae15 | 204 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 205 | |
Helmut64 | 0:c43b6919ae15 | 206 | dprintf(" > FSK Mode <"); |
Helmut64 | 0:c43b6919ae15 | 207 | Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
Helmut64 | 0:c43b6919ae15 | 208 | FSK_DATARATE, 0, |
Helmut64 | 0:c43b6919ae15 | 209 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
Helmut64 | 0:c43b6919ae15 | 210 | FSK_CRC_ENABLED, 0, 0, 0, 2000 ); |
Helmut64 | 0:c43b6919ae15 | 211 | |
Helmut64 | 0:c43b6919ae15 | 212 | Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
Helmut64 | 0:c43b6919ae15 | 213 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
Helmut64 | 0:c43b6919ae15 | 214 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
Helmut64 | 0:c43b6919ae15 | 215 | 0, 0, false, true ); |
Helmut64 | 0:c43b6919ae15 | 216 | |
Helmut64 | 0:c43b6919ae15 | 217 | #else |
Helmut64 | 0:c43b6919ae15 | 218 | |
Helmut64 | 0:c43b6919ae15 | 219 | #error "Please define a modem in the compiler options." |
Helmut64 | 0:c43b6919ae15 | 220 | |
Helmut64 | 0:c43b6919ae15 | 221 | #endif |
Helmut64 | 0:c43b6919ae15 | 222 | |
Helmut64 | 0:c43b6919ae15 | 223 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 224 | dprintf("Starting Ping-Pong loop"); |
Helmut64 | 0:c43b6919ae15 | 225 | |
Helmut64 | 0:c43b6919ae15 | 226 | |
Helmut64 | 0:c43b6919ae15 | 227 | Radio->Rx( RX_TIMEOUT_VALUE ); |
Helmut64 | 0:c43b6919ae15 | 228 | |
Helmut64 | 0:c43b6919ae15 | 229 | while( 1 ) |
Helmut64 | 0:c43b6919ae15 | 230 | { |
Helmut64 | 0:c43b6919ae15 | 231 | #ifdef TARGET_STM32L4 |
Helmut64 | 0:c43b6919ae15 | 232 | WatchDogUpdate(); |
Helmut64 | 0:c43b6919ae15 | 233 | #endif |
Helmut64 | 0:c43b6919ae15 | 234 | |
Helmut64 | 0:c43b6919ae15 | 235 | switch( State ) |
Helmut64 | 0:c43b6919ae15 | 236 | { |
Helmut64 | 0:c43b6919ae15 | 237 | case RX: |
Helmut64 | 0:c43b6919ae15 | 238 | *led3 = 0; |
Helmut64 | 0:c43b6919ae15 | 239 | if( isMaster == true ) |
Helmut64 | 0:c43b6919ae15 | 240 | { |
Helmut64 | 0:c43b6919ae15 | 241 | if( BufferSize > 0 ) |
Helmut64 | 0:c43b6919ae15 | 242 | { |
Helmut64 | 0:c43b6919ae15 | 243 | if( memcmp(Buffer, PongMsg, sizeof(PongMsg)) == 0 ) |
Helmut64 | 0:c43b6919ae15 | 244 | { |
Helmut64 | 0:c43b6919ae15 | 245 | *led = !*led; |
Helmut64 | 0:c43b6919ae15 | 246 | dprintf( "...Pong" ); |
Helmut64 | 0:c43b6919ae15 | 247 | // Send the next PING frame |
Helmut64 | 0:c43b6919ae15 | 248 | memcpy(Buffer, PingMsg, sizeof(PingMsg)); |
Helmut64 | 0:c43b6919ae15 | 249 | // We fill the buffer with numbers for the payload |
Helmut64 | 0:c43b6919ae15 | 250 | for( i = sizeof(PingMsg); i < BufferSize; i++ ) |
Helmut64 | 0:c43b6919ae15 | 251 | { |
Helmut64 | 0:c43b6919ae15 | 252 | Buffer[i] = i - sizeof(PingMsg); |
Helmut64 | 0:c43b6919ae15 | 253 | } |
Helmut64 | 0:c43b6919ae15 | 254 | wait_ms( 10 ); |
Helmut64 | 0:c43b6919ae15 | 255 | Radio->Send( Buffer, BufferSize ); |
Helmut64 | 0:c43b6919ae15 | 256 | } |
Helmut64 | 0:c43b6919ae15 | 257 | else if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) |
Helmut64 | 0:c43b6919ae15 | 258 | { // A master already exists then become a slave |
Helmut64 | 0:c43b6919ae15 | 259 | dprintf( "...Ping" ); |
Helmut64 | 0:c43b6919ae15 | 260 | *led = !*led; |
Helmut64 | 0:c43b6919ae15 | 261 | isMaster = false; |
Helmut64 | 0:c43b6919ae15 | 262 | // Send the next PONG frame |
Helmut64 | 0:c43b6919ae15 | 263 | memcpy(Buffer, PongMsg, sizeof(PongMsg)); |
Helmut64 | 0:c43b6919ae15 | 264 | // We fill the buffer with numbers for the payload |
Helmut64 | 0:c43b6919ae15 | 265 | for( i = sizeof(PongMsg); i < BufferSize; i++ ) |
Helmut64 | 0:c43b6919ae15 | 266 | { |
Helmut64 | 0:c43b6919ae15 | 267 | Buffer[i] = i - sizeof(PongMsg); |
Helmut64 | 0:c43b6919ae15 | 268 | } |
Helmut64 | 0:c43b6919ae15 | 269 | wait_ms( 10 ); |
Helmut64 | 0:c43b6919ae15 | 270 | Radio->Send( Buffer, BufferSize ); |
Helmut64 | 0:c43b6919ae15 | 271 | } |
Helmut64 | 0:c43b6919ae15 | 272 | else // valid reception but neither a PING or a PONG message |
Helmut64 | 0:c43b6919ae15 | 273 | { // Set device as master ans start again |
Helmut64 | 0:c43b6919ae15 | 274 | isMaster = true; |
Helmut64 | 0:c43b6919ae15 | 275 | Radio->Rx( RX_TIMEOUT_VALUE ); |
Helmut64 | 0:c43b6919ae15 | 276 | } |
Helmut64 | 0:c43b6919ae15 | 277 | } |
Helmut64 | 0:c43b6919ae15 | 278 | } |
Helmut64 | 0:c43b6919ae15 | 279 | else |
Helmut64 | 0:c43b6919ae15 | 280 | { |
Helmut64 | 0:c43b6919ae15 | 281 | if( BufferSize > 0 ) |
Helmut64 | 0:c43b6919ae15 | 282 | { |
Helmut64 | 0:c43b6919ae15 | 283 | if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) |
Helmut64 | 0:c43b6919ae15 | 284 | { |
Helmut64 | 0:c43b6919ae15 | 285 | *led = !*led; |
Helmut64 | 0:c43b6919ae15 | 286 | dprintf( "...Ping" ); |
Helmut64 | 0:c43b6919ae15 | 287 | // Send the reply to the PING string |
Helmut64 | 0:c43b6919ae15 | 288 | memcpy(Buffer, PongMsg, sizeof(PongMsg)); |
Helmut64 | 0:c43b6919ae15 | 289 | // We fill the buffer with numbers for the payload |
Helmut64 | 0:c43b6919ae15 | 290 | for( i = sizeof(PongMsg); i < BufferSize; i++ ) |
Helmut64 | 0:c43b6919ae15 | 291 | { |
Helmut64 | 0:c43b6919ae15 | 292 | Buffer[i] = i - sizeof(PongMsg); |
Helmut64 | 0:c43b6919ae15 | 293 | } |
Helmut64 | 0:c43b6919ae15 | 294 | wait_ms( 10 ); |
Helmut64 | 0:c43b6919ae15 | 295 | Radio->Send( Buffer, BufferSize ); |
Helmut64 | 0:c43b6919ae15 | 296 | } |
Helmut64 | 0:c43b6919ae15 | 297 | else // valid reception but not a PING as expected |
Helmut64 | 0:c43b6919ae15 | 298 | { // Set device as master and start again |
Helmut64 | 0:c43b6919ae15 | 299 | isMaster = true; |
Helmut64 | 0:c43b6919ae15 | 300 | Radio->Rx( RX_TIMEOUT_VALUE ); |
Helmut64 | 0:c43b6919ae15 | 301 | } |
Helmut64 | 0:c43b6919ae15 | 302 | } |
Helmut64 | 0:c43b6919ae15 | 303 | } |
Helmut64 | 0:c43b6919ae15 | 304 | State = LOWPOWER; |
Helmut64 | 0:c43b6919ae15 | 305 | break; |
Helmut64 | 0:c43b6919ae15 | 306 | case TX: |
Helmut64 | 0:c43b6919ae15 | 307 | *led3 = 1; |
Helmut64 | 0:c43b6919ae15 | 308 | if( isMaster == true ) |
Helmut64 | 0:c43b6919ae15 | 309 | { |
Helmut64 | 0:c43b6919ae15 | 310 | dprintf("Ping..." ); |
Helmut64 | 0:c43b6919ae15 | 311 | } |
Helmut64 | 0:c43b6919ae15 | 312 | else |
Helmut64 | 0:c43b6919ae15 | 313 | { |
Helmut64 | 0:c43b6919ae15 | 314 | dprintf("Pong..." ); |
Helmut64 | 0:c43b6919ae15 | 315 | } |
Helmut64 | 0:c43b6919ae15 | 316 | Radio->Rx( RX_TIMEOUT_VALUE ); |
Helmut64 | 0:c43b6919ae15 | 317 | State = LOWPOWER; |
Helmut64 | 0:c43b6919ae15 | 318 | break; |
Helmut64 | 0:c43b6919ae15 | 319 | case RX_TIMEOUT: |
Helmut64 | 0:c43b6919ae15 | 320 | if( isMaster == true ) |
Helmut64 | 0:c43b6919ae15 | 321 | { |
Helmut64 | 0:c43b6919ae15 | 322 | // Send the next PING frame |
Helmut64 | 0:c43b6919ae15 | 323 | memcpy(Buffer, PingMsg, sizeof(PingMsg)); |
Helmut64 | 0:c43b6919ae15 | 324 | for( i = sizeof(PingMsg); i < BufferSize; i++ ) |
Helmut64 | 0:c43b6919ae15 | 325 | { |
Helmut64 | 0:c43b6919ae15 | 326 | Buffer[i] = i - sizeof(PingMsg); |
Helmut64 | 0:c43b6919ae15 | 327 | } |
Helmut64 | 0:c43b6919ae15 | 328 | wait_ms( 10 ); |
Helmut64 | 0:c43b6919ae15 | 329 | Radio->Send( Buffer, BufferSize ); |
Helmut64 | 0:c43b6919ae15 | 330 | } |
Helmut64 | 0:c43b6919ae15 | 331 | else |
Helmut64 | 0:c43b6919ae15 | 332 | { |
Helmut64 | 0:c43b6919ae15 | 333 | Radio->Rx( RX_TIMEOUT_VALUE ); |
Helmut64 | 0:c43b6919ae15 | 334 | } |
Helmut64 | 0:c43b6919ae15 | 335 | State = LOWPOWER; |
Helmut64 | 0:c43b6919ae15 | 336 | break; |
Helmut64 | 0:c43b6919ae15 | 337 | case RX_ERROR: |
Helmut64 | 0:c43b6919ae15 | 338 | // We have received a Packet with a CRC error, send reply as if packet was correct |
Helmut64 | 0:c43b6919ae15 | 339 | if( isMaster == true ) |
Helmut64 | 0:c43b6919ae15 | 340 | { |
Helmut64 | 0:c43b6919ae15 | 341 | // Send the next PING frame |
Helmut64 | 0:c43b6919ae15 | 342 | memcpy(Buffer, PingMsg, sizeof(PingMsg)); |
Helmut64 | 0:c43b6919ae15 | 343 | for( i = 4; i < BufferSize; i++ ) |
Helmut64 | 0:c43b6919ae15 | 344 | { |
Helmut64 | 0:c43b6919ae15 | 345 | Buffer[i] = i - 4; |
Helmut64 | 0:c43b6919ae15 | 346 | } |
Helmut64 | 0:c43b6919ae15 | 347 | wait_ms( 10 ); |
Helmut64 | 0:c43b6919ae15 | 348 | Radio->Send( Buffer, BufferSize ); |
Helmut64 | 0:c43b6919ae15 | 349 | } |
Helmut64 | 0:c43b6919ae15 | 350 | else |
Helmut64 | 0:c43b6919ae15 | 351 | { |
Helmut64 | 0:c43b6919ae15 | 352 | // Send the next PONG frame |
Helmut64 | 0:c43b6919ae15 | 353 | memcpy(Buffer, PongMsg, sizeof(PongMsg)); |
Helmut64 | 0:c43b6919ae15 | 354 | for( i = sizeof(PongMsg); i < BufferSize; i++ ) |
Helmut64 | 0:c43b6919ae15 | 355 | { |
Helmut64 | 0:c43b6919ae15 | 356 | Buffer[i] = i - sizeof(PongMsg); |
Helmut64 | 0:c43b6919ae15 | 357 | } |
Helmut64 | 0:c43b6919ae15 | 358 | wait_ms( 10 ); |
Helmut64 | 0:c43b6919ae15 | 359 | Radio->Send( Buffer, BufferSize ); |
Helmut64 | 0:c43b6919ae15 | 360 | } |
Helmut64 | 0:c43b6919ae15 | 361 | State = LOWPOWER; |
Helmut64 | 0:c43b6919ae15 | 362 | break; |
Helmut64 | 0:c43b6919ae15 | 363 | case TX_TIMEOUT: |
Helmut64 | 0:c43b6919ae15 | 364 | Radio->Rx( RX_TIMEOUT_VALUE ); |
Helmut64 | 0:c43b6919ae15 | 365 | State = LOWPOWER; |
Helmut64 | 0:c43b6919ae15 | 366 | break; |
Helmut64 | 0:c43b6919ae15 | 367 | case LOWPOWER: |
Helmut64 | 1:65d602e2d746 | 368 | sleep(); |
Helmut64 | 0:c43b6919ae15 | 369 | break; |
Helmut64 | 0:c43b6919ae15 | 370 | default: |
Helmut64 | 0:c43b6919ae15 | 371 | State = LOWPOWER; |
Helmut64 | 0:c43b6919ae15 | 372 | break; |
Helmut64 | 0:c43b6919ae15 | 373 | } |
Helmut64 | 0:c43b6919ae15 | 374 | } |
Helmut64 | 0:c43b6919ae15 | 375 | } |
Helmut64 | 0:c43b6919ae15 | 376 | |
Helmut64 | 0:c43b6919ae15 | 377 | void OnTxDone( void ) |
Helmut64 | 0:c43b6919ae15 | 378 | { |
Helmut64 | 0:c43b6919ae15 | 379 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 380 | State = TX; |
Helmut64 | 0:c43b6919ae15 | 381 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 382 | dprintf("> OnTxDone"); |
Helmut64 | 0:c43b6919ae15 | 383 | } |
Helmut64 | 0:c43b6919ae15 | 384 | |
Helmut64 | 0:c43b6919ae15 | 385 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
Helmut64 | 0:c43b6919ae15 | 386 | { |
Helmut64 | 0:c43b6919ae15 | 387 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 388 | BufferSize = size; |
Helmut64 | 0:c43b6919ae15 | 389 | memcpy( Buffer, payload, BufferSize ); |
Helmut64 | 0:c43b6919ae15 | 390 | State = RX; |
Helmut64 | 0:c43b6919ae15 | 391 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 392 | dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr); |
Helmut64 | 0:c43b6919ae15 | 393 | dump("Data:", payload, size); |
Helmut64 | 0:c43b6919ae15 | 394 | } |
Helmut64 | 0:c43b6919ae15 | 395 | |
Helmut64 | 0:c43b6919ae15 | 396 | void OnTxTimeout( void ) |
Helmut64 | 0:c43b6919ae15 | 397 | { |
Helmut64 | 0:c43b6919ae15 | 398 | *led3 = 0; |
Helmut64 | 0:c43b6919ae15 | 399 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 400 | State = TX_TIMEOUT; |
Helmut64 | 0:c43b6919ae15 | 401 | if(DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 402 | dprintf("> OnTxTimeout"); |
Helmut64 | 0:c43b6919ae15 | 403 | } |
Helmut64 | 0:c43b6919ae15 | 404 | |
Helmut64 | 0:c43b6919ae15 | 405 | void OnRxTimeout( void ) |
Helmut64 | 0:c43b6919ae15 | 406 | { |
Helmut64 | 0:c43b6919ae15 | 407 | *led3 = 0; |
Helmut64 | 0:c43b6919ae15 | 408 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 409 | Buffer[BufferSize-1] = 0; |
Helmut64 | 0:c43b6919ae15 | 410 | State = RX_TIMEOUT; |
Helmut64 | 0:c43b6919ae15 | 411 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 412 | dprintf("> OnRxTimeout"); |
Helmut64 | 0:c43b6919ae15 | 413 | } |
Helmut64 | 0:c43b6919ae15 | 414 | |
Helmut64 | 0:c43b6919ae15 | 415 | void OnRxError( void ) |
Helmut64 | 0:c43b6919ae15 | 416 | { |
Helmut64 | 0:c43b6919ae15 | 417 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 418 | State = RX_ERROR; |
Helmut64 | 0:c43b6919ae15 | 419 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 420 | dprintf("> OnRxError"); |
Helmut64 | 0:c43b6919ae15 | 421 | } |
Helmut64 | 0:c43b6919ae15 | 422 | |
Helmut64 | 0:c43b6919ae15 | 423 | #endif |