libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::STM32PWM Class Reference
Inheritance diagram for LibXR::STM32PWM:
Collaboration diagram for LibXR::STM32PWM:

Public Member Functions

 STM32PWM (TIM_HandleTypeDef *htim, uint32_t channel)
 
ErrorCode SetDutyCycle (float value) override
 Sets the duty cycle of the PWM signal. 设置 PWM 信号的占空比。
 
ErrorCode SetConfig (Configuration config) override
 Configures the PWM settings. 配置 PWM 参数。
 
ErrorCode Enable () override
 Enables the PWM output. 启用 PWM 输出。
 
ErrorCode Disable () override
 Disables the PWM output. 禁用 PWM 输出。
 

Private Attributes

TIM_HandleTypeDefhtim_
 
uint32_t channel_
 

Detailed Description

Definition at line 12 of file stm32_pwm.hpp.

Constructor & Destructor Documentation

◆ STM32PWM()

LibXR::STM32PWM::STM32PWM ( TIM_HandleTypeDef htim,
uint32_t  channel 
)
inline

Definition at line 15 of file stm32_pwm.hpp.

15: htim_(htim), channel_(channel) {}
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ Disable()

ErrorCode LibXR::STM32PWM::Disable ( )
inlineoverridevirtual

Disables the PWM output. 禁用 PWM 输出。

Returns
ErrorCode indicating success or failure. 返回操作结果的错误码。

Implements LibXR::PWM.

Definition at line 177 of file stm32_pwm.hpp.

178 {
179 if (HAL_TIM_PWM_Stop(htim_, channel_) != HAL_OK)
180 {
181 return ErrorCode::FAILED;
182 }
183 return ErrorCode::OK;
184 }

◆ Enable()

ErrorCode LibXR::STM32PWM::Enable ( )
inlineoverridevirtual

Enables the PWM output. 启用 PWM 输出。

Returns
ErrorCode indicating success or failure. 返回操作结果的错误码。

Implements LibXR::PWM.

Definition at line 168 of file stm32_pwm.hpp.

169 {
170 if (HAL_TIM_PWM_Start(htim_, channel_) != HAL_OK)
171 {
172 return ErrorCode::FAILED;
173 }
174 return ErrorCode::OK;
175 }

◆ SetConfig()

ErrorCode LibXR::STM32PWM::SetConfig ( Configuration  config)
inlineoverridevirtual

Configures the PWM settings. 配置 PWM 参数。

Parameters
configThe configuration structure containing PWM settings. 配置结构体,包含 PWM 设置。
Returns
ErrorCode indicating success or failure. 返回操作结果的错误码。

Implements LibXR::PWM.

Definition at line 36 of file stm32_pwm.hpp.

37 {
38 uint32_t clock_freq = 0; // NOLINT
39
40#if defined(STM32F0) || defined(STM32G0)
41 // STM32F0 and STM32 G0 do not have PCLK1/PCLK2,use HCLK as reference clock
43#else
44 uint32_t& target_freq = config.frequency;
45 if (target_freq == 0)
46 {
47 return ErrorCode::ARG_ERR;
48 }
49 else if (
50#if defined(TIM1)
51 htim_->Instance == TIM1 ||
52#endif
53#if defined(TIM8)
54 htim_->Instance == TIM8 ||
55#endif
56#if defined(TIM9)
57 htim_->Instance == TIM9 ||
58#endif
59#if defined(TIM10)
60 htim_->Instance == TIM10 ||
61#endif
62#if defined(TIM11)
63 htim_->Instance == TIM11 ||
64#endif
65#if defined(TIM15)
66 htim_->Instance == TIM15 ||
67#endif
68#if defined(TIM16)
69 htim_->Instance == TIM16 ||
70#endif
71#if defined(TIM17)
72 htim_->Instance == TIM17 ||
73#endif
74#if defined(TIM20)
75 htim_->Instance == TIM20 ||
76#endif
77 false)
78 {
80#ifdef RCC_CFGR_PPRE2
81 if ((RCC->CFGR & RCC_CFGR_PPRE2) != RCC_CFGR_PPRE2_DIV1)
82 {
83 clock_freq *= 2;
84 }
85#endif
86 }
87 else if (
88#if defined(TIM2)
89 htim_->Instance == TIM2 ||
90#endif
91#if defined(TIM3)
92 htim_->Instance == TIM3 ||
93#endif
94#if defined(TIM4)
95 htim_->Instance == TIM4 ||
96#endif
97#if defined(TIM5)
98 htim_->Instance == TIM5 ||
99#endif
100#if defined(TIM6)
101 htim_->Instance == TIM6 ||
102#endif
103#if defined(TIM7)
104 htim_->Instance == TIM7 ||
105#endif
106#if defined(TIM12)
107 htim_->Instance == TIM12 ||
108#endif
109#if defined(TIM13)
110 htim_->Instance == TIM13 ||
111#endif
112#if defined(TIM14)
113 htim_->Instance == TIM14 ||
114#endif
115 false)
116 {
118#ifdef RCC_CFGR_PPRE1
119 if ((RCC->CFGR & RCC_CFGR_PPRE1) != RCC_CFGR_PPRE1_DIV1)
120 {
121 clock_freq *= 2;
122 }
123#endif
124 }
125 else
126 {
127 return ErrorCode::NOT_SUPPORT;
128 }
129#endif
130
131 if (clock_freq == 0)
132 {
133 return ErrorCode::INIT_ERR;
134 }
135
136 bool found = false;
137
139 uint32_t period = 0;
140
141 for (prescaler = 1; prescaler <= 0xFFFF; ++prescaler)
142 {
144 if (temp_period <= 0x10000)
145 {
147 found = true;
148 break;
149 }
150 }
151
152 if (!found || period == 0)
153 {
154 return ErrorCode::INIT_ERR;
155 }
156
157 htim_->Init.Prescaler = prescaler - 1;
158 htim_->Init.Period = period - 1;
159
160 if (HAL_TIM_PWM_Init(htim_) != HAL_OK)
161 {
162 return ErrorCode::INIT_ERR;
163 }
164
165 return ErrorCode::OK;
166 }

◆ SetDutyCycle()

ErrorCode LibXR::STM32PWM::SetDutyCycle ( float  value)
inlineoverridevirtual

Sets the duty cycle of the PWM signal. 设置 PWM 信号的占空比。

Parameters
valueThe duty cycle as a floating-point value (0.0 to 1.0). 占空比,浮点值(0.0 到 1.0)。
Returns
ErrorCode indicating success or failure. 返回操作结果的错误码。

Implements LibXR::PWM.

Definition at line 17 of file stm32_pwm.hpp.

18 {
19 if (value < 0.0f)
20 {
21 value = 0.0f;
22 }
23 else if (value > 1.0f)
24 {
25 value = 1.0f;
26 }
27
29 static_cast<uint32_t>(static_cast<float>(htim_->Init.Period + 1) * value);
30
31 __HAL_TIM_SET_COMPARE(htim_, channel_, pulse);
32
33 return ErrorCode::OK;
34 }

Field Documentation

◆ channel_

uint32_t LibXR::STM32PWM::channel_
private

Definition at line 188 of file stm32_pwm.hpp.

◆ htim_

TIM_HandleTypeDef* LibXR::STM32PWM::htim_
private

Definition at line 187 of file stm32_pwm.hpp.


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