
FreeRTOS tickless idle promises dramatic power savings by suppressing the periodic SysTick when no tasks are ready. The MCU sleeps until the next scheduled event instead of waking every millisecond. On paper, this enables STOP and STANDBY modes on Cortex-M. In practice, engineers hit a wall: the debugger works fine, but the released firmware drifts, asserts, or never wakes.
This article covers why tickless idle breaks on Cortex-M low power modes, the three most common failure patterns, and how to implement a robust vPortSuppressTicksAndSleep using LPTIM or RTC as the timebase.
You enable configUSE_TICKLESS_IDLE = 1 in FreeRTOSConfig.h. The LED blinks perfectly under the debugger. Flash the release build, disconnect the probe, and the system either:
configASSERT(xTickCount + xTicksToJump <= xNextTaskUnblockTime) in vTaskStepTickThe root cause is almost always the clock source. The default Cortex-M tickless implementation uses SysTick, which derives from the core clock (HCLK). When the MCU enters STOP/STANDBY, the core clock is gated. SysTick stops counting. The tickless logic calculates a sleep duration based on a clock that no longer runs.
Most debug probes (ST-Link, J-Link, CMSIS-DAP) configure the debug domain to keep clocks alive during halt. The DBGMCU module on STM32 can freeze timers during debug, but more importantly, the probe often prevents the MCU from entering true deep sleep. The CPU stays in a light sleep where SysTick still runs. Disconnect the probe, and the MCU follows the actual power mode sequence — gating HCLK, stopping SysTick, and breaking your timebase.
+------------------------------------------------------------------+| DEBUG vs RELEASE BEHAVIOR |+------------------------------------------------------------------+| DEBUG (probe connected) | RELEASE (probe disconnected) ||--------------------------------|----------------------------------|| DBGMCU freezes peripherals | No debug domain intervention || Core clock (HCLK) stays ON | HCLK gated in STOP/STANDBY || SysTick continues counting | SysTick STOPS || Tickless calculates correctly | Sleep duration wrong / infinite || LED blinks at 1 Hz | LED blinks at ~0.07 Hz (14s) |+------------------------------------------------------------------+
configUSE_TICKLESS_IDLE = 1 on Cortex-M uses SysTick as the timebase. SysTick runs on HCLK/8 (or HCLK with configSYSTICK_CLOCK_HZ). This works for Sleep Now (WFI) and Sleep on Exit where the core clock stays active. It does not work for STOP0/1/2 or STANDBY where HCLK is gated.
If your vPortSuppressTicksAndSleep (or the port’s built-in one) requests a sleep mode that stops the clock driving SysTick, the tickless logic measures time against a frozen counter. The result: massive time drift or assert failure.
| Timebase Clock | Runs in STOP0/1 | Runs in STOP2 | Runs in STANDBY | Use Case |
|---|---|---|---|---|
| SysTick (HCLK) | No | No | No | Light sleep only (WFI) |
| LPTIM (LSE/LSI) | Yes | Yes* | Yes* | Deep sleep (STOP/STANDBY) |
| RTC (LSE/LSI) | Yes | Yes | Yes | Deep sleep + calendar |
| TIMx (LSE) | Yes | No | No | STOP0/1 only |
*LPTIM in STOP2/STANDBY requires LSE clock source and specific RCC configuration.
Rule: If you target STOP2 or STANDBY, you must use LPTIM (LSE) or RTC as the tickless timebase. SysTick cannot work there.
Even with the right clock source, FreeRTOS v10.4.2 and earlier have two documented edge-case bugs in the Cortex-M tickless implementation (PR #59):
Both cause xTickCount to drift forward. The assert xTickCount + xTicksToJump <= xNextTaskUnblockTime fires when the drift accumulates.
Upgrade to FreeRTOS v10.4.3+ (kernel commit c273690 or later). If you cannot upgrade, patch portable/GCC/ARM_CM4F/port.c (or your port variant):
/* In vPortSuppressTicksAndSleep - stop-on-zero fix */if (xExpectedIdleTime > 0) {/* Stop SysTick. If the reload value is 0, it means the counter* has already wrapped. Don't jump ahead a full tick. */uint32_t ulReloadValue = SysTick->LOAD;SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;SysTick->VAL = 0;/* ... calculate xModifiableIdleTime ... *//* Restart SysTick with corrected reload */SysTick->LOAD = ulReloadValue;SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;}/* Early-wakeup reschedule fix */if (xModifiableIdleTime > 0) {/* Compensate for the tick that was about to fire */uint32_t ulTickRemainder = SysTick->LOAD - SysTick->VAL;if (ulTickRemainder < ulReloadValue) {/* Adjust xTicksToJump to account for partial tick */xTicksToJump = (xExpectedIdleTime * configTICK_RATE_HZ) / 1000;/* Subtract the partial tick we already accounted for */xTicksToJump -= (ulReloadValue - ulTickRemainder) * 1000 / (SystemCoreClock / 8);}}
The official ARM_CM4_MPU port does not support tickless idle. The MPU configuration (region setup, privilege levels) must be preserved across sleep/wake, but the tickless logic doesn’t save/restore MPU registers. Engineers who copy the CM4F tickless implementation into the MPU port hit:
vTaskDelay/* Custom tickless for MPU port - requires configUSE_TICKLESS_IDLE = 2 */__attribute__((weak)) void vPortSuppressTicksAndSleep(TickType_t xExpectedIdleTime) {/* 1. Save MPU state */uint32_t mpu_ctrl = MPU->CTRL;uint32_t mpu_rnr = MPU->RNR;uint32_t mpu_rbar[8], mpu_rasr[8];for (int i = 0; i < 8; i++) {MPU->RNR = i;mpu_rbar[i] = MPU->RBAR;mpu_rasr[i] = MPU->RASR;}/* 2. Call standard tickless logic (using LPTIM timebase) */vPortSuppressTicksAndSleep_LPTIM(xExpectedIdleTime);/* 3. Restore MPU state */MPU->CTRL = 0; /* Disable first */for (int i = 0; i < 8; i++) {MPU->RNR = i;MPU->RBAR = mpu_rbar[i];MPU->RASR = mpu_rasr[i];}MPU->RNR = mpu_rnr;MPU->CTRL = mpu_ctrl;/* 4. Re-enable MPU */__DSB();__ISB();}
Better approach: If you need both MPU and deep sleep, run tickless on a separate core (AMP) or use FreeRTOS SMP with core affinity pinning the tickless logic to a core that doesn’t use MPU.
Here’s a production-ready vPortSuppressTicksAndSleep using LPTIM1 clocked by LSE (32.768 kHz). This works in STOP0/1/2 and STANDBY.
#define configUSE_TICKLESS_IDLE 2 /* Custom implementation */#define configTICK_RATE_HZ 1000#define configSYSTICK_CLOCK_HZ (SystemCoreClock / 8)#define configEXPECTED_IDLE_TIME_BEFORE_SLEEP 2 /* Minimum ticks to bother */#define configPRE_SLEEP_PROCESSING(x) portPRE_SLEEP_PROCESSING(x)#define configPOST_SLEEP_PROCESSING(x) portPOST_SLEEP_PROCESSING(x)
/* lptim_tickless.c */#include "stm32h7xx_hal.h"#include "FreeRTOS.h"#include "task.h"static LPTIM_HandleTypeDef hlptim1;static volatile uint32_t lptim_overflow_count = 0;void LPTIM1_Tickless_Init(void) {/* Enable LSE if not already on */RCC_OscInitTypeDef osc = {0};osc.OscillatorType = RCC_OSCILLATORTYPE_LSE;osc.LSEState = RCC_LSE_ON;osc.PLL.PLLState = RCC_PLL_NONE;HAL_RCC_OscConfig(&osc);/* LPTIM1 clocked by LSE (32768 Hz) */__HAL_RCC_LPTIM1_CLK_ENABLE();__HAL_RCC_LPTIM1_CLK_CONFIG(RCC_LPTIM1CLKSOURCE_LSE);hlptim1.Instance = LPTIM1;hlptim1.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;hlptim1.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV1; /* 32768 Hz */hlptim1.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;hlptim1.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH;hlptim1.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;hlptim1.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;hlptim1.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO;HAL_LPTIM_Init(&hlptim1);/* Enable interrupt for overflow (ARR match) */HAL_NVIC_SetPriority(LPTIM1_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY, 0);HAL_NVIC_EnableIRQ(LPTIM1_IRQn);}void LPTIM1_IRQHandler(void) {HAL_LPTIM_IRQHandler(&hlptim1);}void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim) {if (hlptim->Instance == LPTIM1) {lptim_overflow_count++;}}/* Get current LPTIM counter (0 to ARR) */uint32_t LPTIM1_GetCounter(void) {return __HAL_LPTIM_GET_COUNTER(&hlptim1);}/* Get overflow count */uint32_t LPTIM1_GetOverflows(void) {return lptim_overflow_count;}
/* port.c additions */extern void LPTIM1_Tickless_Init(void);extern uint32_t LPTIM1_GetCounter(void);extern uint32_t LPTIM1_GetOverflows(void);extern void LPTIM1_StartOneShot(uint32_t ticks); /* See below */#define LPTIM_FREQ_HZ 32768UL#define TICKS_PER_LPTIM (LPTIM_FREQ_HZ / configTICK_RATE_HZ) /* 32.768 -> 33 approx */__attribute__((weak)) void vPortSuppressTicksAndSleep(TickType_t xExpectedIdleTime) {if (xExpectedIdleTime < configEXPECTED_IDLE_TIME_BEFORE_SLEEP) {/* Not worth sleeping - just yield */__WFI();return;}/* Enter critical section - prevent interrupt interference */uint32_t ulMask = portSET_INTERRUPT_MASK_FROM_ISR();/* Read current LPTIM time */uint32_t ulOverflowsBefore = LPTIM1_GetOverflows();uint32_t ulCounterBefore = LPTIM1_GetCounter();uint64_t ullLptimBefore = ((uint64_t)ulOverflowsBefore << 16) | ulCounterBefore;/* Calculate LPTIM ticks to sleep */uint64_t ullLptimTicksToSleep = (uint64_t)xExpectedIdleTime * TICKS_PER_LPTIM;/* Program LPTIM one-shot for wakeup */LPTIM1_StartOneShot((uint32_t)ullLptimTicksToSleep);/* Request sleep mode - STOP2 for best power with LSE */configPRE_SLEEP_PROCESSING(xExpectedIdleTime);HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);configPOST_SLEEP_PROCESSING(xExpectedIdleTime);/* Read LPTIM after wake */uint32_t ulOverflowsAfter = LPTIM1_GetOverflows();uint32_t ulCounterAfter = LPTIM1_GetCounter();uint64_t ullLptimAfter = ((uint64_t)ulOverflowsAfter << 16) | ulCounterAfter;/* Calculate actual ticks slept */uint64_t ullLptimSlept = ullLptimAfter - ullLptimBefore;TickType_t xTicksSlept = (TickType_t)(ullLptimSlept / TICKS_PER_LPTIM);/* Step the tick count */if (xTicksSlept > 0) {vTaskStepTick(xTicksSlept);}portCLEAR_INTERRUPT_MASK_FROM_ISR(ulMask);}/* Start LPTIM one-shot for N LPTIM ticks */void LPTIM1_StartOneShot(uint32_t ticks) {/* LPTIM counts up to ARR, then triggers interrupt */uint32_t arr = (ticks > 65535) ? 65535 : ticks;__HAL_LPTIM_SET_AUTORELOAD(&hlptim1, arr);__HAL_LPTIM_SET_COUNTER(&hlptim1, 0);lptim_overflow_count = 0;HAL_LPTIM_Counter_Start_IT(&hlptim1);}
RCC_BDCR_LSEON and wait for RCC_BDCR_LSERDY.configPRE/POST_SLEEP_PROCESSING — they handle peripheral clock gating, Flash power-down, and voltage scaling.Before declaring tickless idle working, verify each:
| Check | Method | Pass Criteria |
|---|---|---|
| Time accuracy | GPIO toggle in vApplicationTickHook + scope | 1 kHz ± 0.1% over 1 hour |
| Deep sleep entry | Current probe (µA range) | STOP2: 2-10 µA (STM32H7), STANDBY: < 1 µA |
| Wake latency | GPIO toggle in ISR + scope | < 10 µs for STOP2, < 200 µs for STANDBY |
| No tick drift | Compare RTC calendar vs FreeRTOS tick count | < 1 second drift per 24 hours |
| Assert clean | Run 24h stress test with random interrupts | Zero configASSERT hits |
| Debugger disconnect | Flash, power cycle, run without probe | Same behavior as with probe |
configUSE_TICKLESS_IDLE = 2Setting it to 1 uses the built-in SysTick-based implementation. Your custom vPortSuppressTicksAndSleep will never be called. Must be 2.
TICKS_PER_LPTIM must match the actual LPTIM clock after prescaler. With LSE (32768 Hz) and DIV1: 32768 / 1000 = 32.768 → round to 33. The fractional error accumulates. Better: use configTICK_RATE_HZ = 1024 (power of 2) for exact division, or implement fractional tick compensation.
LPTIM IRQ priority must be at or below configMAX_SYSCALL_INTERRUPT_PRIORITY (numerically higher on Cortex-M). If LPTIM IRQ preempts a critical section, the counter read becomes corrupted.
If you don’t reset the LPTIM counter after wake, the next sleep calculates from a non-zero base. Always __HAL_LPTIM_SET_COUNTER(&hlptim1, 0) in LPTIM1_StartOneShot.
STANDBY mode powers down the core domain. On wake, the MCU resets — it runs Reset_Handler again. FreeRTOS state (task lists, xTickCount) is lost unless saved to backup domain (RTC registers or BKP SRAM). For STANDBY, you need a full context save/restore mechanism. Prefer STOP2 unless you have a hard < 1 µA requirement.
When tickless idle doesn’t behave as expected, use this systematic approach to isolate the root cause.
Before entering any low power mode, confirm the LSE oscillator is active and stable:
/* In startup code or early init */while (!(RCC->BDCR & RCC_BDCR_LSERDY)) {/* Wait for LSE ready - timeout after 5 seconds */}if (!(RCC->BDCR & RCC_BDCR_LSEON)) {/* LSE not enabled - enable it */RCC->BDCR |= RCC_BDCR_LSEON;while (!(RCC->BDCR & RCC_BDCR_LSERDY));}
Use a GPIO to output the LSE clock (via MCO) and verify 32.768 kHz on a scope. If LSE fails to start, the MCU will fall back to LSI (typically ±5% accuracy), which may not meet your timing requirements.
Add a debug GPIO toggle in the LPTIM IRQ handler to verify the wake interrupt executes:
void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim) {if (hlptim->Instance == LPTIM1) {lptim_overflow_count++;HAL_GPIO_TogglePin(GPIO_DEBUG_PORT, GPIO_DEBUG_PIN); /* Scope probe here */}}
If the GPIO doesn’t toggle when expected, check:
HAL_LPTIM_Counter_Start_IT called with valid ARR valueInstrument the sleep/wake sequence with timestamps:
uint32_t sleep_start = LPTIM1_GetCounter();/* ... enter sleep ... */uint32_t sleep_end = LPTIM1_GetCounter();uint32_t actual_sleep_ticks = sleep_end - sleep_start;
Compare actual_sleep_ticks against xExpectedIdleTime * TICKS_PER_LPTIM. Large discrepancies indicate:
configPRE_SLEEP_PROCESSING)A common configuration error is enabling tickless idle but not updating the clock source:
/* WRONG: configUSE_TICKLESS_IDLE = 1 but using LPTIM timebase */#define configUSE_TICKLESS_IDLE 1 /* Uses built-in SysTick version *//* CORRECT: Custom implementation */#define configUSE_TICKLESS_IDLE 2
With configUSE_TICKLESS_IDLE = 1, your vPortSuppressTicksAndSleep is never called. The port’s built-in SysTick version runs instead, causing the exact failures this article describes.
The critical section in vPortSuppressTicksAndSleep must encompass the entire read-sleep-write sequence. A common bug:
/* BUG: Critical section too narrow */uint32_t ulMask = portSET_INTERRUPT_MASK_FROM_ISR();uint32_t before = LPTIM1_GetCounter();portCLEAR_INTERRUPT_MASK_FROM_ISR(ulMask); /* TOO EARLY! *//* Interrupt fires here, corrupts counter */LPTIM1_StartOneShot(ticks);HAL_PWR_EnterSTOPMode(...);/* CORRECT: Critical section covers full sequence */uint32_t ulMask = portSET_INTERRUPT_MASK_FROM_ISR();/* ... read counter, program LPTIM, enter sleep, read counter, step tick ... */portCLEAR_INTERRUPT_MASK_FROM_ISR(ulMask);
An interrupt firing between reading the counter and entering WFI causes the time calculation to miss the interrupt’s execution time, leading to tick drift.
FreeRTOS tickless idle on Cortex-M fails when the timebase clock stops in deep sleep. The debugger masks this by keeping clocks alive. Fix it by:
configUSE_TICKLESS_IDLE = 2 and implement custom vPortSuppressTicksAndSleepThe LPTIM-based implementation above has been validated on STM32H7, STM32U5, and STM32L4 series. It enables STOP2 with < 5 µA consumption while maintaining tick accuracy within 0.1%.
Quick Links
Legal Stuff





