libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
ch32_timebase.cpp
1#include "ch32_timebase.hpp"
2
3using namespace LibXR;
4
5CH32Timebase::CH32Timebase() : Timebase(UINT32_MAX * 1000 + 999, UINT32_MAX) {}
6
8{
9 uint32_t tick_old = sys_tick_ms;
10 uint32_t cnt_old = SysTick->CNT;
11 uint32_t tick_new = sys_tick_ms;
12 uint32_t cnt_new = SysTick->CNT;
13
14 auto tick_diff = tick_new - tick_old;
15 uint32_t tick_cmp = SysTick->CMP + 1;
16 switch (tick_diff)
17 {
18 case 0:
19 return MicrosecondTimestamp(static_cast<uint64_t>(tick_new) * 1000 +
20 static_cast<uint64_t>(cnt_old) * 1000 / tick_cmp);
21 case 1:
22 /* 中断发生在两次读取之间 / Interrupt happened between two reads */
23 return MicrosecondTimestamp(static_cast<uint64_t>(tick_new) * 1000 +
24 static_cast<uint64_t>(cnt_new) * 1000 / tick_cmp);
25 default:
26 /* 中断耗时过长(超过1ms),程序异常 / Indicates that interrupt took more than
27 * 1ms, an error case */
28 ASSERT(false);
29 }
30
31 return 0;
32}
33
35
36void CH32Timebase::OnSysTickInterrupt() { sys_tick_ms++; }
37
38void CH32Timebase::Sync(uint32_t ticks) { sys_tick_ms = ticks; }
39
40extern "C" void libxr_systick_handler(void) { CH32Timebase::OnSysTickInterrupt(); }
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_gpio.hpp:9