libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
stm32_pwm.hpp
1#pragma once
2
3#include "main.h"
4
5#ifdef HAL_TIM_MODULE_ENABLED
6
7#include "pwm.hpp"
8
9namespace LibXR
10{
11
12class STM32PWM : public PWM
13{
14 public:
15 STM32PWM(TIM_HandleTypeDef* htim, uint32_t channel) : htim_(htim), channel_(channel) {}
16
17 ErrorCode SetDutyCycle(float value) override
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 }
35
36 ErrorCode SetConfig(Configuration config) override
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 }
167
168 ErrorCode Enable() override
169 {
170 if (HAL_TIM_PWM_Start(htim_, channel_) != HAL_OK)
171 {
172 return ErrorCode::FAILED;
173 }
174 return ErrorCode::OK;
175 }
176
177 ErrorCode Disable() override
178 {
179 if (HAL_TIM_PWM_Stop(htim_, channel_) != HAL_OK)
180 {
181 return ErrorCode::FAILED;
182 }
183 return ErrorCode::OK;
184 }
185
186 private:
187 TIM_HandleTypeDef* htim_;
188 uint32_t channel_;
189};
190
191} // namespace LibXR
192
193#endif
Abstract base class for PWM (Pulse Width Modulation) control. PWM(脉冲宽度调制)控制的抽象基类。
Definition pwm.hpp:14
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.hpp:17
ErrorCode Enable() override
Enables the PWM output. 启用 PWM 输出。
ErrorCode SetConfig(Configuration config) override
Configures the PWM settings. 配置 PWM 参数。
Definition stm32_pwm.hpp:36
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值
Configuration parameters for PWM. PWM 配置参数。
Definition pwm.hpp:23