1#include "stm32_timebase.hpp"
7enum class STM32TimebaseBackend : uint8_t
13STM32TimebaseBackend g_backend = STM32TimebaseBackend::SYSTICK;
19 const uint32_t tick_before = HAL_GetTick();
20 const uint32_t counter_before = SysTick->VAL;
23 const bool pending = (SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) != 0U;
24 const uint32_t counter_after = SysTick->VAL;
25 const uint32_t tick_after = HAL_GetTick();
26 if (tick_before != tick_after)
31 const bool crossed_zero =
32 counter_before != 0U && (counter_after == 0U || counter_after > counter_before);
33 uint32_t logical_tick = tick_after +
static_cast<uint32_t
>(pending || crossed_zero);
34 const uint64_t period_counts =
static_cast<uint64_t
>(SysTick->LOAD) + 1U;
35 uint64_t fraction = 0U;
36 if (counter_after != 0U)
38 fraction = 1000U -
static_cast<uint64_t
>(counter_after) * 1000U / period_counts;
39 if (fraction == 1000U)
49#ifdef HAL_TIM_MODULE_ENABLED
52 DEV_ASSERT(htim !=
nullptr);
56 const uint32_t tick_before = HAL_GetTick();
57 const uint32_t counter_before = __HAL_TIM_GET_COUNTER(htim);
58 const bool pending = __HAL_TIM_GET_FLAG(htim, TIM_FLAG_UPDATE) != 0U;
59 const uint32_t counter_after = __HAL_TIM_GET_COUNTER(htim);
60 const uint32_t tick_after = HAL_GetTick();
61 if (tick_before != tick_after)
66 const bool period_elapsed = pending || counter_after < counter_before;
67 const uint32_t logical_tick = tick_after +
static_cast<uint32_t
>(period_elapsed);
68 const uint64_t period_counts =
69 static_cast<uint64_t
>(__HAL_TIM_GET_AUTORELOAD(htim)) + 1U;
70 const uint64_t fraction =
71 static_cast<uint64_t
>(counter_after) * 1000U / period_counts;
82 g_backend = STM32TimebaseBackend::SYSTICK;
90 case STM32TimebaseBackend::SYSTICK:
91 return GetSysTickMicroseconds();
92#ifdef HAL_TIM_MODULE_ENABLED
93 case STM32TimebaseBackend::TIMER:
104#ifdef HAL_TIM_MODULE_ENABLED
110 ASSERT(timer !=
nullptr);
114 g_backend = STM32TimebaseBackend::TIMER;
微秒时间戳 / 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.
static void SetReady(bool ready=true) noexcept
设置时间基就绪状态。 Set the timebase ready flag.
static MicrosecondTimestamp GetMicroseconds()
获取当前时间的微秒级时间戳。 Gets the current timestamp in microseconds.
static MillisecondTimestamp GetMilliseconds()
获取当前时间的毫秒级时间戳。 Gets the current timestamp in milliseconds.
LibXR 命名空间 / LibXR namespace.