This commit is contained in:
Matthew Kennedy 2020-09-19 15:44:10 -07:00
parent 5527b04f8f
commit cda402e38f
4 changed files with 68 additions and 0 deletions

View File

@ -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.

View File

@ -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) {
}

37
firmware/pwm.cpp Normal file
View File

@ -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);
}

19
firmware/pwm.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <cstdint>
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;
};