-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication_task.c
More file actions
498 lines (431 loc) · 17.1 KB
/
Copy pathapplication_task.c
File metadata and controls
498 lines (431 loc) · 17.1 KB
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
/*
* BSD 3-Clause License
*
* Copyright (c) 2026, Northern Mechatronics, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <am_mcu_apollo.h>
#include <am_util.h>
#include <FreeRTOS.h>
#include <queue.h>
#include <task.h>
#include "am_bsp.h"
#include "button.h"
#include "led.h"
#include "lorawan.h"
#include "gpio_cli.h"
#include "application_task.h"
#include "application_task_cli.h"
#if defined(BSP_NM180410) || defined(BSP_NM180411)
#define APPLICATION_LED AM_BSP_GPIO_LED0
#define APPLICATION_LED_TIMER_NUMBER (1)
#define APPLICATION_LED_TIMER_SEGMENT AM_HAL_CTIMER_TIMERA
#define APPLICATION_LED_TIMER_INTERRUPT AM_HAL_CTIMER_INT_TIMERA1C0
#elif defined(BSP_NM180100EVB)
#define APPLICATION_LED AM_BSP_GPIO_LED1
#define APPLICATION_LED_TIMER_NUMBER (2)
#define APPLICATION_LED_TIMER_SEGMENT AM_HAL_CTIMER_TIMERB
#define APPLICATION_LED_TIMER_INTERRUPT AM_HAL_CTIMER_INT_TIMERB2C0
#elif defined(BSP_AP510EVB)
#define APPLICATION_LED AM_BSP_GPIO_LED0
#define APPLICATION_LED_TIMER_NUMBER (0)
#endif
#define APPLICATION_DEFAULT_LORAWAN_CLASS LORAWAN_CLASS_A
#define APPLICATION_QUEUE_MAX_SIZE (10)
#define APPLICATION_RX_BUFFER_MAX_SIZE (256)
#define LORAWAN_PM_ENABLE 1
typedef enum
{
APP_MSG_RX,
APP_MSG_BUTTON_PRESSED,
} application_message_t;
typedef struct
{
uint32_t counter;
uint32_t port;
int32_t slot;
int32_t dr;
int32_t rssi;
int32_t snr;
uint32_t size;
uint8_t *payload;
} application_rx_packet_t;
static uint8_t application_rx_buffer[APPLICATION_RX_BUFFER_MAX_SIZE];
static application_rx_packet_t application_rx_packet;
static TaskHandle_t application_task_handle;
static QueueHandle_t application_queue_handle;
static uint32_t application_led_handle;
static void on_lorawan_join_request(LmHandlerJoinParams_t *params)
{
if (params->Status == LORAMAC_HANDLER_ERROR)
{
lorawan_join();
}
else
{
lorawan_class_set(APPLICATION_DEFAULT_LORAWAN_CLASS);
lorawan_request_time_sync();
led_command_t command = {
.ui32Handle = application_led_handle, .ui32Id = LED_EFFECT_PULSE2, .ui32Repeat = 1};
led_send(&command);
}
}
static void on_lorawan_receive(LmHandlerAppData_t *data, LmHandlerRxParams_t *params)
{
application_message_t msg;
// Class B beacon messages has no data
if (data == NULL)
{
return;
}
// Port 0 is reserved for MAC layer messages
if (data->Port == 0)
{
return;
}
// Deep-copy the received data into a local buffer
application_rx_packet.counter = params->DownlinkCounter;
application_rx_packet.port = data->Port;
application_rx_packet.slot = params->RxSlot;
application_rx_packet.dr = params->Datarate;
application_rx_packet.rssi = params->Rssi;
application_rx_packet.snr = params->Snr;
application_rx_packet.size = data->BufferSize;
application_rx_packet.payload = application_rx_buffer;
memcpy(application_rx_buffer, data->Buffer, data->BufferSize);
// This is executed within the LoRaWAN transport layer context,
// do not block. In this example, we timeout in 10ms if the queue is full.
// Adjust or remove the timeout (set it to zero) to accommodate application
// requirements.
msg = APP_MSG_RX;
xQueueSend(application_queue_handle, &msg, pdMS_TO_TICKS(10));
}
static void on_lorawan_mcps_request(LoRaMacStatus_t status, McpsReq_t *mcps, TimerTime_t delay)
{
if (status == LORAMAC_STATUS_NO_CHANNEL_FOUND)
{
am_util_stdio_printf("on_lorawan_mcps_request: no channel found\r\n");
// trigger a re-join or implement other error handling here.
}
}
static void on_lorawan_mlme_request(LoRaMacStatus_t status, MlmeReq_t *mlme, TimerTime_t delay)
{
if (mlme->Type == MLME_JOIN)
{
if (status == LORAMAC_STATUS_DUTYCYCLE_RESTRICTED)
{
led_command_t command = {
.ui32Handle = application_led_handle, .ui32Id = LED_EFFECT_PULSE1, .ui32Repeat = 1};
led_send(&command);
}
else
{
led_command_t command = {.ui32Handle = application_led_handle,
.ui32Id = LED_EFFECT_BREATHING,
.ui32Repeat = 0};
led_send(&command);
}
}
}
static void on_lorawan_nvm_data_change(LmHandlerNvmContextStates_t sState, uint16_t ui16Size)
{
lorawan_class_e lorawan_class;
lorawan_class = lorawan_class_get();
if (lorawan_class == LORAWAN_CLASS_C)
{
return;
}
if (sState == LORAMAC_HANDLER_NVM_STORE)
{
lorawan_sleep();
}
}
static void on_lorawan_sleep(void)
{
#if defined(BSP_NM180410) || defined(BSP_NM180411)
am_hal_gpio_state_write(AM_BSP_GPIO_PETAL_CORE_nLORA_EN, AM_HAL_GPIO_OUTPUT_SET);
#endif
}
static void on_lorawan_wake(void)
{
#if defined(BSP_NM180410) || defined(BSP_NM180411)
am_hal_gpio_state_write(AM_BSP_GPIO_PETAL_CORE_nLORA_EN, AM_HAL_GPIO_OUTPUT_CLEAR);
// This function is executed in the lorawan_task context. We stabilize the
// radio power rail with a spinning delay to prevent the radio from
// further processing without using a critical block. This allows other
// radios to preempt the lorawan_task without impacting LoRaWAN operations.
am_util_delay_ms(1);
#endif
}
static void on_lorawan_class_change(DeviceClass_t eDeviceClass)
{
if (eDeviceClass == CLASS_C)
{
lorawan_event_callback_register(LORAWAN_EVENT_SLEEP, NULL);
lorawan_event_callback_register(LORAWAN_EVENT_WAKE, NULL);
}
else
{
lorawan_event_callback_register(LORAWAN_EVENT_SLEEP, on_lorawan_sleep);
lorawan_event_callback_register(LORAWAN_EVENT_WAKE, on_lorawan_wake);
lorawan_sleep();
}
}
static void on_button_pressed(void)
{
// This callback executes in the button task context and not from an ISR.
application_message_t msg = APP_MSG_BUTTON_PRESSED;
xQueueSend(application_queue_handle, &msg, pdMS_TO_TICKS(100));
}
// Disable the SWD port for measuring current draw on the Petal Core.
// This is required since the pull-up and pull-down for the clock and data lines
// are located on the Petal Development board. If the SWD port remains on without
// the pull-up and pull-down, the additional current draw can exceed 90uA.
//
// We also do not disable the port automatically after boot as we cannot program
// the chip if the SWD port is disabled.
static void on_swd_disabled(void)
{
// This callback executes in the button task context and not from an ISR.
am_hal_gpio_pinconfig(AM_BSP_GPIO_SWDCK, g_AM_HAL_GPIO_DISABLE);
am_hal_gpio_pinconfig(AM_BSP_GPIO_SWDIO, g_AM_HAL_GPIO_DISABLE);
am_hal_gpio_pinconfig(AM_BSP_GPIO_ITM_SWO, g_AM_HAL_GPIO_DISABLE);
}
#if defined(BSP_NM180100EVB) || defined(BSP_NM180410) || defined(BSP_NM180411)
static void on_led_ctimer(void)
{
led_interrupt_service(application_led_handle);
}
#elif defined(BSP_AP510EVB)
static void on_led_timer(am_hal_timer_compare_e eCompare)
{
led_interrupt_service(application_led_handle);
}
#endif
static void process_downlink_packet(void)
{
am_util_stdio_printf("\n\rReceived Data\n\r");
am_util_stdio_printf(" COUNTER : %-4d\n\r", application_rx_packet.counter);
am_util_stdio_printf(" PORT : %-4d\n\r", application_rx_packet.port);
am_util_stdio_printf(" SLOT : %-4d\n\r", application_rx_packet.slot);
am_util_stdio_printf(" DATA RATE : %-4d\n\r", application_rx_packet.dr);
am_util_stdio_printf(" RSSI : %-4d\n\r", application_rx_packet.rssi);
am_util_stdio_printf(" SNR : %-4d\n\r", application_rx_packet.snr);
am_util_stdio_printf(" SIZE : %-4d\n\r", application_rx_packet.size);
am_util_stdio_printf(" PAYLOAD :");
for (int i = 0; i < application_rx_packet.size; i++)
{
if ((i % 8) == 0)
{
am_util_stdio_printf("\n\r ");
}
am_util_stdio_printf("%02x ", application_rx_packet.payload[i]);
}
am_util_stdio_printf("\n\r");
// blink the LED to indicate receive
led_command_t command = {
.ui32Handle = application_led_handle,
.ui32Id = LED_EFFECT_PULSE2,
.ui32Repeat = 1,
};
led_send(&command);
}
static void process_button_press(void)
{
// Transmit a single byte to indicate to the LNS that a button
// sequence has been pressed:
// Port: 1
// ACK: 0 (0 for no acknowledgment, 1 for acknowledgment)
// Payload Size: 1 byte
// Payload: "1"
lorawan_transmit(1, 0, 1, "1");
}
static void setup_button(void)
{
uint32_t handle;
button_config(&handle, AM_BSP_GPIO_BUTTON0, g_AM_BSP_GPIO_BUTTON0, 1);
// The following register a single short press sequence to the button.
// Other press sequences are possible. For example, a two presses sequence
// with a short press first followed by a long press would be
//
// button_sequence_register(handle, 2, 0b10, on_button_pressed);
//
// In general, the order of the sequence starts from LSB to MSB;
// 0 being a short press and 1 being a long press.
// A single press to trigger a transmit. In this example, we
// register both a short or a long press to accommodate user variations.
button_sequence_register(handle, 1, 0b0, on_button_pressed);
button_sequence_register(handle, 1, 0b1, on_button_pressed);
// A double press to disable the SWD port for current measurement.
button_sequence_register(handle, 2, 0b00, on_swd_disabled);
button_sequence_register(handle, 2, 0b01, on_swd_disabled);
button_sequence_register(handle, 2, 0b10, on_swd_disabled);
button_sequence_register(handle, 2, 0b11, on_swd_disabled);
}
static void setup_led(void)
{
#if defined(BSP_NM180100EVB) || defined(BSP_NM180410) || defined(BSP_NM180411)
// Configure the LED that is connected to a GPIO with CTIMER output.
const led_timer_config_t led_cfg = {
.ui32Number = APPLICATION_LED_TIMER_NUMBER,
.ui32Segment = APPLICATION_LED_TIMER_SEGMENT,
.ui32Interrupt = APPLICATION_LED_TIMER_INTERRUPT,
.ui32ActiveLow = 0,
.ui32Pin = APPLICATION_LED,
.pfnInterruptService = on_led_ctimer,
};
#elif defined(BSP_AP510EVB)
const led_timer_config_t led_cfg = {
.ui32Number = APPLICATION_LED_TIMER_NUMBER,
.ui32ActiveLow = 1,
.ui32Pin = APPLICATION_LED,
.pfnInterruptService = on_led_timer,
};
#endif
led_config(&application_led_handle, &led_cfg);
}
static void setup_lorawan(void)
{
lorawan_tracing_set(1);
lorawan_network_config(LORAWAN_REGION_US915, LORAWAN_DATARATE_0, true, true);
lorawan_activation_config(LORAWAN_ACTIVATION_OTAA, NULL);
lorawan_key_set_by_str(LORAWAN_KEY_JOIN_EUI, "b4c231a359bc2e3d");
lorawan_key_set_by_str(LORAWAN_KEY_APP, "01c3f004a2d6efffe32c4eda14bcd2b4");
lorawan_key_set_by_str(LORAWAN_KEY_NWK, "3f4ca100e2fc675ea123f4eb12c4a012");
// While callbacks are executed outside of interrupt context, they are
// called by the LoRaWAN stack transport layer. Avoid blocking or
// performing excessive processing in these callbacks.
lorawan_event_callback_register(LORAWAN_EVENT_MAC_MLME_REQUEST, on_lorawan_mlme_request);
lorawan_event_callback_register(LORAWAN_EVENT_JOIN_REQUEST, on_lorawan_join_request);
lorawan_event_callback_register(LORAWAN_EVENT_RX_DATA, on_lorawan_receive);
lorawan_event_callback_register(LORAWAN_EVENT_MAC_MCPS_REQUEST, on_lorawan_mcps_request);
#if (LORAWAN_PM_ENABLE == 1)
lorawan_event_callback_register(LORAWAN_EVENT_NVM_DATA_CHANGE, on_lorawan_nvm_data_change);
lorawan_event_callback_register(LORAWAN_EVENT_CLASS_CHANGE, on_lorawan_class_change);
lorawan_event_callback_register(LORAWAN_EVENT_SLEEP, on_lorawan_sleep);
lorawan_event_callback_register(LORAWAN_EVENT_WAKE, on_lorawan_wake);
#else
// In this example, if we to elect to disable power management by the
// stack, then we just keep the radio on all the time.
am_hal_gpio_state_write(AM_BSP_GPIO_PETAL_CORE_nLORA_EN, AM_HAL_GPIO_OUTPUT_CLEAR);
#endif
// Start the LoRaWAN stack.
// There is no need to explicitly turn the radio on or off,
// radio power management is handled by the stack if enabled.
lorawan_stack_state_set(LORAWAN_STACK_ENABLE);
// In the event of a reboot, check the join context in flash.
// If the device had already joined the network, switch to the
// default LoRaWAN class.
if (lorawan_get_join_state())
{
lorawan_class_set(APPLICATION_DEFAULT_LORAWAN_CLASS);
}
}
static void application_task_setup(void)
{
#if !defined(CURRENT_MEASUREMENT_ENABLE)
#if defined(BSP_NM180100EVB) || (BSP_NM180410) || defined(BSP_NM180411)
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED0, g_AM_BSP_GPIO_LED0);
am_hal_gpio_state_write(AM_BSP_GPIO_LED0, AM_HAL_GPIO_OUTPUT_CLEAR);
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED1, g_AM_BSP_GPIO_LED1);
am_hal_gpio_state_write(AM_BSP_GPIO_LED1, AM_HAL_GPIO_OUTPUT_CLEAR);
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED2, g_AM_BSP_GPIO_LED2);
am_hal_gpio_state_write(AM_BSP_GPIO_LED2, AM_HAL_GPIO_OUTPUT_CLEAR);
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED3, g_AM_BSP_GPIO_LED3);
am_hal_gpio_state_write(AM_BSP_GPIO_LED3, AM_HAL_GPIO_OUTPUT_CLEAR);
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED4, g_AM_BSP_GPIO_LED4);
am_hal_gpio_state_write(AM_BSP_GPIO_LED4, AM_HAL_GPIO_OUTPUT_CLEAR);
#elif defined(BSP_AP510EVB)
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED0, g_AM_BSP_GPIO_LED0);
am_hal_gpio_state_write(AM_BSP_GPIO_LED0, AM_HAL_GPIO_OUTPUT_SET);
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED1, g_AM_BSP_GPIO_LED1);
am_hal_gpio_state_write(AM_BSP_GPIO_LED1, AM_HAL_GPIO_OUTPUT_SET);
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED2, g_AM_BSP_GPIO_LED2);
am_hal_gpio_state_write(AM_BSP_GPIO_LED2, AM_HAL_GPIO_OUTPUT_SET);
#endif
#else
#if defined(BSP_NM180100EVB) || (BSP_NM180410) || defined(BSP_NM180411)
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED0, g_AM_BSP_GPIO_LED0);
am_hal_gpio_state_write(AM_BSP_GPIO_LED0, AM_HAL_GPIO_OUTPUT_CLEAR);
#elif defined(BSP_AP510EVB)
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED0, g_AM_BSP_GPIO_LED0);
am_hal_gpio_state_write(AM_BSP_GPIO_LED0, AM_HAL_GPIO_OUTPUT_SET);
#endif
#endif
// The Petal ecosystem has the ability to shutdown the LoRa radio. In addition,
// the Petal development board has the ability to shutdown the I/O level shifters
// for power savings.
#if defined(BSP_NM180410) || defined(BSP_NM180411)
am_hal_gpio_pinconfig(AM_BSP_GPIO_PETAL_CORE_nLORA_EN, g_AM_HAL_GPIO_OUTPUT);
am_hal_gpio_state_write(AM_BSP_GPIO_PETAL_CORE_nLORA_EN, AM_HAL_GPIO_OUTPUT_SET);
am_hal_gpio_pinconfig(AM_BSP_GPIO_PETAL_DEV_IO_EN, g_AM_HAL_GPIO_OUTPUT);
am_hal_gpio_state_write(AM_BSP_GPIO_PETAL_DEV_IO_EN, AM_HAL_GPIO_OUTPUT_SET);
#endif
setup_button();
#if !defined(CURRENT_MEASUREMENT_ENABLE)
setup_led();
#endif
setup_lorawan();
}
static void application_task_loop(void)
{
application_message_t msg;
if (xQueueReceive(application_queue_handle, &msg, portMAX_DELAY) == pdPASS)
{
switch (msg)
{
case APP_MSG_RX:
process_downlink_packet();
break;
case APP_MSG_BUTTON_PRESSED:
process_button_press();
break;
default:
break;
}
}
}
static void application_task(void *parameter)
{
#if defined(CLI_ENABLE)
gpio_cli_register();
application_task_cli_register();
#endif
application_task_setup();
while (1)
{
application_task_loop();
}
}
void application_task_create(uint32_t priority)
{
application_queue_handle =
xQueueCreate(APPLICATION_QUEUE_MAX_SIZE, sizeof(application_message_t));
xTaskCreate(application_task, "application", 512, 0, priority, &application_task_handle);
}