libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
hpm_timebase.cpp
1#include "hpm_timebase.hpp"
2
3using namespace LibXR;
4
5namespace
6{
7MCHTMR_Type* g_timer = HPM_MCHTMR;
8uint32_t g_clock_hz = 0u;
9
10static inline uint64_t ConvertTicksToTime(uint64_t ticks, uint32_t freq_hz,
11 uint64_t scale)
12{
13 if (freq_hz == 0u) return 0u;
14
15 const uint64_t whole = ticks / freq_hz;
16 const uint64_t rem = ticks - (whole * freq_hz);
17 const uint64_t frac = (rem * scale) / freq_hz;
18 return whole * scale + frac;
19}
20} // namespace
21
22HPMTimebase::HPMTimebase(MCHTMR_Type* timer, clock_name_t clock)
23{
24 g_timer = timer;
25 g_clock_hz = clock_get_frequency(clock);
26 ConfigureWrapRange(static_cast<uint64_t>(UINT32_MAX) * 1000ULL + 999ULL, UINT32_MAX);
27 SetReady();
28}
29
31{
32 const uint64_t ticks = mchtmr_get_count(g_timer);
33 return MicrosecondTimestamp(ConvertTicksToTime(ticks, g_clock_hz, 1000000ULL));
34}
35
37{
38 const uint64_t ticks = mchtmr_get_count(g_timer);
39 return MillisecondTimestamp(ConvertTicksToTime(ticks, g_clock_hz, 1000ULL));
40}
HPMTimebase(MCHTMR_Type *timer=HPM_MCHTMR, clock_name_t clock=clock_mchtmr0)
构造 HPM 时间基对象 / Construct an HPM timebase object.
微秒时间戳 / Microsecond timestamp
毫秒时间戳 / Millisecond timestamp
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