#include "FreeRTOS.h" #include "task.h" #include "main.h" #include "cmsis_os.h" #include "stdio.h"
时间: 2023-09-02 08:08:35 浏览: 354
这段代码是一个基于FreeRTOS的多任务应用程序的主程序。以下是代码中各部分的解释:
- `#include`语句用于引入所需的头文件。
- `FreeRTOS.h`和`task.h`是FreeRTOS的核心头文件,`cmsis_os.h`是FreeRTOS的CMSIS-RTOS API头文件,`main.h`是主程序的头文件,`stdio.h`是标准输入输出头文件。
- 这些头文件提供了所需的函数和数据类型,例如任务处理函数、任务句柄和队列句柄等。
- 在这段代码中,还没有定义任何任务或队列,这只是一个主程序的框架。
相关问题
#include "FreeRTOS.h" #include "task.h" #include "main.h" #include "cmsis_os.h" #include "stdio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* 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 */ QueueHandle_t xQueue; #define dui_len 5 #define content_len 10 /* USER CODE END Variables */ osThreadId defaultTaskHandle; osThreadId myTask02Handle; osThreadId myTask03Handle; /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN FunctionPrototypes */ /* USER CODE END FunctionPrototypes */ void StartDefaultTask(void const * argument); void StartTask02(void const * argument); void StartTask03(void const * argument); void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ /* GetIdleTaskMemory prototype (linked to static allocation support) */ void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize ); /* USER CODE BEGIN GET_IDLE_TASK_MEMORY */ static StaticTask_t xIdleTaskTCBBuffer; static StackType_t xIdleStack[configMINIMAL_STACK_SIZE]; void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize ) { *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer; *ppxIdleTaskStackBuffer = &xIdleStack[0]; *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;请写出详细的注释
这段代码是一个基于FreeRTOS的多任务应用程序的主程序。以下是代码中各部分的解释:
- `#include`语句用于引入所需的头文件。
- `QueueHandle_t xQueue;`声明一个队列句柄。
- `dui_len`和`content_len`分别定义了队列中数据项的长度。
- `osThreadId`类型的三个变量`defaultTaskHandle`、`myTask02Handle`和`myTask03Handle`声明了三个任务的句柄。
- `StartDefaultTask`、`StartTask02`和`StartTask03`是三个任务的函数原型。
- `MX_FREERTOS_Init`是FreeRTOS的初始化函数,其中包含了定义空闲任务的回调函数。
下面是三个任务的函数:
- `StartDefaultTask`函数是FreeRTOS默认的空闲任务,它没有任何实际作用。
- `StartTask02`函数是一个任务,主要是向队列中发送数据。
- `StartTask03`函数是一个任务,主要是从队列中接收数据。
在`vApplicationGetIdleTaskMemory`回调函数中,我们为空闲任务分配了堆栈和TCB缓冲区,这些缓冲区将在FreeRTOS启动时用于空闲任务。
/* USER CODE BEGIN Header */ /** ****************************************************************************** * File Name : freertos.c * Description : Code for freertos applications ****************************************************************************** * @attention * * <h2><center>© Copyright (c) 2025 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 <stdio.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 Task_HighFuncHandle; osThreadId Task_MidFuncHandle; osThreadId Task_LowFuncHandle; osThreadId Task_LedFuncHandle; osThreadId DefaultTaskHandle; osMessageQId myQueue01Handle; osMutexId myMutex01Handle; osSemaphoreId BinarySem_UartHandle; osSemaphoreId BinarySem_KeyHandle; /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN FunctionPrototypes */ /* USER CODE END FunctionPrototypes */ void StartTask_HighFunc(void const * argument); void StartTask_MidFunc(void const * argument); void StartTask_LowFunc(void const * argument); void StartTask_LedFunc(void const * argument); void StartDefaultTask(void const * argument); void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ /* GetIdleTaskMemory prototype (linked to static allocation support) */ void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize ); /* USER CODE BEGIN GET_IDLE_TASK_MEMORY */ static StaticTask_t xIdleTaskTCBBuffer; static StackType_t xIdleStack[configMINIMAL_STACK_SIZE]; void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize ) { *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer; *ppxIdleTaskStackBuffer = &xIdleStack[0]; *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE; /* place for user code */ } /* USER CODE END GET_IDLE_TASK_MEMORY */ /** * @brief FreeRTOS initialization * @param None * @retval None */ void MX_FREERTOS_Init(void) { /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Create the mutex(es) */ /* definition and creation of myMutex01 */ osMutexDef(myMutex01); myMutex01Handle = osMutexCreate(osMutex(myMutex01)); /* USER CODE BEGIN RTOS_MUTEX */ /* add mutexes, ... */ /* USER CODE END RTOS_MUTEX */ /* Create the semaphores(s) */ /* definition and creation of BinarySem_Uart */ osSemaphoreDef(BinarySem_Uart); BinarySem_UartHandle = osSemaphoreCreate(osSemaphore(BinarySem_Uart), 1); /* definition and creation of BinarySem_Key */ osSemaphoreDef(BinarySem_Key); BinarySem_KeyHandle = osSemaphoreCreate(osSemaphore(BinarySem_Key), 1); /* 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 myQueue01 */ osMessageQDef(myQueue01, 16, uint16_t); myQueue01Handle = osMessageCreate(osMessageQ(myQueue01), NULL); /* USER CODE BEGIN RTOS_QUEUES */ /* add queues, ... */ /* USER CODE END RTOS_QUEUES */ /* Create the thread(s) */ /* definition and creation of Task_HighFunc */ osThreadDef(Task_HighFunc, StartTask_HighFunc, osPriorityHigh, 0, 128); Task_HighFuncHandle = osThreadCreate(osThread(Task_HighFunc), NULL); /* definition and creation of Task_MidFunc */ osThreadDef(Task_MidFunc, StartTask_MidFunc, osPriorityNormal, 0, 128); Task_MidFuncHandle = osThreadCreate(osThread(Task_MidFunc), NULL); /* definition and creation of Task_LowFunc */ osThreadDef(Task_LowFunc, StartTask_LowFunc, osPriorityLow, 0, 128); Task_LowFuncHandle = osThreadCreate(osThread(Task_LowFunc), NULL); /* definition and creation of Task_LedFunc */ osThreadDef(Task_LedFunc, StartTask_LedFunc, osPriorityNormal, 0, 128); Task_LedFuncHandle = osThreadCreate(osThread(Task_LedFunc), NULL); /* definition and creation of DefaultTask */ osThreadDef(DefaultTask, StartDefaultTask, osPriorityIdle, 0, 128); DefaultTaskHandle = osThreadCreate(osThread(DefaultTask), NULL); /* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ /* USER CODE END RTOS_THREADS */ } /* USER CODE BEGIN Header_StartTask_HighFunc */ /** * @brief Function implementing the Task_HighFunc thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartTask_HighFunc */ void StartTask_HighFunc(void const * argument) { /* USER CODE BEGIN StartTask_HighFunc */ /* Infinite loop */ for(;;) { printf("Task_HighFunc gets binarySem!\r\n"); if(osSemaphorewait(myMutex0lHandle, osWaitForever)== osOK) { printf("Task_HighPunc Running\r\n"); } printf("Task_HighFunc Releasingsesaphore!\r\n"); osSemaphoreRelease (myMutex0lHandle); osDelay(500); } /* USER CODE END StartTask_HighFunc */ } /* USER CODE BEGIN Header_StartTask_MidFunc */ /** * @brief Function implementing the Task_MidFunc thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartTask_MidFunc */ void StartTask_MidFunc(void const * argument) { /* USER CODE BEGIN StartTask_MidFunc */ /* Infinite loop */ for(;;) { printf("Task_MidFunc Running\r\n"); osDelay (500); } /* USER CODE END StartTask_MidFunc */ } /* USER CODE BEGIN Header_StartTask_LowFunc */ /** * @brief Function implementing the Task_LowFunc thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartTask_LowFunc */ void StartTask_LowFunc(void const * argument) { /* USER CODE BEGIN StartTask_LowFunc */ static uint32_t i; /* Infinite loop */ for(;;) { printf("Task_LowFunc gets binarySem!\r\n"); if(osSemaphoreWait(myMutex0lHandle, osWaitForever)== osOK) { printf("Task_LowFunc Running\r\n"); } for(i=0;i<2000000;i++) { if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin)== 0) { osDelay(10); while(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin) == 0); printf("Task_HighFunc Suspend!\r\n"); vTaskSuspend(Task_HighHandle); printf("Task_MidFunc Suspend!\r\n"); vTaskSuspend(Task_MidHandle); printf("Task_LedFunc Resume!\r\n"); vTaskResume(Task_LedHandle); printf("Task_LowFunc Suspend!\r\n"); vTaskSuspend(Task_LowHandle); } osThreadYield(); } printf("Task_LowFunc Releasing semaphore!\r\n"); osSemaphoreRelease(myMutex0lHandle); osDelay(500); } /* USER CODE END StartTask_LowFunc */ } /* USER CODE BEGIN Header_StartTask_LedFunc */ /** * @brief Function implementing the Task_LedFunc thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartTask_LedFunc */ void StartTask_LedFunc(void const * argument) { /* USER CODE BEGIN StartTask_LedFunc */ uint32_t temp=0; /* Infinite loop */ for(;;) { if(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==0) { osDelay(10); while(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin)==0); printf("myQueueOlHandle Send!\r\n"); xQueueSend(myQueueOlHandle, &temp, portMAX_DELAY); temp++; } if(HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)==0) { osDelay(10); while(HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)==0); printf("BinarySem_KeyHandle Send!\r\n"); xSemaphoreGive(BinarySem_KeyHandle); } } /* USER CODE END StartTask_LedFunc */ } /* USER CODE BEGIN Header_StartDefaultTask */ /** * @brief Function implementing the DefaultTask thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartDefaultTask */ void StartDefaultTask(void const * argument) { /* USER CODE BEGIN StartDefaultTask */ QueueSetMemberHandle_t add = NULL; uint32_t queue_recv = 0; /* Infinite loop */ for(;;) { add = xQueueSelectFromSet(myQueueSet, portMAX_DELAY ); if(add==myQueue0lHandle) { xQueueReceive(add, &queue_recv, portMAX_DELAY); printf("myQueue0lHandle Rev:%d!\r\n",queue_recv); } else if (add==BinarySem_KeyHandle) { xSemaphoreTake (add, portMAX_DELAY); printf("BinarySem_KeyHandle Revok!\r\n"); } osDelay(1); } /* USER CODE END StartDefaultTask */ } /* Private application code --------------------------------------------------*/ /* USER CODE BEGIN Application */ /* USER CODE END Application */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 报错/* USER CODE BEGIN Header */ /** ****************************************************************************** * File Name : freertos.c * Description : Code for freertos applications ****************************************************************************** * @attention * * <h2><center>© Copyright (c) 2025 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 <stdio.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 Task_HighFuncHandle; osThreadId Task_MidFuncHandle; osThreadId Task_LowFuncHandle; osThreadId Task_LedFuncHandle; osThreadId DefaultTaskHandle; osMessageQId myQueue01Handle; osMutexId myMutex01Handle; osSemaphoreId BinarySem_UartHandle; osSemaphoreId BinarySem_KeyHandle; /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN FunctionPrototypes */ /* USER CODE END FunctionPrototypes */ void StartTask_HighFunc(void const * argument); void StartTask_MidFunc(void const * argument); void StartTask_LowFunc(void const * argument); void StartTask_LedFunc(void const * argument); void StartDefaultTask(void const * argument); void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ /* GetIdleTaskMemory prototype (linked to static allocation support) */ void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize ); /* USER CODE BEGIN GET_IDLE_TASK_MEMORY */ static StaticTask_t xIdleTaskTCBBuffer; static StackType_t xIdleStack[configMINIMAL_STACK_SIZE]; void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize ) { *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer; *ppxIdleTaskStackBuffer = &xIdleStack[0]; *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE; /* place for user code */ } /* USER CODE END GET_IDLE_TASK_MEMORY */ /** * @brief FreeRTOS initialization * @param None * @retval None */ void MX_FREERTOS_Init(void) { /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Create the mutex(es) */ /* definition and creation of myMutex01 */ osMutexDef(myMutex01); myMutex01Handle = osMutexCreate(osMutex(myMutex01)); /* USER CODE BEGIN RTOS_MUTEX */ /* add mutexes, ... */ /* USER CODE END RTOS_MUTEX */ /* Create the semaphores(s) */ /* definition and creation of BinarySem_Uart */ osSemaphoreDef(BinarySem_Uart); BinarySem_UartHandle = osSemaphoreCreate(osSemaphore(BinarySem_Uart), 1); /* definition and creation of BinarySem_Key */ osSemaphoreDef(BinarySem_Key); BinarySem_KeyHandle = osSemaphoreCreate(osSemaphore(BinarySem_Key), 1); /* 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 myQueue01 */ osMessageQDef(myQueue01, 16, uint16_t); myQueue01Handle = osMessageCreate(osMessageQ(myQueue01), NULL); /* USER CODE BEGIN RTOS_QUEUES */ /* add queues, ... */ /* USER CODE END RTOS_QUEUES */ /* Create the thread(s) */ /* definition and creation of Task_HighFunc */ osThreadDef(Task_HighFunc, StartTask_HighFunc, osPriorityHigh, 0, 128); Task_HighFuncHandle = osThreadCreate(osThread(Task_HighFunc), NULL); /* definition and creation of Task_MidFunc */ osThreadDef(Task_MidFunc, StartTask_MidFunc, osPriorityNormal, 0, 128); Task_MidFuncHandle = osThreadCreate(osThread(Task_MidFunc), NULL); /* definition and creation of Task_LowFunc */ osThreadDef(Task_LowFunc, StartTask_LowFunc, osPriorityLow, 0, 128); Task_LowFuncHandle = osThreadCreate(osThread(Task_LowFunc), NULL); /* definition and creation of Task_LedFunc */ osThreadDef(Task_LedFunc, StartTask_LedFunc, osPriorityNormal, 0, 128); Task_LedFuncHandle = osThreadCreate(osThread(Task_LedFunc), NULL); /* definition and creation of DefaultTask */ osThreadDef(DefaultTask, StartDefaultTask, osPriorityIdle, 0, 128); DefaultTaskHandle = osThreadCreate(osThread(DefaultTask), NULL); /* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ /* USER CODE END RTOS_THREADS */ } /* USER CODE BEGIN Header_StartTask_HighFunc */ /** * @brief Function implementing the Task_HighFunc thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartTask_HighFunc */ void StartTask_HighFunc(void const * argument) { /* USER CODE BEGIN StartTask_HighFunc */ /* Infinite loop */ for(;;) { printf("Task_HighFunc gets binarySem!\r\n"); if(osSemaphorewait(myMutex0lHandle, osWaitForever)== osOK) { printf("Task_HighPunc Running\r\n"); } printf("Task_HighFunc Releasingsesaphore!\r\n"); osSemaphoreRelease (myMutex0lHandle); osDelay(500); } /* USER CODE END StartTask_HighFunc */ } /* USER CODE BEGIN Header_StartTask_MidFunc */ /** * @brief Function implementing the Task_MidFunc thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartTask_MidFunc */ void StartTask_MidFunc(void const * argument) { /* USER CODE BEGIN StartTask_MidFunc */ /* Infinite loop */ for(;;) { printf("Task_MidFunc Running\r\n"); osDelay (500); } /* USER CODE END StartTask_MidFunc */ } /* USER CODE BEGIN Header_StartTask_LowFunc */ /** * @brief Function implementing the Task_LowFunc thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartTask_LowFunc */ void StartTask_LowFunc(void const * argument) { /* USER CODE BEGIN StartTask_LowFunc */ static uint32_t i; /* Infinite loop */ for(;;) { printf("Task_LowFunc gets binarySem!\r\n"); if(osSemaphoreWait(myMutex0lHandle, osWaitForever)== osOK) { printf("Task_LowFunc Running\r\n"); } for(i=0;i<2000000;i++) { if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin)== 0) { osDelay(10); while(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin) == 0); printf("Task_HighFunc Suspend!\r\n"); vTaskSuspend(Task_HighHandle); printf("Task_MidFunc Suspend!\r\n"); vTaskSuspend(Task_MidHandle); printf("Task_LedFunc Resume!\r\n"); vTaskResume(Task_LedHandle); printf("Task_LowFunc Suspend!\r\n"); vTaskSuspend(Task_LowHandle); } osThreadYield(); } printf("Task_LowFunc Releasing semaphore!\r\n"); osSemaphoreRelease(myMutex0lHandle); osDelay(500); } /* USER CODE END StartTask_LowFunc */ } /* USER CODE BEGIN Header_StartTask_LedFunc */ /** * @brief Function implementing the Task_LedFunc thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartTask_LedFunc */ void StartTask_LedFunc(void const * argument) { /* USER CODE BEGIN StartTask_LedFunc */ uint32_t temp=0; /* Infinite loop */ for(;;) { if(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==0) { osDelay(10); while(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin)==0); printf("myQueueOlHandle Send!\r\n"); xQueueSend(myQueueOlHandle, &temp, portMAX_DELAY); temp++; } if(HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)==0) { osDelay(10); while(HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)==0); printf("BinarySem_KeyHandle Send!\r\n"); xSemaphoreGive(BinarySem_KeyHandle); } } /* USER CODE END StartTask_LedFunc */ } /* USER CODE BEGIN Header_StartDefaultTask */ /** * @brief Function implementing the DefaultTask thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartDefaultTask */ void StartDefaultTask(void const * argument) { /* USER CODE BEGIN StartDefaultTask */ QueueSetMemberHandle_t add = NULL; uint32_t queue_recv = 0; /* Infinite loop */ for(;;) { add = xQueueSelectFromSet(myQueueSet, portMAX_DELAY ); if(add==myQueue0lHandle) { xQueueReceive(add, &queue_recv, portMAX_DELAY); printf("myQueue0lHandle Rev:%d!\r\n",queue_recv); } else if (add==BinarySem_KeyHandle) { xSemaphoreTake (add, portMAX_DELAY); printf("BinarySem_KeyHandle Revok!\r\n"); } osDelay(1); } /* USER CODE END StartDefaultTask */ } /* Private application code --------------------------------------------------*/ /* USER CODE BEGIN Application */ /* USER CODE END Application */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 报错../Core/Src/freertos.c(176): warning: #223-D: function "osSemaphorewait" declared implicitly if(osSemaphorewait(myMutex0lHandle, osWaitForever)== osOK) ../Core/Src/freertos.c(176): error: #20: identifier "myMutex0lHandle" is undefined if(osSemaphorewait(myMutex0lHandle, osWaitForever)== osOK) ../Core/Src/freertos.c(181): error: #20: identifier "myMutex0lHandle" is undefined osSemaphoreRelease (myMutex0lHandle); ../Core/Src/freertos.c(221): error: #20: identifier "myMutex0lHandle" is undefined if(osSemaphoreWait(myMutex0lHandle, osWaitForever)== osOK) ../Core/Src/freertos.c(232): error: #20: identifier "Task_HighHandle" is undefined vTaskSuspend(Task_HighHandle); ../Core/Src/freertos.c(234): error: #20: identifier "Task_MidHandle" is undefined vTaskSuspend(Task_MidHandle); ../Core/Src/freertos.c(236): error: #20: identifier "Task_LedHandle" is undefined vTaskResume(Task_LedHandle); ../Core/Src/freertos.c(238): error: #20: identifier "Task_LowHandle" is undefined vTaskSuspend(Task_LowHandle); ../Core/Src/freertos.c(243): error: #20: identifier "myMutex0lHandle" is undefined osSemaphoreRelease(myMutex0lHandle); ../Core/Src/freertos.c(268): error: #20: identifier "myQueueOlHandle" is undefined xQueueSend(myQueueOlHandle, &temp, portMAX_DELAY); ../Core/Src/freertos.c(297): error: #20: identifier "myQueueSet" is undefined add = xQueueSelectFromSet(myQueueSet, portMAX_DELAY ); ../Core/Src/freertos.c(298): error: #20: identifier "myQueue0lHandle" is undefined if(add==myQueue0lHandle) ../Core/Src/freertos.c: 1 warning, 11 errors
### FreeRTOS 编译错误分析与解决
在 FreeRTOS 的开发中,编译错误和警告通常是由以下几个原因导致的:变量未定义、函数调用不正确或配置问题。以下是对问题的具体分析和解决方案。
#### 1. 变量未定义
在代码中出现 `osSemaphorewait myMutex0lHandle` 和 `xQueueSend myQueueOlHandle` 等错误时,通常是因为这些句柄未被正确初始化或声明。FreeRTOS 中的任务句柄(如 `Task_HighHandle`)、队列句柄(如 `myQueueOlHandle`)以及互斥量句柄(如 `myMutex0lHandle`)需要在使用前进行定义和初始化[^1]。
- **解决方法**:
- 确保所有句柄在全局范围内声明,并通过相应的 API 初始化。例如:
```c
SemaphoreHandle_t myMutex0lHandle = NULL;
TaskHandle_t Task_HighHandle = NULL;
QueueHandle_t myQueueOlHandle = NULL;
void setup() {
myMutex0lHandle = xSemaphoreCreateMutex(); // 初始化互斥量
if (myMutex0lHandle == NULL) {
printf("Failed to create mutex\r\n");
}
xTaskCreate(Task_High, "High", configMINIMAL_STACK_SIZE, NULL, 2, &Task_HighHandle); // 创建任务并获取句柄
myQueueOlHandle = xQueueCreate(10, sizeof(int)); // 创建队列
}
```
#### 2. 函数调用不正确
`vTaskSuspend` 和 `xQueueSelectFromSet` 等函数需要确保其参数类型正确且句柄已初始化。如果句柄未定义或类型不匹配,编译器会报错。
- **解决方法**:
- 检查 `vTaskSuspend` 的参数是否为有效的任务句柄。例如:
```c
vTaskSuspend(Task_HighHandle); // 暂停任务
```
- 对于 `xQueueSelectFromSet`,确保队列集已正确创建并添加了相关队列:
```c
QueueSetHandle_t myQueueSet = xQueueCreateSet(10);
xQueueAddToSet(myQueueOlHandle, myQueueSet);
QueueHandle_t selectedQueue = xQueueSelectFromSet(myQueueSet, portMAX_DELAY);
```
#### 3. 配置问题
某些编译错误可能与 FreeRTOS 的配置文件 `FreeRTOSConfig.h` 相关。例如,`configUSE_PORT_OPTIMISED_TASK_SELECTION` 的启用会影响任务调度逻辑[^2]。如果未正确配置,可能会导致编译失败。
- **解决方法**:
- 确保 `FreeRTOSConfig.h` 中的宏定义正确。例如:
```c
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
```
#### 4. 延迟时间设置
引用中提到的延迟时间 `vTaskDelay(150)` 是为了避免主线程干扰[^1]。如果延迟时间不足,可能导致任务切换异常或资源竞争问题。建议根据实际需求调整延迟值。
```c
vTaskDelay(pdMS_TO_TICKS(150)); // 使用 pdMS_TO_TICKS 转换毫秒为 ticks
```
---
### 示例代码
以下是一个完整的示例,展示如何初始化句柄并避免编译错误:
```c
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "queue.h"
SemaphoreHandle_t myMutex0lHandle = NULL;
TaskHandle_t Task_HighHandle = NULL;
QueueHandle_t myQueueOlHandle = NULL;
void task_high(void *pvParameters) {
while (1) {
xSemaphoreTake(myMutex0lHandle, portMAX_DELAY); // 获取互斥量
printf("Task High Running...\n");
xSemaphoreGive(myMutex0lHandle); // 释放互斥量
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void setup() {
myMutex0lHandle = xSemaphoreCreateMutex();
if (myMutex0lHandle == NULL) {
printf("Failed to create mutex\r\n");
}
myQueueOlHandle = xQueueCreate(10, sizeof(int));
if (myQueueOlHandle == NULL) {
printf("Failed to create queue\r\n");
}
xTaskCreate(task_high, "High", configMINIMAL_STACK_SIZE, NULL, 2, &Task_HighHandle);
vTaskStartScheduler(); // 启动调度器
}
```
---
阅读全文
相关推荐





