LoRa send is causing hang-up when a gps fix is acquired

Dependencies:   mbed LoRaWAN-lib_publishing_testing_UART_bug SingleFrequencyLora

Fork of simple-demo-76_revised_20171113_copy by Rishin Amin

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