diff --git a/firmware/Makefile b/firmware/Makefile index ee7c9f9..91b9129 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -145,6 +145,7 @@ CPPSRC = $(ALLCPPSRC) \ fault.cpp \ lambda_conversion.cpp \ pwm.cpp \ + dac.cpp \ pump_dac.cpp \ sampling.cpp \ heater_control.cpp \ diff --git a/firmware/auxout.cpp b/firmware/auxout.cpp index bd72dd3..4644190 100644 --- a/firmware/auxout.cpp +++ b/firmware/auxout.cpp @@ -1,4 +1,5 @@ #include "pwm.h" +#include "dac.h" #include "lambda_conversion.h" #include "port.h" #include "io_pins.h" @@ -58,42 +59,6 @@ void SetAuxDac(int channel, float voltage) #ifdef AUXOUT_DAC_DEVICE -class Dac -{ -public: - Dac(DACDriver& driver); - - void Start(DACConfig& config); - void SetVoltage(int channel, float duty); - float GetLastVoltage(int channel); - -private: - DACDriver* const m_driver; - float m_voltageFloat[2]; -}; - -Dac::Dac(DACDriver& driver) - : m_driver(&driver) -{ -} - -void Dac::Start(DACConfig& config) -{ - dacStart(m_driver, &config); -} - -void Dac::SetVoltage(int channel, float voltage) { - voltage = clampF(0, voltage, VCC_VOLTS); - m_voltageFloat[channel] = voltage; - - dacPutChannelX(m_driver, channel, voltage / VCC_VOLTS * (1 << 12)); -} - -float Dac::GetLastVoltage(int channel) -{ - return m_voltageFloat[channel]; -} - static DACConfig auxDacConfig = { .init = 2047U, .datamode = DAC_DHRM_12BIT_RIGHT, diff --git a/firmware/dac.cpp b/firmware/dac.cpp new file mode 100644 index 0000000..51a548a --- /dev/null +++ b/firmware/dac.cpp @@ -0,0 +1,32 @@ +#include "dac.h" + +/* for VCC_VOLTS */ +#include "wideband_config.h" + +#include + +#if HAL_USE_DAC + +Dac::Dac(DACDriver& driver) + : m_driver(&driver) +{ +} + +void Dac::Start(DACConfig& config) +{ + dacStart(m_driver, &config); +} + +void Dac::SetVoltage(int channel, float voltage) { + voltage = clampF(0, voltage, VCC_VOLTS); + m_voltageFloat[channel] = voltage; + + dacPutChannelX(m_driver, channel, voltage / VCC_VOLTS * (1 << 12)); +} + +float Dac::GetLastVoltage(int channel) +{ + return m_voltageFloat[channel]; +} + +#endif diff --git a/firmware/dac.h b/firmware/dac.h new file mode 100644 index 0000000..4b492f0 --- /dev/null +++ b/firmware/dac.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +/* for DACConfig */ +#include "hal.h" + +#if HAL_USE_DAC + +class Dac +{ +public: + Dac(DACDriver& driver); + + void Start(DACConfig& config); + void SetVoltage(int channel, float volage); + float GetLastVoltage(int channel); + +private: + DACDriver* const m_driver; + float m_voltageFloat[2]; +}; + +#endif