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

Dependencies:   mbed LoRaWAN-lib SX1276Lib

Committer:
nimita23
Date:
Thu Jun 27 18:58:43 2019 +0000
Revision:
11:a2c88830d3bf
Parent:
10:715b2204399b
Child:
12:bf1efd688cc8
downlink issue fixed

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 10:715b2204399b 293 // uint32_t tempValue = ( uint32_t )( 12345678 );
nimita23 10:715b2204399b 294 AppData[0] = 0;
nimita23 10:715b2204399b 295 AppData[1] = 0x12;
nimita23 10:715b2204399b 296 AppData[2] = 0x34;
nimita23 10:715b2204399b 297 AppData[3] = 0x56;
nimita23 10:715b2204399b 298 AppData[4] = 0x78;
nimita23 10:715b2204399b 299 }
nimita23 10:715b2204399b 300 break;
mluis 1:352f608c3337 301 case 15:
mluis 1:352f608c3337 302 {
mluis 1:352f608c3337 303 AppData[0] = AppLedStateOn;
mluis 1:352f608c3337 304 if( IsTxConfirmed == true )
mluis 1:352f608c3337 305 {
mluis 1:352f608c3337 306 AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8;
mluis 1:352f608c3337 307 AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter;
mluis 1:352f608c3337 308 AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8;
mluis 1:352f608c3337 309 AppData[4] = LoRaMacDownlinkStatus.Rssi;
mluis 1:352f608c3337 310 AppData[5] = LoRaMacDownlinkStatus.Snr;
mluis 1:352f608c3337 311 }
mluis 1:352f608c3337 312 }
mluis 1:352f608c3337 313 break;
mluis 1:352f608c3337 314 case 224:
mluis 3:9c6f7f082151 315 if( ComplianceTest.LinkCheck == true )
mluis 1:352f608c3337 316 {
mluis 3:9c6f7f082151 317 ComplianceTest.LinkCheck = false;
mluis 1:352f608c3337 318 AppDataSize = 3;
mluis 1:352f608c3337 319 AppData[0] = 5;
mluis 3:9c6f7f082151 320 AppData[1] = ComplianceTest.DemodMargin;
mluis 3:9c6f7f082151 321 AppData[2] = ComplianceTest.NbGateways;
mluis 3:9c6f7f082151 322 ComplianceTest.State = 1;
mluis 1:352f608c3337 323 }
mluis 1:352f608c3337 324 else
mluis 1:352f608c3337 325 {
mluis 3:9c6f7f082151 326 switch( ComplianceTest.State )
mluis 1:352f608c3337 327 {
mluis 1:352f608c3337 328 case 4:
mluis 3:9c6f7f082151 329 ComplianceTest.State = 1;
mluis 1:352f608c3337 330 break;
mluis 1:352f608c3337 331 case 1:
mluis 1:352f608c3337 332 AppDataSize = 2;
mluis 3:9c6f7f082151 333 AppData[0] = ComplianceTest.DownLinkCounter >> 8;
mluis 3:9c6f7f082151 334 AppData[1] = ComplianceTest.DownLinkCounter;
mluis 1:352f608c3337 335 break;
mluis 1:352f608c3337 336 }
mluis 1:352f608c3337 337 }
mluis 1:352f608c3337 338 break;
mluis 3:9c6f7f082151 339 default:
mluis 3:9c6f7f082151 340 break;
mluis 1:352f608c3337 341 }
mluis 0:92bca02df485 342 }
mluis 0:92bca02df485 343
mluis 3:9c6f7f082151 344 /*!
mluis 3:9c6f7f082151 345 * \brief Prepares the payload of the frame
mluis 3:9c6f7f082151 346 *
mluis 3:9c6f7f082151 347 * \retval [0: frame could be send, 1: error]
mluis 3:9c6f7f082151 348 */
mluis 0:92bca02df485 349 static bool SendFrame( void )
mluis 0:92bca02df485 350 {
mluis 3:9c6f7f082151 351 McpsReq_t mcpsReq;
mluis 3:9c6f7f082151 352 LoRaMacTxInfo_t txInfo;
mluis 9:ee9dcbb9708d 353
mluis 3:9c6f7f082151 354 if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
mluis 1:352f608c3337 355 {
mluis 3:9c6f7f082151 356 // Send empty frame in order to flush MAC commands
mluis 3:9c6f7f082151 357 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 3:9c6f7f082151 358 mcpsReq.Req.Unconfirmed.fBuffer = NULL;
mluis 3:9c6f7f082151 359 mcpsReq.Req.Unconfirmed.fBufferSize = 0;
mluis 3:9c6f7f082151 360 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 5:1e9f6a365854 361
mluis 3:9c6f7f082151 362 LoRaMacUplinkStatus.Acked = false;
mluis 3:9c6f7f082151 363 LoRaMacUplinkStatus.Port = 0;
mluis 3:9c6f7f082151 364 LoRaMacUplinkStatus.Buffer = NULL;
mluis 3:9c6f7f082151 365 LoRaMacUplinkStatus.BufferSize = 0;
mluis 3:9c6f7f082151 366 SerialDisplayUpdateFrameType( false );
mluis 1:352f608c3337 367 }
mluis 1:352f608c3337 368 else
mluis 1:352f608c3337 369 {
mluis 3:9c6f7f082151 370 LoRaMacUplinkStatus.Acked = false;
mluis 3:9c6f7f082151 371 LoRaMacUplinkStatus.Port = AppPort;
mluis 3:9c6f7f082151 372 LoRaMacUplinkStatus.Buffer = AppData;
mluis 3:9c6f7f082151 373 LoRaMacUplinkStatus.BufferSize = AppDataSize;
mluis 3:9c6f7f082151 374 SerialDisplayUpdateFrameType( IsTxConfirmed );
mluis 3:9c6f7f082151 375
mluis 3:9c6f7f082151 376 if( IsTxConfirmed == false )
mluis 3:9c6f7f082151 377 {
mluis 3:9c6f7f082151 378 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 3:9c6f7f082151 379 mcpsReq.Req.Unconfirmed.fPort = AppPort;
mluis 3:9c6f7f082151 380 mcpsReq.Req.Unconfirmed.fBuffer = AppData;
mluis 3:9c6f7f082151 381 mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
mluis 3:9c6f7f082151 382 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 3:9c6f7f082151 383 }
mluis 3:9c6f7f082151 384 else
mluis 3:9c6f7f082151 385 {
mluis 3:9c6f7f082151 386 mcpsReq.Type = MCPS_CONFIRMED;
mluis 3:9c6f7f082151 387 mcpsReq.Req.Confirmed.fPort = AppPort;
mluis 3:9c6f7f082151 388 mcpsReq.Req.Confirmed.fBuffer = AppData;
mluis 3:9c6f7f082151 389 mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
mluis 4:00cf2370c99d 390 mcpsReq.Req.Confirmed.NbTrials = 8;
mluis 3:9c6f7f082151 391 mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 3:9c6f7f082151 392 }
mluis 1:352f608c3337 393 }
mluis 1:352f608c3337 394
mluis 3:9c6f7f082151 395 if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK )
mluis 0:92bca02df485 396 {
mluis 0:92bca02df485 397 return false;
mluis 0:92bca02df485 398 }
mluis 3:9c6f7f082151 399 return true;
mluis 0:92bca02df485 400 }
mluis 0:92bca02df485 401
mluis 0:92bca02df485 402 /*!
mluis 0:92bca02df485 403 * \brief Function executed on TxNextPacket Timeout event
mluis 0:92bca02df485 404 */
mluis 0:92bca02df485 405 static void OnTxNextPacketTimerEvent( void )
mluis 0:92bca02df485 406 {
mluis 3:9c6f7f082151 407 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 408 LoRaMacStatus_t status;
mluis 3:9c6f7f082151 409
mluis 0:92bca02df485 410 TimerStop( &TxNextPacketTimer );
mluis 3:9c6f7f082151 411
mluis 3:9c6f7f082151 412 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 413 status = LoRaMacMibGetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 414
mluis 3:9c6f7f082151 415 if( status == LORAMAC_STATUS_OK )
mluis 3:9c6f7f082151 416 {
mluis 3:9c6f7f082151 417 if( mibReq.Param.IsNetworkJoined == true )
mluis 3:9c6f7f082151 418 {
mluis 3:9c6f7f082151 419 DeviceState = DEVICE_STATE_SEND;
mluis 3:9c6f7f082151 420 NextTx = true;
mluis 3:9c6f7f082151 421 }
mluis 3:9c6f7f082151 422 else
mluis 3:9c6f7f082151 423 {
mluis 3:9c6f7f082151 424 DeviceState = DEVICE_STATE_JOIN;
mluis 3:9c6f7f082151 425 }
mluis 3:9c6f7f082151 426 }
mluis 0:92bca02df485 427 }
mluis 0:92bca02df485 428
mluis 0:92bca02df485 429 /*!
mluis 0:92bca02df485 430 * \brief Function executed on Led 1 Timeout event
mluis 0:92bca02df485 431 */
mluis 0:92bca02df485 432 static void OnLed1TimerEvent( void )
mluis 0:92bca02df485 433 {
mluis 0:92bca02df485 434 TimerStop( &Led1Timer );
mluis 3:9c6f7f082151 435 // Switch LED 1 OFF
mluis 1:352f608c3337 436 Led1State = false;
mluis 0:92bca02df485 437 Led1StateChanged = true;
mluis 0:92bca02df485 438 }
mluis 0:92bca02df485 439
mluis 0:92bca02df485 440 /*!
mluis 0:92bca02df485 441 * \brief Function executed on Led 2 Timeout event
mluis 0:92bca02df485 442 */
mluis 0:92bca02df485 443 static void OnLed2TimerEvent( void )
mluis 0:92bca02df485 444 {
mluis 0:92bca02df485 445 TimerStop( &Led2Timer );
mluis 3:9c6f7f082151 446 // Switch LED 2 OFF
mluis 1:352f608c3337 447 Led2State = false;
mluis 0:92bca02df485 448 Led2StateChanged = true;
mluis 0:92bca02df485 449 }
mluis 0:92bca02df485 450
mluis 0:92bca02df485 451 /*!
mluis 3:9c6f7f082151 452 * \brief MCPS-Confirm event function
mluis 3:9c6f7f082151 453 *
mluis 5:1e9f6a365854 454 * \param [IN] mcpsConfirm - Pointer to the confirm structure,
mluis 3:9c6f7f082151 455 * containing confirm attributes.
mluis 0:92bca02df485 456 */
mluis 5:1e9f6a365854 457 static void McpsConfirm( McpsConfirm_t *mcpsConfirm )
mluis 0:92bca02df485 458 {
mluis 5:1e9f6a365854 459 if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:92bca02df485 460 {
mluis 5:1e9f6a365854 461 switch( mcpsConfirm->McpsRequest )
mluis 3:9c6f7f082151 462 {
mluis 3:9c6f7f082151 463 case MCPS_UNCONFIRMED:
mluis 3:9c6f7f082151 464 {
mluis 3:9c6f7f082151 465 // Check Datarate
mluis 3:9c6f7f082151 466 // Check TxPower
mluis 3:9c6f7f082151 467 break;
mluis 3:9c6f7f082151 468 }
mluis 3:9c6f7f082151 469 case MCPS_CONFIRMED:
mluis 3:9c6f7f082151 470 {
mluis 3:9c6f7f082151 471 // Check Datarate
mluis 3:9c6f7f082151 472 // Check TxPower
mluis 3:9c6f7f082151 473 // Check AckReceived
mluis 5:1e9f6a365854 474 // Check NbTrials
mluis 5:1e9f6a365854 475 LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived;
mluis 3:9c6f7f082151 476 break;
mluis 3:9c6f7f082151 477 }
mluis 3:9c6f7f082151 478 case MCPS_PROPRIETARY:
mluis 3:9c6f7f082151 479 {
mluis 3:9c6f7f082151 480 break;
mluis 3:9c6f7f082151 481 }
mluis 3:9c6f7f082151 482 default:
mluis 3:9c6f7f082151 483 break;
mluis 3:9c6f7f082151 484 }
mluis 5:1e9f6a365854 485 LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate;
mluis 5:1e9f6a365854 486 LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter;
mluis 5:1e9f6a365854 487
mluis 7:3173f0508a98 488 // Switch LED 1 ON
mluis 7:3173f0508a98 489 Led1State = true;
mluis 7:3173f0508a98 490 Led1StateChanged = true;
mluis 7:3173f0508a98 491 TimerStart( &Led1Timer );
mluis 7:3173f0508a98 492
mluis 3:9c6f7f082151 493 UplinkStatusUpdated = true;
mluis 3:9c6f7f082151 494 }
mluis 3:9c6f7f082151 495 NextTx = true;
mluis 3:9c6f7f082151 496 }
mluis 3:9c6f7f082151 497
mluis 3:9c6f7f082151 498 /*!
mluis 3:9c6f7f082151 499 * \brief MCPS-Indication event function
mluis 3:9c6f7f082151 500 *
mluis 5:1e9f6a365854 501 * \param [IN] mcpsIndication - Pointer to the indication structure,
mluis 3:9c6f7f082151 502 * containing indication attributes.
mluis 3:9c6f7f082151 503 */
mluis 5:1e9f6a365854 504 static void McpsIndication( McpsIndication_t *mcpsIndication )
mluis 3:9c6f7f082151 505 {
mluis 5:1e9f6a365854 506 if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
mluis 3:9c6f7f082151 507 {
mluis 3:9c6f7f082151 508 return;
mluis 3:9c6f7f082151 509 }
mluis 3:9c6f7f082151 510
mluis 5:1e9f6a365854 511 switch( mcpsIndication->McpsIndication )
mluis 3:9c6f7f082151 512 {
mluis 3:9c6f7f082151 513 case MCPS_UNCONFIRMED:
mluis 3:9c6f7f082151 514 {
mluis 3:9c6f7f082151 515 break;
mluis 3:9c6f7f082151 516 }
mluis 3:9c6f7f082151 517 case MCPS_CONFIRMED:
mluis 3:9c6f7f082151 518 {
mluis 3:9c6f7f082151 519 break;
mluis 3:9c6f7f082151 520 }
mluis 3:9c6f7f082151 521 case MCPS_PROPRIETARY:
mluis 3:9c6f7f082151 522 {
mluis 3:9c6f7f082151 523 break;
mluis 3:9c6f7f082151 524 }
mluis 3:9c6f7f082151 525 case MCPS_MULTICAST:
mluis 3:9c6f7f082151 526 {
mluis 3:9c6f7f082151 527 break;
mluis 3:9c6f7f082151 528 }
mluis 3:9c6f7f082151 529 default:
mluis 3:9c6f7f082151 530 break;
mluis 3:9c6f7f082151 531 }
mluis 3:9c6f7f082151 532
mluis 3:9c6f7f082151 533 // Check Multicast
mluis 3:9c6f7f082151 534 // Check Port
mluis 3:9c6f7f082151 535 // Check Datarate
mluis 3:9c6f7f082151 536 // Check FramePending
mluis 3:9c6f7f082151 537 // Check Buffer
mluis 3:9c6f7f082151 538 // Check BufferSize
mluis 3:9c6f7f082151 539 // Check Rssi
mluis 3:9c6f7f082151 540 // Check Snr
mluis 3:9c6f7f082151 541 // Check RxSlot
mluis 5:1e9f6a365854 542 LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi;
mluis 5:1e9f6a365854 543 if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
mluis 3:9c6f7f082151 544 {
mluis 3:9c6f7f082151 545 // Invert and divide by 4
mluis 5:1e9f6a365854 546 LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2;
mluis 3:9c6f7f082151 547 LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
mluis 0:92bca02df485 548 }
mluis 0:92bca02df485 549 else
mluis 0:92bca02df485 550 {
mluis 3:9c6f7f082151 551 // Divide by 4
mluis 5:1e9f6a365854 552 LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2;
mluis 3:9c6f7f082151 553 }
mluis 3:9c6f7f082151 554 LoRaMacDownlinkStatus.DownlinkCounter++;
mluis 5:1e9f6a365854 555 LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData;
mluis 5:1e9f6a365854 556 LoRaMacDownlinkStatus.Port = mcpsIndication->Port;
mluis 5:1e9f6a365854 557 LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer;
mluis 5:1e9f6a365854 558 LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize;
nimita23 11:a2c88830d3bf 559
nimita23 11:a2c88830d3bf 560 SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
nimita23 11:a2c88830d3bf 561
mluis 3:9c6f7f082151 562 if( ComplianceTest.Running == true )
mluis 3:9c6f7f082151 563 {
mluis 3:9c6f7f082151 564 ComplianceTest.DownLinkCounter++;
mluis 3:9c6f7f082151 565 }
mluis 0:92bca02df485 566
mluis 5:1e9f6a365854 567 if( mcpsIndication->RxData == true )
mluis 3:9c6f7f082151 568 {
mluis 5:1e9f6a365854 569 switch( mcpsIndication->Port )
mluis 0:92bca02df485 570 {
mluis 3:9c6f7f082151 571 case 1: // The application LED can be controlled on port 1 or 2
mluis 3:9c6f7f082151 572 case 2:
mluis 5:1e9f6a365854 573 if( mcpsIndication->BufferSize == 1 )
mluis 1:352f608c3337 574 {
mluis 5:1e9f6a365854 575 AppLedStateOn = mcpsIndication->Buffer[0] & 0x01;
mluis 3:9c6f7f082151 576 Led3StateChanged = true;
mluis 3:9c6f7f082151 577 }
mluis 3:9c6f7f082151 578 break;
mluis 3:9c6f7f082151 579 case 224:
mluis 3:9c6f7f082151 580 if( ComplianceTest.Running == false )
mluis 3:9c6f7f082151 581 {
mluis 3:9c6f7f082151 582 // Check compliance test enable command (i)
mluis 5:1e9f6a365854 583 if( ( mcpsIndication->BufferSize == 4 ) &&
mluis 5:1e9f6a365854 584 ( mcpsIndication->Buffer[0] == 0x01 ) &&
mluis 5:1e9f6a365854 585 ( mcpsIndication->Buffer[1] == 0x01 ) &&
mluis 5:1e9f6a365854 586 ( mcpsIndication->Buffer[2] == 0x01 ) &&
mluis 5:1e9f6a365854 587 ( mcpsIndication->Buffer[3] == 0x01 ) )
mluis 1:352f608c3337 588 {
mluis 3:9c6f7f082151 589 IsTxConfirmed = false;
mluis 3:9c6f7f082151 590 AppPort = 224;
mluis 3:9c6f7f082151 591 AppDataSize = 2;
mluis 3:9c6f7f082151 592 ComplianceTest.DownLinkCounter = 0;
mluis 3:9c6f7f082151 593 ComplianceTest.LinkCheck = false;
mluis 3:9c6f7f082151 594 ComplianceTest.DemodMargin = 0;
mluis 3:9c6f7f082151 595 ComplianceTest.NbGateways = 0;
mluis 3:9c6f7f082151 596 ComplianceTest.Running = true;
mluis 3:9c6f7f082151 597 ComplianceTest.State = 1;
mluis 9:ee9dcbb9708d 598
mluis 3:9c6f7f082151 599 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 600 mibReq.Type = MIB_ADR;
mluis 3:9c6f7f082151 601 mibReq.Param.AdrEnable = true;
mluis 3:9c6f7f082151 602 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 603
mluis 3:9c6f7f082151 604 #if defined( USE_BAND_868 )
mluis 3:9c6f7f082151 605 LoRaMacTestSetDutyCycleOn( false );
mluis 3:9c6f7f082151 606 #endif
mluis 1:352f608c3337 607 }
mluis 1:352f608c3337 608 }
mluis 0:92bca02df485 609 else
mluis 0:92bca02df485 610 {
mluis 5:1e9f6a365854 611 ComplianceTest.State = mcpsIndication->Buffer[0];
mluis 3:9c6f7f082151 612 switch( ComplianceTest.State )
mluis 3:9c6f7f082151 613 {
mluis 3:9c6f7f082151 614 case 0: // Check compliance test disable command (ii)
mluis 3:9c6f7f082151 615 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 3:9c6f7f082151 616 AppPort = LORAWAN_APP_PORT;
mluis 3:9c6f7f082151 617 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 3:9c6f7f082151 618 ComplianceTest.DownLinkCounter = 0;
mluis 3:9c6f7f082151 619 ComplianceTest.Running = false;
mluis 9:ee9dcbb9708d 620
mluis 3:9c6f7f082151 621 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 622 mibReq.Type = MIB_ADR;
mluis 3:9c6f7f082151 623 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 3:9c6f7f082151 624 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 625 #if defined( USE_BAND_868 )
mluis 3:9c6f7f082151 626 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 3:9c6f7f082151 627 #endif
mluis 3:9c6f7f082151 628 break;
mluis 3:9c6f7f082151 629 case 1: // (iii, iv)
mluis 3:9c6f7f082151 630 AppDataSize = 2;
mluis 3:9c6f7f082151 631 break;
mluis 3:9c6f7f082151 632 case 2: // Enable confirmed messages (v)
mluis 3:9c6f7f082151 633 IsTxConfirmed = true;
mluis 3:9c6f7f082151 634 ComplianceTest.State = 1;
mluis 3:9c6f7f082151 635 break;
mluis 3:9c6f7f082151 636 case 3: // Disable confirmed messages (vi)
mluis 3:9c6f7f082151 637 IsTxConfirmed = false;
mluis 3:9c6f7f082151 638 ComplianceTest.State = 1;
mluis 3:9c6f7f082151 639 break;
mluis 3:9c6f7f082151 640 case 4: // (vii)
mluis 5:1e9f6a365854 641 AppDataSize = mcpsIndication->BufferSize;
mluis 3:9c6f7f082151 642
mluis 3:9c6f7f082151 643 AppData[0] = 4;
mluis 3:9c6f7f082151 644 for( uint8_t i = 1; i < AppDataSize; i++ )
mluis 3:9c6f7f082151 645 {
mluis 5:1e9f6a365854 646 AppData[i] = mcpsIndication->Buffer[i] + 1;
mluis 3:9c6f7f082151 647 }
mluis 3:9c6f7f082151 648 break;
mluis 3:9c6f7f082151 649 case 5: // (viii)
mluis 3:9c6f7f082151 650 {
mluis 3:9c6f7f082151 651 MlmeReq_t mlmeReq;
mluis 3:9c6f7f082151 652 mlmeReq.Type = MLME_LINK_CHECK;
mluis 3:9c6f7f082151 653 LoRaMacMlmeRequest( &mlmeReq );
mluis 3:9c6f7f082151 654 }
mluis 3:9c6f7f082151 655 break;
mluis 7:3173f0508a98 656 case 6: // (ix)
mluis 7:3173f0508a98 657 {
mluis 7:3173f0508a98 658 MlmeReq_t mlmeReq;
mluis 7:3173f0508a98 659
mluis 9:ee9dcbb9708d 660 // Disable TestMode and revert back to normal operation
mluis 9:ee9dcbb9708d 661 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 9:ee9dcbb9708d 662 AppPort = LORAWAN_APP_PORT;
mluis 9:ee9dcbb9708d 663 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 9:ee9dcbb9708d 664 ComplianceTest.DownLinkCounter = 0;
mluis 9:ee9dcbb9708d 665 ComplianceTest.Running = false;
mluis 9:ee9dcbb9708d 666
mluis 9:ee9dcbb9708d 667 MibRequestConfirm_t mibReq;
mluis 9:ee9dcbb9708d 668 mibReq.Type = MIB_ADR;
mluis 9:ee9dcbb9708d 669 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 9:ee9dcbb9708d 670 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 9:ee9dcbb9708d 671 #if defined( USE_BAND_868 )
mluis 9:ee9dcbb9708d 672 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 9:ee9dcbb9708d 673 #endif
mluis 9:ee9dcbb9708d 674
mluis 7:3173f0508a98 675 mlmeReq.Type = MLME_JOIN;
mluis 7:3173f0508a98 676
mluis 7:3173f0508a98 677 mlmeReq.Req.Join.DevEui = DevEui;
mluis 7:3173f0508a98 678 mlmeReq.Req.Join.AppEui = AppEui;
mluis 7:3173f0508a98 679 mlmeReq.Req.Join.AppKey = AppKey;
mluis 9:ee9dcbb9708d 680 mlmeReq.Req.Join.NbTrials = 3;
mluis 7:3173f0508a98 681
mluis 7:3173f0508a98 682 LoRaMacMlmeRequest( &mlmeReq );
mluis 7:3173f0508a98 683 DeviceState = DEVICE_STATE_SLEEP;
mluis 7:3173f0508a98 684 }
mluis 7:3173f0508a98 685 break;
mluis 9:ee9dcbb9708d 686 case 7: // (x)
mluis 9:ee9dcbb9708d 687 {
mluis 9:ee9dcbb9708d 688 if( mcpsIndication->BufferSize == 3 )
mluis 9:ee9dcbb9708d 689 {
mluis 9:ee9dcbb9708d 690 MlmeReq_t mlmeReq;
mluis 9:ee9dcbb9708d 691 mlmeReq.Type = MLME_TXCW;
mluis 9:ee9dcbb9708d 692 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 9:ee9dcbb9708d 693 LoRaMacMlmeRequest( &mlmeReq );
mluis 9:ee9dcbb9708d 694 }
mluis 9:ee9dcbb9708d 695 else if( mcpsIndication->BufferSize == 7 )
mluis 9:ee9dcbb9708d 696 {
mluis 9:ee9dcbb9708d 697 MlmeReq_t mlmeReq;
mluis 9:ee9dcbb9708d 698 mlmeReq.Type = MLME_TXCW_1;
mluis 9:ee9dcbb9708d 699 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 9:ee9dcbb9708d 700 mlmeReq.Req.TxCw.Frequency = ( uint32_t )( ( mcpsIndication->Buffer[3] << 16 ) | ( mcpsIndication->Buffer[4] << 8 ) | mcpsIndication->Buffer[5] ) * 100;
mluis 9:ee9dcbb9708d 701 mlmeReq.Req.TxCw.Power = mcpsIndication->Buffer[6];
mluis 9:ee9dcbb9708d 702 LoRaMacMlmeRequest( &mlmeReq );
mluis 9:ee9dcbb9708d 703 }
mluis 9:ee9dcbb9708d 704 ComplianceTest.State = 1;
mluis 9:ee9dcbb9708d 705 }
mluis 9:ee9dcbb9708d 706 break;
mluis 3:9c6f7f082151 707 default:
mluis 3:9c6f7f082151 708 break;
mluis 3:9c6f7f082151 709 }
mluis 0:92bca02df485 710 }
mluis 3:9c6f7f082151 711 break;
mluis 3:9c6f7f082151 712 default:
mluis 3:9c6f7f082151 713 break;
mluis 0:92bca02df485 714 }
mluis 0:92bca02df485 715 }
mluis 1:352f608c3337 716
mluis 3:9c6f7f082151 717 // Switch LED 2 ON for each received downlink
mluis 3:9c6f7f082151 718 Led2State = true;
mluis 3:9c6f7f082151 719 Led2StateChanged = true;
mluis 3:9c6f7f082151 720 TimerStart( &Led2Timer );
mluis 3:9c6f7f082151 721 DownlinkStatusUpdated = true;
mluis 3:9c6f7f082151 722 }
mluis 3:9c6f7f082151 723
mluis 3:9c6f7f082151 724 /*!
mluis 3:9c6f7f082151 725 * \brief MLME-Confirm event function
mluis 3:9c6f7f082151 726 *
mluis 5:1e9f6a365854 727 * \param [IN] mlmeConfirm - Pointer to the confirm structure,
mluis 3:9c6f7f082151 728 * containing confirm attributes.
mluis 3:9c6f7f082151 729 */
mluis 5:1e9f6a365854 730 static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
mluis 3:9c6f7f082151 731 {
mluis 9:ee9dcbb9708d 732 switch( mlmeConfirm->MlmeRequest )
mluis 3:9c6f7f082151 733 {
mluis 9:ee9dcbb9708d 734 case MLME_JOIN:
mluis 3:9c6f7f082151 735 {
mluis 9:ee9dcbb9708d 736 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 3:9c6f7f082151 737 {
mluis 3:9c6f7f082151 738 // Status is OK, node has joined the network
mluis 3:9c6f7f082151 739 IsNetworkJoinedStatusUpdate = true;
mluis 7:3173f0508a98 740 DeviceState = DEVICE_STATE_SEND;
mluis 9:ee9dcbb9708d 741 }
mluis 9:ee9dcbb9708d 742 else
mluis 9:ee9dcbb9708d 743 {
mluis 9:ee9dcbb9708d 744 // Join was not successful. Try to join again
mluis 9:ee9dcbb9708d 745 DeviceState = DEVICE_STATE_JOIN;
mluis 3:9c6f7f082151 746 }
mluis 9:ee9dcbb9708d 747 break;
mluis 9:ee9dcbb9708d 748 }
mluis 9:ee9dcbb9708d 749 case MLME_LINK_CHECK:
mluis 9:ee9dcbb9708d 750 {
mluis 9:ee9dcbb9708d 751 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 3:9c6f7f082151 752 {
mluis 3:9c6f7f082151 753 // Check DemodMargin
mluis 3:9c6f7f082151 754 // Check NbGateways
mluis 3:9c6f7f082151 755 if( ComplianceTest.Running == true )
mluis 3:9c6f7f082151 756 {
mluis 3:9c6f7f082151 757 ComplianceTest.LinkCheck = true;
mluis 5:1e9f6a365854 758 ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin;
mluis 5:1e9f6a365854 759 ComplianceTest.NbGateways = mlmeConfirm->NbGateways;
mluis 3:9c6f7f082151 760 }
mluis 3:9c6f7f082151 761 }
mluis 9:ee9dcbb9708d 762 break;
mluis 3:9c6f7f082151 763 }
mluis 9:ee9dcbb9708d 764 default:
mluis 9:ee9dcbb9708d 765 break;
mluis 3:9c6f7f082151 766 }
mluis 3:9c6f7f082151 767 NextTx = true;
mluis 3:9c6f7f082151 768 UplinkStatusUpdated = true;
mluis 0:92bca02df485 769 }
mluis 0:92bca02df485 770
mluis 0:92bca02df485 771 /**
mluis 0:92bca02df485 772 * Main application entry point.
mluis 0:92bca02df485 773 */
mluis 0:92bca02df485 774 int main( void )
mluis 0:92bca02df485 775 {
mluis 3:9c6f7f082151 776 LoRaMacPrimitives_t LoRaMacPrimitives;
mluis 3:9c6f7f082151 777 LoRaMacCallback_t LoRaMacCallbacks;
mluis 3:9c6f7f082151 778 MibRequestConfirm_t mibReq;
mluis 0:92bca02df485 779
mluis 0:92bca02df485 780 BoardInit( );
mluis 3:9c6f7f082151 781 SerialDisplayInit( );
mluis 0:92bca02df485 782
mluis 7:3173f0508a98 783 SerialDisplayUpdateEui( 5, DevEui );
mluis 7:3173f0508a98 784 SerialDisplayUpdateEui( 6, AppEui );
mluis 7:3173f0508a98 785 SerialDisplayUpdateKey( 7, AppKey );
mluis 7:3173f0508a98 786
mluis 7:3173f0508a98 787 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 7:3173f0508a98 788 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 7:3173f0508a98 789 SerialDisplayUpdateDevAddr( DevAddr );
mluis 7:3173f0508a98 790 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 7:3173f0508a98 791 SerialDisplayUpdateKey( 13, AppSKey );
mluis 7:3173f0508a98 792 #endif
mluis 7:3173f0508a98 793
mluis 3:9c6f7f082151 794 DeviceState = DEVICE_STATE_INIT;
mluis 0:92bca02df485 795
mluis 0:92bca02df485 796 while( 1 )
mluis 0:92bca02df485 797 {
mluis 0:92bca02df485 798 SerialRxProcess( );
mluis 0:92bca02df485 799 if( IsNetworkJoinedStatusUpdate == true )
mluis 0:92bca02df485 800 {
mluis 0:92bca02df485 801 IsNetworkJoinedStatusUpdate = false;
mluis 3:9c6f7f082151 802 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 803 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 804 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:92bca02df485 805 }
mluis 0:92bca02df485 806 if( Led1StateChanged == true )
mluis 0:92bca02df485 807 {
mluis 0:92bca02df485 808 Led1StateChanged = false;
mluis 1:352f608c3337 809 SerialDisplayUpdateLedState( 1, Led1State );
mluis 0:92bca02df485 810 }
mluis 0:92bca02df485 811 if( Led2StateChanged == true )
mluis 0:92bca02df485 812 {
mluis 0:92bca02df485 813 Led2StateChanged = false;
mluis 1:352f608c3337 814 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:92bca02df485 815 }
mluis 0:92bca02df485 816 if( Led3StateChanged == true )
mluis 0:92bca02df485 817 {
mluis 0:92bca02df485 818 Led3StateChanged = false;
mluis 0:92bca02df485 819 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:92bca02df485 820 }
mluis 3:9c6f7f082151 821 if( UplinkStatusUpdated == true )
mluis 0:92bca02df485 822 {
mluis 3:9c6f7f082151 823 UplinkStatusUpdated = false;
mluis 3:9c6f7f082151 824 SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize );
mluis 3:9c6f7f082151 825 }
mluis 3:9c6f7f082151 826 if( DownlinkStatusUpdated == true )
mluis 3:9c6f7f082151 827 {
mluis 3:9c6f7f082151 828 DownlinkStatusUpdated = false;
mluis 1:352f608c3337 829 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:92bca02df485 830 SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
mluis 0:92bca02df485 831 }
mluis 3:9c6f7f082151 832
mluis 3:9c6f7f082151 833 switch( DeviceState )
mluis 0:92bca02df485 834 {
mluis 3:9c6f7f082151 835 case DEVICE_STATE_INIT:
mluis 3:9c6f7f082151 836 {
mluis 3:9c6f7f082151 837 LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
mluis 3:9c6f7f082151 838 LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
mluis 3:9c6f7f082151 839 LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
mluis 3:9c6f7f082151 840 LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
mluis 3:9c6f7f082151 841 LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
mluis 3:9c6f7f082151 842
mluis 3:9c6f7f082151 843 TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
mluis 3:9c6f7f082151 844
mluis 3:9c6f7f082151 845 TimerInit( &Led1Timer, OnLed1TimerEvent );
mluis 9:ee9dcbb9708d 846 TimerSetValue( &Led1Timer, 25 );
mluis 3:9c6f7f082151 847
mluis 3:9c6f7f082151 848 TimerInit( &Led2Timer, OnLed2TimerEvent );
mluis 9:ee9dcbb9708d 849 TimerSetValue( &Led2Timer, 25 );
mluis 3:9c6f7f082151 850
mluis 3:9c6f7f082151 851 mibReq.Type = MIB_ADR;
mluis 3:9c6f7f082151 852 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 3:9c6f7f082151 853 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 854
mluis 3:9c6f7f082151 855 mibReq.Type = MIB_PUBLIC_NETWORK;
mluis 3:9c6f7f082151 856 mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
mluis 3:9c6f7f082151 857 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 858
mluis 3:9c6f7f082151 859 #if defined( USE_BAND_868 )
mluis 3:9c6f7f082151 860 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 3:9c6f7f082151 861 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 5:1e9f6a365854 862
mluis 9:ee9dcbb9708d 863 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 5:1e9f6a365854 864 LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
mluis 5:1e9f6a365854 865 LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
mluis 5:1e9f6a365854 866 LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
mluis 5:1e9f6a365854 867 LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 );
mluis 5:1e9f6a365854 868 LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 );
mluis 5:1e9f6a365854 869 LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
mluis 5:1e9f6a365854 870 LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
mluis 7:3173f0508a98 871
mluis 9:ee9dcbb9708d 872 mibReq.Type = MIB_RX2_DEFAULT_CHANNEL;
mluis 9:ee9dcbb9708d 873 mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
mluis 9:ee9dcbb9708d 874 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 9:ee9dcbb9708d 875
mluis 7:3173f0508a98 876 mibReq.Type = MIB_RX2_CHANNEL;
mluis 7:3173f0508a98 877 mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
mluis 7:3173f0508a98 878 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 5:1e9f6a365854 879 #endif
mluis 5:1e9f6a365854 880
mluis 3:9c6f7f082151 881 #endif
mluis 3:9c6f7f082151 882 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 3:9c6f7f082151 883 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 3:9c6f7f082151 884 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 3:9c6f7f082151 885
mluis 3:9c6f7f082151 886 LoRaMacDownlinkStatus.DownlinkCounter = 0;
mluis 3:9c6f7f082151 887
mluis 3:9c6f7f082151 888 DeviceState = DEVICE_STATE_JOIN;
mluis 3:9c6f7f082151 889 break;
mluis 3:9c6f7f082151 890 }
mluis 3:9c6f7f082151 891 case DEVICE_STATE_JOIN:
mluis 3:9c6f7f082151 892 {
mluis 3:9c6f7f082151 893 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 3:9c6f7f082151 894 MlmeReq_t mlmeReq;
mluis 3:9c6f7f082151 895
mluis 3:9c6f7f082151 896 mlmeReq.Type = MLME_JOIN;
mluis 3:9c6f7f082151 897
mluis 3:9c6f7f082151 898 mlmeReq.Req.Join.DevEui = DevEui;
mluis 3:9c6f7f082151 899 mlmeReq.Req.Join.AppEui = AppEui;
mluis 3:9c6f7f082151 900 mlmeReq.Req.Join.AppKey = AppKey;
mluis 3:9c6f7f082151 901
mluis 3:9c6f7f082151 902 if( NextTx == true )
mluis 3:9c6f7f082151 903 {
mluis 3:9c6f7f082151 904 LoRaMacMlmeRequest( &mlmeReq );
mluis 3:9c6f7f082151 905 }
mluis 7:3173f0508a98 906 DeviceState = DEVICE_STATE_SLEEP;
mluis 3:9c6f7f082151 907 #else
mluis 3:9c6f7f082151 908 mibReq.Type = MIB_NET_ID;
mluis 3:9c6f7f082151 909 mibReq.Param.NetID = LORAWAN_NETWORK_ID;
mluis 3:9c6f7f082151 910 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 911
mluis 3:9c6f7f082151 912 mibReq.Type = MIB_DEV_ADDR;
mluis 3:9c6f7f082151 913 mibReq.Param.DevAddr = DevAddr;
mluis 3:9c6f7f082151 914 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 915
mluis 3:9c6f7f082151 916 mibReq.Type = MIB_NWK_SKEY;
mluis 3:9c6f7f082151 917 mibReq.Param.NwkSKey = NwkSKey;
mluis 3:9c6f7f082151 918 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 919
mluis 3:9c6f7f082151 920 mibReq.Type = MIB_APP_SKEY;
mluis 3:9c6f7f082151 921 mibReq.Param.AppSKey = AppSKey;
mluis 3:9c6f7f082151 922 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 923
mluis 3:9c6f7f082151 924 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 925 mibReq.Param.IsNetworkJoined = true;
mluis 3:9c6f7f082151 926 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 927
mluis 3:9c6f7f082151 928 DeviceState = DEVICE_STATE_SEND;
mluis 3:9c6f7f082151 929 #endif
mluis 3:9c6f7f082151 930 IsNetworkJoinedStatusUpdate = true;
mluis 3:9c6f7f082151 931 break;
mluis 3:9c6f7f082151 932 }
mluis 3:9c6f7f082151 933 case DEVICE_STATE_SEND:
mluis 3:9c6f7f082151 934 {
mluis 3:9c6f7f082151 935 if( NextTx == true )
mluis 3:9c6f7f082151 936 {
mluis 3:9c6f7f082151 937 SerialDisplayUpdateUplinkAcked( false );
mluis 3:9c6f7f082151 938 SerialDisplayUpdateDonwlinkRxData( false );
mluis 3:9c6f7f082151 939 PrepareTxFrame( AppPort );
mluis 3:9c6f7f082151 940
mluis 3:9c6f7f082151 941 NextTx = SendFrame( );
mluis 3:9c6f7f082151 942 }
mluis 3:9c6f7f082151 943 if( ComplianceTest.Running == true )
mluis 3:9c6f7f082151 944 {
mluis 5:1e9f6a365854 945 // Schedule next packet transmission
mluis 9:ee9dcbb9708d 946 TxDutyCycleTime = 5000; // 5000 ms
mluis 3:9c6f7f082151 947 }
mluis 3:9c6f7f082151 948 else
mluis 3:9c6f7f082151 949 {
mluis 3:9c6f7f082151 950 // Schedule next packet transmission
mluis 3:9c6f7f082151 951 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
mluis 3:9c6f7f082151 952 }
mluis 3:9c6f7f082151 953 DeviceState = DEVICE_STATE_CYCLE;
mluis 3:9c6f7f082151 954 break;
mluis 3:9c6f7f082151 955 }
mluis 3:9c6f7f082151 956 case DEVICE_STATE_CYCLE:
mluis 3:9c6f7f082151 957 {
mluis 5:1e9f6a365854 958 DeviceState = DEVICE_STATE_SLEEP;
mluis 5:1e9f6a365854 959
mluis 3:9c6f7f082151 960 // Schedule next packet transmission
mluis 1:352f608c3337 961 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
mluis 1:352f608c3337 962 TimerStart( &TxNextPacketTimer );
mluis 3:9c6f7f082151 963 break;
mluis 3:9c6f7f082151 964 }
mluis 3:9c6f7f082151 965 case DEVICE_STATE_SLEEP:
mluis 3:9c6f7f082151 966 {
mluis 3:9c6f7f082151 967 // Wake up through events
mluis 3:9c6f7f082151 968 break;
mluis 3:9c6f7f082151 969 }
mluis 3:9c6f7f082151 970 default:
mluis 3:9c6f7f082151 971 {
mluis 3:9c6f7f082151 972 DeviceState = DEVICE_STATE_INIT;
mluis 3:9c6f7f082151 973 break;
mluis 3:9c6f7f082151 974 }
mluis 0:92bca02df485 975 }
mluis 0:92bca02df485 976 }
mluis 0:92bca02df485 977 }