-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication_task.c
165 lines (143 loc) · 5.85 KB
/
application_task.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
/*
* BSD 3-Clause License
*
* Copyright (c) 2024, 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 <am_mcu_apollo.h>
#include <am_util.h>
#include <FreeRTOS.h>
#include <task.h>
#include "am_bsp.h"
#include "button.h"
#include "led.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
#else
#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
#endif
static TaskHandle_t application_task_handle;
static uint32_t current_led_effect;
// Button press handler to cycle through all the LED effects.
static void on_button_pressed(void)
{
current_led_effect = (current_led_effect + 1) % (LED_COMMAND_MAX + 1);
if (current_led_effect < LED_COMMAND_MAX)
{
led_command_t command = { .ui32Id = current_led_effect, .ui32Repeat = 0 };
led_send(&command);
}
else
{
led_command_t command = { .ui32Id = LED_COMMAND_OFF, .ui32Repeat = 0 };
led_send(&command);
}
}
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 sequence 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.
button_sequence_register(handle, 1, 0b0, on_button_pressed);
}
static void setup_led(void)
{
// Configure the LED that is connected to a GPIO with CTIMER output.
const led_config_t led_cfg = {
.ui32Number = APPLICATION_LED_TIMER_NUMBER,
.ui32Segment = APPLICATION_LED_TIMER_SEGMENT,
.ui32Interrupt = APPLICATION_LED_TIMER_INTERRUPT,
.ui32ActiveLow = 0,
.ui32Pin = APPLICATION_LED,
};
led_config(&led_cfg);
current_led_effect = LED_COMMAND_MAX;
}
static void application_task_setup(void)
{
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED0, g_AM_HAL_GPIO_OUTPUT);
am_hal_gpio_state_write(AM_BSP_GPIO_LED0, AM_HAL_GPIO_OUTPUT_CLEAR);
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED1, g_AM_HAL_GPIO_OUTPUT);
am_hal_gpio_state_write(AM_BSP_GPIO_LED1, AM_HAL_GPIO_OUTPUT_CLEAR);
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED2, g_AM_HAL_GPIO_OUTPUT);
am_hal_gpio_state_write(AM_BSP_GPIO_LED2, AM_HAL_GPIO_OUTPUT_CLEAR);
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED3, g_AM_HAL_GPIO_OUTPUT);
am_hal_gpio_state_write(AM_BSP_GPIO_LED3, AM_HAL_GPIO_OUTPUT_CLEAR);
am_hal_gpio_pinconfig(AM_BSP_GPIO_LED4, g_AM_HAL_GPIO_OUTPUT);
am_hal_gpio_state_write(AM_BSP_GPIO_LED4, AM_HAL_GPIO_OUTPUT_CLEAR);
// 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();
setup_led();
}
static void application_task_loop(void)
{
vTaskDelay(pdMS_TO_TICKS(500));
// if no LED effect is active, just toggle the LED
if (led_status_get() == LED_STATUS_IDLE)
{
am_hal_gpio_state_write(APPLICATION_LED, AM_HAL_GPIO_OUTPUT_TOGGLE);
}
}
static void application_task(void *parameter)
{
#if defined(CLI_ENABLE)
application_task_cli_register();
#endif
application_task_setup();
while (1)
{
application_task_loop();
}
}
void application_task_create(uint32_t priority)
{
xTaskCreate(application_task, "application", 512, 0, priority, &application_task_handle);
}