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

Dependencies:   mbed LoRaWAN-lib SX1276Lib

Committer:
nimita23
Date:
Sun Sep 15 17:10:06 2019 +0000
Revision:
13:779fef3e4573
Parent:
12:bf1efd688cc8
led working

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 #include "mbed.h"
mluis 0:92bca02df485 12 #include "board.h"
mluis 0:92bca02df485 13 #include "radio.h"
mluis 0:92bca02df485 14
mluis 0:92bca02df485 15 #include "LoRaMac.h"
mluis 9:ee9dcbb9708d 16 #include "Commissioning.h"
mluis 0:92bca02df485 17 #include "SerialDisplay.h"
mluis 0:92bca02df485 18
mluis 0:92bca02df485 19 /*!
mluis 9:ee9dcbb9708d 20 * Defines the application data transmission duty cycle. 5s, value in [ms].
mluis 0:92bca02df485 21 */
mluis 9:ee9dcbb9708d 22 #define APP_TX_DUTYCYCLE 5000
mluis 1:352f608c3337 23
mluis 1:352f608c3337 24 /*!
mluis 1:352f608c3337 25 * Defines a random delay for application data transmission duty cycle. 1s,
mluis 9:ee9dcbb9708d 26 * value in [ms].
mluis 1:352f608c3337 27 */
mluis 9:ee9dcbb9708d 28 #define APP_TX_DUTYCYCLE_RND 1000
mluis 0:92bca02df485 29
mluis 0:92bca02df485 30 /*!
mluis 5:1e9f6a365854 31 * Default datarate
mluis 3:9c6f7f082151 32 */
mluis 3:9c6f7f082151 33 #define LORAWAN_DEFAULT_DATARATE DR_0
mluis 3:9c6f7f082151 34
mluis 3:9c6f7f082151 35 /*!
mluis 0:92bca02df485 36 * LoRaWAN confirmed messages
mluis 0:92bca02df485 37 */
nimita23 10:715b2204399b 38 #define LORAWAN_CONFIRMED_MSG_ON false
mluis 0:92bca02df485 39
mluis 0:92bca02df485 40 /*!
mluis 3:9c6f7f082151 41 * LoRaWAN Adaptive Data Rate
mluis 0:92bca02df485 42 *
mluis 0:92bca02df485 43 * \remark Please note that when ADR is enabled the end-device should be static
mluis 0:92bca02df485 44 */
mluis 0:92bca02df485 45 #define LORAWAN_ADR_ON 1
mluis 0:92bca02df485 46
mluis 1:352f608c3337 47 #if defined( USE_BAND_868 )
mluis 1:352f608c3337 48
mluis 3:9c6f7f082151 49 #include "LoRaMacTest.h"
mluis 3:9c6f7f082151 50
mluis 0:92bca02df485 51 /*!
mluis 0:92bca02df485 52 * LoRaWAN ETSI duty cycle control enable/disable
mluis 0:92bca02df485 53 *
mluis 0:92bca02df485 54 * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
mluis 0:92bca02df485 55 */
mluis 9:ee9dcbb9708d 56 #define LORAWAN_DUTYCYCLE_ON false
mluis 0:92bca02df485 57
mluis 5:1e9f6a365854 58 #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1
mluis 5:1e9f6a365854 59
mluis 9:ee9dcbb9708d 60 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 5:1e9f6a365854 61
mluis 5:1e9f6a365854 62 #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 63 #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 64 #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 65 #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 66 #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 67 #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 }
mluis 5:1e9f6a365854 68 #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 }
mluis 5:1e9f6a365854 69
mluis 5:1e9f6a365854 70 #endif
mluis 5:1e9f6a365854 71
mluis 1:352f608c3337 72 #endif
mluis 1:352f608c3337 73
mluis 0:92bca02df485 74 /*!
mluis 0:92bca02df485 75 * LoRaWAN application port
mluis 0:92bca02df485 76 */
nimita23 10:715b2204399b 77 #define LORAWAN_APP_PORT 10
mluis 0:92bca02df485 78
mluis 0:92bca02df485 79 /*!
mluis 0:92bca02df485 80 * User application data buffer size
mluis 0:92bca02df485 81 */
mluis 0:92bca02df485 82 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
mluis 0:92bca02df485 83 #define LORAWAN_APP_DATA_SIZE 6
mluis 0:92bca02df485 84
mluis 0:92bca02df485 85 #else
nimita23 10:715b2204399b 86 #define LORAWAN_APP_DATA_SIZE 5
mluis 0:92bca02df485 87
mluis 0:92bca02df485 88 #endif
mluis 0:92bca02df485 89
mluis 0:92bca02df485 90 static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
mluis 0:92bca02df485 91 static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
mluis 0:92bca02df485 92 static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
mluis 0:92bca02df485 93
mluis 7:3173f0508a98 94 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:92bca02df485 95
mluis 0:92bca02df485 96 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mluis 0:92bca02df485 97 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
mluis 0:92bca02df485 98
mluis 3:9c6f7f082151 99 /*!
mluis 3:9c6f7f082151 100 * Device address
mluis 3:9c6f7f082151 101 */
mluis 3:9c6f7f082151 102 static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
mluis 0:92bca02df485 103
mluis 3:9c6f7f082151 104 #endif
mluis 0:92bca02df485 105
mluis 0:92bca02df485 106 /*!
mluis 1:352f608c3337 107 * Application port
mluis 1:352f608c3337 108 */
mluis 1:352f608c3337 109 static uint8_t AppPort = LORAWAN_APP_PORT;
mluis 1:352f608c3337 110
mluis 1:352f608c3337 111 /*!
mluis 1:352f608c3337 112 * User application data size
mluis 1:352f608c3337 113 */
mluis 1:352f608c3337 114 static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 1:352f608c3337 115
mluis 1:352f608c3337 116 /*!
mluis 1:352f608c3337 117 * User application data buffer size
mluis 1:352f608c3337 118 */
mluis 1:352f608c3337 119 #define LORAWAN_APP_DATA_MAX_SIZE 64
mluis 1:352f608c3337 120
mluis 1:352f608c3337 121 /*!
mluis 0:92bca02df485 122 * User application data
mluis 0:92bca02df485 123 */
mluis 1:352f608c3337 124 static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
mluis 1:352f608c3337 125
mluis 1:352f608c3337 126 /*!
mluis 1:352f608c3337 127 * Indicates if the node is sending confirmed or unconfirmed messages
mluis 1:352f608c3337 128 */
mluis 1:352f608c3337 129 static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:92bca02df485 130
mluis 0:92bca02df485 131 /*!
mluis 0:92bca02df485 132 * Defines the application data transmission duty cycle
mluis 0:92bca02df485 133 */
mluis 0:92bca02df485 134 static uint32_t TxDutyCycleTime;
mluis 0:92bca02df485 135
mluis 1:352f608c3337 136 /*!
mluis 1:352f608c3337 137 * Timer to handle the application data transmission duty cycle
mluis 1:352f608c3337 138 */
mluis 1:352f608c3337 139 static TimerEvent_t TxNextPacketTimer;
mluis 0:92bca02df485 140
mluis 3:9c6f7f082151 141 /*!
mluis 3:9c6f7f082151 142 * Specifies the state of the application LED
mluis 3:9c6f7f082151 143 */
mluis 3:9c6f7f082151 144 static bool AppLedStateOn = false;
mluis 3:9c6f7f082151 145 volatile bool Led3StateChanged = false;
mluis 0:92bca02df485 146 /*!
mluis 3:9c6f7f082151 147 * Timer to handle the state of LED1
mluis 0:92bca02df485 148 */
mluis 3:9c6f7f082151 149 static TimerEvent_t Led1Timer;
mluis 3:9c6f7f082151 150 volatile bool Led1State = false;
mluis 3:9c6f7f082151 151 volatile bool Led1StateChanged = false;
mluis 3:9c6f7f082151 152 /*!
mluis 3:9c6f7f082151 153 * Timer to handle the state of LED2
mluis 3:9c6f7f082151 154 */
mluis 3:9c6f7f082151 155 static TimerEvent_t Led2Timer;
mluis 3:9c6f7f082151 156 volatile bool Led2State = false;
mluis 3:9c6f7f082151 157 volatile bool Led2StateChanged = false;
mluis 0:92bca02df485 158
nimita23 13:779fef3e4573 159
nimita23 13:779fef3e4573 160 DigitalOut myled(PA_5, 1);
nimita23 13:779fef3e4573 161
mluis 0:92bca02df485 162 /*!
mluis 0:92bca02df485 163 * Indicates if a new packet can be sent
mluis 0:92bca02df485 164 */
mluis 3:9c6f7f082151 165 static bool NextTx = true;
mluis 0:92bca02df485 166
mluis 3:9c6f7f082151 167 /*!
mluis 3:9c6f7f082151 168 * Device states
mluis 3:9c6f7f082151 169 */
mluis 9:ee9dcbb9708d 170 static enum eDeviceState
mluis 3:9c6f7f082151 171 {
mluis 3:9c6f7f082151 172 DEVICE_STATE_INIT,
mluis 3:9c6f7f082151 173 DEVICE_STATE_JOIN,
mluis 3:9c6f7f082151 174 DEVICE_STATE_SEND,
mluis 3:9c6f7f082151 175 DEVICE_STATE_CYCLE,
mluis 3:9c6f7f082151 176 DEVICE_STATE_SLEEP
mluis 3:9c6f7f082151 177 }DeviceState;
mluis 0:92bca02df485 178
mluis 3:9c6f7f082151 179 /*!
mluis 3:9c6f7f082151 180 * LoRaWAN compliance tests support data
mluis 3:9c6f7f082151 181 */
mluis 3:9c6f7f082151 182 struct ComplianceTest_s
mluis 3:9c6f7f082151 183 {
mluis 3:9c6f7f082151 184 bool Running;
mluis 3:9c6f7f082151 185 uint8_t State;
mluis 3:9c6f7f082151 186 bool IsTxConfirmed;
mluis 3:9c6f7f082151 187 uint8_t AppPort;
mluis 3:9c6f7f082151 188 uint8_t AppDataSize;
mluis 3:9c6f7f082151 189 uint8_t *AppDataBuffer;
mluis 3:9c6f7f082151 190 uint16_t DownLinkCounter;
mluis 3:9c6f7f082151 191 bool LinkCheck;
mluis 3:9c6f7f082151 192 uint8_t DemodMargin;
mluis 3:9c6f7f082151 193 uint8_t NbGateways;
mluis 3:9c6f7f082151 194 }ComplianceTest;
mluis 0:92bca02df485 195
mluis 3:9c6f7f082151 196 /*
mluis 3:9c6f7f082151 197 * SerialDisplay managment variables
mluis 3:9c6f7f082151 198 */
mluis 1:352f608c3337 199
mluis 3:9c6f7f082151 200 /*!
mluis 3:9c6f7f082151 201 * Indicates if the MAC layer network join status has changed.
mluis 3:9c6f7f082151 202 */
mluis 3:9c6f7f082151 203 static bool IsNetworkJoinedStatusUpdate = false;
mluis 3:9c6f7f082151 204
mluis 3:9c6f7f082151 205 /*!
mluis 3:9c6f7f082151 206 * Strucure containing the Uplink status
mluis 3:9c6f7f082151 207 */
mluis 0:92bca02df485 208 struct sLoRaMacUplinkStatus
mluis 0:92bca02df485 209 {
mluis 0:92bca02df485 210 uint8_t Acked;
mluis 0:92bca02df485 211 int8_t Datarate;
mluis 0:92bca02df485 212 uint16_t UplinkCounter;
mluis 0:92bca02df485 213 uint8_t Port;
mluis 0:92bca02df485 214 uint8_t *Buffer;
mluis 0:92bca02df485 215 uint8_t BufferSize;
mluis 0:92bca02df485 216 }LoRaMacUplinkStatus;
mluis 3:9c6f7f082151 217 volatile bool UplinkStatusUpdated = false;
mluis 0:92bca02df485 218
mluis 3:9c6f7f082151 219 /*!
mluis 3:9c6f7f082151 220 * Strucure containing the Downlink status
mluis 3:9c6f7f082151 221 */
mluis 0:92bca02df485 222 struct sLoRaMacDownlinkStatus
mluis 0:92bca02df485 223 {
mluis 0:92bca02df485 224 int16_t Rssi;
mluis 0:92bca02df485 225 int8_t Snr;
mluis 0:92bca02df485 226 uint16_t DownlinkCounter;
mluis 0:92bca02df485 227 bool RxData;
mluis 0:92bca02df485 228 uint8_t Port;
mluis 0:92bca02df485 229 uint8_t *Buffer;
mluis 0:92bca02df485 230 uint8_t BufferSize;
mluis 0:92bca02df485 231 }LoRaMacDownlinkStatus;
mluis 3:9c6f7f082151 232 volatile bool DownlinkStatusUpdated = false;
mluis 0:92bca02df485 233
mluis 0:92bca02df485 234 void SerialDisplayRefresh( void )
mluis 0:92bca02df485 235 {
mluis 3:9c6f7f082151 236 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 237
mluis 0:92bca02df485 238 SerialDisplayInit( );
mluis 0:92bca02df485 239 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:92bca02df485 240
mluis 0:92bca02df485 241 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:92bca02df485 242 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 3:9c6f7f082151 243 SerialDisplayUpdateDevAddr( DevAddr );
mluis 0:92bca02df485 244 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:92bca02df485 245 SerialDisplayUpdateKey( 13, AppSKey );
mluis 7:3173f0508a98 246 #endif
mluis 0:92bca02df485 247 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:92bca02df485 248 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:92bca02df485 249 SerialDisplayUpdateKey( 7, AppKey );
mluis 3:9c6f7f082151 250
mluis 3:9c6f7f082151 251 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 252 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 253 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:92bca02df485 254
mluis 0:92bca02df485 255 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 1:352f608c3337 256 #if defined( USE_BAND_868 )
mluis 0:92bca02df485 257 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 1:352f608c3337 258 #else
mluis 1:352f608c3337 259 SerialDisplayUpdateDutyCycle( false );
mluis 1:352f608c3337 260 #endif
mluis 0:92bca02df485 261 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:92bca02df485 262
mluis 0:92bca02df485 263 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:92bca02df485 264 }
mluis 0:92bca02df485 265
mluis 0:92bca02df485 266 void SerialRxProcess( void )
mluis 0:92bca02df485 267 {
mluis 0:92bca02df485 268 if( SerialDisplayReadable( ) == true )
mluis 0:92bca02df485 269 {
mluis 0:92bca02df485 270 switch( SerialDisplayGetChar( ) )
mluis 0:92bca02df485 271 {
mluis 0:92bca02df485 272 case 'R':
mluis 0:92bca02df485 273 case 'r':
mluis 0:92bca02df485 274 // Refresh Serial screen
mluis 0:92bca02df485 275 SerialDisplayRefresh( );
mluis 0:92bca02df485 276 break;
mluis 0:92bca02df485 277 default:
mluis 0:92bca02df485 278 break;
mluis 0:92bca02df485 279 }
mluis 0:92bca02df485 280 }
mluis 0:92bca02df485 281 }
mluis 0:92bca02df485 282
mluis 0:92bca02df485 283 /*!
mluis 3:9c6f7f082151 284 * \brief Prepares the payload of the frame
mluis 0:92bca02df485 285 */
mluis 0:92bca02df485 286 static void PrepareTxFrame( uint8_t port )
mluis 0:92bca02df485 287 {
mluis 1:352f608c3337 288 switch( port )
mluis 1:352f608c3337 289 {
nimita23 10:715b2204399b 290 case 10:
nimita23 10:715b2204399b 291 {
nimita23 12:bf1efd688cc8 292 static uint32_t counter = ( uint32_t )480;
nimita23 12:bf1efd688cc8 293 AppData[0] = counter>>24 & 0xFF;
nimita23 12:bf1efd688cc8 294 AppData[1] = counter>>16 & 0xFF;
nimita23 12:bf1efd688cc8 295 AppData[2] = counter>>8 & 0xFF;
nimita23 12:bf1efd688cc8 296 AppData[3] = counter & 0xFF;
nimita23 12:bf1efd688cc8 297
nimita23 12:bf1efd688cc8 298 counter ++;
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;
nimita23 13:779fef3e4573 576 myled = 1;
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 }