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:
[legend]
Collaboration diagram for LibXR::STM32TimerTimebase:
[legend]

Public Member Functions

 STM32TimerTimebase (TIM_HandleTypeDef *timer)
 构造函数 / Constructor
 
MicrosecondTimestamp _get_microseconds ()
 获取当前时间(微秒级) / Get current time in microseconds
 
MillisecondTimestamp _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_HandleTypeDef * htim = 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 MicrosecondTimestamp GetMicroseconds ()
 获取当前时间的微秒级时间戳。 Gets the current timestamp in microseconds.
 
static MillisecondTimestamp GetMilliseconds ()
 获取当前时间的毫秒级时间戳。 Gets the current timestamp in milliseconds.
 

Detailed Description

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

Definition at line 46 of file stm32_timebase.hpp.

Constructor & Destructor Documentation

◆ STM32TimerTimebase()

STM32TimerTimebase::STM32TimerTimebase ( TIM_HandleTypeDef * timer)

构造函数 / Constructor

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

Definition at line 45 of file stm32_timebase.cpp.

46 : Timebase(static_cast<uint64_t>(UINT32_MAX) * 1000 + 999, UINT32_MAX)
47{
48 htim = timer;
49}
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

Member Function Documentation

◆ _get_microseconds()

MicrosecondTimestamp STM32TimerTimebase::_get_microseconds ( )
virtual

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

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

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

Implements LibXR::Timebase.

Definition at line 51 of file stm32_timebase.cpp.

52{
53 uint32_t ms_old = HAL_GetTick();
54 uint32_t tick_value_old = __HAL_TIM_GET_COUNTER(htim);
55 uint32_t ms_new = HAL_GetTick();
56 uint32_t tick_value_new = __HAL_TIM_GET_COUNTER(htim);
57
58 uint32_t autoreload = __HAL_TIM_GET_AUTORELOAD(htim) + 1;
59
60 uint32_t delta_ms = ms_new - ms_old;
61 switch (delta_ms)
62 {
63 case 0:
64 return MicrosecondTimestamp(static_cast<uint64_t>(ms_new) * 1000 +
65 static_cast<uint64_t>(tick_value_old) * 1000 /
66 autoreload);
67 case 1:
68 /* 中断发生在两次读取之间 / Interrupt happened between two reads */
69 return MicrosecondTimestamp(static_cast<uint64_t>(ms_new) * 1000 +
70 static_cast<uint64_t>(tick_value_new) * 1000 /
71 autoreload);
72 default:
73 /* 中断耗时过长(超过1ms),程序异常 / Indicates that interrupt took more than
74 * 1ms, an error case */
75 ASSERT(false);
76 }
77
78 return 0;
79}
表示微秒级时间戳的类。Class representing a timestamp in microseconds.

◆ _get_milliseconds()

MillisecondTimestamp STM32TimerTimebase::_get_milliseconds ( )
virtual

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

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

Implements LibXR::Timebase.

Definition at line 81 of file stm32_timebase.cpp.

81{ return HAL_GetTick(); }

Field Documentation

◆ htim

TIM_HandleTypeDef * STM32TimerTimebase::htim = nullptr
static

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

Definition at line 76 of file stm32_timebase.hpp.


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