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

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

[复制链接]
STMCU小助手 发布时间:2022-2-25 19:31
背景介绍
1 L7 D7 _( s) h" N9 f: y+ nFreeRTOS 以其移植方便,高度可定制,footprint 小,使其在嵌入式操作系统中的份额不容小觑!尤以免费 license,颇受开发者青睐。. v6 b! c5 V. s: Y

5 D# C' n5 q: ~2 J- T% X问题描述. e4 A  H8 o2 [, M, d
客户在基于 cortex-m3 的平台上使用 FreeRTOS 系统提供的定时器功能时,意外发现定时器的精确度不够高。譬如,设置 1 秒钟的定时器,理论上 1 秒超时,并且执行相应的回调函数。但是调试却发现,有时回调函数是在 1.4 秒后被执行!这对于精度要求较高的实时系统,是不能接受的!% i' i* [3 i7 P5 J# W* a

% I  R& C# K, z2 k+ j- U, D; K* N问题复现与分析
; ^+ n6 x, f& g5 @: |首先在 stm32f407-discovery 平台移植 FreeRTOS,并创建一个定时器,在其回调函数里 toggleled 灯,并测量被执行的时间。鉴于 FreeRTOS 是一个多任务可抢占式系统,这个问题需要在多种情况下分析。
. Y: z6 p2 @# w9 w+ j4 U1 c% a/ \  S
  R: t0 `! A: X* u  H: y8 C- f5 [, O$ aCase 1 :% _. k% {/ N- H3 c- a% p
单任务,即系统里仅有 timer task 和 idle task。整个系统最高优先级为 4,Timer task 的优先级为默认优先级 2。
3 j5 Q* m: W2 U# u* I& ]& a9 u5 n在这种环境下,回调函数能精确的以 1 秒的时长超时执行回调函数。虽然此刻精度能满足要求,但是实际的系统一般会包含多个 task。
$ e) J7 {' c* R* c+ V1 d2 u0 L以下为测试的日志,显而易见,该定时器严格 1 秒钟超时。- L- B6 k1 b  {* q
expired 1000
4 |  T, ]. O2 a# j; B! B& O) Fexpired 2000
/ `( `( c' \) T2 Gexpired 3000
$ X8 |" }7 m0 g' @$ k: j& M7 Qexpired 4000
3 u. G  R2 t/ c3 n9 _4 l5 @expired 5000
$ P( H3 |% O0 ]; ^# x- ^3 uexpired 6000
5 G# q8 ~0 T. _: m…
% D6 P) ^" Z& H8 l+ v0 O7 k* k
( _. l) v4 v+ Y) m: M7 Z+ n: g3 l, D5 A4 M. \- J
Case 2 :* X& W9 V" l9 ^3 H! y5 B+ D
多任务,即系统里不仅有 timer task 和 idle task,还有用户创建的 task。整个系统最高优先级为 4,Timer task 的优先级为默认优先级 2。
' B% b; u' ^% k( f7 l! _. u4 U以下为测试的日志。
3 B' i1 T  w. e  }UART Printf Example: retarget the C library printf function to the UART9 ~, X& X2 q! R& {- \$ e6 `
UART Printf Example: retarget the C library printf function to the UART
" J/ d. M9 ~+ c8 lUART Printf Example: retarget the C library printf function to the UART$ P  A1 T" `7 j
expired 1000& ]% s8 f  E; ?+ @1 B9 R5 j' ~; N
UART Printf Example: retarget the C library printf function to the UART7 C2 }2 ?* v, J: X( M+ ~" N  d
UART Printf Example: retarget the C library printf function to the UART
0 {( U! G* U2 SUART Printf Example: retarget the C library printf function to the UART
* b$ C7 L- O5 f& \9 nUART Printf Example: retarget the C library printf function to the UART. A+ G1 Z; h: g9 X9 p
UART Printf Example: retarget the C library printf function to the UART+ k2 \' y& |# p" d; c! t2 b
expired 3000
6 v6 u: O  O) R7 aUART Printf Example: retarget the C library printf function to the UART, q0 ?- [' S! D! q; I6 C! W7 e
UART Printf Example: retarget the C library printf function to the UART& r; T- C) z- y0 M1 l3 }
UART Printf Example: retarget the C library printf function to the UART
# i. w2 A. U7 vexpired 4000! c( @7 ~. g: G7 T( A1 g
UART Printf Example: retarget the C library printf function to the UART9 U! z8 t1 X% \& k9 K- D7 Z
UART Printf Example: retarget the C library printf function to the UART, O* _2 p1 Q; j/ s$ Z7 K8 t- f) }
expired 5000
* E! v2 T9 }8 y% K* h% c; |# b/ M5 XUART Printf Example: retarget the C library printf function to the UART
: ~+ Y. u$ m5 }& QUART Printf Example: retarget the C library printf function to the UART
1 V& Z2 a0 S7 p8 X1 zUART Printf Example: retarget the C library printf function to the UART
: d1 ~7 ^$ g1 c7 e) Pexpired 60002 N3 D# N( N" j% u$ R$ l
…
) L% l: P; G! E5 M7 L, a此时定时器回调函数有时能准确地被调度执行,有时则偏差较大,与期望值整整延迟了 1 秒钟。
% |7 U4 ]; i: N% a) X# i4 H7 F* X6 M; y# f+ U+ `
Case 3 :4 x( U/ I) N  d1 ]0 c! b
多任务,即系统里不仅有 timer task 和 idle task,还有用户创建的 task。整个系统最高优先级为 4,Timer task 的优先级为默认优先级 4。这样设置优先级,是希望能通过将 timer task 设置为最高优先级,以期望调度器能优先调度执行 timer task。
* l2 E5 e2 a; X6 @" o! G以下为实测的日志。
. A9 i$ u2 P  s9 `UART Printf Example: retarget the C library printf function to the UART. t8 a' x( x* N) ]
UART Printf Example: retarget the C library printf function to the UART3 a, k2 k* ]: r! M/ ^7 S
UART Printf Example: retarget the C library printf function to the UART
7 p$ V! J" {" C, m+ Nexpired 1000# R; s8 R% g0 G
UART Printf Example: retarget the C library printf function to the UART
/ |( b1 H4 O1 O( A4 ?% j- O) {$ x7 ZUART Printf Example: retarget the C library printf function to the UART
! j1 Y7 }* t" O- ]3 t" L8 hUART Printf Example: retarget the C library printf function to the UART
8 T) N6 R4 ~& T: zUART Printf Example: retarget the C library printf function to the UART& e7 W3 S0 s% o$ F% m2 b+ s
UART Printf Example: retarget the C library printf function to the UART# J4 o& z2 h0 D+ D! R8 S
expired 3000; C6 ^( k' f( B9 m7 S7 Y
UART Printf Example: retarget the C library printf function to the UART
7 M2 }) U9 H7 d) a. h+ OUART Printf Example: retarget the C library printf function to the UART
+ S( j8 V& c+ r' d. \: eUART Printf Example: retarget the C library printf function to the UART* _0 F4 \5 ^  D/ S" G9 k3 U
expired 4000  P; E( q; [; x5 j; B" g
UART Printf Example: retarget the C library printf function to the UART0 {' }0 K, J9 g5 C0 A
UART Printf Example: retarget the C library printf function to the UART
# D2 X1 L* O- Y" r- D* N" X5 J( gexpired 50007 n  q: v* L- p1 e
UART Printf Example: retarget the C library printf function to the UART
: R: R" O6 T% I0 E9 ~UART Printf Example: retarget the C library printf function to the UART
: J* |. P) ^+ v% J/ ~UART Printf Example: retarget the C library printf function to the UART1 B6 U8 F/ V; ?
expired 6000
, i3 b0 `% s( U2 d. `: m…7 q& P6 g& X$ @4 U8 v7 {, f
虽然调整了 timer task 的优先级至最高,但是依然会出现严重的偏差。) j1 S' {4 _' F

2 B  w3 z0 S3 L6 d

/ o, u- E/ o9 U* ~5 q解决方案. ^* ~1 F* X$ G  n& Q
考虑到 FreeRTOS 定时器的精准性不高,建议客户使用 SysTick 或者 MCU 的外设硬件定时器。1 c  E" l9 J+ p

+ l6 j! D  X) f* G: S! u# ^. `
赞 收藏 评论0 发布时间:2022-2-25 19:31

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版