embedded resource_error ai_generated true

FreeRTOS: Timer command queue overflow, timer service task cannot process commands

ID: embedded/freertos-timer-queue-overflow

Also available as: JSON · Markdown · 中文
90%Fix Rate
87%Confidence
1Evidence
2024-01-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
FreeRTOS v10.5.1 active
STM32Cube_FW_L4 v1.18.0 active
IAR EWARM v9.40.1 active

Root Cause

Timer command queue length (configTIMER_QUEUE_LENGTH) is insufficient for the number of concurrent timer API calls from ISRs or tasks, causing queue send failures.

generic

中文

定时器命令队列长度(configTIMER_QUEUE_LENGTH)不足以容纳来自ISR或任务的并发定时器API调用,导致队列发送失败。

Official Documentation

https://www.freertos.org/FreeRTOS-Timers.html

Workarounds

  1. 93% success Increase configTIMER_QUEUE_LENGTH in FreeRTOSConfig.h to at least 2x the number of concurrent timer commands (e.g., from 10 to 20). Also set configTIMER_TASK_STACK_DEPTH to 256 to avoid stack overflow.
    Increase configTIMER_QUEUE_LENGTH in FreeRTOSConfig.h to at least 2x the number of concurrent timer commands (e.g., from 10 to 20). Also set configTIMER_TASK_STACK_DEPTH to 256 to avoid stack overflow.
  2. 85% success Batch timer commands: instead of calling xTimerChangePeriod() per timer, use a single xTimerStop() and xTimerStart() with new period for grouped timers.
    Batch timer commands: instead of calling xTimerChangePeriod() per timer, use a single xTimerStop() and xTimerStart() with new period for grouped timers.

中文步骤

  1. 在FreeRTOSConfig.h中将configTIMER_QUEUE_LENGTH增加到并发定时器命令数的至少2倍(例如从10增加到20)。同时将configTIMER_TASK_STACK_DEPTH设置为256以避免堆栈溢出。
  2. 批量处理定时器命令:不要为每个定时器调用xTimerChangePeriod(),而是对分组定时器使用单个xTimerStop()和xTimerStart()设置新周期。

Dead Ends

Common approaches that don't work:

  1. Increase timer task priority to highest level 95% fail

    Higher priority does not increase queue length; timer commands still overflow if queue is full.

  2. Remove all timer API calls from ISRs 65% fail

    ISR calls are often necessary for time-critical operations; removal may break functionality, and overflow can still occur from tasks.