Simple Ping-Pong demo application between two SX1280 demo boards. Application demonstrating simple Tx/Rx between two boards. By default, each board starts as a "master" and will transmit a "Ping" message, and then wait for an answer. The first board receiving a "Ping" message will become a slave and answer the "master" with a "Pong". The Ping-Pong is then started

Dependencies:   SX1280Lib mbed

This code is made to run without the screen and the user may observe some glitches on the display if it remains connected during the Ping Pong demo.

Committer:
GregCr
Date:
Wed Mar 15 13:16:44 2017 +0000
Revision:
1:22e02d1cfbca
Parent:
0:eb7d6a52f42d
candidate release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GregCr 1:22e02d1cfbca 1 /*
GregCr 1:22e02d1cfbca 2 ______ _
GregCr 1:22e02d1cfbca 3 / _____) _ | |
GregCr 1:22e02d1cfbca 4 ( (____ _____ ____ _| |_ _____ ____| |__
GregCr 1:22e02d1cfbca 5 \____ \| ___ | (_ _) ___ |/ ___) _ \
GregCr 1:22e02d1cfbca 6 _____) ) ____| | | || |_| ____( (___| | | |
GregCr 1:22e02d1cfbca 7 (______/|_____)_|_|_| \__)_____)\____)_| |_|
GregCr 1:22e02d1cfbca 8 (C)2017 Semtech
GregCr 1:22e02d1cfbca 9
GregCr 1:22e02d1cfbca 10 Description: Main program
GregCr 1:22e02d1cfbca 11
GregCr 1:22e02d1cfbca 12 Maintainer: Gregory Cristian & Gilbert Menth
GregCr 1:22e02d1cfbca 13 */
GregCr 1:22e02d1cfbca 14
GregCr 1:22e02d1cfbca 15 #include "mbed.h"
GregCr 1:22e02d1cfbca 16 #include "radio.h"
GregCr 1:22e02d1cfbca 17 #include "sx1280-hal.h"
GregCr 1:22e02d1cfbca 18
GregCr 1:22e02d1cfbca 19
GregCr 1:22e02d1cfbca 20 /*!
GregCr 1:22e02d1cfbca 21 * \brief Used to display firmware version on RS232
GregCr 1:22e02d1cfbca 22 */
GregCr 1:22e02d1cfbca 23 #define FIRMWARE_VERSION ( ( char* )"Firmware Version: 170314A" )
GregCr 1:22e02d1cfbca 24
GregCr 1:22e02d1cfbca 25 /*!
GregCr 1:22e02d1cfbca 26 * Use the Ping Ping in uncommented Mode
GregCr 1:22e02d1cfbca 27 */
GregCr 1:22e02d1cfbca 28 //#define MODE_BLE
GregCr 1:22e02d1cfbca 29 #define MODE_LORA
GregCr 1:22e02d1cfbca 30 //#define MODE_GENERIC
GregCr 1:22e02d1cfbca 31 //#define MODE_FLRC
GregCr 1:22e02d1cfbca 32
GregCr 1:22e02d1cfbca 33
GregCr 1:22e02d1cfbca 34 /*!
GregCr 1:22e02d1cfbca 35 * \brief Defines the nominal frequency
GregCr 1:22e02d1cfbca 36 */
GregCr 1:22e02d1cfbca 37 #define RF_FREQUENCY 2400000000UL // Hz
GregCr 1:22e02d1cfbca 38
GregCr 1:22e02d1cfbca 39 /*!
GregCr 1:22e02d1cfbca 40 * \brief Defines the output power in dBm
GregCr 1:22e02d1cfbca 41 *
GregCr 1:22e02d1cfbca 42 * \remark The range of the output power is [-18..+13] dBm
GregCr 1:22e02d1cfbca 43 */
GregCr 1:22e02d1cfbca 44 #define TX_OUTPUT_POWER 13
GregCr 1:22e02d1cfbca 45
GregCr 1:22e02d1cfbca 46 /*!
GregCr 1:22e02d1cfbca 47 * \brief Defines the states of the application
GregCr 1:22e02d1cfbca 48 */
GregCr 1:22e02d1cfbca 49 typedef enum
GregCr 1:22e02d1cfbca 50 {
GregCr 1:22e02d1cfbca 51 APP_LOWPOWER,
GregCr 1:22e02d1cfbca 52 APP_RX,
GregCr 1:22e02d1cfbca 53 APP_RX_TIMEOUT,
GregCr 1:22e02d1cfbca 54 APP_RX_ERROR,
GregCr 1:22e02d1cfbca 55 APP_TX,
GregCr 1:22e02d1cfbca 56 APP_TX_TIMEOUT,
GregCr 1:22e02d1cfbca 57 }AppStates_t;
GregCr 1:22e02d1cfbca 58
GregCr 1:22e02d1cfbca 59 /*!
GregCr 1:22e02d1cfbca 60 * \brief Defines the buffer size, i.e. the payload size
GregCr 1:22e02d1cfbca 61 */
GregCr 1:22e02d1cfbca 62 #define BUFFER_SIZE 16
GregCr 1:22e02d1cfbca 63
GregCr 1:22e02d1cfbca 64 /*!
GregCr 1:22e02d1cfbca 65 * \brief Define the possible message type for this application
GregCr 1:22e02d1cfbca 66 */
GregCr 1:22e02d1cfbca 67 const uint8_t PingMsg[] = "PING";
GregCr 1:22e02d1cfbca 68 const uint8_t PongMsg[] = "PONG";
GregCr 1:22e02d1cfbca 69
GregCr 1:22e02d1cfbca 70 /*!
GregCr 1:22e02d1cfbca 71 * \brief Defines the size of the token defining message type in the payload
GregCr 1:22e02d1cfbca 72 */
GregCr 1:22e02d1cfbca 73 #define PINGPONGSIZE 4
GregCr 1:22e02d1cfbca 74
GregCr 1:22e02d1cfbca 75 /*!
GregCr 1:22e02d1cfbca 76 * \brief The size of the buffer
GregCr 1:22e02d1cfbca 77 */
GregCr 1:22e02d1cfbca 78 uint8_t BufferSize = BUFFER_SIZE;
GregCr 1:22e02d1cfbca 79
GregCr 1:22e02d1cfbca 80 /*!
GregCr 1:22e02d1cfbca 81 * \brief The buffer
GregCr 1:22e02d1cfbca 82 */
GregCr 1:22e02d1cfbca 83 uint8_t Buffer[BUFFER_SIZE];
GregCr 1:22e02d1cfbca 84
GregCr 1:22e02d1cfbca 85 /*!
GregCr 1:22e02d1cfbca 86 * \brief The State of the application
GregCr 1:22e02d1cfbca 87 */
GregCr 1:22e02d1cfbca 88 AppStates_t AppState = APP_LOWPOWER;
GregCr 1:22e02d1cfbca 89
GregCr 1:22e02d1cfbca 90 int8_t RssiValue = 0;
GregCr 1:22e02d1cfbca 91 int8_t SnrValue = 0;
GregCr 1:22e02d1cfbca 92
GregCr 1:22e02d1cfbca 93 /*!
GregCr 1:22e02d1cfbca 94 * \brief Function to be executed on Radio Tx Done event
GregCr 1:22e02d1cfbca 95 */
GregCr 1:22e02d1cfbca 96 void OnTxDone( void );
GregCr 1:22e02d1cfbca 97
GregCr 1:22e02d1cfbca 98 /*!
GregCr 1:22e02d1cfbca 99 * \brief Function to be executed on Radio Rx Done event
GregCr 1:22e02d1cfbca 100 */
GregCr 1:22e02d1cfbca 101 void OnRxDone( void );
GregCr 1:22e02d1cfbca 102
GregCr 1:22e02d1cfbca 103 /*!
GregCr 1:22e02d1cfbca 104 * \brief Function executed on Radio Tx Timeout event
GregCr 1:22e02d1cfbca 105 */
GregCr 1:22e02d1cfbca 106 void OnTxTimeout( void );
GregCr 1:22e02d1cfbca 107
GregCr 1:22e02d1cfbca 108 /*!
GregCr 1:22e02d1cfbca 109 * \brief Function executed on Radio Rx Timeout event
GregCr 1:22e02d1cfbca 110 */
GregCr 1:22e02d1cfbca 111 void OnRxTimeout( void );
GregCr 1:22e02d1cfbca 112
GregCr 1:22e02d1cfbca 113 /*!
GregCr 1:22e02d1cfbca 114 * \brief Function executed on Radio Rx Error event
GregCr 1:22e02d1cfbca 115 */
GregCr 1:22e02d1cfbca 116 void OnRxError( IrqErrorCode_t );
GregCr 1:22e02d1cfbca 117
GregCr 1:22e02d1cfbca 118 /*!
GregCr 1:22e02d1cfbca 119 * \brief All the callbacks are stored in a structure
GregCr 1:22e02d1cfbca 120 */
GregCr 1:22e02d1cfbca 121 RadioCallbacks_t callbacks =
GregCr 1:22e02d1cfbca 122 {
GregCr 1:22e02d1cfbca 123 &OnTxDone, // txDone
GregCr 1:22e02d1cfbca 124 &OnRxDone, // rxDone
GregCr 1:22e02d1cfbca 125 NULL, // syncWordDone
GregCr 1:22e02d1cfbca 126 NULL, // headerDone
GregCr 1:22e02d1cfbca 127 &OnTxTimeout, // txTimeout
GregCr 1:22e02d1cfbca 128 &OnRxTimeout, // rxTimeout
GregCr 1:22e02d1cfbca 129 &OnRxError, // rxError
GregCr 1:22e02d1cfbca 130 NULL, // rangingDone
GregCr 1:22e02d1cfbca 131 NULL, // cadDone
GregCr 1:22e02d1cfbca 132 };
GregCr 1:22e02d1cfbca 133
GregCr 1:22e02d1cfbca 134 // mosi, miso, sclk, nss, busy, dio1, dio2, dio3, rst, callbacks...
GregCr 1:22e02d1cfbca 135 SX1280Hal Radio( D11, D12, D13, D7, D3, D5, NC, NC, A0, &callbacks );
GregCr 1:22e02d1cfbca 136
GregCr 1:22e02d1cfbca 137 DigitalOut ANT_SW( A3 );
GregCr 1:22e02d1cfbca 138 DigitalOut TxLed( A4 );
GregCr 1:22e02d1cfbca 139 DigitalOut RxLed( A5 );
GregCr 1:22e02d1cfbca 140
GregCr 1:22e02d1cfbca 141 /*!
GregCr 1:22e02d1cfbca 142 * \brief Define IO for Unused Pin
GregCr 1:22e02d1cfbca 143 */
GregCr 1:22e02d1cfbca 144 DigitalOut F_CS( D6 ); // MBED description of pin
GregCr 1:22e02d1cfbca 145 DigitalOut SD_CS( D8 ); // MBED description of pin
GregCr 1:22e02d1cfbca 146
GregCr 1:22e02d1cfbca 147 /*!
GregCr 1:22e02d1cfbca 148 * \brief Number of tick size steps for tx timeout
GregCr 1:22e02d1cfbca 149 */
GregCr 1:22e02d1cfbca 150 #define TX_TIMEOUT_VALUE 100 // ms
GregCr 1:22e02d1cfbca 151
GregCr 1:22e02d1cfbca 152 /*!
GregCr 1:22e02d1cfbca 153 * \brief Number of tick size steps for rx timeout
GregCr 1:22e02d1cfbca 154 */
GregCr 1:22e02d1cfbca 155 #define RX_TIMEOUT_VALUE 100 // ms
GregCr 1:22e02d1cfbca 156
GregCr 1:22e02d1cfbca 157 /*!
GregCr 1:22e02d1cfbca 158 * \brief Size of ticks (used for Tx and Rx timeout)
GregCr 1:22e02d1cfbca 159 */
GregCr 1:22e02d1cfbca 160 #define RX_TIMEOUT_TICK_SIZE RADIO_TICK_SIZE_1000_US
GregCr 1:22e02d1cfbca 161
GregCr 1:22e02d1cfbca 162 /*!
GregCr 1:22e02d1cfbca 163 * \brief Mask of IRQs to listen to in rx mode
GregCr 1:22e02d1cfbca 164 */
GregCr 1:22e02d1cfbca 165 uint16_t RxIrqMask = IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT;
GregCr 1:22e02d1cfbca 166
GregCr 1:22e02d1cfbca 167 /*!
GregCr 1:22e02d1cfbca 168 * \brief Mask of IRQs to listen to in tx mode
GregCr 1:22e02d1cfbca 169 */
GregCr 1:22e02d1cfbca 170 uint16_t TxIrqMask = IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT;
GregCr 1:22e02d1cfbca 171
GregCr 1:22e02d1cfbca 172 /*!
GregCr 1:22e02d1cfbca 173 * \brief Locals parameters and status for radio API
GregCr 1:22e02d1cfbca 174 * NEED TO BE OPTIMIZED, COPY OF STUCTURE ALREADY EXISTING
GregCr 1:22e02d1cfbca 175 */
GregCr 1:22e02d1cfbca 176 PacketParams_t PacketParams;
GregCr 1:22e02d1cfbca 177 PacketStatus_t PacketStatus;
GregCr 1:22e02d1cfbca 178
GregCr 1:22e02d1cfbca 179 /*!
GregCr 1:22e02d1cfbca 180 * \brief Specify serial datarate for UART debug output
GregCr 1:22e02d1cfbca 181 */
GregCr 1:22e02d1cfbca 182 void baud( int baudrate )
GregCr 1:22e02d1cfbca 183 {
GregCr 1:22e02d1cfbca 184 Serial s( USBTX, USBRX );
GregCr 1:22e02d1cfbca 185
GregCr 1:22e02d1cfbca 186 s.baud( baudrate );
GregCr 1:22e02d1cfbca 187 }
GregCr 1:22e02d1cfbca 188
GregCr 1:22e02d1cfbca 189 int main( )
GregCr 1:22e02d1cfbca 190 {
GregCr 1:22e02d1cfbca 191 bool isMaster = true;
GregCr 1:22e02d1cfbca 192 ModulationParams_t modulationParams;
GregCr 1:22e02d1cfbca 193
GregCr 1:22e02d1cfbca 194 baud( 115200 );
GregCr 1:22e02d1cfbca 195
GregCr 1:22e02d1cfbca 196 F_CS = 1;
GregCr 1:22e02d1cfbca 197 SD_CS = 1;
GregCr 1:22e02d1cfbca 198 RxLed = 1;
GregCr 1:22e02d1cfbca 199 TxLed = 1;
GregCr 1:22e02d1cfbca 200 ANT_SW = 1;
GregCr 1:22e02d1cfbca 201
GregCr 1:22e02d1cfbca 202 wait_ms( 500 ); // wait for on board DC/DC start-up time
GregCr 1:22e02d1cfbca 203
GregCr 1:22e02d1cfbca 204 Radio.Init( );
GregCr 1:22e02d1cfbca 205 Radio.SetRegulatorMode( USE_DCDC ); // Can also be set in LDO mode but consume more power
GregCr 1:22e02d1cfbca 206
GregCr 1:22e02d1cfbca 207 memset( &Buffer, 0x00, BufferSize );
GregCr 1:22e02d1cfbca 208
GregCr 1:22e02d1cfbca 209 printf( "\n\n\r SX1280 Ping Pong Demo Application (%s)\n\n\r", FIRMWARE_VERSION );
GregCr 1:22e02d1cfbca 210
GregCr 1:22e02d1cfbca 211 #if defined( MODE_BLE )
GregCr 1:22e02d1cfbca 212
GregCr 1:22e02d1cfbca 213 printf( "\nPing Pong running in BLE mode\n\r");
GregCr 1:22e02d1cfbca 214 modulationParams.PacketType = PACKET_TYPE_BLE;
GregCr 1:22e02d1cfbca 215 modulationParams.Params.Ble.BitrateBandwidth = GEN_BLE_BR_0_125_BW_0_3;
GregCr 1:22e02d1cfbca 216 modulationParams.Params.Ble.ModulationIndex = GEN_BLE_MOD_IND_1_00;
GregCr 1:22e02d1cfbca 217 modulationParams.Params.Ble.ModulationShaping = RADIO_MOD_SHAPING_BT_1_0;
GregCr 1:22e02d1cfbca 218
GregCr 1:22e02d1cfbca 219 packetParams.PacketType = PACKET_TYPE_BLE;
GregCr 1:22e02d1cfbca 220 packetParams.Params.Ble.BlePacketType = BLE_EYELONG_1_0;
GregCr 1:22e02d1cfbca 221 packetParams.Params.Ble.ConnectionState = BLE_MASTER_SLAVE;
GregCr 1:22e02d1cfbca 222 packetParams.Params.Ble.CrcField = BLE_CRC_3B;
GregCr 1:22e02d1cfbca 223 packetParams.Params.Ble.Whitening = RADIO_WHITENING_OFF;
GregCr 1:22e02d1cfbca 224
GregCr 1:22e02d1cfbca 225 #elif defined( MODE_GENERIC )
GregCr 1:22e02d1cfbca 226
GregCr 1:22e02d1cfbca 227 printf( "\nPing Pong running in GENERIC mode\n\r");
GregCr 1:22e02d1cfbca 228 modulationParams.PacketType = PACKET_TYPE_GENERIC;
GregCr 1:22e02d1cfbca 229 modulationParams.Params.Generic.BitrateBandwidth = GEN_BLE_BR_0_125_BW_0_3;
GregCr 1:22e02d1cfbca 230 modulationParams.Params.Generic.ModulationIndex = GEN_BLE_MOD_IND_1_00;
GregCr 1:22e02d1cfbca 231 modulationParams.Params.Generic.ModulationShaping = RADIO_MOD_SHAPING_BT_1_0;
GregCr 1:22e02d1cfbca 232
GregCr 1:22e02d1cfbca 233 packetParams.PacketType = PACKET_TYPE_GENERIC;
GregCr 1:22e02d1cfbca 234 packetParams.Params.Generic.PreambleLength = PREAMBLE_LENGTH_32_BITS;
GregCr 1:22e02d1cfbca 235 packetParams.Params.Generic.SyncWordLength = GEN_SYNCWORD_LENGTH_5_BYTE;
GregCr 1:22e02d1cfbca 236 packetParams.Params.Generic.SyncWordMatch = RADIO_RX_MATCH_SYNCWORD_1;
GregCr 1:22e02d1cfbca 237 packetParams.Params.Generic.HeaderType = RADIO_PACKET_VARIABLE_LENGTH;
GregCr 1:22e02d1cfbca 238 packetParams.Params.Generic.PayloadLength = 15;
GregCr 1:22e02d1cfbca 239 packetParams.Params.Generic.CrcLength = RADIO_CRC_3_BYTES;
GregCr 1:22e02d1cfbca 240 packetParams.Params.Generic.Whitening = RADIO_WHITENING_ON;
GregCr 1:22e02d1cfbca 241
GregCr 1:22e02d1cfbca 242 #elif defined( MODE_LORA )
GregCr 1:22e02d1cfbca 243
GregCr 1:22e02d1cfbca 244 printf( "\nPing Pong running in LORA mode\n\r");
GregCr 1:22e02d1cfbca 245 modulationParams.PacketType = PACKET_TYPE_LORA;
GregCr 1:22e02d1cfbca 246 modulationParams.Params.LoRa.SpreadingFactor = LORA_SF7;
GregCr 1:22e02d1cfbca 247 modulationParams.Params.LoRa.Bandwidth = LORA_BW_0400;
GregCr 1:22e02d1cfbca 248 modulationParams.Params.LoRa.CodingRate = LORA_CR_4_5;
GregCr 1:22e02d1cfbca 249
GregCr 1:22e02d1cfbca 250 PacketParams.PacketType = PACKET_TYPE_LORA;
GregCr 1:22e02d1cfbca 251 PacketParams.Params.LoRa.PreambleLength = 0x08;
GregCr 1:22e02d1cfbca 252 PacketParams.Params.LoRa.HeaderType = LORA_PACKET_VARIABLE_LENGTH;
GregCr 1:22e02d1cfbca 253 PacketParams.Params.LoRa.PayloadLength = 15;
GregCr 1:22e02d1cfbca 254 PacketParams.Params.LoRa.CrcMode = LORA_CRC_ON;
GregCr 1:22e02d1cfbca 255 PacketParams.Params.LoRa.InvertIQ = LORA_IQ_INVERTED;
GregCr 1:22e02d1cfbca 256
GregCr 1:22e02d1cfbca 257 #elif defined( MODE_FLRC )
GregCr 1:22e02d1cfbca 258
GregCr 1:22e02d1cfbca 259 printf( "\nPing Pong running in FLRC mode\n\r");
GregCr 1:22e02d1cfbca 260 modulationParams.PacketType = PACKET_TYPE_FLRC;
GregCr 1:22e02d1cfbca 261 modulationParams.Params.Flrc.BitrateBandwidth = FLRC_BR_0_260_BW_0_3;
GregCr 1:22e02d1cfbca 262 modulationParams.Params.Flrc.CodingRate = FLRC_CR_1_2;
GregCr 1:22e02d1cfbca 263 modulationParams.Params.Flrc.ModulationShaping = RADIO_MOD_SHAPING_BT_1_0;
GregCr 1:22e02d1cfbca 264
GregCr 1:22e02d1cfbca 265 packetParams.PacketType = PACKET_TYPE_FLRC;
GregCr 1:22e02d1cfbca 266 packetParams.Params.Flrc.PreambleLength = PREAMBLE_LENGTH_32_BITS;
GregCr 1:22e02d1cfbca 267 packetParams.Params.Flrc.SyncWordLength = FLRC_SYNCWORD_LENGTH_4_BYTE;
GregCr 1:22e02d1cfbca 268 packetParams.Params.Flrc.SyncWordMatch = RADIO_RX_MATCH_SYNCWORD_1;
GregCr 1:22e02d1cfbca 269 packetParams.Params.Flrc.HeaderType = RADIO_PACKET_VARIABLE_LENGTH;
GregCr 1:22e02d1cfbca 270 packetParams.Params.Flrc.PayloadLength = 15;
GregCr 1:22e02d1cfbca 271 packetParams.Params.Flrc.CrcLength = RADIO_CRC_3_BYTES;
GregCr 1:22e02d1cfbca 272 packetParams.Params.Flrc.Whitening = RADIO_WHITENING_OFF;
GregCr 1:22e02d1cfbca 273
GregCr 1:22e02d1cfbca 274 #else
GregCr 1:22e02d1cfbca 275 #error "Please select the mode of operation for the Ping Ping demo"
GregCr 1:22e02d1cfbca 276 #endif
GregCr 1:22e02d1cfbca 277
GregCr 1:22e02d1cfbca 278 Radio.SetStandby( STDBY_RC );
GregCr 1:22e02d1cfbca 279 Radio.SetPacketType( modulationParams.PacketType );
GregCr 1:22e02d1cfbca 280 Radio.SetModulationParams( &modulationParams );
GregCr 1:22e02d1cfbca 281 Radio.SetPacketParams( &PacketParams );
GregCr 1:22e02d1cfbca 282
GregCr 1:22e02d1cfbca 283 Radio.SetRfFrequency( RF_FREQUENCY );
GregCr 1:22e02d1cfbca 284 Radio.SetBufferBaseAddresses( 0x00, 0x00 );
GregCr 1:22e02d1cfbca 285 Radio.SetTxParams( TX_OUTPUT_POWER, RADIO_RAMP_20_US );
GregCr 1:22e02d1cfbca 286
GregCr 1:22e02d1cfbca 287 // only used in GENERIC and BLE mode
GregCr 1:22e02d1cfbca 288 Radio.SetSyncWord( 1, ( uint8_t[] ){ 0xDD, 0xA0, 0x96, 0x69, 0xDD } );
GregCr 1:22e02d1cfbca 289
GregCr 1:22e02d1cfbca 290 RxLed = 0;
GregCr 1:22e02d1cfbca 291 TxLed = 0;
GregCr 1:22e02d1cfbca 292
GregCr 1:22e02d1cfbca 293 Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
GregCr 1:22e02d1cfbca 294 Radio.SetRx( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
GregCr 1:22e02d1cfbca 295 AppState = APP_LOWPOWER;
GregCr 1:22e02d1cfbca 296
GregCr 1:22e02d1cfbca 297 while( 1 )
GregCr 1:22e02d1cfbca 298 {
GregCr 1:22e02d1cfbca 299 switch( AppState )
GregCr 1:22e02d1cfbca 300 {
GregCr 1:22e02d1cfbca 301 case APP_RX:
GregCr 1:22e02d1cfbca 302 AppState = APP_LOWPOWER;
GregCr 1:22e02d1cfbca 303 RxLed = !RxLed;
GregCr 1:22e02d1cfbca 304 Radio.GetPayload( Buffer, &BufferSize, BUFFER_SIZE );
GregCr 1:22e02d1cfbca 305 if( isMaster == true )
GregCr 1:22e02d1cfbca 306 {
GregCr 1:22e02d1cfbca 307 if( BufferSize > 0 )
GregCr 1:22e02d1cfbca 308 {
GregCr 1:22e02d1cfbca 309 if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, PINGPONGSIZE ) == 0 )
GregCr 1:22e02d1cfbca 310 {
GregCr 1:22e02d1cfbca 311 printf( "...Pong\r\n" );
GregCr 1:22e02d1cfbca 312 memcpy( Buffer, PingMsg, PINGPONGSIZE );
GregCr 1:22e02d1cfbca 313 Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
GregCr 1:22e02d1cfbca 314 Radio.SendPayload( Buffer, BufferSize, ( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
GregCr 1:22e02d1cfbca 315 }
GregCr 1:22e02d1cfbca 316 else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, PINGPONGSIZE ) == 0 )
GregCr 1:22e02d1cfbca 317 {
GregCr 1:22e02d1cfbca 318 // A master already exists then become a slave
GregCr 1:22e02d1cfbca 319 printf( "...Ping\r\n" );
GregCr 1:22e02d1cfbca 320 isMaster = false;
GregCr 1:22e02d1cfbca 321 memcpy( Buffer, PongMsg, PINGPONGSIZE );
GregCr 1:22e02d1cfbca 322 Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
GregCr 1:22e02d1cfbca 323 Radio.SendPayload( Buffer, BufferSize, ( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
GregCr 1:22e02d1cfbca 324 }
GregCr 1:22e02d1cfbca 325 else // valid reception but neither a PING or a PONG message
GregCr 1:22e02d1cfbca 326 { // Set device as master ans start again
GregCr 1:22e02d1cfbca 327 isMaster = true;
GregCr 1:22e02d1cfbca 328 Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
GregCr 1:22e02d1cfbca 329 Radio.SetRx( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
GregCr 1:22e02d1cfbca 330 }
GregCr 1:22e02d1cfbca 331 }
GregCr 1:22e02d1cfbca 332 }
GregCr 1:22e02d1cfbca 333 else
GregCr 1:22e02d1cfbca 334 {
GregCr 1:22e02d1cfbca 335 if( BufferSize > 0 )
GregCr 1:22e02d1cfbca 336 {
GregCr 1:22e02d1cfbca 337 if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, PINGPONGSIZE ) == 0 )
GregCr 1:22e02d1cfbca 338 {
GregCr 1:22e02d1cfbca 339 printf( "...Ping\r\n" );
GregCr 1:22e02d1cfbca 340 memcpy( Buffer, PongMsg, 4 );
GregCr 1:22e02d1cfbca 341 Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
GregCr 1:22e02d1cfbca 342 Radio.SendPayload( Buffer, BufferSize, ( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
GregCr 1:22e02d1cfbca 343 }
GregCr 1:22e02d1cfbca 344 else // valid reception but not a PING as expected
GregCr 1:22e02d1cfbca 345 {
GregCr 1:22e02d1cfbca 346 isMaster = true;
GregCr 1:22e02d1cfbca 347 Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
GregCr 1:22e02d1cfbca 348 Radio.SetRx( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
GregCr 1:22e02d1cfbca 349 }
GregCr 1:22e02d1cfbca 350 }
GregCr 1:22e02d1cfbca 351 }
GregCr 1:22e02d1cfbca 352 break;
GregCr 1:22e02d1cfbca 353
GregCr 1:22e02d1cfbca 354 case APP_TX:
GregCr 1:22e02d1cfbca 355 AppState = APP_LOWPOWER;
GregCr 1:22e02d1cfbca 356 TxLed = !TxLed;
GregCr 1:22e02d1cfbca 357 if( isMaster == true )
GregCr 1:22e02d1cfbca 358 {
GregCr 1:22e02d1cfbca 359 printf( "Ping...\r\n" );
GregCr 1:22e02d1cfbca 360 }
GregCr 1:22e02d1cfbca 361 else
GregCr 1:22e02d1cfbca 362 {
GregCr 1:22e02d1cfbca 363 printf( "Pong...\r\n" );
GregCr 1:22e02d1cfbca 364 }
GregCr 1:22e02d1cfbca 365 Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
GregCr 1:22e02d1cfbca 366 Radio.SetRx( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
GregCr 1:22e02d1cfbca 367 break;
GregCr 1:22e02d1cfbca 368
GregCr 1:22e02d1cfbca 369 case APP_RX_TIMEOUT:
GregCr 1:22e02d1cfbca 370 AppState = APP_LOWPOWER;
GregCr 1:22e02d1cfbca 371 if( isMaster == true )
GregCr 1:22e02d1cfbca 372 {
GregCr 1:22e02d1cfbca 373 // Send the next PING frame
GregCr 1:22e02d1cfbca 374 memcpy( Buffer, PingMsg, PINGPONGSIZE );
GregCr 1:22e02d1cfbca 375 Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
GregCr 1:22e02d1cfbca 376 Radio.SendPayload( Buffer, BufferSize, ( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
GregCr 1:22e02d1cfbca 377 }
GregCr 1:22e02d1cfbca 378 else
GregCr 1:22e02d1cfbca 379 {
GregCr 1:22e02d1cfbca 380 Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
GregCr 1:22e02d1cfbca 381 Radio.SetRx( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
GregCr 1:22e02d1cfbca 382 }
GregCr 1:22e02d1cfbca 383 break;
GregCr 1:22e02d1cfbca 384
GregCr 1:22e02d1cfbca 385 case APP_RX_ERROR:
GregCr 1:22e02d1cfbca 386 AppState = APP_LOWPOWER;
GregCr 1:22e02d1cfbca 387 // We have received a Packet with a CRC error, send reply as if packet was correct
GregCr 1:22e02d1cfbca 388 if( isMaster == true )
GregCr 1:22e02d1cfbca 389 {
GregCr 1:22e02d1cfbca 390 // Send the next PING frame
GregCr 1:22e02d1cfbca 391 memcpy( Buffer, PingMsg, PINGPONGSIZE );
GregCr 1:22e02d1cfbca 392 Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
GregCr 1:22e02d1cfbca 393 Radio.SendPayload( Buffer, BufferSize, ( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
GregCr 1:22e02d1cfbca 394 }
GregCr 1:22e02d1cfbca 395 else
GregCr 1:22e02d1cfbca 396 {
GregCr 1:22e02d1cfbca 397 // Send the next PONG frame
GregCr 1:22e02d1cfbca 398 memcpy( Buffer, PongMsg, PINGPONGSIZE );
GregCr 1:22e02d1cfbca 399 Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
GregCr 1:22e02d1cfbca 400 Radio.SendPayload( Buffer, BufferSize, ( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
GregCr 1:22e02d1cfbca 401 }
GregCr 1:22e02d1cfbca 402 break;
GregCr 1:22e02d1cfbca 403
GregCr 1:22e02d1cfbca 404 case APP_TX_TIMEOUT:
GregCr 1:22e02d1cfbca 405 AppState = APP_LOWPOWER;
GregCr 1:22e02d1cfbca 406 Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
GregCr 1:22e02d1cfbca 407 Radio.SetRx( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
GregCr 1:22e02d1cfbca 408 break;
GregCr 1:22e02d1cfbca 409
GregCr 1:22e02d1cfbca 410 case APP_LOWPOWER:
GregCr 1:22e02d1cfbca 411 break;
GregCr 1:22e02d1cfbca 412
GregCr 1:22e02d1cfbca 413 default:
GregCr 1:22e02d1cfbca 414 // Set low power
GregCr 1:22e02d1cfbca 415 break;
GregCr 1:22e02d1cfbca 416 }
GregCr 1:22e02d1cfbca 417 }
GregCr 1:22e02d1cfbca 418 }
GregCr 1:22e02d1cfbca 419
GregCr 1:22e02d1cfbca 420 void OnTxDone( void )
GregCr 1:22e02d1cfbca 421 {
GregCr 1:22e02d1cfbca 422 AppState = APP_TX;
GregCr 1:22e02d1cfbca 423 }
GregCr 1:22e02d1cfbca 424
GregCr 1:22e02d1cfbca 425 void OnRxDone( void )
GregCr 1:22e02d1cfbca 426 {
GregCr 1:22e02d1cfbca 427 AppState = APP_RX;/*
GregCr 1:22e02d1cfbca 428 Radio.GetPacketStatus(&packetStatus);
GregCr 1:22e02d1cfbca 429 #if ( defined( MODE_BLE ) || defined( MODE_GENERIC ) )
GregCr 1:22e02d1cfbca 430 RssiValue = packetStatus.Ble.RssiSync;
GregCr 1:22e02d1cfbca 431 printf("rssi: %d\n\r", RssiValue);
GregCr 1:22e02d1cfbca 432 #elif defined( MODE_LORA )
GregCr 1:22e02d1cfbca 433 RssiValue = packetStatus.Lr24.RssiPkt;
GregCr 1:22e02d1cfbca 434 SnrValue = packetStatus.Lr24.SnrPkt;
GregCr 1:22e02d1cfbca 435 printf("rssi: %d; snr: %d\n\r", RssiValue, SnrValue );
GregCr 1:22e02d1cfbca 436 #endif*/
GregCr 1:22e02d1cfbca 437 }
GregCr 1:22e02d1cfbca 438
GregCr 1:22e02d1cfbca 439 void OnTxTimeout( void )
GregCr 1:22e02d1cfbca 440 {
GregCr 1:22e02d1cfbca 441 AppState = APP_TX_TIMEOUT;
GregCr 1:22e02d1cfbca 442 printf( "<>>>>>>>>TXE\r\n" );
GregCr 1:22e02d1cfbca 443 }
GregCr 1:22e02d1cfbca 444
GregCr 1:22e02d1cfbca 445 void OnRxTimeout( void )
GregCr 1:22e02d1cfbca 446 {
GregCr 1:22e02d1cfbca 447 AppState = APP_RX_TIMEOUT;
GregCr 1:22e02d1cfbca 448 }
GregCr 1:22e02d1cfbca 449
GregCr 1:22e02d1cfbca 450 void OnRxError( IrqErrorCode_t errorCode )
GregCr 1:22e02d1cfbca 451 {
GregCr 1:22e02d1cfbca 452 AppState = APP_RX_ERROR;
GregCr 1:22e02d1cfbca 453 printf( "RXE<>>>>>>>>\r\n" );
GregCr 1:22e02d1cfbca 454 }
GregCr 1:22e02d1cfbca 455
GregCr 1:22e02d1cfbca 456 void OnRangingDone( IrqRangingCode_t val )
GregCr 1:22e02d1cfbca 457 {
GregCr 1:22e02d1cfbca 458 }
GregCr 1:22e02d1cfbca 459
GregCr 1:22e02d1cfbca 460 void OnCadDone( bool channelActivityDetected )
GregCr 1:22e02d1cfbca 461 {
GregCr 1:22e02d1cfbca 462 }