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
7{
8 ConfigureWrapRange(static_cast<uint64_t>(UINT32_MAX) * 1000ULL + 999ULL, UINT32_MAX);
9 SetReady();
10}
11
13{
14 do
15 {
16 uint32_t tick_old = CH32Timebase::sys_tick_ms_;
17 uint32_t cnt_old = SysTick->CNT;
18 uint32_t tick_new = CH32Timebase::sys_tick_ms_;
19 uint32_t cnt_new = SysTick->CNT;
20
21 auto tick_diff = tick_new - tick_old;
22 uint32_t tick_cmp = SysTick->CMP + 1;
23 switch (tick_diff)
24 {
25 case 0:
26 return MicrosecondTimestamp(static_cast<uint64_t>(tick_new) * 1000 +
27 static_cast<uint64_t>(cnt_old) * 1000 / tick_cmp);
28 case 1:
29 /* 中断发生在两次读取之间 / Interrupt happened between two reads */
30 return MicrosecondTimestamp(static_cast<uint64_t>(tick_new) * 1000 +
31 static_cast<uint64_t>(cnt_new) * 1000 / tick_cmp);
32 default:
33 /* 中断耗时过长(超过1ms),程序异常 / Indicates that interrupt took more
34 * than 1ms, an error case */
35 continue;
36 }
37 } while (true);
38}
39
41
43
44void CH32Timebase::Sync(uint32_t ticks) { sys_tick_ms_ = ticks; }
45
46extern "C" void libxr_systick_handler(void) { CH32Timebase::OnSysTickInterrupt(); }
47
48// NOLINTEND(cppcoreguidelines-pro-type-cstyle-cast,performance-no-int-to-ptr)
void Sync(uint32_t ticks)
同步毫秒计数器。 Synchronize the millisecond counter.
static void OnSysTickInterrupt()
SysTick 中断入口辅助函数。 Helper called from the SysTick interrupt path.
static volatile uint32_t sys_tick_ms_
SysTick 毫秒计数器。 SysTick millisecond counter.
CH32Timebase()
构造函数 / Constructor
微秒时间戳 / 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