-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
main.c
573 lines (483 loc) · 14.5 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*!
* \file main.c
*
* \brief Performs a periodic uplink
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
* ______ _
* / _____) _ | |
* ( (____ _____ ____ _| |_ _____ ____| |__
* \____ \| ___ | (_ _) ___ |/ ___) _ \
* _____) ) ____| | | || |_| ____( (___| | | |
* (______/|_____)_|_|_| \__)_____)\____)_| |_|
* (C)2013-2018 Semtech
*
* \endcode
*
* \author Miguel Luis ( Semtech )
*/
/*! \file periodic-uplink/NucleoL073/main.c */
#include <stdio.h>
#include "../firmwareVersion.h"
#include "../../common/githubVersion.h"
#include "utilities.h"
#include "board.h"
#include "gpio.h"
#include "uart.h"
#include "RegionCommon.h"
#include "cli.h"
#include "Commissioning.h"
#include "LmHandler.h"
#include "LmhpCompliance.h"
#include "CayenneLpp.h"
#include "LmHandlerMsgDisplay.h"
#ifndef ACTIVE_REGION
#warning "No active region defined, LORAMAC_REGION_EU868 will be used as default."
#define ACTIVE_REGION LORAMAC_REGION_EU868
#endif
/*!
* LoRaWAN default end-device class
*/
#ifndef LORAWAN_DEFAULT_CLASS
#define LORAWAN_DEFAULT_CLASS CLASS_A
#endif
/*!
* Defines the application data transmission duty cycle. 5s, value in [ms].
*/
#define APP_TX_DUTYCYCLE 5000
/*!
* Defines a random delay for application data transmission duty cycle. 1s,
* value in [ms].
*/
#define APP_TX_DUTYCYCLE_RND 1000
/*!
* LoRaWAN Adaptive Data Rate
*
* \remark Please note that when ADR is enabled the end-device should be static
*/
#define LORAWAN_ADR_STATE LORAMAC_HANDLER_ADR_ON
/*!
* Default datarate
*
* \remark Please note that LORAWAN_DEFAULT_DATARATE is used only when ADR is disabled
*/
#define LORAWAN_DEFAULT_DATARATE DR_0
/*!
* LoRaWAN confirmed messages
*/
#define LORAWAN_DEFAULT_CONFIRMED_MSG_STATE LORAMAC_HANDLER_UNCONFIRMED_MSG
/*!
* User application data buffer size
*/
#define LORAWAN_APP_DATA_BUFFER_MAX_SIZE 242
/*!
* LoRaWAN ETSI duty cycle control enable/disable
*
* \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
*/
#define LORAWAN_DUTYCYCLE_ON true
/*!
* LoRaWAN application port
* @remark The allowed port range is from 1 up to 223. Other values are reserved.
*/
#define LORAWAN_APP_PORT 2
/*!
*
*/
typedef enum
{
LORAMAC_HANDLER_TX_ON_TIMER,
LORAMAC_HANDLER_TX_ON_EVENT,
}LmHandlerTxEvents_t;
/*!
* User application data
*/
static uint8_t AppDataBuffer[LORAWAN_APP_DATA_BUFFER_MAX_SIZE];
/*!
* User application data structure
*/
static LmHandlerAppData_t AppData =
{
.Buffer = AppDataBuffer,
.BufferSize = 0,
.Port = 0,
};
/*!
* Specifies the state of the application LED
*/
static bool AppLedStateOn = false;
/*!
* Timer to handle the application data transmission duty cycle
*/
static TimerEvent_t TxTimer;
/*!
* Timer to handle the state of LED1
*/
static TimerEvent_t Led1Timer;
/*!
* Timer to handle the state of LED2
*/
static TimerEvent_t Led2Timer;
/*!
* Timer to handle the state of LED beacon indicator
*/
static TimerEvent_t LedBeaconTimer;
static void OnMacProcessNotify( void );
static void OnNvmDataChange( LmHandlerNvmContextStates_t state, uint16_t size );
static void OnNetworkParametersChange( CommissioningParams_t* params );
static void OnMacMcpsRequest( LoRaMacStatus_t status, McpsReq_t *mcpsReq, TimerTime_t nextTxIn );
static void OnMacMlmeRequest( LoRaMacStatus_t status, MlmeReq_t *mlmeReq, TimerTime_t nextTxIn );
static void OnJoinRequest( LmHandlerJoinParams_t* params );
static void OnTxData( LmHandlerTxParams_t* params );
static void OnRxData( LmHandlerAppData_t* appData, LmHandlerRxParams_t* params );
static void OnClassChange( DeviceClass_t deviceClass );
static void OnBeaconStatusChange( LoRaMacHandlerBeaconParams_t* params );
#if( LMH_SYS_TIME_UPDATE_NEW_API == 1 )
static void OnSysTimeUpdate( bool isSynchronized, int32_t timeCorrection );
#else
static void OnSysTimeUpdate( void );
#endif
static void PrepareTxFrame( void );
static void StartTxProcess( LmHandlerTxEvents_t txEvent );
static void UplinkProcess( void );
static void OnTxPeriodicityChanged( uint32_t periodicity );
static void OnTxFrameCtrlChanged( LmHandlerMsgTypes_t isTxConfirmed );
static void OnPingSlotPeriodicityChanged( uint8_t pingSlotPeriodicity );
/*!
* Function executed on TxTimer event
*/
static void OnTxTimerEvent( void* context );
/*!
* Function executed on Led 1 Timeout event
*/
static void OnLed1TimerEvent( void* context );
/*!
* Function executed on Led 2 Timeout event
*/
static void OnLed2TimerEvent( void* context );
/*!
* \brief Function executed on Beacon timer Timeout event
*/
static void OnLedBeaconTimerEvent( void* context );
static LmHandlerCallbacks_t LmHandlerCallbacks =
{
.GetBatteryLevel = BoardGetBatteryLevel,
.GetTemperature = NULL,
.GetRandomSeed = BoardGetRandomSeed,
.OnMacProcess = OnMacProcessNotify,
.OnNvmDataChange = OnNvmDataChange,
.OnNetworkParametersChange = OnNetworkParametersChange,
.OnMacMcpsRequest = OnMacMcpsRequest,
.OnMacMlmeRequest = OnMacMlmeRequest,
.OnJoinRequest = OnJoinRequest,
.OnTxData = OnTxData,
.OnRxData = OnRxData,
.OnClassChange= OnClassChange,
.OnBeaconStatusChange = OnBeaconStatusChange,
.OnSysTimeUpdate = OnSysTimeUpdate,
};
static LmHandlerParams_t LmHandlerParams =
{
.Region = ACTIVE_REGION,
.AdrEnable = LORAWAN_ADR_STATE,
.IsTxConfirmed = LORAWAN_DEFAULT_CONFIRMED_MSG_STATE,
.TxDatarate = LORAWAN_DEFAULT_DATARATE,
.PublicNetworkEnable = LORAWAN_PUBLIC_NETWORK,
.DutyCycleEnabled = LORAWAN_DUTYCYCLE_ON,
.DataBufferMaxSize = LORAWAN_APP_DATA_BUFFER_MAX_SIZE,
.DataBuffer = AppDataBuffer,
.PingSlotPeriodicity = REGION_COMMON_DEFAULT_PING_SLOT_PERIODICITY,
};
static LmhpComplianceParams_t LmhpComplianceParams =
{
.FwVersion.Value = FIRMWARE_VERSION,
.OnTxPeriodicityChanged = OnTxPeriodicityChanged,
.OnTxFrameCtrlChanged = OnTxFrameCtrlChanged,
.OnPingSlotPeriodicityChanged = OnPingSlotPeriodicityChanged,
};
/*!
* Indicates if LoRaMacProcess call is pending.
*
* \warning If variable is equal to 0 then the MCU can be set in low power mode
*/
static volatile uint8_t IsMacProcessPending = 0;
static volatile uint8_t IsTxFramePending = 0;
static volatile uint32_t TxPeriodicity = 0;
/*!
* LED GPIO pins objects
*/
extern Gpio_t Led1; // Tx
extern Gpio_t Led2; // Rx
/*!
* UART object used for command line interface handling
*/
extern Uart_t Uart2;
/*!
* Main application entry point.
*/
int main( void )
{
BoardInitMcu( );
BoardInitPeriph( );
TimerInit( &Led1Timer, OnLed1TimerEvent );
TimerSetValue( &Led1Timer, 25 );
TimerInit( &Led2Timer, OnLed2TimerEvent );
TimerSetValue( &Led2Timer, 25 );
TimerInit( &LedBeaconTimer, OnLedBeaconTimerEvent );
TimerSetValue( &LedBeaconTimer, 5000 );
// Initialize transmission periodicity variable
TxPeriodicity = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
const Version_t appVersion = { .Value = FIRMWARE_VERSION };
const Version_t gitHubVersion = { .Value = GITHUB_VERSION };
DisplayAppInfo( "periodic-uplink-lpp",
&appVersion,
&gitHubVersion );
if ( LmHandlerInit( &LmHandlerCallbacks, &LmHandlerParams ) != LORAMAC_HANDLER_SUCCESS )
{
printf( "LoRaMac wasn't properly initialized\n" );
// Fatal error, endless loop.
while ( 1 )
{
}
}
// Set system maximum tolerated rx error in milliseconds
LmHandlerSetSystemMaxRxError( 20 );
// The LoRa-Alliance Compliance protocol package should always be
// initialized and activated.
LmHandlerPackageRegister( PACKAGE_ID_COMPLIANCE, &LmhpComplianceParams );
LmHandlerJoin( );
StartTxProcess( LORAMAC_HANDLER_TX_ON_TIMER );
while( 1 )
{
// Process characters sent over the command line interface
CliProcess( &Uart2 );
// Processes the LoRaMac events
LmHandlerProcess( );
// Process application uplinks management
UplinkProcess( );
CRITICAL_SECTION_BEGIN( );
if( IsMacProcessPending == 1 )
{
// Clear flag and prevent MCU to go into low power modes.
IsMacProcessPending = 0;
}
else
{
// The MCU wakes up through events
BoardLowPowerHandler( );
}
CRITICAL_SECTION_END( );
}
}
static void OnMacProcessNotify( void )
{
IsMacProcessPending = 1;
}
static void OnNvmDataChange( LmHandlerNvmContextStates_t state, uint16_t size )
{
DisplayNvmDataChange( state, size );
}
static void OnNetworkParametersChange( CommissioningParams_t* params )
{
DisplayNetworkParametersUpdate( params );
}
static void OnMacMcpsRequest( LoRaMacStatus_t status, McpsReq_t *mcpsReq, TimerTime_t nextTxIn )
{
DisplayMacMcpsRequestUpdate( status, mcpsReq, nextTxIn );
}
static void OnMacMlmeRequest( LoRaMacStatus_t status, MlmeReq_t *mlmeReq, TimerTime_t nextTxIn )
{
DisplayMacMlmeRequestUpdate( status, mlmeReq, nextTxIn );
}
static void OnJoinRequest( LmHandlerJoinParams_t* params )
{
DisplayJoinRequestUpdate( params );
if( params->Status == LORAMAC_HANDLER_ERROR )
{
LmHandlerJoin( );
}
else
{
LmHandlerRequestClass( LORAWAN_DEFAULT_CLASS );
}
}
static void OnTxData( LmHandlerTxParams_t* params )
{
DisplayTxUpdate( params );
}
static void OnRxData( LmHandlerAppData_t* appData, LmHandlerRxParams_t* params )
{
DisplayRxUpdate( appData, params );
switch( appData->Port )
{
case 1: // The application LED can be controlled on port 1 or 2
case LORAWAN_APP_PORT:
{
AppLedStateOn = appData->Buffer[0] & 0x01;
}
break;
default:
break;
}
// Switch LED 2 ON for each received downlink
GpioWrite( &Led2, 1 );
TimerStart( &Led2Timer );
}
static void OnClassChange( DeviceClass_t deviceClass )
{
DisplayClassUpdate( deviceClass );
// Inform the server as soon as possible that the end-device has switched to ClassB
LmHandlerAppData_t appData =
{
.Buffer = NULL,
.BufferSize = 0,
.Port = 0,
};
LmHandlerSend( &appData, LORAMAC_HANDLER_UNCONFIRMED_MSG );
}
static void OnBeaconStatusChange( LoRaMacHandlerBeaconParams_t* params )
{
switch( params->State )
{
case LORAMAC_HANDLER_BEACON_RX:
{
TimerStart( &LedBeaconTimer );
break;
}
case LORAMAC_HANDLER_BEACON_LOST:
case LORAMAC_HANDLER_BEACON_NRX:
{
TimerStop( &LedBeaconTimer );
break;
}
default:
{
break;
}
}
DisplayBeaconUpdate( params );
}
#if( LMH_SYS_TIME_UPDATE_NEW_API == 1 )
static void OnSysTimeUpdate( bool isSynchronized, int32_t timeCorrection )
{
}
#else
static void OnSysTimeUpdate( void )
{
}
#endif
/*!
* Prepares the payload of the frame and transmits it.
*/
static void PrepareTxFrame( void )
{
if( LmHandlerIsBusy( ) == true )
{
return;
}
uint8_t channel = 0;
AppData.Port = LORAWAN_APP_PORT;
CayenneLppReset( );
CayenneLppAddDigitalInput( channel++, AppLedStateOn );
CayenneLppAddAnalogInput( channel++, BoardGetBatteryLevel( ) * 100 / 254 );
CayenneLppCopy( AppData.Buffer );
AppData.BufferSize = CayenneLppGetSize( );
if( LmHandlerSend( &AppData, LmHandlerParams.IsTxConfirmed ) == LORAMAC_HANDLER_SUCCESS )
{
// Switch LED 1 ON
GpioWrite( &Led1, 1 );
TimerStart( &Led1Timer );
}
}
static void StartTxProcess( LmHandlerTxEvents_t txEvent )
{
switch( txEvent )
{
default:
// Intentional fall through
case LORAMAC_HANDLER_TX_ON_TIMER:
{
// Schedule 1st packet transmission
TimerInit( &TxTimer, OnTxTimerEvent );
TimerSetValue( &TxTimer, TxPeriodicity );
OnTxTimerEvent( NULL );
}
break;
case LORAMAC_HANDLER_TX_ON_EVENT:
{
}
break;
}
}
static void UplinkProcess( void )
{
uint8_t isPending = 0;
CRITICAL_SECTION_BEGIN( );
isPending = IsTxFramePending;
IsTxFramePending = 0;
CRITICAL_SECTION_END( );
if( isPending == 1 )
{
PrepareTxFrame( );
}
}
static void OnTxPeriodicityChanged( uint32_t periodicity )
{
TxPeriodicity = periodicity;
if( TxPeriodicity == 0 )
{ // Revert to application default periodicity
TxPeriodicity = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
}
// Update timer periodicity
TimerStop( &TxTimer );
TimerSetValue( &TxTimer, TxPeriodicity );
TimerStart( &TxTimer );
}
static void OnTxFrameCtrlChanged( LmHandlerMsgTypes_t isTxConfirmed )
{
LmHandlerParams.IsTxConfirmed = isTxConfirmed;
}
static void OnPingSlotPeriodicityChanged( uint8_t pingSlotPeriodicity )
{
LmHandlerParams.PingSlotPeriodicity = pingSlotPeriodicity;
}
/*!
* Function executed on TxTimer event
*/
static void OnTxTimerEvent( void* context )
{
TimerStop( &TxTimer );
IsTxFramePending = 1;
// Schedule next transmission
TimerSetValue( &TxTimer, TxPeriodicity );
TimerStart( &TxTimer );
}
/*!
* Function executed on Led 1 Timeout event
*/
static void OnLed1TimerEvent( void* context )
{
TimerStop( &Led1Timer );
// Switch LED 1 OFF
GpioWrite( &Led1, 0 );
}
/*!
* Function executed on Led 2 Timeout event
*/
static void OnLed2TimerEvent( void* context )
{
TimerStop( &Led2Timer );
// Switch LED 2 OFF
GpioWrite( &Led2, 0 );
}
/*!
* \brief Function executed on Beacon timer Timeout event
*/
static void OnLedBeaconTimerEvent( void* context )
{
GpioWrite( &Led2, 1 );
TimerStart( &Led2Timer );
TimerStart( &LedBeaconTimer );
}