/* USER CODE BEGIN Header */ /** ****************************************************************************** * File Name : freertos.c * Description : Code for freertos applications ****************************************************************************** * @attention stm32f103c8t6程序报错../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(297): error: #20: identifier "myQueueSet" is undefined add = xQueueSelectFromSet(myQueueSet, portMAX_DELAY ); ../Core/Src/freertos.c: 0 warnings, 5 errors * <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(myMutex01Handle, osWaitForever)== osOK) { printf("Task_HighPunc Running\r\n"); } printf("Task_HighFunc Releasingsesaphore!\r\n"); osSemaphoreRelease(myMutex01Handle); 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(myMutex01Handle, 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(myMutex01Handle); 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(myQueue01Handle, &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==myQueue01Handle) { 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****/
时间: 2025-06-07 08:46:38 浏览: 13
### 问题分析与解决方案
在STM32 FreeRTOS程序中,如果出现未定义标识符的编译错误(如`identifier undefined Task_HighHandle`、`Task_MidHandle`等),通常是因为以下原因导致的:变量或句柄未正确定义、头文件未正确包含或链接库配置错误。以下是针对该问题的专业分析与解决方法。
---
#### 1. 确保任务句柄正确定义
FreeRTOS的任务句柄类型为`TaskHandle_t`,必须在创建任务时进行初始化。如果这些句柄未被声明或初始化,会导致编译器报错。例如:
```c
TaskHandle_t Task_HighHandle = NULL; // 高优先级任务句柄
TaskHandle_t Task_MidHandle = NULL; // 中优先级任务句柄
TaskHandle_t Task_LedHandle = NULL; // LED控制任务句柄
TaskHandle_t Task_LowHandle = NULL; // 低优先级任务句柄
```
上述代码需要在全局范围内声明[^1]。确保在任务创建函数中对这些句柄进行赋值,例如:
```c
xTaskCreate(TaskHighPriority, "HighTask", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 3, &Task_HighHandle);
xTaskCreate(TaskMidPriority, "MidTask", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, &Task_MidHandle);
xTaskCreate(TaskLedControl, "LedTask", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, &Task_LedHandle);
xTaskCreate(TaskLowPriority, "LowTask", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &Task_LowHandle);
```
---
#### 2. 检查头文件是否正确包含
确保所有相关头文件已正确包含。例如,`FreeRTOS.h`和`task.h`是FreeRTOS的核心头文件,必须在源文件中包含:
```c
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h" // 如果使用队列
#include "semphr.h" // 如果使用信号量
#include "bsp.h" // 板级支持包头文件[^2]
```
此外,确认`FreeRTOSConfig.h`文件已被正确包含,并且路径配置无误。如果项目使用了Keil或其他IDE,请检查项目设置中的包含路径是否正确。
---
#### 3. 确保队列集(Queue Set)正确定义
对于`myQueueSet`未定义的问题,需确保在程序中正确创建了队列集。例如:
```c
QueueSetHandle_t myQueueSet = NULL;
// 创建队列集
myQueueSet = xQueueCreateSet(5 * (UBaseType_t)sizeof(void*)); // 假设最多有5个队列加入集合
if (myQueueSet == NULL) {
// 错误处理
}
```
随后,将队列添加到队列集中:
```c
xQueueAddToSet(myQueue, myQueueSet);
```
---
#### 4. 链接库配置检查
如果使用的是Keil MDK或其他工具链,确保FreeRTOS库已正确链接。例如,在Keil中,检查以下内容:
- **目标选项** -> **输出** -> **执行文件生成**:确保启用了正确的执行文件格式。
- **目标选项** -> **C/C++** -> **包含路径**:确保包含了FreeRTOS的源码路径。
- **目标选项** -> **链接器** -> **库**:确保链接了FreeRTOS的静态库(如果使用静态库)。
---
#### 5. 屏蔽重复定义的中断服务函数
根据引用内容[^1],如果在`stm32fxxx_it.c`中存在重复定义的中断服务函数,可能导致编译错误。建议屏蔽这些函数的定义,或者通过条件编译的方式避免冲突。例如:
```c
#ifndef __FREERTOS__
void SysTick_Handler(void) {
// 默认中断处理逻辑
}
#endif
```
---
#### 6. 配置`FreeRTOSConfig.h`文件
确保`FreeRTOSConfig.h`文件中正确配置了宏定义。例如,中断相关的宏定义应如下所示:
```c
#define configTICK_RATE_HZ ((TickType_t)1000)
#define configUSE_PREEMPTION 1
#define configUSE_TICK_HOOK 0
#define configCPU_CLOCK_HZ (SystemCoreClock)
```
此外,根据引用内容[^1],对于STM32F407芯片,需调整以下内容:
```c
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
// 编译器特定配置
#endif
```
---
#### 7. 验证主函数逻辑
在主函数中,确保任务创建后调用了调度启动函数`vTaskStartScheduler()`,并且没有在任务外部直接调用`vTaskDelay()`。例如:
```c
int main(void) {
BSP_Init(); // 初始化板级支持包
xTaskCreate(TaskHighPriority, "HighTask", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 3, &Task_HighHandle);
vTaskStartScheduler(); // 启动任务调度
for (;;); // 防止退出主函数
}
```
---
###
阅读全文
相关推荐





