libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Timebase Class Reference

时间基类,用于提供高精度时间戳。 Timebase class for providing high-precision timestamps. More...

#include <timebase.hpp>

Inheritance diagram for LibXR::Timebase:
[legend]

Public Member Functions

 Timebase ()=default
 默认构造函数。 Default constructor.
 
 Timebase (const Timebase &)=delete
 禁止拷贝构造。 Copy construction is disabled.
 
Timebaseoperator= (const Timebase &)=delete
 禁止拷贝赋值。 Copy assignment is disabled.
 

Static Public Member Functions

static MicrosecondTimestamp GetMicroseconds ()
 获取当前时间的微秒级时间戳。 Gets the current timestamp in microseconds.
 
static MillisecondTimestamp GetMilliseconds ()
 获取当前时间的毫秒级时间戳。 Gets the current timestamp in milliseconds.
 
static bool IsReady () noexcept
 检查时间基是否已经初始化。 Check whether the active timebase backend is initialized.
 
static void DelayMicroseconds (uint32_t us)
 微秒级延时 / Delay in microseconds
 

Static Protected Member Functions

static void SetReady (bool ready=true) noexcept
 设置时间基就绪状态。 Set the timebase ready flag.
 
static void ConfigureWrapRange (uint64_t max_valid_us, uint32_t max_valid_ms) noexcept
 配置时间戳回绕上界。 Configure the timestamp wraparound limits.
 
static uint64_t GetConfiguredWrapRangeUs () noexcept
 读取当前配置的微秒回绕上界。 Read the configured microsecond wraparound limit.
 
static uint32_t GetConfiguredWrapRangeMs () noexcept
 读取当前配置的毫秒回绕上界。 Read the configured millisecond wraparound limit.
 

Static Private Attributes

static bool ready_ = false
 时间基是否已完成初始化。 Whether the timebase backend has been initialized.
 

Detailed Description

时间基类,用于提供高精度时间戳。 Timebase class for providing high-precision timestamps.

该类提供了微秒和毫秒级的时间戳获取接口。 This class provides interfaces for obtaining timestamps in microseconds and milliseconds.

Definition at line 16 of file timebase.hpp.

Member Function Documentation

◆ ConfigureWrapRange()

static void LibXR::Timebase::ConfigureWrapRange ( uint64_t max_valid_us,
uint32_t max_valid_ms )
inlinestaticprotectednoexcept

配置时间戳回绕上界。 Configure the timestamp wraparound limits.

Parameters
max_valid_us微秒时间戳的有效上界。 Maximum valid microsecond timestamp value.
max_valid_ms毫秒时间戳的有效上界。 Maximum valid millisecond timestamp value.

Definition at line 95 of file timebase.hpp.

96 {
97 Detail::ConfigureTimebaseWrapRange(max_valid_us, max_valid_ms);
98 }

◆ DelayMicroseconds()

static void LibXR::Timebase::DelayMicroseconds ( uint32_t us)
inlinestatic

微秒级延时 / Delay in microseconds

Parameters
us延时长度(us)/ Delay length (us)

Definition at line 65 of file timebase.hpp.

66 {
67 if (us == 0u)
68 {
69 return;
70 }
71
72 const uint64_t START = static_cast<uint64_t>(Timebase::GetMicroseconds());
73 while ((static_cast<uint64_t>(Timebase::GetMicroseconds()) - START) < us)
74 {
75 // busy-wait
76 }
77 }
static MicrosecondTimestamp GetMicroseconds()
获取当前时间的微秒级时间戳。 Gets the current timestamp in microseconds.

◆ GetConfiguredWrapRangeMs()

static uint32_t LibXR::Timebase::GetConfiguredWrapRangeMs ( )
inlinestaticnodiscardprotectednoexcept

读取当前配置的毫秒回绕上界。 Read the configured millisecond wraparound limit.

Definition at line 113 of file timebase.hpp.

114 {
115 return Detail::TimebaseMaxValidMs();
116 }

◆ GetConfiguredWrapRangeUs()

static uint64_t LibXR::Timebase::GetConfiguredWrapRangeUs ( )
inlinestaticnodiscardprotectednoexcept

读取当前配置的微秒回绕上界。 Read the configured microsecond wraparound limit.

Definition at line 104 of file timebase.hpp.

105 {
106 return Detail::TimebaseMaxValidUs();
107 }

◆ GetMicroseconds()

MicrosecondTimestamp LibXR::Timebase::GetMicroseconds ( )
static

获取当前时间的微秒级时间戳。 Gets the current timestamp in microseconds.

Returns
返回当前的时间戳(单位:微秒)。 Returns the current timestamp (in microseconds).

Definition at line 12 of file ch32_timebase.cpp.

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}
static volatile uint32_t sys_tick_ms_
SysTick 毫秒计数器。 SysTick millisecond counter.
微秒时间戳 / Microsecond timestamp

◆ GetMilliseconds()

MillisecondTimestamp LibXR::Timebase::GetMilliseconds ( )
static

获取当前时间的毫秒级时间戳。 Gets the current timestamp in milliseconds.

Returns
返回当前的时间戳(单位:毫秒)。 Returns the current timestamp (in milliseconds).

Definition at line 40 of file ch32_timebase.cpp.

◆ IsReady()

static bool LibXR::Timebase::IsReady ( )
inlinestaticnodiscardnoexcept

检查时间基是否已经初始化。 Check whether the active timebase backend is initialized.

Definition at line 59 of file timebase.hpp.

59{ return ready_; }
static bool ready_
时间基是否已完成初始化。 Whether the timebase backend has been initialized.
Definition timebase.hpp:123

◆ SetReady()

static void LibXR::Timebase::SetReady ( bool ready = true)
inlinestaticprotectednoexcept

设置时间基就绪状态。 Set the timebase ready flag.

Parameters
ready是否就绪。Whether the backend is ready.

Definition at line 85 of file timebase.hpp.

85{ ready_ = ready; }

Field Documentation

◆ ready_

bool LibXR::Timebase::ready_ = false
inlinestaticprivate

时间基是否已完成初始化。 Whether the timebase backend has been initialized.

Definition at line 123 of file timebase.hpp.


The documentation for this class was generated from the following files: