Anish updated the code to work with L152. RTC seems to causing issue. Will have to disable LSE.

Dependencies:   mbed LoRaWAN-lib SX1276Lib

Committer:
uss1994
Date:
Mon Jan 21 19:53:37 2019 +0000
Revision:
15:f4805eb13d73
Parent:
14:9ba8f6975308
Child:
16:e1d692816864
Adding debug

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