Bootcamp application example using LoRaWAN-lib MAC layer implementation. Provides an application example controlling a 3 color LED and a light sensor.

Dependencies:   mbed Chainable_RGB_LED DigitDisplay LoRaWAN-lib SX1276Lib

Dependents:   LoRaWAN

For a detailed description of the LoRaWAN operations, please visit the MBED dedicated page at https://developer.mbed.org/teams/Semtech/code/LoRaWAN-demo-76/

Committer:
mluis
Date:
Fri May 13 15:52:23 2016 +0000
Revision:
1:21e3eef8200f
Parent:
0:cb80564f40e1
Child:
3:de1dcfbe175a
Updated mbed, LoRaWAN-lib and SX1276Lib libraries.

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