你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】FreeRTOS 定时器精度研究

[复制链接]
STMCU小助手 发布时间:2022-2-25 19:31
背景介绍
FreeRTOS 以其移植方便,高度可定制,footprint 小,使其在嵌入式操作系统中的份额不容小觑!尤以免费 license,颇受开发者青睐。

问题描述
客户在基于 cortex-m3 的平台上使用 FreeRTOS 系统提供的定时器功能时,意外发现定时器的精确度不够高。譬如,设置 1 秒钟的定时器,理论上 1 秒超时,并且执行相应的回调函数。但是调试却发现,有时回调函数是在 1.4 秒后被执行!这对于精度要求较高的实时系统,是不能接受的!

问题复现与分析
首先在 stm32f407-discovery 平台移植 FreeRTOS,并创建一个定时器,在其回调函数里 toggleled 灯,并测量被执行的时间。鉴于 FreeRTOS 是一个多任务可抢占式系统,这个问题需要在多种情况下分析。

Case 1 :
单任务,即系统里仅有 timer task 和 idle task。整个系统最高优先级为 4,Timer task 的优先级为默认优先级 2。
在这种环境下,回调函数能精确的以 1 秒的时长超时执行回调函数。虽然此刻精度能满足要求,但是实际的系统一般会包含多个 task。
以下为测试的日志,显而易见,该定时器严格 1 秒钟超时。
expired 1000
expired 2000
expired 3000
expired 4000
expired 5000
expired 6000



Case 2 :
多任务,即系统里不仅有 timer task 和 idle task,还有用户创建的 task。整个系统最高优先级为 4,Timer task 的优先级为默认优先级 2。
以下为测试的日志。
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
expired 1000
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
expired 3000
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
expired 4000
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
expired 5000
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
expired 6000

此时定时器回调函数有时能准确地被调度执行,有时则偏差较大,与期望值整整延迟了 1 秒钟。

Case 3 :
多任务,即系统里不仅有 timer task 和 idle task,还有用户创建的 task。整个系统最高优先级为 4,Timer task 的优先级为默认优先级 4。这样设置优先级,是希望能通过将 timer task 设置为最高优先级,以期望调度器能优先调度执行 timer task。
以下为实测的日志。
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
expired 1000
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
expired 3000
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
expired 4000
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
expired 5000
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
UART Printf Example: retarget the C library printf function to the UART
expired 6000

虽然调整了 timer task 的优先级至最高,但是依然会出现严重的偏差。


解决方案
考虑到 FreeRTOS 定时器的精准性不高,建议客户使用 SysTick 或者 MCU 的外设硬件定时器。

收藏 评论0 发布时间:2022-2-25 19:31

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版