Application example using LoRaWAN-lib MAC layer implementation for STM32 + SX1276 on OpenChirp

Dependencies:   mbed LoRaWAN-lib SX1276Lib

Committer:
nimita23
Date:
Fri Jun 28 20:18:15 2019 +0000
Revision:
12:bf1efd688cc8
Parent:
11:a2c88830d3bf
Child:
13:779fef3e4573
upling and downlink working. TODO add led functionality

Who changed what in which revision?

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