RP

Dependencies:   mbed SX1272Lib

Committer:
limchy
Date:
Thu Jul 26 07:33:53 2018 +0000
Revision:
14:c7449c450d0b
Parent:
13:4429cb012011
Child:
15:d4ce35d5c9f2
ver1

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