FreeRTOS Exercise Lab04

From EdWiki

FreeRTOS Lab 04

  • The tasks created in the previous examples spend most of their time in the Blocked state. While in this state, they are not able to run, so cannot be selected by the scheduler.
  • There must always be at least one task that can enter the Running state. To ensure this is the case, an Idle task is automatically created by the scheduler when vTaskStartScheduler() is called. The idle task does very little more than sit in a loop—so, it is always able to run.
  • The idle task has the lowest possible priority (priority zero), to ensure it never prevents a higher priority application task from entering the Running state.
  • It is possible to add application specific functionality directly into the idle task through the use of an idle hook (or idle callback) function — a function that is called automatically by the idle task once per iteration of the idle task loop.
  1. Create two Tasks as in the Lab 01
  2. Create a third Task which will print the number of idle counts.
  • Note
    • configUSE_IDLE_HOOK must be set to 1 in FreeRTOSConfig.h for the idle hook function to get called.
    • An Idle task hook function must never attempt to block or suspend.
    • Idle task hook function must have the name and prototype as shown below
void vApplicationIdleHook( void )

/* Declare a variable that will be incremented by the hook function. */
volatile uint32_t ulIdleCycleCount = 0UL;
/* Idle hook functions MUST be called vApplicationIdleHook(), take no parameters, and return void. */
void vApplicationIdleHook( void )
    /* This hook function does nothing but increment a counter. */

Freertos contnuous idle tasks.png

  • The Idle task is immediately swapped out to allow Task 2 to execute at the instant Task 2 leaves the Blocked state.
    Task 2 pre-empts the idle task automatically.