From 3ecd2425d50473bb37e0eb682a8076722025328c Mon Sep 17 00:00:00 2001 From: rusEfi Date: Fri, 7 Nov 2014 21:04:45 -0600 Subject: [PATCH] auto-sync --- .../controllers/algo/accel_enrichment.cpp | 4 +- firmware/controllers/engine_controller.cpp | 14 +++-- firmware/controllers/flash_main.cpp | 13 ++-- firmware/controllers/idle_thread.cpp | 2 +- firmware/controllers/map_averaging.cpp | 2 +- .../controllers/map_multiplier_thread.cpp | 2 +- firmware/controllers/math/engine_math.h | 2 +- firmware/controllers/settings.cpp | 60 +++++++++---------- .../trigger/main_trigger_callback.cpp | 46 +++++++------- .../controllers/trigger/rpm_calculator.cpp | 6 +- firmware/controllers/trigger/rpm_calculator.h | 2 +- .../controllers/trigger/trigger_central.cpp | 4 +- .../trigger/trigger_emulator_algo.cpp | 4 +- firmware/emulation/wave_analyzer.cpp | 2 +- firmware/global.h | 19 ++++++ firmware/rusefi.cpp | 20 +++---- unit_tests/global.h | 5 ++ unit_tests/test_trigger_decoder.cpp | 5 -- win32_functional_tests/simulator/global.h | 5 ++ .../simulator/rusEfiFunctionalTest.cpp | 23 +++---- 20 files changed, 138 insertions(+), 102 deletions(-) diff --git a/firmware/controllers/algo/accel_enrichment.cpp b/firmware/controllers/algo/accel_enrichment.cpp index 585942ed99..594a166660 100644 --- a/firmware/controllers/algo/accel_enrichment.cpp +++ b/firmware/controllers/algo/accel_enrichment.cpp @@ -14,7 +14,9 @@ #include "engine_math.h" #include "signal_executor.h" -extern Engine engine; +EXTERN_ENGINE; + + static AccelEnrichmemnt instance; void AccelEnrichmemnt::updateDiffEnrichment(engine_configuration_s *engineConfiguration, float engineLoad) { diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index 014c29eb8a..47b49666cc 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -84,10 +84,14 @@ static engine_configuration2_s ec2 CCM_OPTIONAL ; engine_configuration2_s * engineConfiguration2 = &ec2; +#if (EFI_PROD_CODE || EFI_SIMULATOR) || defined(__DOXYGEN__) + /** * todo: this should probably become 'static', i.e. private, and propagated around explicitly? */ -Engine engine; +static Engine _engine; +Engine * engine = &_engine; +#endif /** * I am not sure if this needs to be configurable. @@ -123,9 +127,9 @@ static void updateErrorCodes(void) { /** * technically we can set error codes right inside the getMethods, but I a bit on a fence about it */ - setError(isValidIntakeAirTemperature(getIntakeAirTemperature(&engine)), + setError(isValidIntakeAirTemperature(getIntakeAirTemperature(engine)), OBD_Intake_Air_Temperature_Circuit_Malfunction); - setError(isValidCoolantTemperature(getCoolantTemperature(&engine)), + setError(isValidCoolantTemperature(getCoolantTemperature(engine)), OBD_Engine_Coolant_Temperature_Circuit_Malfunction); } @@ -137,9 +141,9 @@ static void fanRelayControl(void) { int newValue; if (isCurrentlyOn) { // if the fan is already on, we keep it on till the 'fanOff' temperature - newValue = getCoolantTemperature(&engine) > engineConfiguration->fanOffTemperature; + newValue = getCoolantTemperature(engine) > engineConfiguration->fanOffTemperature; } else { - newValue = getCoolantTemperature(&engine) > engineConfiguration->fanOnTemperature; + newValue = getCoolantTemperature(engine) > engineConfiguration->fanOnTemperature; } if (isCurrentlyOn != newValue) { diff --git a/firmware/controllers/flash_main.cpp b/firmware/controllers/flash_main.cpp index 3c4cd35173..e8891255ee 100644 --- a/firmware/controllers/flash_main.cpp +++ b/firmware/controllers/flash_main.cpp @@ -25,7 +25,7 @@ static bool needToWriteConfiguration = false; -extern Engine engine; +EXTERN_ENGINE; static Logging logger; extern persistent_config_container_s persistentState; @@ -63,6 +63,7 @@ void writeToFlashIfPending() { void writeToFlash(void) { #if EFI_INTERNAL_FLASH + scheduleMsg(&logger, " !!!!!!!!!!!!!!!!!!!! BE SURE NOT WRITE WITH IGNITION ON !!!!!!!!!!!!!!!!!!!!"); persistentState.size = PERSISTENT_SIZE; persistentState.version = FLASH_DATA_VERSION; scheduleMsg(&logger, "flash compatible with %d", persistentState.version); @@ -88,7 +89,7 @@ static bool isValidCrc(persistent_config_container_s *state) { } static void doResetConfiguration(void) { - resetConfigurationExt(&logger, engineConfiguration->engineType, &engine); + resetConfigurationExt(&logger, engineConfiguration->engineType, engine); } static bool hasValidEngineType(engine_configuration_s *engineConfiguration) { @@ -102,16 +103,16 @@ void readFromFlash(void) { if (!isValidCrc(&persistentState)) { printMsg(&logger, "Need to reset flash to default due to CRC"); - resetConfigurationExt(&logger, DEFAULT_ENGINE_TYPE, &engine); + resetConfigurationExt(&logger, DEFAULT_ENGINE_TYPE, engine); } else if (persistentState.version == FLASH_DATA_VERSION && persistentState.size == PERSISTENT_SIZE) { printMsg(&logger, "Got valid configuration from flash!"); - applyNonPersistentConfiguration(&logger, &engine); + applyNonPersistentConfiguration(&logger, engine); } else if (hasValidEngineType(engineConfiguration)) { printMsg(&logger, "Resetting but saving engine type [%d]", engineConfiguration->engineType); - resetConfigurationExt(&logger, engineConfiguration->engineType, &engine); + resetConfigurationExt(&logger, engineConfiguration->engineType, engine); } else { printMsg(&logger, "Need to reset flash to default due to version change"); - resetConfigurationExt(&logger, DEFAULT_ENGINE_TYPE, &engine); + resetConfigurationExt(&logger, DEFAULT_ENGINE_TYPE, engine); } // we can only change the state after the CRC check engineConfiguration->firmwareVersion = getRusEfiVersion(); diff --git a/firmware/controllers/idle_thread.cpp b/firmware/controllers/idle_thread.cpp index da1451a321..29214e67dd 100644 --- a/firmware/controllers/idle_thread.cpp +++ b/firmware/controllers/idle_thread.cpp @@ -43,7 +43,7 @@ extern engine_configuration_s *engineConfiguration; static volatile int idleSwitchState; static Logging logger; -extern Engine engine; +EXTERN_ENGINE; static SimplePwm idleValvePwm; diff --git a/firmware/controllers/map_averaging.cpp b/firmware/controllers/map_averaging.cpp index b182a62c58..6207f46e12 100644 --- a/firmware/controllers/map_averaging.cpp +++ b/firmware/controllers/map_averaging.cpp @@ -64,7 +64,7 @@ static volatile int mapMeasurementsCounter = 0; */ static float v_averagedMapValue; -extern Engine engine; +EXTERN_ENGINE; extern engine_configuration_s *engineConfiguration; diff --git a/firmware/controllers/map_multiplier_thread.cpp b/firmware/controllers/map_multiplier_thread.cpp index b00bd2a9c2..da67805c60 100644 --- a/firmware/controllers/map_multiplier_thread.cpp +++ b/firmware/controllers/map_multiplier_thread.cpp @@ -21,7 +21,7 @@ extern engine_configuration_s *engineConfiguration; static Logging logger; -extern Engine engine; +EXTERN_ENGINE; static THD_WORKING_AREA(maThreadStack, UTILITY_THREAD_STACK_SIZE); diff --git a/firmware/controllers/math/engine_math.h b/firmware/controllers/math/engine_math.h index c9ccd1a564..abc5f9836a 100644 --- a/firmware/controllers/math/engine_math.h +++ b/firmware/controllers/math/engine_math.h @@ -54,7 +54,7 @@ int isCrankingRT(engine_configuration_s *engineConfiguration, int rpm); #define isCrankingR(rpm) isCrankingRT(engineConfiguration, rpm) float getEngineLoadT(Engine *engine); -#define getEngineLoad() getEngineLoadT(&engine) +#define getEngineLoad() getEngineLoadT(engine) float getSparkDwellMsT(engine_configuration_s *engineConfiguration, int rpm); diff --git a/firmware/controllers/settings.cpp b/firmware/controllers/settings.cpp index 8d616adbd6..9626b79ee9 100644 --- a/firmware/controllers/settings.cpp +++ b/firmware/controllers/settings.cpp @@ -182,17 +182,17 @@ static void doPrintConfiguration(Engine *engine) { printConfiguration(engineConfiguration, engine->engineConfiguration2); } -extern Engine engine; +EXTERN_ENGINE; static void setFixedModeTiming(int value) { engineConfiguration->fixedModeTiming = value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); incrementGlobalConfigurationVersion(); } static void setTimingMode(int value) { engineConfiguration->timingMode = (timing_mode_e) value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); incrementGlobalConfigurationVersion(); } @@ -202,13 +202,13 @@ static void setIdleMode(int mode) { void setEngineType(int value) { engineConfiguration->engineType = (engine_type_e) value; - resetConfigurationExt(&logger, (engine_type_e) value, &engine); + resetConfigurationExt(&logger, (engine_type_e) value, engine); #if EFI_INTERNAL_FLASH writeToFlash(); // scheduleReset(); #endif /* EFI_PROD_CODE */ incrementGlobalConfigurationVersion(); - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setIdleSolenoidFrequency(int value) { @@ -217,49 +217,49 @@ static void setIdleSolenoidFrequency(int value) { static void setInjectionPinMode(int value) { boardConfiguration->injectionPinMode = (pin_output_mode_e) value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setIgnitionPinMode(int value) { boardConfiguration->ignitionPinMode = (pin_output_mode_e) value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setIdlePinMode(int value) { boardConfiguration->idleValvePinMode = (pin_output_mode_e) value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setInjectionOffset(int value) { engineConfiguration->injectionOffset = value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); incrementGlobalConfigurationVersion(); } static void setIgnitionOffset(int value) { engineConfiguration->ignitionOffset = value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); incrementGlobalConfigurationVersion(); } static void setFuelPumpPinMode(int value) { boardConfiguration->fuelPumpPinMode = (pin_output_mode_e) value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setMalfunctionIndicatorPinMode(int value) { boardConfiguration->malfunctionIndicatorPinMode = (pin_output_mode_e) value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setAnalogChartMode(int value) { engineConfiguration->analogChartMode = value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setRpmMultiplier(int value) { engineConfiguration->rpmMultiplier = value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static char pinNameBuffer[16]; @@ -314,12 +314,12 @@ static void printTPSInfo(void) { } static void printTemperatureInfo(void) { - printThermistor("CLT", &engine.clt); - if (!isValidCoolantTemperature(getCoolantTemperature(&engine))) { + printThermistor("CLT", &engine->clt); + if (!isValidCoolantTemperature(getCoolantTemperature(engine))) { scheduleMsg(&logger, "CLT sensing error"); } - printThermistor("IAT", &engine.iat); - if (!isValidIntakeAirTemperature(getIntakeAirTemperature(&engine))) { + printThermistor("IAT", &engine->iat); + if (!isValidIntakeAirTemperature(getIntakeAirTemperature(engine))) { scheduleMsg(&logger, "IAT sensing error"); } @@ -330,22 +330,22 @@ static void printTemperatureInfo(void) { static void setCrankingRpm(int value) { engineConfiguration->crankingSettings.crankingRpm = value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setAlgorithm(int value) { engineConfiguration->algorithm = (engine_load_mode_e) value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setFiringOrder(int value) { engineConfiguration->firingOrder = (firing_order_e) value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setRpmHardLimit(int value) { engineConfiguration->rpmHardLimit = value; - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setCrankingFuel(float timeMs) { @@ -356,50 +356,50 @@ static void setCrankingFuel(float timeMs) { static void setGlobalTriggerAngleOffset(int value) { engineConfiguration->globalTriggerAngleOffset = value; incrementGlobalConfigurationVersion(); - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setCrankingTimingAngle(float value) { engineConfiguration->crankingTimingAngle = value; incrementGlobalConfigurationVersion(); - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setCrankingInjectionMode(int value) { engineConfiguration->crankingInjectionMode = (injection_mode_e) value; incrementGlobalConfigurationVersion(); - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setInjectionMode(int value) { engineConfiguration->injectionMode = (injection_mode_e) value; incrementGlobalConfigurationVersion(); - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setIgnitionMode(int value) { engineConfiguration->ignitionMode = (ignition_mode_e) value; incrementGlobalConfigurationVersion(); - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setTriggerType(int value) { engineConfiguration->triggerConfig.triggerType = (trigger_type_e) value; incrementGlobalConfigurationVersion(); - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setToothedWheel(int total, int skipped) { setToothedWheelConfiguration(&engineConfiguration2->triggerShape, total, skipped, engineConfiguration); // initializeTriggerShape(&logger, engineConfiguration, engineConfiguration2); incrementGlobalConfigurationVersion(); - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setCrankingChargeAngle(float value) { engineConfiguration->crankingChargeAngle = value; incrementGlobalConfigurationVersion(); - doPrintConfiguration(&engine); + doPrintConfiguration(engine); } static void setGlobalFuelCorrection(float value) { diff --git a/firmware/controllers/trigger/main_trigger_callback.cpp b/firmware/controllers/trigger/main_trigger_callback.cpp index 2c5b29f12c..12f3e99942 100644 --- a/firmware/controllers/trigger/main_trigger_callback.cpp +++ b/firmware/controllers/trigger/main_trigger_callback.cpp @@ -54,6 +54,8 @@ #include "event_queue.h" #include "engine.h" +EXTERN_ENGINE; + static LocalVersionHolder localVersion; static MainTriggerCallback mainTriggerCallbackInstance; @@ -75,12 +77,12 @@ static cyclic_buffer ignitionErrorDetection; static Logging logger; -static void handleFuelInjectionEvent(MainTriggerCallback *mainTriggerCallback, ActuatorEvent *event, int rpm) { +static INLINE void handleFuelInjectionEvent(ActuatorEvent *event, int rpm DECLATE_ENGINE_PARAMETER) { /** * todo: we do not really need to calculate fuel for each individual cylinder */ - float fuelMs = getFuelMs(rpm, mainTriggerCallback->engine) - * mainTriggerCallback->engineConfiguration->globalFuelCorrection; + float fuelMs = getFuelMs(rpm, engine) + * engine->engineConfiguration->globalFuelCorrection; if (cisnan(fuelMs)) { warning(OBD_PCM_Processor_Fault, "NaN injection pulse"); return; @@ -90,7 +92,7 @@ static void handleFuelInjectionEvent(MainTriggerCallback *mainTriggerCallback, A return; } - if (mainTriggerCallback->engine->isCylinderCleanupMode) + if (engine->isCylinderCleanupMode) return; float delay = getOneDegreeTimeMs(rpm) * event->position.angleOffset; @@ -101,11 +103,11 @@ static void handleFuelInjectionEvent(MainTriggerCallback *mainTriggerCallback, A scheduleOutput(event->actuator, delay, fuelMs); } -static void handleFuel(Engine *engine, MainTriggerCallback *mainTriggerCallback, uint32_t eventIndex, int rpm) { - if (!isInjectionEnabled(mainTriggerCallback->engineConfiguration)) +static INLINE void handleFuel(uint32_t eventIndex, int rpm DECLATE_ENGINE_PARAMETER) { + if (!isInjectionEnabled(engine->engineConfiguration)) return; efiAssertVoid(getRemainingStack(chThdSelf()) > 64, "lowstck#3"); - efiAssertVoid(eventIndex < mainTriggerCallback->engineConfiguration2->triggerShape.getLength(), "event index"); + efiAssertVoid(eventIndex < engine->engineConfiguration2->triggerShape.getLength(), "event index"); engine_configuration_s *engineConfiguration = engine->engineConfiguration; /** @@ -114,8 +116,8 @@ static void handleFuel(Engine *engine, MainTriggerCallback *mainTriggerCallback, */ FuelSchedule *fs = isCrankingR(rpm) ? - &mainTriggerCallback->engineConfiguration2->crankingInjectionEvents : - &mainTriggerCallback->engineConfiguration2->injectionEvents; + &engine->engineConfiguration2->crankingInjectionEvents : + &engine->engineConfiguration2->injectionEvents; ActuatorEventList *source = &fs->events; @@ -123,14 +125,14 @@ static void handleFuel(Engine *engine, MainTriggerCallback *mainTriggerCallback, ActuatorEvent *event = &source->events[i]; if (event->position.eventIndex != eventIndex) continue; - handleFuelInjectionEvent(mainTriggerCallback, event, rpm); + handleFuelInjectionEvent(event, rpm PASS_ENGINE_PARAMETER); } } -static void handleSparkEvent(MainTriggerCallback *mainTriggerCallback, uint32_t eventIndex, IgnitionEvent *iEvent, - int rpm) { - engine_configuration_s *engineConfiguration = mainTriggerCallback->engineConfiguration; - engine_configuration2_s *engineConfiguration2 = mainTriggerCallback->engineConfiguration2; +static INLINE void handleSparkEvent(uint32_t eventIndex, IgnitionEvent *iEvent, + int rpm DECLATE_ENGINE_PARAMETER) { + engine_configuration_s *engineConfiguration = engine->engineConfiguration; + engine_configuration2_s *engineConfiguration2 = engine->engineConfiguration2; float dwellMs = getSparkDwellMsT(engineConfiguration, rpm); if (cisnan(dwellMs) || dwellMs < 0) { @@ -192,9 +194,9 @@ static void handleSparkEvent(MainTriggerCallback *mainTriggerCallback, uint32_t } } -static void handleSpark(MainTriggerCallback *mainTriggerCallback, uint32_t eventIndex, int rpm, - IgnitionEventList *list) { - if (!isValidRpm(rpm) || !mainTriggerCallback->engineConfiguration->isIgnitionEnabled) +static INLINE void handleSpark(uint32_t eventIndex, int rpm, + IgnitionEventList *list DECLATE_ENGINE_PARAMETER) { + if (!isValidRpm(rpm) || !engine->engineConfiguration->isIgnitionEnabled) return; // this might happen for instance in case of a single trigger event after a pause /** @@ -223,7 +225,7 @@ static void handleSpark(MainTriggerCallback *mainTriggerCallback, uint32_t event IgnitionEvent *event = &list->events[i]; if (event->dwellPosition.eventIndex != eventIndex) continue; - handleSparkEvent(mainTriggerCallback, eventIndex, event, rpm); + handleSparkEvent(eventIndex, event, rpm PASS_ENGINE_PARAMETER); } } @@ -303,9 +305,11 @@ void onTriggerEvent(trigger_event_e ckpSignalType, uint32_t eventIndex, MainTrig triggerEventsQueue.executeAll(getCrankEventCounter()); - handleFuel(mainTriggerCallback->engine, mainTriggerCallback, eventIndex, rpm); - handleSpark(mainTriggerCallback, eventIndex, rpm, - &mainTriggerCallback->engineConfiguration2->ignitionEvents[revolutionIndex]); + Engine *engine = mainTriggerCallback->engine; + + handleFuel(eventIndex, rpm PASS_ENGINE_PARAMETER); + handleSpark(eventIndex, rpm, + &mainTriggerCallback->engineConfiguration2->ignitionEvents[revolutionIndex] PASS_ENGINE_PARAMETER); #if EFI_HISTOGRAMS && EFI_PROD_CODE int diff = hal_lld_get_counter_value() - beforeCallback; if (diff > 0) diff --git a/firmware/controllers/trigger/rpm_calculator.cpp b/firmware/controllers/trigger/rpm_calculator.cpp index 7d661693bc..991f071643 100644 --- a/firmware/controllers/trigger/rpm_calculator.cpp +++ b/firmware/controllers/trigger/rpm_calculator.cpp @@ -53,7 +53,7 @@ extern engine_configuration2_s *engineConfiguration2; #if EFI_PROD_CODE || EFI_SIMULATOR static Logging logger; -extern Engine engine; +EXTERN_ENGINE; #endif RpmCalculator::RpmCalculator() { @@ -117,7 +117,7 @@ bool isCrankingE(Engine *engine) { * WARNING: this is a heavy method because 'getRpm()' is relatively heavy */ bool isCranking(void) { - return isCrankingE(&engine); + return isCrankingE(engine); } #endif @@ -223,7 +223,7 @@ float getCrankshaftAngleNt(uint64_t timeNt) { void initRpmCalculator(void) { #if (EFI_PROD_CODE || EFI_SIMULATOR) || defined(__DOXYGEN__) initLogging(&logger, "rpm calc"); - engine.rpmCalculator = &rpmState; + engine->rpmCalculator = &rpmState; tdcScheduler[0].name = "tdc0"; tdcScheduler[1].name = "tdc1"; diff --git a/firmware/controllers/trigger/rpm_calculator.h b/firmware/controllers/trigger/rpm_calculator.h index 47104fbcfc..2397dfc838 100644 --- a/firmware/controllers/trigger/rpm_calculator.h +++ b/firmware/controllers/trigger/rpm_calculator.h @@ -47,7 +47,7 @@ private: volatile uint32_t revolutionCounterSinceStart; }; -#define getRpm() getRpmE(&engine) +#define getRpm() getRpmE(engine) /** * @brief Current RPM diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index b722672db9..a983175902 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -58,11 +58,11 @@ void addTriggerEventListener(ShaftPositionListener listener, const char *name, v } #if (EFI_PROD_CODE || EFI_SIMULATOR) || defined(__DOXYGEN__) -extern Engine engine; +EXTERN_ENGINE; void hwHandleShaftSignal(trigger_event_e signal) { efiAssertVoid(getRemainingStack(chThdSelf()) > 64, "lowstck#8"); - triggerCentral.handleShaftSignal(&engine, signal); + triggerCentral.handleShaftSignal(engine, signal); } #endif /* EFI_PROD_CODE */ diff --git a/firmware/controllers/trigger/trigger_emulator_algo.cpp b/firmware/controllers/trigger/trigger_emulator_algo.cpp index 72a476710b..02217c3b6b 100644 --- a/firmware/controllers/trigger/trigger_emulator_algo.cpp +++ b/firmware/controllers/trigger/trigger_emulator_algo.cpp @@ -70,7 +70,7 @@ static bool isEmulating = true; static Logging logger; static LocalVersionHolder localVersion; -extern Engine engine; +EXTERN_ENGINE; void setTriggerEmulatorRPM(int rpm, Engine *engine) { engineConfiguration->bc.triggerSimulatorFrequency = rpm; @@ -92,7 +92,7 @@ static void updateTriggerShapeIfNeeded(PwmConfig *state) { scheduleMsg(&logger, "Stimulator: updating trigger shape: %d/%d %d", localVersion.getVersion(), getGlobalConfigurationVersion(), currentTimeMillis()); - applyNonPersistentConfiguration(&logger, &engine); + applyNonPersistentConfiguration(&logger, engine); trigger_shape_s *s = &engineConfiguration2->triggerShape; int *pinStates[PWM_PHASE_MAX_WAVE_PER_PWM] = { s->wave.waves[0].pinStates, s->wave.waves[1].pinStates, diff --git a/firmware/emulation/wave_analyzer.cpp b/firmware/emulation/wave_analyzer.cpp index ae65e9d529..a866e95874 100644 --- a/firmware/emulation/wave_analyzer.cpp +++ b/firmware/emulation/wave_analyzer.cpp @@ -24,7 +24,7 @@ #include "engine.h" #include "rpm_calculator.h" -extern Engine engine; +EXTERN_ENGINE; #define CHART_RESET_DELAY 1 diff --git a/firmware/global.h b/firmware/global.h index 41e0977757..97430ff861 100644 --- a/firmware/global.h +++ b/firmware/global.h @@ -50,4 +50,23 @@ typedef Thread thread_t; #define THD_WORKING_AREA WORKING_AREA +/** + * The following obscurantism is a hack to reduce stack usage, maybe even a questionable performance + * optimization. + * + * rusEfi main processing happends on IRQ so PORT_INT_REQUIRED_STACK has to be pretty large. Problem + * is that PORT_INT_REQUIRED_STACK is included within each user thread stack, thus this large stack multiplies + * and this consumes a lot of valueable RAM. While forcing the comiler to inline helps to some degree, + * it would be even better not to waste stack on passing the parameter. + * + * In the firmware we are using 'extern *Engine' - in the firmware Engine is a signleton + * + * On the other hand, in order to have a meaningful unit test we are passing Engine * engine as a parameter + */ + +#define EXTERN_ENGINE extern Engine *engine; + +#define DECLATE_ENGINE_PARAMETER +#define PASS_ENGINE_PARAMETER + #endif /* GLOBAL_H_ */ diff --git a/firmware/rusefi.cpp b/firmware/rusefi.cpp index 1b827f55e4..6102cabceb 100644 --- a/firmware/rusefi.cpp +++ b/firmware/rusefi.cpp @@ -131,7 +131,7 @@ static bool hasFirmwareErrorFlag = FALSE; extern engine_configuration_s *engineConfiguration; extern board_configuration_s *boardConfiguration; extern engine_configuration2_s *engineConfiguration2; -extern Engine engine; +EXTERN_ENGINE; char *getFirmwareError(void) { return (char*)errorMessageBuffer; @@ -141,8 +141,8 @@ void runRusEfi(void) { msObjectInit(&firmwareErrorMessageStream, errorMessageBuffer, sizeof(errorMessageBuffer), 0); // that's dirty, this assignment should be nicer or in a better spot - engine.engineConfiguration = engineConfiguration; - engine.engineConfiguration2 = engineConfiguration2; + engine->engineConfiguration = engineConfiguration; + engine->engineConfiguration2 = engineConfiguration2; engineConfiguration2->engineConfiguration = engineConfiguration; @@ -159,30 +159,30 @@ void runRusEfi(void) { initializeConsole(); initLogging(&logging, "main"); - engine.init(); + engine->init(); addConsoleAction("reset", scheduleReset); /** * Initialize hardware drivers */ - initHardware(&logging, &engine); + initHardware(&logging, engine); - initStatusLoop(&engine); + initStatusLoop(engine); /** * Now let's initialize actual engine control logic * todo: should we initialize some? most? controllers before hardware? */ - initEngineContoller(&engine); + initEngineContoller(engine); #if EFI_PERF_METRICS initTimePerfActions(); #endif #if EFI_ENGINE_EMULATOR - initEngineEmulator(&engine); + initEngineEmulator(engine); #endif - startStatusThreads(&engine); + startStatusThreads(engine); print("Running main loop\r\n"); main_loop_started = TRUE; @@ -195,7 +195,7 @@ void runRusEfi(void) { #if EFI_CLI_SUPPORT && !EFI_UART_ECHO_TEST_MODE // sensor state + all pending messages for our own dev console - updateDevConsoleState(&engine); + updateDevConsoleState(engine); #endif /* EFI_CLI_SUPPORT */ chThdSleepMilliseconds(boardConfiguration->consoleLoopPeriod); diff --git a/unit_tests/global.h b/unit_tests/global.h index c1055d6273..017224ba1d 100644 --- a/unit_tests/global.h +++ b/unit_tests/global.h @@ -32,4 +32,9 @@ typedef void * Logging; #define CCM_OPTIONAL +#define EXTERN_ENGINE + +#define DECLATE_ENGINE_PARAMETER , Engine *engine +#define PASS_ENGINE_PARAMETER , engine + #endif /* GLOBAL_H_ */ diff --git a/unit_tests/test_trigger_decoder.cpp b/unit_tests/test_trigger_decoder.cpp index 00a8997b7e..3f1bb1c805 100644 --- a/unit_tests/test_trigger_decoder.cpp +++ b/unit_tests/test_trigger_decoder.cpp @@ -26,8 +26,6 @@ #include "engine_test_helper.h" #include "speed_density.h" -//Engine engine; - extern int timeNow; extern bool printGapRatio; @@ -338,9 +336,6 @@ void testGY6_139QMB(void) { extern EventQueue schedulingQueue; -// this is a very dirty and sad hack. todo: eliminate -extern Engine engine; - extern int mockTps; static void testStartupFuelPumping(void) { diff --git a/win32_functional_tests/simulator/global.h b/win32_functional_tests/simulator/global.h index 724ded8c2b..f48a9743d9 100644 --- a/win32_functional_tests/simulator/global.h +++ b/win32_functional_tests/simulator/global.h @@ -85,3 +85,8 @@ void unlockAnyContext(void); typedef VirtualTimer virtual_timer_t; typedef EventListener event_listener_t; #define THD_WORKING_AREA WORKING_AREA + +#define EXTERN_ENGINE extern Engine *engine; + +#define DECLATE_ENGINE_PARAMETER +#define PASS_ENGINE_PARAMETER diff --git a/win32_functional_tests/simulator/rusEfiFunctionalTest.cpp b/win32_functional_tests/simulator/rusEfiFunctionalTest.cpp index 10ef85c41a..c9b90e8ccc 100644 --- a/win32_functional_tests/simulator/rusEfiFunctionalTest.cpp +++ b/win32_functional_tests/simulator/rusEfiFunctionalTest.cpp @@ -29,7 +29,8 @@ #include "tunerstudio.h" #include "trigger_emulator.h" -Engine engine; +static Engine _engine; +Engine *engine = &_engine; extern WaveChart waveChart; @@ -63,34 +64,34 @@ void rusEfiFunctionalTest(void) { initFakeBoard(); - initStatusLoop(&engine); + initStatusLoop(engine); initDataStructures(engineConfiguration); - engine.engineConfiguration = engineConfiguration; - engine.engineConfiguration2 = engineConfiguration2; + engine->engineConfiguration = engineConfiguration; + engine->engineConfiguration2 = engineConfiguration2; - resetConfigurationExt(NULL, FORD_ASPIRE_1996, &engine); + resetConfigurationExt(NULL, FORD_ASPIRE_1996, engine); - initThermistors(&engine); + initThermistors(engine); initAlgo(engineConfiguration); initRpmCalculator(); initAnalogChart(); - initTriggerEmulator(&engine); + initTriggerEmulator(engine); - initMainEventListener(&engine, engineConfiguration2); + initMainEventListener(engine, engineConfiguration2); - initTriggerCentral(&engine); + initTriggerCentral(engine); - startStatusThreads(&engine); + startStatusThreads(engine); startTunerStudioConnectivity(); } void printPendingMessages(void) { - updateDevConsoleState(&engine); + updateDevConsoleState(engine); waveChart.publishChartIfFull(); }