LoRaWAN VIT

Dependencies:   mbed DigitDisplay Chainable_RGB_LED LoRaWAN-lib SX1276Lib

Committer:
balarayar
Date:
Fri Nov 29 12:35:54 2019 +0000
Revision:
4:cd9a19f40d47
Parent:
3:042ccdf1dd92
VIT LoRaWAN

Who changed what in which revision?

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