2020-09-19 15:44:10 -07:00
|
|
|
#include "pwm.h"
|
|
|
|
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
Pwm::Pwm(PWMDriver& driver, uint8_t channel, uint32_t counterFrequency, uint32_t counterPeriod)
|
|
|
|
: m_driver(&driver)
|
|
|
|
, m_channel(channel)
|
2020-10-26 11:26:31 -07:00
|
|
|
, m_counterFrequency(counterFrequency)
|
2020-09-19 15:44:10 -07:00
|
|
|
, m_counterPeriod(counterPeriod)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pwm::Start()
|
|
|
|
{
|
|
|
|
PWMConfig config = {
|
|
|
|
m_counterFrequency,
|
|
|
|
m_counterPeriod,
|
|
|
|
nullptr,
|
|
|
|
{
|
2020-10-26 12:45:50 -07:00
|
|
|
{PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, nullptr},
|
|
|
|
{PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, nullptr},
|
|
|
|
{PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, nullptr},
|
|
|
|
{PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, nullptr}
|
2020-09-19 15:44:10 -07:00
|
|
|
},
|
|
|
|
0,
|
2021-03-17 22:53:24 -07:00
|
|
|
0,
|
2021-04-26 17:47:17 -07:00
|
|
|
#if STM32_PWM_USE_ADVANCED
|
2020-09-19 15:44:10 -07:00
|
|
|
0
|
2021-04-26 17:47:17 -07:00
|
|
|
#endif
|
2020-09-19 15:44:10 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
pwmStart(m_driver, &config);
|
|
|
|
}
|
|
|
|
|
2020-10-30 02:39:39 -07:00
|
|
|
float maxF(float i1, float i2) {
|
|
|
|
return i1 > i2 ? i1 : i2;
|
|
|
|
}
|
|
|
|
|
|
|
|
float minF(float i1, float i2) {
|
|
|
|
return i1 < i2 ? i1 : i2;
|
|
|
|
}
|
|
|
|
|
|
|
|
float clampF(float min, float clamp, float max) {
|
|
|
|
return maxF(min, minF(clamp, max));
|
|
|
|
}
|
|
|
|
|
2020-09-19 15:44:10 -07:00
|
|
|
void Pwm::SetDuty(float duty) {
|
2020-10-30 02:39:39 -07:00
|
|
|
pwmcnt_t highTime = m_counterPeriod * clampF(0, duty, 1);
|
2020-09-19 15:44:10 -07:00
|
|
|
|
2020-12-15 14:58:39 -08:00
|
|
|
m_lastDuty = highTime;
|
|
|
|
|
2020-09-19 15:44:10 -07:00
|
|
|
pwm_lld_enable_channel(m_driver, m_channel, highTime);
|
|
|
|
}
|
2020-12-15 14:58:39 -08:00
|
|
|
|
|
|
|
uint16_t Pwm::GetLastDuty() const
|
|
|
|
{
|
|
|
|
return m_lastDuty;
|
|
|
|
}
|