Lora_with_GPS integration code - program hangs after a few transmissions

Dependencies:   mbed LoRaWAN-lib SingleFrequencyLora

Fork of simple-demo-76_revised_20171113 by Christopher De Bank

Committer:
mluis
Date:
Thu Jan 07 15:14:22 2016 +0000
Revision:
3:9c6f7f082151
Parent:
1:352f608c3337
Child:
4:00cf2370c99d
Updated according to https://github.com/Lora-net/LoRaMac-node git revision a2226468d470eceb251338e1acfb24cfd121effa

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