AUX outputs (#95)

* AUX outputs

* Rev2: define AUX output PWM device and channels

* Rev2: enalbe TIM1 used for aux outputs

* Rev2: define aux output gain

Co-authored-by: Andrey Gusakov <dron0gus@gmail.com>
This commit is contained in:
rusefillc 2022-07-03 15:11:12 -04:00 committed by GitHub
parent fe50e80a89
commit 1454c10713
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 92 additions and 2 deletions

View File

@ -146,6 +146,7 @@ CPPSRC = $(ALLCPPSRC) \
uart.cpp \
shared/crc.cpp \
interpolation.cpp \
auxout.cpp \
console/binary/tunerstudio_io_serial.cpp \
console/binary/tunerstudio_io.cpp \
main.cpp

65
firmware/auxout.cpp Normal file
View File

@ -0,0 +1,65 @@
#include "pwm.h"
#include "lambda_conversion.h"
#include "wideband_config.h"
#include "hal.h"
#ifdef AUXOUT_DAC_PWM_DEVICE
// Rev2 low pass filter cut frequency is about 21Hz (sic!)
// 48Mhz / (2 ^ 12) ~= 12 KHz
PWMConfig auxPwmConfig = {
48'000'000,
1 << 12,
nullptr,
{
{PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH, nullptr},
{PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH, nullptr},
{PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH, nullptr},
{PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH, nullptr}
},
0,
0,
#if STM32_PWM_USE_ADVANCED
0
#endif
};
static Pwm auxDac(AUXOUT_DAC_PWM_DEVICE);
extern float clampF(float min, float clamp, float max);
void SetAuxDac(int channel, float voltage)
{
voltage = voltage / AUXOUT_GAIN;
auto duty = voltage / VCC_VOLTS;
duty = 1.0 - duty;
duty = clampF(0, duty, 1);
auxDac.SetDuty(channel ? AUXOUT_DAC_PWM_CHANNEL_1 : AUXOUT_DAC_PWM_CHANNEL_0, duty);
}
/* TODO: merge with some other communication thread? */
static THD_WORKING_AREA(waAuxOutThread, 256);
void AuxOutThread(void*)
{
while(1)
{
SetAuxDac(0, GetLambda());
chThdSleepMilliseconds(10);
}
}
void InitAuxDac()
{
auxDac.Start(auxPwmConfig);
SetAuxDac(0, 0.0);
SetAuxDac(1, 0.0);
chThdCreateStatic(waAuxOutThread, sizeof(waAuxOutThread), NORMALPRIO, AuxOutThread, nullptr);
}
#endif

6
firmware/auxout.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <cstdint>
void InitAuxDac();
void SetAuxDac(int channel, float voltage);

View File

@ -26,6 +26,13 @@
#define PUMP_DAC_PWM_DEVICE PWMD2
#define PUMP_DAC_PWM_CHANNEL 1
// TIM1 - DAC for AUX outputs
#define AUXOUT_DAC_PWM_DEVICE PWMD1
// PB14 - TIM1_CH2N
#define AUXOUT_DAC_PWM_CHANNEL_0 1
// PB15 - TIM1_CH3N
#define AUXOUT_DAC_PWM_CHANNEL_1 2
#define ID_SEL1_PORT GPIOC
#define ID_SEL1_PIN 13

View File

@ -133,8 +133,8 @@
/*
* PWM driver system settings.
*/
#define STM32_PWM_USE_ADVANCED FALSE
#define STM32_PWM_USE_TIM1 FALSE
#define STM32_PWM_USE_ADVANCED TRUE
#define STM32_PWM_USE_TIM1 TRUE
#define STM32_PWM_USE_TIM2 TRUE
#define STM32_PWM_USE_TIM3 FALSE
#define STM32_PWM_USE_TIM4 TRUE

View File

@ -35,3 +35,9 @@
// Nernst voltage & ESR sense
// *******************************
#define VM_RESISTOR_VALUE (0)
// *******************************
// AUX outputs
// *******************************
// OpAmp with 82K + 160K
#define AUXOUT_GAIN ((82.0 + 160.0) / 160.0)

View File

@ -9,6 +9,7 @@
#include "sampling.h"
#include "uart.h"
#include "io_pins.h"
#include "auxout.h"
using namespace wbo;
@ -25,6 +26,10 @@ int main() {
StartHeaterControl();
StartPumpControl();
#ifdef AUXOUT_DAC_PWM_DEVICE
InitAuxDac();
#endif
InitCan();
#ifdef ECHO_UART
InitUart();