Node code for sx1272 LoRa transciever

Dependencies:   mbed BMP085 BufferedSerial DHT Sds021 Chainable_RGB_LED DigitDisplay LoRaWAN-lib SX1272Lib

Fork of LoRaWAN-demo-72-bootcamp by Semtech

Committer:
mluis
Date:
Fri May 13 15:51:40 2016 +0000
Revision:
5:fa113b25f612
Parent:
4:de084e23d884
Child:
7:ceb4063e6863
Updated mbed, LoRaWAN-lib and SX1272Lib libraries.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 0:45496a70a8a5 1 /*
mluis 0:45496a70a8a5 2 / _____) _ | |
mluis 0:45496a70a8a5 3 ( (____ _____ ____ _| |_ _____ ____| |__
mluis 0:45496a70a8a5 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
mluis 0:45496a70a8a5 5 _____) ) ____| | | || |_| ____( (___| | | |
mluis 0:45496a70a8a5 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
mluis 0:45496a70a8a5 7 (C)2015 Semtech
mluis 0:45496a70a8a5 8
mluis 0:45496a70a8a5 9 Description: LoRaMac classA device implementation
mluis 0:45496a70a8a5 10
mluis 0:45496a70a8a5 11 License: Revised BSD License, see LICENSE.TXT file include in the project
mluis 0:45496a70a8a5 12
mluis 0:45496a70a8a5 13 Maintainer: Miguel Luis and Gregory Cristian
mluis 0:45496a70a8a5 14 */
mluis 0:45496a70a8a5 15 #include "mbed.h"
mluis 0:45496a70a8a5 16 #include "board.h"
mluis 0:45496a70a8a5 17 #include "radio.h"
mluis 0:45496a70a8a5 18
mluis 0:45496a70a8a5 19 #include "LoRaMac.h"
mluis 0:45496a70a8a5 20 #include "Comissioning.h"
mluis 0:45496a70a8a5 21 #include "SerialDisplay.h"
GregCr 2:48d8d4806d48 22 #include "DigitDisplay.h"
GregCr 2:48d8d4806d48 23 #include "ChainableLED.h"
mluis 0:45496a70a8a5 24 /*!
mluis 0:45496a70a8a5 25 * Join requests trials duty cycle.
mluis 0:45496a70a8a5 26 */
mluis 5:fa113b25f612 27 #define OVER_THE_AIR_ACTIVATION_DUTYCYCLE 10000000 // 10 [s] value in us
mluis 0:45496a70a8a5 28
mluis 0:45496a70a8a5 29 /*!
mluis 0:45496a70a8a5 30 * Defines the application data transmission duty cycle. 5s, value in [us].
mluis 0:45496a70a8a5 31 */
mluis 0:45496a70a8a5 32 #define APP_TX_DUTYCYCLE 5000000
mluis 0:45496a70a8a5 33
mluis 0:45496a70a8a5 34 /*!
mluis 0:45496a70a8a5 35 * Defines a random delay for application data transmission duty cycle. 1s,
mluis 0:45496a70a8a5 36 * value in [us].
mluis 0:45496a70a8a5 37 */
mluis 0:45496a70a8a5 38 #define APP_TX_DUTYCYCLE_RND 1000000
mluis 0:45496a70a8a5 39
mluis 0:45496a70a8a5 40 /*!
mluis 5:fa113b25f612 41 * Default datarate
mluis 0:45496a70a8a5 42 */
GregCr 2:48d8d4806d48 43 #define LORAWAN_DEFAULT_DATARATE DR_5
mluis 0:45496a70a8a5 44
mluis 0:45496a70a8a5 45 /*!
mluis 0:45496a70a8a5 46 * LoRaWAN confirmed messages
mluis 0:45496a70a8a5 47 */
GregCr 2:48d8d4806d48 48 #define LORAWAN_CONFIRMED_MSG_ON false
mluis 0:45496a70a8a5 49
mluis 0:45496a70a8a5 50 /*!
mluis 0:45496a70a8a5 51 * LoRaWAN Adaptive Data Rate
mluis 0:45496a70a8a5 52 *
mluis 0:45496a70a8a5 53 * \remark Please note that when ADR is enabled the end-device should be static
mluis 0:45496a70a8a5 54 */
mluis 0:45496a70a8a5 55 #define LORAWAN_ADR_ON 1
mluis 0:45496a70a8a5 56
mluis 0:45496a70a8a5 57 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 58
mluis 0:45496a70a8a5 59 #include "LoRaMacTest.h"
mluis 0:45496a70a8a5 60
mluis 0:45496a70a8a5 61 /*!
mluis 0:45496a70a8a5 62 * LoRaWAN ETSI duty cycle control enable/disable
mluis 0:45496a70a8a5 63 *
mluis 0:45496a70a8a5 64 * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
mluis 0:45496a70a8a5 65 */
GregCr 2:48d8d4806d48 66 #define LORAWAN_DUTYCYCLE_ON false
mluis 0:45496a70a8a5 67
mluis 5:fa113b25f612 68 #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1
mluis 5:fa113b25f612 69
mluis 5:fa113b25f612 70 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 5:fa113b25f612 71
mluis 5:fa113b25f612 72 #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:fa113b25f612 73 #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:fa113b25f612 74 #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:fa113b25f612 75 #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:fa113b25f612 76 #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:fa113b25f612 77 #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 }
mluis 5:fa113b25f612 78 #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 }
mluis 5:fa113b25f612 79
mluis 5:fa113b25f612 80 #endif
mluis 5:fa113b25f612 81
mluis 0:45496a70a8a5 82 #endif
mluis 0:45496a70a8a5 83
mluis 0:45496a70a8a5 84 /*!
mluis 0:45496a70a8a5 85 * LoRaWAN application port
mluis 0:45496a70a8a5 86 */
GregCr 3:d4142832d5af 87 #define LORAWAN_APP_PORT 10
mluis 0:45496a70a8a5 88
mluis 0:45496a70a8a5 89 /*!
mluis 0:45496a70a8a5 90 * User application data buffer size
mluis 0:45496a70a8a5 91 */
mluis 0:45496a70a8a5 92 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
mluis 0:45496a70a8a5 93 #define LORAWAN_APP_DATA_SIZE 6
mluis 0:45496a70a8a5 94
mluis 0:45496a70a8a5 95 #else
GregCr 3:d4142832d5af 96 #define LORAWAN_APP_DATA_SIZE 5
mluis 0:45496a70a8a5 97
mluis 0:45496a70a8a5 98 #endif
mluis 0:45496a70a8a5 99
mluis 0:45496a70a8a5 100 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 0:45496a70a8a5 101
mluis 0:45496a70a8a5 102 static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
mluis 0:45496a70a8a5 103 static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
mluis 0:45496a70a8a5 104 static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
mluis 0:45496a70a8a5 105
mluis 0:45496a70a8a5 106 #else
mluis 0:45496a70a8a5 107
mluis 0:45496a70a8a5 108 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mluis 0:45496a70a8a5 109 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
mluis 0:45496a70a8a5 110
mluis 0:45496a70a8a5 111 /*!
mluis 0:45496a70a8a5 112 * Device address
mluis 0:45496a70a8a5 113 */
mluis 0:45496a70a8a5 114 static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
mluis 0:45496a70a8a5 115
mluis 0:45496a70a8a5 116 #endif
mluis 0:45496a70a8a5 117
mluis 0:45496a70a8a5 118 /*!
mluis 0:45496a70a8a5 119 * Application port
mluis 0:45496a70a8a5 120 */
mluis 0:45496a70a8a5 121 static uint8_t AppPort = LORAWAN_APP_PORT;
mluis 0:45496a70a8a5 122
mluis 0:45496a70a8a5 123 /*!
mluis 0:45496a70a8a5 124 * User application data size
mluis 0:45496a70a8a5 125 */
mluis 0:45496a70a8a5 126 static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 0:45496a70a8a5 127
mluis 0:45496a70a8a5 128 /*!
mluis 0:45496a70a8a5 129 * User application data buffer size
mluis 0:45496a70a8a5 130 */
mluis 0:45496a70a8a5 131 #define LORAWAN_APP_DATA_MAX_SIZE 64
mluis 0:45496a70a8a5 132
mluis 0:45496a70a8a5 133 /*!
mluis 0:45496a70a8a5 134 * User application data
mluis 0:45496a70a8a5 135 */
mluis 0:45496a70a8a5 136 static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
mluis 0:45496a70a8a5 137
mluis 0:45496a70a8a5 138 /*!
mluis 0:45496a70a8a5 139 * Indicates if the node is sending confirmed or unconfirmed messages
mluis 0:45496a70a8a5 140 */
mluis 0:45496a70a8a5 141 static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:45496a70a8a5 142
mluis 0:45496a70a8a5 143 /*!
mluis 0:45496a70a8a5 144 * Defines the application data transmission duty cycle
mluis 0:45496a70a8a5 145 */
mluis 0:45496a70a8a5 146 static uint32_t TxDutyCycleTime;
mluis 0:45496a70a8a5 147
mluis 0:45496a70a8a5 148 /*!
mluis 0:45496a70a8a5 149 * Timer to handle the application data transmission duty cycle
mluis 0:45496a70a8a5 150 */
mluis 0:45496a70a8a5 151 static TimerEvent_t TxNextPacketTimer;
mluis 0:45496a70a8a5 152
mluis 0:45496a70a8a5 153 /*!
mluis 0:45496a70a8a5 154 * Specifies the state of the application LED
mluis 0:45496a70a8a5 155 */
mluis 0:45496a70a8a5 156 static bool AppLedStateOn = false;
mluis 0:45496a70a8a5 157 volatile bool Led3StateChanged = false;
mluis 0:45496a70a8a5 158 /*!
mluis 0:45496a70a8a5 159 * Timer to handle the state of LED1
mluis 0:45496a70a8a5 160 */
mluis 0:45496a70a8a5 161 static TimerEvent_t Led1Timer;
mluis 0:45496a70a8a5 162 volatile bool Led1State = false;
mluis 0:45496a70a8a5 163 volatile bool Led1StateChanged = false;
mluis 0:45496a70a8a5 164 /*!
mluis 0:45496a70a8a5 165 * Timer to handle the state of LED2
mluis 0:45496a70a8a5 166 */
mluis 0:45496a70a8a5 167 static TimerEvent_t Led2Timer;
mluis 0:45496a70a8a5 168 volatile bool Led2State = false;
mluis 0:45496a70a8a5 169 volatile bool Led2StateChanged = false;
mluis 0:45496a70a8a5 170
mluis 0:45496a70a8a5 171 /*!
mluis 0:45496a70a8a5 172 * Indicates if a new packet can be sent
mluis 0:45496a70a8a5 173 */
mluis 0:45496a70a8a5 174 static bool NextTx = true;
mluis 0:45496a70a8a5 175
GregCr 3:d4142832d5af 176 /*!
GregCr 3:d4142832d5af 177 * Hold the value returned from the Light Sensor
GregCr 3:d4142832d5af 178 */
GregCr 3:d4142832d5af 179 static float LightValue = 0.0;
GregCr 2:48d8d4806d48 180
GregCr 3:d4142832d5af 181 /*!
GregCr 3:d4142832d5af 182 * Control the 3-color LED
GregCr 3:d4142832d5af 183 * 0: automatic (LED goes brigther as the light decrease,
GregCr 3:d4142832d5af 184 * 1: manual (The LED is controlled by the user)
GregCr 3:d4142832d5af 185 */
mluis 5:fa113b25f612 186 static uint8_t LightMode = 0; // 0: automatic, 1: manual
GregCr 3:d4142832d5af 187
GregCr 3:d4142832d5af 188 /*!
GregCr 3:d4142832d5af 189 * Ticker to create a PWM for the buzzer
GregCr 3:d4142832d5af 190 */
mluis 5:fa113b25f612 191 Ticker BuzzerTimer;
GregCr 2:48d8d4806d48 192
GregCr 2:48d8d4806d48 193
GregCr 3:d4142832d5af 194 /*!
GregCr 3:d4142832d5af 195 * Constructor for Buzzer
GregCr 3:d4142832d5af 196 */
mluis 5:fa113b25f612 197 DigitalOut Buzzer( A3 );
GregCr 3:d4142832d5af 198
GregCr 3:d4142832d5af 199 /*!
GregCr 3:d4142832d5af 200 * Constructor for the 3-color LED
GregCr 3:d4142832d5af 201 */
GregCr 3:d4142832d5af 202 #define NUM_LED 3
mluis 5:fa113b25f612 203 ChainableLED ColorLed( D6, D7, NUM_LED );
GregCr 2:48d8d4806d48 204
GregCr 3:d4142832d5af 205 /*!
GregCr 3:d4142832d5af 206 * Constructor for Light Sensor
GregCr 3:d4142832d5af 207 */
GregCr 3:d4142832d5af 208 AnalogIn LightSens( A1 );
GregCr 3:d4142832d5af 209
GregCr 3:d4142832d5af 210 /*!
GregCr 3:d4142832d5af 211 * Constructor for 4 Digit 7 semgent display
GregCr 3:d4142832d5af 212 */
mluis 5:fa113b25f612 213 DigitDisplay Display( D8, D9 );
GregCr 3:d4142832d5af 214
mluis 0:45496a70a8a5 215 /*!
mluis 0:45496a70a8a5 216 * Device states
mluis 0:45496a70a8a5 217 */
mluis 0:45496a70a8a5 218 static enum eDevicState
mluis 0:45496a70a8a5 219 {
mluis 0:45496a70a8a5 220 DEVICE_STATE_INIT,
mluis 0:45496a70a8a5 221 DEVICE_STATE_JOIN,
mluis 0:45496a70a8a5 222 DEVICE_STATE_SEND,
mluis 0:45496a70a8a5 223 DEVICE_STATE_CYCLE,
mluis 0:45496a70a8a5 224 DEVICE_STATE_SLEEP
mluis 0:45496a70a8a5 225 }DeviceState;
mluis 0:45496a70a8a5 226
mluis 0:45496a70a8a5 227 /*!
mluis 0:45496a70a8a5 228 * LoRaWAN compliance tests support data
mluis 0:45496a70a8a5 229 */
mluis 0:45496a70a8a5 230 struct ComplianceTest_s
mluis 0:45496a70a8a5 231 {
mluis 0:45496a70a8a5 232 bool Running;
mluis 0:45496a70a8a5 233 uint8_t State;
mluis 0:45496a70a8a5 234 bool IsTxConfirmed;
mluis 0:45496a70a8a5 235 uint8_t AppPort;
mluis 0:45496a70a8a5 236 uint8_t AppDataSize;
mluis 0:45496a70a8a5 237 uint8_t *AppDataBuffer;
mluis 0:45496a70a8a5 238 uint16_t DownLinkCounter;
mluis 0:45496a70a8a5 239 bool LinkCheck;
mluis 0:45496a70a8a5 240 uint8_t DemodMargin;
mluis 0:45496a70a8a5 241 uint8_t NbGateways;
mluis 0:45496a70a8a5 242 }ComplianceTest;
mluis 0:45496a70a8a5 243
mluis 0:45496a70a8a5 244 /*
mluis 0:45496a70a8a5 245 * SerialDisplay managment variables
mluis 0:45496a70a8a5 246 */
mluis 0:45496a70a8a5 247
mluis 0:45496a70a8a5 248 /*!
mluis 0:45496a70a8a5 249 * Indicates if the MAC layer network join status has changed.
mluis 0:45496a70a8a5 250 */
mluis 0:45496a70a8a5 251 static bool IsNetworkJoinedStatusUpdate = false;
mluis 0:45496a70a8a5 252
mluis 0:45496a70a8a5 253 /*!
mluis 0:45496a70a8a5 254 * Strucure containing the Uplink status
mluis 0:45496a70a8a5 255 */
mluis 0:45496a70a8a5 256 struct sLoRaMacUplinkStatus
mluis 0:45496a70a8a5 257 {
mluis 0:45496a70a8a5 258 uint8_t Acked;
mluis 0:45496a70a8a5 259 int8_t Datarate;
mluis 0:45496a70a8a5 260 uint16_t UplinkCounter;
mluis 0:45496a70a8a5 261 uint8_t Port;
mluis 0:45496a70a8a5 262 uint8_t *Buffer;
mluis 0:45496a70a8a5 263 uint8_t BufferSize;
mluis 0:45496a70a8a5 264 }LoRaMacUplinkStatus;
mluis 0:45496a70a8a5 265 volatile bool UplinkStatusUpdated = false;
mluis 0:45496a70a8a5 266
mluis 0:45496a70a8a5 267 /*!
mluis 0:45496a70a8a5 268 * Strucure containing the Downlink status
mluis 0:45496a70a8a5 269 */
mluis 0:45496a70a8a5 270 struct sLoRaMacDownlinkStatus
mluis 0:45496a70a8a5 271 {
mluis 0:45496a70a8a5 272 int16_t Rssi;
mluis 0:45496a70a8a5 273 int8_t Snr;
mluis 0:45496a70a8a5 274 uint16_t DownlinkCounter;
mluis 0:45496a70a8a5 275 bool RxData;
mluis 0:45496a70a8a5 276 uint8_t Port;
mluis 0:45496a70a8a5 277 uint8_t *Buffer;
mluis 0:45496a70a8a5 278 uint8_t BufferSize;
mluis 0:45496a70a8a5 279 }LoRaMacDownlinkStatus;
mluis 0:45496a70a8a5 280 volatile bool DownlinkStatusUpdated = false;
mluis 0:45496a70a8a5 281
mluis 0:45496a70a8a5 282 void SerialDisplayRefresh( void )
mluis 0:45496a70a8a5 283 {
mluis 0:45496a70a8a5 284 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 285
mluis 0:45496a70a8a5 286 SerialDisplayInit( );
mluis 0:45496a70a8a5 287 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:45496a70a8a5 288
mluis 0:45496a70a8a5 289 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:45496a70a8a5 290 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 0:45496a70a8a5 291 SerialDisplayUpdateDevAddr( DevAddr );
mluis 0:45496a70a8a5 292 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:45496a70a8a5 293 SerialDisplayUpdateKey( 13, AppSKey );
mluis 0:45496a70a8a5 294 #else
mluis 0:45496a70a8a5 295 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:45496a70a8a5 296 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:45496a70a8a5 297 SerialDisplayUpdateKey( 7, AppKey );
mluis 0:45496a70a8a5 298 #endif
mluis 0:45496a70a8a5 299
mluis 0:45496a70a8a5 300 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 301 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 302 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:45496a70a8a5 303
mluis 0:45496a70a8a5 304 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 0:45496a70a8a5 305 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 306 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 0:45496a70a8a5 307 #else
mluis 0:45496a70a8a5 308 SerialDisplayUpdateDutyCycle( false );
mluis 0:45496a70a8a5 309 #endif
mluis 0:45496a70a8a5 310 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:45496a70a8a5 311
mluis 0:45496a70a8a5 312 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:45496a70a8a5 313 }
mluis 0:45496a70a8a5 314
mluis 0:45496a70a8a5 315 void SerialRxProcess( void )
mluis 0:45496a70a8a5 316 {
mluis 0:45496a70a8a5 317 if( SerialDisplayReadable( ) == true )
mluis 0:45496a70a8a5 318 {
mluis 0:45496a70a8a5 319 switch( SerialDisplayGetChar( ) )
mluis 0:45496a70a8a5 320 {
mluis 0:45496a70a8a5 321 case 'R':
mluis 0:45496a70a8a5 322 case 'r':
mluis 0:45496a70a8a5 323 // Refresh Serial screen
mluis 0:45496a70a8a5 324 SerialDisplayRefresh( );
mluis 0:45496a70a8a5 325 break;
mluis 0:45496a70a8a5 326 default:
mluis 0:45496a70a8a5 327 break;
mluis 0:45496a70a8a5 328 }
mluis 0:45496a70a8a5 329 }
mluis 0:45496a70a8a5 330 }
mluis 0:45496a70a8a5 331
mluis 0:45496a70a8a5 332 /*!
mluis 0:45496a70a8a5 333 * \brief Prepares the payload of the frame
mluis 0:45496a70a8a5 334 */
mluis 0:45496a70a8a5 335 static void PrepareTxFrame( uint8_t port )
mluis 0:45496a70a8a5 336 {
mluis 0:45496a70a8a5 337 switch( port )
mluis 0:45496a70a8a5 338 {
GregCr 2:48d8d4806d48 339 case 10:
GregCr 2:48d8d4806d48 340 {
mluis 5:fa113b25f612 341 uint32_t tempValue = ( uint32_t )( LightValue * 1000000.0 );
GregCr 3:d4142832d5af 342 AppData[0] = LightMode;
GregCr 3:d4142832d5af 343 AppData[1] = ( ( tempValue & 0xFF000000 ) >> 24 ) & 0xFF;
GregCr 3:d4142832d5af 344 AppData[2] = ( ( tempValue & 0x00FF0000 ) >> 16 ) & 0xFF;
GregCr 3:d4142832d5af 345 AppData[3] = ( ( tempValue & 0x0000FF00 ) >> 8 ) & 0xFF;
mluis 5:fa113b25f612 346 AppData[4] = ( tempValue & 0x000000FF );
GregCr 2:48d8d4806d48 347 }
GregCr 2:48d8d4806d48 348 break;
mluis 0:45496a70a8a5 349 case 15:
mluis 0:45496a70a8a5 350 {
mluis 0:45496a70a8a5 351 AppData[0] = AppLedStateOn;
mluis 0:45496a70a8a5 352 if( IsTxConfirmed == true )
mluis 0:45496a70a8a5 353 {
mluis 0:45496a70a8a5 354 AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8;
mluis 0:45496a70a8a5 355 AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter;
mluis 0:45496a70a8a5 356 AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8;
mluis 0:45496a70a8a5 357 AppData[4] = LoRaMacDownlinkStatus.Rssi;
mluis 0:45496a70a8a5 358 AppData[5] = LoRaMacDownlinkStatus.Snr;
mluis 0:45496a70a8a5 359 }
mluis 0:45496a70a8a5 360 }
mluis 0:45496a70a8a5 361 break;
mluis 0:45496a70a8a5 362 case 224:
mluis 0:45496a70a8a5 363 if( ComplianceTest.LinkCheck == true )
mluis 0:45496a70a8a5 364 {
mluis 0:45496a70a8a5 365 ComplianceTest.LinkCheck = false;
mluis 0:45496a70a8a5 366 AppDataSize = 3;
mluis 0:45496a70a8a5 367 AppData[0] = 5;
mluis 0:45496a70a8a5 368 AppData[1] = ComplianceTest.DemodMargin;
mluis 0:45496a70a8a5 369 AppData[2] = ComplianceTest.NbGateways;
mluis 0:45496a70a8a5 370 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 371 }
mluis 0:45496a70a8a5 372 else
mluis 0:45496a70a8a5 373 {
mluis 0:45496a70a8a5 374 switch( ComplianceTest.State )
mluis 0:45496a70a8a5 375 {
mluis 0:45496a70a8a5 376 case 4:
mluis 0:45496a70a8a5 377 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 378 break;
mluis 0:45496a70a8a5 379 case 1:
mluis 0:45496a70a8a5 380 AppDataSize = 2;
mluis 0:45496a70a8a5 381 AppData[0] = ComplianceTest.DownLinkCounter >> 8;
mluis 0:45496a70a8a5 382 AppData[1] = ComplianceTest.DownLinkCounter;
mluis 0:45496a70a8a5 383 break;
mluis 0:45496a70a8a5 384 }
mluis 0:45496a70a8a5 385 }
mluis 0:45496a70a8a5 386 break;
mluis 0:45496a70a8a5 387 default:
mluis 0:45496a70a8a5 388 break;
mluis 0:45496a70a8a5 389 }
mluis 0:45496a70a8a5 390 }
mluis 0:45496a70a8a5 391
mluis 0:45496a70a8a5 392 /*!
mluis 0:45496a70a8a5 393 * \brief Prepares the payload of the frame
mluis 0:45496a70a8a5 394 *
mluis 0:45496a70a8a5 395 * \retval [0: frame could be send, 1: error]
mluis 0:45496a70a8a5 396 */
mluis 0:45496a70a8a5 397 static bool SendFrame( void )
mluis 0:45496a70a8a5 398 {
mluis 0:45496a70a8a5 399 McpsReq_t mcpsReq;
mluis 0:45496a70a8a5 400 LoRaMacTxInfo_t txInfo;
mluis 0:45496a70a8a5 401
mluis 0:45496a70a8a5 402 if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 403 {
mluis 0:45496a70a8a5 404 // Send empty frame in order to flush MAC commands
mluis 0:45496a70a8a5 405 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 0:45496a70a8a5 406 mcpsReq.Req.Unconfirmed.fBuffer = NULL;
mluis 0:45496a70a8a5 407 mcpsReq.Req.Unconfirmed.fBufferSize = 0;
mluis 0:45496a70a8a5 408 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 5:fa113b25f612 409
mluis 0:45496a70a8a5 410 LoRaMacUplinkStatus.Acked = false;
mluis 0:45496a70a8a5 411 LoRaMacUplinkStatus.Port = 0;
mluis 0:45496a70a8a5 412 LoRaMacUplinkStatus.Buffer = NULL;
mluis 0:45496a70a8a5 413 LoRaMacUplinkStatus.BufferSize = 0;
mluis 0:45496a70a8a5 414 SerialDisplayUpdateFrameType( false );
mluis 0:45496a70a8a5 415 }
mluis 0:45496a70a8a5 416 else
mluis 0:45496a70a8a5 417 {
mluis 0:45496a70a8a5 418 LoRaMacUplinkStatus.Acked = false;
mluis 0:45496a70a8a5 419 LoRaMacUplinkStatus.Port = AppPort;
mluis 0:45496a70a8a5 420 LoRaMacUplinkStatus.Buffer = AppData;
mluis 0:45496a70a8a5 421 LoRaMacUplinkStatus.BufferSize = AppDataSize;
mluis 0:45496a70a8a5 422 SerialDisplayUpdateFrameType( IsTxConfirmed );
mluis 0:45496a70a8a5 423
mluis 0:45496a70a8a5 424 if( IsTxConfirmed == false )
mluis 0:45496a70a8a5 425 {
mluis 0:45496a70a8a5 426 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 0:45496a70a8a5 427 mcpsReq.Req.Unconfirmed.fPort = AppPort;
mluis 0:45496a70a8a5 428 mcpsReq.Req.Unconfirmed.fBuffer = AppData;
mluis 0:45496a70a8a5 429 mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
mluis 0:45496a70a8a5 430 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 0:45496a70a8a5 431 }
mluis 0:45496a70a8a5 432 else
mluis 0:45496a70a8a5 433 {
mluis 0:45496a70a8a5 434 mcpsReq.Type = MCPS_CONFIRMED;
mluis 0:45496a70a8a5 435 mcpsReq.Req.Confirmed.fPort = AppPort;
mluis 0:45496a70a8a5 436 mcpsReq.Req.Confirmed.fBuffer = AppData;
mluis 0:45496a70a8a5 437 mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
mluis 4:de084e23d884 438 mcpsReq.Req.Confirmed.NbTrials = 8;
mluis 0:45496a70a8a5 439 mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 0:45496a70a8a5 440 }
mluis 0:45496a70a8a5 441 }
mluis 0:45496a70a8a5 442
mluis 0:45496a70a8a5 443 if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 444 {
mluis 0:45496a70a8a5 445 return false;
mluis 0:45496a70a8a5 446 }
mluis 0:45496a70a8a5 447 return true;
mluis 0:45496a70a8a5 448 }
mluis 0:45496a70a8a5 449
mluis 0:45496a70a8a5 450 /*!
mluis 0:45496a70a8a5 451 * \brief Function executed on TxNextPacket Timeout event
mluis 0:45496a70a8a5 452 */
mluis 0:45496a70a8a5 453 static void OnTxNextPacketTimerEvent( void )
mluis 0:45496a70a8a5 454 {
mluis 0:45496a70a8a5 455 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 456 LoRaMacStatus_t status;
mluis 0:45496a70a8a5 457
mluis 0:45496a70a8a5 458 TimerStop( &TxNextPacketTimer );
mluis 0:45496a70a8a5 459
mluis 0:45496a70a8a5 460 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 461 status = LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 462
mluis 0:45496a70a8a5 463 if( status == LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 464 {
mluis 0:45496a70a8a5 465 if( mibReq.Param.IsNetworkJoined == true )
mluis 0:45496a70a8a5 466 {
mluis 0:45496a70a8a5 467 DeviceState = DEVICE_STATE_SEND;
mluis 0:45496a70a8a5 468 NextTx = true;
mluis 0:45496a70a8a5 469 }
mluis 0:45496a70a8a5 470 else
mluis 0:45496a70a8a5 471 {
mluis 0:45496a70a8a5 472 DeviceState = DEVICE_STATE_JOIN;
mluis 0:45496a70a8a5 473 }
mluis 0:45496a70a8a5 474 }
mluis 0:45496a70a8a5 475 }
mluis 0:45496a70a8a5 476
mluis 0:45496a70a8a5 477 /*!
mluis 0:45496a70a8a5 478 * \brief Function executed on Led 1 Timeout event
mluis 0:45496a70a8a5 479 */
mluis 0:45496a70a8a5 480 static void OnLed1TimerEvent( void )
mluis 0:45496a70a8a5 481 {
mluis 0:45496a70a8a5 482 TimerStop( &Led1Timer );
mluis 0:45496a70a8a5 483 // Switch LED 1 OFF
mluis 0:45496a70a8a5 484 Led1State = false;
mluis 0:45496a70a8a5 485 Led1StateChanged = true;
mluis 0:45496a70a8a5 486 }
mluis 0:45496a70a8a5 487
mluis 0:45496a70a8a5 488 /*!
mluis 0:45496a70a8a5 489 * \brief Function executed on Led 2 Timeout event
mluis 0:45496a70a8a5 490 */
mluis 0:45496a70a8a5 491 static void OnLed2TimerEvent( void )
mluis 0:45496a70a8a5 492 {
mluis 0:45496a70a8a5 493 TimerStop( &Led2Timer );
mluis 0:45496a70a8a5 494 // Switch LED 2 OFF
mluis 0:45496a70a8a5 495 Led2State = false;
mluis 0:45496a70a8a5 496 Led2StateChanged = true;
mluis 0:45496a70a8a5 497 }
mluis 0:45496a70a8a5 498
mluis 0:45496a70a8a5 499 /*!
GregCr 3:d4142832d5af 500 * \brief Function executed on Buzzer Timeout event
GregCr 3:d4142832d5af 501 */
mluis 5:fa113b25f612 502 static void OnBuzzerTimerEvent( void )
GregCr 3:d4142832d5af 503 {
mluis 5:fa113b25f612 504 Buzzer = 0;
mluis 5:fa113b25f612 505 BuzzerTimer.detach( );
GregCr 3:d4142832d5af 506 }
GregCr 3:d4142832d5af 507
GregCr 3:d4142832d5af 508 /*!
mluis 0:45496a70a8a5 509 * \brief MCPS-Confirm event function
mluis 0:45496a70a8a5 510 *
mluis 5:fa113b25f612 511 * \param [IN] mcpsConfirm - Pointer to the confirm structure,
mluis 0:45496a70a8a5 512 * containing confirm attributes.
mluis 0:45496a70a8a5 513 */
mluis 5:fa113b25f612 514 static void McpsConfirm( McpsConfirm_t *mcpsConfirm )
mluis 0:45496a70a8a5 515 {
mluis 5:fa113b25f612 516 if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 517 {
mluis 5:fa113b25f612 518 switch( mcpsConfirm->McpsRequest )
mluis 0:45496a70a8a5 519 {
mluis 0:45496a70a8a5 520 case MCPS_UNCONFIRMED:
mluis 0:45496a70a8a5 521 {
mluis 0:45496a70a8a5 522 // Check Datarate
mluis 0:45496a70a8a5 523 // Check TxPower
mluis 0:45496a70a8a5 524 break;
mluis 0:45496a70a8a5 525 }
mluis 0:45496a70a8a5 526 case MCPS_CONFIRMED:
mluis 0:45496a70a8a5 527 {
mluis 0:45496a70a8a5 528 // Check Datarate
mluis 0:45496a70a8a5 529 // Check TxPower
mluis 0:45496a70a8a5 530 // Check AckReceived
mluis 5:fa113b25f612 531 // Check NbTrials
mluis 5:fa113b25f612 532 LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived;
mluis 0:45496a70a8a5 533 break;
mluis 0:45496a70a8a5 534 }
mluis 0:45496a70a8a5 535 case MCPS_PROPRIETARY:
mluis 0:45496a70a8a5 536 {
mluis 0:45496a70a8a5 537 break;
mluis 0:45496a70a8a5 538 }
mluis 0:45496a70a8a5 539 default:
mluis 0:45496a70a8a5 540 break;
mluis 0:45496a70a8a5 541 }
mluis 5:fa113b25f612 542 LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate;
mluis 5:fa113b25f612 543 LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter;
mluis 5:fa113b25f612 544
mluis 0:45496a70a8a5 545 UplinkStatusUpdated = true;
mluis 0:45496a70a8a5 546 }
mluis 0:45496a70a8a5 547 NextTx = true;
mluis 0:45496a70a8a5 548 }
mluis 0:45496a70a8a5 549
mluis 0:45496a70a8a5 550 /*!
mluis 0:45496a70a8a5 551 * \brief MCPS-Indication event function
mluis 0:45496a70a8a5 552 *
mluis 5:fa113b25f612 553 * \param [IN] mcpsIndication - Pointer to the indication structure,
mluis 0:45496a70a8a5 554 * containing indication attributes.
mluis 0:45496a70a8a5 555 */
mluis 5:fa113b25f612 556 static void McpsIndication( McpsIndication_t *mcpsIndication )
mluis 0:45496a70a8a5 557 {
mluis 5:fa113b25f612 558 if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 559 {
mluis 0:45496a70a8a5 560 return;
mluis 0:45496a70a8a5 561 }
mluis 0:45496a70a8a5 562
mluis 5:fa113b25f612 563 switch( mcpsIndication->McpsIndication )
mluis 0:45496a70a8a5 564 {
mluis 0:45496a70a8a5 565 case MCPS_UNCONFIRMED:
mluis 0:45496a70a8a5 566 {
mluis 0:45496a70a8a5 567 break;
mluis 0:45496a70a8a5 568 }
mluis 0:45496a70a8a5 569 case MCPS_CONFIRMED:
mluis 0:45496a70a8a5 570 {
mluis 0:45496a70a8a5 571 break;
mluis 0:45496a70a8a5 572 }
mluis 0:45496a70a8a5 573 case MCPS_PROPRIETARY:
mluis 0:45496a70a8a5 574 {
mluis 0:45496a70a8a5 575 break;
mluis 0:45496a70a8a5 576 }
mluis 0:45496a70a8a5 577 case MCPS_MULTICAST:
mluis 0:45496a70a8a5 578 {
mluis 0:45496a70a8a5 579 break;
mluis 0:45496a70a8a5 580 }
mluis 0:45496a70a8a5 581 default:
mluis 0:45496a70a8a5 582 break;
mluis 0:45496a70a8a5 583 }
mluis 0:45496a70a8a5 584
mluis 0:45496a70a8a5 585 // Check Multicast
mluis 0:45496a70a8a5 586 // Check Port
mluis 0:45496a70a8a5 587 // Check Datarate
mluis 0:45496a70a8a5 588 // Check FramePending
mluis 0:45496a70a8a5 589 // Check Buffer
mluis 0:45496a70a8a5 590 // Check BufferSize
mluis 0:45496a70a8a5 591 // Check Rssi
mluis 0:45496a70a8a5 592 // Check Snr
mluis 0:45496a70a8a5 593 // Check RxSlot
mluis 5:fa113b25f612 594 LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi;
mluis 5:fa113b25f612 595 if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
mluis 0:45496a70a8a5 596 {
mluis 0:45496a70a8a5 597 // Invert and divide by 4
mluis 5:fa113b25f612 598 LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2;
mluis 0:45496a70a8a5 599 LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
mluis 0:45496a70a8a5 600 }
mluis 0:45496a70a8a5 601 else
mluis 0:45496a70a8a5 602 {
mluis 0:45496a70a8a5 603 // Divide by 4
mluis 5:fa113b25f612 604 LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2;
mluis 0:45496a70a8a5 605 }
mluis 0:45496a70a8a5 606 LoRaMacDownlinkStatus.DownlinkCounter++;
mluis 5:fa113b25f612 607 LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData;
mluis 5:fa113b25f612 608 LoRaMacDownlinkStatus.Port = mcpsIndication->Port;
mluis 5:fa113b25f612 609 LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer;
mluis 5:fa113b25f612 610 LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize;
mluis 0:45496a70a8a5 611
mluis 0:45496a70a8a5 612 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 613 {
mluis 0:45496a70a8a5 614 ComplianceTest.DownLinkCounter++;
mluis 0:45496a70a8a5 615 }
mluis 0:45496a70a8a5 616
mluis 5:fa113b25f612 617 if( mcpsIndication->RxData == true )
mluis 0:45496a70a8a5 618 {
mluis 5:fa113b25f612 619 switch( mcpsIndication->Port )
mluis 0:45496a70a8a5 620 {
mluis 0:45496a70a8a5 621 case 1: // The application LED can be controlled on port 1 or 2
mluis 0:45496a70a8a5 622 case 2:
mluis 5:fa113b25f612 623 if( mcpsIndication->BufferSize == 1 )
mluis 0:45496a70a8a5 624 {
mluis 5:fa113b25f612 625 AppLedStateOn = mcpsIndication->Buffer[0] & 0x01;
mluis 0:45496a70a8a5 626 Led3StateChanged = true;
mluis 0:45496a70a8a5 627 }
mluis 0:45496a70a8a5 628 break;
GregCr 2:48d8d4806d48 629 case 10:
mluis 5:fa113b25f612 630 Display.write( 0, mcpsIndication->Buffer[0] );
mluis 5:fa113b25f612 631 Display.write( 1, mcpsIndication->Buffer[1] );
mluis 5:fa113b25f612 632 Display.write( 2, mcpsIndication->Buffer[2] );
mluis 5:fa113b25f612 633 Display.write( 3, mcpsIndication->Buffer[3] );
GregCr 2:48d8d4806d48 634 break;
GregCr 2:48d8d4806d48 635 case 20:
mluis 5:fa113b25f612 636 LightMode = mcpsIndication->Buffer[0];
GregCr 2:48d8d4806d48 637 if( LightMode )
GregCr 2:48d8d4806d48 638 {
mluis 5:fa113b25f612 639 ColorLed.setColorRGB( 0, mcpsIndication->Buffer[1], mcpsIndication->Buffer[2], mcpsIndication->Buffer[3] );
GregCr 2:48d8d4806d48 640 }
GregCr 2:48d8d4806d48 641 break;
GregCr 2:48d8d4806d48 642 case 30:
mluis 5:fa113b25f612 643 BuzzerTimer.attach_us( &OnBuzzerTimerEvent, 200000 );
mluis 5:fa113b25f612 644 Buzzer = 1;
GregCr 2:48d8d4806d48 645 break;
mluis 0:45496a70a8a5 646 case 224:
mluis 0:45496a70a8a5 647 if( ComplianceTest.Running == false )
mluis 0:45496a70a8a5 648 {
mluis 0:45496a70a8a5 649 // Check compliance test enable command (i)
mluis 5:fa113b25f612 650 if( ( mcpsIndication->BufferSize == 4 ) &&
mluis 5:fa113b25f612 651 ( mcpsIndication->Buffer[0] == 0x01 ) &&
mluis 5:fa113b25f612 652 ( mcpsIndication->Buffer[1] == 0x01 ) &&
mluis 5:fa113b25f612 653 ( mcpsIndication->Buffer[2] == 0x01 ) &&
mluis 5:fa113b25f612 654 ( mcpsIndication->Buffer[3] == 0x01 ) )
mluis 0:45496a70a8a5 655 {
mluis 0:45496a70a8a5 656 IsTxConfirmed = false;
mluis 0:45496a70a8a5 657 AppPort = 224;
mluis 0:45496a70a8a5 658 AppDataSize = 2;
mluis 0:45496a70a8a5 659 ComplianceTest.DownLinkCounter = 0;
mluis 0:45496a70a8a5 660 ComplianceTest.LinkCheck = false;
mluis 0:45496a70a8a5 661 ComplianceTest.DemodMargin = 0;
mluis 0:45496a70a8a5 662 ComplianceTest.NbGateways = 0;
mluis 0:45496a70a8a5 663 ComplianceTest.Running = true;
mluis 0:45496a70a8a5 664 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 665
mluis 0:45496a70a8a5 666 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 667 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 668 mibReq.Param.AdrEnable = true;
mluis 0:45496a70a8a5 669 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 670
mluis 0:45496a70a8a5 671 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 672 LoRaMacTestSetDutyCycleOn( false );
mluis 0:45496a70a8a5 673 #endif
mluis 0:45496a70a8a5 674 }
mluis 0:45496a70a8a5 675 }
mluis 0:45496a70a8a5 676 else
mluis 0:45496a70a8a5 677 {
mluis 5:fa113b25f612 678 ComplianceTest.State = mcpsIndication->Buffer[0];
mluis 0:45496a70a8a5 679 switch( ComplianceTest.State )
mluis 0:45496a70a8a5 680 {
mluis 0:45496a70a8a5 681 case 0: // Check compliance test disable command (ii)
mluis 0:45496a70a8a5 682 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:45496a70a8a5 683 AppPort = LORAWAN_APP_PORT;
mluis 0:45496a70a8a5 684 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 0:45496a70a8a5 685 ComplianceTest.DownLinkCounter = 0;
mluis 0:45496a70a8a5 686 ComplianceTest.Running = false;
mluis 0:45496a70a8a5 687
mluis 0:45496a70a8a5 688 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 689 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 690 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 0:45496a70a8a5 691 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 692 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 693 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 0:45496a70a8a5 694 #endif
mluis 0:45496a70a8a5 695 break;
mluis 0:45496a70a8a5 696 case 1: // (iii, iv)
mluis 0:45496a70a8a5 697 AppDataSize = 2;
mluis 0:45496a70a8a5 698 break;
mluis 0:45496a70a8a5 699 case 2: // Enable confirmed messages (v)
mluis 0:45496a70a8a5 700 IsTxConfirmed = true;
mluis 0:45496a70a8a5 701 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 702 break;
mluis 0:45496a70a8a5 703 case 3: // Disable confirmed messages (vi)
mluis 0:45496a70a8a5 704 IsTxConfirmed = false;
mluis 0:45496a70a8a5 705 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 706 break;
mluis 0:45496a70a8a5 707 case 4: // (vii)
mluis 5:fa113b25f612 708 AppDataSize = mcpsIndication->BufferSize;
mluis 0:45496a70a8a5 709
mluis 0:45496a70a8a5 710 AppData[0] = 4;
mluis 0:45496a70a8a5 711 for( uint8_t i = 1; i < AppDataSize; i++ )
mluis 0:45496a70a8a5 712 {
mluis 5:fa113b25f612 713 AppData[i] = mcpsIndication->Buffer[i] + 1;
mluis 0:45496a70a8a5 714 }
mluis 0:45496a70a8a5 715 break;
mluis 0:45496a70a8a5 716 case 5: // (viii)
mluis 0:45496a70a8a5 717 {
mluis 0:45496a70a8a5 718 MlmeReq_t mlmeReq;
mluis 0:45496a70a8a5 719 mlmeReq.Type = MLME_LINK_CHECK;
mluis 0:45496a70a8a5 720 LoRaMacMlmeRequest( &mlmeReq );
mluis 0:45496a70a8a5 721 }
mluis 0:45496a70a8a5 722 break;
mluis 0:45496a70a8a5 723 default:
mluis 0:45496a70a8a5 724 break;
mluis 0:45496a70a8a5 725 }
mluis 0:45496a70a8a5 726 }
mluis 0:45496a70a8a5 727 break;
mluis 0:45496a70a8a5 728 default:
mluis 0:45496a70a8a5 729 break;
mluis 0:45496a70a8a5 730 }
mluis 0:45496a70a8a5 731 }
mluis 0:45496a70a8a5 732
mluis 0:45496a70a8a5 733 // Switch LED 2 ON for each received downlink
mluis 0:45496a70a8a5 734 Led2State = true;
mluis 0:45496a70a8a5 735 Led2StateChanged = true;
mluis 0:45496a70a8a5 736 TimerStart( &Led2Timer );
mluis 0:45496a70a8a5 737 DownlinkStatusUpdated = true;
mluis 0:45496a70a8a5 738 }
mluis 0:45496a70a8a5 739
mluis 0:45496a70a8a5 740 /*!
mluis 0:45496a70a8a5 741 * \brief MLME-Confirm event function
mluis 0:45496a70a8a5 742 *
mluis 5:fa113b25f612 743 * \param [IN] mlmeConfirm - Pointer to the confirm structure,
mluis 0:45496a70a8a5 744 * containing confirm attributes.
mluis 0:45496a70a8a5 745 */
mluis 5:fa113b25f612 746 static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
mluis 0:45496a70a8a5 747 {
mluis 5:fa113b25f612 748 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 749 {
mluis 5:fa113b25f612 750 switch( mlmeConfirm->MlmeRequest )
mluis 0:45496a70a8a5 751 {
mluis 0:45496a70a8a5 752 case MLME_JOIN:
mluis 0:45496a70a8a5 753 {
mluis 0:45496a70a8a5 754 // Status is OK, node has joined the network
mluis 0:45496a70a8a5 755 IsNetworkJoinedStatusUpdate = true;
mluis 0:45496a70a8a5 756 break;
mluis 0:45496a70a8a5 757 }
mluis 0:45496a70a8a5 758 case MLME_LINK_CHECK:
mluis 0:45496a70a8a5 759 {
mluis 0:45496a70a8a5 760 // Check DemodMargin
mluis 0:45496a70a8a5 761 // Check NbGateways
mluis 0:45496a70a8a5 762 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 763 {
mluis 0:45496a70a8a5 764 ComplianceTest.LinkCheck = true;
mluis 5:fa113b25f612 765 ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin;
mluis 5:fa113b25f612 766 ComplianceTest.NbGateways = mlmeConfirm->NbGateways;
mluis 0:45496a70a8a5 767 }
mluis 0:45496a70a8a5 768 break;
mluis 0:45496a70a8a5 769 }
mluis 0:45496a70a8a5 770 default:
mluis 0:45496a70a8a5 771 break;
mluis 0:45496a70a8a5 772 }
mluis 0:45496a70a8a5 773 }
mluis 0:45496a70a8a5 774 NextTx = true;
mluis 0:45496a70a8a5 775 UplinkStatusUpdated = true;
mluis 0:45496a70a8a5 776 }
mluis 0:45496a70a8a5 777
mluis 0:45496a70a8a5 778 /**
mluis 0:45496a70a8a5 779 * Main application entry point.
mluis 0:45496a70a8a5 780 */
mluis 0:45496a70a8a5 781 int main( void )
mluis 0:45496a70a8a5 782 {
mluis 0:45496a70a8a5 783 LoRaMacPrimitives_t LoRaMacPrimitives;
mluis 0:45496a70a8a5 784 LoRaMacCallback_t LoRaMacCallbacks;
mluis 0:45496a70a8a5 785 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 786
mluis 0:45496a70a8a5 787 BoardInit( );
mluis 0:45496a70a8a5 788 SerialDisplayInit( );
mluis 0:45496a70a8a5 789
mluis 5:fa113b25f612 790 LightMode = 0; // 0: manual, 1: automatic
mluis 5:fa113b25f612 791 Buzzer = 0; // 0: OFF, 1: ON
mluis 5:fa113b25f612 792
mluis 0:45496a70a8a5 793 DeviceState = DEVICE_STATE_INIT;
mluis 0:45496a70a8a5 794
mluis 0:45496a70a8a5 795 while( 1 )
mluis 0:45496a70a8a5 796 {
mluis 0:45496a70a8a5 797 SerialRxProcess( );
mluis 0:45496a70a8a5 798 if( IsNetworkJoinedStatusUpdate == true )
mluis 0:45496a70a8a5 799 {
mluis 0:45496a70a8a5 800 IsNetworkJoinedStatusUpdate = false;
mluis 0:45496a70a8a5 801 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 802 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 803 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:45496a70a8a5 804 }
mluis 0:45496a70a8a5 805 if( Led1StateChanged == true )
mluis 0:45496a70a8a5 806 {
mluis 0:45496a70a8a5 807 Led1StateChanged = false;
mluis 0:45496a70a8a5 808 SerialDisplayUpdateLedState( 1, Led1State );
mluis 0:45496a70a8a5 809 }
mluis 0:45496a70a8a5 810 if( Led2StateChanged == true )
mluis 0:45496a70a8a5 811 {
mluis 0:45496a70a8a5 812 Led2StateChanged = false;
mluis 0:45496a70a8a5 813 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:45496a70a8a5 814 }
mluis 0:45496a70a8a5 815 if( Led3StateChanged == true )
mluis 0:45496a70a8a5 816 {
mluis 0:45496a70a8a5 817 Led3StateChanged = false;
mluis 0:45496a70a8a5 818 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:45496a70a8a5 819 }
mluis 0:45496a70a8a5 820 if( UplinkStatusUpdated == true )
mluis 0:45496a70a8a5 821 {
mluis 0:45496a70a8a5 822 UplinkStatusUpdated = false;
mluis 0:45496a70a8a5 823 SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize );
mluis 0:45496a70a8a5 824 }
mluis 0:45496a70a8a5 825 if( DownlinkStatusUpdated == true )
mluis 0:45496a70a8a5 826 {
mluis 0:45496a70a8a5 827 DownlinkStatusUpdated = false;
mluis 0:45496a70a8a5 828 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:45496a70a8a5 829 SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
mluis 0:45496a70a8a5 830 }
mluis 0:45496a70a8a5 831
mluis 0:45496a70a8a5 832 switch( DeviceState )
mluis 0:45496a70a8a5 833 {
mluis 0:45496a70a8a5 834 case DEVICE_STATE_INIT:
mluis 0:45496a70a8a5 835 {
mluis 0:45496a70a8a5 836 LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
mluis 0:45496a70a8a5 837 LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
mluis 0:45496a70a8a5 838 LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
mluis 0:45496a70a8a5 839 LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
mluis 0:45496a70a8a5 840 LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
mluis 0:45496a70a8a5 841
mluis 0:45496a70a8a5 842 TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
mluis 0:45496a70a8a5 843
mluis 0:45496a70a8a5 844 TimerInit( &Led1Timer, OnLed1TimerEvent );
mluis 0:45496a70a8a5 845 TimerSetValue( &Led1Timer, 25000 );
mluis 0:45496a70a8a5 846
mluis 0:45496a70a8a5 847 TimerInit( &Led2Timer, OnLed2TimerEvent );
mluis 0:45496a70a8a5 848 TimerSetValue( &Led2Timer, 25000 );
mluis 0:45496a70a8a5 849
mluis 0:45496a70a8a5 850 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 851 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 0:45496a70a8a5 852 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 853
mluis 0:45496a70a8a5 854 mibReq.Type = MIB_PUBLIC_NETWORK;
mluis 0:45496a70a8a5 855 mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
mluis 0:45496a70a8a5 856 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 857
mluis 0:45496a70a8a5 858 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 859 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 0:45496a70a8a5 860 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 5:fa113b25f612 861
mluis 5:fa113b25f612 862 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 5:fa113b25f612 863 LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
mluis 5:fa113b25f612 864 LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
mluis 5:fa113b25f612 865 LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
mluis 5:fa113b25f612 866 LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 );
mluis 5:fa113b25f612 867 LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 );
mluis 5:fa113b25f612 868 LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
mluis 5:fa113b25f612 869 LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
mluis 5:fa113b25f612 870 #endif
mluis 5:fa113b25f612 871
mluis 0:45496a70a8a5 872 #endif
mluis 0:45496a70a8a5 873 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:45496a70a8a5 874 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 0:45496a70a8a5 875 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:45496a70a8a5 876
mluis 0:45496a70a8a5 877 LoRaMacDownlinkStatus.DownlinkCounter = 0;
mluis 0:45496a70a8a5 878
mluis 0:45496a70a8a5 879 DeviceState = DEVICE_STATE_JOIN;
mluis 0:45496a70a8a5 880 break;
mluis 0:45496a70a8a5 881 }
mluis 0:45496a70a8a5 882 case DEVICE_STATE_JOIN:
mluis 0:45496a70a8a5 883 {
mluis 0:45496a70a8a5 884 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 0:45496a70a8a5 885 MlmeReq_t mlmeReq;
mluis 0:45496a70a8a5 886
mluis 0:45496a70a8a5 887 mlmeReq.Type = MLME_JOIN;
mluis 0:45496a70a8a5 888
mluis 0:45496a70a8a5 889 mlmeReq.Req.Join.DevEui = DevEui;
mluis 0:45496a70a8a5 890 mlmeReq.Req.Join.AppEui = AppEui;
mluis 0:45496a70a8a5 891 mlmeReq.Req.Join.AppKey = AppKey;
mluis 0:45496a70a8a5 892
mluis 0:45496a70a8a5 893 if( NextTx == true )
mluis 0:45496a70a8a5 894 {
mluis 0:45496a70a8a5 895 LoRaMacMlmeRequest( &mlmeReq );
mluis 0:45496a70a8a5 896 }
mluis 0:45496a70a8a5 897
mluis 0:45496a70a8a5 898 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:45496a70a8a5 899 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:45496a70a8a5 900 SerialDisplayUpdateKey( 7, AppKey );
mluis 0:45496a70a8a5 901
mluis 0:45496a70a8a5 902 // Schedule next packet transmission
mluis 0:45496a70a8a5 903 TxDutyCycleTime = OVER_THE_AIR_ACTIVATION_DUTYCYCLE;
mluis 0:45496a70a8a5 904 DeviceState = DEVICE_STATE_CYCLE;
mluis 0:45496a70a8a5 905
mluis 0:45496a70a8a5 906 #else
mluis 0:45496a70a8a5 907 mibReq.Type = MIB_NET_ID;
mluis 0:45496a70a8a5 908 mibReq.Param.NetID = LORAWAN_NETWORK_ID;
mluis 0:45496a70a8a5 909 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 910
mluis 0:45496a70a8a5 911 mibReq.Type = MIB_DEV_ADDR;
mluis 0:45496a70a8a5 912 mibReq.Param.DevAddr = DevAddr;
mluis 0:45496a70a8a5 913 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 914
mluis 0:45496a70a8a5 915 mibReq.Type = MIB_NWK_SKEY;
mluis 0:45496a70a8a5 916 mibReq.Param.NwkSKey = NwkSKey;
mluis 0:45496a70a8a5 917 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 918
mluis 0:45496a70a8a5 919 mibReq.Type = MIB_APP_SKEY;
mluis 0:45496a70a8a5 920 mibReq.Param.AppSKey = AppSKey;
mluis 0:45496a70a8a5 921 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 922
mluis 0:45496a70a8a5 923 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 924 mibReq.Param.IsNetworkJoined = true;
mluis 0:45496a70a8a5 925 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 926
mluis 0:45496a70a8a5 927 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 0:45496a70a8a5 928 SerialDisplayUpdateDevAddr( DevAddr );
mluis 0:45496a70a8a5 929 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:45496a70a8a5 930 SerialDisplayUpdateKey( 13, AppSKey );
mluis 0:45496a70a8a5 931
mluis 0:45496a70a8a5 932 DeviceState = DEVICE_STATE_SEND;
mluis 0:45496a70a8a5 933 #endif
mluis 0:45496a70a8a5 934 IsNetworkJoinedStatusUpdate = true;
mluis 0:45496a70a8a5 935 break;
mluis 0:45496a70a8a5 936 }
mluis 0:45496a70a8a5 937 case DEVICE_STATE_SEND:
mluis 0:45496a70a8a5 938 {
mluis 0:45496a70a8a5 939 if( NextTx == true )
mluis 0:45496a70a8a5 940 {
mluis 0:45496a70a8a5 941 SerialDisplayUpdateUplinkAcked( false );
mluis 0:45496a70a8a5 942 SerialDisplayUpdateDonwlinkRxData( false );
mluis 0:45496a70a8a5 943 PrepareTxFrame( AppPort );
mluis 0:45496a70a8a5 944
mluis 0:45496a70a8a5 945 NextTx = SendFrame( );
mluis 0:45496a70a8a5 946
mluis 0:45496a70a8a5 947 // Switch LED 1 ON
mluis 0:45496a70a8a5 948 Led1State = true;
mluis 0:45496a70a8a5 949 Led1StateChanged = true;
mluis 0:45496a70a8a5 950 TimerStart( &Led1Timer );
mluis 0:45496a70a8a5 951 }
mluis 0:45496a70a8a5 952 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 953 {
mluis 5:fa113b25f612 954 // Schedule next packet transmission
mluis 5:fa113b25f612 955 TxDutyCycleTime = 5000000; // 5000000 us
mluis 0:45496a70a8a5 956 }
mluis 0:45496a70a8a5 957 else
mluis 0:45496a70a8a5 958 {
mluis 0:45496a70a8a5 959 // Schedule next packet transmission
mluis 0:45496a70a8a5 960 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
mluis 0:45496a70a8a5 961 }
mluis 0:45496a70a8a5 962 DeviceState = DEVICE_STATE_CYCLE;
mluis 0:45496a70a8a5 963 break;
mluis 0:45496a70a8a5 964 }
mluis 0:45496a70a8a5 965 case DEVICE_STATE_CYCLE:
mluis 0:45496a70a8a5 966 {
mluis 5:fa113b25f612 967 DeviceState = DEVICE_STATE_SLEEP;
mluis 5:fa113b25f612 968
mluis 0:45496a70a8a5 969 // Schedule next packet transmission
mluis 0:45496a70a8a5 970 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
mluis 0:45496a70a8a5 971 TimerStart( &TxNextPacketTimer );
mluis 0:45496a70a8a5 972 break;
mluis 0:45496a70a8a5 973 }
mluis 0:45496a70a8a5 974 case DEVICE_STATE_SLEEP:
mluis 0:45496a70a8a5 975 {
mluis 0:45496a70a8a5 976 // Wake up through events
mluis 0:45496a70a8a5 977 break;
mluis 0:45496a70a8a5 978 }
mluis 0:45496a70a8a5 979 default:
mluis 0:45496a70a8a5 980 {
mluis 0:45496a70a8a5 981 DeviceState = DEVICE_STATE_INIT;
mluis 0:45496a70a8a5 982 break;
mluis 0:45496a70a8a5 983 }
GregCr 2:48d8d4806d48 984
GregCr 2:48d8d4806d48 985 }
mluis 5:fa113b25f612 986
GregCr 2:48d8d4806d48 987 // Read light sensor
mluis 5:fa113b25f612 988 LightValue = ( 1 - ( LightSens.read( ) * 1.65 ) );
mluis 5:fa113b25f612 989
GregCr 2:48d8d4806d48 990 // Set automatic RGB from light sensor
GregCr 2:48d8d4806d48 991 if( LightMode == 0 )
GregCr 2:48d8d4806d48 992 {
mluis 5:fa113b25f612 993 ColorLed.setColorRGB( 0, ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ) );
mluis 0:45496a70a8a5 994 }
mluis 0:45496a70a8a5 995 }
mluis 0:45496a70a8a5 996 }