Timer¶
FAQ1 常见延时函数¶
常用的延时函数分为 HAL 和 RT 两种。
HAL 层延时函数:(等同于
while中指令循环,延时时不会切换到其他线程)HAL_Delay(10); /* 延时10ms */ HAL_Delay_us(10); /* 延时10us */
RTT 接口的延时函数:
rt_thread_delay(100); /* 延时100ms */
注意: RTT 接口的延时函数执行时,会切换到其他线程,比如 idle 线程。当睡眠门限低于延时时长时,会进入 Standby 睡眠。
FAQ2 获取时间戳、Tick 值和 RC10K 振荡频率¶
获取时间戳和 Tick 值是比较常用的操作。
获取时间戳:
/* 32768 晶体时钟下,约每 1/32768 秒寄存器值加 1 */ /* RC10K 时钟下,约每 1/9000 秒寄存器值加 1 */ uint32_t start_time = HAL_GTIMER_READ();
获取每 1ms 递增的 Tick 值:
rt_tick_t start_timer = rt_tick_get(); /* RTT 系统函数,每 1ms 返回值会加 1 */ uint32_t tickstart = HAL_GetTick(); /* HAL 层函数,每 1ms 返回值会加 1 */
获取当前时钟频率:
/* 32768 晶体时钟下,会返回 32768 */ /* RC10K 时钟下,会返回 8k-10k 之间的值 */ uint32_t mcuOscData = HAL_LPTIM_GetFreq();
FAQ3 查看现有定时器命令¶
串口发送
list_timer命令,可以查看当前使用的定时器信息。
log 信息:
展开细节
04-10 16:38:32:024 TX:list_timer
04-10 16:38:32:026 list_timer
04-10 16:38:32:033 timer periodic timeout flag
04-10 16:38:32:034 -------------------- ---------- ---------- -----------
04-10 16:38:32:038 temp_thread 0x00000000 0x00000000 deactivated
04-10 16:38:32:039 sensor_timer 0x000000c8 0x00002171 activated
04-10 16:38:32:040 audio_box_scan_timer 0x000003e8 0x00002427 activated
04-10 16:38:32:041 battery_timer 0x0000ea60 0x0000fab5 activated
04-10 16:38:32:042 battery_charger_task 0x00000001 0x00001091 deactivated
04-10 16:38:32:043 ble_connect 0x00007530 0x00000000 deactivated
04-10 16:38:32:043 bts 0x000001e8 0x00000f6c deactivated
04-10 16:38:32:044 BLEHost 0x000dbba0 0x000dc923 activated
04-10 16:38:32:045 KE_EVT3 0x00000000 0x00000000 deactivated
04-10 16:38:32:048 KE_EVT0 0x00000000 0x00000000 deactivated
04-10 16:38:32:049 KE_EVT2 0x00000000 0x00000000 deactivated
04-10 16:38:32:050 wheel 0x00000001 0x00000000 deactivated
04-10 16:38:32:050 tpread 0x00000000 0x00000000 deactivated
04-10 16:38:32:051 lcd_task 0x000001f4 0x00002332 deactivated
04-10 16:38:32:052 tshell 0x00000000 0x00000000 deactivated
04-10 16:38:32:053 lcpu_thread 0x0000000a 0x000010ff deactivated
04-10 16:38:32:055 ble_msg 0x00000000 0x00000000 deactivated
04-10 16:38:32:056 bg_thread 0x00000000 0x00000000 deactivated
04-10 16:38:32:057 gui_thread 0x00000010 0x0000214c deactivated
04-10 16:38:32:058 motor 0x000000c8 0x00000000 deactivated
04-10 16:38:32:059 mc 0x0036ee80 0x0036ef0e activated
04-10 16:38:32:061 bt_check 0x00003e80 0x00000000 deactivated
04-10 16:38:32:061 bt_connection 0x00000bb8 0x00000000 deactivated
04-10 16:38:32:063 rtc 0x00003a98 0x00003b1d activated
04-10 16:38:32:064 btn_tm2 0x00000bb8 0x00000000 deactivated
04-10 16:38:32:065 btn 0x00000001 0x00000000 deactivated
04-10 16:38:32:066 epic_task 0x00001388 0x000034c5 deactivated
04-10 16:38:32:067 metrics 0x00000000 0x00000000 deactivated
04-10 16:38:32:068 deplayback_th 0x00000000 0x00000000 deactivated
04-10 16:38:32:069 iwdt_sleep 0x00046cd0 0x00046d4f deactivated
04-10 16:38:32:071 bt_absolute 0x0000012c 0x00000000 deactivated
04-10 16:38:32:073 bt_service 0x00000000 0x00000000 deactivated
04-10 16:38:32:073 bt_downvoice 0x00000000 0x00000000 deactivated
04-10 16:38:32:074 audiosvr 0x0000000a 0x0000133b deactivated
04-10 16:38:32:075 bt_wq 0x00000000 0x00000000 deactivated
04-10 16:38:32:076 alarmsvc 0x00000000 0x00000000 deactivated
04-10 16:38:32:076 tidle 0x00000000 0x00000000 deactivated
04-10 16:38:32:077 timer 0x000000c8 0x00002171 activated
04-10 16:38:32:079 current tick:0x00002164
list_timer 状态说明¶
列名 |
含义 |
|---|---|
timer |
定时器名字 |
periodic |
定时器周期(十六进制,单位:ms) |
timeout |
下一次定时器触发的时间戳 |
flag |
定时器是否为激活状态 |
示例说明¶
从输出结果可以看到,当前系统中处于 activated(激活)状态的定时器包括:
定时器名称 |
十六进制周期 |
十进制周期 |
|---|---|---|
ft3169_check |
0x00000064 |
100ms |
sensor_timer |
0x000000c8 |
200ms |
battery_timer |
0x0000ea60 |
60000ms(60s) |
BLEHost |
0x000dbba0 |
900000ms(15min) |
lcd_task |
0x000001f4 |
500ms |
mc |
0x0036ee80 |
3600000ms(1h) |
rtc |
0x00003a98 |
15000ms(15s) |
timer |
0x00000064 |
100ms |
命令末尾的 current tick:0x09e8fba8 为系统当前的时间戳,用于计算定时器剩余触发时间。