libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
stm32_pwm.cpp
1#include "stm32_pwm.hpp"
2
3#ifdef HAL_TIM_MODULE_ENABLED
4
5using namespace LibXR;
6
7STM32PWM::STM32PWM(TIM_HandleTypeDef* htim, uint32_t channel, bool complementary)
8 : htim_(htim), channel_(channel), complementary_(complementary)
9{
10}
11
12ErrorCode STM32PWM::SetDutyCycle(float value)
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}
30
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}
161
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}
180
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}
199
200#endif
ErrorCode SetConfig(Configuration config) override
Configures the PWM settings. 配置 PWM 参数。
Definition stm32_pwm.cpp:31
ErrorCode Disable() override
Disables the PWM output. 禁用 PWM 输出。
ErrorCode SetDutyCycle(float value) override
Sets the duty cycle of the PWM signal. 设置 PWM 信号的占空比。
Definition stm32_pwm.cpp:12
ErrorCode Enable() override
Enables the PWM output. 启用 PWM 输出。
LibXR 命名空间
Definition ch32_gpio.hpp:9
Configuration parameters for PWM. PWM 配置参数。
Definition pwm.hpp:23
uint32_t frequency
PWM signal frequency in Hz. PWM 信号的频率(Hz)。
Definition pwm.hpp:24