semf
stm32pwm.cpp
Go to the documentation of this file.
1
12
13#if defined(STM32) && defined(HAL_TIM_MODULE_ENABLED)
14namespace semf
15{
16Stm32Pwm::Stm32Pwm(TIM_HandleTypeDef& hwHandle, uint32_t channel, bool fastMode, Mode mode)
17: m_hwHandle(&hwHandle),
18 m_channel(channel),
19 m_fastModeEnabled(fastMode),
20 m_mode(mode)
21{
22}
23
24void Stm32Pwm::setMaxValue(unsigned int maxValue)
25{
26 SEMF_INFO("set max value %u", maxValue);
27 m_hwHandle->Instance->ARR = maxValue;
28}
29
30unsigned int Stm32Pwm::maxValue() const
31{
32 return m_hwHandle->Instance->ARR;
33}
34
35void Stm32Pwm::set(unsigned int value, bool inverted)
36{
37 SEMF_INFO("set: value %u, inverted: %d", value, inverted);
38 // Secure having flickers by updating the hardware register without changing the value.
39 if (value == m_pwmValue && value != 0)
40 {
41 SEMF_INFO("value is m_pwmValue and is not zero, return");
42 return;
43 }
44 m_pwmValue = value;
45
46 TIM_OC_InitTypeDef sConfigOC;
47 if (m_mode == Mode::Mode1)
48 {
49 sConfigOC.OCMode = TIM_OCMODE_PWM1;
50 }
51 else
52 {
53 sConfigOC.OCMode = TIM_OCMODE_PWM2;
54 }
55 sConfigOC.Pulse = value;
56 if (inverted)
57 {
58 sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
59 }
60 else
61 {
62 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
63 }
64 if (m_fastModeEnabled)
65 {
66 sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
67 }
68 else
69 {
70 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
71 }
72#if !defined(STM32L0)
73 sConfigOC.OCIdleState = 0;
74 sConfigOC.OCNIdleState = 0;
75#endif
76 HAL_StatusTypeDef state = HAL_TIM_PWM_ConfigChannel(m_hwHandle, &sConfigOC, m_channel);
77 if (state != HAL_OK)
78 {
79 if (state == HAL_ERROR)
80 {
81 SEMF_ERROR("hal error");
82 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Set_HalError)));
83 }
84 else if (state == HAL_BUSY)
85 {
86 SEMF_ERROR("hal busy");
87 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Set_HalBusy)));
88 }
89 else if (state == HAL_TIMEOUT)
90 {
91 SEMF_ERROR("hal timeout");
92 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Set_HalTimeout)));
93 }
94 return;
95 }
96
97 if (m_enabled)
98 {
99 start();
100 }
101}
102
104{
105 m_enabled = true;
106 SEMF_INFO("start");
107 HAL_StatusTypeDef state = HAL_TIM_PWM_Start(m_hwHandle, m_channel);
108 if (state != HAL_OK)
109 {
110 if (state == HAL_ERROR)
111 {
112 SEMF_ERROR("hal error");
113 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_HalError)));
114 }
115 else if (state == HAL_BUSY)
116 {
117 SEMF_ERROR("hal busy");
118 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_HalBusy)));
119 }
120 else if (state == HAL_TIMEOUT)
121 {
122 SEMF_ERROR("hal timeout");
123 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_HalTimeout)));
124 }
125 return;
126 }
127}
128
129unsigned int Stm32Pwm::value() const
130{
131 return m_pwmValue;
132}
133
135{
136 m_enabled = false;
137 SEMF_INFO("stop");
138 HAL_StatusTypeDef state = HAL_TIM_PWM_Stop(m_hwHandle, m_channel);
139 if (state != HAL_OK)
140 {
141 if (state == HAL_ERROR)
142 {
143 SEMF_ERROR("hal error");
144 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Stop_HalError)));
145 }
146 else if (state == HAL_BUSY)
147 {
148 SEMF_ERROR("hal busy");
149 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Stop_HalBusy)));
150 }
151 else if (state == HAL_TIMEOUT)
152 {
153 SEMF_ERROR("hal timeout");
154 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Stop_HalTimeout)));
155 }
156 return;
157 }
158}
159
161{
162 return m_enabled;
163}
164} /* namespace semf */
165#endif
Class for representing errors. Every error should have a unique source code. As a user feel encourage...
Definition: error.h:22
Signal< Error > error
Definition: pwm.h:60
void start() override
Definition: stm32pwm.cpp:103
void stop() override
Definition: stm32pwm.cpp:134
void set(unsigned int value, bool inverted=false) override
Set a PWM value. Duty cycle is value / maxValue.
Definition: stm32pwm.cpp:35
Mode
Defines the start level of a PWM cycle. See also OCxM setting IMx_CCMR1 register description.
Definition: stm32pwm.h:44
Stm32Pwm(TIM_HandleTypeDef &hwHandle, uint32_t channel, bool fastMode=false, Mode mode=Mode::Mode1)
Constructor.
Definition: stm32pwm.cpp:16
bool isEnabled() const override
Returns the current status of PWM module.
Definition: stm32pwm.cpp:160
void setMaxValue(unsigned int maxValue) override
Set the maximum possible PWM value.
Definition: stm32pwm.cpp:24
unsigned int maxValue() const override
Returns the maximum possible PWM value.
Definition: stm32pwm.cpp:30
unsigned int value() const override
Returns the current PWM value.
Definition: stm32pwm.cpp:129
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_INFO(...)
Definition: debug.h:41