Implemented LED Indicator Patterns

Dependencies:   mbed

Committer:
mluis
Date:
Tue Jul 05 15:01:57 2016 +0000
Revision:
7:3173f0508a98
Parent:
5:1e9f6a365854
Child:
9:ee9dcbb9708d
Synchronized example application with GitHub LoRaMac-node examples; Updated mbed and LoRaWAN-lib libraries

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