Dac helper (#226)

* dac: move helpers to separate cpp and h

(cherry picked from commit aee71931db269fc256ab43e1fb84b71c77e215d7)

* dac: HAL_USE_DAC guards

(cherry picked from commit 5517844e4d0f79c7cf8c5bb097c110541063bddf)

---------

Co-authored-by: Andrey Gusakov <dron0gus@gmail.com>
This commit is contained in:
rusefillc 2023-04-17 17:11:34 -04:00 committed by GitHub
parent 36d57e7d73
commit 898fa20c2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 36 deletions

View File

@ -145,6 +145,7 @@ CPPSRC = $(ALLCPPSRC) \
fault.cpp \
lambda_conversion.cpp \
pwm.cpp \
dac.cpp \
pump_dac.cpp \
sampling.cpp \
heater_control.cpp \

View File

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

32
firmware/dac.cpp Normal file
View File

@ -0,0 +1,32 @@
#include "dac.h"
/* for VCC_VOLTS */
#include "wideband_config.h"
#include <rusefi/math.h>
#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

24
firmware/dac.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include <cstdint>
/* 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