FreeRTOS Deep Dive · Part 7 of 8

Low-Power RTOS & Tickless Idle

FreeRTOS STM32 Advanced Source Code
FreeRTOS Part 7
FreeRTOS Deep Dive — Part 7
Low-Power RTOS & Tickless Idle
44:05
Part 7 / 8
44:05
Advanced
FreeRTOS v10 · STM32F4
01

Overview

Battery-powered IoT devices live and die by average current draw. FreeRTOS's tickless idle mode lets the MCU sleep between ticks instead of spinning in the idle loop — cutting average current from milliamps to microamps. This part covers enabling it, configuring wake sources, and verifying the savings with a current probe.

  • Enable tickless idle with configUSE_TICKLESS_IDLE
  • Implement a custom sleep function using STM32 Stop Mode
  • Reconfigure clocks correctly after waking from deep sleep
  • Measure average current reduction on real hardware
02

Sleep Modes

😴
Standard Idle
Idle task spins. Tick fires every 1ms. MCU never sleeps. Average current: 5–20mA.
configUSE_TICKLESS_IDLE = 0
💤
Tickless Idle
FreeRTOS calculates max sleep time, programs a wake timer, and calls your sleep function. Average current: 10–100µA.
configUSE_TICKLESS_IDLE = 1
Custom Sleep
Override vPortSuppressTicksAndSleep() for vendor-specific Stop/Standby modes and deepest possible sleep.
configUSE_TICKLESS_IDLE = 2
03

Code

01
Custom tickless sleep on STM32
low_power.c
C
#define configUSE_TICKLESS_IDLE         1
#define configEXPECTED_IDLE_TIME_BEFORE_SLEEP  2

void vPortSuppressTicksAndSleep(TickType_t xIdleTime) {
    uint32_t ms = xIdleTime * portTICK_PERIOD_MS;
    HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, ms,
        RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
    // Enter STM32 Stop Mode — wakes on RTC or GPIO EXTI
    HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
    // Stop mode resets PLL — must reconfigure clocks
    SystemClock_Config();
    HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
}

void vSensorTask(void *pv) {
    TickType_t xLast = xTaskGetTickCount();
    while(1) {
        ReadAndTransmit(); // ~8ms of work
        // System sleeps ~992ms per cycle
        vTaskDelayUntil(&xLast, pdMS_TO_TICKS(1000));
    }
}
⚠️
Reconfigure clocks after Stop mode
On STM32, Stop mode resets the PLL. Always call SystemClock_Config() after waking or all peripherals run at the wrong frequency.

Continue the Series

Work through all 8 parts of the FreeRTOS Deep Dive to master real-time embedded systems programming.