new code ani

Dependencies:   mbed

Committer:
amirchaudhary
Date:
Mon Dec 02 16:01:33 2019 +0000
Revision:
15:6d177c1f6a15
Parent:
14:fd17c5190f79
Ani coe

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 0:92bca02df485 1 /*
mluis 0:92bca02df485 2 / _____) _ | |
mluis 0:92bca02df485 3 ( (____ _____ ____ _| |_ _____ ____| |__
mluis 0:92bca02df485 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
mluis 0:92bca02df485 5 _____) ) ____| | | || |_| ____( (___| | | |
mluis 0:92bca02df485 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
mluis 0:92bca02df485 7 (C)2015 Semtech
mluis 0:92bca02df485 8
mluis 0:92bca02df485 9 Description: LoRaMac classA device implementation
mluis 0:92bca02df485 10
mluis 0:92bca02df485 11 License: Revised BSD License, see LICENSE.TXT file include in the project
mluis 0:92bca02df485 12
mluis 0:92bca02df485 13 Maintainer: Miguel Luis and Gregory Cristian
mluis 0:92bca02df485 14 */
mluis 0:92bca02df485 15 #include "mbed.h"
mluis 0:92bca02df485 16 #include "board.h"
mluis 0:92bca02df485 17 #include "radio.h"
mluis 0:92bca02df485 18
mluis 0:92bca02df485 19 #include "LoRaMac.h"
mluis 9:ee9dcbb9708d 20 #include "Commissioning.h"
mluis 0:92bca02df485 21 #include "SerialDisplay.h"
mluis 0:92bca02df485 22
mluis 0:92bca02df485 23 /*!
mluis 9:ee9dcbb9708d 24 * Defines the application data transmission duty cycle. 5s, value in [ms].
mluis 0:92bca02df485 25 */
mluis 9:ee9dcbb9708d 26 #define APP_TX_DUTYCYCLE 5000
mluis 1:352f608c3337 27
mluis 1:352f608c3337 28 /*!
mluis 1:352f608c3337 29 * Defines a random delay for application data transmission duty cycle. 1s,
mluis 9:ee9dcbb9708d 30 * value in [ms].
mluis 1:352f608c3337 31 */
mluis 9:ee9dcbb9708d 32 #define APP_TX_DUTYCYCLE_RND 1000
mluis 0:92bca02df485 33
mluis 0:92bca02df485 34 /*!
mluis 5:1e9f6a365854 35 * Default datarate
mluis 3:9c6f7f082151 36 */
mluis 3:9c6f7f082151 37 #define LORAWAN_DEFAULT_DATARATE DR_0
mluis 3:9c6f7f082151 38
mluis 3:9c6f7f082151 39 /*!
mluis 0:92bca02df485 40 * LoRaWAN confirmed messages
mluis 0:92bca02df485 41 */
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 9:ee9dcbb9708d 60 #define LORAWAN_DUTYCYCLE_ON false
mluis 0:92bca02df485 61
mluis 5:1e9f6a365854 62 #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1
mluis 5:1e9f6a365854 63
mluis 9:ee9dcbb9708d 64 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 5:1e9f6a365854 65
mluis 5:1e9f6a365854 66 #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 67 #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 68 #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 69 #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 70 #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 71 #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 }
mluis 5:1e9f6a365854 72 #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 }
mluis 5:1e9f6a365854 73
mluis 5:1e9f6a365854 74 #endif
mluis 5:1e9f6a365854 75
mluis 1:352f608c3337 76 #endif
mluis 1:352f608c3337 77
mluis 0:92bca02df485 78 /*!
mluis 0:92bca02df485 79 * LoRaWAN application port
mluis 0:92bca02df485 80 */
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 */
amirchaudhary 15:6d177c1f6a15 86 #define LORAWAN_APP_DATA_SIZE 41
mluis 0:92bca02df485 87
mluis 0:92bca02df485 88
mluis 0:92bca02df485 89
mluis 0:92bca02df485 90 static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
mluis 0:92bca02df485 91 static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
mluis 0:92bca02df485 92 static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
mluis 0:92bca02df485 93
mluis 7:3173f0508a98 94 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:92bca02df485 95
mluis 0:92bca02df485 96 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mluis 0:92bca02df485 97 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
mluis 0:92bca02df485 98
mluis 3:9c6f7f082151 99 /*!
mluis 3:9c6f7f082151 100 * Device address
mluis 3:9c6f7f082151 101 */
mluis 3:9c6f7f082151 102 static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
mluis 0:92bca02df485 103
mluis 3:9c6f7f082151 104 #endif
mluis 0:92bca02df485 105
mluis 0:92bca02df485 106 /*!
mluis 1:352f608c3337 107 * Application port
mluis 1:352f608c3337 108 */
mluis 1:352f608c3337 109 static uint8_t AppPort = LORAWAN_APP_PORT;
mluis 1:352f608c3337 110
mluis 1:352f608c3337 111 /*!
mluis 1:352f608c3337 112 * User application data size
mluis 1:352f608c3337 113 */
mluis 1:352f608c3337 114 static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 1:352f608c3337 115
mluis 1:352f608c3337 116 /*!
mluis 1:352f608c3337 117 * User application data buffer size
mluis 1:352f608c3337 118 */
mluis 1:352f608c3337 119 #define LORAWAN_APP_DATA_MAX_SIZE 64
mluis 1:352f608c3337 120
mluis 1:352f608c3337 121 /*!
mluis 0:92bca02df485 122 * User application data
mluis 0:92bca02df485 123 */
mluis 1:352f608c3337 124 static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
mluis 1:352f608c3337 125
mluis 1:352f608c3337 126 /*!
mluis 1:352f608c3337 127 * Indicates if the node is sending confirmed or unconfirmed messages
mluis 1:352f608c3337 128 */
mluis 1:352f608c3337 129 static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:92bca02df485 130
mluis 0:92bca02df485 131 /*!
mluis 0:92bca02df485 132 * Defines the application data transmission duty cycle
mluis 0:92bca02df485 133 */
mluis 0:92bca02df485 134 static uint32_t TxDutyCycleTime;
mluis 0:92bca02df485 135
mluis 1:352f608c3337 136 /*!
mluis 1:352f608c3337 137 * Timer to handle the application data transmission duty cycle
mluis 1:352f608c3337 138 */
mluis 1:352f608c3337 139 static TimerEvent_t TxNextPacketTimer;
mluis 0:92bca02df485 140
mluis 3:9c6f7f082151 141 /*!
mluis 0:92bca02df485 142 * Indicates if a new packet can be sent
mluis 0:92bca02df485 143 */
mluis 3:9c6f7f082151 144 static bool NextTx = true;
mluis 0:92bca02df485 145
mluis 3:9c6f7f082151 146 /*!
mluis 3:9c6f7f082151 147 * Device states
mluis 3:9c6f7f082151 148 */
mluis 9:ee9dcbb9708d 149 static enum eDeviceState
mluis 3:9c6f7f082151 150 {
mluis 3:9c6f7f082151 151 DEVICE_STATE_INIT,
mluis 3:9c6f7f082151 152 DEVICE_STATE_JOIN,
mluis 3:9c6f7f082151 153 DEVICE_STATE_SEND,
mluis 3:9c6f7f082151 154 DEVICE_STATE_CYCLE,
mluis 3:9c6f7f082151 155 DEVICE_STATE_SLEEP
mluis 3:9c6f7f082151 156 }DeviceState;
mluis 0:92bca02df485 157
mluis 3:9c6f7f082151 158 /*!
mluis 3:9c6f7f082151 159 * LoRaWAN compliance tests support data
mluis 3:9c6f7f082151 160 */
mluis 3:9c6f7f082151 161 struct ComplianceTest_s
mluis 3:9c6f7f082151 162 {
mluis 3:9c6f7f082151 163 bool Running;
mluis 3:9c6f7f082151 164 uint8_t State;
mluis 3:9c6f7f082151 165 bool IsTxConfirmed;
mluis 3:9c6f7f082151 166 uint8_t AppPort;
mluis 3:9c6f7f082151 167 uint8_t AppDataSize;
mluis 3:9c6f7f082151 168 uint8_t *AppDataBuffer;
mluis 3:9c6f7f082151 169 uint16_t DownLinkCounter;
mluis 3:9c6f7f082151 170 bool LinkCheck;
mluis 3:9c6f7f082151 171 uint8_t DemodMargin;
mluis 3:9c6f7f082151 172 uint8_t NbGateways;
mluis 3:9c6f7f082151 173 }ComplianceTest;
mluis 0:92bca02df485 174
mluis 3:9c6f7f082151 175 /*
mluis 3:9c6f7f082151 176 * SerialDisplay managment variables
mluis 3:9c6f7f082151 177 */
mluis 1:352f608c3337 178
mluis 3:9c6f7f082151 179 /*!
mluis 3:9c6f7f082151 180 * Indicates if the MAC layer network join status has changed.
mluis 3:9c6f7f082151 181 */
mluis 3:9c6f7f082151 182 static bool IsNetworkJoinedStatusUpdate = false;
mluis 3:9c6f7f082151 183
mluis 3:9c6f7f082151 184 /*!
mluis 3:9c6f7f082151 185 * Strucure containing the Uplink status
mluis 3:9c6f7f082151 186 */
mluis 0:92bca02df485 187 struct sLoRaMacUplinkStatus
mluis 0:92bca02df485 188 {
mluis 0:92bca02df485 189 uint8_t Acked;
mluis 0:92bca02df485 190 int8_t Datarate;
mluis 0:92bca02df485 191 uint16_t UplinkCounter;
mluis 0:92bca02df485 192 uint8_t Port;
mluis 0:92bca02df485 193 uint8_t *Buffer;
mluis 0:92bca02df485 194 uint8_t BufferSize;
mluis 0:92bca02df485 195 }LoRaMacUplinkStatus;
mluis 3:9c6f7f082151 196 volatile bool UplinkStatusUpdated = false;
mluis 0:92bca02df485 197
mluis 3:9c6f7f082151 198 /*!
mluis 3:9c6f7f082151 199 * Strucure containing the Downlink status
mluis 3:9c6f7f082151 200 */
mluis 0:92bca02df485 201 struct sLoRaMacDownlinkStatus
mluis 0:92bca02df485 202 {
mluis 0:92bca02df485 203 int16_t Rssi;
mluis 0:92bca02df485 204 int8_t Snr;
mluis 0:92bca02df485 205 uint16_t DownlinkCounter;
mluis 0:92bca02df485 206 bool RxData;
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 }LoRaMacDownlinkStatus;
mluis 3:9c6f7f082151 211 volatile bool DownlinkStatusUpdated = false;
mluis 0:92bca02df485 212
uss1994 10:4e528716aa7a 213
uss1994 10:4e528716aa7a 214 // Application Globals
amirchaudhary 15:6d177c1f6a15 215 DigitalOut myled(D7);
uss1994 10:4e528716aa7a 216
uss1994 10:4e528716aa7a 217
uss1994 10:4e528716aa7a 218
mluis 0:92bca02df485 219 void SerialDisplayRefresh( void )
mluis 0:92bca02df485 220 {
mluis 3:9c6f7f082151 221 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 222
mluis 0:92bca02df485 223 SerialDisplayInit( );
mluis 0:92bca02df485 224 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:92bca02df485 225
mluis 0:92bca02df485 226 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:92bca02df485 227 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 3:9c6f7f082151 228 SerialDisplayUpdateDevAddr( DevAddr );
mluis 0:92bca02df485 229 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:92bca02df485 230 SerialDisplayUpdateKey( 13, AppSKey );
mluis 7:3173f0508a98 231 #endif
mluis 0:92bca02df485 232 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:92bca02df485 233 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:92bca02df485 234 SerialDisplayUpdateKey( 7, AppKey );
mluis 3:9c6f7f082151 235
mluis 3:9c6f7f082151 236 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 237 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 238 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:92bca02df485 239
mluis 0:92bca02df485 240 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 1:352f608c3337 241 #if defined( USE_BAND_868 )
mluis 0:92bca02df485 242 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 1:352f608c3337 243 #else
mluis 1:352f608c3337 244 SerialDisplayUpdateDutyCycle( false );
mluis 1:352f608c3337 245 #endif
mluis 0:92bca02df485 246 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:92bca02df485 247 }
mluis 0:92bca02df485 248
mluis 0:92bca02df485 249 void SerialRxProcess( void )
mluis 0:92bca02df485 250 {
mluis 0:92bca02df485 251 if( SerialDisplayReadable( ) == true )
mluis 0:92bca02df485 252 {
mluis 0:92bca02df485 253 switch( SerialDisplayGetChar( ) )
mluis 0:92bca02df485 254 {
mluis 0:92bca02df485 255 case 'R':
mluis 0:92bca02df485 256 case 'r':
mluis 0:92bca02df485 257 // Refresh Serial screen
mluis 0:92bca02df485 258 SerialDisplayRefresh( );
mluis 0:92bca02df485 259 break;
mluis 0:92bca02df485 260 default:
mluis 0:92bca02df485 261 break;
mluis 0:92bca02df485 262 }
mluis 0:92bca02df485 263 }
mluis 0:92bca02df485 264 }
mluis 0:92bca02df485 265
mluis 0:92bca02df485 266 /*!
mluis 3:9c6f7f082151 267 * \brief Prepares the payload of the frame
mluis 0:92bca02df485 268 */
mluis 0:92bca02df485 269 static void PrepareTxFrame( uint8_t port )
mluis 0:92bca02df485 270 {
mluis 1:352f608c3337 271 switch( port )
mluis 1:352f608c3337 272 {
mluis 1:352f608c3337 273 case 15:
mluis 1:352f608c3337 274 {
uss1994 10:4e528716aa7a 275 AppData[0] = 0;
mluis 1:352f608c3337 276 if( IsTxConfirmed == true )
mluis 1:352f608c3337 277 {
mluis 1:352f608c3337 278 AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8;
mluis 1:352f608c3337 279 AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter;
mluis 1:352f608c3337 280 AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8;
mluis 1:352f608c3337 281 AppData[4] = LoRaMacDownlinkStatus.Rssi;
mluis 1:352f608c3337 282 AppData[5] = LoRaMacDownlinkStatus.Snr;
mluis 1:352f608c3337 283 }
mluis 1:352f608c3337 284 }
mluis 1:352f608c3337 285 break;
mluis 1:352f608c3337 286 case 224:
mluis 3:9c6f7f082151 287 if( ComplianceTest.LinkCheck == true )
mluis 1:352f608c3337 288 {
mluis 3:9c6f7f082151 289 ComplianceTest.LinkCheck = false;
mluis 1:352f608c3337 290 AppDataSize = 3;
mluis 1:352f608c3337 291 AppData[0] = 5;
mluis 3:9c6f7f082151 292 AppData[1] = ComplianceTest.DemodMargin;
mluis 3:9c6f7f082151 293 AppData[2] = ComplianceTest.NbGateways;
mluis 3:9c6f7f082151 294 ComplianceTest.State = 1;
mluis 1:352f608c3337 295 }
mluis 1:352f608c3337 296 else
mluis 1:352f608c3337 297 {
mluis 3:9c6f7f082151 298 switch( ComplianceTest.State )
mluis 1:352f608c3337 299 {
mluis 1:352f608c3337 300 case 4:
mluis 3:9c6f7f082151 301 ComplianceTest.State = 1;
mluis 1:352f608c3337 302 break;
mluis 1:352f608c3337 303 case 1:
mluis 1:352f608c3337 304 AppDataSize = 2;
mluis 3:9c6f7f082151 305 AppData[0] = ComplianceTest.DownLinkCounter >> 8;
mluis 3:9c6f7f082151 306 AppData[1] = ComplianceTest.DownLinkCounter;
mluis 1:352f608c3337 307 break;
mluis 1:352f608c3337 308 }
mluis 1:352f608c3337 309 }
mluis 1:352f608c3337 310 break;
mluis 3:9c6f7f082151 311 default:
mluis 3:9c6f7f082151 312 break;
mluis 1:352f608c3337 313 }
mluis 0:92bca02df485 314 }
mluis 0:92bca02df485 315
mluis 3:9c6f7f082151 316 /*!
mluis 3:9c6f7f082151 317 * \brief Prepares the payload of the frame
mluis 3:9c6f7f082151 318 *
mluis 3:9c6f7f082151 319 * \retval [0: frame could be send, 1: error]
mluis 3:9c6f7f082151 320 */
mluis 0:92bca02df485 321 static bool SendFrame( void )
mluis 0:92bca02df485 322 {
mluis 3:9c6f7f082151 323 McpsReq_t mcpsReq;
mluis 3:9c6f7f082151 324 LoRaMacTxInfo_t txInfo;
mluis 9:ee9dcbb9708d 325
mluis 3:9c6f7f082151 326 if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
mluis 1:352f608c3337 327 {
mluis 3:9c6f7f082151 328 // Send empty frame in order to flush MAC commands
mluis 3:9c6f7f082151 329 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 3:9c6f7f082151 330 mcpsReq.Req.Unconfirmed.fBuffer = NULL;
mluis 3:9c6f7f082151 331 mcpsReq.Req.Unconfirmed.fBufferSize = 0;
mluis 3:9c6f7f082151 332 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 5:1e9f6a365854 333
mluis 3:9c6f7f082151 334 LoRaMacUplinkStatus.Acked = false;
mluis 3:9c6f7f082151 335 LoRaMacUplinkStatus.Port = 0;
mluis 3:9c6f7f082151 336 LoRaMacUplinkStatus.Buffer = NULL;
mluis 3:9c6f7f082151 337 LoRaMacUplinkStatus.BufferSize = 0;
mluis 3:9c6f7f082151 338 SerialDisplayUpdateFrameType( false );
mluis 1:352f608c3337 339 }
mluis 1:352f608c3337 340 else
mluis 1:352f608c3337 341 {
mluis 3:9c6f7f082151 342 LoRaMacUplinkStatus.Acked = false;
mluis 3:9c6f7f082151 343 LoRaMacUplinkStatus.Port = AppPort;
mluis 3:9c6f7f082151 344 LoRaMacUplinkStatus.Buffer = AppData;
mluis 3:9c6f7f082151 345 LoRaMacUplinkStatus.BufferSize = AppDataSize;
mluis 3:9c6f7f082151 346 SerialDisplayUpdateFrameType( IsTxConfirmed );
mluis 3:9c6f7f082151 347
mluis 3:9c6f7f082151 348 if( IsTxConfirmed == false )
mluis 3:9c6f7f082151 349 {
mluis 3:9c6f7f082151 350 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 3:9c6f7f082151 351 mcpsReq.Req.Unconfirmed.fPort = AppPort;
mluis 3:9c6f7f082151 352 mcpsReq.Req.Unconfirmed.fBuffer = AppData;
mluis 3:9c6f7f082151 353 mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
mluis 3:9c6f7f082151 354 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 3:9c6f7f082151 355 }
mluis 3:9c6f7f082151 356 else
mluis 3:9c6f7f082151 357 {
mluis 3:9c6f7f082151 358 mcpsReq.Type = MCPS_CONFIRMED;
mluis 3:9c6f7f082151 359 mcpsReq.Req.Confirmed.fPort = AppPort;
mluis 3:9c6f7f082151 360 mcpsReq.Req.Confirmed.fBuffer = AppData;
mluis 3:9c6f7f082151 361 mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
mluis 4:00cf2370c99d 362 mcpsReq.Req.Confirmed.NbTrials = 8;
mluis 3:9c6f7f082151 363 mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 3:9c6f7f082151 364 }
mluis 1:352f608c3337 365 }
mluis 1:352f608c3337 366
mluis 3:9c6f7f082151 367 if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK )
mluis 0:92bca02df485 368 {
mluis 0:92bca02df485 369 return false;
mluis 0:92bca02df485 370 }
mluis 3:9c6f7f082151 371 return true;
mluis 0:92bca02df485 372 }
mluis 0:92bca02df485 373
mluis 0:92bca02df485 374 /*!
mluis 0:92bca02df485 375 * \brief Function executed on TxNextPacket Timeout event
mluis 0:92bca02df485 376 */
mluis 0:92bca02df485 377 static void OnTxNextPacketTimerEvent( void )
mluis 0:92bca02df485 378 {
mluis 3:9c6f7f082151 379 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 380 LoRaMacStatus_t status;
mluis 3:9c6f7f082151 381
mluis 0:92bca02df485 382 TimerStop( &TxNextPacketTimer );
mluis 3:9c6f7f082151 383
mluis 3:9c6f7f082151 384 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 385 status = LoRaMacMibGetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 386
mluis 3:9c6f7f082151 387 if( status == LORAMAC_STATUS_OK )
mluis 3:9c6f7f082151 388 {
mluis 3:9c6f7f082151 389 if( mibReq.Param.IsNetworkJoined == true )
mluis 3:9c6f7f082151 390 {
mluis 3:9c6f7f082151 391 DeviceState = DEVICE_STATE_SEND;
mluis 3:9c6f7f082151 392 NextTx = true;
mluis 3:9c6f7f082151 393 }
mluis 3:9c6f7f082151 394 else
mluis 3:9c6f7f082151 395 {
mluis 3:9c6f7f082151 396 DeviceState = DEVICE_STATE_JOIN;
mluis 3:9c6f7f082151 397 }
mluis 3:9c6f7f082151 398 }
mluis 0:92bca02df485 399 }
mluis 0:92bca02df485 400
mluis 0:92bca02df485 401
mluis 0:92bca02df485 402 /*!
mluis 3:9c6f7f082151 403 * \brief MCPS-Confirm event function
mluis 3:9c6f7f082151 404 *
mluis 5:1e9f6a365854 405 * \param [IN] mcpsConfirm - Pointer to the confirm structure,
mluis 3:9c6f7f082151 406 * containing confirm attributes.
mluis 0:92bca02df485 407 */
mluis 5:1e9f6a365854 408 static void McpsConfirm( McpsConfirm_t *mcpsConfirm )
mluis 0:92bca02df485 409 {
mluis 5:1e9f6a365854 410 if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:92bca02df485 411 {
mluis 5:1e9f6a365854 412 switch( mcpsConfirm->McpsRequest )
mluis 3:9c6f7f082151 413 {
mluis 3:9c6f7f082151 414 case MCPS_UNCONFIRMED:
mluis 3:9c6f7f082151 415 {
mluis 3:9c6f7f082151 416 // Check Datarate
mluis 3:9c6f7f082151 417 // Check TxPower
mluis 3:9c6f7f082151 418 break;
mluis 3:9c6f7f082151 419 }
mluis 3:9c6f7f082151 420 case MCPS_CONFIRMED:
mluis 3:9c6f7f082151 421 {
mluis 3:9c6f7f082151 422 // Check Datarate
mluis 3:9c6f7f082151 423 // Check TxPower
mluis 3:9c6f7f082151 424 // Check AckReceived
mluis 5:1e9f6a365854 425 // Check NbTrials
mluis 5:1e9f6a365854 426 LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived;
mluis 3:9c6f7f082151 427 break;
mluis 3:9c6f7f082151 428 }
mluis 3:9c6f7f082151 429 case MCPS_PROPRIETARY:
mluis 3:9c6f7f082151 430 {
mluis 3:9c6f7f082151 431 break;
mluis 3:9c6f7f082151 432 }
mluis 3:9c6f7f082151 433 default:
mluis 3:9c6f7f082151 434 break;
mluis 3:9c6f7f082151 435 }
mluis 5:1e9f6a365854 436 LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate;
mluis 5:1e9f6a365854 437 LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter;
mluis 5:1e9f6a365854 438
mluis 3:9c6f7f082151 439 UplinkStatusUpdated = true;
mluis 3:9c6f7f082151 440 }
mluis 3:9c6f7f082151 441 NextTx = true;
mluis 3:9c6f7f082151 442 }
mluis 3:9c6f7f082151 443
mluis 3:9c6f7f082151 444 /*!
mluis 3:9c6f7f082151 445 * \brief MCPS-Indication event function
mluis 3:9c6f7f082151 446 *
mluis 5:1e9f6a365854 447 * \param [IN] mcpsIndication - Pointer to the indication structure,
mluis 3:9c6f7f082151 448 * containing indication attributes.
mluis 3:9c6f7f082151 449 */
mluis 5:1e9f6a365854 450 static void McpsIndication( McpsIndication_t *mcpsIndication )
mluis 3:9c6f7f082151 451 {
mluis 5:1e9f6a365854 452 if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
mluis 3:9c6f7f082151 453 {
mluis 3:9c6f7f082151 454 return;
mluis 3:9c6f7f082151 455 }
mluis 3:9c6f7f082151 456
mluis 5:1e9f6a365854 457 switch( mcpsIndication->McpsIndication )
mluis 3:9c6f7f082151 458 {
mluis 3:9c6f7f082151 459 case MCPS_UNCONFIRMED:
mluis 3:9c6f7f082151 460 {
mluis 3:9c6f7f082151 461 break;
mluis 3:9c6f7f082151 462 }
mluis 3:9c6f7f082151 463 case MCPS_CONFIRMED:
mluis 3:9c6f7f082151 464 {
mluis 3:9c6f7f082151 465 break;
mluis 3:9c6f7f082151 466 }
mluis 3:9c6f7f082151 467 case MCPS_PROPRIETARY:
mluis 3:9c6f7f082151 468 {
mluis 3:9c6f7f082151 469 break;
mluis 3:9c6f7f082151 470 }
mluis 3:9c6f7f082151 471 case MCPS_MULTICAST:
mluis 3:9c6f7f082151 472 {
mluis 3:9c6f7f082151 473 break;
mluis 3:9c6f7f082151 474 }
mluis 3:9c6f7f082151 475 default:
mluis 3:9c6f7f082151 476 break;
mluis 3:9c6f7f082151 477 }
mluis 3:9c6f7f082151 478
mluis 3:9c6f7f082151 479 // Check Multicast
mluis 3:9c6f7f082151 480 // Check Port
mluis 3:9c6f7f082151 481 // Check Datarate
mluis 3:9c6f7f082151 482 // Check FramePending
mluis 3:9c6f7f082151 483 // Check Buffer
mluis 3:9c6f7f082151 484 // Check BufferSize
mluis 3:9c6f7f082151 485 // Check Rssi
mluis 3:9c6f7f082151 486 // Check Snr
mluis 3:9c6f7f082151 487 // Check RxSlot
mluis 5:1e9f6a365854 488 LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi;
mluis 5:1e9f6a365854 489 if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
mluis 3:9c6f7f082151 490 {
mluis 3:9c6f7f082151 491 // Invert and divide by 4
mluis 5:1e9f6a365854 492 LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2;
mluis 3:9c6f7f082151 493 LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
mluis 0:92bca02df485 494 }
mluis 0:92bca02df485 495 else
mluis 0:92bca02df485 496 {
mluis 3:9c6f7f082151 497 // Divide by 4
mluis 5:1e9f6a365854 498 LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2;
mluis 3:9c6f7f082151 499 }
mluis 3:9c6f7f082151 500 LoRaMacDownlinkStatus.DownlinkCounter++;
mluis 5:1e9f6a365854 501 LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData;
mluis 5:1e9f6a365854 502 LoRaMacDownlinkStatus.Port = mcpsIndication->Port;
mluis 5:1e9f6a365854 503 LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer;
mluis 5:1e9f6a365854 504 LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize;
mluis 3:9c6f7f082151 505
mluis 3:9c6f7f082151 506 if( ComplianceTest.Running == true )
mluis 3:9c6f7f082151 507 {
mluis 3:9c6f7f082151 508 ComplianceTest.DownLinkCounter++;
mluis 3:9c6f7f082151 509 }
mluis 0:92bca02df485 510
mluis 5:1e9f6a365854 511 if( mcpsIndication->RxData == true )
mluis 3:9c6f7f082151 512 {
mluis 5:1e9f6a365854 513 switch( mcpsIndication->Port )
mluis 0:92bca02df485 514 {
mluis 3:9c6f7f082151 515 case 1: // The application LED can be controlled on port 1 or 2
mluis 3:9c6f7f082151 516 case 2:
mluis 3:9c6f7f082151 517 break;
mluis 3:9c6f7f082151 518 case 224:
mluis 3:9c6f7f082151 519 if( ComplianceTest.Running == false )
mluis 3:9c6f7f082151 520 {
mluis 3:9c6f7f082151 521 // Check compliance test enable command (i)
mluis 5:1e9f6a365854 522 if( ( mcpsIndication->BufferSize == 4 ) &&
mluis 5:1e9f6a365854 523 ( mcpsIndication->Buffer[0] == 0x01 ) &&
mluis 5:1e9f6a365854 524 ( mcpsIndication->Buffer[1] == 0x01 ) &&
mluis 5:1e9f6a365854 525 ( mcpsIndication->Buffer[2] == 0x01 ) &&
mluis 5:1e9f6a365854 526 ( mcpsIndication->Buffer[3] == 0x01 ) )
mluis 1:352f608c3337 527 {
mluis 3:9c6f7f082151 528 IsTxConfirmed = false;
mluis 3:9c6f7f082151 529 AppPort = 224;
mluis 3:9c6f7f082151 530 AppDataSize = 2;
mluis 3:9c6f7f082151 531 ComplianceTest.DownLinkCounter = 0;
mluis 3:9c6f7f082151 532 ComplianceTest.LinkCheck = false;
mluis 3:9c6f7f082151 533 ComplianceTest.DemodMargin = 0;
mluis 3:9c6f7f082151 534 ComplianceTest.NbGateways = 0;
mluis 3:9c6f7f082151 535 ComplianceTest.Running = true;
mluis 3:9c6f7f082151 536 ComplianceTest.State = 1;
mluis 9:ee9dcbb9708d 537
mluis 3:9c6f7f082151 538 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 539 mibReq.Type = MIB_ADR;
mluis 3:9c6f7f082151 540 mibReq.Param.AdrEnable = true;
mluis 3:9c6f7f082151 541 LoRaMacMibSetRequestConfirm( &mibReq );
amirchaudhary 15:6d177c1f6a15 542
amirchaudhary 15:6d177c1f6a15 543 // Limiting to just 2nd subband of 8 channels
amirchaudhary 15:6d177c1f6a15 544 static uint16_t GatewayChannelsMask[] = {0xFF00, 0x0000, 0x0000, 0x0000, 0x0002, 0x0000};
amirchaudhary 15:6d177c1f6a15 545 mibReq.Type = MIB_CHANNELS_DEFAULT_MASK;
amirchaudhary 15:6d177c1f6a15 546 mibReq.Param.ChannelsDefaultMask = GatewayChannelsMask;
amirchaudhary 15:6d177c1f6a15 547 LoRaMacMibSetRequestConfirm( &mibReq );
amirchaudhary 15:6d177c1f6a15 548
amirchaudhary 15:6d177c1f6a15 549 mibReq.Type = MIB_CHANNELS_MASK;
amirchaudhary 15:6d177c1f6a15 550 mibReq.Param.ChannelsMask = GatewayChannelsMask;
amirchaudhary 15:6d177c1f6a15 551 LoRaMacMibSetRequestConfirm( &mibReq );
amirchaudhary 15:6d177c1f6a15 552
amirchaudhary 15:6d177c1f6a15 553
mluis 3:9c6f7f082151 554
mluis 3:9c6f7f082151 555 #if defined( USE_BAND_868 )
mluis 3:9c6f7f082151 556 LoRaMacTestSetDutyCycleOn( false );
mluis 3:9c6f7f082151 557 #endif
mluis 1:352f608c3337 558 }
mluis 1:352f608c3337 559 }
mluis 0:92bca02df485 560 else
mluis 0:92bca02df485 561 {
mluis 5:1e9f6a365854 562 ComplianceTest.State = mcpsIndication->Buffer[0];
mluis 3:9c6f7f082151 563 switch( ComplianceTest.State )
mluis 3:9c6f7f082151 564 {
mluis 3:9c6f7f082151 565 case 0: // Check compliance test disable command (ii)
mluis 3:9c6f7f082151 566 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 3:9c6f7f082151 567 AppPort = LORAWAN_APP_PORT;
mluis 3:9c6f7f082151 568 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 3:9c6f7f082151 569 ComplianceTest.DownLinkCounter = 0;
mluis 3:9c6f7f082151 570 ComplianceTest.Running = false;
mluis 9:ee9dcbb9708d 571
mluis 3:9c6f7f082151 572 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 573 mibReq.Type = MIB_ADR;
mluis 3:9c6f7f082151 574 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 3:9c6f7f082151 575 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 576 #if defined( USE_BAND_868 )
mluis 3:9c6f7f082151 577 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 3:9c6f7f082151 578 #endif
mluis 3:9c6f7f082151 579 break;
mluis 3:9c6f7f082151 580 case 1: // (iii, iv)
mluis 3:9c6f7f082151 581 AppDataSize = 2;
mluis 3:9c6f7f082151 582 break;
mluis 3:9c6f7f082151 583 case 2: // Enable confirmed messages (v)
mluis 3:9c6f7f082151 584 IsTxConfirmed = true;
mluis 3:9c6f7f082151 585 ComplianceTest.State = 1;
mluis 3:9c6f7f082151 586 break;
mluis 3:9c6f7f082151 587 case 3: // Disable confirmed messages (vi)
mluis 3:9c6f7f082151 588 IsTxConfirmed = false;
mluis 3:9c6f7f082151 589 ComplianceTest.State = 1;
mluis 3:9c6f7f082151 590 break;
mluis 3:9c6f7f082151 591 case 4: // (vii)
mluis 5:1e9f6a365854 592 AppDataSize = mcpsIndication->BufferSize;
mluis 3:9c6f7f082151 593
mluis 3:9c6f7f082151 594 AppData[0] = 4;
mluis 3:9c6f7f082151 595 for( uint8_t i = 1; i < AppDataSize; i++ )
mluis 3:9c6f7f082151 596 {
mluis 5:1e9f6a365854 597 AppData[i] = mcpsIndication->Buffer[i] + 1;
mluis 3:9c6f7f082151 598 }
mluis 3:9c6f7f082151 599 break;
mluis 3:9c6f7f082151 600 case 5: // (viii)
mluis 3:9c6f7f082151 601 {
mluis 3:9c6f7f082151 602 MlmeReq_t mlmeReq;
mluis 3:9c6f7f082151 603 mlmeReq.Type = MLME_LINK_CHECK;
mluis 3:9c6f7f082151 604 LoRaMacMlmeRequest( &mlmeReq );
mluis 3:9c6f7f082151 605 }
mluis 3:9c6f7f082151 606 break;
mluis 7:3173f0508a98 607 case 6: // (ix)
mluis 7:3173f0508a98 608 {
mluis 7:3173f0508a98 609 MlmeReq_t mlmeReq;
mluis 7:3173f0508a98 610
mluis 9:ee9dcbb9708d 611 // Disable TestMode and revert back to normal operation
mluis 9:ee9dcbb9708d 612 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 9:ee9dcbb9708d 613 AppPort = LORAWAN_APP_PORT;
mluis 9:ee9dcbb9708d 614 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 9:ee9dcbb9708d 615 ComplianceTest.DownLinkCounter = 0;
mluis 9:ee9dcbb9708d 616 ComplianceTest.Running = false;
mluis 9:ee9dcbb9708d 617
mluis 9:ee9dcbb9708d 618 MibRequestConfirm_t mibReq;
mluis 9:ee9dcbb9708d 619 mibReq.Type = MIB_ADR;
mluis 9:ee9dcbb9708d 620 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 9:ee9dcbb9708d 621 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 9:ee9dcbb9708d 622 #if defined( USE_BAND_868 )
mluis 9:ee9dcbb9708d 623 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 9:ee9dcbb9708d 624 #endif
mluis 9:ee9dcbb9708d 625
mluis 7:3173f0508a98 626 mlmeReq.Type = MLME_JOIN;
mluis 7:3173f0508a98 627
mluis 7:3173f0508a98 628 mlmeReq.Req.Join.DevEui = DevEui;
mluis 7:3173f0508a98 629 mlmeReq.Req.Join.AppEui = AppEui;
mluis 7:3173f0508a98 630 mlmeReq.Req.Join.AppKey = AppKey;
mluis 9:ee9dcbb9708d 631 mlmeReq.Req.Join.NbTrials = 3;
mluis 7:3173f0508a98 632
mluis 7:3173f0508a98 633 LoRaMacMlmeRequest( &mlmeReq );
mluis 7:3173f0508a98 634 DeviceState = DEVICE_STATE_SLEEP;
mluis 7:3173f0508a98 635 }
mluis 7:3173f0508a98 636 break;
mluis 9:ee9dcbb9708d 637 case 7: // (x)
mluis 9:ee9dcbb9708d 638 {
mluis 9:ee9dcbb9708d 639 if( mcpsIndication->BufferSize == 3 )
mluis 9:ee9dcbb9708d 640 {
mluis 9:ee9dcbb9708d 641 MlmeReq_t mlmeReq;
mluis 9:ee9dcbb9708d 642 mlmeReq.Type = MLME_TXCW;
mluis 9:ee9dcbb9708d 643 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 9:ee9dcbb9708d 644 LoRaMacMlmeRequest( &mlmeReq );
mluis 9:ee9dcbb9708d 645 }
mluis 9:ee9dcbb9708d 646 else if( mcpsIndication->BufferSize == 7 )
mluis 9:ee9dcbb9708d 647 {
mluis 9:ee9dcbb9708d 648 MlmeReq_t mlmeReq;
mluis 9:ee9dcbb9708d 649 mlmeReq.Type = MLME_TXCW_1;
mluis 9:ee9dcbb9708d 650 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 9:ee9dcbb9708d 651 mlmeReq.Req.TxCw.Frequency = ( uint32_t )( ( mcpsIndication->Buffer[3] << 16 ) | ( mcpsIndication->Buffer[4] << 8 ) | mcpsIndication->Buffer[5] ) * 100;
mluis 9:ee9dcbb9708d 652 mlmeReq.Req.TxCw.Power = mcpsIndication->Buffer[6];
mluis 9:ee9dcbb9708d 653 LoRaMacMlmeRequest( &mlmeReq );
mluis 9:ee9dcbb9708d 654 }
mluis 9:ee9dcbb9708d 655 ComplianceTest.State = 1;
mluis 9:ee9dcbb9708d 656 }
mluis 9:ee9dcbb9708d 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 DownlinkStatusUpdated = true;
mluis 3:9c6f7f082151 669 }
mluis 3:9c6f7f082151 670
mluis 3:9c6f7f082151 671 /*!
mluis 3:9c6f7f082151 672 * \brief MLME-Confirm event function
mluis 3:9c6f7f082151 673 *
mluis 5:1e9f6a365854 674 * \param [IN] mlmeConfirm - Pointer to the confirm structure,
mluis 3:9c6f7f082151 675 * containing confirm attributes.
mluis 3:9c6f7f082151 676 */
mluis 5:1e9f6a365854 677 static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
mluis 3:9c6f7f082151 678 {
mluis 9:ee9dcbb9708d 679 switch( mlmeConfirm->MlmeRequest )
mluis 3:9c6f7f082151 680 {
mluis 9:ee9dcbb9708d 681 case MLME_JOIN:
mluis 3:9c6f7f082151 682 {
mluis 9:ee9dcbb9708d 683 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 3:9c6f7f082151 684 {
mluis 3:9c6f7f082151 685 // Status is OK, node has joined the network
mluis 3:9c6f7f082151 686 IsNetworkJoinedStatusUpdate = true;
mluis 7:3173f0508a98 687 DeviceState = DEVICE_STATE_SEND;
mluis 9:ee9dcbb9708d 688 }
mluis 9:ee9dcbb9708d 689 else
mluis 9:ee9dcbb9708d 690 {
mluis 9:ee9dcbb9708d 691 // Join was not successful. Try to join again
mluis 9:ee9dcbb9708d 692 DeviceState = DEVICE_STATE_JOIN;
mluis 3:9c6f7f082151 693 }
mluis 9:ee9dcbb9708d 694 break;
mluis 9:ee9dcbb9708d 695 }
mluis 9:ee9dcbb9708d 696 case MLME_LINK_CHECK:
mluis 9:ee9dcbb9708d 697 {
mluis 9:ee9dcbb9708d 698 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 3:9c6f7f082151 699 {
mluis 3:9c6f7f082151 700 // Check DemodMargin
mluis 3:9c6f7f082151 701 // Check NbGateways
mluis 3:9c6f7f082151 702 if( ComplianceTest.Running == true )
mluis 3:9c6f7f082151 703 {
mluis 3:9c6f7f082151 704 ComplianceTest.LinkCheck = true;
mluis 5:1e9f6a365854 705 ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin;
mluis 5:1e9f6a365854 706 ComplianceTest.NbGateways = mlmeConfirm->NbGateways;
mluis 3:9c6f7f082151 707 }
mluis 3:9c6f7f082151 708 }
mluis 9:ee9dcbb9708d 709 break;
mluis 3:9c6f7f082151 710 }
mluis 9:ee9dcbb9708d 711 default:
mluis 9:ee9dcbb9708d 712 break;
mluis 3:9c6f7f082151 713 }
mluis 3:9c6f7f082151 714 NextTx = true;
mluis 3:9c6f7f082151 715 UplinkStatusUpdated = true;
mluis 0:92bca02df485 716 }
mluis 0:92bca02df485 717
uss1994 10:4e528716aa7a 718 void flash_builtin() {
uss1994 10:4e528716aa7a 719 myled = 1; // turn the LED on (HIGH is the voltage level)
amirchaudhary 15:6d177c1f6a15 720 wait(2); // wait for 2 second
uss1994 10:4e528716aa7a 721 myled = 0; // turn the LED off by making the voltage LOW
amirchaudhary 15:6d177c1f6a15 722 wait(1); // wait for 1 second
uss1994 10:4e528716aa7a 723 }
uss1994 10:4e528716aa7a 724
mluis 0:92bca02df485 725 /**
mluis 0:92bca02df485 726 * Main application entry point.
mluis 0:92bca02df485 727 */
a22zsolutions 11:876331d64e0e 728 Serial pc(SERIAL_TX, SERIAL_RX,115200);
a22zsolutions 14:fd17c5190f79 729
a22zsolutions 14:fd17c5190f79 730 int sysclk_hse_pll_patch(void)
a22zsolutions 11:876331d64e0e 731 {
a22zsolutions 14:fd17c5190f79 732 RCC_ClkInitTypeDef RCC_ClkInitStruct;
a22zsolutions 14:fd17c5190f79 733 RCC_OscInitTypeDef RCC_OscInitStruct;
a22zsolutions 11:876331d64e0e 734
a22zsolutions 14:fd17c5190f79 735 HAL_StatusTypeDef status;
a22zsolutions 11:876331d64e0e 736
amirchaudhary 15:6d177c1f6a15 737 #if defined(TARGET_NUCLEO_F411RE)
amirchaudhary 15:6d177c1f6a15 738 /* Start HSE first */
amirchaudhary 15:6d177c1f6a15 739 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
amirchaudhary 15:6d177c1f6a15 740 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
amirchaudhary 15:6d177c1f6a15 741 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
amirchaudhary 15:6d177c1f6a15 742
amirchaudhary 15:6d177c1f6a15 743
amirchaudhary 15:6d177c1f6a15 744
amirchaudhary 15:6d177c1f6a15 745
amirchaudhary 15:6d177c1f6a15 746
amirchaudhary 15:6d177c1f6a15 747
amirchaudhary 15:6d177c1f6a15 748
amirchaudhary 15:6d177c1f6a15 749 #elif defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_NUCLEO_L152RE)
a22zsolutions 14:fd17c5190f79 750 /* To clear HSE_BYPASS, we need to stop HSE first */
a22zsolutions 14:fd17c5190f79 751 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
a22zsolutions 14:fd17c5190f79 752 RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
a22zsolutions 14:fd17c5190f79 753 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
amirchaudhary 15:6d177c1f6a15 754
amirchaudhary 15:6d177c1f6a15 755
amirchaudhary 15:6d177c1f6a15 756
a22zsolutions 12:48fe1f8eb613 757 #else
a22zsolutions 14:fd17c5190f79 758 #error "!! Target not supported.";
a22zsolutions 12:48fe1f8eb613 759 #endif
a22zsolutions 14:fd17c5190f79 760 status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
a22zsolutions 14:fd17c5190f79 761 if (status != HAL_OK) {
a22zsolutions 11:876331d64e0e 762 return (-1); // FAIL
a22zsolutions 11:876331d64e0e 763 }
a22zsolutions 11:876331d64e0e 764
a22zsolutions 14:fd17c5190f79 765 /* To disable PLL, change SYSCLK to HSI without PLL */
a22zsolutions 14:fd17c5190f79 766 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
a22zsolutions 14:fd17c5190f79 767 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; // 16 MHz
a22zsolutions 14:fd17c5190f79 768 #if defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_NUCLEO_L152RE)
a22zsolutions 14:fd17c5190f79 769 status = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
a22zsolutions 14:fd17c5190f79 770 #elif defined(TARGET_NUCLEO_F411RE)
a22zsolutions 14:fd17c5190f79 771 status = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3);
a22zsolutions 14:fd17c5190f79 772 #else
a22zsolutions 14:fd17c5190f79 773 #error "!! Target not supported.";
a22zsolutions 14:fd17c5190f79 774 #endif
a22zsolutions 14:fd17c5190f79 775 if (status != HAL_OK) {
a22zsolutions 11:876331d64e0e 776 return (-2); // FAIL
a22zsolutions 11:876331d64e0e 777 }
a22zsolutions 14:fd17c5190f79 778
a22zsolutions 14:fd17c5190f79 779 /* Disable PLL */
a22zsolutions 14:fd17c5190f79 780 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_NONE;
a22zsolutions 14:fd17c5190f79 781 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_OFF;
a22zsolutions 14:fd17c5190f79 782 status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
a22zsolutions 14:fd17c5190f79 783 if (status != HAL_OK) {
a22zsolutions 14:fd17c5190f79 784 return (-3); // FAIL
a22zsolutions 14:fd17c5190f79 785 }
a22zsolutions 11:876331d64e0e 786
a22zsolutions 11:876331d64e0e 787 /* Enable HSE and activate PLL with HSE as source */
a22zsolutions 14:fd17c5190f79 788 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
a22zsolutions 14:fd17c5190f79 789 RCC_OscInitStruct.HSEState = RCC_HSE_ON; // External 8 MHz xtal on OSC_IN/OSC_OUT
a22zsolutions 14:fd17c5190f79 790 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
a22zsolutions 14:fd17c5190f79 791 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
a22zsolutions 14:fd17c5190f79 792 #if defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_NUCLEO_L152RE)
a22zsolutions 14:fd17c5190f79 793 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
a22zsolutions 14:fd17c5190f79 794 RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
a22zsolutions 14:fd17c5190f79 795 #elif defined(TARGET_NUCLEO_F411RE)
a22zsolutions 14:fd17c5190f79 796 RCC_OscInitStruct.PLL.PLLM = 4; /* config for 8MHz */
a22zsolutions 14:fd17c5190f79 797 RCC_OscInitStruct.PLL.PLLN = 200;
a22zsolutions 14:fd17c5190f79 798 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
a22zsolutions 14:fd17c5190f79 799 RCC_OscInitStruct.PLL.PLLQ = 8;
a22zsolutions 14:fd17c5190f79 800 #else
a22zsolutions 14:fd17c5190f79 801 #error "!! Target not supported.";
a22zsolutions 14:fd17c5190f79 802 #endif
a22zsolutions 14:fd17c5190f79 803 status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
a22zsolutions 14:fd17c5190f79 804 if (status != HAL_OK) {
a22zsolutions 14:fd17c5190f79 805 return (-4); // FAIL
a22zsolutions 14:fd17c5190f79 806 }
a22zsolutions 14:fd17c5190f79 807
a22zsolutions 14:fd17c5190f79 808 /* Select PLL as system clock source and configure the PCLK1 and PCLK2 clocks dividers */
a22zsolutions 14:fd17c5190f79 809 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
a22zsolutions 14:fd17c5190f79 810 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
a22zsolutions 14:fd17c5190f79 811 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
a22zsolutions 14:fd17c5190f79 812 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
a22zsolutions 14:fd17c5190f79 813 #if defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_NUCLEO_L152RE)
a22zsolutions 14:fd17c5190f79 814 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
a22zsolutions 14:fd17c5190f79 815 status = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
a22zsolutions 14:fd17c5190f79 816 #elif defined(TARGET_NUCLEO_F411RE)
a22zsolutions 14:fd17c5190f79 817 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
a22zsolutions 14:fd17c5190f79 818 status = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3);
a22zsolutions 14:fd17c5190f79 819 #else
a22zsolutions 14:fd17c5190f79 820 #error "!! Target not supported.";
a22zsolutions 14:fd17c5190f79 821 #endif
a22zsolutions 14:fd17c5190f79 822 if (status != HAL_OK) {
a22zsolutions 14:fd17c5190f79 823 return (-5); // FAIL
a22zsolutions 14:fd17c5190f79 824 }
a22zsolutions 14:fd17c5190f79 825
a22zsolutions 14:fd17c5190f79 826 /* Turn off unused clocks */
a22zsolutions 14:fd17c5190f79 827 #if defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_NUCLEO_L152RE)
a22zsolutions 14:fd17c5190f79 828 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI;
a22zsolutions 13:740272d6139f 829 RCC_OscInitStruct.MSIState = RCC_MSI_OFF;
a22zsolutions 12:48fe1f8eb613 830 #elif defined(TARGET_NUCLEO_F411RE)
a22zsolutions 14:fd17c5190f79 831 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
a22zsolutions 12:48fe1f8eb613 832 #else
a22zsolutions 14:fd17c5190f79 833 #error "!! Target not supported.";
a22zsolutions 13:740272d6139f 834 #endif
a22zsolutions 11:876331d64e0e 835 RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
a22zsolutions 11:876331d64e0e 836 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
a22zsolutions 14:fd17c5190f79 837 status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
a22zsolutions 14:fd17c5190f79 838 if (status != HAL_OK) {
a22zsolutions 14:fd17c5190f79 839 return (-6); // FAIL
a22zsolutions 11:876331d64e0e 840 }
a22zsolutions 14:fd17c5190f79 841
a22zsolutions 11:876331d64e0e 842 return 0; // OK
a22zsolutions 11:876331d64e0e 843 }
a22zsolutions 11:876331d64e0e 844
a22zsolutions 14:fd17c5190f79 845 void ConfigureSystemClockForHSE_PLL(void)
a22zsolutions 11:876331d64e0e 846 {
a22zsolutions 14:fd17c5190f79 847 int retVal = 0;
a22zsolutions 14:fd17c5190f79 848
a22zsolutions 14:fd17c5190f79 849 RCC_OscInitTypeDef RCC_OscInitStruct;
a22zsolutions 14:fd17c5190f79 850 RCC_ClkInitTypeDef RCC_ClkInitStruct;
a22zsolutions 14:fd17c5190f79 851 uint32_t pFLatency;
a22zsolutions 14:fd17c5190f79 852
a22zsolutions 14:fd17c5190f79 853 HAL_RCC_GetOscConfig( &RCC_OscInitStruct);
a22zsolutions 14:fd17c5190f79 854 HAL_RCC_GetClockConfig( &RCC_ClkInitStruct, &pFLatency);
a22zsolutions 14:fd17c5190f79 855
a22zsolutions 14:fd17c5190f79 856 printf("\r\n==> mbed-os-rev=%d.%d.%d lib-rev=%d AppBuild=%s %s <==\r\n", \
a22zsolutions 14:fd17c5190f79 857 MBED_MAJOR_VERSION, MBED_MINOR_VERSION,MBED_PATCH_VERSION,\
a22zsolutions 14:fd17c5190f79 858 MBED_LIBRARY_VERSION, __TIME__, __DATE__);
a22zsolutions 14:fd17c5190f79 859
a22zsolutions 14:fd17c5190f79 860 printf(">>>> Current <<<< \r\n");
a22zsolutions 14:fd17c5190f79 861 printf("> SysClock= %d Hz \r\n",SystemCoreClock);
a22zsolutions 14:fd17c5190f79 862 #if defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_NUCLEO_L152RE)
a22zsolutions 14:fd17c5190f79 863 printf("> CR=%08X CFGR=%08X CSR=%08X ICSCR=%08X \r\n",\
a22zsolutions 14:fd17c5190f79 864 RCC->CR, RCC->CFGR, RCC->CSR, RCC->ICSCR);
a22zsolutions 14:fd17c5190f79 865
a22zsolutions 14:fd17c5190f79 866 printf("> PLLSRC=%s PLLMUL=%u PLLDIV=%u \r\n",\
a22zsolutions 14:fd17c5190f79 867 ((RCC->CFGR & (1UL<<16))?"HSE":"HSI"),\
a22zsolutions 14:fd17c5190f79 868 RCC_OscInitStruct.PLL.PLLMUL, RCC_OscInitStruct.PLL.PLLDIV);
a22zsolutions 14:fd17c5190f79 869
a22zsolutions 14:fd17c5190f79 870 printf("> AHB=%d APB1=%d APB2=%d Latency=%u \r\n",\
a22zsolutions 14:fd17c5190f79 871 RCC_ClkInitStruct.AHBCLKDivider, RCC_ClkInitStruct.APB1CLKDivider,\
a22zsolutions 14:fd17c5190f79 872 RCC_ClkInitStruct.APB2CLKDivider,pFLatency);
a22zsolutions 14:fd17c5190f79 873 wait(0.1); // wait for printf to finish
a22zsolutions 14:fd17c5190f79 874
a22zsolutions 14:fd17c5190f79 875 #elif defined(TARGET_NUCLEO_F411RE)
a22zsolutions 14:fd17c5190f79 876 printf("> CR=%08X CFGR=%08X CSR=%08X PLLCFGR=%08X \r\n",\
a22zsolutions 14:fd17c5190f79 877 RCC->CR, RCC->CFGR, RCC->CSR, RCC->PLLCFGR);
a22zsolutions 11:876331d64e0e 878
a22zsolutions 14:fd17c5190f79 879 printf("> PLLSRC=%s PLLM=%d PLLN=%d PLLP=%d PLLQ=%d \r\n",
a22zsolutions 14:fd17c5190f79 880 ((RCC->PLLCFGR & (1UL<<22))?"HSE":"HSI"),\
a22zsolutions 14:fd17c5190f79 881 RCC_OscInitStruct.PLL.PLLM, RCC_OscInitStruct.PLL.PLLN,\
a22zsolutions 14:fd17c5190f79 882 RCC_OscInitStruct.PLL.PLLP, RCC_OscInitStruct.PLL.PLLQ);
a22zsolutions 11:876331d64e0e 883
a22zsolutions 14:fd17c5190f79 884 printf("> AHB=%d APB1=%d APB2=%d Latency=%u \r\n",\
a22zsolutions 14:fd17c5190f79 885 RCC_ClkInitStruct.AHBCLKDivider, RCC_ClkInitStruct.APB1CLKDivider,\
a22zsolutions 14:fd17c5190f79 886 RCC_ClkInitStruct.APB2CLKDivider,pFLatency);
a22zsolutions 14:fd17c5190f79 887 wait(0.1); // wait for print to finish
a22zsolutions 14:fd17c5190f79 888
a22zsolutions 14:fd17c5190f79 889 #else
a22zsolutions 14:fd17c5190f79 890 #error "!! Target not supported.";
a22zsolutions 14:fd17c5190f79 891 #endif
a22zsolutions 14:fd17c5190f79 892
a22zsolutions 14:fd17c5190f79 893 /* setup hse */
a22zsolutions 14:fd17c5190f79 894 retVal = sysclk_hse_pll_patch();
a22zsolutions 14:fd17c5190f79 895
a22zsolutions 14:fd17c5190f79 896 printf(">>>> NEW <<<< \r\n");
a22zsolutions 14:fd17c5190f79 897 printf("> SysClock= %d Hz, retVal= %d \r\n",SystemCoreClock, retVal);
a22zsolutions 14:fd17c5190f79 898 #if defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_NUCLEO_L152RE)
a22zsolutions 14:fd17c5190f79 899 printf("> CR=%08X CFGR=%08X CSR=%08X ICSCR=%08X \r\n",\
a22zsolutions 14:fd17c5190f79 900 RCC->CR, RCC->CFGR, RCC->CSR, RCC->ICSCR);
a22zsolutions 14:fd17c5190f79 901
a22zsolutions 14:fd17c5190f79 902 printf("> PLLSRC=%s PLLMUL=%u PLLDIV=%u \r\n",\
a22zsolutions 14:fd17c5190f79 903 ((RCC->CFGR & (1UL<<16))?"HSE":"HSI"),\
a22zsolutions 14:fd17c5190f79 904 RCC_OscInitStruct.PLL.PLLMUL, RCC_OscInitStruct.PLL.PLLDIV);
a22zsolutions 14:fd17c5190f79 905
a22zsolutions 14:fd17c5190f79 906 printf("> AHB=%d APB1=%d APB2=%d Latency=%u \r\n",\
a22zsolutions 14:fd17c5190f79 907 RCC_ClkInitStruct.AHBCLKDivider, RCC_ClkInitStruct.APB1CLKDivider,\
a22zsolutions 14:fd17c5190f79 908 RCC_ClkInitStruct.APB2CLKDivider, pFLatency);
a22zsolutions 14:fd17c5190f79 909
a22zsolutions 14:fd17c5190f79 910 #elif defined(TARGET_NUCLEO_F411RE)
a22zsolutions 14:fd17c5190f79 911 printf("> CR=%08X CFGR=%08X CSR=%08X PLLCFGR=%08X \r\n",\
a22zsolutions 14:fd17c5190f79 912 RCC->CR, RCC->CFGR, RCC->CSR, RCC->PLLCFGR);
a22zsolutions 14:fd17c5190f79 913
a22zsolutions 14:fd17c5190f79 914 printf("> PLLSRC=%s PLLM=%d PLLN=%d PLLP=%d PLLQ=%d \r\n",\
a22zsolutions 14:fd17c5190f79 915 ((RCC->PLLCFGR & (1UL<<22))?"HSE":"HSI"),\
a22zsolutions 14:fd17c5190f79 916 RCC_OscInitStruct.PLL.PLLM, RCC_OscInitStruct.PLL.PLLN,\
a22zsolutions 14:fd17c5190f79 917 RCC_OscInitStruct.PLL.PLLP, RCC_OscInitStruct.PLL.PLLQ);
a22zsolutions 14:fd17c5190f79 918
a22zsolutions 14:fd17c5190f79 919 printf("> AHB=%d APB1=%d APB2=%d Latency=%u \r\n",\
a22zsolutions 14:fd17c5190f79 920 RCC_ClkInitStruct.AHBCLKDivider, RCC_ClkInitStruct.APB1CLKDivider,\
a22zsolutions 14:fd17c5190f79 921 RCC_ClkInitStruct.APB2CLKDivider, pFLatency);
a22zsolutions 14:fd17c5190f79 922
a22zsolutions 14:fd17c5190f79 923 #else
a22zsolutions 14:fd17c5190f79 924 #error "!! Target not supported.";
a22zsolutions 14:fd17c5190f79 925 #endif
a22zsolutions 14:fd17c5190f79 926
a22zsolutions 14:fd17c5190f79 927 if(retVal < 0)
a22zsolutions 14:fd17c5190f79 928 {
a22zsolutions 14:fd17c5190f79 929 /* indicate error i.e failed to set HSE_PLL */
a22zsolutions 11:876331d64e0e 930 while(1)
a22zsolutions 11:876331d64e0e 931 {
a22zsolutions 11:876331d64e0e 932 myled = 1;
a22zsolutions 11:876331d64e0e 933 wait(0.2);
a22zsolutions 11:876331d64e0e 934 myled = 0;
a22zsolutions 11:876331d64e0e 935 wait(0.5);
a22zsolutions 11:876331d64e0e 936 }
a22zsolutions 14:fd17c5190f79 937 }
a22zsolutions 11:876331d64e0e 938 }
a22zsolutions 11:876331d64e0e 939
mluis 0:92bca02df485 940 int main( void )
mluis 0:92bca02df485 941 {
a22zsolutions 14:fd17c5190f79 942 ConfigureSystemClockForHSE_PLL();
a22zsolutions 11:876331d64e0e 943
uss1994 10:4e528716aa7a 944 flash_builtin();
uss1994 10:4e528716aa7a 945 flash_builtin();
uss1994 10:4e528716aa7a 946 flash_builtin();
uss1994 10:4e528716aa7a 947 flash_builtin();
uss1994 10:4e528716aa7a 948
mluis 3:9c6f7f082151 949 LoRaMacPrimitives_t LoRaMacPrimitives;
mluis 3:9c6f7f082151 950 LoRaMacCallback_t LoRaMacCallbacks;
mluis 3:9c6f7f082151 951 MibRequestConfirm_t mibReq;
mluis 0:92bca02df485 952
mluis 0:92bca02df485 953 BoardInit( );
mluis 3:9c6f7f082151 954 SerialDisplayInit( );
mluis 0:92bca02df485 955
mluis 7:3173f0508a98 956 SerialDisplayUpdateEui( 5, DevEui );
mluis 7:3173f0508a98 957 SerialDisplayUpdateEui( 6, AppEui );
mluis 7:3173f0508a98 958 SerialDisplayUpdateKey( 7, AppKey );
mluis 7:3173f0508a98 959
mluis 7:3173f0508a98 960 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 7:3173f0508a98 961 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 7:3173f0508a98 962 SerialDisplayUpdateDevAddr( DevAddr );
mluis 7:3173f0508a98 963 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 7:3173f0508a98 964 SerialDisplayUpdateKey( 13, AppSKey );
mluis 7:3173f0508a98 965 #endif
mluis 7:3173f0508a98 966
mluis 3:9c6f7f082151 967 DeviceState = DEVICE_STATE_INIT;
mluis 0:92bca02df485 968
mluis 0:92bca02df485 969 while( 1 )
mluis 0:92bca02df485 970 {
mluis 0:92bca02df485 971 SerialRxProcess( );
uss1994 10:4e528716aa7a 972 flash_builtin();
mluis 0:92bca02df485 973 if( IsNetworkJoinedStatusUpdate == true )
mluis 0:92bca02df485 974 {
mluis 0:92bca02df485 975 IsNetworkJoinedStatusUpdate = false;
mluis 3:9c6f7f082151 976 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 977 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 978 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:92bca02df485 979 }
mluis 3:9c6f7f082151 980 if( UplinkStatusUpdated == true )
mluis 0:92bca02df485 981 {
mluis 3:9c6f7f082151 982 UplinkStatusUpdated = false;
mluis 3:9c6f7f082151 983 SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize );
mluis 3:9c6f7f082151 984 }
mluis 3:9c6f7f082151 985 if( DownlinkStatusUpdated == true )
mluis 3:9c6f7f082151 986 {
mluis 3:9c6f7f082151 987 DownlinkStatusUpdated = false;
mluis 0:92bca02df485 988 SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
mluis 0:92bca02df485 989 }
mluis 3:9c6f7f082151 990
mluis 3:9c6f7f082151 991 switch( DeviceState )
mluis 0:92bca02df485 992 {
mluis 3:9c6f7f082151 993 case DEVICE_STATE_INIT:
mluis 3:9c6f7f082151 994 {
mluis 3:9c6f7f082151 995 LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
mluis 3:9c6f7f082151 996 LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
mluis 3:9c6f7f082151 997 LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
mluis 3:9c6f7f082151 998 LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
mluis 3:9c6f7f082151 999 LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
mluis 3:9c6f7f082151 1000
mluis 3:9c6f7f082151 1001 TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
mluis 3:9c6f7f082151 1002
mluis 3:9c6f7f082151 1003 mibReq.Type = MIB_ADR;
mluis 3:9c6f7f082151 1004 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 3:9c6f7f082151 1005 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1006
mluis 3:9c6f7f082151 1007 mibReq.Type = MIB_PUBLIC_NETWORK;
mluis 3:9c6f7f082151 1008 mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
mluis 3:9c6f7f082151 1009 LoRaMacMibSetRequestConfirm( &mibReq );
amirchaudhary 15:6d177c1f6a15 1010
amirchaudhary 15:6d177c1f6a15 1011
amirchaudhary 15:6d177c1f6a15 1012 // Limiting to just 2nd subband of 8 channels
amirchaudhary 15:6d177c1f6a15 1013 static uint16_t GatewayChannelsMask[] = {0xFF00, 0x0000, 0x0000, 0x0000, 0x0002, 0x0000};
amirchaudhary 15:6d177c1f6a15 1014 mibReq.Type = MIB_CHANNELS_DEFAULT_MASK;
amirchaudhary 15:6d177c1f6a15 1015 mibReq.Param.ChannelsDefaultMask = GatewayChannelsMask;
amirchaudhary 15:6d177c1f6a15 1016 LoRaMacMibSetRequestConfirm( &mibReq );
amirchaudhary 15:6d177c1f6a15 1017
amirchaudhary 15:6d177c1f6a15 1018 mibReq.Type = MIB_CHANNELS_MASK;
amirchaudhary 15:6d177c1f6a15 1019 mibReq.Param.ChannelsMask = GatewayChannelsMask;
amirchaudhary 15:6d177c1f6a15 1020 LoRaMacMibSetRequestConfirm( &mibReq );
amirchaudhary 15:6d177c1f6a15 1021
amirchaudhary 15:6d177c1f6a15 1022
amirchaudhary 15:6d177c1f6a15 1023
mluis 3:9c6f7f082151 1024
mluis 3:9c6f7f082151 1025 #if defined( USE_BAND_868 )
mluis 3:9c6f7f082151 1026 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 3:9c6f7f082151 1027 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 5:1e9f6a365854 1028
mluis 9:ee9dcbb9708d 1029 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 5:1e9f6a365854 1030 LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
mluis 5:1e9f6a365854 1031 LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
mluis 5:1e9f6a365854 1032 LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
mluis 5:1e9f6a365854 1033 LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 );
mluis 5:1e9f6a365854 1034 LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 );
mluis 5:1e9f6a365854 1035 LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
mluis 5:1e9f6a365854 1036 LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
mluis 7:3173f0508a98 1037
mluis 9:ee9dcbb9708d 1038 mibReq.Type = MIB_RX2_DEFAULT_CHANNEL;
mluis 9:ee9dcbb9708d 1039 mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
mluis 9:ee9dcbb9708d 1040 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 9:ee9dcbb9708d 1041
mluis 7:3173f0508a98 1042 mibReq.Type = MIB_RX2_CHANNEL;
mluis 7:3173f0508a98 1043 mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
mluis 7:3173f0508a98 1044 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 5:1e9f6a365854 1045 #endif
mluis 5:1e9f6a365854 1046
mluis 3:9c6f7f082151 1047 #endif
mluis 3:9c6f7f082151 1048 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 3:9c6f7f082151 1049 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 3:9c6f7f082151 1050 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 3:9c6f7f082151 1051
mluis 3:9c6f7f082151 1052 LoRaMacDownlinkStatus.DownlinkCounter = 0;
mluis 3:9c6f7f082151 1053
mluis 3:9c6f7f082151 1054 DeviceState = DEVICE_STATE_JOIN;
mluis 3:9c6f7f082151 1055 break;
mluis 3:9c6f7f082151 1056 }
mluis 3:9c6f7f082151 1057 case DEVICE_STATE_JOIN:
mluis 3:9c6f7f082151 1058 {
mluis 3:9c6f7f082151 1059 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 3:9c6f7f082151 1060 MlmeReq_t mlmeReq;
mluis 3:9c6f7f082151 1061
mluis 3:9c6f7f082151 1062 mlmeReq.Type = MLME_JOIN;
mluis 3:9c6f7f082151 1063
mluis 3:9c6f7f082151 1064 mlmeReq.Req.Join.DevEui = DevEui;
mluis 3:9c6f7f082151 1065 mlmeReq.Req.Join.AppEui = AppEui;
mluis 3:9c6f7f082151 1066 mlmeReq.Req.Join.AppKey = AppKey;
mluis 3:9c6f7f082151 1067
mluis 3:9c6f7f082151 1068 if( NextTx == true )
mluis 3:9c6f7f082151 1069 {
mluis 3:9c6f7f082151 1070 LoRaMacMlmeRequest( &mlmeReq );
mluis 3:9c6f7f082151 1071 }
mluis 7:3173f0508a98 1072 DeviceState = DEVICE_STATE_SLEEP;
mluis 3:9c6f7f082151 1073 #else
mluis 3:9c6f7f082151 1074 mibReq.Type = MIB_NET_ID;
mluis 3:9c6f7f082151 1075 mibReq.Param.NetID = LORAWAN_NETWORK_ID;
mluis 3:9c6f7f082151 1076 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1077
mluis 3:9c6f7f082151 1078 mibReq.Type = MIB_DEV_ADDR;
mluis 3:9c6f7f082151 1079 mibReq.Param.DevAddr = DevAddr;
mluis 3:9c6f7f082151 1080 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1081
mluis 3:9c6f7f082151 1082 mibReq.Type = MIB_NWK_SKEY;
mluis 3:9c6f7f082151 1083 mibReq.Param.NwkSKey = NwkSKey;
mluis 3:9c6f7f082151 1084 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1085
mluis 3:9c6f7f082151 1086 mibReq.Type = MIB_APP_SKEY;
mluis 3:9c6f7f082151 1087 mibReq.Param.AppSKey = AppSKey;
mluis 3:9c6f7f082151 1088 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1089
mluis 3:9c6f7f082151 1090 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 1091 mibReq.Param.IsNetworkJoined = true;
mluis 3:9c6f7f082151 1092 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1093
mluis 3:9c6f7f082151 1094 DeviceState = DEVICE_STATE_SEND;
mluis 3:9c6f7f082151 1095 #endif
mluis 3:9c6f7f082151 1096 IsNetworkJoinedStatusUpdate = true;
mluis 3:9c6f7f082151 1097 break;
mluis 3:9c6f7f082151 1098 }
mluis 3:9c6f7f082151 1099 case DEVICE_STATE_SEND:
mluis 3:9c6f7f082151 1100 {
mluis 3:9c6f7f082151 1101 if( NextTx == true )
mluis 3:9c6f7f082151 1102 {
mluis 3:9c6f7f082151 1103 SerialDisplayUpdateUplinkAcked( false );
mluis 3:9c6f7f082151 1104 SerialDisplayUpdateDonwlinkRxData( false );
mluis 3:9c6f7f082151 1105 PrepareTxFrame( AppPort );
mluis 3:9c6f7f082151 1106
mluis 3:9c6f7f082151 1107 NextTx = SendFrame( );
mluis 3:9c6f7f082151 1108 }
mluis 3:9c6f7f082151 1109 if( ComplianceTest.Running == true )
mluis 3:9c6f7f082151 1110 {
mluis 5:1e9f6a365854 1111 // Schedule next packet transmission
mluis 9:ee9dcbb9708d 1112 TxDutyCycleTime = 5000; // 5000 ms
mluis 3:9c6f7f082151 1113 }
mluis 3:9c6f7f082151 1114 else
mluis 3:9c6f7f082151 1115 {
mluis 3:9c6f7f082151 1116 // Schedule next packet transmission
mluis 3:9c6f7f082151 1117 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
mluis 3:9c6f7f082151 1118 }
mluis 3:9c6f7f082151 1119 DeviceState = DEVICE_STATE_CYCLE;
mluis 3:9c6f7f082151 1120 break;
mluis 3:9c6f7f082151 1121 }
mluis 3:9c6f7f082151 1122 case DEVICE_STATE_CYCLE:
mluis 3:9c6f7f082151 1123 {
mluis 5:1e9f6a365854 1124 DeviceState = DEVICE_STATE_SLEEP;
mluis 5:1e9f6a365854 1125
mluis 3:9c6f7f082151 1126 // Schedule next packet transmission
mluis 1:352f608c3337 1127 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
mluis 1:352f608c3337 1128 TimerStart( &TxNextPacketTimer );
mluis 3:9c6f7f082151 1129 break;
mluis 3:9c6f7f082151 1130 }
mluis 3:9c6f7f082151 1131 case DEVICE_STATE_SLEEP:
mluis 3:9c6f7f082151 1132 {
mluis 3:9c6f7f082151 1133 // Wake up through events
mluis 3:9c6f7f082151 1134 break;
mluis 3:9c6f7f082151 1135 }
mluis 3:9c6f7f082151 1136 default:
mluis 3:9c6f7f082151 1137 {
mluis 3:9c6f7f082151 1138 DeviceState = DEVICE_STATE_INIT;
mluis 3:9c6f7f082151 1139 break;
mluis 3:9c6f7f082151 1140 }
mluis 0:92bca02df485 1141 }
mluis 0:92bca02df485 1142 }
mluis 0:92bca02df485 1143 }