freertos-空闲钩子函数实验
·
目录
任务优先级与任务状态
freertos的优先级机制是用来管理就绪态->运行态时,哪个任务排在前面,而对于阻塞态和挂起态的任务,优先级就失去作用。空闲任务的优先级是最低的,就算你建立的任务的优先级也是0,但是其优先级还是高于空闲任务。
空闲钩子函数的使用
两个宏configIDLE_SHOULD_YIELD & configUSE_IDLE_HOOK
configUSE_IDLE_HOOK


configIDLE_SHOULD_YIELD

关于钩子函数特性的实验
实验要求在一个任务函数中同时调用两个任务,并在第二个任务函数中尝试使用vtaskdelayuntil,在空闲钩子函数中实现灯光闪烁。
开发环境使用:stm32f103、keil5、cubemx
/* USER CODE BEGIN Header */
/**
******************************************************************************
* File Name : freertos.c
* Description : Code for freertos applications
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* 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 */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for myTask01 */
osThreadId_t myTask01Handle;
const osThreadAttr_t myTask01_attributes = {
.name = "myTask01",
.stack_size = 128 * 4,
.priority = (osPriority_t) 0,
};
/* Definitions for myTask02 */
osThreadId_t myTask02Handle;
const osThreadAttr_t myTask02_attributes = {
.name = "myTask02",
.stack_size = 128 * 4,
.priority = (osPriority_t) 0,
};
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
osThreadId_t printfperiodHandle;
const osThreadAttr_t printfperiod_attributes = {
.name = "printfperiod_task",
.stack_size = 128 * 4,
.priority = (osPriority_t)osPriorityLow,
};
osMutexId_t printfMutexHandle;
const osMutexAttr_t printfMutex_attributes = {
.name = "printfMutex"
};
/* USER CODE END FunctionPrototypes */
void StartDefaultTask(void *argument);
void StartTask01(void *argument);
void StartTask02(void *argument);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/* Hook prototypes */
void vApplicationIdleHook(void);
/* USER CODE BEGIN 2 */
//volatile unsigned long ulIdleCycleCount = 0UL;
void vApplicationIdleHook( void )
{
static TickType_t xLastToggleTime = 0;
TickType_t xCurrentTime = xTaskGetTickCount();
if((xCurrentTime - xLastToggleTime) >= pdMS_TO_TICKS(100))
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
xLastToggleTime = xCurrentTime;
}
/* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
task. It is essential that code added to this hook function never attempts
to block in any way (for example, call xQueueReceive() with a block time
specified, or call vTaskDelay()). If the application makes use of the
vTaskDelete() API function (as this demo application does) then it is also
important that vApplicationIdleHook() is permitted to return to its calling
function, because it is the responsibility of the idle task to clean up
memory allocated by the kernel to any task that has since been deleted. */
}
//void vApplicationIdleHook(void)
//{
// for(;;)
// {
// HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
// HAL_Delay(100); // 在空闲任务中这是可以接受的
// //vTaskDelay(100);
// }
//}
/* USER CODE END 2 */
/**
* @brief FreeRTOS initialization
* @param None
* @retval None
*/
void MX_FREERTOS_Init(void) {
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */
printfMutexHandle = osMutexNew(&printfMutex_attributes);
/* 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 */
/* USER CODE BEGIN RTOS_QUEUES */
/* add queues, ... */
/* USER CODE END RTOS_QUEUES */
/* Create the thread(s) */
/* creation of defaultTask */
// defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
/* creation of myTask01 */
myTask01Handle = osThreadNew(StartTask01, (void*) 1, &myTask01_attributes);
/* creation of myTask02 */
myTask02Handle = osThreadNew(StartTask01, (void*) 2, &myTask02_attributes);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
printfperiodHandle = osThreadNew(StartTask02, NULL, &printfperiod_attributes);
/* USER CODE END RTOS_THREADS */
/* USER CODE BEGIN RTOS_EVENTS */
/* add events, ... */
/* USER CODE END RTOS_EVENTS */
}
/* 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 *argument)
{
/* USER CODE BEGIN StartDefaultTask */
// /* Infinite loop */
// for (;;)
// {
// osDelay(1);
// }
/* USER CODE END StartDefaultTask */
}
/* USER CODE BEGIN Header_StartTask01 */
/**
* @brief Function implementing the myTask01 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask01 */
void StartTask01(void *argument)
{
/* USER CODE BEGIN StartTask01 */
const TickType_t xDelay100ms = pdMS_TO_TICKS(100);
void *arg = argument;
/* Infinite loop */
for (;;)
{
osMutexAcquire(printfMutexHandle, osWaitForever);
printf("This is task %d\r\n", (int)arg);
osMutexRelease(printfMutexHandle);
vTaskDelay(xDelay100ms);
}
/* USER CODE END StartTask01 */
}
/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the myTask02 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void *argument)
{
/* USER CODE BEGIN StartTask02 */
TickType_t xLastWakeTime;
const TickType_t xDelay10ms = pdMS_TO_TICKS(10);
xLastWakeTime = xTaskGetTickCount();
/* Infinite loop */
for (;;)
{
osMutexAcquire(printfMutexHandle, osWaitForever);
printf("This is task period over");
osMutexRelease(printfMutexHandle);
//osDelay(1);
vTaskDelayUntil( &xLastWakeTime, xDelay10ms );
}
/* USER CODE END StartTask02 */
}
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
/* Declare a variable that will be incremented by the hook function. */
/*
* Idle hook functions MUST be called vApplicationIdleHook(), take no
* parameters, and return void.
*/
/* USER CODE END Application */
关于忙等待和低功耗的方式:
/*忙等待的方式*/
void vApplicationIdleHook(void)
{
for(;;)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
HAL_Delay(100); // 在空闲任务中这是可以接受的
//vTaskDelay(100);
}
}
/*低功耗的方式*/
void vApplicationIdleHook (void)
{
static TickType_t xLastToggleTime = 0;
TickType_t xCurrentTime = xTaskGetTickCount();
if((xCurrentTime - xLastToggleTime) >= pdMS_TO_TICKS(100))
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
xLastToggleTime = xCurrentTime;
}
}
思考:
空闲钩子函数里写for循环和haldelay没有问题的原因是因为其优先级是最低的,就算你建立的任务的优先级也是0,但是其优先级还是高于空闲任务。
但是假如你使用vtaskdelay或者一些将空闲任务阻塞的手段,那么可能会导致没有任务处于就绪态(都被阻塞)那么就会导致系统崩溃。
更多推荐



所有评论(0)