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

基于硬件定时器的时间基准类 / Provides a timebase using hardware timer (TIM) More...

#include <stm32_timebase.hpp>

Inheritance diagram for LibXR::STM32TimerTimebase:
Collaboration diagram for LibXR::STM32TimerTimebase:

Public Member Functions

 STM32TimerTimebase (TIM_HandleTypeDef *timer)
 构造函数 / 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.
 

Static Public Attributes

static TIM_HandleTypeDefhtim = nullptr
 硬件定时器句柄指针 / Static pointer to hardware timer handle
 
- Static Public Attributes inherited from LibXR::Timebase
static Timebasetimebase = nullptr
 静态指针,用于存储全局时间基对象。 Static pointer storing the global timebase instance.
 

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.
 

Detailed Description

基于硬件定时器的时间基准类 / Provides a timebase using hardware timer (TIM)

Definition at line 73 of file stm32_timebase.hpp.

Constructor & Destructor Documentation

◆ STM32TimerTimebase()

LibXR::STM32TimerTimebase::STM32TimerTimebase ( TIM_HandleTypeDef timer)
inline

构造函数 / Constructor

Parameters
timer定时器句柄指针 / Pointer to hardware timer handle

Definition at line 80 of file stm32_timebase.hpp.

81 : Timebase(static_cast<uint64_t>(UINT32_MAX) * 1000 + 999, UINT32_MAX)
82 {
83 htim = timer;
84 }
static TIM_HandleTypeDef * htim
硬件定时器句柄指针 / Static pointer to hardware timer handle
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::STM32TimerTimebase::_get_microseconds ( )
inlinevirtual

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

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

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

Implements LibXR::Timebase.

Definition at line 95 of file stm32_timebase.hpp.

96 {
101
103
105 switch (delta_ms)
106 {
107 case 0:
108 return TimestampUS(static_cast<uint64_t>(ms_new) * 1000 +
109 static_cast<uint64_t>(tick_value_old) * 1000 / autoreload);
110 case 1:
111 /* 中断发生在两次读取之间 / Interrupt happened between two reads */
112 return TimestampUS(static_cast<uint64_t>(ms_new) * 1000 +
113 static_cast<uint64_t>(tick_value_new) * 1000 / autoreload);
114 default:
115 /* 中断耗时过长(超过1ms),程序异常 / Indicates that interrupt took more than
116 * 1ms, an error case */
117 ASSERT(false);
118 }
119
120 return 0;
121 }

◆ _get_milliseconds()

TimestampMS LibXR::STM32TimerTimebase::_get_milliseconds ( )
inlinevirtual

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

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

Implements LibXR::Timebase.

Definition at line 128 of file stm32_timebase.hpp.

128{ return HAL_GetTick(); }

Field Documentation

◆ htim

TIM_HandleTypeDef * STM32TimerTimebase::htim = nullptr
static

硬件定时器句柄指针 / Static pointer to hardware timer handle

Definition at line 133 of file stm32_timebase.hpp.


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