问题复现与分析
首先在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的优先级至最高,但是依然会出现严重的偏差。