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

Public Member Functions

 STM32PWM (TIM_HandleTypeDef *htim, uint32_t channel, bool complementary=false)
 
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 输出。
 
- Public Member Functions inherited from LibXR::PWM

Private Attributes

TIM_HandleTypeDef * htim_
 
uint32_t channel_
 
bool complementary_
 

Detailed Description

Definition at line 12 of file stm32_pwm.hpp.

Constructor & Destructor Documentation

◆ STM32PWM()

STM32PWM::STM32PWM ( TIM_HandleTypeDef * htim,
uint32_t channel,
bool complementary = false )

Definition at line 7 of file stm32_pwm.cpp.

8 : htim_(htim), channel_(channel), complementary_(complementary)
9{
10}

Member Function Documentation

◆ Disable()

ErrorCode STM32PWM::Disable ( )
overridevirtual

Disables the PWM output. 禁用 PWM 输出。

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

Implements LibXR::PWM.

Definition at line 181 of file stm32_pwm.cpp.

182{
183 if (!complementary_)
184 {
185 if (HAL_TIM_PWM_Stop(htim_, channel_) != HAL_OK)
186 {
187 return ErrorCode::FAILED;
188 }
189 }
190 else
191 {
192 if (HAL_TIMEx_PWMN_Stop(htim_, channel_) != HAL_OK)
193 {
194 return ErrorCode::FAILED;
195 }
196 }
197 return ErrorCode::OK;
198}

◆ Enable()

ErrorCode STM32PWM::Enable ( )
overridevirtual

Enables the PWM output. 启用 PWM 输出。

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

Implements LibXR::PWM.

Definition at line 162 of file stm32_pwm.cpp.

163{
164 if (!complementary_)
165 {
166 if (HAL_TIM_PWM_Start(htim_, channel_) != HAL_OK)
167 {
168 return ErrorCode::FAILED;
169 }
170 }
171 else
172 {
173 if (HAL_TIMEx_PWMN_Start(htim_, channel_) != HAL_OK)
174 {
175 return ErrorCode::FAILED;
176 }
177 }
178 return ErrorCode::OK;
179}

◆ SetConfig()

ErrorCode STM32PWM::SetConfig ( Configuration config)
overridevirtual

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 31 of file stm32_pwm.cpp.

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

◆ SetDutyCycle()

ErrorCode STM32PWM::SetDutyCycle ( float value)
overridevirtual

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 12 of file stm32_pwm.cpp.

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

Field Documentation

◆ channel_

uint32_t LibXR::STM32PWM::channel_
private

Definition at line 27 of file stm32_pwm.hpp.

◆ complementary_

bool LibXR::STM32PWM::complementary_
private

Definition at line 28 of file stm32_pwm.hpp.

◆ htim_

TIM_HandleTypeDef* LibXR::STM32PWM::htim_
private

Definition at line 26 of file stm32_pwm.hpp.


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