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{
7static inline uint64_t ConvertTicksToTime(uint64_t ticks, uint32_t freq_hz,
8 uint64_t scale)
9{
10 if (freq_hz == 0u) return 0u;
11
12 const uint64_t whole = ticks / freq_hz;
13 const uint64_t rem = ticks - (whole * freq_hz);
14 const uint64_t frac = (rem * scale) / freq_hz;
15 return whole * scale + frac;
16}
17} // namespace
18
19HPMTimebase::HPMTimebase(MCHTMR_Type* timer, clock_name_t clock)
20 : Timebase(static_cast<uint64_t>(UINT32_MAX) * 1000 + 999, UINT32_MAX),
21 timer_(timer),
22 clock_hz_(clock_get_frequency(clock))
23{
24}
25
27{
28 const uint64_t ticks = mchtmr_get_count(timer_);
29 return MicrosecondTimestamp(ConvertTicksToTime(ticks, clock_hz_, 1000000ULL));
30}
31
33{
34 const uint64_t ticks = mchtmr_get_count(timer_);
35 return MillisecondTimestamp(ConvertTicksToTime(ticks, clock_hz_, 1000ULL));
36}
MCHTMR_Type * timer_
定时器外设实例 / Timer peripheral instance.
MillisecondTimestamp _get_milliseconds() override
获取毫秒时间戳 / Get current timestamp in milliseconds.
HPMTimebase(MCHTMR_Type *timer=HPM_MCHTMR, clock_name_t clock=clock_mchtmr0)
构造 HPM 时间基对象 / Construct an HPM timebase object.
MicrosecondTimestamp _get_microseconds() override
获取微秒时间戳 / Get current timestamp in microseconds.
uint32_t clock_hz_
定时器输入时钟频率(Hz) / Timer input clock frequency in Hz.
表示微秒级时间戳的类。Class representing a timestamp in microseconds.
表示毫秒级时间戳的类。Class representing a timestamp in milliseconds.
时间基类,用于提供高精度时间戳。 Timebase class for providing high-precision timestamps.
Definition timebase.hpp:18
LibXR 命名空间
Definition ch32_can.hpp:14