diff --git a/firmware/hw_layer/adc/adc_external.h b/firmware/hw_layer/adc/adc_external.h index 7a0fd23158..099804ccca 100644 --- a/firmware/hw_layer/adc/adc_external.h +++ b/firmware/hw_layer/adc/adc_external.h @@ -13,4 +13,4 @@ #define getAdcValue(channel) getMcp3208adc(channel) #define adcToVoltsDivided(adc) (5.0f / 4095 * (adc)) -#define getVoltageDivided(msg, channel) (isAdcChannelValid(channel) ? adcToVoltsDivided(getAdcValue(msg, channel)) : 66.66) +#define getVoltageDivided(msg, channel) (isAdcChannelValid(channel) ? adcToVoltsDivided(getAdcValue(msg, channel), channel) : 66.66) diff --git a/firmware/hw_layer/adc/adc_inputs.cpp b/firmware/hw_layer/adc/adc_inputs.cpp index 6800b9dda6..4ee17ad243 100644 --- a/firmware/hw_layer/adc/adc_inputs.cpp +++ b/firmware/hw_layer/adc/adc_inputs.cpp @@ -21,8 +21,11 @@ #include "pch.h" -#if HAL_USE_ADC +float __attribute__((weak)) getAnalogInputDividerCoefficient(adc_channel_e) { + return engineConfiguration->analogInputDividerCoefficient; +} +#if HAL_USE_ADC #include "adc_subscription.h" #include "AdcConfiguration.h" @@ -42,7 +45,7 @@ static adc_channel_mode_e adcHwChannelEnabled[HW_MAX_ADC_INDEX]; // Board voltage, with divider coefficient accounted for float getVoltageDivided(const char *msg, adc_channel_e hwChannel) { - return getVoltage(msg, hwChannel) * engineConfiguration->analogInputDividerCoefficient; + return getVoltage(msg, hwChannel) * getAnalogInputDividerCoefficient(hwChannel); } // voltage in MCU universe, from zero to VDD @@ -295,7 +298,7 @@ adc_channel_e AdcDevice::getAdcHardwareIndexByInternalIndex(int index) const { static void printAdcValue(int channel) { int value = getAdcValue("print", (adc_channel_e)channel); - float volts = adcToVoltsDivided(value); + float volts = adcToVoltsDivided(value, (adc_channel_e)channel); efiPrintf("adc voltage : %.2f", volts); } diff --git a/firmware/hw_layer/adc/adc_inputs.h b/firmware/hw_layer/adc/adc_inputs.h index 078492c015..6897e515b4 100644 --- a/firmware/hw_layer/adc/adc_inputs.h +++ b/firmware/hw_layer/adc/adc_inputs.h @@ -15,6 +15,8 @@ #define SLOW_ADC_RATE 500 #endif +float getAnalogInputDividerCoefficient(adc_channel_e); + static inline bool isAdcChannelValid(adc_channel_e hwChannel) { if (hwChannel <= EFI_ADC_NONE) { return false; @@ -66,7 +68,7 @@ void removeChannel(const char *name, adc_channel_e setting); #define getAdcValue(msg, hwChannel) getInternalAdcValue(msg, hwChannel) -#define adcToVoltsDivided(adc) (adcToVolts(adc) * engineConfiguration->analogInputDividerCoefficient) +#define adcToVoltsDivided(adc, hwChannel) (adcToVolts(adc) * getAnalogInputDividerCoefficient(hwChannel)) #if !defined(GPT_FREQ_FAST) || !defined(GPT_PERIOD_FAST) /** diff --git a/firmware/hw_layer/adc/adc_subscription.cpp b/firmware/hw_layer/adc/adc_subscription.cpp index c9c9f1cf24..4f9042a46b 100644 --- a/firmware/hw_layer/adc/adc_subscription.cpp +++ b/firmware/hw_layer/adc/adc_subscription.cpp @@ -86,9 +86,9 @@ TODO: this code is similar to initIfValid, what is the plan? shall we extract he efiSetPadMode(name, pin, PAL_MODE_INPUT_ANALOG); } - // if 0, default to the board's divider coefficient + // if 0, default to the board's divider coefficient for given channel if (voltsPerAdcVolt == 0) { - voltsPerAdcVolt = engineConfiguration->analogInputDividerCoefficient; + voltsPerAdcVolt = getAnalogInputDividerCoefficient(channel); } #endif /* EFI_PROD_CODE */ // Populate the entry diff --git a/firmware/hw_layer/hardware.cpp b/firmware/hw_layer/hardware.cpp index 6536cb9f18..b0be9f9cea 100644 --- a/firmware/hw_layer/hardware.cpp +++ b/firmware/hw_layer/hardware.cpp @@ -185,11 +185,11 @@ void onFastAdcComplete(adcsample_t*) { #endif /* EFI_SENSOR_CHART */ #if EFI_MAP_AVERAGING - mapAveragingAdcCallback(adcToVoltsDivided(getFastAdc(fastMapSampleIndex))); + mapAveragingAdcCallback(adcToVoltsDivided(getFastAdc(fastMapSampleIndex), engineConfiguration->map.sensor.hwChannel)); #endif /* EFI_MAP_AVERAGING */ #if EFI_HIP_9011 if (engineConfiguration->isHip9011Enabled) { - hipAdcCallback(adcToVoltsDivided(getFastAdc(hipSampleIndex))); + hipAdcCallback(adcToVoltsDivided(getFastAdc(hipSampleIndex), engineConfiguration->hipOutputChannel)); } #endif /* EFI_HIP_9011 */ }