查看: 3756|回复: 0

FreeRTOS 定时器精度研究

[复制链接]
发表于 2016-10-11 10:52:51 | 显示全部楼层 |阅读模式
1.webp.jpg

背景介绍                       
FreeRTOS 以其移植方便,高度可定制,footprint 小,使其在嵌入式操作系统中的份额不容小觑! 尤以免费 license,颇受开发者青睐。

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

问题复现与分析
首先在stm32f407-discovery平台移植FreeRTOS,并创建一个定时器,在其回调函数里toggle led灯,并测量被执行的时间。鉴于FreeRTOS是一个多任务可抢占式系统,这个问题需要在多种情况下分析。
Case 1 :
单任务,即系统里仅有timertask和idle task。整个系统最高优先级为4,Timer task的优先级为默认优先级2。
在这种环境下,回调函数能精确的以1秒的时长超时执行回调函数。虽然此刻精度能满足要求,但是实际的系统一般会包含多个task。
以下为测试的日志,显而易见,该定时器严格1秒钟超时。
expired 1000
expired 2000
expired 3000
expired 4000
expired 5000
expired 6000
Case 2 :
多任务,即系统里不仅有timertask和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,Timertask的优先级为默认优先级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
虽然调整了timertask的优先级至最高,但是依然会出现严重的偏差。

解决方案   
考虑到FreeRTOS定时器的精准性不高,建议客户使用SysTick或者MCU的外设硬件定时器。
重要通知 - 请仔细阅读
意法半导体公司及其子公司(“ST”)保留随时对ST 产品和/ 或本文档进行变更、更正、增强、修改和改进的权利,恕不另行通知。买方订货之前应获取关于ST 产品的最新信息。ST 产品的销售依照订单确认时的相关ST 销售条款。
买方自行负责对ST 产品的选择和使用, ST 概不承担与应用协助或买方产品设计相关的任何责任。
ST 不对任何知识产权进行任何明示或默示的授权或许可。
转售的ST 产品如有不同于此处提供的信息的规定,将导致ST 针对该产品授予的任何保证失效。
ST 和ST 徽标是ST 的商标。所有其他产品或服务名称均为其各自所有者的财产。
本文档中的信息取代本文档所有早期版本中提供的信息。




文章来源:微信公众号  融创芯城(一站式电子元器件、PCB、PCBA购买服务平台,项目众包平台)


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表