wideband/firmware/pwm.cpp

54 lines
1.2 KiB
C++
Raw Normal View History

2020-09-19 15:44:10 -07:00
#include "pwm.h"
2022-07-16 19:42:30 -07:00
#include <rusefi/math.h>
Pwm::Pwm(PWMDriver& driver)
: m_driver(&driver)
, m_counterFrequency(0)
, m_counterPeriod(0)
{
}
2020-09-19 15:44:10 -07:00
void Pwm::Start()
{
2022-12-01 15:52:03 -08:00
static const PWMConfig config = {
2020-09-19 15:44:10 -07:00
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-12-01 15:51:01 -08:00
void Pwm::Start(const 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);
}
void Pwm::SetDuty(int channel, float duty) {
2022-01-26 10:56:58 -08:00
auto dutyFloat = clampF(0, duty, 1);
m_dutyFloat[channel] = dutyFloat;
2022-01-26 10:56:58 -08:00
pwmcnt_t highTime = m_counterPeriod * dutyFloat;
2020-12-15 14:58:39 -08:00
pwmEnableChannel(m_driver, channel, highTime);
2020-09-19 15:44:10 -07:00
}
2020-12-15 14:58:39 -08:00
float Pwm::GetLastDuty(int channel)
2020-12-15 14:58:39 -08:00
{
return m_dutyFloat[channel];
2020-12-15 14:58:39 -08:00
}