Check the entire code examples at the ESP32-Repo.
Setting up your project to use vApplicationIdleHook for the ESP32make menuconfig
- Enter
component config
FreeRTOS
Use FreeRTOS legacy hooks
> Press y/Y
Enable legacy idle hook
> Press y/Y
- Save your changes
NOTE: In your main.c
file do not forget to add the function void vApplicationIdleHook(void)
Most of the priority tasks have a fixed duration or time to run. Most of the time apart from serving these priority tasks is spent in an idle state by the microcontroller.
However, this idle state can be utilized to get generic information about the system and run continuously when the other tasks are in a blocked state.
It is important to note that the IdleTask has a priority of 0 assigned by the FreeRTOS kernel. Hence even a Task having a priority of 1 will be given priority instead of the IdleTask.
Using the APIThere is no API for IdleTask
This needs to be added to your main.c
file
void vApplicationIdleHook(void)
{
// Write your code here
// NOTE, Make sure it is compact
}
Code exampleIn this code example we will use a counter variable to store the number of cycles spent by the controller in the idle state.
static uint32_t ulIdleCycleCount;
void print_idle(void *ignore);
void app_main()
{
xTaskCreate(&print_idle, "print task", 2048, NULL, 1, NULL);
}
// Create a periodic task that prints the idleCount value
void print_idle(void *ignore)
{
while(1)
{
printf("IdleCount: %u\n", ulIdleCycleCount);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
// Increments the idleCount variable
void vApplicationIdleHook(void)
{
ulIdleCycleCount++;
}
OutputSince my kernel is set to a speed of 100Hz this is my output
IdleCount: 0
IdleCount: 50
IdleCount: 100
ConclusionIn this tutorial we have learnt about IdleTasks, how they can be implemented in FreeRTOS and where they can be beneficial.
In the next tutorial we shall learn about how to dynamically change the priority of different tasks.
Meanwhile, have fun and keep learning!
WebsiteAll my tutorials and projects will be documented on my website and my GitHub repository.
Do check them out for the most recent updates.
Comments
Please log in or sign up to comment.