diff --git a/firmware/Makefile b/firmware/Makefile index 7548f44..4e62eb3 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -119,6 +119,7 @@ CSRC = $(ALLCSRC) cfg/board.c # C++ sources that can be compiled in ARM or THUMB mode depending on the global # setting. CPPSRC = $(ALLCPPSRC) \ + pwm.cpp \ main.cpp # List ASM source files here. diff --git a/firmware/main.cpp b/firmware/main.cpp index a5c857b..667bd85 100644 --- a/firmware/main.cpp +++ b/firmware/main.cpp @@ -1,6 +1,14 @@ #include "ch.h" #include "hal.h" +#include "pwm.h" + +// 400khz / 1024 = 390hz PWM +Pwm heaterPwm(PWMD1, 1, 400000, 1024); + +// 48MHz / 1024 = 46.8khz PWM +Pwm pumpDac(PWMD3, 1, 48000000, 1024); + /* * Application entry point. */ @@ -8,6 +16,9 @@ int main(void) { halInit(); chSysInit(); + heaterPwm.Start(); + pumpDac.Start(); + while (true) { } diff --git a/firmware/pwm.cpp b/firmware/pwm.cpp new file mode 100644 index 0000000..cb03bf9 --- /dev/null +++ b/firmware/pwm.cpp @@ -0,0 +1,37 @@ +#include "pwm.h" + +#include "ch.h" +#include "hal.h" + +Pwm::Pwm(PWMDriver& driver, uint8_t channel, uint32_t counterFrequency, uint32_t counterPeriod) + : m_driver(&driver) + , m_channel(channel) + , m_counterFrequency(m_counterFrequency) + , m_counterPeriod(counterPeriod) +{ +} + +void Pwm::Start() +{ + PWMConfig config = { + m_counterFrequency, + m_counterPeriod, + nullptr, + { + {PWM_OUTPUT_ACTIVE_HIGH, nullptr}, + {PWM_OUTPUT_ACTIVE_HIGH, nullptr}, + {PWM_OUTPUT_ACTIVE_HIGH, nullptr}, + {PWM_OUTPUT_ACTIVE_HIGH, nullptr} + }, + 0, + 0 + }; + + pwmStart(m_driver, &config); +} + +void Pwm::SetDuty(float duty) { + pwmcnt_t highTime = m_counterPeriod * duty; + + pwm_lld_enable_channel(m_driver, m_channel, highTime); +} diff --git a/firmware/pwm.h b/firmware/pwm.h new file mode 100644 index 0000000..adbd9b9 --- /dev/null +++ b/firmware/pwm.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +struct PWMDriver; + +class Pwm +{ +public: + Pwm(PWMDriver& driver, uint8_t channel, uint32_t counterFrequency, uint32_t counterPeriod); + + void Start(); + void SetDuty(float duty); +private: + PWMDriver* const m_driver; + const uint8_t m_channel; + const uint32_t m_counterFrequency; + const uint16_t m_counterPeriod; +};