wideband/firmware/pwm.cpp

77 lines
1.6 KiB
C++
Raw Normal View History

2020-09-19 15:44:10 -07:00
#include "pwm.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)
{
}
Pwm::Pwm(PWMDriver& driver)
: m_driver(&driver)
, m_channel(0)
, m_counterFrequency(0)
, m_counterPeriod(0)
{
}
2020-09-19 15:44:10 -07:00
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
};
2022-06-29 11:41:45 -07:00
Start(config);
2020-09-19 15:44:10 -07:00
}
2022-06-29 11:41:45 -07:00
void Pwm::Start(PWMConfig& config)
{
2022-06-29 11:41:45 -07:00
m_counterFrequency = config.frequency;
m_counterPeriod = config.period;
2022-06-29 11:41:45 -07:00
pwmStart(m_driver, &config);
}
2020-10-30 02:39:39 -07:00
float maxF(float i1, float i2) {
2022-05-07 11:15:11 -07:00
return i1 > i2 ? i1 : i2;
2020-10-30 02:39:39 -07:00
}
float minF(float i1, float i2) {
2022-05-07 11:15:11 -07:00
return i1 < i2 ? i1 : i2;
2020-10-30 02:39:39 -07:00
}
float clampF(float min, float clamp, float max) {
2022-05-07 11:15:11 -07:00
return maxF(min, minF(clamp, max));
2020-10-30 02:39:39 -07:00
}
void Pwm::SetDuty(int channel, float duty) {
2022-01-26 10:56:58 -08:00
auto dutyFloat = clampF(0, duty, 1);
m_dutyFloat = dutyFloat;
pwmcnt_t highTime = m_counterPeriod * dutyFloat;
2020-12-15 14:58:39 -08:00
pwm_lld_enable_channel(m_driver, channel, highTime);
}
void Pwm::SetDuty(float duty) {
SetDuty(m_channel, duty);
2020-09-19 15:44:10 -07:00
}
2020-12-15 14:58:39 -08:00
2022-01-26 10:56:58 -08:00
float Pwm::GetLastDuty() const
2020-12-15 14:58:39 -08:00
{
2022-01-26 10:56:58 -08:00
return m_dutyFloat;
2020-12-15 14:58:39 -08:00
}