Implemented LED Indicator Patterns

Dependencies:   mbed

Committer:
uss1994
Date:
Wed Jan 23 19:23:44 2019 +0000
Revision:
18:0a4de80c02a6
Parent:
17:23caa53e6ec1
Child:
19:6f88d1cabaca
Better debug & attempt resend if too long b/c of Fopts

Who changed what in which revision?

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