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

获取基于STM32 SysTick计数器的时间基准(微秒与毫秒) / Provides a timebase using STM32 SysTick timer (microseconds and milliseconds) More...

#include <stm32_timebase.hpp>

Inheritance diagram for LibXR::STM32Timebase:
Collaboration diagram for LibXR::STM32Timebase:

Public Member Functions

 STM32Timebase ()
 默认构造函数 / Default constructor
 
TimestampUS _get_microseconds ()
 获取当前时间(微秒级) / Get current time in microseconds
 
TimestampMS _get_milliseconds ()
 获取当前时间(毫秒级) / Get current time in milliseconds
 
- Public Member Functions inherited from LibXR::Timebase
 Timebase (uint64_t max_valid_us=UINT64_MAX, uint32_t max_valid_ms=UINT32_MAX)
 默认构造函数,初始化全局时间基指针。 Default constructor, initializing the global timebase pointer.
 

Additional Inherited Members

- Static Public Member Functions inherited from LibXR::Timebase
static TimestampUS GetMicroseconds ()
 获取当前时间的微秒级时间戳。 Gets the current timestamp in microseconds.
 
static TimestampMS GetMilliseconds ()
 获取当前时间的毫秒级时间戳。 Gets the current timestamp in milliseconds.
 
- Static Public Attributes inherited from LibXR::Timebase
static Timebasetimebase = nullptr
 静态指针,用于存储全局时间基对象。 Static pointer storing the global timebase instance.
 

Detailed Description

获取基于STM32 SysTick计数器的时间基准(微秒与毫秒) / Provides a timebase using STM32 SysTick timer (microseconds and milliseconds)

Definition at line 13 of file stm32_timebase.hpp.

Constructor & Destructor Documentation

◆ STM32Timebase()

LibXR::STM32Timebase::STM32Timebase ( )
inline

默认构造函数 / Default constructor

Definition at line 19 of file stm32_timebase.hpp.

19 : Timebase(static_cast<uint64_t>(UINT32_MAX) * 1000 + 999, UINT32_MAX)
20 {
21 }
Timebase(uint64_t max_valid_us=UINT64_MAX, uint32_t max_valid_ms=UINT32_MAX)
默认构造函数,初始化全局时间基指针。 Default constructor, initializing the global timebase pointer.
Definition timebase.hpp:30
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ _get_microseconds()

TimestampUS LibXR::STM32Timebase::_get_microseconds ( )
inlinevirtual

获取当前时间(微秒级) / Get current time in microseconds

该函数通过读取SysTick计数器的值和系统滴答定时器的毫秒数,计算当前的微秒时间。 It calculates current microsecond timestamp based on SysTick counter and system tick value.

Returns
TimestampUS 当前时间(微秒) / Current timestamp in microseconds

Implements LibXR::Timebase.

Definition at line 32 of file stm32_timebase.hpp.

33 {
38
39 auto time_diff = ms_new - ms_old;
40 uint32_t tick_load = SysTick->LOAD + 1;
41 switch (time_diff)
42 {
43 case 0:
44 return TimestampUS(static_cast<uint64_t>(ms_new) * 1000 + 1000 -
45 static_cast<uint64_t>(tick_value_old) * 1000 / tick_load);
46 case 1:
47 /* 中断发生在两次读取之间 / Interrupt happened between two reads */
48 return TimestampUS(static_cast<uint64_t>(ms_new) * 1000 + 1000 -
49 static_cast<uint64_t>(tick_value_new) * 1000 / tick_load);
50 default:
51 /* 中断耗时过长(超过1ms),程序异常 / Indicates that interrupt took more than
52 * 1ms, an error case */
53 ASSERT(false);
54 }
55
56 return 0;
57 }

◆ _get_milliseconds()

TimestampMS LibXR::STM32Timebase::_get_milliseconds ( )
inlinevirtual

获取当前时间(毫秒级) / Get current time in milliseconds

Returns
TimestampMS 当前时间(毫秒) / Current timestamp in milliseconds

Implements LibXR::Timebase.

Definition at line 64 of file stm32_timebase.hpp.

64{ return HAL_GetTick(); }

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