libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
stm32_timebase.cpp
1#include "stm32_timebase.hpp"
2
3using namespace LibXR;
4
6 : Timebase(static_cast<uint64_t>(UINT32_MAX) * 1000 + 999, UINT32_MAX)
7{
8}
9
11{
12 uint32_t ms_old = HAL_GetTick();
13 uint32_t tick_value_old = SysTick->VAL;
14 uint32_t ms_new = HAL_GetTick();
15 uint32_t tick_value_new = SysTick->VAL;
16
17 auto time_diff = ms_new - ms_old;
18 uint32_t tick_load = SysTick->LOAD + 1;
19 switch (time_diff)
20 {
21 case 0:
22 return MicrosecondTimestamp(static_cast<uint64_t>(ms_new) * 1000 + 1000 -
23 static_cast<uint64_t>(tick_value_old) * 1000 /
24 tick_load);
25 case 1:
26 /* 中断发生在两次读取之间 / Interrupt happened between two reads */
27 return MicrosecondTimestamp(static_cast<uint64_t>(ms_new) * 1000 + 1000 -
28 static_cast<uint64_t>(tick_value_new) * 1000 /
29 tick_load);
30 default:
31 /* 中断耗时过长(超过1ms),程序异常 / Indicates that interrupt took more than
32 * 1ms, an error case */
33 ASSERT(false);
34 }
35
36 return 0;
37}
38
40
41#ifdef HAL_TIM_MODULE_ENABLED
42
43TIM_HandleTypeDef* STM32TimerTimebase::htim = nullptr;
44
46 : Timebase(static_cast<uint64_t>(UINT32_MAX) * 1000 + 999, UINT32_MAX)
47{
48 htim = timer;
49}
50
52{
53 uint32_t ms_old = HAL_GetTick();
54 uint32_t tick_value_old = __HAL_TIM_GET_COUNTER(htim);
55 uint32_t ms_new = HAL_GetTick();
56 uint32_t tick_value_new = __HAL_TIM_GET_COUNTER(htim);
57
58 uint32_t autoreload = __HAL_TIM_GET_AUTORELOAD(htim) + 1;
59
60 uint32_t delta_ms = ms_new - ms_old;
61 switch (delta_ms)
62 {
63 case 0:
64 return MicrosecondTimestamp(static_cast<uint64_t>(ms_new) * 1000 +
65 static_cast<uint64_t>(tick_value_old) * 1000 /
66 autoreload);
67 case 1:
68 /* 中断发生在两次读取之间 / Interrupt happened between two reads */
69 return MicrosecondTimestamp(static_cast<uint64_t>(ms_new) * 1000 +
70 static_cast<uint64_t>(tick_value_new) * 1000 /
71 autoreload);
72 default:
73 /* 中断耗时过长(超过1ms),程序异常 / Indicates that interrupt took more than
74 * 1ms, an error case */
75 ASSERT(false);
76 }
77
78 return 0;
79}
80
82
83#endif
表示微秒级时间戳的类。Class representing a timestamp in microseconds.
表示毫秒级时间戳的类。Class representing a timestamp in milliseconds.
MillisecondTimestamp _get_milliseconds()
获取当前时间(毫秒级) / Get current time in milliseconds
STM32Timebase()
默认构造函数 / Default constructor
MicrosecondTimestamp _get_microseconds()
获取当前时间(微秒级) / Get current time in microseconds
STM32TimerTimebase(TIM_HandleTypeDef *timer)
构造函数 / Constructor
MillisecondTimestamp _get_milliseconds()
获取当前时间(毫秒级) / Get current time in milliseconds
static TIM_HandleTypeDef * htim
硬件定时器句柄指针 / Static pointer to hardware timer handle
MicrosecondTimestamp _get_microseconds()
获取当前时间(微秒级) / Get current time in microseconds
时间基类,用于提供高精度时间戳。 Timebase class for providing high-precision timestamps.
Definition timebase.hpp:18
LibXR 命名空间
Definition ch32_gpio.hpp:9