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
5namespace
6{
7enum class STM32TimebaseBackend : uint8_t
8{
9 SYSTICK = 0,
10 TIMER = 1,
11};
12
13STM32TimebaseBackend g_backend = STM32TimebaseBackend::SYSTICK;
14
15MicrosecondTimestamp GetSysTickMicroseconds()
16{
17 do
18 {
19 uint32_t tick_old = HAL_GetTick();
20 uint32_t cnt_old = SysTick->VAL;
21 uint32_t tick_new = HAL_GetTick();
22 uint32_t cnt_new = SysTick->VAL;
23
24 const auto time_diff = tick_new - tick_old;
25 const uint32_t tick_load = SysTick->LOAD + 1U;
26 switch (time_diff)
27 {
28 case 0:
29 return MicrosecondTimestamp(static_cast<uint64_t>(tick_new) * 1000ULL + 1000ULL -
30 static_cast<uint64_t>(cnt_old) * 1000ULL / tick_load);
31 case 1:
32 return MicrosecondTimestamp(static_cast<uint64_t>(tick_new) * 1000ULL + 1000ULL -
33 static_cast<uint64_t>(cnt_new) * 1000ULL / tick_load);
34 default:
35 continue;
36 }
37 } while (true);
38}
39
40#ifdef HAL_TIM_MODULE_ENABLED
41MicrosecondTimestamp GetTimerMicroseconds(TIM_HandleTypeDef* htim)
42{
43 ASSERT(htim != nullptr);
44
45 do
46 {
47 uint32_t tick_old = HAL_GetTick();
48 uint32_t cnt_old = __HAL_TIM_GET_COUNTER(htim);
49 uint32_t tick_new = HAL_GetTick();
50 uint32_t cnt_new = __HAL_TIM_GET_COUNTER(htim);
51
52 const uint32_t autoreload = __HAL_TIM_GET_AUTORELOAD(htim) + 1U;
53 const uint32_t delta_ms = tick_new - tick_old;
54 switch (delta_ms)
55 {
56 case 0:
57 return MicrosecondTimestamp(static_cast<uint64_t>(tick_new) * 1000ULL +
58 static_cast<uint64_t>(cnt_old) * 1000ULL /
59 autoreload);
60 case 1:
61 return MicrosecondTimestamp(static_cast<uint64_t>(tick_new) * 1000ULL +
62 static_cast<uint64_t>(cnt_new) * 1000ULL /
63 autoreload);
64 default:
65 continue;
66 }
67 } while (true);
68}
69
70#endif
71} // namespace
72
74{
75 ConfigureWrapRange(static_cast<uint64_t>(UINT32_MAX) * 1000ULL + 999ULL, UINT32_MAX);
76 g_backend = STM32TimebaseBackend::SYSTICK;
77 SetReady();
78}
79
81{
82 switch (g_backend)
83 {
84 case STM32TimebaseBackend::SYSTICK:
85 return GetSysTickMicroseconds();
86#ifdef HAL_TIM_MODULE_ENABLED
87 case STM32TimebaseBackend::TIMER:
88 return GetTimerMicroseconds(STM32TimerTimebase::htim);
89#endif
90 }
91
92 ASSERT(false);
93 return MicrosecondTimestamp(0ULL);
94}
95
96MillisecondTimestamp Timebase::GetMilliseconds() { return HAL_GetTick(); }
97
98#ifdef HAL_TIM_MODULE_ENABLED
99
100TIM_HandleTypeDef* STM32TimerTimebase::htim = nullptr;
101
103{
104 htim = timer;
105 ConfigureWrapRange(static_cast<uint64_t>(UINT32_MAX) * 1000ULL + 999ULL, UINT32_MAX);
106 g_backend = STM32TimebaseBackend::TIMER;
107 SetReady();
108}
109
110#endif
微秒时间戳 / Microsecond timestamp
毫秒时间戳 / Millisecond timestamp
STM32Timebase()
默认构造函数 / Default constructor
STM32TimerTimebase(TIM_HandleTypeDef *timer)
构造函数 / Constructor
static TIM_HandleTypeDef * htim
硬件定时器句柄静态指针 / Static pointer to timer handle
static void ConfigureWrapRange(uint64_t max_valid_us, uint32_t max_valid_ms) noexcept
配置时间戳回绕上界。 Configure the timestamp wraparound limits.
Definition timebase.hpp:95
static void SetReady(bool ready=true) noexcept
设置时间基就绪状态。 Set the timebase ready flag.
Definition timebase.hpp:85
static MicrosecondTimestamp GetMicroseconds()
获取当前时间的微秒级时间戳。 Gets the current timestamp in microseconds.
static MillisecondTimestamp GetMilliseconds()
获取当前时间的毫秒级时间戳。 Gets the current timestamp in milliseconds.
LibXR 命名空间
Definition ch32_can.hpp:14