diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index 4bed4cb854..15021b2e7e 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -982,13 +982,13 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ break; #endif /* EFI_CAN_SUPPORT */ case DBG_ANALOG_INPUTS: - tsOutputChannels->debugFloatField1 = (engineConfiguration->vbattAdcChannel != EFI_ADC_NONE) ? getVoltageDivided("vbatt", engineConfiguration->vbattAdcChannel) : 0.0f; - tsOutputChannels->debugFloatField2 = (engineConfiguration->tps1_1AdcChannel != EFI_ADC_NONE) ? getVoltageDivided("tps", engineConfiguration->tps1_1AdcChannel) : 0.0f; - tsOutputChannels->debugFloatField3 = (engineConfiguration->mafAdcChannel != EFI_ADC_NONE) ? getVoltageDivided("maf", engineConfiguration->mafAdcChannel) : 0.0f; - tsOutputChannels->debugFloatField4 = (engineConfiguration->map.sensor.hwChannel != EFI_ADC_NONE) ? getVoltageDivided("map", engineConfiguration->map.sensor.hwChannel) : 0.0f; - tsOutputChannels->debugFloatField5 = (engineConfiguration->clt.adcChannel != EFI_ADC_NONE) ? getVoltageDivided("clt", engineConfiguration->clt.adcChannel) : 0.0f; - tsOutputChannels->debugFloatField6 = (engineConfiguration->iat.adcChannel != EFI_ADC_NONE) ? getVoltageDivided("iat", engineConfiguration->iat.adcChannel) : 0.0f; - tsOutputChannels->debugFloatField7 = (engineConfiguration->afr.hwChannel != EFI_ADC_NONE) ? getVoltageDivided("ego", engineConfiguration->afr.hwChannel) : 0.0f; + tsOutputChannels->debugFloatField1 = (engineConfiguration->vbattAdcChannel != EFI_ADC_NONE) ? getVoltageDivided("vbatt", engineConfiguration->vbattAdcChannel PASS_ENGINE_PARAMETER_SUFFIX) : 0.0f; + tsOutputChannels->debugFloatField2 = (engineConfiguration->tps1_1AdcChannel != EFI_ADC_NONE) ? getVoltageDivided("tps", engineConfiguration->tps1_1AdcChannel PASS_ENGINE_PARAMETER_SUFFIX) : 0.0f; + tsOutputChannels->debugFloatField3 = (engineConfiguration->mafAdcChannel != EFI_ADC_NONE) ? getVoltageDivided("maf", engineConfiguration->mafAdcChannel PASS_ENGINE_PARAMETER_SUFFIX) : 0.0f; + tsOutputChannels->debugFloatField4 = (engineConfiguration->map.sensor.hwChannel != EFI_ADC_NONE) ? getVoltageDivided("map", engineConfiguration->map.sensor.hwChannel PASS_ENGINE_PARAMETER_SUFFIX) : 0.0f; + tsOutputChannels->debugFloatField5 = (engineConfiguration->clt.adcChannel != EFI_ADC_NONE) ? getVoltageDivided("clt", engineConfiguration->clt.adcChannel PASS_ENGINE_PARAMETER_SUFFIX) : 0.0f; + tsOutputChannels->debugFloatField6 = (engineConfiguration->iat.adcChannel != EFI_ADC_NONE) ? getVoltageDivided("iat", engineConfiguration->iat.adcChannel PASS_ENGINE_PARAMETER_SUFFIX) : 0.0f; + tsOutputChannels->debugFloatField7 = (engineConfiguration->afr.hwChannel != EFI_ADC_NONE) ? getVoltageDivided("ego", engineConfiguration->afr.hwChannel PASS_ENGINE_PARAMETER_SUFFIX) : 0.0f; break; case DBG_ANALOG_INPUTS2: tsOutputChannels->debugFloatField4 = getVoltage("debug", engineConfiguration->throttlePedalPositionAdcChannel); diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index b2a37f44c2..78bb5de27e 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -164,7 +164,7 @@ void Engine::updateSlowSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE) { // todo: move this logic somewhere to sensors folder? if (CONFIG(fuelLevelSensor) != EFI_ADC_NONE) { - float fuelLevelVoltage = getVoltageDivided("fuel", engineConfiguration->fuelLevelSensor); + float fuelLevelVoltage = getVoltageDivided("fuel", engineConfiguration->fuelLevelSensor PASS_ENGINE_PARAMETER_SUFFIX); sensors.fuelTankLevel = interpolateMsg("fgauge", CONFIGB(fuelLevelEmptyTankVoltage), 0, CONFIGB(fuelLevelFullTankVoltage), 100, fuelLevelVoltage); diff --git a/firmware/controllers/sensors/allsensors.cpp b/firmware/controllers/sensors/allsensors.cpp index fce99e3758..61619bfae0 100644 --- a/firmware/controllers/sensors/allsensors.cpp +++ b/firmware/controllers/sensors/allsensors.cpp @@ -29,5 +29,5 @@ bool getAcToggle(DECLARE_ENGINE_PARAMETER_SIGNATURE) { * for now we are looking for a pull-up. High level means input switch is floating (which is OFF position) * low value means input is ground - which means ON. */ - return getVoltageDivided("A/C", engineConfiguration->acSwitchAdc) < 2.5; + return getVoltageDivided("A/C", engineConfiguration->acSwitchAdc PASS_ENGINE_PARAMETER_SUFFIX) < 2.5; } diff --git a/firmware/controllers/sensors/allsensors.h b/firmware/controllers/sensors/allsensors.h index 673a5c4b3d..2388b2d43b 100644 --- a/firmware/controllers/sensors/allsensors.h +++ b/firmware/controllers/sensors/allsensors.h @@ -21,8 +21,6 @@ #include "thermistors.h" #include "adc_inputs.h" -#include "adc_inputs.h" - void initSensors(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX); bool hasAcToggle(DECLARE_ENGINE_PARAMETER_SIGNATURE); diff --git a/firmware/controllers/sensors/ego.cpp b/firmware/controllers/sensors/ego.cpp index 4b1307f25d..d03813aa2b 100644 --- a/firmware/controllers/sensors/ego.cpp +++ b/firmware/controllers/sensors/ego.cpp @@ -111,7 +111,7 @@ float getAfr(DECLARE_ENGINE_PARAMETER_SIGNATURE) { #endif /* EFI_CJ125 && HAL_USE_SPI */ afr_sensor_s * sensor = &CONFIG(afr); - float volts = getVoltageDivided("ego", sensor->hwChannel); + float volts = getVoltageDivided("ego", sensor->hwChannel PASS_ENGINE_PARAMETER_SUFFIX); if (CONFIGB(afr_type) == ES_NarrowBand) { float afr = interpolate2d("narrow", volts, engineConfiguration->narrowToWideOxygenBins, engineConfiguration->narrowToWideOxygen); diff --git a/firmware/controllers/sensors/maf.cpp b/firmware/controllers/sensors/maf.cpp index 89b65a9e16..496798f9cf 100644 --- a/firmware/controllers/sensors/maf.cpp +++ b/firmware/controllers/sensors/maf.cpp @@ -10,7 +10,7 @@ EXTERN_ENGINE * @return MAF sensor voltage */ float getMafVoltage(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - return getVoltageDivided("maf", engineConfiguration->mafAdcChannel); + return getVoltageDivided("maf", engineConfiguration->mafAdcChannel PASS_ENGINE_PARAMETER_SUFFIX); } bool hasMafSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE) { diff --git a/firmware/controllers/sensors/map.cpp b/firmware/controllers/sensors/map.cpp index ec29c327e0..7b4d0915d6 100644 --- a/firmware/controllers/sensors/map.cpp +++ b/firmware/controllers/sensors/map.cpp @@ -148,7 +148,7 @@ float getRawMap(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return interpolateMsg("rmap", CONFIGB(mapFrequency0Kpa), 0, CONFIGB(mapFrequency100Kpa), 100, mapFreq); } - float voltage = getVoltageDivided("map", engineConfiguration->map.sensor.hwChannel); + float voltage = getVoltageDivided("map", engineConfiguration->map.sensor.hwChannel PASS_ENGINE_PARAMETER_SUFFIX); return getMapByVoltage(voltage PASS_ENGINE_PARAMETER_SUFFIX); } @@ -168,7 +168,7 @@ float getBaroPressure(DECLARE_ENGINE_PARAMETER_SIGNATURE) { // Override the real Baro sensor with the stored initial MAP value, if the option is set. if (CONFIG(useFixedBaroCorrFromMap)) return storedInitialBaroPressure; - float voltage = getVoltageDivided("baro", engineConfiguration->baroSensor.hwChannel); + float voltage = getVoltageDivided("baro", engineConfiguration->baroSensor.hwChannel PASS_ENGINE_PARAMETER_SUFFIX); return decodePressure(voltage, &engineConfiguration->baroSensor PASS_ENGINE_PARAMETER_SUFFIX); } diff --git a/firmware/controllers/sensors/thermistors.cpp b/firmware/controllers/sensors/thermistors.cpp index 56eed9c448..31abf73e7f 100644 --- a/firmware/controllers/sensors/thermistors.cpp +++ b/firmware/controllers/sensors/thermistors.cpp @@ -89,7 +89,7 @@ temperature_t getTemperatureC(ThermistorConf *cfg, ThermistorMath *tm, bool useL DISPLAY_TEXT(Analog_ECU_reads); #if EFI_UNIT_TEST // todo: get rid of this branch, unify unit test with real firmware. maybe analogInputDividerCoefficient needs to be set? - tm->voltageBoard = getVoltageDivided("term", cfg->adcChannel); + tm->voltageBoard = getVoltageDivided("term", cfg->adcChannel PASS_ENGINE_PARAMETER_SUFFIX); // CONFIG(analogInputDividerCoefficient) = 1; // tm-> ISPLAY_FIELD(voltageBoard) = ISPLAY_TEXT(Rdivider) tm->voltageMCU * CONFIG( ISPLAY_CONFIG(analogInputDividerCoefficient)); #else diff --git a/firmware/controllers/sensors/tps.cpp b/firmware/controllers/sensors/tps.cpp index 224eb18bae..96878731e4 100644 --- a/firmware/controllers/sensors/tps.cpp +++ b/firmware/controllers/sensors/tps.cpp @@ -133,7 +133,7 @@ percent_t getTpsValue(int adc DECLARE_ENGINE_PARAMETER_SUFFIX) { * Return voltage on TPS AND channel * */ float getTPSVoltage(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - return getVoltageDivided("tps", engineConfiguration->tps1_1AdcChannel); + return getVoltageDivided("tps", engineConfiguration->tps1_1AdcChannel PASS_ENGINE_PARAMETER_SUFFIX); } /* @@ -176,7 +176,7 @@ void grabTPSIsWideOpen() { void grabPedalIsUp() { #if EFI_PROD_CODE - float voltage = getVoltageDivided("pPS", engineConfiguration->throttlePedalPositionAdcChannel); + float voltage = getVoltageDivided("pPS", engineConfiguration->throttlePedalPositionAdcChannel PASS_ENGINE_PARAMETER_SUFFIX); engineConfiguration->throttlePedalUpVoltage = voltage; printTPSInfo(); #endif /* EFI_PROD_CODE */ @@ -184,7 +184,7 @@ void grabPedalIsUp() { void grabPedalIsWideOpen() { #if EFI_PROD_CODE - float voltage = getVoltageDivided("pPS", engineConfiguration->throttlePedalPositionAdcChannel); + float voltage = getVoltageDivided("pPS", engineConfiguration->throttlePedalPositionAdcChannel PASS_ENGINE_PARAMETER_SUFFIX); engineConfiguration->throttlePedalWOTVoltage = voltage; printTPSInfo(); #endif /* EFI_PROD_CODE */ @@ -211,7 +211,7 @@ percent_t getPedalPosition(DECLARE_ENGINE_PARAMETER_SIGNATURE) { DISPLAY_TAG(PEDAL_SECTION); DISPLAY_TEXT(Analog_MCU_reads); - float voltage = getVoltageDivided("pPS", CONFIG(DISPLAY_CONFIG(throttlePedalPositionAdcChannel))); + float voltage = getVoltageDivided("pPS", CONFIG(DISPLAY_CONFIG(throttlePedalPositionAdcChannel)) PASS_ENGINE_PARAMETER_SUFFIX); percent_t result = interpolateMsg("pedal", engineConfiguration->throttlePedalUpVoltage, 0, engineConfiguration->throttlePedalWOTVoltage, 100, voltage); // this would put the value into the 0-100 range diff --git a/firmware/controllers/settings.cpp b/firmware/controllers/settings.cpp index 3aabd816b3..8dd70911f9 100644 --- a/firmware/controllers/settings.cpp +++ b/firmware/controllers/settings.cpp @@ -385,7 +385,7 @@ static char pinNameBuffer[16]; static void printThermistor(const char *msg, ThermistorConf *config, ThermistorMath *tm, bool useLinear) { adc_channel_e adcChannel = config->adcChannel; - float voltage = getVoltageDivided("term", adcChannel); + float voltage = getVoltageDivided("term", adcChannel PASS_ENGINE_PARAMETER_SUFFIX); float r = getResistance(config, voltage); float t = getTemperatureC(config, tm, useLinear); diff --git a/firmware/controllers/trigger/main_trigger_callback.cpp b/firmware/controllers/trigger/main_trigger_callback.cpp index 482148f614..b8a23bad09 100644 --- a/firmware/controllers/trigger/main_trigger_callback.cpp +++ b/firmware/controllers/trigger/main_trigger_callback.cpp @@ -520,7 +520,7 @@ void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t trgEventIndex D if (trgEventIndex == CONFIG(ignMathCalculateAtIndex)) { if (CONFIG(externalKnockSenseAdc) != EFI_ADC_NONE) { - float externalKnockValue = getVoltageDivided("knock", engineConfiguration->externalKnockSenseAdc); + float externalKnockValue = getVoltageDivided("knock", engineConfiguration->externalKnockSenseAdc PASS_ENGINE_PARAMETER_SUFFIX); engine->knockLogic(externalKnockValue PASS_ENGINE_PARAMETER_SUFFIX); } } diff --git a/firmware/hw_layer/adc_inputs.cpp b/firmware/hw_layer/adc_inputs.cpp index e83ba1b616..35ecbc3c7f 100644 --- a/firmware/hw_layer/adc_inputs.cpp +++ b/firmware/hw_layer/adc_inputs.cpp @@ -46,6 +46,13 @@ static adc_channel_mode_e adcHwChannelEnabled[HW_MAX_ADC_INDEX]; static const char * adcHwChannelUsage[HW_MAX_ADC_INDEX]; +EXTERN_ENGINE; + +// Board voltage, with divider coefficient accounted for +float getVoltageDivided(const char *msg, adc_channel_e hwChannel DECLARE_ENGINE_PARAMETER_SUFFIX) { + return getVoltage(msg, hwChannel) * engineConfiguration->analogInputDividerCoefficient; +} + AdcDevice::AdcDevice(ADCConversionGroup* hwConfig) { this->hwConfig = hwConfig; channelCount = 0; diff --git a/firmware/hw_layer/algo/adc_math.h b/firmware/hw_layer/algo/adc_math.h index 02f055df61..17daab6f17 100644 --- a/firmware/hw_layer/algo/adc_math.h +++ b/firmware/hw_layer/algo/adc_math.h @@ -11,6 +11,7 @@ #ifndef ADC_MATH_H_ #define ADC_MATH_H_ +#include "global.h" #define ADC_MAX_VALUE 4095 #define adcToVolts(adc) ((engineConfiguration->adcVcc) / ADC_MAX_VALUE * (adc)) @@ -20,7 +21,7 @@ // voltage in MCU universe, from zero to VDD #define getVoltage(msg, hwChannel) (adcToVolts(getAdcValue(msg, hwChannel))) -// Board voltage, with divider coefficient accounted for -#define getVoltageDivided(msg, hwChannel) (getVoltage(msg, hwChannel) * engineConfiguration->analogInputDividerCoefficient) +// DECLARE_ENGINE_PARAMETER_SUFFIX +float getVoltageDivided(const char *msg, adc_channel_e channel); #endif /* ADC_MATH_H_ */ diff --git a/firmware/hw_layer/sensors/cj125.cpp b/firmware/hw_layer/sensors/cj125.cpp index f997cc7089..a969774829 100644 --- a/firmware/hw_layer/sensors/cj125.cpp +++ b/firmware/hw_layer/sensors/cj125.cpp @@ -114,7 +114,7 @@ static float getUr() { return getVoltage("cj125ur", CONFIG(cj125ur)); } else { // if a standard voltage division scheme with OpAmp is used - return getVoltageDivided("cj125ur", CONFIG(cj125ur)); + return getVoltageDivided("cj125ur", CONFIG(cj125ur) PASS_ENGINE_PARAMETER_SUFFIX); } #endif /* EFI_PROD_CODE */ } @@ -129,7 +129,7 @@ static float getUa() { if (CONFIG(cj125ua) != EFI_ADC_NONE) { #if EFI_PROD_CODE if (engineConfiguration->cj125isUaDivided) { - return getVoltageDivided("cj125ua", CONFIG(cj125ua)); + return getVoltageDivided("cj125ua", CONFIG(cj125ua) PASS_ENGINE_PARAMETER_SUFFIX); } else { return getVoltage("cj125ua", CONFIG(cj125ua)); } diff --git a/simulator/simulator/boards.cpp b/simulator/simulator/boards.cpp index 1e84c5bc9a..918718b814 100644 --- a/simulator/simulator/boards.cpp +++ b/simulator/simulator/boards.cpp @@ -9,11 +9,17 @@ #include "boards.h" #include "engine.h" #include "engine_sniffer.h" +#include "adc_math.h" static LoggingWithStorage logger("simulator board"); -extern engine_configuration_s *engineConfiguration; -extern Engine *engine; + +EXTERN_ENGINE; int getAdcValue(const char *msg, int hwChannel) { return engine->engineState.mockAdcState.getMockAdcValue(hwChannel); } + +// Board voltage, with divider coefficient accounted for +float getVoltageDivided(const char *msg, adc_channel_e hwChannel DECLARE_ENGINE_PARAMETER_SUFFIX) { + return getVoltage(msg, hwChannel) * engineConfiguration->analogInputDividerCoefficient; +} diff --git a/unit_tests/adc_inputs.h b/unit_tests/adc_inputs.h index 767a2c1699..068370a19e 100644 --- a/unit_tests/adc_inputs.h +++ b/unit_tests/adc_inputs.h @@ -8,4 +8,6 @@ #ifndef ADC_INPUTS_H_ #define ADC_INPUTS_H_ +#include "boards.h" + #endif /* ADC_INPUTS_H_ */ diff --git a/unit_tests/boards.cpp b/unit_tests/boards.cpp index e69b0dab8f..be3ff04d8d 100644 --- a/unit_tests/boards.cpp +++ b/unit_tests/boards.cpp @@ -11,7 +11,7 @@ float testMafValue = 0; float testCltValue = 0; float testIatValue = 0; -float getVoltageDivided(const char *msg, adc_channel_e channel) { +float getVoltageDivided(const char *msg, adc_channel_e channel DECLARE_ENGINE_PARAMETER_SUFFIX) { switch(channel) { case TEST_MAF_CHANNEL: return testMafValue; diff --git a/unit_tests/boards.h b/unit_tests/boards.h index 2d99a228d6..61819c283f 100644 --- a/unit_tests/boards.h +++ b/unit_tests/boards.h @@ -1,18 +1,18 @@ /* - * boards.h + * @file boards.h * - * Created on: Nov 15, 2013 - * Author: Andrey Belomutskiy, (c) 2012-2015 + * @date Nov 15, 2013 + * @author Andrey Belomutskiy, (c) 2012-2019 */ #ifndef BOARDS_H_ #define BOARDS_H_ -#include "rusefi_enums.h" +#include "engine_configuration.h" #define ADC_CHANNEL_VREF 0 -float getVoltageDivided(const char *msg, adc_channel_e); +float getVoltageDivided(const char *msg, adc_channel_e channel DECLARE_ENGINE_PARAMETER_SUFFIX); float getVoltage(const char *msg, int channel); int getAdcValue(const char *msg, int channel); diff --git a/unit_tests/global.h b/unit_tests/global.h index f90ac9f670..eaaf6a0b88 100644 --- a/unit_tests/global.h +++ b/unit_tests/global.h @@ -15,8 +15,6 @@ #include "common_headers.h" -#include "boards.h" - typedef uint32_t iomode_t; typedef uint32_t ioportid_t; typedef uint32_t ioportmask_t; diff --git a/unit_tests/tests/sensor/lin_sensor.cpp b/unit_tests/tests/sensor/lin_sensor.cpp index 847cf0e972..10eb38d2eb 100644 --- a/unit_tests/tests/sensor/lin_sensor.cpp +++ b/unit_tests/tests/sensor/lin_sensor.cpp @@ -1,6 +1,6 @@ #include "linear_sensor.h" -#include +#include "unit_test_framework.h" class SensorLinear : public ::testing::Test { protected: @@ -21,7 +21,7 @@ protected: auto result = dut.get(); \ \ EXPECT_TRUE(result.Valid); \ - EXPECT_FLOAT_EQ(result.Value, (out)); \ + ASSERT_NEAR(result.Value, (out), EPS4D) << "Not " << out << " for " << in; \ } #define test_point_invalid(in) \ diff --git a/unit_tests/unit_test_framework.h b/unit_tests/unit_test_framework.h index 4ec6f08d8e..808b6c6044 100644 --- a/unit_tests/unit_test_framework.h +++ b/unit_tests/unit_test_framework.h @@ -1,8 +1,8 @@ /* - * unit_test_framework.h + * @file unit_test_framework.h * - * Created on: Mar 4, 2018 - * Author: Andrey Belomutskiy, (c) 2012-2018 + * @date Mar 4, 2018 + * @author Andrey Belomutskiy, (c) 2012-2018 */ #ifndef UNIT_TEST_FRAMEWORK_H_