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 44 of file stm32_timebase.cpp.

45 : Timebase(static_cast<uint64_t>(UINT32_MAX) * 1000 + 999, UINT32_MAX)
46{
47 htim = timer;
48}
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 50 of file stm32_timebase.cpp.

51{
52 do
53 {
54 uint32_t tick_old = HAL_GetTick();
55 uint32_t cnt_old = __HAL_TIM_GET_COUNTER(htim);
56 uint32_t tick_new = HAL_GetTick();
57 uint32_t cnt_new = __HAL_TIM_GET_COUNTER(htim);
58
59 uint32_t autoreload = __HAL_TIM_GET_AUTORELOAD(htim) + 1;
60
61 uint32_t delta_ms = tick_new - tick_old;
62 switch (delta_ms)
63 {
64 case 0:
65 return MicrosecondTimestamp(static_cast<uint64_t>(tick_new) * 1000 +
66 static_cast<uint64_t>(cnt_old) * 1000 / autoreload);
67 case 1:
68 /* 中断发生在两次读取之间 / Interrupt happened between two reads */
69 return MicrosecondTimestamp(static_cast<uint64_t>(tick_new) * 1000 +
70 static_cast<uint64_t>(cnt_new) * 1000 / autoreload);
71 default:
72 /* 中断耗时过长(超过1ms),程序异常 / Indicates that interrupt took more than
73 * 1ms, an error case */
74 continue;
75 }
76 } while (true);
77}
表示微秒级时间戳的类。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 79 of file stm32_timebase.cpp.

79{ 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: