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

Public Member Functions

 STM32Timebase ()
 默认构造函数 / Default 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.
 

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.
 
- 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()

STM32Timebase::STM32Timebase ( )

默认构造函数 / Default constructor

Definition at line 5 of file stm32_timebase.cpp.

6 : Timebase(static_cast<uint64_t>(UINT32_MAX) * 1000 + 999, UINT32_MAX)
7{
8}
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 STM32Timebase::_get_microseconds ( )
virtual

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

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

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

Implements LibXR::Timebase.

Definition at line 10 of file stm32_timebase.cpp.

11{
12 uint32_t ms_old = HAL_GetTick();
13 uint32_t tick_value_old = SysTick->VAL;
14 uint32_t ms_new = HAL_GetTick();
15 uint32_t tick_value_new = SysTick->VAL;
16
17 auto time_diff = ms_new - ms_old;
18 uint32_t tick_load = SysTick->LOAD + 1;
19 switch (time_diff)
20 {
21 case 0:
22 return MicrosecondTimestamp(static_cast<uint64_t>(ms_new) * 1000 + 1000 -
23 static_cast<uint64_t>(tick_value_old) * 1000 /
24 tick_load);
25 case 1:
26 /* 中断发生在两次读取之间 / Interrupt happened between two reads */
27 return MicrosecondTimestamp(static_cast<uint64_t>(ms_new) * 1000 + 1000 -
28 static_cast<uint64_t>(tick_value_new) * 1000 /
29 tick_load);
30 default:
31 /* 中断耗时过长(超过1ms),程序异常 / Indicates that interrupt took more than
32 * 1ms, an error case */
33 ASSERT(false);
34 }
35
36 return 0;
37}
表示微秒级时间戳的类。Class representing a timestamp in microseconds.

◆ _get_milliseconds()

MillisecondTimestamp STM32Timebase::_get_milliseconds ( )
virtual

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

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

Implements LibXR::Timebase.

Definition at line 39 of file stm32_timebase.cpp.

39{ return HAL_GetTick(); }

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