2020-10-27 16:33:32 -07:00
|
|
|
#include "pump_dac.h"
|
|
|
|
#include "pwm.h"
|
2020-10-31 14:54:50 -07:00
|
|
|
#include "heater_control.h"
|
2020-10-27 16:33:32 -07:00
|
|
|
|
2020-10-30 02:03:12 -07:00
|
|
|
#include "wideband_config.h"
|
|
|
|
|
2020-10-27 16:33:32 -07:00
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
// 48MHz / 1024 = 46.8khz PWM
|
2022-12-03 17:23:02 -08:00
|
|
|
// 64MHz / 1024 = 62.5khz PWM
|
2022-12-01 15:51:01 -08:00
|
|
|
static const PWMConfig pumpDacConfig = {
|
2022-12-03 17:23:02 -08:00
|
|
|
STM32_SYSCLK,
|
2022-08-29 17:19:30 -07:00
|
|
|
1024,
|
|
|
|
nullptr,
|
|
|
|
{
|
|
|
|
{PWM_OUTPUT_ACTIVE_HIGH, nullptr},
|
|
|
|
{PWM_OUTPUT_ACTIVE_HIGH, nullptr},
|
|
|
|
{PWM_OUTPUT_ACTIVE_HIGH, nullptr},
|
|
|
|
{PWM_OUTPUT_ACTIVE_HIGH, nullptr}
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
#if STM32_PWM_USE_ADVANCED
|
|
|
|
0
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static Pwm pumpDac(PUMP_DAC_PWM_DEVICE);
|
|
|
|
|
|
|
|
struct pump_dac_state {
|
|
|
|
int32_t curIpump;
|
|
|
|
};
|
2020-10-27 16:33:32 -07:00
|
|
|
|
2022-08-29 17:19:30 -07:00
|
|
|
static const uint8_t pumpDacPwmCh[] = {
|
|
|
|
PUMP_DAC_PWM_CHANNEL_0,
|
|
|
|
#if (AFR_CHANNELS > 1)
|
|
|
|
PUMP_DAC_PWM_CHANNEL_1,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct pump_dac_state state[AFR_CHANNELS];
|
2022-05-11 01:41:07 -07:00
|
|
|
|
2020-10-27 16:33:32 -07:00
|
|
|
void InitPumpDac()
|
|
|
|
{
|
2022-08-29 17:19:30 -07:00
|
|
|
pumpDac.Start(pumpDacConfig);
|
2020-10-27 16:33:32 -07:00
|
|
|
|
2022-08-29 17:19:30 -07:00
|
|
|
for (int ch = 0; ch < AFR_CHANNELS; ch++)
|
|
|
|
{
|
|
|
|
// Set zero current to start - sensor can be damaged if current flowing
|
|
|
|
// while warming up
|
|
|
|
SetPumpCurrentTarget(ch, 0);
|
|
|
|
}
|
2020-10-27 16:33:32 -07:00
|
|
|
}
|
|
|
|
|
2022-08-29 17:19:30 -07:00
|
|
|
void SetPumpCurrentTarget(int ch, int32_t microampere)
|
2020-10-27 16:33:32 -07:00
|
|
|
{
|
2020-10-31 14:54:50 -07:00
|
|
|
// Don't allow pump current when the sensor isn't hot
|
2022-08-29 17:19:30 -07:00
|
|
|
if (!IsRunningClosedLoop(ch))
|
2020-10-31 14:54:50 -07:00
|
|
|
{
|
|
|
|
microampere = 0;
|
|
|
|
}
|
|
|
|
|
2022-08-29 17:19:30 -07:00
|
|
|
state[ch].curIpump = microampere;
|
2022-05-11 01:41:07 -07:00
|
|
|
|
2020-10-27 16:33:32 -07:00
|
|
|
// 47 ohm resistor
|
|
|
|
// 0.147 gain
|
|
|
|
// effective resistance of 317 ohms
|
2020-10-31 14:54:50 -07:00
|
|
|
float volts = -0.000321162f * microampere;
|
2020-10-27 16:33:32 -07:00
|
|
|
|
2020-10-30 02:03:12 -07:00
|
|
|
// offset by half vcc
|
|
|
|
volts += HALF_VCC;
|
2020-10-27 16:33:32 -07:00
|
|
|
|
2022-08-29 17:19:30 -07:00
|
|
|
pumpDac.SetDuty(pumpDacPwmCh[ch], volts / VCC_VOLTS);
|
2020-10-27 16:33:32 -07:00
|
|
|
}
|
2020-12-15 14:58:39 -08:00
|
|
|
|
2022-08-29 17:19:30 -07:00
|
|
|
float GetPumpOutputDuty(int ch)
|
2020-12-15 14:58:39 -08:00
|
|
|
{
|
2023-02-02 17:00:49 -08:00
|
|
|
return pumpDac.GetLastDuty(pumpDacPwmCh[ch]);
|
2020-12-15 14:58:39 -08:00
|
|
|
}
|
2022-05-11 01:41:07 -07:00
|
|
|
|
2022-08-29 17:19:30 -07:00
|
|
|
float GetPumpCurrent(int ch)
|
2022-05-11 01:41:07 -07:00
|
|
|
{
|
2022-08-29 17:19:30 -07:00
|
|
|
return (float)state[ch].curIpump / 1000.0;
|
2022-05-11 01:41:07 -07:00
|
|
|
}
|