2020-09-19 15:44:10 -07:00
|
|
|
#include "pwm.h"
|
|
|
|
|
2022-07-16 19:42:30 -07:00
|
|
|
#include <rusefi/math.h>
|
|
|
|
|
2020-09-19 15:44:10 -07:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-06-29 11:39:50 -07:00
|
|
|
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:39:50 -07:00
|
|
|
{
|
2022-06-29 11:41:45 -07:00
|
|
|
m_counterFrequency = config.frequency;
|
|
|
|
m_counterPeriod = config.period;
|
2022-06-29 11:39:50 -07:00
|
|
|
|
2022-06-29 11:41:45 -07:00
|
|
|
pwmStart(m_driver, &config);
|
2022-06-29 11:39:50 -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
|
|
|
|
2022-06-29 11:39:50 -07: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
|
|
|
}
|