STMCU小助手
发布时间:2022-2-25 19:31
|
背景介绍 FreeRTOS 以其移植方便,高度可定制,footprint 小,使其在嵌入式操作系统中的份额不容小觑!尤以免费 license,颇受开发者青睐。 5 q/ a2 t- r3 A0 Q% k* W8 `: V) z 问题描述2 Y' \. }: J' K" ]4 `1 K 客户在基于 cortex-m3 的平台上使用 FreeRTOS 系统提供的定时器功能时,意外发现定时器的精确度不够高。譬如,设置 1 秒钟的定时器,理论上 1 秒超时,并且执行相应的回调函数。但是调试却发现,有时回调函数是在 1.4 秒后被执行!这对于精度要求较高的实时系统,是不能接受的!, F. R6 t9 D6 H& g: j& C/ L& E& x $ E$ k- `1 G- w# T8 W7 o: M 问题复现与分析! \* X: G3 M2 ~6 a5 p/ N 首先在 stm32f407-discovery 平台移植 FreeRTOS,并创建一个定时器,在其回调函数里 toggleled 灯,并测量被执行的时间。鉴于 FreeRTOS 是一个多任务可抢占式系统,这个问题需要在多种情况下分析。! n2 E1 v, ~2 H9 m# [! D1 A* w Case 1 : 单任务,即系统里仅有 timer task 和 idle task。整个系统最高优先级为 4,Timer task 的优先级为默认优先级 2。, w$ _3 f Q( L- h5 Y9 L% C0 O 在这种环境下,回调函数能精确的以 1 秒的时长超时执行回调函数。虽然此刻精度能满足要求,但是实际的系统一般会包含多个 task。 以下为测试的日志,显而易见,该定时器严格 1 秒钟超时。 expired 1000 expired 2000 expired 30009 ^* K4 A4 a2 x* M4 F expired 4000# [) r1 b. h7 m: k- M expired 5000! ^( E r$ `6 S" R) W expired 6000 … 5 ^+ t1 C* u: m- C Case 2 :$ [, G7 T8 G9 O8 f 多任务,即系统里不仅有 timer task 和 idle task,还有用户创建的 task。整个系统最高优先级为 4,Timer task 的优先级为默认优先级 2。1 Z6 u( t- d/ o 以下为测试的日志。 UART Printf Example: retarget the C library printf function to the UART) q4 G% [) \* x UART Printf Example: retarget the C library printf function to the UART' \% N: D1 n2 b& X5 V UART Printf Example: retarget the C library printf function to the UART expired 10007 h9 k6 _+ A4 c r3 D' p% T UART Printf Example: retarget the C library printf function to the UART) _5 k# M, A( a5 c# F7 { 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# V( V) O$ q+ I# R( q expired 3000' L k+ o. }- U UART Printf Example: retarget the C library printf function to the UART: J3 A( [5 b1 P9 J 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+ `7 C# q4 j2 \5 j 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 UART3 O( z6 H X7 _" {* g 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& z* m+ J( d8 C) v …+ D3 u0 v- j: J& V. g 此时定时器回调函数有时能准确地被调度执行,有时则偏差较大,与期望值整整延迟了 1 秒钟。 7 b; Z) j$ m$ ~* S( ~ Case 3 :+ A5 o% B5 y6 @2 C 多任务,即系统里不仅有 timer task 和 idle task,还有用户创建的 task。整个系统最高优先级为 4,Timer task 的优先级为默认优先级 4。这样设置优先级,是希望能通过将 timer task 设置为最高优先级,以期望调度器能优先调度执行 timer task。# S* D( n% D0 D0 j: k) N 以下为实测的日志。 UART Printf Example: retarget the C library printf function to the UART9 p6 ?% W5 P6 e( I' Q3 ` q/ g* _) ? UART Printf Example: retarget the C library printf function to the UART" E- V; u( \4 {+ U1 n! M2 c UART Printf Example: retarget the C library printf function to the UART expired 1000' }+ _% s# v5 [1 v. D* e UART Printf Example: retarget the C library printf function to the UART+ L5 ^& E; q5 q UART Printf Example: retarget the C library printf function to the UART UART Printf Example: retarget the C library printf function to the UART5 V* v6 |5 g- o UART Printf Example: retarget the C library printf function to the UART' |9 }4 k/ a/ H) _ UART Printf Example: retarget the C library printf function to the UART9 A$ |& m' q% ?0 `; f expired 3000, l1 d c9 m1 O' ~4 c 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/ B9 {+ ]( ]% [$ ?0 a 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' f& ^- T. j& P2 Q2 e( b UART Printf Example: retarget the C library printf function to the UART5 S8 h% V( A1 _1 h6 z4 { UART Printf Example: retarget the C library printf function to the UART# {3 N& ]' T1 g& t, I expired 6000 … 虽然调整了 timer task 的优先级至最高,但是依然会出现严重的偏差。" M$ W$ x& D: r/ A P! @ I + ` f$ i% S& b2 p J 解决方案, R) G' n8 J$ x+ \, v 考虑到 FreeRTOS 定时器的精准性不高,建议客户使用 SysTick 或者 MCU 的外设硬件定时器。+ l$ ?; L, q! b3 z " t* T& ~( @+ T4 }* |5 i% v. f- o h& v |
【经验分享】STM32语音识别的智能蓝牙音箱(源程序+原理图+PCB+设计说明书+PPT)
想找个ST芯片
[GPIO] 使用新型抽象开始C++嵌入式编程,从GPIO开始
技术手册stm32f1xx中的一个小问题
【经验分享】STM32 HAL库移植FreeModbus详细步骤
【逢7发帖赢大礼】STM32开发之IC门禁卡UID读取方法!
实战经验 | Keil工程使用NEAI库的异常问题
经验分享 | FDCAN数据段波特率增加后发送失败的问题分析
STM32几种流行开发环境的优缺点
好用软件推荐-TouchGFX Designer
微信公众号
手机版