Implemented LED Indicator Patterns

Dependencies:   mbed

Committer:
amirchaudhary
Date:
Thu Apr 25 15:41:02 2019 +0000
Revision:
27:f024c8c51c6e
Parent:
26:2cdfd2d309a6
Child:
28:eca152c69c9b
Implemented LED Indicator Patterns ;

Who changed what in which revision?

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