SX126xDevKit
Dependencies: mbed DmTftLibrary SX126xLib
Demo/DemoApplication.cpp@2:8e1b4210df6b, 2018-07-18 (annotated)
- Committer:
- GregCr
- Date:
- Wed Jul 18 13:34:10 2018 +0000
- Revision:
- 2:8e1b4210df6b
- Parent:
- 1:b96176a4ccb8
added support for sx1268
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GregCr | 0:e5420f1a8a1a | 1 | /* |
GregCr | 0:e5420f1a8a1a | 2 | ______ _ |
GregCr | 0:e5420f1a8a1a | 3 | / _____) _ | | |
GregCr | 0:e5420f1a8a1a | 4 | ( (____ _____ ____ _| |_ _____ ____| |__ |
GregCr | 0:e5420f1a8a1a | 5 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
GregCr | 0:e5420f1a8a1a | 6 | _____) ) ____| | | || |_| ____( (___| | | | |
GregCr | 0:e5420f1a8a1a | 7 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
GregCr | 0:e5420f1a8a1a | 8 | (C)2016 Semtech |
GregCr | 0:e5420f1a8a1a | 9 | |
GregCr | 0:e5420f1a8a1a | 10 | Description: PingPong, PER demo implementation. |
GregCr | 0:e5420f1a8a1a | 11 | |
GregCr | 0:e5420f1a8a1a | 12 | Maintainer: Gregory Cristian & Gilbert Menth |
GregCr | 0:e5420f1a8a1a | 13 | */ |
GregCr | 0:e5420f1a8a1a | 14 | |
GregCr | 0:e5420f1a8a1a | 15 | #include "mbed.h" |
GregCr | 0:e5420f1a8a1a | 16 | #include "radio.h" |
GregCr | 0:e5420f1a8a1a | 17 | #include "sx126x-hal.h" |
GregCr | 0:e5420f1a8a1a | 18 | #include "Eeprom.h" |
GregCr | 0:e5420f1a8a1a | 19 | #include "DemoApplication.h" |
GregCr | 0:e5420f1a8a1a | 20 | |
GregCr | 0:e5420f1a8a1a | 21 | /*! |
GregCr | 0:e5420f1a8a1a | 22 | * \brief Defines the local payload buffer size |
GregCr | 0:e5420f1a8a1a | 23 | */ |
GregCr | 0:e5420f1a8a1a | 24 | #define BUFFER_SIZE 255 |
GregCr | 0:e5420f1a8a1a | 25 | |
GregCr | 0:e5420f1a8a1a | 26 | /*! |
GregCr | 0:e5420f1a8a1a | 27 | * \brief Defines the size of the token defining message type in the payload |
GregCr | 0:e5420f1a8a1a | 28 | * cf. above. |
GregCr | 0:e5420f1a8a1a | 29 | */ |
GregCr | 0:e5420f1a8a1a | 30 | #define PINGPONG_SIZE 4 |
GregCr | 0:e5420f1a8a1a | 31 | #define PER_SIZE 3 |
GregCr | 0:e5420f1a8a1a | 32 | |
GregCr | 0:e5420f1a8a1a | 33 | /*! |
GregCr | 0:e5420f1a8a1a | 34 | * \brief Define time used in PingPong demo to synch with cycle |
GregCr | 0:e5420f1a8a1a | 35 | * RX_TIMEOUT_MARGIN is the free time between each cycle (time reserve) |
GregCr | 0:e5420f1a8a1a | 36 | */ |
GregCr | 0:e5420f1a8a1a | 37 | #define RX_TIMEOUT_MARGIN 150 // ms |
GregCr | 0:e5420f1a8a1a | 38 | #define RX_TX_TRANSITION_WAIT 5 // ms |
GregCr | 0:e5420f1a8a1a | 39 | |
GregCr | 0:e5420f1a8a1a | 40 | |
GregCr | 0:e5420f1a8a1a | 41 | /*! |
GregCr | 0:e5420f1a8a1a | 42 | * \brief Define the possible message type for the Ping-Pong and PER apps |
GregCr | 0:e5420f1a8a1a | 43 | */ |
GregCr | 0:e5420f1a8a1a | 44 | const uint8_t PingMsg[] = "PING"; |
GregCr | 0:e5420f1a8a1a | 45 | const uint8_t PongMsg[] = "PONG"; |
GregCr | 0:e5420f1a8a1a | 46 | const uint8_t PerMsg[] = "PER"; |
GregCr | 0:e5420f1a8a1a | 47 | |
GregCr | 0:e5420f1a8a1a | 48 | /*! |
GregCr | 0:e5420f1a8a1a | 49 | * \brief Buffer and its size |
GregCr | 0:e5420f1a8a1a | 50 | */ |
GregCr | 0:e5420f1a8a1a | 51 | uint8_t BufferSize = BUFFER_SIZE; |
GregCr | 0:e5420f1a8a1a | 52 | uint8_t Buffer[BUFFER_SIZE]; |
GregCr | 0:e5420f1a8a1a | 53 | |
GregCr | 0:e5420f1a8a1a | 54 | /*! |
GregCr | 0:e5420f1a8a1a | 55 | * \brief Function to be executed on Radio Tx Done event |
GregCr | 0:e5420f1a8a1a | 56 | */ |
GregCr | 0:e5420f1a8a1a | 57 | void OnTxDone( void ); |
GregCr | 0:e5420f1a8a1a | 58 | |
GregCr | 0:e5420f1a8a1a | 59 | /*! |
GregCr | 0:e5420f1a8a1a | 60 | * \brief Function to be executed on Radio Rx Done event |
GregCr | 0:e5420f1a8a1a | 61 | */ |
GregCr | 0:e5420f1a8a1a | 62 | void OnRxDone( void ); |
GregCr | 0:e5420f1a8a1a | 63 | |
GregCr | 0:e5420f1a8a1a | 64 | /*! |
GregCr | 0:e5420f1a8a1a | 65 | * \brief Function executed on Radio Tx Timeout event |
GregCr | 0:e5420f1a8a1a | 66 | */ |
GregCr | 0:e5420f1a8a1a | 67 | void OnTxTimeout( void ); |
GregCr | 0:e5420f1a8a1a | 68 | |
GregCr | 0:e5420f1a8a1a | 69 | /*! |
GregCr | 0:e5420f1a8a1a | 70 | * \brief Function executed on Radio Rx Timeout event |
GregCr | 0:e5420f1a8a1a | 71 | */ |
GregCr | 0:e5420f1a8a1a | 72 | void OnRxTimeout( void ); |
GregCr | 0:e5420f1a8a1a | 73 | |
GregCr | 0:e5420f1a8a1a | 74 | /*! |
GregCr | 0:e5420f1a8a1a | 75 | * \brief Function executed on Radio Rx Error event |
GregCr | 0:e5420f1a8a1a | 76 | */ |
GregCr | 0:e5420f1a8a1a | 77 | void OnRxError( IrqErrorCode_t ); |
GregCr | 0:e5420f1a8a1a | 78 | |
GregCr | 0:e5420f1a8a1a | 79 | /*! |
GregCr | 0:e5420f1a8a1a | 80 | * \brief Function executed on Radio CAD Done event |
GregCr | 0:e5420f1a8a1a | 81 | */ |
GregCr | 0:e5420f1a8a1a | 82 | void OnCadDone( bool channelActivityDetected ); |
GregCr | 0:e5420f1a8a1a | 83 | |
GregCr | 0:e5420f1a8a1a | 84 | /*! |
GregCr | 0:e5420f1a8a1a | 85 | * \brief All the callbacks are stored in a structure |
GregCr | 0:e5420f1a8a1a | 86 | */ |
GregCr | 0:e5420f1a8a1a | 87 | RadioCallbacks_t RadioEvents = |
GregCr | 0:e5420f1a8a1a | 88 | { |
GregCr | 0:e5420f1a8a1a | 89 | &OnTxDone, // txDone |
GregCr | 0:e5420f1a8a1a | 90 | &OnRxDone, // rxDone |
GregCr | 0:e5420f1a8a1a | 91 | NULL, // rxPreambleDetect |
GregCr | 0:e5420f1a8a1a | 92 | NULL, // rxSyncWordDone |
GregCr | 0:e5420f1a8a1a | 93 | NULL, // rxHeaderDone |
GregCr | 0:e5420f1a8a1a | 94 | &OnTxTimeout, // txTimeout |
GregCr | 0:e5420f1a8a1a | 95 | &OnRxTimeout, // rxTimeout |
GregCr | 0:e5420f1a8a1a | 96 | &OnRxError, // rxError |
GregCr | 0:e5420f1a8a1a | 97 | &OnCadDone, // cadDone |
GregCr | 0:e5420f1a8a1a | 98 | }; |
GregCr | 0:e5420f1a8a1a | 99 | |
GregCr | 0:e5420f1a8a1a | 100 | // SPI |
GregCr | 1:b96176a4ccb8 | 101 | // mosi, miso, sclk, nss, dio0, dio1, dio2, dio3, rst, freqSel, deviceSel, antSwPower, callbacks... |
GregCr | 1:b96176a4ccb8 | 102 | SX126xHal Radio( D11, D12, D13, D7, D3, D5, NC, NC, A0, A1, A2, D8, &RadioEvents ); |
GregCr | 0:e5420f1a8a1a | 103 | |
GregCr | 0:e5420f1a8a1a | 104 | /*! |
GregCr | 0:e5420f1a8a1a | 105 | * \brief Tx LED toggling on transmition success |
GregCr | 0:e5420f1a8a1a | 106 | */ |
GregCr | 0:e5420f1a8a1a | 107 | DigitalOut TX_LED( A4 ); |
GregCr | 0:e5420f1a8a1a | 108 | |
GregCr | 0:e5420f1a8a1a | 109 | /*! |
GregCr | 0:e5420f1a8a1a | 110 | * \brief Rx LED toggling on reception success |
GregCr | 0:e5420f1a8a1a | 111 | */ |
GregCr | 0:e5420f1a8a1a | 112 | DigitalOut RX_LED( A5 ); |
GregCr | 0:e5420f1a8a1a | 113 | |
GregCr | 0:e5420f1a8a1a | 114 | /*! |
GregCr | 0:e5420f1a8a1a | 115 | * \brief Mask of IRQs |
GregCr | 0:e5420f1a8a1a | 116 | */ |
GregCr | 0:e5420f1a8a1a | 117 | uint16_t IrqMask = 0x0000; |
GregCr | 0:e5420f1a8a1a | 118 | |
GregCr | 0:e5420f1a8a1a | 119 | /*! |
GregCr | 0:e5420f1a8a1a | 120 | * \brief Locals parameters and status for radio API |
GregCr | 0:e5420f1a8a1a | 121 | * NEED TO BE OPTIMIZED, COPY OF STUCTURE ALREADY EXISTING |
GregCr | 0:e5420f1a8a1a | 122 | */ |
GregCr | 0:e5420f1a8a1a | 123 | PacketParams_t PacketParams; |
GregCr | 0:e5420f1a8a1a | 124 | PacketStatus_t PacketStatus; |
GregCr | 0:e5420f1a8a1a | 125 | ModulationParams_t ModulationParams; |
GregCr | 0:e5420f1a8a1a | 126 | |
GregCr | 0:e5420f1a8a1a | 127 | /*! |
GregCr | 0:e5420f1a8a1a | 128 | * \brief Flag to indicate if the demo is already running |
GregCr | 0:e5420f1a8a1a | 129 | */ |
GregCr | 0:e5420f1a8a1a | 130 | static bool DemoRunning = false; |
GregCr | 0:e5420f1a8a1a | 131 | |
GregCr | 0:e5420f1a8a1a | 132 | /*! |
GregCr | 0:e5420f1a8a1a | 133 | * \brief Frequency Error (only LSB) |
GregCr | 0:e5420f1a8a1a | 134 | */ |
GregCr | 0:e5420f1a8a1a | 135 | static double FreErrorLsb = 0.0; |
GregCr | 0:e5420f1a8a1a | 136 | |
GregCr | 0:e5420f1a8a1a | 137 | /*! |
GregCr | 0:e5420f1a8a1a | 138 | * \brief Flag holding the current internal state of the demo application |
GregCr | 0:e5420f1a8a1a | 139 | */ |
GregCr | 0:e5420f1a8a1a | 140 | static uint8_t DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 141 | |
GregCr | 0:e5420f1a8a1a | 142 | /*! |
GregCr | 0:e5420f1a8a1a | 143 | * \brief Ticker for master to synch Tx frames. Flags for PER and PingPong demo |
GregCr | 0:e5420f1a8a1a | 144 | * for Synch TX in cycle. |
GregCr | 0:e5420f1a8a1a | 145 | */ |
GregCr | 0:e5420f1a8a1a | 146 | Ticker SendNextPacket; |
GregCr | 0:e5420f1a8a1a | 147 | static bool SendNext = false; |
GregCr | 0:e5420f1a8a1a | 148 | |
GregCr | 0:e5420f1a8a1a | 149 | /*! |
GregCr | 0:e5420f1a8a1a | 150 | * \brief Hold last Rx packet number to compute PER in PER and PingPong demo |
GregCr | 0:e5420f1a8a1a | 151 | */ |
GregCr | 0:e5420f1a8a1a | 152 | static uint32_t PacketRxSequence = 0; |
GregCr | 0:e5420f1a8a1a | 153 | static uint32_t PacketRxSequencePrev = 0; |
GregCr | 0:e5420f1a8a1a | 154 | |
GregCr | 0:e5420f1a8a1a | 155 | void LedBlink( void ); |
GregCr | 0:e5420f1a8a1a | 156 | void InitializeDemoParameters( uint8_t modulation ); |
GregCr | 0:e5420f1a8a1a | 157 | uint16_t GetTimeOnAir( uint8_t modulation ); |
GregCr | 0:e5420f1a8a1a | 158 | void SendNextPacketEvent( void ); |
GregCr | 0:e5420f1a8a1a | 159 | |
GregCr | 0:e5420f1a8a1a | 160 | // ************************** RF Test Demo ****************************** |
GregCr | 0:e5420f1a8a1a | 161 | // * * |
GregCr | 0:e5420f1a8a1a | 162 | // * * |
GregCr | 0:e5420f1a8a1a | 163 | // * * |
GregCr | 0:e5420f1a8a1a | 164 | // ***************************************************************************** |
GregCr | 1:b96176a4ccb8 | 165 | uint8_t RunDemoTestRssi( void ) |
GregCr | 1:b96176a4ccb8 | 166 | { |
GregCr | 1:b96176a4ccb8 | 167 | uint8_t refreshDisplay = 0; |
GregCr | 1:b96176a4ccb8 | 168 | static uint16_t i; |
GregCr | 1:b96176a4ccb8 | 169 | |
GregCr | 1:b96176a4ccb8 | 170 | if( Eeprom.EepromData.DemoSettings.HoldDemo == true ) |
GregCr | 1:b96176a4ccb8 | 171 | { |
GregCr | 1:b96176a4ccb8 | 172 | return 0; |
GregCr | 1:b96176a4ccb8 | 173 | } |
GregCr | 1:b96176a4ccb8 | 174 | |
GregCr | 1:b96176a4ccb8 | 175 | if( DemoRunning == false ) |
GregCr | 1:b96176a4ccb8 | 176 | { |
GregCr | 1:b96176a4ccb8 | 177 | DemoRunning = true; |
GregCr | 1:b96176a4ccb8 | 178 | |
GregCr | 1:b96176a4ccb8 | 179 | printf( "Start RunDemoTestRssi\n\r" ); |
GregCr | 1:b96176a4ccb8 | 180 | |
GregCr | 1:b96176a4ccb8 | 181 | TX_LED = 0; |
GregCr | 1:b96176a4ccb8 | 182 | RX_LED = 0; |
GregCr | 1:b96176a4ccb8 | 183 | |
GregCr | 1:b96176a4ccb8 | 184 | Eeprom.EepromData.DemoSettings.CntPacketTx = 0; |
GregCr | 1:b96176a4ccb8 | 185 | Eeprom.EepromData.DemoSettings.CntPacketRxOK = 0; |
GregCr | 1:b96176a4ccb8 | 186 | Eeprom.EepromData.DemoSettings.CntPacketRxKO = 0; |
GregCr | 1:b96176a4ccb8 | 187 | |
GregCr | 1:b96176a4ccb8 | 188 | InitializeDemoParameters( Eeprom.EepromData.DemoSettings.ModulationType ); |
GregCr | 1:b96176a4ccb8 | 189 | |
GregCr | 1:b96176a4ccb8 | 190 | IrqMask = IRQ_RX_DONE | IRQ_CRC_ERROR | IRQ_RX_TX_TIMEOUT; |
GregCr | 1:b96176a4ccb8 | 191 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 1:b96176a4ccb8 | 192 | // Rx Continuous |
GregCr | 1:b96176a4ccb8 | 193 | if( Eeprom.EepromData.DemoSettings.BoostedRx == false ) |
GregCr | 1:b96176a4ccb8 | 194 | { |
GregCr | 1:b96176a4ccb8 | 195 | Radio.SetRx( 0xFFFFFF ); |
GregCr | 1:b96176a4ccb8 | 196 | } |
GregCr | 1:b96176a4ccb8 | 197 | else |
GregCr | 1:b96176a4ccb8 | 198 | { |
GregCr | 1:b96176a4ccb8 | 199 | Radio.SetRxBoosted( 0xFFFFFF ); |
GregCr | 1:b96176a4ccb8 | 200 | } |
GregCr | 1:b96176a4ccb8 | 201 | DemoInternalState = APP_IDLE; |
GregCr | 1:b96176a4ccb8 | 202 | i = 0; |
GregCr | 1:b96176a4ccb8 | 203 | } |
GregCr | 1:b96176a4ccb8 | 204 | |
GregCr | 1:b96176a4ccb8 | 205 | Radio.ProcessIrqs( ); |
GregCr | 1:b96176a4ccb8 | 206 | |
GregCr | 1:b96176a4ccb8 | 207 | if( i < 5000 ) |
GregCr | 1:b96176a4ccb8 | 208 | { |
GregCr | 1:b96176a4ccb8 | 209 | i++; |
GregCr | 1:b96176a4ccb8 | 210 | } |
GregCr | 1:b96176a4ccb8 | 211 | else |
GregCr | 1:b96176a4ccb8 | 212 | { |
GregCr | 1:b96176a4ccb8 | 213 | i = 0; |
GregCr | 1:b96176a4ccb8 | 214 | Eeprom.EepromData.DemoSettings.RssiValue = Radio.GetRssiInst( ); |
GregCr | 1:b96176a4ccb8 | 215 | refreshDisplay = 1; |
GregCr | 1:b96176a4ccb8 | 216 | } |
GregCr | 1:b96176a4ccb8 | 217 | |
GregCr | 1:b96176a4ccb8 | 218 | return refreshDisplay; |
GregCr | 1:b96176a4ccb8 | 219 | } |
GregCr | 0:e5420f1a8a1a | 220 | |
GregCr | 0:e5420f1a8a1a | 221 | uint8_t RunDemoSleepMode( void ) |
GregCr | 0:e5420f1a8a1a | 222 | { |
GregCr | 0:e5420f1a8a1a | 223 | SleepParams_t SleepParam; |
GregCr | 0:e5420f1a8a1a | 224 | |
GregCr | 0:e5420f1a8a1a | 225 | if( Eeprom.EepromData.DemoSettings.HoldDemo == true ) |
GregCr | 0:e5420f1a8a1a | 226 | { |
GregCr | 0:e5420f1a8a1a | 227 | return 0; |
GregCr | 0:e5420f1a8a1a | 228 | } |
GregCr | 0:e5420f1a8a1a | 229 | |
GregCr | 0:e5420f1a8a1a | 230 | if( DemoRunning == false ) |
GregCr | 0:e5420f1a8a1a | 231 | { |
GregCr | 0:e5420f1a8a1a | 232 | DemoRunning = true; |
GregCr | 0:e5420f1a8a1a | 233 | InitializeDemoParameters( PACKET_TYPE_LORA ); |
GregCr | 0:e5420f1a8a1a | 234 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 235 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 236 | |
GregCr | 0:e5420f1a8a1a | 237 | SleepParam.Fields.WakeUpRTC = 0; //!< Get out of sleep mode if wakeup signal received from RTC |
GregCr | 0:e5420f1a8a1a | 238 | SleepParam.Fields.Reset = 0; //!< |
GregCr | 0:e5420f1a8a1a | 239 | SleepParam.Fields.WarmStart = 1; //!< |
GregCr | 0:e5420f1a8a1a | 240 | Radio.SetSleep( SleepParam ); |
GregCr | 0:e5420f1a8a1a | 241 | } |
GregCr | 0:e5420f1a8a1a | 242 | else |
GregCr | 0:e5420f1a8a1a | 243 | { |
GregCr | 0:e5420f1a8a1a | 244 | LedBlink( ); |
GregCr | 0:e5420f1a8a1a | 245 | } |
GregCr | 0:e5420f1a8a1a | 246 | return 0; |
GregCr | 0:e5420f1a8a1a | 247 | } |
GregCr | 0:e5420f1a8a1a | 248 | |
GregCr | 0:e5420f1a8a1a | 249 | uint8_t RunDemoStandbyRcMode( void ) |
GregCr | 0:e5420f1a8a1a | 250 | { |
GregCr | 0:e5420f1a8a1a | 251 | if( Eeprom.EepromData.DemoSettings.HoldDemo == true ) |
GregCr | 0:e5420f1a8a1a | 252 | { |
GregCr | 0:e5420f1a8a1a | 253 | return 0; |
GregCr | 0:e5420f1a8a1a | 254 | } |
GregCr | 0:e5420f1a8a1a | 255 | |
GregCr | 0:e5420f1a8a1a | 256 | if( DemoRunning == false ) |
GregCr | 0:e5420f1a8a1a | 257 | { |
GregCr | 0:e5420f1a8a1a | 258 | DemoRunning = true; |
GregCr | 0:e5420f1a8a1a | 259 | InitializeDemoParameters( PACKET_TYPE_LORA ); |
GregCr | 0:e5420f1a8a1a | 260 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 261 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 262 | Radio.SetRegulatorMode( ( RadioRegulatorMode_t )Eeprom.EepromData.DemoSettings.RadioPowerMode ); |
GregCr | 0:e5420f1a8a1a | 263 | Radio.SetStandby( STDBY_RC ); |
GregCr | 0:e5420f1a8a1a | 264 | DemoRunning = true; |
GregCr | 0:e5420f1a8a1a | 265 | } |
GregCr | 0:e5420f1a8a1a | 266 | else |
GregCr | 0:e5420f1a8a1a | 267 | { |
GregCr | 0:e5420f1a8a1a | 268 | LedBlink( ); |
GregCr | 0:e5420f1a8a1a | 269 | } |
GregCr | 0:e5420f1a8a1a | 270 | return 0; |
GregCr | 0:e5420f1a8a1a | 271 | } |
GregCr | 0:e5420f1a8a1a | 272 | |
GregCr | 0:e5420f1a8a1a | 273 | uint8_t RunDemoStandbyXoscMode( void ) |
GregCr | 0:e5420f1a8a1a | 274 | { |
GregCr | 0:e5420f1a8a1a | 275 | if( Eeprom.EepromData.DemoSettings.HoldDemo == true ) |
GregCr | 0:e5420f1a8a1a | 276 | { |
GregCr | 0:e5420f1a8a1a | 277 | return 0; |
GregCr | 0:e5420f1a8a1a | 278 | } |
GregCr | 0:e5420f1a8a1a | 279 | if( DemoRunning == false ) |
GregCr | 0:e5420f1a8a1a | 280 | { |
GregCr | 0:e5420f1a8a1a | 281 | DemoRunning = true; |
GregCr | 0:e5420f1a8a1a | 282 | InitializeDemoParameters( PACKET_TYPE_LORA ); |
GregCr | 0:e5420f1a8a1a | 283 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 284 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 285 | Radio.SetRegulatorMode( ( RadioRegulatorMode_t )Eeprom.EepromData.DemoSettings.RadioPowerMode ); |
GregCr | 0:e5420f1a8a1a | 286 | Radio.SetStandby( STDBY_XOSC ); |
GregCr | 0:e5420f1a8a1a | 287 | DemoRunning = true; |
GregCr | 0:e5420f1a8a1a | 288 | } |
GregCr | 0:e5420f1a8a1a | 289 | else |
GregCr | 0:e5420f1a8a1a | 290 | { |
GregCr | 0:e5420f1a8a1a | 291 | LedBlink( ); |
GregCr | 0:e5420f1a8a1a | 292 | } |
GregCr | 0:e5420f1a8a1a | 293 | return 0; |
GregCr | 0:e5420f1a8a1a | 294 | } |
GregCr | 0:e5420f1a8a1a | 295 | |
GregCr | 0:e5420f1a8a1a | 296 | uint8_t RunDemoTxCw( void ) |
GregCr | 0:e5420f1a8a1a | 297 | { |
GregCr | 0:e5420f1a8a1a | 298 | if( Eeprom.EepromData.DemoSettings.HoldDemo == true ) |
GregCr | 0:e5420f1a8a1a | 299 | { |
GregCr | 0:e5420f1a8a1a | 300 | return 0; |
GregCr | 0:e5420f1a8a1a | 301 | } |
GregCr | 0:e5420f1a8a1a | 302 | |
GregCr | 0:e5420f1a8a1a | 303 | if( DemoRunning == false ) |
GregCr | 0:e5420f1a8a1a | 304 | { |
GregCr | 0:e5420f1a8a1a | 305 | DemoRunning = true; |
GregCr | 0:e5420f1a8a1a | 306 | InitializeDemoParameters( PACKET_TYPE_LORA ); |
GregCr | 0:e5420f1a8a1a | 307 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 308 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 309 | Radio.SetStandby( STDBY_RC ); |
GregCr | 0:e5420f1a8a1a | 310 | Radio.SetRegulatorMode( ( RadioRegulatorMode_t )Eeprom.EepromData.DemoSettings.RadioPowerMode ); |
GregCr | 0:e5420f1a8a1a | 311 | Radio.SetRfFrequency( Eeprom.EepromData.DemoSettings.Frequency ); |
GregCr | 1:b96176a4ccb8 | 312 | Radio.SetTxParams( Eeprom.EepromData.DemoSettings.TxPower, RADIO_RAMP_200_US ); |
GregCr | 0:e5420f1a8a1a | 313 | Radio.SetTxContinuousWave( ); |
GregCr | 0:e5420f1a8a1a | 314 | DemoRunning = true; |
GregCr | 0:e5420f1a8a1a | 315 | } |
GregCr | 0:e5420f1a8a1a | 316 | else |
GregCr | 0:e5420f1a8a1a | 317 | { |
GregCr | 0:e5420f1a8a1a | 318 | LedBlink( ); |
GregCr | 0:e5420f1a8a1a | 319 | } |
GregCr | 0:e5420f1a8a1a | 320 | return 0; |
GregCr | 0:e5420f1a8a1a | 321 | } |
GregCr | 0:e5420f1a8a1a | 322 | |
GregCr | 0:e5420f1a8a1a | 323 | uint8_t RunDemoTxContinuousModulation( void ) |
GregCr | 0:e5420f1a8a1a | 324 | { |
GregCr | 0:e5420f1a8a1a | 325 | uint8_t localPayloadSize = 250; |
GregCr | 0:e5420f1a8a1a | 326 | uint8_t i = 0; |
GregCr | 0:e5420f1a8a1a | 327 | |
GregCr | 0:e5420f1a8a1a | 328 | if( Eeprom.EepromData.DemoSettings.HoldDemo == true ) |
GregCr | 0:e5420f1a8a1a | 329 | { |
GregCr | 0:e5420f1a8a1a | 330 | return 0; |
GregCr | 0:e5420f1a8a1a | 331 | } |
GregCr | 0:e5420f1a8a1a | 332 | |
GregCr | 0:e5420f1a8a1a | 333 | Radio.ProcessIrqs( ); |
GregCr | 0:e5420f1a8a1a | 334 | |
GregCr | 0:e5420f1a8a1a | 335 | if( DemoRunning == false ) |
GregCr | 0:e5420f1a8a1a | 336 | { |
GregCr | 0:e5420f1a8a1a | 337 | DemoRunning = true; |
GregCr | 0:e5420f1a8a1a | 338 | InitializeDemoParameters( Eeprom.EepromData.DemoSettings.ModulationType ); |
GregCr | 0:e5420f1a8a1a | 339 | Radio.SetTxParams( Eeprom.EepromData.DemoSettings.TxPower, RADIO_RAMP_3400_US ); |
GregCr | 0:e5420f1a8a1a | 340 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 341 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 342 | IrqMask = IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 343 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 0:e5420f1a8a1a | 344 | for( i = 0; i < localPayloadSize; i++ ) |
GregCr | 0:e5420f1a8a1a | 345 | { |
GregCr | 0:e5420f1a8a1a | 346 | Buffer[i] = ( uint8_t )rand( ); |
GregCr | 0:e5420f1a8a1a | 347 | } |
GregCr | 0:e5420f1a8a1a | 348 | Radio.SendPayload( Buffer, localPayloadSize, 0xFFFFFF ); |
GregCr | 0:e5420f1a8a1a | 349 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 350 | } |
GregCr | 0:e5420f1a8a1a | 351 | else |
GregCr | 0:e5420f1a8a1a | 352 | { |
GregCr | 0:e5420f1a8a1a | 353 | switch( DemoInternalState ) |
GregCr | 0:e5420f1a8a1a | 354 | { |
GregCr | 0:e5420f1a8a1a | 355 | case APP_RX: |
GregCr | 0:e5420f1a8a1a | 356 | break; |
GregCr | 0:e5420f1a8a1a | 357 | |
GregCr | 0:e5420f1a8a1a | 358 | case APP_TX: |
GregCr | 0:e5420f1a8a1a | 359 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 360 | LedBlink( ); |
GregCr | 0:e5420f1a8a1a | 361 | // Send the next frame |
GregCr | 0:e5420f1a8a1a | 362 | IrqMask = IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 363 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 0:e5420f1a8a1a | 364 | |
GregCr | 0:e5420f1a8a1a | 365 | Radio.SendPayload( Buffer, localPayloadSize, 0xFFFFFF ); |
GregCr | 0:e5420f1a8a1a | 366 | // we prepare next payload during transmission |
GregCr | 0:e5420f1a8a1a | 367 | for( i = 0; i < localPayloadSize; i++ ) |
GregCr | 0:e5420f1a8a1a | 368 | { |
GregCr | 0:e5420f1a8a1a | 369 | Buffer[i] = ( uint8_t )rand( ); |
GregCr | 0:e5420f1a8a1a | 370 | } |
GregCr | 0:e5420f1a8a1a | 371 | break; |
GregCr | 0:e5420f1a8a1a | 372 | |
GregCr | 0:e5420f1a8a1a | 373 | case APP_RX_TIMEOUT: |
GregCr | 0:e5420f1a8a1a | 374 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 375 | break; |
GregCr | 0:e5420f1a8a1a | 376 | |
GregCr | 0:e5420f1a8a1a | 377 | case APP_RX_ERROR: |
GregCr | 0:e5420f1a8a1a | 378 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 379 | break; |
GregCr | 0:e5420f1a8a1a | 380 | |
GregCr | 0:e5420f1a8a1a | 381 | case APP_TX_TIMEOUT: |
GregCr | 0:e5420f1a8a1a | 382 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 383 | break; |
GregCr | 0:e5420f1a8a1a | 384 | |
GregCr | 0:e5420f1a8a1a | 385 | case APP_IDLE: |
GregCr | 0:e5420f1a8a1a | 386 | break; |
GregCr | 0:e5420f1a8a1a | 387 | |
GregCr | 0:e5420f1a8a1a | 388 | default: |
GregCr | 0:e5420f1a8a1a | 389 | break; |
GregCr | 0:e5420f1a8a1a | 390 | } |
GregCr | 0:e5420f1a8a1a | 391 | } |
GregCr | 0:e5420f1a8a1a | 392 | |
GregCr | 0:e5420f1a8a1a | 393 | return 0; |
GregCr | 0:e5420f1a8a1a | 394 | } |
GregCr | 0:e5420f1a8a1a | 395 | |
GregCr | 0:e5420f1a8a1a | 396 | uint8_t RunDemoRxContinuous( void ) |
GregCr | 0:e5420f1a8a1a | 397 | { |
GregCr | 0:e5420f1a8a1a | 398 | uint8_t refreshDisplay = 0; |
GregCr | 0:e5420f1a8a1a | 399 | |
GregCr | 0:e5420f1a8a1a | 400 | if( Eeprom.EepromData.DemoSettings.HoldDemo == true ) |
GregCr | 0:e5420f1a8a1a | 401 | { |
GregCr | 0:e5420f1a8a1a | 402 | return 0; |
GregCr | 0:e5420f1a8a1a | 403 | } |
GregCr | 0:e5420f1a8a1a | 404 | |
GregCr | 0:e5420f1a8a1a | 405 | if( DemoRunning == false ) |
GregCr | 0:e5420f1a8a1a | 406 | { |
GregCr | 0:e5420f1a8a1a | 407 | DemoRunning = true; |
GregCr | 0:e5420f1a8a1a | 408 | |
GregCr | 0:e5420f1a8a1a | 409 | printf( "Start RunDemoRxContinuous\n\r" ); |
GregCr | 0:e5420f1a8a1a | 410 | |
GregCr | 0:e5420f1a8a1a | 411 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 412 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 413 | |
GregCr | 0:e5420f1a8a1a | 414 | Eeprom.EepromData.DemoSettings.CntPacketTx = 0; |
GregCr | 0:e5420f1a8a1a | 415 | Eeprom.EepromData.DemoSettings.CntPacketRxOK = 0; |
GregCr | 0:e5420f1a8a1a | 416 | Eeprom.EepromData.DemoSettings.CntPacketRxKO = 0; |
GregCr | 0:e5420f1a8a1a | 417 | |
GregCr | 0:e5420f1a8a1a | 418 | InitializeDemoParameters( Eeprom.EepromData.DemoSettings.ModulationType ); |
GregCr | 0:e5420f1a8a1a | 419 | |
GregCr | 0:e5420f1a8a1a | 420 | // set preamble to max size to avoid filtering of the receiver on preamble lenght |
GregCr | 0:e5420f1a8a1a | 421 | if( Eeprom.EepromData.DemoSettings.ModulationType == PACKET_TYPE_LORA ) |
GregCr | 0:e5420f1a8a1a | 422 | { |
GregCr | 0:e5420f1a8a1a | 423 | PacketParams.Params.LoRa.PreambleLength = 0xFFFF; |
GregCr | 0:e5420f1a8a1a | 424 | PacketParams.Params.LoRa.HeaderType = ( RadioLoRaPacketLengthsMode_t )Eeprom.EepromData.DemoSettings.PacketParam2; |
GregCr | 0:e5420f1a8a1a | 425 | PacketParams.Params.LoRa.PayloadLength = Eeprom.EepromData.DemoSettings.PacketParam3; |
GregCr | 0:e5420f1a8a1a | 426 | PacketParams.Params.LoRa.CrcMode = ( RadioLoRaCrcModes_t ) Eeprom.EepromData.DemoSettings.PacketParam4; |
GregCr | 0:e5420f1a8a1a | 427 | PacketParams.Params.LoRa.InvertIQ = ( RadioLoRaIQModes_t ) Eeprom.EepromData.DemoSettings.PacketParam5; |
GregCr | 0:e5420f1a8a1a | 428 | Radio.SetPacketParams( &PacketParams ); |
GregCr | 0:e5420f1a8a1a | 429 | } |
GregCr | 0:e5420f1a8a1a | 430 | |
GregCr | 0:e5420f1a8a1a | 431 | IrqMask = IRQ_RX_DONE | IRQ_CRC_ERROR | IRQ_RX_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 432 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 0:e5420f1a8a1a | 433 | // Rx Continuous |
GregCr | 0:e5420f1a8a1a | 434 | if( Eeprom.EepromData.DemoSettings.BoostedRx == false ) |
GregCr | 0:e5420f1a8a1a | 435 | { |
GregCr | 0:e5420f1a8a1a | 436 | Radio.SetRx( 0xFFFFFF ); |
GregCr | 0:e5420f1a8a1a | 437 | } |
GregCr | 0:e5420f1a8a1a | 438 | else |
GregCr | 0:e5420f1a8a1a | 439 | { |
GregCr | 0:e5420f1a8a1a | 440 | Radio.SetRxBoosted( 0xFFFFFF ); |
GregCr | 0:e5420f1a8a1a | 441 | } |
GregCr | 0:e5420f1a8a1a | 442 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 443 | |
GregCr | 0:e5420f1a8a1a | 444 | } |
GregCr | 0:e5420f1a8a1a | 445 | |
GregCr | 0:e5420f1a8a1a | 446 | if( Eeprom.EepromData.DemoSettings.MaxNumPacket > 0 ) // != Infinite |
GregCr | 0:e5420f1a8a1a | 447 | { |
GregCr | 0:e5420f1a8a1a | 448 | if( ( Eeprom.EepromData.DemoSettings.CntPacketRxOK + \ |
GregCr | 0:e5420f1a8a1a | 449 | Eeprom.EepromData.DemoSettings.CntPacketRxKO ) >= \ |
GregCr | 0:e5420f1a8a1a | 450 | Eeprom.EepromData.DemoSettings.MaxNumPacket ) |
GregCr | 0:e5420f1a8a1a | 451 | { |
GregCr | 0:e5420f1a8a1a | 452 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 453 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 454 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 455 | Radio.SetStandby( STDBY_RC ); |
GregCr | 0:e5420f1a8a1a | 456 | SendNextPacket.detach( ); |
GregCr | 0:e5420f1a8a1a | 457 | Eeprom.EepromData.DemoSettings.HoldDemo = true; |
GregCr | 0:e5420f1a8a1a | 458 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 459 | } |
GregCr | 0:e5420f1a8a1a | 460 | } |
GregCr | 0:e5420f1a8a1a | 461 | |
GregCr | 0:e5420f1a8a1a | 462 | Radio.ProcessIrqs( ); |
GregCr | 0:e5420f1a8a1a | 463 | |
GregCr | 0:e5420f1a8a1a | 464 | switch( DemoInternalState ) |
GregCr | 0:e5420f1a8a1a | 465 | { |
GregCr | 0:e5420f1a8a1a | 466 | case APP_TX: |
GregCr | 0:e5420f1a8a1a | 467 | break; |
GregCr | 0:e5420f1a8a1a | 468 | |
GregCr | 0:e5420f1a8a1a | 469 | case APP_RX: |
GregCr | 0:e5420f1a8a1a | 470 | RX_LED = !RX_LED; |
GregCr | 0:e5420f1a8a1a | 471 | Radio.GetPayload( Buffer, &BufferSize, BUFFER_SIZE ); |
GregCr | 0:e5420f1a8a1a | 472 | Radio.GetPacketStatus( &PacketStatus ); |
GregCr | 0:e5420f1a8a1a | 473 | if( Eeprom.EepromData.DemoSettings.ModulationType == PACKET_TYPE_LORA ) |
GregCr | 0:e5420f1a8a1a | 474 | { |
GregCr | 0:e5420f1a8a1a | 475 | Eeprom.EepromData.DemoSettings.RssiValue = PacketStatus.Params.LoRa.RssiPkt; |
GregCr | 0:e5420f1a8a1a | 476 | Eeprom.EepromData.DemoSettings.SnrValue = PacketStatus.Params.LoRa.SnrPkt; |
GregCr | 0:e5420f1a8a1a | 477 | if( ( PacketStatus.Params.LoRa.FreqError & 0x00080000 ) == 0x00080000 ) |
GregCr | 0:e5420f1a8a1a | 478 | { |
GregCr | 0:e5420f1a8a1a | 479 | PacketStatus.Params.LoRa.FreqError = 0xFFFFF - PacketStatus.Params.LoRa.FreqError; |
GregCr | 0:e5420f1a8a1a | 480 | } |
GregCr | 0:e5420f1a8a1a | 481 | Eeprom.EepromData.DemoSettings.FreqErrorEst = ( double )PacketStatus.Params.LoRa.FreqError * FreErrorLsb; |
GregCr | 0:e5420f1a8a1a | 482 | } |
GregCr | 0:e5420f1a8a1a | 483 | else // Eeprom.EepromData.DemoSettings.ModulationType == PACKET_TYPE_GFSK |
GregCr | 0:e5420f1a8a1a | 484 | { |
GregCr | 0:e5420f1a8a1a | 485 | Eeprom.EepromData.DemoSettings.RssiValue = PacketStatus.Params.Gfsk.RssiAvg; |
GregCr | 0:e5420f1a8a1a | 486 | Eeprom.EepromData.DemoSettings.SnrValue = 0; |
GregCr | 0:e5420f1a8a1a | 487 | } |
GregCr | 0:e5420f1a8a1a | 488 | |
GregCr | 0:e5420f1a8a1a | 489 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 490 | Eeprom.EepromData.DemoSettings.CntPacketRxOK ++; |
GregCr | 0:e5420f1a8a1a | 491 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 492 | break; |
GregCr | 0:e5420f1a8a1a | 493 | |
GregCr | 0:e5420f1a8a1a | 494 | case APP_RX_ERROR: |
GregCr | 0:e5420f1a8a1a | 495 | case APP_RX_TIMEOUT: |
GregCr | 0:e5420f1a8a1a | 496 | Eeprom.EepromData.DemoSettings.CntPacketRxKO ++; |
GregCr | 1:b96176a4ccb8 | 497 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 498 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 499 | break; |
GregCr | 0:e5420f1a8a1a | 500 | |
GregCr | 0:e5420f1a8a1a | 501 | case APP_TX_TIMEOUT: |
GregCr | 0:e5420f1a8a1a | 502 | break; |
GregCr | 0:e5420f1a8a1a | 503 | |
GregCr | 0:e5420f1a8a1a | 504 | case APP_IDLE: |
GregCr | 0:e5420f1a8a1a | 505 | break; |
GregCr | 0:e5420f1a8a1a | 506 | |
GregCr | 0:e5420f1a8a1a | 507 | default: |
GregCr | 0:e5420f1a8a1a | 508 | break; |
GregCr | 0:e5420f1a8a1a | 509 | } |
GregCr | 0:e5420f1a8a1a | 510 | return refreshDisplay; |
GregCr | 0:e5420f1a8a1a | 511 | } |
GregCr | 0:e5420f1a8a1a | 512 | |
GregCr | 0:e5420f1a8a1a | 513 | // ************************* PER Demo ****************************** |
GregCr | 0:e5420f1a8a1a | 514 | // * * |
GregCr | 0:e5420f1a8a1a | 515 | // * * |
GregCr | 0:e5420f1a8a1a | 516 | // * * |
GregCr | 0:e5420f1a8a1a | 517 | // ***************************************************************************** |
GregCr | 0:e5420f1a8a1a | 518 | uint8_t RunDemoApplicationPer( void ) |
GregCr | 0:e5420f1a8a1a | 519 | { |
GregCr | 0:e5420f1a8a1a | 520 | uint8_t i = 0; |
GregCr | 0:e5420f1a8a1a | 521 | uint8_t refreshDisplay = 0; |
GregCr | 0:e5420f1a8a1a | 522 | |
GregCr | 0:e5420f1a8a1a | 523 | if( Eeprom.EepromData.DemoSettings.HoldDemo == true ) |
GregCr | 0:e5420f1a8a1a | 524 | { |
GregCr | 0:e5420f1a8a1a | 525 | return 0; |
GregCr | 0:e5420f1a8a1a | 526 | } |
GregCr | 0:e5420f1a8a1a | 527 | |
GregCr | 0:e5420f1a8a1a | 528 | if( DemoRunning == false ) |
GregCr | 0:e5420f1a8a1a | 529 | { |
GregCr | 0:e5420f1a8a1a | 530 | DemoRunning = true; |
GregCr | 0:e5420f1a8a1a | 531 | |
GregCr | 0:e5420f1a8a1a | 532 | printf( "Start RunDemoApplicationPer\n\r" ); |
GregCr | 0:e5420f1a8a1a | 533 | |
GregCr | 0:e5420f1a8a1a | 534 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 535 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 536 | |
GregCr | 0:e5420f1a8a1a | 537 | Eeprom.EepromData.DemoSettings.CntPacketTx = 0; |
GregCr | 0:e5420f1a8a1a | 538 | Eeprom.EepromData.DemoSettings.CntPacketRxOK = 0; |
GregCr | 0:e5420f1a8a1a | 539 | Eeprom.EepromData.DemoSettings.CntPacketRxKO = 0; |
GregCr | 0:e5420f1a8a1a | 540 | Eeprom.EepromData.DemoSettings.RxTimeOutCount = 0; |
GregCr | 0:e5420f1a8a1a | 541 | |
GregCr | 0:e5420f1a8a1a | 542 | InitializeDemoParameters( Eeprom.EepromData.DemoSettings.ModulationType ); |
GregCr | 0:e5420f1a8a1a | 543 | |
GregCr | 0:e5420f1a8a1a | 544 | Eeprom.EepromData.DemoSettings.InterPacketDelay = GetTimeOnAir( Eeprom.EepromData.DemoSettings.ModulationType ) + RX_TX_TRANSITION_WAIT; |
GregCr | 0:e5420f1a8a1a | 545 | |
GregCr | 0:e5420f1a8a1a | 546 | if( Eeprom.EepromData.DemoSettings.Entity == MASTER ) |
GregCr | 0:e5420f1a8a1a | 547 | { |
GregCr | 0:e5420f1a8a1a | 548 | SendNextPacket.attach_us( &SendNextPacketEvent, ( uint32_t )( ( Eeprom.EepromData.DemoSettings.InterPacketDelay + RX_TIMEOUT_MARGIN ) * 1000 ) ); |
GregCr | 0:e5420f1a8a1a | 549 | DemoInternalState = APP_TX; |
GregCr | 0:e5420f1a8a1a | 550 | } |
GregCr | 0:e5420f1a8a1a | 551 | else |
GregCr | 0:e5420f1a8a1a | 552 | { |
GregCr | 1:b96176a4ccb8 | 553 | IrqMask = /*0xFFFF; */IRQ_HEADER_VALID | IRQ_RX_DONE | IRQ_CRC_ERROR | IRQ_RX_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 554 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 0:e5420f1a8a1a | 555 | // Rx Single without timeout for the start |
GregCr | 0:e5420f1a8a1a | 556 | if( Eeprom.EepromData.DemoSettings.BoostedRx == false ) |
GregCr | 0:e5420f1a8a1a | 557 | { |
GregCr | 0:e5420f1a8a1a | 558 | Radio.SetRx( 0x0000 ); |
GregCr | 0:e5420f1a8a1a | 559 | } |
GregCr | 0:e5420f1a8a1a | 560 | else |
GregCr | 0:e5420f1a8a1a | 561 | { |
GregCr | 0:e5420f1a8a1a | 562 | Radio.SetRxBoosted( 0x0000 ); |
GregCr | 0:e5420f1a8a1a | 563 | } |
GregCr | 0:e5420f1a8a1a | 564 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 565 | } |
GregCr | 0:e5420f1a8a1a | 566 | } |
GregCr | 0:e5420f1a8a1a | 567 | |
GregCr | 0:e5420f1a8a1a | 568 | Radio.ProcessIrqs( ); |
GregCr | 0:e5420f1a8a1a | 569 | |
GregCr | 0:e5420f1a8a1a | 570 | if( Eeprom.EepromData.DemoSettings.MaxNumPacket > 0 ) // != Infinite |
GregCr | 0:e5420f1a8a1a | 571 | { |
GregCr | 0:e5420f1a8a1a | 572 | if( ( Eeprom.EepromData.DemoSettings.CntPacketRxOK + \ |
GregCr | 0:e5420f1a8a1a | 573 | Eeprom.EepromData.DemoSettings.CntPacketRxKO + \ |
GregCr | 0:e5420f1a8a1a | 574 | Eeprom.EepromData.DemoSettings.RxTimeOutCount) >= \ |
GregCr | 0:e5420f1a8a1a | 575 | Eeprom.EepromData.DemoSettings.MaxNumPacket ) |
GregCr | 0:e5420f1a8a1a | 576 | { |
GregCr | 0:e5420f1a8a1a | 577 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 578 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 579 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 580 | Radio.SetStandby( STDBY_RC ); |
GregCr | 0:e5420f1a8a1a | 581 | SendNextPacket.detach( ); |
GregCr | 0:e5420f1a8a1a | 582 | Eeprom.EepromData.DemoSettings.HoldDemo = true; |
GregCr | 0:e5420f1a8a1a | 583 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 584 | } |
GregCr | 0:e5420f1a8a1a | 585 | } |
GregCr | 0:e5420f1a8a1a | 586 | |
GregCr | 0:e5420f1a8a1a | 587 | switch( DemoInternalState ) |
GregCr | 0:e5420f1a8a1a | 588 | { |
GregCr | 0:e5420f1a8a1a | 589 | case PER_TX_START: |
GregCr | 0:e5420f1a8a1a | 590 | Eeprom.EepromData.DemoSettings.CntPacketTx++; |
GregCr | 0:e5420f1a8a1a | 591 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 592 | |
GregCr | 0:e5420f1a8a1a | 593 | Buffer[0] = 0x00; |
GregCr | 0:e5420f1a8a1a | 594 | Buffer[1] = ( Eeprom.EepromData.DemoSettings.CntPacketTx >> 24 ) & 0xFF; |
GregCr | 0:e5420f1a8a1a | 595 | Buffer[2] = ( Eeprom.EepromData.DemoSettings.CntPacketTx >> 16 ) & 0xFF; |
GregCr | 0:e5420f1a8a1a | 596 | Buffer[3] = ( Eeprom.EepromData.DemoSettings.CntPacketTx >> 8 ) & 0xFF; |
GregCr | 0:e5420f1a8a1a | 597 | Buffer[4] = Eeprom.EepromData.DemoSettings.CntPacketTx & 0xFF; |
GregCr | 0:e5420f1a8a1a | 598 | Buffer[5] = PerMsg[0]; |
GregCr | 0:e5420f1a8a1a | 599 | Buffer[6] = PerMsg[1]; |
GregCr | 0:e5420f1a8a1a | 600 | Buffer[7] = PerMsg[2]; |
GregCr | 0:e5420f1a8a1a | 601 | for( i = 8; i < Eeprom.EepromData.DemoSettings.PayloadLength; i++ ) |
GregCr | 0:e5420f1a8a1a | 602 | { |
GregCr | 0:e5420f1a8a1a | 603 | Buffer[i] = i; |
GregCr | 0:e5420f1a8a1a | 604 | } |
GregCr | 0:e5420f1a8a1a | 605 | TX_LED = !TX_LED; |
GregCr | 0:e5420f1a8a1a | 606 | IrqMask = 0xFFFF; //IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 607 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 0:e5420f1a8a1a | 608 | Radio.SendPayload( Buffer, Eeprom.EepromData.DemoSettings.PayloadLength, ( Eeprom.EepromData.DemoSettings.InterPacketDelay ) << 6 ); |
GregCr | 0:e5420f1a8a1a | 609 | break; |
GregCr | 0:e5420f1a8a1a | 610 | |
GregCr | 0:e5420f1a8a1a | 611 | case PER_RX_START: |
GregCr | 1:b96176a4ccb8 | 612 | IrqMask = /*0xFFFF;*/ IRQ_HEADER_VALID | IRQ_RX_DONE | IRQ_CRC_ERROR | IRQ_RX_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 613 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 0:e5420f1a8a1a | 614 | if( Eeprom.EepromData.DemoSettings.BoostedRx == false ) |
GregCr | 0:e5420f1a8a1a | 615 | { |
GregCr | 0:e5420f1a8a1a | 616 | Radio.SetRx( ( Eeprom.EepromData.DemoSettings.InterPacketDelay + RX_TIMEOUT_MARGIN ) << 6 ); |
GregCr | 0:e5420f1a8a1a | 617 | } |
GregCr | 0:e5420f1a8a1a | 618 | else |
GregCr | 0:e5420f1a8a1a | 619 | { |
GregCr | 0:e5420f1a8a1a | 620 | Radio.SetRxBoosted( ( Eeprom.EepromData.DemoSettings.InterPacketDelay + RX_TIMEOUT_MARGIN ) << 6 ); |
GregCr | 0:e5420f1a8a1a | 621 | } |
GregCr | 0:e5420f1a8a1a | 622 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 623 | break; |
GregCr | 0:e5420f1a8a1a | 624 | |
GregCr | 0:e5420f1a8a1a | 625 | case APP_TX: |
GregCr | 0:e5420f1a8a1a | 626 | if( SendNext == true ) |
GregCr | 0:e5420f1a8a1a | 627 | { |
GregCr | 0:e5420f1a8a1a | 628 | SendNext = false; |
GregCr | 0:e5420f1a8a1a | 629 | if( Eeprom.EepromData.DemoSettings.MaxNumPacket == 0 ) |
GregCr | 0:e5420f1a8a1a | 630 | { |
GregCr | 0:e5420f1a8a1a | 631 | DemoInternalState = PER_TX_START; // Infinite -> send next |
GregCr | 0:e5420f1a8a1a | 632 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 633 | } |
GregCr | 0:e5420f1a8a1a | 634 | else if( Eeprom.EepromData.DemoSettings.CntPacketTx < \ |
GregCr | 0:e5420f1a8a1a | 635 | Eeprom.EepromData.DemoSettings.MaxNumPacket ) |
GregCr | 0:e5420f1a8a1a | 636 | { |
GregCr | 0:e5420f1a8a1a | 637 | DemoInternalState = PER_TX_START; // MaxNumPacket not sent |
GregCr | 0:e5420f1a8a1a | 638 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 639 | } |
GregCr | 0:e5420f1a8a1a | 640 | else // MaxNumPacket sent -> end of demo |
GregCr | 0:e5420f1a8a1a | 641 | { |
GregCr | 0:e5420f1a8a1a | 642 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 643 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 644 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 645 | Radio.SetStandby( STDBY_RC ); |
GregCr | 0:e5420f1a8a1a | 646 | SendNextPacket.detach( ); |
GregCr | 0:e5420f1a8a1a | 647 | Eeprom.EepromData.DemoSettings.HoldDemo = true; |
GregCr | 0:e5420f1a8a1a | 648 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 649 | } |
GregCr | 0:e5420f1a8a1a | 650 | } |
GregCr | 0:e5420f1a8a1a | 651 | break; |
GregCr | 0:e5420f1a8a1a | 652 | |
GregCr | 0:e5420f1a8a1a | 653 | case APP_RX: |
GregCr | 0:e5420f1a8a1a | 654 | RX_LED = !RX_LED; |
GregCr | 0:e5420f1a8a1a | 655 | Radio.GetPayload( Buffer, &BufferSize, BUFFER_SIZE ); |
GregCr | 0:e5420f1a8a1a | 656 | Radio.GetPacketStatus( &PacketStatus ); |
GregCr | 0:e5420f1a8a1a | 657 | if( Eeprom.EepromData.DemoSettings.ModulationType == PACKET_TYPE_LORA ) |
GregCr | 0:e5420f1a8a1a | 658 | { |
GregCr | 0:e5420f1a8a1a | 659 | Eeprom.EepromData.DemoSettings.RssiValue = PacketStatus.Params.LoRa.RssiPkt; |
GregCr | 0:e5420f1a8a1a | 660 | Eeprom.EepromData.DemoSettings.SnrValue = PacketStatus.Params.LoRa.SnrPkt; |
GregCr | 0:e5420f1a8a1a | 661 | if( ( PacketStatus.Params.LoRa.FreqError & 0x00080000 ) == 0x00080000 ) |
GregCr | 0:e5420f1a8a1a | 662 | { |
GregCr | 0:e5420f1a8a1a | 663 | PacketStatus.Params.LoRa.FreqError = 0xFFFFF - PacketStatus.Params.LoRa.FreqError; |
GregCr | 0:e5420f1a8a1a | 664 | } |
GregCr | 0:e5420f1a8a1a | 665 | Eeprom.EepromData.DemoSettings.FreqErrorEst = ( double )PacketStatus.Params.LoRa.FreqError * FreErrorLsb; |
GregCr | 0:e5420f1a8a1a | 666 | } |
GregCr | 0:e5420f1a8a1a | 667 | else // Eeprom.EepromData.DemoSettings.ModulationType == PACKET_TYPE_GFSK |
GregCr | 0:e5420f1a8a1a | 668 | { |
GregCr | 0:e5420f1a8a1a | 669 | Eeprom.EepromData.DemoSettings.RssiValue = PacketStatus.Params.Gfsk.RssiAvg; |
GregCr | 0:e5420f1a8a1a | 670 | Eeprom.EepromData.DemoSettings.SnrValue = 0; |
GregCr | 0:e5420f1a8a1a | 671 | if( ( PacketStatus.Params.Gfsk.FreqError & 0x00000800 ) == 0x00000800 ) |
GregCr | 0:e5420f1a8a1a | 672 | { |
GregCr | 0:e5420f1a8a1a | 673 | PacketStatus.Params.Gfsk.FreqError = 0x00000FFF - PacketStatus.Params.Gfsk.FreqError; |
GregCr | 0:e5420f1a8a1a | 674 | } |
GregCr | 0:e5420f1a8a1a | 675 | PacketStatus.Params.LoRa.FreqError = PacketStatus.Params.LoRa.FreqError / 2048; |
GregCr | 0:e5420f1a8a1a | 676 | Eeprom.EepromData.DemoSettings.FreqErrorEst = ( ( double )PacketStatus.Params.Gfsk.FreqError / 2048.0 ); |
GregCr | 0:e5420f1a8a1a | 677 | } |
GregCr | 0:e5420f1a8a1a | 678 | |
GregCr | 0:e5420f1a8a1a | 679 | DemoInternalState = PER_RX_START; |
GregCr | 0:e5420f1a8a1a | 680 | if( ( BufferSize >= PER_SIZE ) && ( strncmp( ( const char* )( Buffer + 5 ), ( const char* )PerMsg, PER_SIZE ) == 0 ) ) |
GregCr | 0:e5420f1a8a1a | 681 | { |
GregCr | 0:e5420f1a8a1a | 682 | ComputePerPayload( Buffer, BufferSize ); |
GregCr | 0:e5420f1a8a1a | 683 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 684 | } |
GregCr | 0:e5420f1a8a1a | 685 | else |
GregCr | 0:e5420f1a8a1a | 686 | { |
GregCr | 0:e5420f1a8a1a | 687 | Eeprom.EepromData.DemoSettings.RxTimeOutCount++; |
GregCr | 0:e5420f1a8a1a | 688 | } |
GregCr | 0:e5420f1a8a1a | 689 | break; |
GregCr | 0:e5420f1a8a1a | 690 | |
GregCr | 0:e5420f1a8a1a | 691 | case APP_RX_ERROR: |
GregCr | 0:e5420f1a8a1a | 692 | case APP_RX_TIMEOUT: |
GregCr | 0:e5420f1a8a1a | 693 | Eeprom.EepromData.DemoSettings.RxTimeOutCount++; |
GregCr | 0:e5420f1a8a1a | 694 | DemoInternalState = PER_RX_START; |
GregCr | 0:e5420f1a8a1a | 695 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 696 | break; |
GregCr | 0:e5420f1a8a1a | 697 | |
GregCr | 0:e5420f1a8a1a | 698 | case APP_TX_TIMEOUT: |
GregCr | 0:e5420f1a8a1a | 699 | printf( "Failure: timeout in Tx is shorter than the packet time on air\n\r" ); |
GregCr | 0:e5420f1a8a1a | 700 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 701 | Eeprom.EepromData.DemoSettings.HoldDemo = true; |
GregCr | 0:e5420f1a8a1a | 702 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 703 | break; |
GregCr | 0:e5420f1a8a1a | 704 | |
GregCr | 0:e5420f1a8a1a | 705 | case APP_IDLE: // do nothing |
GregCr | 0:e5420f1a8a1a | 706 | /* |
GregCr | 0:e5420f1a8a1a | 707 | val = Radio.GetSysErrors( ); |
GregCr | 0:e5420f1a8a1a | 708 | if( val.Value != 0x0000 ) |
GregCr | 0:e5420f1a8a1a | 709 | { |
GregCr | 0:e5420f1a8a1a | 710 | printf(" Dev Error = 0x%04x \n\r", val.Value ); |
GregCr | 0:e5420f1a8a1a | 711 | } |
GregCr | 0:e5420f1a8a1a | 712 | */ |
GregCr | 0:e5420f1a8a1a | 713 | break; |
GregCr | 0:e5420f1a8a1a | 714 | |
GregCr | 0:e5420f1a8a1a | 715 | default: |
GregCr | 0:e5420f1a8a1a | 716 | break; |
GregCr | 0:e5420f1a8a1a | 717 | } |
GregCr | 0:e5420f1a8a1a | 718 | return refreshDisplay; |
GregCr | 0:e5420f1a8a1a | 719 | } |
GregCr | 0:e5420f1a8a1a | 720 | |
GregCr | 0:e5420f1a8a1a | 721 | void ComputePerPayload( uint8_t *buffer, uint8_t bufferSize ) |
GregCr | 0:e5420f1a8a1a | 722 | { |
GregCr | 0:e5420f1a8a1a | 723 | uint32_t i = 0; |
GregCr | 0:e5420f1a8a1a | 724 | |
GregCr | 0:e5420f1a8a1a | 725 | Eeprom.EepromData.DemoSettings.CntPacketRxOK++; |
GregCr | 0:e5420f1a8a1a | 726 | PacketRxSequence = ( ( uint32_t )buffer[1] << 24 ) | \ |
GregCr | 0:e5420f1a8a1a | 727 | ( ( uint32_t )buffer[2] << 16 ) | \ |
GregCr | 0:e5420f1a8a1a | 728 | ( ( uint32_t )buffer[3] << 8 ) | \ |
GregCr | 0:e5420f1a8a1a | 729 | buffer[4]; |
GregCr | 0:e5420f1a8a1a | 730 | |
GregCr | 0:e5420f1a8a1a | 731 | if( ( PacketRxSequence <= PacketRxSequencePrev ) || \ |
GregCr | 0:e5420f1a8a1a | 732 | ( PacketRxSequencePrev == 0xFFFFFFFF ) ) |
GregCr | 0:e5420f1a8a1a | 733 | { |
GregCr | 0:e5420f1a8a1a | 734 | // Sequence went back => resynchronization |
GregCr | 0:e5420f1a8a1a | 735 | // Don't count missed packets this time |
GregCr | 0:e5420f1a8a1a | 736 | i = 0; |
GregCr | 0:e5420f1a8a1a | 737 | } |
GregCr | 0:e5420f1a8a1a | 738 | else |
GregCr | 0:e5420f1a8a1a | 739 | { |
GregCr | 0:e5420f1a8a1a | 740 | // Determine number of missed packets |
GregCr | 0:e5420f1a8a1a | 741 | i = PacketRxSequence - PacketRxSequencePrev - 1; |
GregCr | 0:e5420f1a8a1a | 742 | } |
GregCr | 0:e5420f1a8a1a | 743 | // Be ready for the next |
GregCr | 0:e5420f1a8a1a | 744 | PacketRxSequencePrev = PacketRxSequence; |
GregCr | 0:e5420f1a8a1a | 745 | // increment 'missed' counter for the RX session |
GregCr | 0:e5420f1a8a1a | 746 | Eeprom.EepromData.DemoSettings.CntPacketRxKO += i; |
GregCr | 0:e5420f1a8a1a | 747 | Eeprom.EepromData.DemoSettings.RxTimeOutCount = 0; |
GregCr | 0:e5420f1a8a1a | 748 | } |
GregCr | 0:e5420f1a8a1a | 749 | |
GregCr | 0:e5420f1a8a1a | 750 | // ************************ Ping Pong Demo ***************************** |
GregCr | 0:e5420f1a8a1a | 751 | // * * |
GregCr | 0:e5420f1a8a1a | 752 | // * * |
GregCr | 0:e5420f1a8a1a | 753 | // * * |
GregCr | 0:e5420f1a8a1a | 754 | // ***************************************************************************** |
GregCr | 0:e5420f1a8a1a | 755 | uint8_t RunDemoApplicationPingPong( void ) |
GregCr | 0:e5420f1a8a1a | 756 | { |
GregCr | 0:e5420f1a8a1a | 757 | uint8_t i = 0; |
GregCr | 0:e5420f1a8a1a | 758 | uint8_t refreshDisplay = 0; |
GregCr | 0:e5420f1a8a1a | 759 | |
GregCr | 0:e5420f1a8a1a | 760 | if( Eeprom.EepromData.DemoSettings.HoldDemo == true ) |
GregCr | 0:e5420f1a8a1a | 761 | { |
GregCr | 0:e5420f1a8a1a | 762 | return 0; // quit without refresh display |
GregCr | 0:e5420f1a8a1a | 763 | } |
GregCr | 0:e5420f1a8a1a | 764 | |
GregCr | 0:e5420f1a8a1a | 765 | if( DemoRunning == false ) |
GregCr | 0:e5420f1a8a1a | 766 | { |
GregCr | 0:e5420f1a8a1a | 767 | DemoRunning = true; |
GregCr | 0:e5420f1a8a1a | 768 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 769 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 770 | Eeprom.EepromData.DemoSettings.CntPacketTx = 0; |
GregCr | 0:e5420f1a8a1a | 771 | Eeprom.EepromData.DemoSettings.CntPacketRxOK = 0; |
GregCr | 0:e5420f1a8a1a | 772 | Eeprom.EepromData.DemoSettings.CntPacketRxOKSlave = 0; |
GregCr | 0:e5420f1a8a1a | 773 | Eeprom.EepromData.DemoSettings.CntPacketRxKO = 0; |
GregCr | 0:e5420f1a8a1a | 774 | Eeprom.EepromData.DemoSettings.CntPacketRxKOSlave = 0; |
GregCr | 0:e5420f1a8a1a | 775 | Eeprom.EepromData.DemoSettings.RxTimeOutCount = 0; |
GregCr | 0:e5420f1a8a1a | 776 | |
GregCr | 0:e5420f1a8a1a | 777 | InitializeDemoParameters( Eeprom.EepromData.DemoSettings.ModulationType ); |
GregCr | 0:e5420f1a8a1a | 778 | |
GregCr | 0:e5420f1a8a1a | 779 | Eeprom.EepromData.DemoSettings.InterPacketDelay = GetTimeOnAir( Eeprom.EepromData.DemoSettings.ModulationType ) + RX_TX_TRANSITION_WAIT; |
GregCr | 0:e5420f1a8a1a | 780 | |
GregCr | 0:e5420f1a8a1a | 781 | printf( "Start RunDemoApplicationPingPong.\n\r" ); |
GregCr | 0:e5420f1a8a1a | 782 | |
GregCr | 0:e5420f1a8a1a | 783 | if( Eeprom.EepromData.DemoSettings.Entity == MASTER ) |
GregCr | 0:e5420f1a8a1a | 784 | { |
GregCr | 0:e5420f1a8a1a | 785 | DemoInternalState = SEND_PING_MSG; |
GregCr | 0:e5420f1a8a1a | 786 | uint32_t temp = ( Eeprom.EepromData.DemoSettings.InterPacketDelay << 1 ) + RX_TIMEOUT_MARGIN; |
GregCr | 1:b96176a4ccb8 | 787 | float val = ( float )temp / 1000.0; |
GregCr | 0:e5420f1a8a1a | 788 | SendNextPacket.attach( &SendNextPacketEvent, val ); |
GregCr | 0:e5420f1a8a1a | 789 | } |
GregCr | 0:e5420f1a8a1a | 790 | else |
GregCr | 0:e5420f1a8a1a | 791 | { |
GregCr | 0:e5420f1a8a1a | 792 | IrqMask = IRQ_RX_DONE | IRQ_CRC_ERROR | IRQ_RX_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 793 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 0:e5420f1a8a1a | 794 | // Rx Single without timeout for the start |
GregCr | 0:e5420f1a8a1a | 795 | RX_LED = !RX_LED; |
GregCr | 0:e5420f1a8a1a | 796 | |
GregCr | 0:e5420f1a8a1a | 797 | if( Eeprom.EepromData.DemoSettings.BoostedRx == false ) |
GregCr | 0:e5420f1a8a1a | 798 | { |
GregCr | 0:e5420f1a8a1a | 799 | Radio.SetRx( 0x0000 ); |
GregCr | 0:e5420f1a8a1a | 800 | } |
GregCr | 0:e5420f1a8a1a | 801 | else |
GregCr | 0:e5420f1a8a1a | 802 | { |
GregCr | 0:e5420f1a8a1a | 803 | Radio.SetRxBoosted( 0x0000 ); |
GregCr | 0:e5420f1a8a1a | 804 | } |
GregCr | 0:e5420f1a8a1a | 805 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 806 | } |
GregCr | 0:e5420f1a8a1a | 807 | } |
GregCr | 0:e5420f1a8a1a | 808 | |
GregCr | 0:e5420f1a8a1a | 809 | Radio.ProcessIrqs( ); |
GregCr | 0:e5420f1a8a1a | 810 | |
GregCr | 0:e5420f1a8a1a | 811 | if( Eeprom.EepromData.DemoSettings.Entity == MASTER ) |
GregCr | 0:e5420f1a8a1a | 812 | { |
GregCr | 0:e5420f1a8a1a | 813 | switch( DemoInternalState ) |
GregCr | 0:e5420f1a8a1a | 814 | { |
GregCr | 0:e5420f1a8a1a | 815 | case SEND_PING_MSG: |
GregCr | 0:e5420f1a8a1a | 816 | if( ( Eeprom.EepromData.DemoSettings.MaxNumPacket != 0 ) \ |
GregCr | 0:e5420f1a8a1a | 817 | && ( Eeprom.EepromData.DemoSettings.CntPacketTx >= Eeprom.EepromData.DemoSettings.MaxNumPacket ) ) |
GregCr | 0:e5420f1a8a1a | 818 | { |
GregCr | 0:e5420f1a8a1a | 819 | SendNextPacket.detach( ); |
GregCr | 0:e5420f1a8a1a | 820 | SendNext = false; |
GregCr | 0:e5420f1a8a1a | 821 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 822 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 823 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 824 | Radio.SetStandby( STDBY_RC ); |
GregCr | 0:e5420f1a8a1a | 825 | Eeprom.EepromData.DemoSettings.HoldDemo = true; |
GregCr | 0:e5420f1a8a1a | 826 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 827 | } |
GregCr | 0:e5420f1a8a1a | 828 | else |
GregCr | 0:e5420f1a8a1a | 829 | { |
GregCr | 0:e5420f1a8a1a | 830 | if( SendNext == true ) |
GregCr | 0:e5420f1a8a1a | 831 | { |
GregCr | 0:e5420f1a8a1a | 832 | SendNext = false; |
GregCr | 0:e5420f1a8a1a | 833 | Radio.SetStandby( STDBY_RC ); |
GregCr | 0:e5420f1a8a1a | 834 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 835 | Eeprom.EepromData.DemoSettings.CntPacketTx++; |
GregCr | 0:e5420f1a8a1a | 836 | // Send the next PING frame |
GregCr | 0:e5420f1a8a1a | 837 | Buffer[0] = ( Eeprom.EepromData.DemoSettings.CntPacketTx >> 24 ) & 0xFF; |
GregCr | 0:e5420f1a8a1a | 838 | Buffer[1] = ( Eeprom.EepromData.DemoSettings.CntPacketTx >> 16 ) & 0xFF; |
GregCr | 0:e5420f1a8a1a | 839 | Buffer[2] = ( Eeprom.EepromData.DemoSettings.CntPacketTx >> 8 ) & 0xFF; |
GregCr | 0:e5420f1a8a1a | 840 | Buffer[3] = ( Eeprom.EepromData.DemoSettings.CntPacketTx & 0xFF ); |
GregCr | 0:e5420f1a8a1a | 841 | Buffer[4] = PingMsg[0]; |
GregCr | 0:e5420f1a8a1a | 842 | Buffer[5] = PingMsg[1]; |
GregCr | 0:e5420f1a8a1a | 843 | Buffer[6] = PingMsg[2]; |
GregCr | 0:e5420f1a8a1a | 844 | Buffer[7] = PingMsg[3]; |
GregCr | 0:e5420f1a8a1a | 845 | for( i = 8; i < Eeprom.EepromData.DemoSettings.PayloadLength; i++ ) |
GregCr | 0:e5420f1a8a1a | 846 | { |
GregCr | 0:e5420f1a8a1a | 847 | Buffer[i] = i; |
GregCr | 0:e5420f1a8a1a | 848 | } |
GregCr | 0:e5420f1a8a1a | 849 | TX_LED = !TX_LED; |
GregCr | 0:e5420f1a8a1a | 850 | IrqMask = IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 851 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 0:e5420f1a8a1a | 852 | Radio.SendPayload( Buffer, Eeprom.EepromData.DemoSettings.PayloadLength, \ |
GregCr | 0:e5420f1a8a1a | 853 | Eeprom.EepromData.DemoSettings.InterPacketDelay << 6 ); |
GregCr | 0:e5420f1a8a1a | 854 | } |
GregCr | 0:e5420f1a8a1a | 855 | } |
GregCr | 0:e5420f1a8a1a | 856 | break; |
GregCr | 0:e5420f1a8a1a | 857 | |
GregCr | 0:e5420f1a8a1a | 858 | case APP_TX: |
GregCr | 0:e5420f1a8a1a | 859 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 860 | TX_LED = !TX_LED; |
GregCr | 0:e5420f1a8a1a | 861 | RX_LED = !RX_LED; |
GregCr | 0:e5420f1a8a1a | 862 | IrqMask = IRQ_RX_DONE | IRQ_CRC_ERROR | IRQ_RX_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 863 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 0:e5420f1a8a1a | 864 | |
GregCr | 0:e5420f1a8a1a | 865 | if( Eeprom.EepromData.DemoSettings.BoostedRx == false ) |
GregCr | 0:e5420f1a8a1a | 866 | { |
GregCr | 0:e5420f1a8a1a | 867 | Radio.SetRx( ( ( Eeprom.EepromData.DemoSettings.InterPacketDelay << 1 ) + RX_TIMEOUT_MARGIN ) << 6 ); |
GregCr | 0:e5420f1a8a1a | 868 | } |
GregCr | 0:e5420f1a8a1a | 869 | else |
GregCr | 0:e5420f1a8a1a | 870 | { |
GregCr | 0:e5420f1a8a1a | 871 | Radio.SetRxBoosted( ( ( Eeprom.EepromData.DemoSettings.InterPacketDelay << 1 ) + RX_TIMEOUT_MARGIN ) << 6 ); |
GregCr | 0:e5420f1a8a1a | 872 | } |
GregCr | 0:e5420f1a8a1a | 873 | break; |
GregCr | 0:e5420f1a8a1a | 874 | |
GregCr | 0:e5420f1a8a1a | 875 | case APP_RX: |
GregCr | 0:e5420f1a8a1a | 876 | RX_LED = !RX_LED; |
GregCr | 0:e5420f1a8a1a | 877 | Radio.GetPayload( Buffer, &BufferSize, BUFFER_SIZE ); |
GregCr | 0:e5420f1a8a1a | 878 | Radio.GetPacketStatus( &PacketStatus ); |
GregCr | 0:e5420f1a8a1a | 879 | if( Eeprom.EepromData.ModulationParams.PacketType == PACKET_TYPE_LORA ) |
GregCr | 0:e5420f1a8a1a | 880 | { |
GregCr | 0:e5420f1a8a1a | 881 | Eeprom.EepromData.DemoSettings.RssiValue = PacketStatus.Params.LoRa.RssiPkt; |
GregCr | 0:e5420f1a8a1a | 882 | Eeprom.EepromData.DemoSettings.SnrValue = PacketStatus.Params.LoRa.SnrPkt; |
GregCr | 0:e5420f1a8a1a | 883 | } |
GregCr | 0:e5420f1a8a1a | 884 | else // Eeprom.EepromData.ModulationParams.PacketType == PACKET_TYPE_GFSK |
GregCr | 0:e5420f1a8a1a | 885 | { |
GregCr | 0:e5420f1a8a1a | 886 | Eeprom.EepromData.DemoSettings.RssiValue = PacketStatus.Params.Gfsk.RssiAvg; |
GregCr | 0:e5420f1a8a1a | 887 | Eeprom.EepromData.DemoSettings.SnrValue = 0; |
GregCr | 0:e5420f1a8a1a | 888 | } |
GregCr | 0:e5420f1a8a1a | 889 | |
GregCr | 0:e5420f1a8a1a | 890 | if( ( BufferSize >= PINGPONG_SIZE ) && ( strncmp( ( const char* )( Buffer + 8 ), ( const char* )PongMsg, PINGPONG_SIZE ) == 0 ) ) |
GregCr | 0:e5420f1a8a1a | 891 | { |
GregCr | 0:e5420f1a8a1a | 892 | ComputePingPongPayload( Buffer, BufferSize ); |
GregCr | 0:e5420f1a8a1a | 893 | } |
GregCr | 0:e5420f1a8a1a | 894 | else |
GregCr | 0:e5420f1a8a1a | 895 | { |
GregCr | 0:e5420f1a8a1a | 896 | Eeprom.EepromData.DemoSettings.CntPacketRxKO++; |
GregCr | 0:e5420f1a8a1a | 897 | } |
GregCr | 0:e5420f1a8a1a | 898 | DemoInternalState = SEND_PING_MSG; |
GregCr | 0:e5420f1a8a1a | 899 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 900 | break; |
GregCr | 0:e5420f1a8a1a | 901 | |
GregCr | 0:e5420f1a8a1a | 902 | case APP_RX_TIMEOUT: |
GregCr | 0:e5420f1a8a1a | 903 | case APP_RX_ERROR: |
GregCr | 0:e5420f1a8a1a | 904 | RX_LED = !RX_LED; |
GregCr | 0:e5420f1a8a1a | 905 | Eeprom.EepromData.DemoSettings.CntPacketRxKO++; |
GregCr | 0:e5420f1a8a1a | 906 | DemoInternalState = SEND_PING_MSG; |
GregCr | 0:e5420f1a8a1a | 907 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 908 | break; |
GregCr | 0:e5420f1a8a1a | 909 | |
GregCr | 0:e5420f1a8a1a | 910 | case APP_TX_TIMEOUT: |
GregCr | 0:e5420f1a8a1a | 911 | printf( "Failure: timeout in Tx is shorter than the packet time on air\n\r" ); |
GregCr | 0:e5420f1a8a1a | 912 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 913 | Eeprom.EepromData.DemoSettings.HoldDemo = true; |
GregCr | 0:e5420f1a8a1a | 914 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 915 | break; |
GregCr | 0:e5420f1a8a1a | 916 | |
GregCr | 0:e5420f1a8a1a | 917 | case APP_IDLE: // do nothing |
GregCr | 0:e5420f1a8a1a | 918 | break; |
GregCr | 0:e5420f1a8a1a | 919 | |
GregCr | 0:e5420f1a8a1a | 920 | default: |
GregCr | 0:e5420f1a8a1a | 921 | break; |
GregCr | 0:e5420f1a8a1a | 922 | } |
GregCr | 0:e5420f1a8a1a | 923 | } |
GregCr | 0:e5420f1a8a1a | 924 | else // SLAVE |
GregCr | 0:e5420f1a8a1a | 925 | { |
GregCr | 0:e5420f1a8a1a | 926 | switch( DemoInternalState ) |
GregCr | 0:e5420f1a8a1a | 927 | { |
GregCr | 0:e5420f1a8a1a | 928 | case SEND_PONG_MSG: |
GregCr | 0:e5420f1a8a1a | 929 | wait_ms( RX_TX_TRANSITION_WAIT ); |
GregCr | 0:e5420f1a8a1a | 930 | |
GregCr | 0:e5420f1a8a1a | 931 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 932 | // Send the next PING frame |
GregCr | 0:e5420f1a8a1a | 933 | Buffer[0] = ( Eeprom.EepromData.DemoSettings.CntPacketTx >> 24 ) & 0xFF; |
GregCr | 0:e5420f1a8a1a | 934 | Buffer[1] = ( Eeprom.EepromData.DemoSettings.CntPacketTx >> 16 ) & 0xFF; |
GregCr | 0:e5420f1a8a1a | 935 | Buffer[2] = ( Eeprom.EepromData.DemoSettings.CntPacketTx >> 8 ) & 0xFF; |
GregCr | 0:e5420f1a8a1a | 936 | Buffer[3] = ( Eeprom.EepromData.DemoSettings.CntPacketTx & 0xFF ); |
GregCr | 0:e5420f1a8a1a | 937 | Buffer[4] = ( ( Eeprom.EepromData.DemoSettings.CntPacketRxKO + \ |
GregCr | 0:e5420f1a8a1a | 938 | Eeprom.EepromData.DemoSettings.RxTimeOutCount ) >> 24 ) & 0xFF; |
GregCr | 0:e5420f1a8a1a | 939 | Buffer[5] = ( ( Eeprom.EepromData.DemoSettings.CntPacketRxKO + \ |
GregCr | 0:e5420f1a8a1a | 940 | Eeprom.EepromData.DemoSettings.RxTimeOutCount ) >> 16 ) & 0xFF; |
GregCr | 0:e5420f1a8a1a | 941 | Buffer[6] = ( ( Eeprom.EepromData.DemoSettings.CntPacketRxKO + \ |
GregCr | 0:e5420f1a8a1a | 942 | Eeprom.EepromData.DemoSettings.RxTimeOutCount ) >> 8 ) & 0xFF; |
GregCr | 0:e5420f1a8a1a | 943 | Buffer[7] = ( ( Eeprom.EepromData.DemoSettings.CntPacketRxKO + \ |
GregCr | 0:e5420f1a8a1a | 944 | Eeprom.EepromData.DemoSettings.RxTimeOutCount ) & 0xFF ); |
GregCr | 0:e5420f1a8a1a | 945 | Buffer[8] = PongMsg[0]; |
GregCr | 0:e5420f1a8a1a | 946 | Buffer[9] = PongMsg[1]; |
GregCr | 0:e5420f1a8a1a | 947 | Buffer[10] = PongMsg[2]; |
GregCr | 0:e5420f1a8a1a | 948 | Buffer[11] = PongMsg[3]; |
GregCr | 0:e5420f1a8a1a | 949 | for( i = 12; i < Eeprom.EepromData.DemoSettings.PayloadLength; i++ ) |
GregCr | 0:e5420f1a8a1a | 950 | { |
GregCr | 0:e5420f1a8a1a | 951 | Buffer[i] = i; |
GregCr | 0:e5420f1a8a1a | 952 | } |
GregCr | 0:e5420f1a8a1a | 953 | TX_LED = !TX_LED; |
GregCr | 0:e5420f1a8a1a | 954 | IrqMask = IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 955 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 0:e5420f1a8a1a | 956 | Radio.SendPayload( Buffer, Eeprom.EepromData.DemoSettings.PayloadLength, \ |
GregCr | 0:e5420f1a8a1a | 957 | ( Eeprom.EepromData.DemoSettings.InterPacketDelay ) << 6 ); |
GregCr | 0:e5420f1a8a1a | 958 | break; |
GregCr | 0:e5420f1a8a1a | 959 | |
GregCr | 0:e5420f1a8a1a | 960 | case APP_TX: |
GregCr | 0:e5420f1a8a1a | 961 | if( ( Eeprom.EepromData.DemoSettings.MaxNumPacket != 0 ) \ |
GregCr | 0:e5420f1a8a1a | 962 | && ( ( Eeprom.EepromData.DemoSettings.CntPacketRxOK + Eeprom.EepromData.DemoSettings.CntPacketRxKO + \ |
GregCr | 0:e5420f1a8a1a | 963 | Eeprom.EepromData.DemoSettings.RxTimeOutCount ) >= Eeprom.EepromData.DemoSettings.MaxNumPacket ) ) |
GregCr | 0:e5420f1a8a1a | 964 | { |
GregCr | 0:e5420f1a8a1a | 965 | SendNextPacket.detach( ); |
GregCr | 0:e5420f1a8a1a | 966 | SendNext = false; |
GregCr | 0:e5420f1a8a1a | 967 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 968 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 969 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 970 | Radio.SetStandby( STDBY_RC ); |
GregCr | 0:e5420f1a8a1a | 971 | Eeprom.EepromData.DemoSettings.HoldDemo = true; |
GregCr | 0:e5420f1a8a1a | 972 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 973 | } |
GregCr | 0:e5420f1a8a1a | 974 | else |
GregCr | 0:e5420f1a8a1a | 975 | { |
GregCr | 0:e5420f1a8a1a | 976 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 977 | TX_LED = !TX_LED; |
GregCr | 0:e5420f1a8a1a | 978 | RX_LED = !RX_LED; |
GregCr | 0:e5420f1a8a1a | 979 | IrqMask = IRQ_RX_DONE | IRQ_CRC_ERROR | IRQ_RX_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 980 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 0:e5420f1a8a1a | 981 | if( Eeprom.EepromData.DemoSettings.BoostedRx == false ) |
GregCr | 0:e5420f1a8a1a | 982 | { |
GregCr | 0:e5420f1a8a1a | 983 | Radio.SetRx( ( ( Eeprom.EepromData.DemoSettings.InterPacketDelay << 1 ) + RX_TIMEOUT_MARGIN ) << 6 ); |
GregCr | 0:e5420f1a8a1a | 984 | } |
GregCr | 0:e5420f1a8a1a | 985 | else |
GregCr | 0:e5420f1a8a1a | 986 | { |
GregCr | 2:8e1b4210df6b | 987 | Radio.SetRxBoosted( ( ( Eeprom.EepromData.DemoSettings.InterPacketDelay << 1 ) + RX_TIMEOUT_MARGIN ) << 6 ); |
GregCr | 0:e5420f1a8a1a | 988 | } |
GregCr | 0:e5420f1a8a1a | 989 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 990 | } |
GregCr | 0:e5420f1a8a1a | 991 | break; |
GregCr | 0:e5420f1a8a1a | 992 | |
GregCr | 0:e5420f1a8a1a | 993 | case APP_RX: |
GregCr | 0:e5420f1a8a1a | 994 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 995 | RX_LED = !RX_LED; |
GregCr | 0:e5420f1a8a1a | 996 | Radio.GetPayload( Buffer, &BufferSize, BUFFER_SIZE ); |
GregCr | 0:e5420f1a8a1a | 997 | Radio.GetPacketStatus( &PacketStatus ); |
GregCr | 0:e5420f1a8a1a | 998 | if( Eeprom.EepromData.ModulationParams.PacketType == PACKET_TYPE_LORA ) |
GregCr | 0:e5420f1a8a1a | 999 | { |
GregCr | 0:e5420f1a8a1a | 1000 | Eeprom.EepromData.DemoSettings.RssiValue = PacketStatus.Params.LoRa.RssiPkt; |
GregCr | 0:e5420f1a8a1a | 1001 | Eeprom.EepromData.DemoSettings.SnrValue = PacketStatus.Params.LoRa.SnrPkt; |
GregCr | 0:e5420f1a8a1a | 1002 | } |
GregCr | 0:e5420f1a8a1a | 1003 | else // Eeprom.EepromData.ModulationParams.PacketType == PACKET_TYPE_GFSK |
GregCr | 0:e5420f1a8a1a | 1004 | { |
GregCr | 0:e5420f1a8a1a | 1005 | Eeprom.EepromData.DemoSettings.RssiValue = PacketStatus.Params.Gfsk.RssiAvg; |
GregCr | 0:e5420f1a8a1a | 1006 | Eeprom.EepromData.DemoSettings.SnrValue = 0; |
GregCr | 0:e5420f1a8a1a | 1007 | } |
GregCr | 0:e5420f1a8a1a | 1008 | |
GregCr | 0:e5420f1a8a1a | 1009 | if( ( BufferSize >= PINGPONG_SIZE ) && ( strncmp( ( const char* )( Buffer + 4 ), ( const char* )PingMsg, PINGPONG_SIZE ) == 0 ) ) |
GregCr | 0:e5420f1a8a1a | 1010 | { |
GregCr | 0:e5420f1a8a1a | 1011 | ComputePingPongPayload( Buffer, BufferSize ); |
GregCr | 0:e5420f1a8a1a | 1012 | DemoInternalState = SEND_PONG_MSG; |
GregCr | 0:e5420f1a8a1a | 1013 | } |
GregCr | 0:e5420f1a8a1a | 1014 | else |
GregCr | 0:e5420f1a8a1a | 1015 | { |
GregCr | 0:e5420f1a8a1a | 1016 | Eeprom.EepromData.DemoSettings.CntPacketRxKO++; |
GregCr | 0:e5420f1a8a1a | 1017 | RX_LED = !RX_LED; |
GregCr | 0:e5420f1a8a1a | 1018 | IrqMask = IRQ_RX_DONE | IRQ_CRC_ERROR | IRQ_RX_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 1019 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 0:e5420f1a8a1a | 1020 | if( Eeprom.EepromData.DemoSettings.BoostedRx == false ) |
GregCr | 0:e5420f1a8a1a | 1021 | { |
GregCr | 0:e5420f1a8a1a | 1022 | Radio.SetRx( ( Eeprom.EepromData.DemoSettings.InterPacketDelay ) << 6 ); |
GregCr | 0:e5420f1a8a1a | 1023 | } |
GregCr | 0:e5420f1a8a1a | 1024 | else |
GregCr | 0:e5420f1a8a1a | 1025 | { |
GregCr | 0:e5420f1a8a1a | 1026 | Radio.SetRxBoosted( ( Eeprom.EepromData.DemoSettings.InterPacketDelay ) << 6 ); |
GregCr | 0:e5420f1a8a1a | 1027 | } |
GregCr | 0:e5420f1a8a1a | 1028 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 1029 | } |
GregCr | 0:e5420f1a8a1a | 1030 | break; |
GregCr | 0:e5420f1a8a1a | 1031 | |
GregCr | 0:e5420f1a8a1a | 1032 | case APP_RX_TIMEOUT: |
GregCr | 0:e5420f1a8a1a | 1033 | case APP_RX_ERROR: |
GregCr | 0:e5420f1a8a1a | 1034 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 1035 | Eeprom.EepromData.DemoSettings.RxTimeOutCount++; |
GregCr | 0:e5420f1a8a1a | 1036 | IrqMask = IRQ_RX_DONE | IRQ_CRC_ERROR | IRQ_RX_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 1037 | Radio.SetDioIrqParams( IrqMask, IrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); |
GregCr | 0:e5420f1a8a1a | 1038 | if( Eeprom.EepromData.DemoSettings.BoostedRx == false ) |
GregCr | 0:e5420f1a8a1a | 1039 | { |
GregCr | 2:8e1b4210df6b | 1040 | Radio.SetRx( ( ( Eeprom.EepromData.DemoSettings.InterPacketDelay << 1 ) + RX_TIMEOUT_MARGIN ) << 6 ); |
GregCr | 0:e5420f1a8a1a | 1041 | } |
GregCr | 0:e5420f1a8a1a | 1042 | else |
GregCr | 0:e5420f1a8a1a | 1043 | { |
GregCr | 2:8e1b4210df6b | 1044 | Radio.SetRxBoosted( ( ( Eeprom.EepromData.DemoSettings.InterPacketDelay << 1 ) + RX_TIMEOUT_MARGIN ) << 6 ); |
GregCr | 0:e5420f1a8a1a | 1045 | } |
GregCr | 0:e5420f1a8a1a | 1046 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 1047 | break; |
GregCr | 0:e5420f1a8a1a | 1048 | |
GregCr | 0:e5420f1a8a1a | 1049 | case APP_TX_TIMEOUT: |
GregCr | 0:e5420f1a8a1a | 1050 | printf( "Failure: timeout in Tx is shorter than the packet time on air\n\r" ); |
GregCr | 0:e5420f1a8a1a | 1051 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 1052 | Eeprom.EepromData.DemoSettings.HoldDemo = true; |
GregCr | 0:e5420f1a8a1a | 1053 | refreshDisplay = 1; |
GregCr | 0:e5420f1a8a1a | 1054 | break; |
GregCr | 0:e5420f1a8a1a | 1055 | |
GregCr | 0:e5420f1a8a1a | 1056 | case APP_IDLE: // do nothing |
GregCr | 0:e5420f1a8a1a | 1057 | break; |
GregCr | 0:e5420f1a8a1a | 1058 | |
GregCr | 0:e5420f1a8a1a | 1059 | default: |
GregCr | 0:e5420f1a8a1a | 1060 | break; |
GregCr | 0:e5420f1a8a1a | 1061 | } |
GregCr | 0:e5420f1a8a1a | 1062 | } |
GregCr | 0:e5420f1a8a1a | 1063 | return refreshDisplay; |
GregCr | 0:e5420f1a8a1a | 1064 | } |
GregCr | 0:e5420f1a8a1a | 1065 | |
GregCr | 0:e5420f1a8a1a | 1066 | void ComputePingPongPayload( uint8_t *buffer, uint8_t bufferSize ) |
GregCr | 0:e5420f1a8a1a | 1067 | { |
GregCr | 0:e5420f1a8a1a | 1068 | uint32_t i = 0; |
GregCr | 0:e5420f1a8a1a | 1069 | |
GregCr | 0:e5420f1a8a1a | 1070 | PacketRxSequence = ( ( uint32_t )buffer[0] << 24 ) | \ |
GregCr | 0:e5420f1a8a1a | 1071 | ( ( uint32_t )buffer[1] << 16 ) | \ |
GregCr | 0:e5420f1a8a1a | 1072 | ( ( uint32_t )buffer[2] << 8 ) | \ |
GregCr | 0:e5420f1a8a1a | 1073 | buffer[3]; |
GregCr | 0:e5420f1a8a1a | 1074 | |
GregCr | 0:e5420f1a8a1a | 1075 | if( Eeprom.EepromData.DemoSettings.Entity == MASTER ) |
GregCr | 0:e5420f1a8a1a | 1076 | { |
GregCr | 0:e5420f1a8a1a | 1077 | Eeprom.EepromData.DemoSettings.CntPacketRxKOSlave = |
GregCr | 0:e5420f1a8a1a | 1078 | ( ( uint32_t )buffer[4] << 24 ) | \ |
GregCr | 0:e5420f1a8a1a | 1079 | ( ( uint32_t )buffer[5] << 16 ) | \ |
GregCr | 0:e5420f1a8a1a | 1080 | ( ( uint32_t )buffer[6] << 8 ) | \ |
GregCr | 0:e5420f1a8a1a | 1081 | buffer[7]; |
GregCr | 0:e5420f1a8a1a | 1082 | if( PacketRxSequence > Eeprom.EepromData.DemoSettings.CntPacketRxKOSlave ) |
GregCr | 0:e5420f1a8a1a | 1083 | { |
GregCr | 0:e5420f1a8a1a | 1084 | Eeprom.EepromData.DemoSettings.CntPacketRxOKSlave = PacketRxSequence - \ |
GregCr | 0:e5420f1a8a1a | 1085 | Eeprom.EepromData.DemoSettings.CntPacketRxKOSlave; |
GregCr | 0:e5420f1a8a1a | 1086 | } |
GregCr | 0:e5420f1a8a1a | 1087 | else |
GregCr | 0:e5420f1a8a1a | 1088 | { |
GregCr | 0:e5420f1a8a1a | 1089 | Eeprom.EepromData.DemoSettings.CntPacketRxOKSlave = 0; |
GregCr | 0:e5420f1a8a1a | 1090 | } |
GregCr | 0:e5420f1a8a1a | 1091 | |
GregCr | 0:e5420f1a8a1a | 1092 | if( PacketRxSequence == Eeprom.EepromData.DemoSettings.CntPacketTx ) |
GregCr | 0:e5420f1a8a1a | 1093 | { |
GregCr | 0:e5420f1a8a1a | 1094 | Eeprom.EepromData.DemoSettings.CntPacketRxOK += 1; |
GregCr | 0:e5420f1a8a1a | 1095 | } |
GregCr | 0:e5420f1a8a1a | 1096 | else |
GregCr | 0:e5420f1a8a1a | 1097 | { |
GregCr | 0:e5420f1a8a1a | 1098 | Eeprom.EepromData.DemoSettings.CntPacketRxKO += 1; |
GregCr | 0:e5420f1a8a1a | 1099 | } |
GregCr | 0:e5420f1a8a1a | 1100 | } |
GregCr | 0:e5420f1a8a1a | 1101 | else |
GregCr | 0:e5420f1a8a1a | 1102 | { |
GregCr | 0:e5420f1a8a1a | 1103 | Eeprom.EepromData.DemoSettings.CntPacketRxOK += 1; |
GregCr | 0:e5420f1a8a1a | 1104 | if( ( PacketRxSequence <= PacketRxSequencePrev ) || \ |
GregCr | 0:e5420f1a8a1a | 1105 | ( PacketRxSequencePrev == 0 ) ) |
GregCr | 0:e5420f1a8a1a | 1106 | { |
GregCr | 0:e5420f1a8a1a | 1107 | // Sequence went back => resynchronization |
GregCr | 0:e5420f1a8a1a | 1108 | // Don't count missed packets this time |
GregCr | 0:e5420f1a8a1a | 1109 | i = 0; |
GregCr | 0:e5420f1a8a1a | 1110 | } |
GregCr | 0:e5420f1a8a1a | 1111 | else |
GregCr | 0:e5420f1a8a1a | 1112 | { |
GregCr | 0:e5420f1a8a1a | 1113 | // Determine number of missed packets |
GregCr | 0:e5420f1a8a1a | 1114 | i = PacketRxSequence - PacketRxSequencePrev - 1; |
GregCr | 0:e5420f1a8a1a | 1115 | } |
GregCr | 0:e5420f1a8a1a | 1116 | // Be ready for the next |
GregCr | 0:e5420f1a8a1a | 1117 | PacketRxSequencePrev = PacketRxSequence; |
GregCr | 0:e5420f1a8a1a | 1118 | Eeprom.EepromData.DemoSettings.CntPacketTx = PacketRxSequence; |
GregCr | 0:e5420f1a8a1a | 1119 | // increment 'missed' counter for the RX session |
GregCr | 0:e5420f1a8a1a | 1120 | Eeprom.EepromData.DemoSettings.CntPacketRxKO += i; |
GregCr | 0:e5420f1a8a1a | 1121 | Eeprom.EepromData.DemoSettings.RxTimeOutCount = 0; |
GregCr | 0:e5420f1a8a1a | 1122 | } |
GregCr | 0:e5420f1a8a1a | 1123 | } |
GregCr | 0:e5420f1a8a1a | 1124 | |
GregCr | 0:e5420f1a8a1a | 1125 | // ************************ Utils **************************** |
GregCr | 0:e5420f1a8a1a | 1126 | // * * |
GregCr | 0:e5420f1a8a1a | 1127 | // * * |
GregCr | 0:e5420f1a8a1a | 1128 | // * * |
GregCr | 0:e5420f1a8a1a | 1129 | // ***************************************************************************** |
GregCr | 0:e5420f1a8a1a | 1130 | |
GregCr | 0:e5420f1a8a1a | 1131 | uint8_t GetConnectedDevice( void ) |
GregCr | 0:e5420f1a8a1a | 1132 | { |
GregCr | 0:e5420f1a8a1a | 1133 | return( Radio.GetDeviceType( ) ); |
GregCr | 0:e5420f1a8a1a | 1134 | } |
GregCr | 0:e5420f1a8a1a | 1135 | |
GregCr | 1:b96176a4ccb8 | 1136 | uint8_t GetMatchingFrequency( void ) |
GregCr | 1:b96176a4ccb8 | 1137 | { |
GregCr | 1:b96176a4ccb8 | 1138 | return( Radio.GetFreqSelect( ) ); |
GregCr | 1:b96176a4ccb8 | 1139 | } |
GregCr | 1:b96176a4ccb8 | 1140 | |
GregCr | 0:e5420f1a8a1a | 1141 | void InitDemoApplication( void ) |
GregCr | 0:e5420f1a8a1a | 1142 | { |
GregCr | 0:e5420f1a8a1a | 1143 | RX_LED = 1; |
GregCr | 0:e5420f1a8a1a | 1144 | TX_LED = 1; |
GregCr | 0:e5420f1a8a1a | 1145 | |
GregCr | 0:e5420f1a8a1a | 1146 | Radio.Init( ); |
GregCr | 0:e5420f1a8a1a | 1147 | |
GregCr | 0:e5420f1a8a1a | 1148 | // Can also be set in LDO mode but consume more power |
GregCr | 0:e5420f1a8a1a | 1149 | Radio.SetRegulatorMode( ( RadioRegulatorMode_t )Eeprom.EepromData.DemoSettings.RadioPowerMode ); |
GregCr | 0:e5420f1a8a1a | 1150 | Radio.SetStandby( STDBY_RC ); |
GregCr | 0:e5420f1a8a1a | 1151 | |
GregCr | 0:e5420f1a8a1a | 1152 | memset( &Buffer, 0x00, BufferSize ); |
GregCr | 0:e5420f1a8a1a | 1153 | |
GregCr | 0:e5420f1a8a1a | 1154 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 1155 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 1156 | |
GregCr | 0:e5420f1a8a1a | 1157 | PacketRxSequence = 0; |
GregCr | 0:e5420f1a8a1a | 1158 | PacketRxSequencePrev = 0; |
GregCr | 0:e5420f1a8a1a | 1159 | Eeprom.EepromData.DemoSettings.CntPacketTx = 0; |
GregCr | 0:e5420f1a8a1a | 1160 | Eeprom.EepromData.DemoSettings.CntPacketRxOK = 0; |
GregCr | 0:e5420f1a8a1a | 1161 | Eeprom.EepromData.DemoSettings.CntPacketRxKO = 0; |
GregCr | 0:e5420f1a8a1a | 1162 | Eeprom.EepromData.DemoSettings.RxTimeOutCount = 0; |
GregCr | 0:e5420f1a8a1a | 1163 | } |
GregCr | 0:e5420f1a8a1a | 1164 | |
GregCr | 0:e5420f1a8a1a | 1165 | void StopDemoApplication( void ) |
GregCr | 0:e5420f1a8a1a | 1166 | { |
GregCr | 0:e5420f1a8a1a | 1167 | if( DemoRunning == true ) |
GregCr | 0:e5420f1a8a1a | 1168 | { |
GregCr | 0:e5420f1a8a1a | 1169 | Radio.CheckDeviceReady( ); |
GregCr | 0:e5420f1a8a1a | 1170 | |
GregCr | 0:e5420f1a8a1a | 1171 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 1172 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 1173 | DemoRunning = false; |
GregCr | 0:e5420f1a8a1a | 1174 | SendNext = false; |
GregCr | 0:e5420f1a8a1a | 1175 | PacketRxSequence = 0; |
GregCr | 0:e5420f1a8a1a | 1176 | PacketRxSequencePrev = 0; |
GregCr | 0:e5420f1a8a1a | 1177 | Eeprom.EepromData.DemoSettings.CntPacketTx = 0; |
GregCr | 0:e5420f1a8a1a | 1178 | Eeprom.EepromData.DemoSettings.CntPacketRxOK = 0; |
GregCr | 0:e5420f1a8a1a | 1179 | Eeprom.EepromData.DemoSettings.CntPacketRxKO = 0; |
GregCr | 0:e5420f1a8a1a | 1180 | Eeprom.EepromData.DemoSettings.RxTimeOutCount = 0; |
GregCr | 0:e5420f1a8a1a | 1181 | |
GregCr | 0:e5420f1a8a1a | 1182 | DemoInternalState = APP_IDLE; |
GregCr | 0:e5420f1a8a1a | 1183 | Radio.SetStandby( STDBY_RC ); |
GregCr | 0:e5420f1a8a1a | 1184 | Radio.ClearIrqStatus( IRQ_RADIO_ALL ); |
GregCr | 0:e5420f1a8a1a | 1185 | SendNextPacket.detach( ); |
GregCr | 0:e5420f1a8a1a | 1186 | } |
GregCr | 0:e5420f1a8a1a | 1187 | } |
GregCr | 0:e5420f1a8a1a | 1188 | |
GregCr | 0:e5420f1a8a1a | 1189 | /* |
GregCr | 0:e5420f1a8a1a | 1190 | * Function still being implemented >>> To be completed |
GregCr | 0:e5420f1a8a1a | 1191 | * WARNING: Computation is in float and his really slow |
GregCr | 0:e5420f1a8a1a | 1192 | * LongInterLeaving vs LegacyInterLeaving has no influence on TimeOnAir. |
GregCr | 0:e5420f1a8a1a | 1193 | */ |
GregCr | 0:e5420f1a8a1a | 1194 | uint16_t GetTimeOnAir( uint8_t modulation ) |
GregCr | 0:e5420f1a8a1a | 1195 | { |
GregCr | 0:e5420f1a8a1a | 1196 | uint16_t result = 2000; |
GregCr | 0:e5420f1a8a1a | 1197 | uint8_t LowDatarateOptimize = 0; |
GregCr | 0:e5420f1a8a1a | 1198 | |
GregCr | 0:e5420f1a8a1a | 1199 | if( modulation == PACKET_TYPE_LORA ) |
GregCr | 0:e5420f1a8a1a | 1200 | { |
GregCr | 0:e5420f1a8a1a | 1201 | volatile double loraBw = 0.0; |
GregCr | 0:e5420f1a8a1a | 1202 | volatile double FreqErrorUnits = 0.0; |
GregCr | 0:e5420f1a8a1a | 1203 | |
GregCr | 0:e5420f1a8a1a | 1204 | switch( Eeprom.EepromData.ModulationParams.Params.LoRa.Bandwidth ) |
GregCr | 0:e5420f1a8a1a | 1205 | { |
GregCr | 0:e5420f1a8a1a | 1206 | case LORA_BW_500: |
GregCr | 0:e5420f1a8a1a | 1207 | loraBw = 500e3; |
GregCr | 0:e5420f1a8a1a | 1208 | break; |
GregCr | 0:e5420f1a8a1a | 1209 | |
GregCr | 0:e5420f1a8a1a | 1210 | case LORA_BW_250: |
GregCr | 0:e5420f1a8a1a | 1211 | loraBw = 250e3; |
GregCr | 0:e5420f1a8a1a | 1212 | break; |
GregCr | 0:e5420f1a8a1a | 1213 | |
GregCr | 0:e5420f1a8a1a | 1214 | case LORA_BW_125: |
GregCr | 0:e5420f1a8a1a | 1215 | loraBw = 125e3; |
GregCr | 0:e5420f1a8a1a | 1216 | break; |
GregCr | 0:e5420f1a8a1a | 1217 | |
GregCr | 0:e5420f1a8a1a | 1218 | case LORA_BW_062: |
GregCr | 0:e5420f1a8a1a | 1219 | loraBw = 62e3; |
GregCr | 0:e5420f1a8a1a | 1220 | break; |
GregCr | 0:e5420f1a8a1a | 1221 | |
GregCr | 0:e5420f1a8a1a | 1222 | case LORA_BW_041: |
GregCr | 0:e5420f1a8a1a | 1223 | loraBw = 41e3; |
GregCr | 0:e5420f1a8a1a | 1224 | break; |
GregCr | 0:e5420f1a8a1a | 1225 | |
GregCr | 0:e5420f1a8a1a | 1226 | case LORA_BW_031: |
GregCr | 0:e5420f1a8a1a | 1227 | loraBw = 31e3; |
GregCr | 0:e5420f1a8a1a | 1228 | break; |
GregCr | 0:e5420f1a8a1a | 1229 | |
GregCr | 0:e5420f1a8a1a | 1230 | case LORA_BW_020: |
GregCr | 0:e5420f1a8a1a | 1231 | loraBw = 20e3; |
GregCr | 0:e5420f1a8a1a | 1232 | break; |
GregCr | 0:e5420f1a8a1a | 1233 | |
GregCr | 0:e5420f1a8a1a | 1234 | case LORA_BW_015: |
GregCr | 0:e5420f1a8a1a | 1235 | loraBw = 15e3; |
GregCr | 0:e5420f1a8a1a | 1236 | break; |
GregCr | 0:e5420f1a8a1a | 1237 | |
GregCr | 0:e5420f1a8a1a | 1238 | case LORA_BW_010: |
GregCr | 0:e5420f1a8a1a | 1239 | loraBw = 10e3; |
GregCr | 0:e5420f1a8a1a | 1240 | break; |
GregCr | 0:e5420f1a8a1a | 1241 | |
GregCr | 0:e5420f1a8a1a | 1242 | case LORA_BW_007: |
GregCr | 0:e5420f1a8a1a | 1243 | loraBw = 7e3; |
GregCr | 0:e5420f1a8a1a | 1244 | break; |
GregCr | 0:e5420f1a8a1a | 1245 | |
GregCr | 0:e5420f1a8a1a | 1246 | default: |
GregCr | 0:e5420f1a8a1a | 1247 | loraBw = 7e3; |
GregCr | 0:e5420f1a8a1a | 1248 | break; |
GregCr | 0:e5420f1a8a1a | 1249 | } |
GregCr | 0:e5420f1a8a1a | 1250 | |
GregCr | 0:e5420f1a8a1a | 1251 | /* Used to compute the freq Error */ |
GregCr | 0:e5420f1a8a1a | 1252 | FreqErrorUnits = FREQ_ERR; |
GregCr | 0:e5420f1a8a1a | 1253 | FreErrorLsb = FreqErrorUnits * ( ( double )loraBw / 1000 ) / 500; |
GregCr | 0:e5420f1a8a1a | 1254 | |
GregCr | 0:e5420f1a8a1a | 1255 | float ts = 1 << Eeprom.EepromData.ModulationParams.Params.LoRa.SpreadingFactor; // time for one symbol in ms |
GregCr | 0:e5420f1a8a1a | 1256 | ts = (float)ts / (float)loraBw; |
GregCr | 0:e5420f1a8a1a | 1257 | ts = ts * 1000; // from seconds to miliseconds |
GregCr | 0:e5420f1a8a1a | 1258 | |
GregCr | 0:e5420f1a8a1a | 1259 | float tPreamble = ( Eeprom.EepromData.PacketParams.Params.LoRa.PreambleLength + 4.25 + ( ( Eeprom.EepromData.ModulationParams.Params.LoRa.SpreadingFactor > 6 ) ? 2 : 0 )) * ts; // time of preamble |
GregCr | 0:e5420f1a8a1a | 1260 | |
GregCr | 0:e5420f1a8a1a | 1261 | switch( Eeprom.EepromData.ModulationParams.Params.LoRa.Bandwidth ) |
GregCr | 0:e5420f1a8a1a | 1262 | { |
GregCr | 0:e5420f1a8a1a | 1263 | case LORA_BW_500: |
GregCr | 0:e5420f1a8a1a | 1264 | break; |
GregCr | 0:e5420f1a8a1a | 1265 | |
GregCr | 0:e5420f1a8a1a | 1266 | case LORA_BW_250: |
GregCr | 0:e5420f1a8a1a | 1267 | if( Eeprom.EepromData.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF12 ) |
GregCr | 0:e5420f1a8a1a | 1268 | { |
GregCr | 0:e5420f1a8a1a | 1269 | LowDatarateOptimize = 1; |
GregCr | 0:e5420f1a8a1a | 1270 | } |
GregCr | 0:e5420f1a8a1a | 1271 | break; |
GregCr | 0:e5420f1a8a1a | 1272 | |
GregCr | 0:e5420f1a8a1a | 1273 | case LORA_BW_125: |
GregCr | 0:e5420f1a8a1a | 1274 | if( Eeprom.EepromData.ModulationParams.Params.LoRa.SpreadingFactor >= LORA_SF11 ) |
GregCr | 0:e5420f1a8a1a | 1275 | { |
GregCr | 0:e5420f1a8a1a | 1276 | LowDatarateOptimize = 1; |
GregCr | 0:e5420f1a8a1a | 1277 | } |
GregCr | 0:e5420f1a8a1a | 1278 | break; |
GregCr | 0:e5420f1a8a1a | 1279 | |
GregCr | 0:e5420f1a8a1a | 1280 | case LORA_BW_062: |
GregCr | 0:e5420f1a8a1a | 1281 | if( Eeprom.EepromData.ModulationParams.Params.LoRa.SpreadingFactor >= LORA_SF10 ) |
GregCr | 0:e5420f1a8a1a | 1282 | { |
GregCr | 0:e5420f1a8a1a | 1283 | LowDatarateOptimize = 1; |
GregCr | 0:e5420f1a8a1a | 1284 | } |
GregCr | 0:e5420f1a8a1a | 1285 | break; |
GregCr | 0:e5420f1a8a1a | 1286 | |
GregCr | 0:e5420f1a8a1a | 1287 | case LORA_BW_041: |
GregCr | 0:e5420f1a8a1a | 1288 | case LORA_BW_031: |
GregCr | 0:e5420f1a8a1a | 1289 | if( Eeprom.EepromData.ModulationParams.Params.LoRa.SpreadingFactor >= LORA_SF9 ) |
GregCr | 0:e5420f1a8a1a | 1290 | { |
GregCr | 0:e5420f1a8a1a | 1291 | LowDatarateOptimize = 1; |
GregCr | 0:e5420f1a8a1a | 1292 | } |
GregCr | 0:e5420f1a8a1a | 1293 | break; |
GregCr | 0:e5420f1a8a1a | 1294 | |
GregCr | 0:e5420f1a8a1a | 1295 | case LORA_BW_020: |
GregCr | 0:e5420f1a8a1a | 1296 | case LORA_BW_015: |
GregCr | 0:e5420f1a8a1a | 1297 | if( Eeprom.EepromData.ModulationParams.Params.LoRa.SpreadingFactor >= LORA_SF8 ) |
GregCr | 0:e5420f1a8a1a | 1298 | { |
GregCr | 0:e5420f1a8a1a | 1299 | LowDatarateOptimize = 1; |
GregCr | 0:e5420f1a8a1a | 1300 | } |
GregCr | 0:e5420f1a8a1a | 1301 | break; |
GregCr | 0:e5420f1a8a1a | 1302 | |
GregCr | 0:e5420f1a8a1a | 1303 | case LORA_BW_010: |
GregCr | 0:e5420f1a8a1a | 1304 | case LORA_BW_007: |
GregCr | 0:e5420f1a8a1a | 1305 | if( Eeprom.EepromData.ModulationParams.Params.LoRa.SpreadingFactor >= LORA_SF7 ) |
GregCr | 0:e5420f1a8a1a | 1306 | { |
GregCr | 0:e5420f1a8a1a | 1307 | LowDatarateOptimize = 1; |
GregCr | 0:e5420f1a8a1a | 1308 | } |
GregCr | 0:e5420f1a8a1a | 1309 | break; |
GregCr | 0:e5420f1a8a1a | 1310 | } |
GregCr | 0:e5420f1a8a1a | 1311 | |
GregCr | 0:e5420f1a8a1a | 1312 | float nData = ceil( ( float )( ( 8 * Eeprom.EepromData.PacketParams.Params.LoRa.PayloadLength + \ |
GregCr | 0:e5420f1a8a1a | 1313 | 16 * ( ( Eeprom.EepromData.PacketParams.Params.LoRa.CrcMode == LORA_CRC_OFF ) ? 0 : 1 ) + \ |
GregCr | 0:e5420f1a8a1a | 1314 | ( ( Eeprom.EepromData.PacketParams.Params.LoRa.HeaderType == LORA_PACKET_VARIABLE_LENGTH ) ? 20 : 0 ) - \ |
GregCr | 0:e5420f1a8a1a | 1315 | ( 4 * Eeprom.EepromData.ModulationParams.Params.LoRa.SpreadingFactor ) + 8 - \ |
GregCr | 0:e5420f1a8a1a | 1316 | ( 8 *( ( Eeprom.EepromData.ModulationParams.Params.LoRa.SpreadingFactor > 6 ) ? 1 : 0 ) ) ) / 4 ) ); |
GregCr | 0:e5420f1a8a1a | 1317 | |
GregCr | 0:e5420f1a8a1a | 1318 | nData = ceil( ( float )nData / ( ( float )( Eeprom.EepromData.ModulationParams.Params.LoRa.SpreadingFactor - \ |
GregCr | 0:e5420f1a8a1a | 1319 | ( LowDatarateOptimize * 2 ) ) ) * ( ( Eeprom.EepromData.ModulationParams.Params.LoRa.CodingRate % 4 ) + 4 ) ); |
GregCr | 0:e5420f1a8a1a | 1320 | |
GregCr | 0:e5420f1a8a1a | 1321 | float tPayload = nData * ts; |
GregCr | 0:e5420f1a8a1a | 1322 | |
GregCr | 0:e5420f1a8a1a | 1323 | float tHeader = 8 * ts; |
GregCr | 0:e5420f1a8a1a | 1324 | // Time on air [ms] |
GregCr | 0:e5420f1a8a1a | 1325 | float ToA = ceil( tPreamble + tPayload + tHeader ); |
GregCr | 0:e5420f1a8a1a | 1326 | |
GregCr | 0:e5420f1a8a1a | 1327 | result = ( uint16_t )ToA + ( ( uint16_t )ToA >> 1 ); // Set some margin |
GregCr | 0:e5420f1a8a1a | 1328 | } |
GregCr | 0:e5420f1a8a1a | 1329 | else if( modulation == PACKET_TYPE_GFSK ) |
GregCr | 0:e5420f1a8a1a | 1330 | { |
GregCr | 0:e5420f1a8a1a | 1331 | uint16_t packetBitCount = Eeprom.EepromData.PacketParams.Params.Gfsk.PreambleLength; |
GregCr | 0:e5420f1a8a1a | 1332 | |
GregCr | 0:e5420f1a8a1a | 1333 | packetBitCount += ( Eeprom.EepromData.PacketParams.Params.Gfsk.SyncWordLength + 1 ); |
GregCr | 0:e5420f1a8a1a | 1334 | packetBitCount += Eeprom.EepromData.PacketParams.Params.Gfsk.PayloadLength + 3; |
GregCr | 0:e5420f1a8a1a | 1335 | packetBitCount *= 8; |
GregCr | 0:e5420f1a8a1a | 1336 | // 1500 = 1000 * 1.5 : 1000 for translate s in ms and 1.5 is some margin |
GregCr | 0:e5420f1a8a1a | 1337 | result = ( uint16_t )( ceil( 1500 * ( float )packetBitCount / Eeprom.EepromData.ModulationParams.Params.Gfsk.BitRate ) ); |
GregCr | 0:e5420f1a8a1a | 1338 | } |
GregCr | 0:e5420f1a8a1a | 1339 | return result; |
GregCr | 0:e5420f1a8a1a | 1340 | } |
GregCr | 0:e5420f1a8a1a | 1341 | |
GregCr | 0:e5420f1a8a1a | 1342 | void InitializeDemoParameters( uint8_t modulation ) |
GregCr | 0:e5420f1a8a1a | 1343 | { |
GregCr | 0:e5420f1a8a1a | 1344 | Radio.SetStandby( STDBY_RC ); |
GregCr | 0:e5420f1a8a1a | 1345 | |
GregCr | 0:e5420f1a8a1a | 1346 | Radio.SetRegulatorMode( ( RadioRegulatorMode_t )Eeprom.EepromData.DemoSettings.RadioPowerMode ); |
GregCr | 0:e5420f1a8a1a | 1347 | |
GregCr | 0:e5420f1a8a1a | 1348 | printf("> InitializeDemoParameters\n\r"); |
GregCr | 0:e5420f1a8a1a | 1349 | if( modulation == PACKET_TYPE_LORA ) |
GregCr | 0:e5420f1a8a1a | 1350 | { |
GregCr | 0:e5420f1a8a1a | 1351 | printf("set param LORA for demo\n\r"); |
GregCr | 0:e5420f1a8a1a | 1352 | ModulationParams.PacketType = PACKET_TYPE_LORA; |
GregCr | 0:e5420f1a8a1a | 1353 | PacketParams.PacketType = PACKET_TYPE_LORA; |
GregCr | 0:e5420f1a8a1a | 1354 | |
GregCr | 0:e5420f1a8a1a | 1355 | ModulationParams.Params.LoRa.SpreadingFactor = ( RadioLoRaSpreadingFactors_t ) Eeprom.EepromData.DemoSettings.ModulationParam1; |
GregCr | 0:e5420f1a8a1a | 1356 | ModulationParams.Params.LoRa.Bandwidth = ( RadioLoRaBandwidths_t ) Eeprom.EepromData.DemoSettings.ModulationParam2; |
GregCr | 0:e5420f1a8a1a | 1357 | ModulationParams.Params.LoRa.CodingRate = ( RadioLoRaCodingRates_t ) Eeprom.EepromData.DemoSettings.ModulationParam3; |
GregCr | 0:e5420f1a8a1a | 1358 | |
GregCr | 0:e5420f1a8a1a | 1359 | PacketParams.Params.LoRa.PreambleLength = Eeprom.EepromData.DemoSettings.PacketParam1; |
GregCr | 0:e5420f1a8a1a | 1360 | PacketParams.Params.LoRa.HeaderType = ( RadioLoRaPacketLengthsMode_t )Eeprom.EepromData.DemoSettings.PacketParam2; |
GregCr | 0:e5420f1a8a1a | 1361 | PacketParams.Params.LoRa.PayloadLength = Eeprom.EepromData.DemoSettings.PacketParam3; |
GregCr | 0:e5420f1a8a1a | 1362 | PacketParams.Params.LoRa.CrcMode = ( RadioLoRaCrcModes_t ) Eeprom.EepromData.DemoSettings.PacketParam4; |
GregCr | 0:e5420f1a8a1a | 1363 | PacketParams.Params.LoRa.InvertIQ = ( RadioLoRaIQModes_t ) Eeprom.EepromData.DemoSettings.PacketParam5; |
GregCr | 0:e5420f1a8a1a | 1364 | |
GregCr | 0:e5420f1a8a1a | 1365 | Eeprom.EepromData.DemoSettings.PayloadLength = PacketParams.Params.LoRa.PayloadLength; |
GregCr | 1:b96176a4ccb8 | 1366 | |
GregCr | 1:b96176a4ccb8 | 1367 | if( ( ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF6 ) || ( ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF5 ) ) |
GregCr | 1:b96176a4ccb8 | 1368 | { |
GregCr | 1:b96176a4ccb8 | 1369 | if( PacketParams.Params.LoRa.PreambleLength < 12 ) |
GregCr | 1:b96176a4ccb8 | 1370 | { |
GregCr | 1:b96176a4ccb8 | 1371 | PacketParams.Params.LoRa.PreambleLength = 12; |
GregCr | 1:b96176a4ccb8 | 1372 | } |
GregCr | 1:b96176a4ccb8 | 1373 | } |
GregCr | 0:e5420f1a8a1a | 1374 | } |
GregCr | 0:e5420f1a8a1a | 1375 | else// if( modulation == PACKET_TYPE_GFSK ) |
GregCr | 0:e5420f1a8a1a | 1376 | { |
GregCr | 0:e5420f1a8a1a | 1377 | printf("set param GFSK for demo\n\r"); |
GregCr | 0:e5420f1a8a1a | 1378 | ModulationParams.PacketType = PACKET_TYPE_GFSK; |
GregCr | 0:e5420f1a8a1a | 1379 | PacketParams.PacketType = PACKET_TYPE_GFSK; |
GregCr | 0:e5420f1a8a1a | 1380 | |
GregCr | 0:e5420f1a8a1a | 1381 | ModulationParams.Params.Gfsk.BitRate = Eeprom.EepromData.DemoSettings.ModulationParam1; |
GregCr | 0:e5420f1a8a1a | 1382 | ModulationParams.Params.Gfsk.Fdev = Eeprom.EepromData.DemoSettings.ModulationParam2; |
GregCr | 0:e5420f1a8a1a | 1383 | ModulationParams.Params.Gfsk.ModulationShaping = ( RadioModShapings_t ) Eeprom.EepromData.DemoSettings.ModulationParam3; |
GregCr | 0:e5420f1a8a1a | 1384 | ModulationParams.Params.Gfsk.Bandwidth = ( RadioRxBandwidth_t ) Eeprom.EepromData.DemoSettings.ModulationParam4; |
GregCr | 0:e5420f1a8a1a | 1385 | PacketParams.Params.Gfsk.PreambleLength = Eeprom.EepromData.DemoSettings.PacketParam1; |
GregCr | 0:e5420f1a8a1a | 1386 | PacketParams.Params.Gfsk.PreambleMinDetect = ( RadioPreambleDetection_t )Eeprom.EepromData.DemoSettings.PacketParam2; |
GregCr | 0:e5420f1a8a1a | 1387 | PacketParams.Params.Gfsk.SyncWordLength = Eeprom.EepromData.DemoSettings.PacketParam3; |
GregCr | 0:e5420f1a8a1a | 1388 | PacketParams.Params.Gfsk.AddrComp = ( RadioAddressComp_t ) Eeprom.EepromData.DemoSettings.PacketParam4; |
GregCr | 0:e5420f1a8a1a | 1389 | PacketParams.Params.Gfsk.HeaderType = ( RadioPacketLengthModes_t )Eeprom.EepromData.DemoSettings.PacketParam5; |
GregCr | 0:e5420f1a8a1a | 1390 | PacketParams.Params.Gfsk.PayloadLength = Eeprom.EepromData.DemoSettings.PacketParam6; |
GregCr | 0:e5420f1a8a1a | 1391 | |
GregCr | 0:e5420f1a8a1a | 1392 | PacketParams.Params.Gfsk.CrcLength = ( RadioCrcTypes_t ) Eeprom.EepromData.DemoSettings.PacketParam7; |
GregCr | 0:e5420f1a8a1a | 1393 | PacketParams.Params.Gfsk.DcFree = ( RadioDcFree_t ) Eeprom.EepromData.DemoSettings.PacketParam8; |
GregCr | 0:e5420f1a8a1a | 1394 | |
GregCr | 0:e5420f1a8a1a | 1395 | Eeprom.EepromData.DemoSettings.PayloadLength = PacketParams.Params.Gfsk.PayloadLength; |
GregCr | 0:e5420f1a8a1a | 1396 | } |
GregCr | 0:e5420f1a8a1a | 1397 | |
GregCr | 0:e5420f1a8a1a | 1398 | Radio.SetStandby( STDBY_RC ); |
GregCr | 0:e5420f1a8a1a | 1399 | Radio.ClearIrqStatus( IRQ_RADIO_ALL ); |
GregCr | 0:e5420f1a8a1a | 1400 | Radio.SetPacketType( ModulationParams.PacketType ); |
GregCr | 0:e5420f1a8a1a | 1401 | Radio.SetModulationParams( &ModulationParams ); |
GregCr | 0:e5420f1a8a1a | 1402 | Radio.SetPacketParams( &PacketParams ); |
GregCr | 0:e5420f1a8a1a | 1403 | |
GregCr | 0:e5420f1a8a1a | 1404 | Radio.SetRfFrequency( Eeprom.EepromData.DemoSettings.Frequency ); |
GregCr | 0:e5420f1a8a1a | 1405 | Radio.SetBufferBaseAddresses( 0x00, 0x00 ); |
GregCr | 0:e5420f1a8a1a | 1406 | |
GregCr | 0:e5420f1a8a1a | 1407 | |
GregCr | 1:b96176a4ccb8 | 1408 | Radio.SetTxParams( Eeprom.EepromData.DemoSettings.TxPower, RADIO_RAMP_200_US ); |
GregCr | 0:e5420f1a8a1a | 1409 | |
GregCr | 0:e5420f1a8a1a | 1410 | // only used in GFSK |
GregCr | 0:e5420f1a8a1a | 1411 | Radio.SetSyncWord( ( uint8_t[] ){ 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0 } ); |
GregCr | 0:e5420f1a8a1a | 1412 | Radio.SetWhiteningSeed( 0x01FF ); |
GregCr | 0:e5420f1a8a1a | 1413 | |
GregCr | 0:e5420f1a8a1a | 1414 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 1415 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 1416 | } |
GregCr | 0:e5420f1a8a1a | 1417 | |
GregCr | 0:e5420f1a8a1a | 1418 | /*! |
GregCr | 0:e5420f1a8a1a | 1419 | * \brief Callback of ticker PerSendNextPacket |
GregCr | 0:e5420f1a8a1a | 1420 | */ |
GregCr | 0:e5420f1a8a1a | 1421 | void SendNextPacketEvent( void ) |
GregCr | 0:e5420f1a8a1a | 1422 | { |
GregCr | 0:e5420f1a8a1a | 1423 | SendNext = true; |
GregCr | 0:e5420f1a8a1a | 1424 | } |
GregCr | 0:e5420f1a8a1a | 1425 | |
GregCr | 0:e5420f1a8a1a | 1426 | void LedBlink( void ) |
GregCr | 0:e5420f1a8a1a | 1427 | { |
GregCr | 0:e5420f1a8a1a | 1428 | if( ( TX_LED == 0 ) && ( RX_LED == 0 ) ) |
GregCr | 0:e5420f1a8a1a | 1429 | { |
GregCr | 0:e5420f1a8a1a | 1430 | TX_LED = 1; |
GregCr | 0:e5420f1a8a1a | 1431 | } |
GregCr | 0:e5420f1a8a1a | 1432 | else if( ( TX_LED == 1 ) && ( RX_LED == 0 ) ) |
GregCr | 0:e5420f1a8a1a | 1433 | { |
GregCr | 0:e5420f1a8a1a | 1434 | RX_LED = 1; |
GregCr | 0:e5420f1a8a1a | 1435 | } |
GregCr | 0:e5420f1a8a1a | 1436 | else if( ( TX_LED == 1 ) && ( RX_LED == 1 ) ) |
GregCr | 0:e5420f1a8a1a | 1437 | { |
GregCr | 0:e5420f1a8a1a | 1438 | TX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 1439 | } |
GregCr | 0:e5420f1a8a1a | 1440 | else |
GregCr | 0:e5420f1a8a1a | 1441 | { |
GregCr | 0:e5420f1a8a1a | 1442 | RX_LED = 0; |
GregCr | 0:e5420f1a8a1a | 1443 | } |
GregCr | 0:e5420f1a8a1a | 1444 | } |
GregCr | 0:e5420f1a8a1a | 1445 | |
GregCr | 0:e5420f1a8a1a | 1446 | // ************************ Radio Callbacks **************************** |
GregCr | 0:e5420f1a8a1a | 1447 | // * * |
GregCr | 0:e5420f1a8a1a | 1448 | // * These functions are called through function pointer by the Radio low * |
GregCr | 0:e5420f1a8a1a | 1449 | // * level drivers * |
GregCr | 0:e5420f1a8a1a | 1450 | // * * |
GregCr | 0:e5420f1a8a1a | 1451 | // ***************************************************************************** |
GregCr | 0:e5420f1a8a1a | 1452 | void OnTxDone( void ) |
GregCr | 0:e5420f1a8a1a | 1453 | { |
GregCr | 0:e5420f1a8a1a | 1454 | DemoInternalState = APP_TX; |
GregCr | 0:e5420f1a8a1a | 1455 | } |
GregCr | 0:e5420f1a8a1a | 1456 | |
GregCr | 0:e5420f1a8a1a | 1457 | void OnRxDone( void ) |
GregCr | 0:e5420f1a8a1a | 1458 | { |
GregCr | 0:e5420f1a8a1a | 1459 | DemoInternalState = APP_RX; |
GregCr | 0:e5420f1a8a1a | 1460 | } |
GregCr | 0:e5420f1a8a1a | 1461 | |
GregCr | 0:e5420f1a8a1a | 1462 | void OnTxTimeout( void ) |
GregCr | 0:e5420f1a8a1a | 1463 | { |
GregCr | 0:e5420f1a8a1a | 1464 | DemoInternalState = APP_TX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 1465 | } |
GregCr | 0:e5420f1a8a1a | 1466 | |
GregCr | 0:e5420f1a8a1a | 1467 | void OnRxTimeout( void ) |
GregCr | 0:e5420f1a8a1a | 1468 | { |
GregCr | 0:e5420f1a8a1a | 1469 | DemoInternalState = APP_RX_TIMEOUT; |
GregCr | 0:e5420f1a8a1a | 1470 | } |
GregCr | 0:e5420f1a8a1a | 1471 | |
GregCr | 0:e5420f1a8a1a | 1472 | void OnRxError( IrqErrorCode_t errorCode ) |
GregCr | 0:e5420f1a8a1a | 1473 | { |
GregCr | 0:e5420f1a8a1a | 1474 | DemoInternalState = APP_RX_ERROR; |
GregCr | 0:e5420f1a8a1a | 1475 | |
GregCr | 0:e5420f1a8a1a | 1476 | if( errorCode == IRQ_HEADER_ERROR_CODE ) |
GregCr | 0:e5420f1a8a1a | 1477 | { |
GregCr | 0:e5420f1a8a1a | 1478 | #ifdef ADV_DEBUG |
GregCr | 0:e5420f1a8a1a | 1479 | printf( ">> IRQ_HEADER_ERROR_CODE\n\r" ); |
GregCr | 0:e5420f1a8a1a | 1480 | #endif |
GregCr | 0:e5420f1a8a1a | 1481 | } |
GregCr | 0:e5420f1a8a1a | 1482 | else if( errorCode == IRQ_SYNCWORD_ERROR_CODE ) |
GregCr | 0:e5420f1a8a1a | 1483 | { |
GregCr | 0:e5420f1a8a1a | 1484 | #ifdef ADV_DEBUG |
GregCr | 0:e5420f1a8a1a | 1485 | printf( ">> IRQ_SYNCWORD_ERROR_CODE\n\r" ); |
GregCr | 0:e5420f1a8a1a | 1486 | #endif |
GregCr | 0:e5420f1a8a1a | 1487 | } |
GregCr | 0:e5420f1a8a1a | 1488 | else if( errorCode == IRQ_CRC_ERROR_CODE ) |
GregCr | 0:e5420f1a8a1a | 1489 | { |
GregCr | 0:e5420f1a8a1a | 1490 | #ifdef ADV_DEBUG |
GregCr | 0:e5420f1a8a1a | 1491 | printf( ">> IRQ_CRC_ERROR_CODE\n\r" ); |
GregCr | 0:e5420f1a8a1a | 1492 | #endif |
GregCr | 0:e5420f1a8a1a | 1493 | } |
GregCr | 0:e5420f1a8a1a | 1494 | else |
GregCr | 0:e5420f1a8a1a | 1495 | { |
GregCr | 0:e5420f1a8a1a | 1496 | #ifdef ADV_DEBUG |
GregCr | 0:e5420f1a8a1a | 1497 | printf( "unknown error\n\r" ); |
GregCr | 0:e5420f1a8a1a | 1498 | #endif |
GregCr | 0:e5420f1a8a1a | 1499 | } |
GregCr | 0:e5420f1a8a1a | 1500 | Radio.GetPacketStatus( &PacketStatus ); |
GregCr | 0:e5420f1a8a1a | 1501 | } |
GregCr | 0:e5420f1a8a1a | 1502 | |
GregCr | 0:e5420f1a8a1a | 1503 | void OnCadDone( bool channelActivityDetected ) |
GregCr | 0:e5420f1a8a1a | 1504 | { |
GregCr | 0:e5420f1a8a1a | 1505 | if( channelActivityDetected == true ) |
GregCr | 0:e5420f1a8a1a | 1506 | { |
GregCr | 0:e5420f1a8a1a | 1507 | DemoInternalState = CAD_DONE_CHANNEL_DETECTED; |
GregCr | 0:e5420f1a8a1a | 1508 | } |
GregCr | 0:e5420f1a8a1a | 1509 | else |
GregCr | 0:e5420f1a8a1a | 1510 | { |
GregCr | 0:e5420f1a8a1a | 1511 | DemoInternalState = CAD_DONE; |
GregCr | 0:e5420f1a8a1a | 1512 | } |
GregCr | 0:e5420f1a8a1a | 1513 | } |