Implemented LED Indicator Patterns

Dependencies:   mbed

Committer:
amirchaudhary
Date:
Thu Jan 03 17:06:18 2019 +0000
Revision:
13:831ffc0ea56c
Parent:
12:c1cf0e717684
Child:
14:9ba8f6975308
Button Test 2

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