1#include "stm32_timebase.hpp"
7enum class STM32TimebaseBackend : uint8_t
13STM32TimebaseBackend g_backend = STM32TimebaseBackend::SYSTICK;
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;
24 const auto time_diff = tick_new - tick_old;
25 const uint32_t tick_load = SysTick->LOAD + 1U;
30 static_cast<uint64_t
>(cnt_old) * 1000ULL / tick_load);
33 static_cast<uint64_t
>(cnt_new) * 1000ULL / tick_load);
40#ifdef HAL_TIM_MODULE_ENABLED
43 ASSERT(htim !=
nullptr);
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);
52 const uint32_t autoreload = __HAL_TIM_GET_AUTORELOAD(htim) + 1U;
53 const uint32_t delta_ms = tick_new - tick_old;
58 static_cast<uint64_t
>(cnt_old) * 1000ULL /
62 static_cast<uint64_t
>(cnt_new) * 1000ULL /
76 g_backend = STM32TimebaseBackend::SYSTICK;
84 case STM32TimebaseBackend::SYSTICK:
85 return GetSysTickMicroseconds();
86#ifdef HAL_TIM_MODULE_ENABLED
87 case STM32TimebaseBackend::TIMER:
98#ifdef HAL_TIM_MODULE_ENABLED
106 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.