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 SX1272Lib

Fork of LoRaWAN-demo-72 by Semtech

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

Committer:
mluis
Date:
Tue Jul 05 15:01:10 2016 +0000
Revision:
7:ceb4063e6863
Parent:
5:fa113b25f612
Child:
9:0083afd69815
Synchronized example application with GitHub LoRaMac-node examples; Updated mbed, LoRaWAN-lib and SX1272Lib libraries

Who changed what in which revision?

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