Workshop demo program

Dependencies:   PinDetect mbed LoRaWAN-lib SX1272Lib

Committer:
Brandond200
Date:
Wed May 03 16:51:28 2017 +0000
Revision:
19:31fc997c460b
Parent:
18:326069443137
added a new timer to make sure the sending bool never gets stuck in the transmitting position. changed the uint8_t counting cycles for checkAlarm to a uint16_t

Who changed what in which revision?

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