Node code for sx1272 LoRa transciever

Dependencies:   mbed BMP085 BufferedSerial DHT Sds021 Chainable_RGB_LED DigitDisplay LoRaWAN-lib SX1272Lib

Fork of LoRaWAN-demo-72-bootcamp by Semtech

Committer:
GregCr
Date:
Wed Mar 09 16:05:08 2016 +0000
Revision:
3:d4142832d5af
Parent:
2:48d8d4806d48
Child:
4:de084e23d884
Corrected port, payload size, and added comments

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