freertos.c
7.49 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
/* USER CODE BEGIN Header */
/**
******************************************************************************
* File Name : freertos.c
* Description : Code for freertos applications
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2019 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "iwdg.h"
#include "led.h"
#include "beep.h"
#include "key.h"
#include "usart.h"
#include "ATSystem.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
/* USER CODE END Variables */
osThreadId_t LEDTaskHandle;
osThreadId_t KeyScanTaskHandle;
osThreadId_t UartTaskHandle;
osMessageQueueId_t KeyStatusQueueHandle;
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
void WIFIModule_Init();
/* USER CODE END FunctionPrototypes */
void StartLEDTask(void *argument);
void StartKeyScanTask(void *argument);
void StartUartTask(void *argument);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/**
* @brief FreeRTOS initialization
* @param None
* @retval None
*/
void MX_FREERTOS_Init(void) {
/* USER CODE BEGIN Init */
/* USER CODE END Init */
osKernelInitialize();
/* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */
/* USER CODE END RTOS_MUTEX */
/* USER CODE BEGIN RTOS_SEMAPHORES */
/* add semaphores, ... */
/* USER CODE END RTOS_SEMAPHORES */
/* USER CODE BEGIN RTOS_TIMERS */
/* start timers, add new ones, ... */
/* USER CODE END RTOS_TIMERS */
/* Create the queue(s) */
/* definition and creation of KeyStatusQueue */
const osMessageQueueAttr_t KeyStatusQueue_attributes = {
.name = "KeyStatusQueue"
};
KeyStatusQueueHandle = osMessageQueueNew (4, sizeof(uint8_t), &KeyStatusQueue_attributes);
/* USER CODE BEGIN RTOS_QUEUES */
/* add queues, ... */
/* USER CODE END RTOS_QUEUES */
/* Create the thread(s) */
/* definition and creation of LEDTask */
const osThreadAttr_t LEDTask_attributes = {
.name = "LEDTask",
.priority = (osPriority_t) osPriorityNormal,
.stack_size = 512
};
LEDTaskHandle = osThreadNew(StartLEDTask, NULL, &LEDTask_attributes);
/* definition and creation of KeyScanTask */
const osThreadAttr_t KeyScanTask_attributes = {
.name = "KeyScanTask",
.priority = (osPriority_t) osPriorityNormal,
.stack_size = 512
};
KeyScanTaskHandle = osThreadNew(StartKeyScanTask, NULL, &KeyScanTask_attributes);
/* definition and creation of UartTask */
const osThreadAttr_t UartTask_attributes = {
.name = "UartTask",
.priority = (osPriority_t) osPriorityNormal,
.stack_size = 512
};
UartTaskHandle = osThreadNew(StartUartTask, NULL, &UartTask_attributes);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
}
/* USER CODE BEGIN Header_StartLEDTask */
/**
* @brief Function implementing the LEDTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartLEDTask */
void StartLEDTask(void *argument)
{
/* USER CODE BEGIN StartLEDTask */
uint8_t keyState;
LED_State lStates = LED000;
/* Infinite loop */
for(;;)
{
if (osMessageQueueGet(KeyStatusQueueHandle, &keyState, 0, 50) != osOK)
{
lStates = (lStates + 1) % LED_TOTAL_STATE_NUM;
//mprintf("ledState: %d\r\n", lStates);
if (keyState == 1) {
LED_StateControl(lStates);
}
else
{
LED_StateControl2(lStates);
}
osDelay(500);
}
}
/* USER CODE END StartLEDTask */
}
/* USER CODE BEGIN Header_StartKeyScanTask */
/**
* @brief Function implementing the KeyScanTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartKeyScanTask */
void StartKeyScanTask(void *argument)
{
/* USER CODE BEGIN StartKeyScanTask */
uint16_t count = 0;
uint8_t flag = 0;
/* Infinite loop */
for(;;)
{
/* Infinite loop */
if(KEY1_read() == KEY1_DOWN)
{
osDelay(10);
if (KEY1_read() == KEY1_UP)
{
count = 0;
goto DONE;
}
if(count < 500)// 0 ~ 5 000 ms
{
IWDG_Feed();
flag = KEY_PRESS_0_5S;
if(count % 10 == 0)
{
HAL_GPIO_TogglePin(LED1B_GPIO_Port, LED1B_Pin);
}
}
else if((count >= 500) && (count < 1000))// 5 000 ~ 10 000 ms
{
flag = KEY_PRESS_5_10S;
if(count % 30 == 0)
{
HAL_GPIO_TogglePin(LED1B_GPIO_Port, LED1B_Pin);
}
}
else if((count >= 1000) && (count < 1500))// 10 000 ~ 15 000 ms
{
flag = KEY_PRESS_10_15S;
if(count % 50 == 0)
{
HAL_GPIO_TogglePin(LED1B_GPIO_Port, LED1B_Pin);
}
}
else
{
flag = KEY_PRESS_UP_15S;
}
count++;
}
else
{
if (flag != 0) // key state changed
{
BEEP_call();
if (osMessageQueuePut(KeyStatusQueueHandle, &flag, 0, 100) != osOK)
{
mprintf("queue send message failed\r\n");
}
}
count = 0;
flag = 0;
}
DONE:
osDelay(1);
}
/* USER CODE END StartKeyScanTask */
}
/* USER CODE BEGIN Header_StartUartTask */
/**
* @brief Function implementing the UartTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartUartTask */
void StartUartTask(void *argument)
{
/* USER CODE BEGIN StartUartTask */
uint8_t temp;
uint16_t times;
WIFIModule_Init();
/* Infinite loop */
for(;;)
{
while(RINGBUF_CHECKIO())
{
RINGBUF_READ(&temp, 1);
mprintf("%c", temp);
AT_Process(temp);
}
times++;
if (times % 5000 == 0)
{
mprintf("\r\nUART Test\r\n");
}
osDelay(5);
}
/* USER CODE END StartUartTask */
}
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void WIFIModule_Init()
{
WIFI_Enable();
// reset wifi module
WIFI_RstKey_Reset();
osDelay(100);
WIFI_RstKey_Set();
}
/* USER CODE END Application */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/