diff --git a/firmware/config/engines/ford_festiva.cpp b/firmware/config/engines/ford_festiva.cpp index c176af2448..21c080c0bd 100644 --- a/firmware/config/engines/ford_festiva.cpp +++ b/firmware/config/engines/ford_festiva.cpp @@ -147,7 +147,7 @@ void setFordEscortGt(DECLARE_CONFIG_PARAMETER_SIGNATURE) { setLinearCurve(config->ignitionLoadBins, 20, 105, 5); setWholeTimingTable_d(10 PASS_CONFIG_PARAMETER_SUFFIX); - setLambdaMap(config->lambdaTable, 0.92); + setTable(config->lambdaTable, 0.92f); setSingleCoilDwell(PASS_CONFIG_PARAMETER_SIGNATURE); engineConfiguration->ignitionMode = IM_ONE_COIL; diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 1389cfc2b3..a8d36ad200 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -458,7 +458,7 @@ void Engine::injectEngineReferences() { void Engine::setConfig(DECLARE_ENGINE_PARAMETER_SIGNATURE) { INJECT_ENGINE_REFERENCE(this); - memset(config, 0, sizeof(persistent_config_s)); + efi::clear(config); injectEngineReferences(); } diff --git a/firmware/controllers/algo/engine_configuration.cpp b/firmware/controllers/algo/engine_configuration.cpp index 8501403b79..1470535bb6 100644 --- a/firmware/controllers/algo/engine_configuration.cpp +++ b/firmware/controllers/algo/engine_configuration.cpp @@ -218,14 +218,6 @@ void setConstantDwell(floatms_t dwellMs DECLARE_CONFIG_PARAMETER_SUFFIX) { setLinearCurve(engineConfiguration->sparkDwellValues, dwellMs, dwellMs, 0.01); } -void setLambdaMap(lambda_table_t table, float value) { - for (int l = 0; l < FUEL_LOAD_COUNT; l++) { - for (int rpmIndex = 0; rpmIndex < FUEL_RPM_COUNT; rpmIndex++) { - table[l][rpmIndex] = (int)(value * PACK_MULT_LAMBDA_CFG); - } - } -} - void setWholeIgnitionIatCorr(float value DECLARE_CONFIG_PARAMETER_SUFFIX) { // todo: make setMap a template setTable(config->ignitionIatCorrTable, value); @@ -257,7 +249,7 @@ static void initTemperatureCurve(float *bins, float *values, int size, float def void prepareVoidConfiguration(engine_configuration_s *engineConfiguration) { efiAssertVoid(OBD_PCM_Processor_Fault, engineConfiguration != NULL, "ec NULL"); - memset(engineConfiguration, 0, sizeof(engine_configuration_s)); + efi::clear(engineConfiguration); engineConfiguration->clutchDownPinMode = PI_PULLUP; engineConfiguration->clutchUpPinMode = PI_PULLUP; @@ -685,7 +677,7 @@ static void setHip9011FrankensoPinout(DECLARE_ENGINE_PARAMETER_SIGNATURE) { */ static void setDefaultEngineConfiguration(DECLARE_ENGINE_PARAMETER_SIGNATURE) { #if (! EFI_UNIT_TEST) - memset(&persistentState.persistentConfiguration, 0, sizeof(persistentState.persistentConfiguration)); + efi::clear(persistentState.persistentConfiguration); #endif prepareVoidConfiguration(engineConfiguration); @@ -804,7 +796,7 @@ static void setDefaultEngineConfiguration(DECLARE_ENGINE_PARAMETER_SIGNATURE) { setLinearCurve(engineConfiguration->map.samplingWindowBins, 800, 7000, 1); setLinearCurve(engineConfiguration->map.samplingWindow, 50, 50, 1); - setLambdaMap(config->lambdaTable, 1.0f); + setTable(config->lambdaTable, 1.0f); engineConfiguration->stoichRatioPrimary = STOICH_RATIO * PACK_MULT_AFR_CFG; engineConfiguration->stoichRatioSecondary = 9.0f * PACK_MULT_AFR_CFG; diff --git a/firmware/controllers/algo/engine_configuration.h b/firmware/controllers/algo/engine_configuration.h index efc14a17d0..57671e670e 100644 --- a/firmware/controllers/algo/engine_configuration.h +++ b/firmware/controllers/algo/engine_configuration.h @@ -26,7 +26,6 @@ void setOperationMode(engine_configuration_s *engineConfiguration, operation_mod void prepareVoidConfiguration(engine_configuration_s *activeConfiguration); void setTargetRpmCurve(int rpm DECLARE_CONFIG_PARAMETER_SUFFIX); -void setLambdaMap(lambda_table_t table, float value); void setWholeIgnitionIatCorr(float value DECLARE_CONFIG_PARAMETER_SUFFIX); void setFuelTablesLoadBin(float minValue, float maxValue DECLARE_CONFIG_PARAMETER_SUFFIX); void setWholeIatCorrTimingTable(float value DECLARE_CONFIG_PARAMETER_SUFFIX); diff --git a/firmware/controllers/algo/rusefi_types.h b/firmware/controllers/algo/rusefi_types.h index 0791eb61bf..e0fbb2f967 100644 --- a/firmware/controllers/algo/rusefi_types.h +++ b/firmware/controllers/algo/rusefi_types.h @@ -94,9 +94,12 @@ typedef char le_formula_t[LE_COMMAND_LENGTH]; typedef brain_pin_e egt_cs_array_t[EGT_CHANNEL_COUNT]; -typedef uint8_t lambda_table_t[FUEL_LOAD_COUNT][FUEL_RPM_COUNT]; -// todo: merge these two types together? but these tables have different TS parameters like ranges etc -typedef float fuel_table_t[FUEL_LOAD_COUNT][FUEL_RPM_COUNT]; +#if __cplusplus +#include "scaled_channel.h" +using ve_table_t = scaled_channel[FUEL_LOAD_COUNT][FUEL_RPM_COUNT]; +using lambda_table_t = scaled_channel[FUEL_LOAD_COUNT][FUEL_RPM_COUNT]; +#endif + typedef uint16_t map_estimate_table_t[FUEL_LOAD_COUNT][FUEL_RPM_COUNT]; typedef float ignition_table_t[IGN_LOAD_COUNT][IGN_RPM_COUNT]; typedef int16_t ignition_tps_table_t[IGN_LOAD_COUNT][IGN_RPM_COUNT]; @@ -130,8 +133,6 @@ typedef float cfg_float_t_1f; typedef brain_pin_e brain_input_pin_e; typedef brain_pin_e switch_input_pin_e; -typedef fuel_table_t ve_table_t; - typedef void (*VoidPtr)(void*); typedef void (*VoidInt)(int); diff --git a/firmware/controllers/math/speed_density.cpp b/firmware/controllers/math/speed_density.cpp index 10659770c2..c02a3c2870 100644 --- a/firmware/controllers/math/speed_density.cpp +++ b/firmware/controllers/math/speed_density.cpp @@ -114,14 +114,14 @@ temperature_t getTCharge(int rpm, float tps DECLARE_ENGINE_PARAMETER_SUFFIX) { void setDefaultVETable(DECLARE_ENGINE_PARAMETER_SIGNATURE) { setRpmTableBin(config->veRpmBins, FUEL_RPM_COUNT); - veMap.setAll(80); + setTable(config->veTable, 80); // setRpmTableBin(engineConfiguration->ve2RpmBins, FUEL_RPM_COUNT); // setLinearCurve(engineConfiguration->ve2LoadBins, 10, 300, 1); // ve2Map.setAll(0.81); setRpmTableBin(config->lambdaRpmBins, FUEL_RPM_COUNT); - lambdaMap.setAll(1.0); + setTable(config->lambdaTable, 1); setRpmTableBin(engineConfiguration->baroCorrRpmBins, BARO_CORR_SIZE); setLinearCurve(engineConfiguration->baroCorrPressureBins, 75, 105, 1); diff --git a/firmware/util/efilib.h b/firmware/util/efilib.h index b3a283e13a..a6aa454421 100644 --- a/firmware/util/efilib.h +++ b/firmware/util/efilib.h @@ -94,6 +94,7 @@ float tanf_taylor(float theta); } #include +#include #define IS_NEGATIVE_ZERO(value) (__builtin_signbit(value) && value==0) #define fixNegativeZero(value) (IS_NEGATIVE_ZERO(value) ? 0 : value) @@ -105,6 +106,21 @@ template constexpr size_t size(const T(&)[N]) { return N; } + +// Zero the passed object +template +constexpr void clear(T* obj) { + // The cast to void* is to prevent errors like: + // clearing an object of non-trivial type 'struct persistent_config_s'; use assignment or value-initialization instead + // This is technically wrong, but we know config objects only ever actually + // contain integral types, though they may be wrapped in a scaled_channel + memset(reinterpret_cast(obj), 0, sizeof(T)); +} + +template +constexpr void clear(T& obj) { + clear(&obj); +} } // namespace efi #define assertIsInBounds(length, array, msg) efiAssertVoid(OBD_PCM_Processor_Fault, (length) >= 0 && (length) < efi::size(array), msg) diff --git a/firmware/util/math/efi_ratio.h b/firmware/util/math/efi_ratio.h index 390bc185ce..6ca8be5468 100644 --- a/firmware/util/math/efi_ratio.h +++ b/firmware/util/math/efi_ratio.h @@ -3,16 +3,14 @@ namespace efi { template -class ratio { -private: +struct ratio { static constexpr int num = TNum; static constexpr int den = TDenom; -public: // A ratio type representing the reciprocal of this type. using recip = ratio; - static float asFloat() { + static constexpr float asFloat() { return (float)num / den; } }; diff --git a/unit_tests/tests/ignition_injection/injection_mode_transition.cpp b/unit_tests/tests/ignition_injection/injection_mode_transition.cpp index 1c1123a74c..93ad6a2ee1 100644 --- a/unit_tests/tests/ignition_injection/injection_mode_transition.cpp +++ b/unit_tests/tests/ignition_injection/injection_mode_transition.cpp @@ -41,8 +41,7 @@ TEST(fuelControl, transitionIssue1592) { engineConfiguration->ambiguousOperationMode = FOUR_STROKE_CAM_SENSOR; engineConfiguration->isFasterEngineSpinUpEnabled = true; - extern fuel_Map3D_t fuelPhaseMap; - fuelPhaseMap.setAll(0); + setTable(config->injectionPhase, 0.0f); setArrayValues(config->crankingFuelCoef, 1.0f); setArrayValues(config->crankingCycleCoef, 1.0f);