Implemented LED Indicator Patterns

Dependencies:   mbed

Committer:
uss1994
Date:
Wed Jan 30 17:18:24 2019 +0000
Revision:
20:e75b3340d1c7
Parent:
19:6f88d1cabaca
Child:
21:e3052f1fa2a7
fixed type conversion & rounding issues;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 0:92bca02df485 1 /*
mluis 0:92bca02df485 2 / _____) _ | |
mluis 0:92bca02df485 3 ( (____ _____ ____ _| |_ _____ ____| |__
mluis 0:92bca02df485 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
mluis 0:92bca02df485 5 _____) ) ____| | | || |_| ____( (___| | | |
mluis 0:92bca02df485 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
mluis 0:92bca02df485 7 (C)2015 Semtech
mluis 0:92bca02df485 8
mluis 0:92bca02df485 9 Description: LoRaMac classA device implementation
mluis 0:92bca02df485 10
mluis 0:92bca02df485 11 License: Revised BSD License, see LICENSE.TXT file include in the project
mluis 0:92bca02df485 12
mluis 0:92bca02df485 13 Maintainer: Miguel Luis and Gregory Cristian
mluis 0:92bca02df485 14 */
mluis 0:92bca02df485 15 #include "mbed.h"
mluis 0:92bca02df485 16 #include "board.h"
mluis 0:92bca02df485 17 #include "radio.h"
mluis 0:92bca02df485 18
mluis 0:92bca02df485 19 #include "LoRaMac.h"
mluis 9:ee9dcbb9708d 20 #include "Commissioning.h"
mluis 0:92bca02df485 21 #include "SerialDisplay.h"
mluis 0:92bca02df485 22
mluis 0:92bca02df485 23 /*!
mluis 9:ee9dcbb9708d 24 * Defines the application data transmission duty cycle. 5s, value in [ms].
mluis 0:92bca02df485 25 */
mluis 9:ee9dcbb9708d 26 #define APP_TX_DUTYCYCLE 5000
mluis 1:352f608c3337 27
mluis 1:352f608c3337 28 /*!
mluis 1:352f608c3337 29 * Defines a random delay for application data transmission duty cycle. 1s,
mluis 9:ee9dcbb9708d 30 * value in [ms].
mluis 1:352f608c3337 31 */
mluis 9:ee9dcbb9708d 32 #define APP_TX_DUTYCYCLE_RND 1000
mluis 0:92bca02df485 33
mluis 0:92bca02df485 34 /*!
mluis 5:1e9f6a365854 35 * Default datarate
mluis 3:9c6f7f082151 36 */
mluis 3:9c6f7f082151 37 #define LORAWAN_DEFAULT_DATARATE DR_0
mluis 3:9c6f7f082151 38
mluis 3:9c6f7f082151 39 /*!
mluis 0:92bca02df485 40 * LoRaWAN confirmed messages
mluis 0:92bca02df485 41 */
mluis 0:92bca02df485 42 #define LORAWAN_CONFIRMED_MSG_ON true
mluis 0:92bca02df485 43
mluis 0:92bca02df485 44 /*!
mluis 3:9c6f7f082151 45 * LoRaWAN Adaptive Data Rate
mluis 0:92bca02df485 46 *
mluis 0:92bca02df485 47 * \remark Please note that when ADR is enabled the end-device should be static
mluis 0:92bca02df485 48 */
mluis 0:92bca02df485 49 #define LORAWAN_ADR_ON 1
mluis 0:92bca02df485 50
mluis 1:352f608c3337 51 #if defined( USE_BAND_868 )
mluis 1:352f608c3337 52
mluis 3:9c6f7f082151 53 #include "LoRaMacTest.h"
mluis 3:9c6f7f082151 54
mluis 0:92bca02df485 55 /*!
mluis 0:92bca02df485 56 * LoRaWAN ETSI duty cycle control enable/disable
mluis 0:92bca02df485 57 *
mluis 0:92bca02df485 58 * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
mluis 0:92bca02df485 59 */
mluis 9:ee9dcbb9708d 60 #define LORAWAN_DUTYCYCLE_ON false
mluis 0:92bca02df485 61
mluis 5:1e9f6a365854 62 #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1
mluis 5:1e9f6a365854 63
mluis 9:ee9dcbb9708d 64 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 5:1e9f6a365854 65
mluis 5:1e9f6a365854 66 #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 67 #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 68 #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 69 #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 70 #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 71 #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 }
mluis 5:1e9f6a365854 72 #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 }
mluis 5:1e9f6a365854 73
mluis 5:1e9f6a365854 74 #endif
mluis 5:1e9f6a365854 75
mluis 1:352f608c3337 76 #endif
mluis 1:352f608c3337 77
mluis 0:92bca02df485 78 /*!
mluis 0:92bca02df485 79 * LoRaWAN application port
mluis 0:92bca02df485 80 */
mluis 0:92bca02df485 81 #define LORAWAN_APP_PORT 15
mluis 0:92bca02df485 82
mluis 0:92bca02df485 83 /*!
mluis 0:92bca02df485 84 * User application data buffer size
mluis 0:92bca02df485 85 */
uss1994 10:9a4efdd07a77 86 #define LORAWAN_APP_DATA_SIZE 41
mluis 0:92bca02df485 87
mluis 0:92bca02df485 88 static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
mluis 0:92bca02df485 89 static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
mluis 0:92bca02df485 90 static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
mluis 0:92bca02df485 91
mluis 7:3173f0508a98 92 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:92bca02df485 93
mluis 0:92bca02df485 94 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mluis 0:92bca02df485 95 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
mluis 0:92bca02df485 96
mluis 3:9c6f7f082151 97 /*!
mluis 3:9c6f7f082151 98 * Device address
mluis 3:9c6f7f082151 99 */
mluis 3:9c6f7f082151 100 static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
mluis 0:92bca02df485 101
mluis 3:9c6f7f082151 102 #endif
mluis 0:92bca02df485 103
mluis 0:92bca02df485 104 /*!
mluis 1:352f608c3337 105 * Application port
mluis 1:352f608c3337 106 */
mluis 1:352f608c3337 107 static uint8_t AppPort = LORAWAN_APP_PORT;
mluis 1:352f608c3337 108
mluis 1:352f608c3337 109 /*!
mluis 1:352f608c3337 110 * User application data size
mluis 1:352f608c3337 111 */
mluis 1:352f608c3337 112 static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 1:352f608c3337 113
mluis 1:352f608c3337 114 /*!
mluis 1:352f608c3337 115 * User application data buffer size
mluis 1:352f608c3337 116 */
mluis 1:352f608c3337 117 #define LORAWAN_APP_DATA_MAX_SIZE 64
mluis 1:352f608c3337 118
mluis 1:352f608c3337 119 /*!
mluis 0:92bca02df485 120 * User application data
mluis 0:92bca02df485 121 */
mluis 1:352f608c3337 122 static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
mluis 1:352f608c3337 123
mluis 1:352f608c3337 124 /*!
mluis 1:352f608c3337 125 * Indicates if the node is sending confirmed or unconfirmed messages
mluis 1:352f608c3337 126 */
mluis 1:352f608c3337 127 static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:92bca02df485 128
mluis 0:92bca02df485 129 /*!
mluis 0:92bca02df485 130 * Defines the application data transmission duty cycle
mluis 0:92bca02df485 131 */
mluis 0:92bca02df485 132 static uint32_t TxDutyCycleTime;
mluis 0:92bca02df485 133
mluis 1:352f608c3337 134 /*!
mluis 1:352f608c3337 135 * Timer to handle the application data transmission duty cycle
mluis 1:352f608c3337 136 */
mluis 1:352f608c3337 137 static TimerEvent_t TxNextPacketTimer;
mluis 0:92bca02df485 138
mluis 3:9c6f7f082151 139 /*!
uss1994 10:9a4efdd07a77 140 * SOME APPLICATION PARAMETERS
uss1994 10:9a4efdd07a77 141 */
uss1994 18:0a4de80c02a6 142
uss1994 10:9a4efdd07a77 143 DigitalOut myled(D7);
uss1994 10:9a4efdd07a77 144
uss1994 10:9a4efdd07a77 145 DigitalOut relayPin(D6);
uss1994 10:9a4efdd07a77 146 AnalogIn BAT_PIN(A1);
uss1994 10:9a4efdd07a77 147 AnalogIn LIGHT_1_PIN(A2);
uss1994 10:9a4efdd07a77 148 AnalogIn LIGHT_2_PIN(A5);
uss1994 19:6f88d1cabaca 149 AnalogIn RH_PIN(PC_2);
uss1994 10:9a4efdd07a77 150 AnalogIn VCE_PIN(PB_1);
uss1994 10:9a4efdd07a77 151
amirchaudhary 14:9ba8f6975308 152 InterruptIn button1(USER_BUTTON);
uss1994 11:e433cbbfd436 153 volatile bool button1_pressed = false; // Used in the main loop
uss1994 11:e433cbbfd436 154 volatile bool button1_enabled = true; // Used for debouncing
uss1994 11:e433cbbfd436 155 Timeout button1_timeout; // Used for debouncing
uss1994 11:e433cbbfd436 156 int press_count;
uss1994 11:e433cbbfd436 157 Timeout buttonOptions_timeout; // Used for determining number of presses
uss1994 11:e433cbbfd436 158
uss1994 10:9a4efdd07a77 159 unsigned int time_window = 5; // Default 3600 seconds in an hour
uss1994 10:9a4efdd07a77 160 unsigned long long_interval = 604800; // Default 31557600 seconds in a year
uss1994 10:9a4efdd07a77 161 unsigned int hb_interval = 3600; // Default 86400 seconds in a day
uss1994 10:9a4efdd07a77 162 unsigned long test_interval = 86400; // Default 2592000 seconds in a month
uss1994 10:9a4efdd07a77 163
uss1994 10:9a4efdd07a77 164 time_t next_stest;
uss1994 10:9a4efdd07a77 165 time_t next_ltest;
uss1994 10:9a4efdd07a77 166
uss1994 10:9a4efdd07a77 167 uint8_t hb_data[5];
uss1994 16:e1d692816864 168 uint8_t tdata[10];
uss1994 10:9a4efdd07a77 169
uss1994 10:9a4efdd07a77 170 uint8_t *data;
uss1994 10:9a4efdd07a77 171 uint8_t data_len;
uss1994 10:9a4efdd07a77 172
uss1994 10:9a4efdd07a77 173 bool running_test;
uss1994 10:9a4efdd07a77 174 bool joining;
uss1994 16:e1d692816864 175 // bool received_downlink;
uss1994 10:9a4efdd07a77 176
uss1994 10:9a4efdd07a77 177 time_t current_time;
uss1994 10:9a4efdd07a77 178
uss1994 10:9a4efdd07a77 179 unsigned long last_hb;
uss1994 10:9a4efdd07a77 180 unsigned long test_start;
uss1994 10:9a4efdd07a77 181
uss1994 16:e1d692816864 182 uint8_t measurements[5];
uss1994 10:9a4efdd07a77 183
uss1994 10:9a4efdd07a77 184 unsigned long test_duration;
uss1994 10:9a4efdd07a77 185
uss1994 10:9a4efdd07a77 186 /*!
mluis 3:9c6f7f082151 187 * Specifies the state of the application LED
mluis 3:9c6f7f082151 188 */
mluis 3:9c6f7f082151 189 static bool AppLedStateOn = false;
mluis 3:9c6f7f082151 190 volatile bool Led3StateChanged = false;
mluis 0:92bca02df485 191 /*!
mluis 3:9c6f7f082151 192 * Timer to handle the state of LED1
mluis 0:92bca02df485 193 */
mluis 3:9c6f7f082151 194 static TimerEvent_t Led1Timer;
mluis 3:9c6f7f082151 195 volatile bool Led1State = false;
mluis 3:9c6f7f082151 196 volatile bool Led1StateChanged = false;
mluis 3:9c6f7f082151 197 /*!
mluis 3:9c6f7f082151 198 * Timer to handle the state of LED2
mluis 3:9c6f7f082151 199 */
mluis 3:9c6f7f082151 200 static TimerEvent_t Led2Timer;
mluis 3:9c6f7f082151 201 volatile bool Led2State = false;
mluis 3:9c6f7f082151 202 volatile bool Led2StateChanged = false;
mluis 0:92bca02df485 203
mluis 0:92bca02df485 204 /*!
mluis 0:92bca02df485 205 * Indicates if a new packet can be sent
mluis 0:92bca02df485 206 */
mluis 3:9c6f7f082151 207 static bool NextTx = true;
mluis 0:92bca02df485 208
mluis 3:9c6f7f082151 209 /*!
mluis 3:9c6f7f082151 210 * Device states
mluis 3:9c6f7f082151 211 */
mluis 9:ee9dcbb9708d 212 static enum eDeviceState
mluis 3:9c6f7f082151 213 {
mluis 3:9c6f7f082151 214 DEVICE_STATE_INIT,
mluis 3:9c6f7f082151 215 DEVICE_STATE_JOIN,
mluis 3:9c6f7f082151 216 DEVICE_STATE_SEND,
mluis 3:9c6f7f082151 217 DEVICE_STATE_CYCLE,
mluis 3:9c6f7f082151 218 DEVICE_STATE_SLEEP
mluis 3:9c6f7f082151 219 }DeviceState;
mluis 0:92bca02df485 220
uss1994 10:9a4efdd07a77 221 static enum eMessageType
uss1994 10:9a4efdd07a77 222 {
uss1994 10:9a4efdd07a77 223 MESSAGE_TYPE_HB,
uss1994 10:9a4efdd07a77 224 MESSAGE_TYPE_SHORT_TEST,
uss1994 10:9a4efdd07a77 225 MESSAGE_TYPE_LONG_TEST
uss1994 10:9a4efdd07a77 226 }MessageType;
uss1994 10:9a4efdd07a77 227
mluis 3:9c6f7f082151 228 /*!
mluis 3:9c6f7f082151 229 * LoRaWAN compliance tests support data
mluis 3:9c6f7f082151 230 */
mluis 3:9c6f7f082151 231 struct ComplianceTest_s
mluis 3:9c6f7f082151 232 {
mluis 3:9c6f7f082151 233 bool Running;
mluis 3:9c6f7f082151 234 uint8_t State;
mluis 3:9c6f7f082151 235 bool IsTxConfirmed;
mluis 3:9c6f7f082151 236 uint8_t AppPort;
mluis 3:9c6f7f082151 237 uint8_t AppDataSize;
mluis 3:9c6f7f082151 238 uint8_t *AppDataBuffer;
mluis 3:9c6f7f082151 239 uint16_t DownLinkCounter;
mluis 3:9c6f7f082151 240 bool LinkCheck;
mluis 3:9c6f7f082151 241 uint8_t DemodMargin;
mluis 3:9c6f7f082151 242 uint8_t NbGateways;
mluis 3:9c6f7f082151 243 }ComplianceTest;
mluis 0:92bca02df485 244
mluis 3:9c6f7f082151 245 /*
mluis 3:9c6f7f082151 246 * SerialDisplay managment variables
mluis 3:9c6f7f082151 247 */
mluis 1:352f608c3337 248
mluis 3:9c6f7f082151 249 /*!
mluis 3:9c6f7f082151 250 * Indicates if the MAC layer network join status has changed.
mluis 3:9c6f7f082151 251 */
mluis 3:9c6f7f082151 252 static bool IsNetworkJoinedStatusUpdate = false;
mluis 3:9c6f7f082151 253
mluis 3:9c6f7f082151 254 /*!
mluis 3:9c6f7f082151 255 * Strucure containing the Uplink status
mluis 3:9c6f7f082151 256 */
mluis 0:92bca02df485 257 struct sLoRaMacUplinkStatus
mluis 0:92bca02df485 258 {
mluis 0:92bca02df485 259 uint8_t Acked;
mluis 0:92bca02df485 260 int8_t Datarate;
mluis 0:92bca02df485 261 uint16_t UplinkCounter;
mluis 0:92bca02df485 262 uint8_t Port;
mluis 0:92bca02df485 263 uint8_t *Buffer;
mluis 0:92bca02df485 264 uint8_t BufferSize;
mluis 0:92bca02df485 265 }LoRaMacUplinkStatus;
mluis 3:9c6f7f082151 266 volatile bool UplinkStatusUpdated = false;
mluis 0:92bca02df485 267
mluis 3:9c6f7f082151 268 /*!
mluis 3:9c6f7f082151 269 * Strucure containing the Downlink status
mluis 3:9c6f7f082151 270 */
mluis 0:92bca02df485 271 struct sLoRaMacDownlinkStatus
mluis 0:92bca02df485 272 {
mluis 0:92bca02df485 273 int16_t Rssi;
mluis 0:92bca02df485 274 int8_t Snr;
mluis 0:92bca02df485 275 uint16_t DownlinkCounter;
mluis 0:92bca02df485 276 bool RxData;
mluis 0:92bca02df485 277 uint8_t Port;
mluis 0:92bca02df485 278 uint8_t *Buffer;
mluis 0:92bca02df485 279 uint8_t BufferSize;
mluis 0:92bca02df485 280 }LoRaMacDownlinkStatus;
mluis 3:9c6f7f082151 281 volatile bool DownlinkStatusUpdated = false;
mluis 0:92bca02df485 282
mluis 0:92bca02df485 283 void SerialDisplayRefresh( void )
mluis 0:92bca02df485 284 {
mluis 3:9c6f7f082151 285 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 286
mluis 0:92bca02df485 287 SerialDisplayInit( );
mluis 0:92bca02df485 288 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:92bca02df485 289
mluis 0:92bca02df485 290 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:92bca02df485 291 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 3:9c6f7f082151 292 SerialDisplayUpdateDevAddr( DevAddr );
mluis 0:92bca02df485 293 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:92bca02df485 294 SerialDisplayUpdateKey( 13, AppSKey );
mluis 7:3173f0508a98 295 #endif
mluis 0:92bca02df485 296 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:92bca02df485 297 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:92bca02df485 298 SerialDisplayUpdateKey( 7, AppKey );
mluis 3:9c6f7f082151 299
mluis 3:9c6f7f082151 300 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 301 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 302 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:92bca02df485 303
mluis 0:92bca02df485 304 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 1:352f608c3337 305 #if defined( USE_BAND_868 )
mluis 0:92bca02df485 306 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 1:352f608c3337 307 #else
mluis 1:352f608c3337 308 SerialDisplayUpdateDutyCycle( false );
mluis 1:352f608c3337 309 #endif
mluis 0:92bca02df485 310 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:92bca02df485 311
mluis 0:92bca02df485 312 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:92bca02df485 313 }
mluis 0:92bca02df485 314
mluis 0:92bca02df485 315 void SerialRxProcess( void )
mluis 0:92bca02df485 316 {
mluis 0:92bca02df485 317 if( SerialDisplayReadable( ) == true )
mluis 0:92bca02df485 318 {
mluis 0:92bca02df485 319 switch( SerialDisplayGetChar( ) )
mluis 0:92bca02df485 320 {
mluis 0:92bca02df485 321 case 'R':
mluis 0:92bca02df485 322 case 'r':
mluis 0:92bca02df485 323 // Refresh Serial screen
mluis 0:92bca02df485 324 SerialDisplayRefresh( );
mluis 0:92bca02df485 325 break;
mluis 0:92bca02df485 326 default:
mluis 0:92bca02df485 327 break;
mluis 0:92bca02df485 328 }
mluis 0:92bca02df485 329 }
mluis 0:92bca02df485 330 }
mluis 0:92bca02df485 331
uss1994 11:e433cbbfd436 332 // Enables button when bouncing is over
uss1994 11:e433cbbfd436 333 void button1_enabled_cb(void)
uss1994 11:e433cbbfd436 334 {
uss1994 11:e433cbbfd436 335 button1_enabled = true;
uss1994 11:e433cbbfd436 336 }
uss1994 11:e433cbbfd436 337
uss1994 11:e433cbbfd436 338 // ISR handling button pressed event
uss1994 11:e433cbbfd436 339 void button1_onpressed_cb(void)
uss1994 11:e433cbbfd436 340 {
uss1994 11:e433cbbfd436 341 if (button1_enabled) { // Disabled while the button is bouncing
uss1994 11:e433cbbfd436 342 button1_enabled = false;
uss1994 11:e433cbbfd436 343 button1_pressed = true; // To be read by the main loop
uss1994 11:e433cbbfd436 344 button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
uss1994 11:e433cbbfd436 345 }
uss1994 11:e433cbbfd436 346 }
uss1994 11:e433cbbfd436 347
uss1994 10:9a4efdd07a77 348 // **************************** COMMUNICATION PACKET DEFINITION METHODS ******************************** //
uss1994 10:9a4efdd07a77 349
uss1994 10:9a4efdd07a77 350 void heartbeat_message(uint8_t *hb_data, uint8_t *current_date) {
uss1994 10:9a4efdd07a77 351 hb_data[0] = 0xD2;
uss1994 10:9a4efdd07a77 352 memcpy(hb_data+1, current_date, 4);
uss1994 10:9a4efdd07a77 353 }
uss1994 10:9a4efdd07a77 354
uss1994 16:e1d692816864 355 void short_test_result(uint8_t *tdata, uint8_t *test_start_date, uint8_t *measurements) {
uss1994 16:e1d692816864 356 tdata[0] = 0xD7;
uss1994 16:e1d692816864 357 memcpy(tdata+1, test_start_date, 4);
uss1994 16:e1d692816864 358 memcpy(tdata+5, measurements, 5);
uss1994 10:9a4efdd07a77 359 }
uss1994 10:9a4efdd07a77 360
uss1994 16:e1d692816864 361 void long_test_result(uint8_t *tdata, uint8_t *test_start_date, uint8_t *measurements) {
uss1994 16:e1d692816864 362 tdata[0] = 0xD8;
uss1994 16:e1d692816864 363 memcpy(tdata+1, test_start_date, 4);
uss1994 16:e1d692816864 364 memcpy(tdata+5, measurements, 5);
uss1994 10:9a4efdd07a77 365 }
uss1994 10:9a4efdd07a77 366
mluis 0:92bca02df485 367 /*!
mluis 3:9c6f7f082151 368 * \brief Prepares the payload of the frame
mluis 0:92bca02df485 369 */
mluis 0:92bca02df485 370 static void PrepareTxFrame( uint8_t port )
mluis 0:92bca02df485 371 {
mluis 1:352f608c3337 372 switch( port )
mluis 1:352f608c3337 373 {
mluis 1:352f608c3337 374 case 15:
uss1994 10:9a4efdd07a77 375 {
uss1994 10:9a4efdd07a77 376 switch ( MessageType )
mluis 1:352f608c3337 377 {
uss1994 10:9a4efdd07a77 378 case MESSAGE_TYPE_HB:
uss1994 10:9a4efdd07a77 379 {
uss1994 10:9a4efdd07a77 380 AppDataSize = 5;
uss1994 10:9a4efdd07a77 381 heartbeat_message(AppData, (uint8_t *)&current_time);
uss1994 10:9a4efdd07a77 382 break;
uss1994 10:9a4efdd07a77 383 }
uss1994 10:9a4efdd07a77 384 case MESSAGE_TYPE_SHORT_TEST:
uss1994 10:9a4efdd07a77 385 {
uss1994 16:e1d692816864 386 AppDataSize = 10;
uss1994 16:e1d692816864 387 short_test_result(AppData, (uint8_t *)&current_time, measurements);
uss1994 10:9a4efdd07a77 388 break;
uss1994 10:9a4efdd07a77 389 }
uss1994 10:9a4efdd07a77 390 case MESSAGE_TYPE_LONG_TEST:
uss1994 10:9a4efdd07a77 391 {
uss1994 16:e1d692816864 392 AppDataSize = 10;
uss1994 16:e1d692816864 393 long_test_result(AppData, (uint8_t *)&current_time, measurements);
uss1994 10:9a4efdd07a77 394 break;
uss1994 10:9a4efdd07a77 395 }
uss1994 10:9a4efdd07a77 396 default:
uss1994 10:9a4efdd07a77 397 {
uss1994 10:9a4efdd07a77 398 AppDataSize = 5;
uss1994 10:9a4efdd07a77 399 heartbeat_message(AppData, (uint8_t *)&current_time);
uss1994 10:9a4efdd07a77 400 break;
uss1994 10:9a4efdd07a77 401 }
mluis 1:352f608c3337 402 }
mluis 1:352f608c3337 403 }
mluis 1:352f608c3337 404 break;
mluis 1:352f608c3337 405 case 224:
mluis 3:9c6f7f082151 406 if( ComplianceTest.LinkCheck == true )
mluis 1:352f608c3337 407 {
mluis 3:9c6f7f082151 408 ComplianceTest.LinkCheck = false;
mluis 1:352f608c3337 409 AppDataSize = 3;
mluis 1:352f608c3337 410 AppData[0] = 5;
mluis 3:9c6f7f082151 411 AppData[1] = ComplianceTest.DemodMargin;
mluis 3:9c6f7f082151 412 AppData[2] = ComplianceTest.NbGateways;
mluis 3:9c6f7f082151 413 ComplianceTest.State = 1;
mluis 1:352f608c3337 414 }
mluis 1:352f608c3337 415 else
mluis 1:352f608c3337 416 {
mluis 3:9c6f7f082151 417 switch( ComplianceTest.State )
mluis 1:352f608c3337 418 {
mluis 1:352f608c3337 419 case 4:
mluis 3:9c6f7f082151 420 ComplianceTest.State = 1;
mluis 1:352f608c3337 421 break;
mluis 1:352f608c3337 422 case 1:
mluis 1:352f608c3337 423 AppDataSize = 2;
mluis 3:9c6f7f082151 424 AppData[0] = ComplianceTest.DownLinkCounter >> 8;
mluis 3:9c6f7f082151 425 AppData[1] = ComplianceTest.DownLinkCounter;
mluis 1:352f608c3337 426 break;
mluis 1:352f608c3337 427 }
mluis 1:352f608c3337 428 }
mluis 1:352f608c3337 429 break;
mluis 3:9c6f7f082151 430 default:
mluis 3:9c6f7f082151 431 break;
mluis 1:352f608c3337 432 }
mluis 0:92bca02df485 433 }
mluis 0:92bca02df485 434
mluis 3:9c6f7f082151 435 /*!
mluis 3:9c6f7f082151 436 * \brief Prepares the payload of the frame
mluis 3:9c6f7f082151 437 *
mluis 3:9c6f7f082151 438 * \retval [0: frame could be send, 1: error]
mluis 3:9c6f7f082151 439 */
mluis 0:92bca02df485 440 static bool SendFrame( void )
mluis 0:92bca02df485 441 {
mluis 3:9c6f7f082151 442 McpsReq_t mcpsReq;
mluis 3:9c6f7f082151 443 LoRaMacTxInfo_t txInfo;
mluis 9:ee9dcbb9708d 444
mluis 3:9c6f7f082151 445 if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
mluis 1:352f608c3337 446 {
mluis 3:9c6f7f082151 447 // Send empty frame in order to flush MAC commands
mluis 3:9c6f7f082151 448 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 3:9c6f7f082151 449 mcpsReq.Req.Unconfirmed.fBuffer = NULL;
mluis 3:9c6f7f082151 450 mcpsReq.Req.Unconfirmed.fBufferSize = 0;
mluis 3:9c6f7f082151 451 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 5:1e9f6a365854 452
mluis 3:9c6f7f082151 453 LoRaMacUplinkStatus.Acked = false;
uss1994 18:0a4de80c02a6 454 LoRaMacUplinkStatus.Port = 0;
mluis 3:9c6f7f082151 455 LoRaMacUplinkStatus.Buffer = NULL;
mluis 3:9c6f7f082151 456 LoRaMacUplinkStatus.BufferSize = 0;
mluis 3:9c6f7f082151 457 SerialDisplayUpdateFrameType( false );
uss1994 19:6f88d1cabaca 458 // SerialDisplayPrintDebugStatus( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) );
uss1994 15:f4805eb13d73 459
mluis 1:352f608c3337 460 }
mluis 1:352f608c3337 461 else
mluis 1:352f608c3337 462 {
mluis 3:9c6f7f082151 463 LoRaMacUplinkStatus.Acked = false;
mluis 3:9c6f7f082151 464 LoRaMacUplinkStatus.Port = AppPort;
mluis 3:9c6f7f082151 465 LoRaMacUplinkStatus.Buffer = AppData;
mluis 3:9c6f7f082151 466 LoRaMacUplinkStatus.BufferSize = AppDataSize;
mluis 3:9c6f7f082151 467 SerialDisplayUpdateFrameType( IsTxConfirmed );
mluis 3:9c6f7f082151 468
mluis 3:9c6f7f082151 469 if( IsTxConfirmed == false )
mluis 3:9c6f7f082151 470 {
mluis 3:9c6f7f082151 471 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 3:9c6f7f082151 472 mcpsReq.Req.Unconfirmed.fPort = AppPort;
mluis 3:9c6f7f082151 473 mcpsReq.Req.Unconfirmed.fBuffer = AppData;
mluis 3:9c6f7f082151 474 mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
mluis 3:9c6f7f082151 475 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 3:9c6f7f082151 476 }
mluis 3:9c6f7f082151 477 else
mluis 3:9c6f7f082151 478 {
mluis 3:9c6f7f082151 479 mcpsReq.Type = MCPS_CONFIRMED;
mluis 3:9c6f7f082151 480 mcpsReq.Req.Confirmed.fPort = AppPort;
mluis 3:9c6f7f082151 481 mcpsReq.Req.Confirmed.fBuffer = AppData;
mluis 3:9c6f7f082151 482 mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
mluis 4:00cf2370c99d 483 mcpsReq.Req.Confirmed.NbTrials = 8;
mluis 3:9c6f7f082151 484 mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 3:9c6f7f082151 485 }
mluis 1:352f608c3337 486 }
mluis 1:352f608c3337 487
mluis 3:9c6f7f082151 488 if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK )
mluis 0:92bca02df485 489 {
uss1994 18:0a4de80c02a6 490 if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
uss1994 18:0a4de80c02a6 491 {
uss1994 18:0a4de80c02a6 492 return true;
uss1994 18:0a4de80c02a6 493 }
mluis 0:92bca02df485 494 return false;
mluis 0:92bca02df485 495 }
mluis 3:9c6f7f082151 496 return true;
mluis 0:92bca02df485 497 }
mluis 0:92bca02df485 498
mluis 0:92bca02df485 499 /*!
mluis 0:92bca02df485 500 * \brief Function executed on TxNextPacket Timeout event
mluis 0:92bca02df485 501 */
mluis 0:92bca02df485 502 static void OnTxNextPacketTimerEvent( void )
mluis 0:92bca02df485 503 {
mluis 3:9c6f7f082151 504 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 505 LoRaMacStatus_t status;
mluis 3:9c6f7f082151 506
mluis 0:92bca02df485 507 TimerStop( &TxNextPacketTimer );
mluis 3:9c6f7f082151 508
mluis 3:9c6f7f082151 509 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 510 status = LoRaMacMibGetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 511
mluis 3:9c6f7f082151 512 if( status == LORAMAC_STATUS_OK )
mluis 3:9c6f7f082151 513 {
mluis 3:9c6f7f082151 514 if( mibReq.Param.IsNetworkJoined == true )
mluis 3:9c6f7f082151 515 {
mluis 3:9c6f7f082151 516 DeviceState = DEVICE_STATE_SEND;
mluis 3:9c6f7f082151 517 NextTx = true;
mluis 3:9c6f7f082151 518 }
mluis 3:9c6f7f082151 519 else
mluis 3:9c6f7f082151 520 {
mluis 3:9c6f7f082151 521 DeviceState = DEVICE_STATE_JOIN;
mluis 3:9c6f7f082151 522 }
mluis 3:9c6f7f082151 523 }
mluis 0:92bca02df485 524 }
mluis 0:92bca02df485 525
mluis 0:92bca02df485 526 /*!
mluis 0:92bca02df485 527 * \brief Function executed on Led 1 Timeout event
mluis 0:92bca02df485 528 */
mluis 0:92bca02df485 529 static void OnLed1TimerEvent( void )
mluis 0:92bca02df485 530 {
mluis 0:92bca02df485 531 TimerStop( &Led1Timer );
mluis 3:9c6f7f082151 532 // Switch LED 1 OFF
mluis 1:352f608c3337 533 Led1State = false;
mluis 0:92bca02df485 534 Led1StateChanged = true;
mluis 0:92bca02df485 535 }
mluis 0:92bca02df485 536
mluis 0:92bca02df485 537 /*!
mluis 0:92bca02df485 538 * \brief Function executed on Led 2 Timeout event
mluis 0:92bca02df485 539 */
mluis 0:92bca02df485 540 static void OnLed2TimerEvent( void )
mluis 0:92bca02df485 541 {
mluis 0:92bca02df485 542 TimerStop( &Led2Timer );
mluis 3:9c6f7f082151 543 // Switch LED 2 OFF
mluis 1:352f608c3337 544 Led2State = false;
mluis 0:92bca02df485 545 Led2StateChanged = true;
mluis 0:92bca02df485 546 }
mluis 0:92bca02df485 547
mluis 0:92bca02df485 548 /*!
mluis 3:9c6f7f082151 549 * \brief MCPS-Confirm event function
mluis 3:9c6f7f082151 550 *
mluis 5:1e9f6a365854 551 * \param [IN] mcpsConfirm - Pointer to the confirm structure,
mluis 3:9c6f7f082151 552 * containing confirm attributes.
mluis 0:92bca02df485 553 */
mluis 5:1e9f6a365854 554 static void McpsConfirm( McpsConfirm_t *mcpsConfirm )
mluis 0:92bca02df485 555 {
mluis 5:1e9f6a365854 556 if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:92bca02df485 557 {
mluis 5:1e9f6a365854 558 switch( mcpsConfirm->McpsRequest )
mluis 3:9c6f7f082151 559 {
mluis 3:9c6f7f082151 560 case MCPS_UNCONFIRMED:
mluis 3:9c6f7f082151 561 {
mluis 3:9c6f7f082151 562 // Check Datarate
mluis 3:9c6f7f082151 563 // Check TxPower
mluis 3:9c6f7f082151 564 break;
mluis 3:9c6f7f082151 565 }
mluis 3:9c6f7f082151 566 case MCPS_CONFIRMED:
mluis 3:9c6f7f082151 567 {
mluis 3:9c6f7f082151 568 // Check Datarate
mluis 3:9c6f7f082151 569 // Check TxPower
mluis 3:9c6f7f082151 570 // Check AckReceived
mluis 5:1e9f6a365854 571 // Check NbTrials
mluis 5:1e9f6a365854 572 LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived;
mluis 3:9c6f7f082151 573 break;
mluis 3:9c6f7f082151 574 }
mluis 3:9c6f7f082151 575 case MCPS_PROPRIETARY:
mluis 3:9c6f7f082151 576 {
mluis 3:9c6f7f082151 577 break;
mluis 3:9c6f7f082151 578 }
mluis 3:9c6f7f082151 579 default:
mluis 3:9c6f7f082151 580 break;
mluis 3:9c6f7f082151 581 }
mluis 5:1e9f6a365854 582 LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate;
mluis 5:1e9f6a365854 583 LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter;
mluis 5:1e9f6a365854 584
mluis 7:3173f0508a98 585 // Switch LED 1 ON
mluis 7:3173f0508a98 586 Led1State = true;
mluis 7:3173f0508a98 587 Led1StateChanged = true;
mluis 7:3173f0508a98 588 TimerStart( &Led1Timer );
mluis 7:3173f0508a98 589
mluis 3:9c6f7f082151 590 UplinkStatusUpdated = true;
mluis 3:9c6f7f082151 591 }
mluis 3:9c6f7f082151 592 NextTx = true;
mluis 3:9c6f7f082151 593 }
mluis 3:9c6f7f082151 594
mluis 3:9c6f7f082151 595 /*!
mluis 3:9c6f7f082151 596 * \brief MCPS-Indication event function
mluis 3:9c6f7f082151 597 *
mluis 5:1e9f6a365854 598 * \param [IN] mcpsIndication - Pointer to the indication structure,
mluis 3:9c6f7f082151 599 * containing indication attributes.
mluis 3:9c6f7f082151 600 */
mluis 5:1e9f6a365854 601 static void McpsIndication( McpsIndication_t *mcpsIndication )
mluis 3:9c6f7f082151 602 {
mluis 5:1e9f6a365854 603 if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
mluis 3:9c6f7f082151 604 {
mluis 3:9c6f7f082151 605 return;
mluis 3:9c6f7f082151 606 }
mluis 3:9c6f7f082151 607
mluis 5:1e9f6a365854 608 switch( mcpsIndication->McpsIndication )
mluis 3:9c6f7f082151 609 {
mluis 3:9c6f7f082151 610 case MCPS_UNCONFIRMED:
mluis 3:9c6f7f082151 611 {
mluis 3:9c6f7f082151 612 break;
mluis 3:9c6f7f082151 613 }
mluis 3:9c6f7f082151 614 case MCPS_CONFIRMED:
mluis 3:9c6f7f082151 615 {
mluis 3:9c6f7f082151 616 break;
mluis 3:9c6f7f082151 617 }
mluis 3:9c6f7f082151 618 case MCPS_PROPRIETARY:
mluis 3:9c6f7f082151 619 {
mluis 3:9c6f7f082151 620 break;
mluis 3:9c6f7f082151 621 }
mluis 3:9c6f7f082151 622 case MCPS_MULTICAST:
mluis 3:9c6f7f082151 623 {
mluis 3:9c6f7f082151 624 break;
mluis 3:9c6f7f082151 625 }
mluis 3:9c6f7f082151 626 default:
mluis 3:9c6f7f082151 627 break;
mluis 3:9c6f7f082151 628 }
mluis 3:9c6f7f082151 629
mluis 3:9c6f7f082151 630 // Check Multicast
mluis 3:9c6f7f082151 631 // Check Port
mluis 3:9c6f7f082151 632 // Check Datarate
mluis 3:9c6f7f082151 633 // Check FramePending
mluis 3:9c6f7f082151 634 // Check Buffer
mluis 3:9c6f7f082151 635 // Check BufferSize
mluis 3:9c6f7f082151 636 // Check Rssi
mluis 3:9c6f7f082151 637 // Check Snr
mluis 3:9c6f7f082151 638 // Check RxSlot
mluis 5:1e9f6a365854 639 LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi;
mluis 5:1e9f6a365854 640 if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
mluis 3:9c6f7f082151 641 {
mluis 3:9c6f7f082151 642 // Invert and divide by 4
mluis 5:1e9f6a365854 643 LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2;
mluis 3:9c6f7f082151 644 LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
mluis 0:92bca02df485 645 }
mluis 0:92bca02df485 646 else
mluis 0:92bca02df485 647 {
mluis 3:9c6f7f082151 648 // Divide by 4
mluis 5:1e9f6a365854 649 LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2;
mluis 3:9c6f7f082151 650 }
mluis 3:9c6f7f082151 651 LoRaMacDownlinkStatus.DownlinkCounter++;
mluis 5:1e9f6a365854 652 LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData;
mluis 5:1e9f6a365854 653 LoRaMacDownlinkStatus.Port = mcpsIndication->Port;
mluis 5:1e9f6a365854 654 LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer;
mluis 5:1e9f6a365854 655 LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize;
mluis 3:9c6f7f082151 656
mluis 3:9c6f7f082151 657 if( ComplianceTest.Running == true )
mluis 3:9c6f7f082151 658 {
mluis 3:9c6f7f082151 659 ComplianceTest.DownLinkCounter++;
mluis 3:9c6f7f082151 660 }
mluis 0:92bca02df485 661
mluis 5:1e9f6a365854 662 if( mcpsIndication->RxData == true )
mluis 3:9c6f7f082151 663 {
mluis 5:1e9f6a365854 664 switch( mcpsIndication->Port )
mluis 0:92bca02df485 665 {
mluis 3:9c6f7f082151 666 case 1: // The application LED can be controlled on port 1 or 2
mluis 3:9c6f7f082151 667 case 2:
mluis 5:1e9f6a365854 668 if( mcpsIndication->BufferSize == 1 )
mluis 1:352f608c3337 669 {
mluis 5:1e9f6a365854 670 AppLedStateOn = mcpsIndication->Buffer[0] & 0x01;
mluis 3:9c6f7f082151 671 Led3StateChanged = true;
mluis 3:9c6f7f082151 672 }
mluis 3:9c6f7f082151 673 break;
mluis 3:9c6f7f082151 674 case 224:
mluis 3:9c6f7f082151 675 if( ComplianceTest.Running == false )
mluis 3:9c6f7f082151 676 {
mluis 3:9c6f7f082151 677 // Check compliance test enable command (i)
mluis 5:1e9f6a365854 678 if( ( mcpsIndication->BufferSize == 4 ) &&
mluis 5:1e9f6a365854 679 ( mcpsIndication->Buffer[0] == 0x01 ) &&
mluis 5:1e9f6a365854 680 ( mcpsIndication->Buffer[1] == 0x01 ) &&
mluis 5:1e9f6a365854 681 ( mcpsIndication->Buffer[2] == 0x01 ) &&
mluis 5:1e9f6a365854 682 ( mcpsIndication->Buffer[3] == 0x01 ) )
mluis 1:352f608c3337 683 {
mluis 3:9c6f7f082151 684 IsTxConfirmed = false;
mluis 3:9c6f7f082151 685 AppPort = 224;
mluis 3:9c6f7f082151 686 AppDataSize = 2;
mluis 3:9c6f7f082151 687 ComplianceTest.DownLinkCounter = 0;
mluis 3:9c6f7f082151 688 ComplianceTest.LinkCheck = false;
mluis 3:9c6f7f082151 689 ComplianceTest.DemodMargin = 0;
mluis 3:9c6f7f082151 690 ComplianceTest.NbGateways = 0;
mluis 3:9c6f7f082151 691 ComplianceTest.Running = true;
mluis 3:9c6f7f082151 692 ComplianceTest.State = 1;
mluis 9:ee9dcbb9708d 693
mluis 3:9c6f7f082151 694 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 695 mibReq.Type = MIB_ADR;
mluis 3:9c6f7f082151 696 mibReq.Param.AdrEnable = true;
mluis 3:9c6f7f082151 697 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 698
mluis 3:9c6f7f082151 699 #if defined( USE_BAND_868 )
mluis 3:9c6f7f082151 700 LoRaMacTestSetDutyCycleOn( false );
mluis 3:9c6f7f082151 701 #endif
mluis 1:352f608c3337 702 }
mluis 1:352f608c3337 703 }
mluis 0:92bca02df485 704 else
mluis 0:92bca02df485 705 {
mluis 5:1e9f6a365854 706 ComplianceTest.State = mcpsIndication->Buffer[0];
mluis 3:9c6f7f082151 707 switch( ComplianceTest.State )
mluis 3:9c6f7f082151 708 {
mluis 3:9c6f7f082151 709 case 0: // Check compliance test disable command (ii)
mluis 3:9c6f7f082151 710 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 3:9c6f7f082151 711 AppPort = LORAWAN_APP_PORT;
mluis 3:9c6f7f082151 712 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 3:9c6f7f082151 713 ComplianceTest.DownLinkCounter = 0;
mluis 3:9c6f7f082151 714 ComplianceTest.Running = false;
mluis 9:ee9dcbb9708d 715
mluis 3:9c6f7f082151 716 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 717 mibReq.Type = MIB_ADR;
mluis 3:9c6f7f082151 718 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 3:9c6f7f082151 719 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 720 #if defined( USE_BAND_868 )
mluis 3:9c6f7f082151 721 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 3:9c6f7f082151 722 #endif
mluis 3:9c6f7f082151 723 break;
mluis 3:9c6f7f082151 724 case 1: // (iii, iv)
mluis 3:9c6f7f082151 725 AppDataSize = 2;
mluis 3:9c6f7f082151 726 break;
mluis 3:9c6f7f082151 727 case 2: // Enable confirmed messages (v)
mluis 3:9c6f7f082151 728 IsTxConfirmed = true;
mluis 3:9c6f7f082151 729 ComplianceTest.State = 1;
mluis 3:9c6f7f082151 730 break;
mluis 3:9c6f7f082151 731 case 3: // Disable confirmed messages (vi)
mluis 3:9c6f7f082151 732 IsTxConfirmed = false;
mluis 3:9c6f7f082151 733 ComplianceTest.State = 1;
mluis 3:9c6f7f082151 734 break;
mluis 3:9c6f7f082151 735 case 4: // (vii)
mluis 5:1e9f6a365854 736 AppDataSize = mcpsIndication->BufferSize;
mluis 3:9c6f7f082151 737
mluis 3:9c6f7f082151 738 AppData[0] = 4;
mluis 3:9c6f7f082151 739 for( uint8_t i = 1; i < AppDataSize; i++ )
mluis 3:9c6f7f082151 740 {
mluis 5:1e9f6a365854 741 AppData[i] = mcpsIndication->Buffer[i] + 1;
mluis 3:9c6f7f082151 742 }
mluis 3:9c6f7f082151 743 break;
mluis 3:9c6f7f082151 744 case 5: // (viii)
mluis 3:9c6f7f082151 745 {
mluis 3:9c6f7f082151 746 MlmeReq_t mlmeReq;
mluis 3:9c6f7f082151 747 mlmeReq.Type = MLME_LINK_CHECK;
mluis 3:9c6f7f082151 748 LoRaMacMlmeRequest( &mlmeReq );
mluis 3:9c6f7f082151 749 }
mluis 3:9c6f7f082151 750 break;
mluis 7:3173f0508a98 751 case 6: // (ix)
mluis 7:3173f0508a98 752 {
mluis 7:3173f0508a98 753 MlmeReq_t mlmeReq;
mluis 7:3173f0508a98 754
mluis 9:ee9dcbb9708d 755 // Disable TestMode and revert back to normal operation
mluis 9:ee9dcbb9708d 756 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 9:ee9dcbb9708d 757 AppPort = LORAWAN_APP_PORT;
mluis 9:ee9dcbb9708d 758 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 9:ee9dcbb9708d 759 ComplianceTest.DownLinkCounter = 0;
mluis 9:ee9dcbb9708d 760 ComplianceTest.Running = false;
mluis 9:ee9dcbb9708d 761
mluis 9:ee9dcbb9708d 762 MibRequestConfirm_t mibReq;
mluis 9:ee9dcbb9708d 763 mibReq.Type = MIB_ADR;
mluis 9:ee9dcbb9708d 764 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 9:ee9dcbb9708d 765 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 9:ee9dcbb9708d 766 #if defined( USE_BAND_868 )
mluis 9:ee9dcbb9708d 767 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 9:ee9dcbb9708d 768 #endif
mluis 9:ee9dcbb9708d 769
mluis 7:3173f0508a98 770 mlmeReq.Type = MLME_JOIN;
mluis 7:3173f0508a98 771
mluis 7:3173f0508a98 772 mlmeReq.Req.Join.DevEui = DevEui;
mluis 7:3173f0508a98 773 mlmeReq.Req.Join.AppEui = AppEui;
mluis 7:3173f0508a98 774 mlmeReq.Req.Join.AppKey = AppKey;
mluis 9:ee9dcbb9708d 775 mlmeReq.Req.Join.NbTrials = 3;
mluis 7:3173f0508a98 776
mluis 7:3173f0508a98 777 LoRaMacMlmeRequest( &mlmeReq );
mluis 7:3173f0508a98 778 DeviceState = DEVICE_STATE_SLEEP;
mluis 7:3173f0508a98 779 }
mluis 7:3173f0508a98 780 break;
mluis 9:ee9dcbb9708d 781 case 7: // (x)
mluis 9:ee9dcbb9708d 782 {
mluis 9:ee9dcbb9708d 783 if( mcpsIndication->BufferSize == 3 )
mluis 9:ee9dcbb9708d 784 {
mluis 9:ee9dcbb9708d 785 MlmeReq_t mlmeReq;
mluis 9:ee9dcbb9708d 786 mlmeReq.Type = MLME_TXCW;
mluis 9:ee9dcbb9708d 787 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 9:ee9dcbb9708d 788 LoRaMacMlmeRequest( &mlmeReq );
mluis 9:ee9dcbb9708d 789 }
mluis 9:ee9dcbb9708d 790 else if( mcpsIndication->BufferSize == 7 )
mluis 9:ee9dcbb9708d 791 {
mluis 9:ee9dcbb9708d 792 MlmeReq_t mlmeReq;
mluis 9:ee9dcbb9708d 793 mlmeReq.Type = MLME_TXCW_1;
mluis 9:ee9dcbb9708d 794 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 9:ee9dcbb9708d 795 mlmeReq.Req.TxCw.Frequency = ( uint32_t )( ( mcpsIndication->Buffer[3] << 16 ) | ( mcpsIndication->Buffer[4] << 8 ) | mcpsIndication->Buffer[5] ) * 100;
mluis 9:ee9dcbb9708d 796 mlmeReq.Req.TxCw.Power = mcpsIndication->Buffer[6];
mluis 9:ee9dcbb9708d 797 LoRaMacMlmeRequest( &mlmeReq );
mluis 9:ee9dcbb9708d 798 }
mluis 9:ee9dcbb9708d 799 ComplianceTest.State = 1;
mluis 9:ee9dcbb9708d 800 }
mluis 9:ee9dcbb9708d 801 break;
mluis 3:9c6f7f082151 802 default:
mluis 3:9c6f7f082151 803 break;
mluis 3:9c6f7f082151 804 }
mluis 0:92bca02df485 805 }
mluis 3:9c6f7f082151 806 break;
mluis 3:9c6f7f082151 807 default:
mluis 3:9c6f7f082151 808 break;
mluis 0:92bca02df485 809 }
mluis 0:92bca02df485 810 }
mluis 1:352f608c3337 811
mluis 3:9c6f7f082151 812 // Switch LED 2 ON for each received downlink
mluis 3:9c6f7f082151 813 Led2State = true;
mluis 3:9c6f7f082151 814 Led2StateChanged = true;
mluis 3:9c6f7f082151 815 TimerStart( &Led2Timer );
mluis 3:9c6f7f082151 816 DownlinkStatusUpdated = true;
mluis 3:9c6f7f082151 817 }
mluis 3:9c6f7f082151 818
mluis 3:9c6f7f082151 819 /*!
mluis 3:9c6f7f082151 820 * \brief MLME-Confirm event function
mluis 3:9c6f7f082151 821 *
mluis 5:1e9f6a365854 822 * \param [IN] mlmeConfirm - Pointer to the confirm structure,
mluis 3:9c6f7f082151 823 * containing confirm attributes.
mluis 3:9c6f7f082151 824 */
mluis 5:1e9f6a365854 825 static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
mluis 3:9c6f7f082151 826 {
mluis 9:ee9dcbb9708d 827 switch( mlmeConfirm->MlmeRequest )
mluis 3:9c6f7f082151 828 {
mluis 9:ee9dcbb9708d 829 case MLME_JOIN:
mluis 3:9c6f7f082151 830 {
mluis 9:ee9dcbb9708d 831 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 3:9c6f7f082151 832 {
mluis 3:9c6f7f082151 833 // Status is OK, node has joined the network
mluis 3:9c6f7f082151 834 IsNetworkJoinedStatusUpdate = true;
mluis 7:3173f0508a98 835 DeviceState = DEVICE_STATE_SEND;
uss1994 10:9a4efdd07a77 836
uss1994 10:9a4efdd07a77 837
uss1994 10:9a4efdd07a77 838 MessageType = MESSAGE_TYPE_HB;
uss1994 10:9a4efdd07a77 839 joining = false;
uss1994 10:9a4efdd07a77 840 next_stest = current_time + 5;
uss1994 10:9a4efdd07a77 841 next_ltest = current_time + long_interval;
mluis 9:ee9dcbb9708d 842 }
mluis 9:ee9dcbb9708d 843 else
mluis 9:ee9dcbb9708d 844 {
mluis 9:ee9dcbb9708d 845 // Join was not successful. Try to join again
mluis 9:ee9dcbb9708d 846 DeviceState = DEVICE_STATE_JOIN;
mluis 3:9c6f7f082151 847 }
mluis 9:ee9dcbb9708d 848 break;
mluis 9:ee9dcbb9708d 849 }
mluis 9:ee9dcbb9708d 850 case MLME_LINK_CHECK:
mluis 9:ee9dcbb9708d 851 {
mluis 9:ee9dcbb9708d 852 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 3:9c6f7f082151 853 {
mluis 3:9c6f7f082151 854 // Check DemodMargin
mluis 3:9c6f7f082151 855 // Check NbGateways
mluis 3:9c6f7f082151 856 if( ComplianceTest.Running == true )
mluis 3:9c6f7f082151 857 {
mluis 3:9c6f7f082151 858 ComplianceTest.LinkCheck = true;
mluis 5:1e9f6a365854 859 ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin;
mluis 5:1e9f6a365854 860 ComplianceTest.NbGateways = mlmeConfirm->NbGateways;
mluis 3:9c6f7f082151 861 }
mluis 3:9c6f7f082151 862 }
mluis 9:ee9dcbb9708d 863 break;
mluis 3:9c6f7f082151 864 }
mluis 9:ee9dcbb9708d 865 default:
mluis 9:ee9dcbb9708d 866 break;
mluis 3:9c6f7f082151 867 }
mluis 3:9c6f7f082151 868 NextTx = true;
mluis 3:9c6f7f082151 869 UplinkStatusUpdated = true;
mluis 0:92bca02df485 870 }
mluis 0:92bca02df485 871
uss1994 10:9a4efdd07a77 872
uss1994 10:9a4efdd07a77 873 // **************************** TEST METHODS ******************************** //
uss1994 10:9a4efdd07a77 874
uss1994 10:9a4efdd07a77 875 void startTest(uint8_t *measurements) {
uss1994 10:9a4efdd07a77 876
uss1994 16:e1d692816864 877 // Determine test length
uss1994 10:9a4efdd07a77 878 switch (MessageType)
uss1994 10:9a4efdd07a77 879 {
uss1994 10:9a4efdd07a77 880 case MESSAGE_TYPE_SHORT_TEST:
uss1994 10:9a4efdd07a77 881 test_duration = 30;
uss1994 10:9a4efdd07a77 882 break;
uss1994 10:9a4efdd07a77 883 case MESSAGE_TYPE_LONG_TEST:
uss1994 10:9a4efdd07a77 884 test_duration = 5400;
uss1994 10:9a4efdd07a77 885 break;
uss1994 10:9a4efdd07a77 886 default:
uss1994 10:9a4efdd07a77 887 test_duration = 0;
uss1994 10:9a4efdd07a77 888 break;
uss1994 10:9a4efdd07a77 889 }
uss1994 10:9a4efdd07a77 890
uss1994 10:9a4efdd07a77 891 // Measure voltage preval
uss1994 20:e75b3340d1c7 892 float battery_reading = BAT_PIN.read()*1000;
uss1994 20:e75b3340d1c7 893 measurements[0] = (uint8_t) rintf(battery_reading/10);
uss1994 10:9a4efdd07a77 894
uss1994 10:9a4efdd07a77 895 // Start test
uss1994 10:9a4efdd07a77 896 relayPin = 1;
uss1994 12:c1cf0e717684 897 AppLedStateOn = 1;
uss1994 12:c1cf0e717684 898 Led3StateChanged = true;
uss1994 10:9a4efdd07a77 899 }
uss1994 10:9a4efdd07a77 900
uss1994 10:9a4efdd07a77 901 void endTest(uint8_t *measurements) {
uss1994 10:9a4efdd07a77 902 // Measure endvals
uss1994 20:e75b3340d1c7 903 float battery_reading = BAT_PIN.read()*1000;
uss1994 10:9a4efdd07a77 904 int vce_reading = VCE_PIN.read()*1000;
uss1994 10:9a4efdd07a77 905 int light_1_reading = LIGHT_1_PIN.read()*1000 - vce_reading;
uss1994 10:9a4efdd07a77 906 int light_2_reading = LIGHT_2_PIN.read()*1000 - vce_reading;
uss1994 19:6f88d1cabaca 907 int rh_reading = RH_PIN.read()*1000 - vce_reading;
uss1994 10:9a4efdd07a77 908
uss1994 20:e75b3340d1c7 909 measurements[1] = (uint8_t) rintf(battery_reading/10);
uss1994 16:e1d692816864 910 measurements[2] = (uint8_t) light_1_reading;
uss1994 16:e1d692816864 911 measurements[3] = (uint8_t) light_2_reading;
uss1994 19:6f88d1cabaca 912 measurements[4] = (uint8_t) rh_reading;
uss1994 10:9a4efdd07a77 913
uss1994 10:9a4efdd07a77 914 // End test
uss1994 10:9a4efdd07a77 915 relayPin = 0;
uss1994 12:c1cf0e717684 916 AppLedStateOn = 0;
uss1994 12:c1cf0e717684 917 Led3StateChanged = true;
uss1994 10:9a4efdd07a77 918 }
uss1994 10:9a4efdd07a77 919
uss1994 11:e433cbbfd436 920 void button_options_handler(void)
uss1994 11:e433cbbfd436 921 {
uss1994 11:e433cbbfd436 922 if (press_count >= 3) {
uss1994 11:e433cbbfd436 923 // Run long test
uss1994 11:e433cbbfd436 924 MessageType = MESSAGE_TYPE_LONG_TEST;
uss1994 11:e433cbbfd436 925
uss1994 11:e433cbbfd436 926 startTest(measurements);
uss1994 11:e433cbbfd436 927
uss1994 11:e433cbbfd436 928 running_test = true;
uss1994 11:e433cbbfd436 929 test_start = current_time;
uss1994 11:e433cbbfd436 930 }
uss1994 11:e433cbbfd436 931 if (press_count == 2) {
uss1994 11:e433cbbfd436 932 // Run short test
uss1994 11:e433cbbfd436 933 MessageType = MESSAGE_TYPE_SHORT_TEST;
uss1994 11:e433cbbfd436 934
uss1994 11:e433cbbfd436 935 startTest(measurements);
uss1994 11:e433cbbfd436 936
uss1994 11:e433cbbfd436 937 running_test = true;
uss1994 11:e433cbbfd436 938 test_start = current_time;
uss1994 11:e433cbbfd436 939 }
uss1994 11:e433cbbfd436 940
uss1994 11:e433cbbfd436 941 // Reset count
uss1994 11:e433cbbfd436 942 press_count = 0;
uss1994 11:e433cbbfd436 943 }
uss1994 11:e433cbbfd436 944
amirchaudhary 13:831ffc0ea56c 945 /**
amirchaudhary 13:831ffc0ea56c 946 * Main application entry point.
amirchaudhary 13:831ffc0ea56c 947 */
amirchaudhary 13:831ffc0ea56c 948 Serial pc(SERIAL_TX, SERIAL_RX,115200);
amirchaudhary 13:831ffc0ea56c 949 int MY_SetSysClock_PLL_HSE(void)
amirchaudhary 13:831ffc0ea56c 950 {
amirchaudhary 13:831ffc0ea56c 951 RCC_ClkInitTypeDef RCC_ClkInitStruct;
amirchaudhary 13:831ffc0ea56c 952 RCC_OscInitTypeDef RCC_OscInitStruct;
amirchaudhary 13:831ffc0ea56c 953
amirchaudhary 13:831ffc0ea56c 954 /* Enable HSE and activate PLL with HSE as source */
amirchaudhary 13:831ffc0ea56c 955 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
amirchaudhary 13:831ffc0ea56c 956 RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */
amirchaudhary 13:831ffc0ea56c 957
amirchaudhary 13:831ffc0ea56c 958 // PLLCLK = (8 MHz * 8)/2 = 32 MHz
amirchaudhary 13:831ffc0ea56c 959 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
amirchaudhary 13:831ffc0ea56c 960 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
amirchaudhary 13:831ffc0ea56c 961 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_8;
amirchaudhary 13:831ffc0ea56c 962 RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
amirchaudhary 13:831ffc0ea56c 963 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
amirchaudhary 13:831ffc0ea56c 964 return (-1); // FAIL
amirchaudhary 13:831ffc0ea56c 965 }
amirchaudhary 13:831ffc0ea56c 966
amirchaudhary 13:831ffc0ea56c 967 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
amirchaudhary 13:831ffc0ea56c 968 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
amirchaudhary 13:831ffc0ea56c 969 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 32 MHz
amirchaudhary 13:831ffc0ea56c 970 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 32 MHz
amirchaudhary 13:831ffc0ea56c 971 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 32 MHz
amirchaudhary 13:831ffc0ea56c 972 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 32 MHz
amirchaudhary 13:831ffc0ea56c 973 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
amirchaudhary 13:831ffc0ea56c 974 return (-2); // FAIL
amirchaudhary 13:831ffc0ea56c 975 }
amirchaudhary 13:831ffc0ea56c 976
amirchaudhary 13:831ffc0ea56c 977 /* Enable HSE and activate PLL with HSE as source */
amirchaudhary 13:831ffc0ea56c 978 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI;
amirchaudhary 13:831ffc0ea56c 979 RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
amirchaudhary 13:831ffc0ea56c 980 RCC_OscInitStruct.MSIState = RCC_MSI_OFF;
amirchaudhary 13:831ffc0ea56c 981 RCC_OscInitStruct.HSI48State = RCC_HSI48_OFF;
amirchaudhary 13:831ffc0ea56c 982 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
amirchaudhary 13:831ffc0ea56c 983 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
amirchaudhary 13:831ffc0ea56c 984 return (-3); // FAIL
amirchaudhary 13:831ffc0ea56c 985 }
amirchaudhary 13:831ffc0ea56c 986
amirchaudhary 13:831ffc0ea56c 987 return 0; // OK
amirchaudhary 13:831ffc0ea56c 988 }
amirchaudhary 13:831ffc0ea56c 989
amirchaudhary 13:831ffc0ea56c 990 void my_patch(void)
amirchaudhary 13:831ffc0ea56c 991 {
amirchaudhary 13:831ffc0ea56c 992 int retVal;
amirchaudhary 13:831ffc0ea56c 993
amirchaudhary 13:831ffc0ea56c 994 // Put device into default clock, i.e using MSI = 2MHz
amirchaudhary 13:831ffc0ea56c 995 HAL_RCC_DeInit();
amirchaudhary 13:831ffc0ea56c 996
amirchaudhary 13:831ffc0ea56c 997 // Enable HSE clock
amirchaudhary 13:831ffc0ea56c 998 retVal = MY_SetSysClock_PLL_HSE();
amirchaudhary 13:831ffc0ea56c 999 if(retVal< 0)
amirchaudhary 13:831ffc0ea56c 1000 {
amirchaudhary 13:831ffc0ea56c 1001 // fail
amirchaudhary 13:831ffc0ea56c 1002 //pc.printf("Failed to start HSE, ERR= %d\r\n", retVal);
amirchaudhary 13:831ffc0ea56c 1003
amirchaudhary 13:831ffc0ea56c 1004 // indicate error
amirchaudhary 13:831ffc0ea56c 1005 while(1)
amirchaudhary 13:831ffc0ea56c 1006 {
amirchaudhary 13:831ffc0ea56c 1007 myled = 1;
amirchaudhary 13:831ffc0ea56c 1008 wait(0.2);
amirchaudhary 13:831ffc0ea56c 1009 myled = 0;
amirchaudhary 13:831ffc0ea56c 1010 wait(0.5);
amirchaudhary 13:831ffc0ea56c 1011 }
amirchaudhary 13:831ffc0ea56c 1012 }
amirchaudhary 13:831ffc0ea56c 1013 }
amirchaudhary 13:831ffc0ea56c 1014
amirchaudhary 13:831ffc0ea56c 1015
amirchaudhary 13:831ffc0ea56c 1016
amirchaudhary 13:831ffc0ea56c 1017
mluis 0:92bca02df485 1018 int main( void )
mluis 0:92bca02df485 1019 {
amirchaudhary 13:831ffc0ea56c 1020 pc.printf("mbed-os-rev: %d.%d.%d lib-rev: %d\r\n", \
amirchaudhary 13:831ffc0ea56c 1021 MBED_MAJOR_VERSION, MBED_MINOR_VERSION,MBED_PATCH_VERSION,MBED_LIBRARY_VERSION);
amirchaudhary 13:831ffc0ea56c 1022 pc.printf("BUILD= %s, SysClock= %d, RCC= %0X\r\n", __TIME__, SystemCoreClock, RCC->CR);
uss1994 17:23caa53e6ec1 1023 my_patch();
amirchaudhary 13:831ffc0ea56c 1024 pc.printf("NEW SysClock= %d, NEW RCC= %0X\r\n", SystemCoreClock, RCC->CR);
amirchaudhary 13:831ffc0ea56c 1025 wait(3);
mluis 3:9c6f7f082151 1026 LoRaMacPrimitives_t LoRaMacPrimitives;
mluis 3:9c6f7f082151 1027 LoRaMacCallback_t LoRaMacCallbacks;
mluis 3:9c6f7f082151 1028 MibRequestConfirm_t mibReq;
mluis 0:92bca02df485 1029
mluis 0:92bca02df485 1030 BoardInit( );
mluis 3:9c6f7f082151 1031 SerialDisplayInit( );
mluis 0:92bca02df485 1032
mluis 7:3173f0508a98 1033 SerialDisplayUpdateEui( 5, DevEui );
mluis 7:3173f0508a98 1034 SerialDisplayUpdateEui( 6, AppEui );
mluis 7:3173f0508a98 1035 SerialDisplayUpdateKey( 7, AppKey );
mluis 7:3173f0508a98 1036
mluis 7:3173f0508a98 1037 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 7:3173f0508a98 1038 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 7:3173f0508a98 1039 SerialDisplayUpdateDevAddr( DevAddr );
mluis 7:3173f0508a98 1040 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 7:3173f0508a98 1041 SerialDisplayUpdateKey( 13, AppSKey );
mluis 7:3173f0508a98 1042 #endif
mluis 7:3173f0508a98 1043
mluis 3:9c6f7f082151 1044 DeviceState = DEVICE_STATE_INIT;
uss1994 10:9a4efdd07a77 1045 set_time(1514764800);
uss1994 10:9a4efdd07a77 1046 relayPin = 0;
uss1994 10:9a4efdd07a77 1047
uss1994 10:9a4efdd07a77 1048 running_test = false;
uss1994 10:9a4efdd07a77 1049 joining = true;
uss1994 16:e1d692816864 1050 //received_downlink = false;
uss1994 10:9a4efdd07a77 1051 time_t start_time = time(NULL);
uss1994 10:9a4efdd07a77 1052
uss1994 10:9a4efdd07a77 1053 last_hb = start_time;
uss1994 11:e433cbbfd436 1054
uss1994 11:e433cbbfd436 1055 //button1.mode(PullUp); // Activate pull-up
uss1994 11:e433cbbfd436 1056 button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
uss1994 12:c1cf0e717684 1057 AppLedStateOn = 0;
uss1994 12:c1cf0e717684 1058 Led3StateChanged = true;
mluis 0:92bca02df485 1059
mluis 0:92bca02df485 1060 while( 1 )
mluis 0:92bca02df485 1061 {
uss1994 10:9a4efdd07a77 1062 current_time = time(NULL);
mluis 0:92bca02df485 1063 SerialRxProcess( );
mluis 0:92bca02df485 1064 if( IsNetworkJoinedStatusUpdate == true )
mluis 0:92bca02df485 1065 {
mluis 0:92bca02df485 1066 IsNetworkJoinedStatusUpdate = false;
mluis 3:9c6f7f082151 1067 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 1068 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1069 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:92bca02df485 1070 }
mluis 0:92bca02df485 1071 if( Led1StateChanged == true )
mluis 0:92bca02df485 1072 {
mluis 0:92bca02df485 1073 Led1StateChanged = false;
mluis 1:352f608c3337 1074 SerialDisplayUpdateLedState( 1, Led1State );
mluis 0:92bca02df485 1075 }
mluis 0:92bca02df485 1076 if( Led2StateChanged == true )
mluis 0:92bca02df485 1077 {
mluis 0:92bca02df485 1078 Led2StateChanged = false;
mluis 1:352f608c3337 1079 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:92bca02df485 1080 }
mluis 0:92bca02df485 1081 if( Led3StateChanged == true )
mluis 0:92bca02df485 1082 {
mluis 0:92bca02df485 1083 Led3StateChanged = false;
mluis 0:92bca02df485 1084 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:92bca02df485 1085 }
mluis 3:9c6f7f082151 1086 if( UplinkStatusUpdated == true )
mluis 0:92bca02df485 1087 {
mluis 3:9c6f7f082151 1088 UplinkStatusUpdated = false;
mluis 3:9c6f7f082151 1089 SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize );
mluis 3:9c6f7f082151 1090 }
mluis 3:9c6f7f082151 1091 if( DownlinkStatusUpdated == true )
mluis 3:9c6f7f082151 1092 {
mluis 3:9c6f7f082151 1093 DownlinkStatusUpdated = false;
mluis 1:352f608c3337 1094 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:92bca02df485 1095 SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
mluis 0:92bca02df485 1096 }
mluis 3:9c6f7f082151 1097
mluis 3:9c6f7f082151 1098 switch( DeviceState )
mluis 0:92bca02df485 1099 {
mluis 3:9c6f7f082151 1100 case DEVICE_STATE_INIT:
mluis 3:9c6f7f082151 1101 {
mluis 3:9c6f7f082151 1102 LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
mluis 3:9c6f7f082151 1103 LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
mluis 3:9c6f7f082151 1104 LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
mluis 3:9c6f7f082151 1105 LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
mluis 3:9c6f7f082151 1106 LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
mluis 3:9c6f7f082151 1107
mluis 3:9c6f7f082151 1108 TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
mluis 3:9c6f7f082151 1109
mluis 3:9c6f7f082151 1110 TimerInit( &Led1Timer, OnLed1TimerEvent );
mluis 9:ee9dcbb9708d 1111 TimerSetValue( &Led1Timer, 25 );
mluis 3:9c6f7f082151 1112
mluis 3:9c6f7f082151 1113 TimerInit( &Led2Timer, OnLed2TimerEvent );
mluis 9:ee9dcbb9708d 1114 TimerSetValue( &Led2Timer, 25 );
mluis 3:9c6f7f082151 1115
mluis 3:9c6f7f082151 1116 mibReq.Type = MIB_ADR;
mluis 3:9c6f7f082151 1117 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 3:9c6f7f082151 1118 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1119
mluis 3:9c6f7f082151 1120 mibReq.Type = MIB_PUBLIC_NETWORK;
mluis 3:9c6f7f082151 1121 mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
mluis 3:9c6f7f082151 1122 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1123
mluis 3:9c6f7f082151 1124 #if defined( USE_BAND_868 )
mluis 3:9c6f7f082151 1125 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 3:9c6f7f082151 1126 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 5:1e9f6a365854 1127
mluis 9:ee9dcbb9708d 1128 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 5:1e9f6a365854 1129 LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
mluis 5:1e9f6a365854 1130 LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
mluis 5:1e9f6a365854 1131 LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
mluis 5:1e9f6a365854 1132 LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 );
mluis 5:1e9f6a365854 1133 LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 );
mluis 5:1e9f6a365854 1134 LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
mluis 5:1e9f6a365854 1135 LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
mluis 7:3173f0508a98 1136
mluis 9:ee9dcbb9708d 1137 mibReq.Type = MIB_RX2_DEFAULT_CHANNEL;
mluis 9:ee9dcbb9708d 1138 mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
mluis 9:ee9dcbb9708d 1139 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 9:ee9dcbb9708d 1140
mluis 7:3173f0508a98 1141 mibReq.Type = MIB_RX2_CHANNEL;
mluis 7:3173f0508a98 1142 mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
mluis 7:3173f0508a98 1143 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 5:1e9f6a365854 1144 #endif
mluis 5:1e9f6a365854 1145
mluis 3:9c6f7f082151 1146 #endif
mluis 3:9c6f7f082151 1147 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 3:9c6f7f082151 1148 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 3:9c6f7f082151 1149 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 3:9c6f7f082151 1150
mluis 3:9c6f7f082151 1151 LoRaMacDownlinkStatus.DownlinkCounter = 0;
mluis 3:9c6f7f082151 1152
mluis 3:9c6f7f082151 1153 DeviceState = DEVICE_STATE_JOIN;
mluis 3:9c6f7f082151 1154 break;
mluis 3:9c6f7f082151 1155 }
mluis 3:9c6f7f082151 1156 case DEVICE_STATE_JOIN:
mluis 3:9c6f7f082151 1157 {
mluis 3:9c6f7f082151 1158 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 3:9c6f7f082151 1159 MlmeReq_t mlmeReq;
mluis 3:9c6f7f082151 1160
mluis 3:9c6f7f082151 1161 mlmeReq.Type = MLME_JOIN;
mluis 3:9c6f7f082151 1162
mluis 3:9c6f7f082151 1163 mlmeReq.Req.Join.DevEui = DevEui;
mluis 3:9c6f7f082151 1164 mlmeReq.Req.Join.AppEui = AppEui;
mluis 3:9c6f7f082151 1165 mlmeReq.Req.Join.AppKey = AppKey;
mluis 3:9c6f7f082151 1166
mluis 3:9c6f7f082151 1167 if( NextTx == true )
mluis 3:9c6f7f082151 1168 {
mluis 3:9c6f7f082151 1169 LoRaMacMlmeRequest( &mlmeReq );
mluis 3:9c6f7f082151 1170 }
mluis 7:3173f0508a98 1171 DeviceState = DEVICE_STATE_SLEEP;
mluis 3:9c6f7f082151 1172 #else
mluis 3:9c6f7f082151 1173 mibReq.Type = MIB_NET_ID;
mluis 3:9c6f7f082151 1174 mibReq.Param.NetID = LORAWAN_NETWORK_ID;
mluis 3:9c6f7f082151 1175 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1176
mluis 3:9c6f7f082151 1177 mibReq.Type = MIB_DEV_ADDR;
mluis 3:9c6f7f082151 1178 mibReq.Param.DevAddr = DevAddr;
mluis 3:9c6f7f082151 1179 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1180
mluis 3:9c6f7f082151 1181 mibReq.Type = MIB_NWK_SKEY;
mluis 3:9c6f7f082151 1182 mibReq.Param.NwkSKey = NwkSKey;
mluis 3:9c6f7f082151 1183 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1184
mluis 3:9c6f7f082151 1185 mibReq.Type = MIB_APP_SKEY;
mluis 3:9c6f7f082151 1186 mibReq.Param.AppSKey = AppSKey;
mluis 3:9c6f7f082151 1187 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1188
mluis 3:9c6f7f082151 1189 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 1190 mibReq.Param.IsNetworkJoined = true;
mluis 3:9c6f7f082151 1191 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1192
mluis 3:9c6f7f082151 1193 DeviceState = DEVICE_STATE_SEND;
mluis 3:9c6f7f082151 1194 #endif
mluis 3:9c6f7f082151 1195 IsNetworkJoinedStatusUpdate = true;
mluis 3:9c6f7f082151 1196 break;
mluis 3:9c6f7f082151 1197 }
mluis 3:9c6f7f082151 1198 case DEVICE_STATE_SEND:
mluis 3:9c6f7f082151 1199 {
mluis 3:9c6f7f082151 1200 if( NextTx == true )
mluis 3:9c6f7f082151 1201 {
mluis 3:9c6f7f082151 1202 SerialDisplayUpdateUplinkAcked( false );
mluis 3:9c6f7f082151 1203 SerialDisplayUpdateDonwlinkRxData( false );
mluis 3:9c6f7f082151 1204 PrepareTxFrame( AppPort );
mluis 3:9c6f7f082151 1205
mluis 3:9c6f7f082151 1206 NextTx = SendFrame( );
mluis 3:9c6f7f082151 1207 }
mluis 3:9c6f7f082151 1208 if( ComplianceTest.Running == true )
mluis 3:9c6f7f082151 1209 {
mluis 5:1e9f6a365854 1210 // Schedule next packet transmission
mluis 9:ee9dcbb9708d 1211 TxDutyCycleTime = 5000; // 5000 ms
mluis 3:9c6f7f082151 1212 }
mluis 3:9c6f7f082151 1213 else
mluis 3:9c6f7f082151 1214 {
mluis 3:9c6f7f082151 1215 // Schedule next packet transmission
mluis 3:9c6f7f082151 1216 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
mluis 3:9c6f7f082151 1217 }
uss1994 18:0a4de80c02a6 1218
uss1994 18:0a4de80c02a6 1219 if ( NextTx == true )
uss1994 18:0a4de80c02a6 1220 {
uss1994 18:0a4de80c02a6 1221 DeviceState = DEVICE_STATE_SLEEP;
uss1994 18:0a4de80c02a6 1222
uss1994 18:0a4de80c02a6 1223 // Schedule next packet transmission
uss1994 18:0a4de80c02a6 1224 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
uss1994 18:0a4de80c02a6 1225 TimerStart( &TxNextPacketTimer );
uss1994 18:0a4de80c02a6 1226
uss1994 19:6f88d1cabaca 1227 // char my_str[17] = "Scheduled Resend";
uss1994 19:6f88d1cabaca 1228 // SerialDisplayPrintDebugLine( my_str, 17 );
uss1994 18:0a4de80c02a6 1229 }
uss1994 18:0a4de80c02a6 1230 else
uss1994 18:0a4de80c02a6 1231 {
uss1994 18:0a4de80c02a6 1232 DeviceState = DEVICE_STATE_CYCLE;
uss1994 18:0a4de80c02a6 1233 }
uss1994 18:0a4de80c02a6 1234
mluis 3:9c6f7f082151 1235 break;
mluis 3:9c6f7f082151 1236 }
mluis 3:9c6f7f082151 1237 case DEVICE_STATE_CYCLE:
mluis 3:9c6f7f082151 1238 {
uss1994 10:9a4efdd07a77 1239 // DeviceState = DEVICE_STATE_SLEEP;
mluis 5:1e9f6a365854 1240
mluis 3:9c6f7f082151 1241 // Schedule next packet transmission
uss1994 10:9a4efdd07a77 1242 // TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
uss1994 10:9a4efdd07a77 1243 // TimerStart( &TxNextPacketTimer );
uss1994 10:9a4efdd07a77 1244
uss1994 10:9a4efdd07a77 1245
uss1994 10:9a4efdd07a77 1246
uss1994 10:9a4efdd07a77 1247 // Is a test is running
uss1994 10:9a4efdd07a77 1248 if (running_test) {
uss1994 10:9a4efdd07a77 1249 // If it is
uss1994 10:9a4efdd07a77 1250
uss1994 10:9a4efdd07a77 1251 //check if it's time to end the test
uss1994 10:9a4efdd07a77 1252 if (current_time - test_start >= test_duration) {
uss1994 10:9a4efdd07a77 1253 endTest(measurements);
uss1994 10:9a4efdd07a77 1254 // sendResult(measurements, ttype);
uss1994 10:9a4efdd07a77 1255 DeviceState = DEVICE_STATE_SEND;
uss1994 10:9a4efdd07a77 1256 last_hb = current_time;
uss1994 10:9a4efdd07a77 1257
uss1994 10:9a4efdd07a77 1258 // Set the times for the next tests
uss1994 10:9a4efdd07a77 1259 switch ( MessageType )
uss1994 10:9a4efdd07a77 1260 {
uss1994 10:9a4efdd07a77 1261 case MESSAGE_TYPE_SHORT_TEST:
uss1994 10:9a4efdd07a77 1262 {
uss1994 10:9a4efdd07a77 1263 next_stest = test_start + test_interval;
uss1994 10:9a4efdd07a77 1264 break;
uss1994 10:9a4efdd07a77 1265 }
uss1994 10:9a4efdd07a77 1266 default:
uss1994 10:9a4efdd07a77 1267 {
uss1994 10:9a4efdd07a77 1268 // If long test, reset both test types,
uss1994 10:9a4efdd07a77 1269 // to prevent two tests at the same time
uss1994 10:9a4efdd07a77 1270 next_stest = test_start + test_interval;
uss1994 10:9a4efdd07a77 1271 next_ltest = test_start + long_interval;
uss1994 10:9a4efdd07a77 1272 break;
uss1994 10:9a4efdd07a77 1273 }
uss1994 10:9a4efdd07a77 1274 }
uss1994 10:9a4efdd07a77 1275
uss1994 10:9a4efdd07a77 1276 running_test = false;
uss1994 10:9a4efdd07a77 1277 }
uss1994 10:9a4efdd07a77 1278 } else {
uss1994 10:9a4efdd07a77 1279 // If it isn't
uss1994 10:9a4efdd07a77 1280
uss1994 10:9a4efdd07a77 1281 // check if downlink action is required
uss1994 10:9a4efdd07a77 1282
uss1994 10:9a4efdd07a77 1283 //if (received_downlink == true) {
uss1994 10:9a4efdd07a77 1284 // dl_handler(received_data);
uss1994 10:9a4efdd07a77 1285 //}
uss1994 10:9a4efdd07a77 1286
uss1994 11:e433cbbfd436 1287 //check if button is pressed
uss1994 11:e433cbbfd436 1288 if (button1_pressed) { // Set when button is pressed
uss1994 11:e433cbbfd436 1289 if (press_count == 0) {
uss1994 11:e433cbbfd436 1290 buttonOptions_timeout.attach(callback(button_options_handler), 2); // Debounce time 300 ms
uss1994 11:e433cbbfd436 1291 }
uss1994 11:e433cbbfd436 1292 button1_pressed = false;
uss1994 11:e433cbbfd436 1293 press_count++;
uss1994 11:e433cbbfd436 1294 }
uss1994 11:e433cbbfd436 1295
uss1994 10:9a4efdd07a77 1296 //check if it's time to run a test
uss1994 10:9a4efdd07a77 1297 if (current_time >= next_stest || current_time >= next_ltest) {
uss1994 10:9a4efdd07a77 1298
uss1994 10:9a4efdd07a77 1299 // Check what kind of test to run
uss1994 10:9a4efdd07a77 1300 if (current_time >= next_ltest) {
uss1994 10:9a4efdd07a77 1301 MessageType = MESSAGE_TYPE_LONG_TEST;
uss1994 10:9a4efdd07a77 1302 } else {
uss1994 10:9a4efdd07a77 1303 MessageType = MESSAGE_TYPE_SHORT_TEST;
uss1994 10:9a4efdd07a77 1304 }
uss1994 10:9a4efdd07a77 1305
uss1994 10:9a4efdd07a77 1306 startTest(measurements);
uss1994 10:9a4efdd07a77 1307
uss1994 10:9a4efdd07a77 1308 running_test = true;
uss1994 10:9a4efdd07a77 1309 test_start = current_time;
uss1994 10:9a4efdd07a77 1310
uss1994 10:9a4efdd07a77 1311
uss1994 10:9a4efdd07a77 1312 //check if it's time for a heartbeat
uss1994 10:9a4efdd07a77 1313 } else if (current_time - last_hb >= hb_interval) {
uss1994 10:9a4efdd07a77 1314 // sendHb();
uss1994 10:9a4efdd07a77 1315 DeviceState = DEVICE_STATE_SEND;
uss1994 10:9a4efdd07a77 1316 MessageType = MESSAGE_TYPE_HB;
uss1994 10:9a4efdd07a77 1317 last_hb = current_time;
uss1994 10:9a4efdd07a77 1318 }
uss1994 10:9a4efdd07a77 1319 }
uss1994 10:9a4efdd07a77 1320
uss1994 10:9a4efdd07a77 1321
mluis 3:9c6f7f082151 1322 break;
mluis 3:9c6f7f082151 1323 }
mluis 3:9c6f7f082151 1324 case DEVICE_STATE_SLEEP:
mluis 3:9c6f7f082151 1325 {
mluis 3:9c6f7f082151 1326 // Wake up through events
mluis 3:9c6f7f082151 1327 break;
mluis 3:9c6f7f082151 1328 }
mluis 3:9c6f7f082151 1329 default:
mluis 3:9c6f7f082151 1330 {
mluis 3:9c6f7f082151 1331 DeviceState = DEVICE_STATE_INIT;
mluis 3:9c6f7f082151 1332 break;
mluis 3:9c6f7f082151 1333 }
mluis 0:92bca02df485 1334 }
mluis 0:92bca02df485 1335 }
mluis 0:92bca02df485 1336 }