From 8bd19fcc2c4a130f81701eeec00957e3d3d99d4c Mon Sep 17 00:00:00 2001 From: rusefillc <48498823+rusefillc@users.noreply.github.com> Date: Sat, 29 Apr 2023 01:01:13 -0400 Subject: [PATCH] Aux2 (#243) * auxout: manually fill pwm config (cherry picked from commit a01876bc034264de5d6930bf2cc49cf0fc0cbb0a) * auxout: some boards use primary PWM outputs instead of complementary (cherry picked from commit c13b95441e7b284a36445c261199e96580e94ebd) * auxout: implement ripple cancelation using inverted PWM (cherry picked from commit 77cbc04990421b24021639b098ea6040af3a12f8) * f1_common: default AUXOUT value is AFR voltage with 'default' scaling 8.5 to 18.0 AFR is represented with 0.0 to 5.0V (cherry picked from commit 0c62ab8f509ff0ab3ab4260e308ad4b55bd64e40) --------- Co-authored-by: Andrey Gusakov --- firmware/auxout.cpp | 52 +++++++++++++++++++++++---- firmware/boards/f1_common/f1_port.cpp | 11 ++++++ firmware/boards/f1_dual/io/io_pins.h | 2 ++ firmware/boards/f1_rev2/io/io_pins.h | 2 ++ 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/firmware/auxout.cpp b/firmware/auxout.cpp index 99ace10..2e23203 100644 --- a/firmware/auxout.cpp +++ b/firmware/auxout.cpp @@ -15,19 +15,25 @@ #ifdef AUXOUT_DAC_PWM_DEVICE +#ifndef AUXOUT_DAC_PWM_OUTPUT_MODE +#define AUXOUT_DAC_PWM_OUTPUT_MODE PWM_OUTPUT_ACTIVE_HIGH +#endif +#ifndef AUXOUT_DAC_PWM_NC_OUTPUT_MODE +#define AUXOUT_DAC_PWM_NC_OUTPUT_MODE PWM_OUTPUT_ACTIVE_LOW +#endif + // Rev2 low pass filter cut frequency is about 21Hz (sic!) // 48Mhz / (2 ^ 12) ~= 12 KHz // 64mhz / (2 ^ 12) ~= 16 KHz -static const PWMConfig auxPwmConfig = { +static PWMConfig auxPwmConfig = { .frequency = STM32_SYSCLK, .period = 1 << 12, .callback = nullptr, .channels = { - // TODO: do any boards use the "primary" outputs instead of the "complementary" outputs? - [0] = {/*PWM_OUTPUT_ACTIVE_HIGH |*/ PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH, nullptr}, - [1] = {/*PWM_OUTPUT_ACTIVE_HIGH |*/ PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH, nullptr}, - [2] = {/*PWM_OUTPUT_ACTIVE_HIGH |*/ PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH, nullptr}, - [3] = {/*PWM_OUTPUT_ACTIVE_HIGH |*/ PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH, nullptr} + [0] = {0, nullptr}, + [1] = {0, nullptr}, + [2] = {0, nullptr}, + [3] = {0, nullptr} }, .cr2 = 0, #if STM32_PWM_USE_ADVANCED @@ -36,15 +42,42 @@ static const PWMConfig auxPwmConfig = { .dier = 0 }; +static void auxDacFillPwmConfig(void) +{ + auxPwmConfig.channels[AUXOUT_DAC_PWM_CHANNEL_0].mode = AUXOUT_DAC_PWM_OUTPUT_MODE; + auxPwmConfig.channels[AUXOUT_DAC_PWM_CHANNEL_1].mode = AUXOUT_DAC_PWM_OUTPUT_MODE; +#ifdef AUXOUT_DAC_PWM_CHANNEL_0_NC + auxPwmConfig.channels[AUXOUT_DAC_PWM_CHANNEL_0_NC].mode = AUXOUT_DAC_PWM_NC_OUTPUT_MODE; +#endif +#ifdef AUXOUT_DAC_PWM_CHANNEL_1_NC + auxPwmConfig.channels[AUXOUT_DAC_PWM_CHANNEL_1_NC].mode = AUXOUT_DAC_PWM_NC_OUTPUT_MODE; +#endif +} + static Pwm auxDac(AUXOUT_DAC_PWM_DEVICE); -static const uint8_t auxOutPwmCh[] = { +static const uint8_t auxOutPwmCh[AFR_CHANNELS] = { AUXOUT_DAC_PWM_CHANNEL_0, #if (AFR_CHANNELS > 1) AUXOUT_DAC_PWM_CHANNEL_1, #endif }; +static const int8_t auxOutPwmChN[AFR_CHANNELS] = { +#ifdef AUXOUT_DAC_PWM_CHANNEL_0_NC + AUXOUT_DAC_PWM_CHANNEL_0_NC, +#else + -1, +#endif +#if (AFR_CHANNELS > 1) +#ifdef AUXOUT_DAC_PWM_CHANNEL_1_NC + AUXOUT_DAC_PWM_CHANNEL_1_NC, +#else + -1, +#endif +#endif +}; + void SetAuxDac(int channel, float voltage) { voltage = voltage / AUXOUT_GAIN; @@ -53,6 +86,10 @@ void SetAuxDac(int channel, float voltage) duty = clampF(0, duty, 1); auxDac.SetDuty(auxOutPwmCh[channel], duty); + // Ripple cancelation channel + if (auxOutPwmChN[channel >= 0]) { + auxDac.SetDuty(auxOutPwmChN[channel], duty); + } } #endif @@ -135,6 +172,7 @@ void AuxOutThread(void*) void InitAuxDac() { #if defined(AUXOUT_DAC_PWM_DEVICE) + auxDacFillPwmConfig(); auxDac.Start(auxPwmConfig); SetAuxDac(0, 0.0); diff --git a/firmware/boards/f1_common/f1_port.cpp b/firmware/boards/f1_common/f1_port.cpp index 26332a8..fa228d4 100644 --- a/firmware/boards/f1_common/f1_port.cpp +++ b/firmware/boards/f1_common/f1_port.cpp @@ -30,9 +30,20 @@ static Configuration cfg; // Configuration defaults void Configuration::LoadDefaults() { + int i; + CanIndexOffset = 0; sensorType = BOARD_DEFAULT_SENSOR_TYPE; + /* default auxout curve is 0..5V for AFR 8.5 to 18.0 + * default auxout[n] input is AFR[n] */ + for (i = 0; i < 8; i++) { + auxOutBins[0][i] = auxOutBins[1][i] = 8.5 + (18.0 - 8.5) / 7 * i; + auxOutValues[0][i] = auxOutValues[1][i] = 0.0 + (5.0 - 0.0) / 7 * i; + } + auxOutputSource[0] = AuxOutputMode::Afr0; + auxOutputSource[1] = AuxOutputMode::Afr1; + /* Finaly */ Tag = ExpectedTag; } diff --git a/firmware/boards/f1_dual/io/io_pins.h b/firmware/boards/f1_dual/io/io_pins.h index e6ec1f5..8b1183e 100644 --- a/firmware/boards/f1_dual/io/io_pins.h +++ b/firmware/boards/f1_dual/io/io_pins.h @@ -54,6 +54,8 @@ #define AUXOUT_DAC_PWM_CHANNEL_0 1 // PB15 - TIM1_CH3N #define AUXOUT_DAC_PWM_CHANNEL_1 2 +// CH2N and CH3N are complementary outputs +#define AUXOUT_DAC_PWM_OUTPUT_MODE PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH #define ID_SEL1_PORT GPIOC #define ID_SEL1_PIN 13 diff --git a/firmware/boards/f1_rev2/io/io_pins.h b/firmware/boards/f1_rev2/io/io_pins.h index 0897993..f3d2e85 100644 --- a/firmware/boards/f1_rev2/io/io_pins.h +++ b/firmware/boards/f1_rev2/io/io_pins.h @@ -46,6 +46,8 @@ #define AUXOUT_DAC_PWM_CHANNEL_0 1 // PB15 - TIM1_CH3N #define AUXOUT_DAC_PWM_CHANNEL_1 2 +// CH2N and CH3N are complementary outputs +#define AUXOUT_DAC_PWM_OUTPUT_MODE PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH #define ID_SEL1_PORT GPIOC #define ID_SEL1_PIN 13