FreeRTOS Essential APIs and Macros Reference

1. Task Status Information Display

1.1 Purpose of vTaskList

The vTaskList function provides a comprehensive view of all running tasks, displaying their current state information in a formatted string.

1.2 Functon Signature

void vTaskList(char *pcWriteBuffer)

Buffer contains the following fields:

  • Name: Task idantifier
  • State: Current task state (Ready, Running, Blocked, Suspended)
  • Priority: Task priority level
  • Stack: Stack watermark usage
  • Num: Task number for distinguishing tasks with identical names

Note: Stack size should typically be allocated at 1.5-2 times the normal usage to handle edge cases adequately.

1.3 Implementation Example

void monitor_task(void *parameter)
{
    char buffer[256];
    
    while (true)
    {
        vTaskList(buffer);
        
        taskENTER_CRITICAL();
        printf("TaskName              State  Priority  Stack  Number\r\n%s\r\n", buffer);
        taskEXIT_CRITICAL();
        
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

2. Dynamic Task Creation

2.1 Purpose

The xTaskCreate function dynamically allocates and creates a new task at runtime.

3. Task Deletion

Use xTaskDelete() to remove dynamically created tasks from the system.

void cleanup_task(void *parameter)
{
    TaskHandle_t target_handle;
    target_handle = xTaskGetHandle("led_toggle_task");
    
    taskENTER_CRITICAL();
    printf("Task removed successfully\r\n");
    taskEXIT_CRITICAL();
    
    vTaskDelete(target_handle);
}

4. Obtaining Task Handle

Use xTaskGetHandle() to retrieve a task's handle by its name for subsequent operations.

target_handle = xTaskGetHandle("led_toggle_task");

5. Starting the Scheduler

The vTaskStartScheduler function initializes and starts the FreeRTOS scheduler, which essentially enables the system tick interrupt.

6. Critical Sections

FreeRTOS provides two macros for entering and exiting critical sections:

taskENTER_CRITICAL();
/* Protected code section */
taskEXIT_CRITICAL();

Critical sections disable interrupts to ensure atomic access to shared resources.

7. Configuring Priority Levels

In FreeRTOSConfig.h, the configMAX_PRIIORITIES macro defines the maximum number of priority levels (default is 7). This can be adjusted based on application requirements.

8. Total Heap Memory

FreeRTOS manages heap memory dynamically. The total heap size is defined during configuration, typically around 3KB for resource-constrained systems.

9. Heap Usage Monitoring

9.1 xPortGetMinimumEverFreeHeapSize()

Returns the minimum free heap memory observed since system startup.

9.2 xPortGetFreeHeapSize()

Returns the current amount of free heap memory available.

9.3 Usage Example

void heap_monitor_task(void *parameter)
{
    while (true)
    {
        taskENTER_CRITICAL();
        printf("CurrentFree = %d bytes  MinimumEver = %d bytes\r\n", 
               xPortGetFreeHeapSize(), 
               xPortGetMinimumEverFreeHeapSize());
        taskEXIT_CRITICAL();
        
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

10. Retrieving Current Tick Count

TickType_t xTaskGetTickCount(void);

TickType_t is a typedef for uint32_t.

11. Task Delay Functions

11.1 Relative Delay (vTaskDelay)

Provides relative delay based on system tick interrupts. Note that since tick interrupts have the lowest priority, delay accuracy may be affected if higher-priority interrupts preempt the system.

11.2 Absolute Delay (vTaskDelayUntil)

void vTaskDelayUntil(TickType_t *pxPreviousWakeTime, 
                     const TickType_t xTimeIncrement)

Parameters:

  • pxPreviousWakeTime: Pointer to the tick count when the task last executed
  • xTimeIncrement: Delay period in ticks

Important: Ensure INCLUDE_vTaskDelayUntil is set to 1 in FreeRTOSConfig.h.

void periodic_task(void *parameter)
{
    TickType_t last_wake_time;
    last_wake_time = xTaskGetTickCount();
    
    while (true)
    {
        execute_algorithm();
        HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_8);
        
        vTaskDelayUntil(&last_wake_time, pdMS_TO_TICKS(1000));
    }
}

12. Millisecond to Tick Conversion

The pdMS_TO_TICKS() macro converts milliseconds to tick periods:

vTaskDelay(pdMS_TO_TICKS(1000));  // Delay for 1000 milliseconds

13. Task Yield

The taskYIELD() macro causes the currently running task to yield the processor, allowing other ready tasks of the same priority to execute.

taskYIELD();

Tags: FreeRTOS embedded-systems rtos ARM cortex-m

Posted on Fri, 19 Jun 2026 16:50:57 +0000 by netdog