libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
ch32_timebase.cpp
1// NOLINTBEGIN(cppcoreguidelines-pro-type-cstyle-cast,performance-no-int-to-ptr)
2#include "ch32_timebase.hpp"
3
4using namespace LibXR;
5
6CH32Timebase::CH32Timebase() : Timebase(UINT32_MAX * 1000 + 999, UINT32_MAX) {}
7
9{
10 do
11 {
12 uint32_t tick_old = sys_tick_ms_;
13 uint32_t cnt_old = SysTick->CNT;
14 uint32_t tick_new = sys_tick_ms_;
15 uint32_t cnt_new = SysTick->CNT;
16
17 auto tick_diff = tick_new - tick_old;
18 uint32_t tick_cmp = SysTick->CMP + 1;
19 switch (tick_diff)
20 {
21 case 0:
22 return MicrosecondTimestamp(static_cast<uint64_t>(tick_new) * 1000 +
23 static_cast<uint64_t>(cnt_old) * 1000 / tick_cmp);
24 case 1:
25 /* 中断发生在两次读取之间 / Interrupt happened between two reads */
26 return MicrosecondTimestamp(static_cast<uint64_t>(tick_new) * 1000 +
27 static_cast<uint64_t>(cnt_new) * 1000 / tick_cmp);
28 default:
29 /* 中断耗时过长(超过1ms),程序异常 / Indicates that interrupt took more than
30 * 1ms, an error case */
31 continue;
32 }
33 } while (true);
34}
35
37
38void CH32Timebase::OnSysTickInterrupt() { sys_tick_ms_++; }
39
40void CH32Timebase::Sync(uint32_t ticks) { sys_tick_ms_ = ticks; }
41
42extern "C" void libxr_systick_handler(void) { CH32Timebase::OnSysTickInterrupt(); }
43
44// NOLINTEND(cppcoreguidelines-pro-type-cstyle-cast,performance-no-int-to-ptr)
MicrosecondTimestamp _get_microseconds() override
纯虚函数,获取当前时间的微秒级时间戳(由派生类实现)。 Pure virtual function for obtaining the current timestamp in microseconds...
MillisecondTimestamp _get_milliseconds() override
纯虚函数,获取当前时间的毫秒级时间戳(由派生类实现)。 Pure virtual function for obtaining the current timestamp in milliseconds...
表示微秒级时间戳的类。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