diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 1b9fae9a7d..d26bbc37af 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -416,14 +416,18 @@ void Engine::efiWatchdog() { void Engine::checkShutdown() { #if EFI_MAIN_RELAY_CONTROL // if we are already in the "ignition_on" mode, then do nothing +/* this logic is not alive if (ignitionOnTimeNt > 0) { return; } +todo: move to shutdown_controller.cpp +*/ // here we are in the shutdown (the ignition is off) or initial mode (after the firmware fresh start) const efitick_t engineStopWaitTimeoutUs = 500000LL; // 0.5 sec // in shutdown mode, we need a small cooldown time between the ignition off and on /* this needs work or tests +todo: move to shutdown_controller.cpp if (stopEngineRequestTimeNt == 0 || (getTimeNowNt() - stopEngineRequestTimeNt) > US2NT(engineStopWaitTimeoutUs)) { // if the ignition key is turned on again, // we cancel the shutdown mode, but only if all shutdown procedures are complete @@ -518,18 +522,6 @@ void Engine::periodicFastCallback() { engine->engineModules.apply_all([](auto & m) { m.onFastCallback(); }); } -void doScheduleStopEngine() { - efiPrintf("Starting doScheduleStopEngine"); - engine->limpManager.stopEngine(); - engine->ignitionOnTimeNt = 0; - // todo: initiate stepper motor parking - // make sure we have stored all the info -#if EFI_PROD_CODE - //todo: FIX kinetis build with this line - //backupRamFlush(); -#endif // EFI_PROD_CODE -} - EngineRotationState * getEngineRotationState() { return &engine->rpmCalculator; } diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index ac014604db..3fcdee0be3 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -231,11 +231,6 @@ public: bool startStopState = false; int startStopStateToggleCounter = 0; - /** - * this is needed by and checkShutdown() - * todo: refactor to Timer? - */ - efitick_t ignitionOnTimeNt = 0; Timer configBurnTimer; @@ -329,7 +324,6 @@ void applyNonPersistentConfiguration(); void prepareOutputSignals(); void validateConfiguration(); -void doScheduleStopEngine(); void scheduleReboot(); bool isLockedFromUser(); void unlockEcu(int password); diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 09fa7d839d..138f1cf8cb 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -213,6 +213,7 @@ angle_t getInjectionOffset(float rpm, float load) { if (cisnan(value)) { // we could be here while resetting configuration for example + // huh? what? when do we have RPM while resetting configuration? is that CI edge case? shall we fix CI? warning(CUSTOM_ERR_6569, "phase map not ready"); return 0; } diff --git a/firmware/controllers/controllers.mk b/firmware/controllers/controllers.mk index c6bff26d0a..b01ba92acc 100644 --- a/firmware/controllers/controllers.mk +++ b/firmware/controllers/controllers.mk @@ -65,6 +65,7 @@ CONTROLLERS_SRC_CPP = \ $(CONTROLLERS_DIR)/tcu/gear_controller.cpp \ $(CONTROLLERS_DIR)/tcu/simple_tcu.cpp \ $(CONTROLLERS_DIR)/tcu/tc_4l6x.cpp \ + $(CONTROLLERS_DIR)/shutdown_controller.cpp \ $(CONTROLLERS_DIR)/limp_manager.cpp \ CONTROLLERS_INC=\ diff --git a/firmware/controllers/engine_cycle/rpm_calculator.cpp b/firmware/controllers/engine_cycle/rpm_calculator.cpp index d03a41be09..24e0596b26 100644 --- a/firmware/controllers/engine_cycle/rpm_calculator.cpp +++ b/firmware/controllers/engine_cycle/rpm_calculator.cpp @@ -82,7 +82,7 @@ bool RpmCalculator::isRunning() const { * @return true if engine is spinning (cranking or running) */ bool RpmCalculator::checkIfSpinning(efitick_t nowNt) const { - if (engine->limpManager.isEngineStop(nowNt)) { + if (engine->limpManager.shutdownController.isEngineStop(nowNt)) { return false; } diff --git a/firmware/controllers/limp_manager.cpp b/firmware/controllers/limp_manager.cpp index ed66c4761c..a261408c19 100644 --- a/firmware/controllers/limp_manager.cpp +++ b/firmware/controllers/limp_manager.cpp @@ -112,7 +112,7 @@ void LimpManager::updateState(int rpm, efitick_t nowNt) { } // If we're in engine stop mode, inhibit fuel - if (isEngineStop(nowNt)) { + if (shutdownController.isEngineStop(nowNt)) { /** * todo: we need explicit clarification on why do we cut fuel but do not cut spark here! */ @@ -173,21 +173,6 @@ void LimpManager::fatalError() { setFaultRevLimit(0); } -void LimpManager::stopEngine() { - m_engineStopTimer.reset(); -} - -bool LimpManager::isEngineStop(efitick_t nowNt) const { - float timeSinceStop = getTimeSinceEngineStop(nowNt); - - // If there was stop requested in the past 5 seconds, we're in stop mode - return timeSinceStop < 5; -} - -float LimpManager::getTimeSinceEngineStop(efitick_t nowNt) const { - return m_engineStopTimer.getElapsedSeconds(nowNt); -} - void LimpManager::setFaultRevLimit(int limit) { // Only allow decreasing the limit // aka uses the limit of the worst fault to yet occur diff --git a/firmware/controllers/limp_manager.h b/firmware/controllers/limp_manager.h index 9792cf6133..582fd4721f 100644 --- a/firmware/controllers/limp_manager.h +++ b/firmware/controllers/limp_manager.h @@ -1,6 +1,6 @@ #pragma once -#include "rusefi_types.h" +#include "shutdown_controller.h" #include @@ -22,6 +22,7 @@ enum class ClearReason : uint8_t { KickStart, // 13 // Keep this list in sync with fuelIgnCutCodeList in rusefi.input! + // todo: add a code generator between ClearReason and fuelIgnCutCodeList in rusefi.input }; // Only allows clearing the value, but never resetting it. @@ -79,6 +80,8 @@ private: class LimpManager { public: + ShutdownController shutdownController; + // This is called from periodicFastCallback to update internal state void updateState(int rpm, efitick_t nowNt); @@ -93,10 +96,6 @@ public: // Other subsystems call these APIs to indicate a problem has occured void etbProblem(); void fatalError(); - void stopEngine(); - - bool isEngineStop(efitick_t nowNt) const; - float getTimeSinceEngineStop(efitick_t nowNt) const; private: void setFaultRevLimit(int limit); @@ -117,8 +116,6 @@ private: Clearable m_transientAllowIgnition = true; bool m_hadOilPressureAfterStart = false; - - Timer m_engineStopTimer; }; LimpManager * getLimpManager(); diff --git a/firmware/controllers/shutdown_controller.cpp b/firmware/controllers/shutdown_controller.cpp new file mode 100644 index 0000000000..5562e8ecc7 --- /dev/null +++ b/firmware/controllers/shutdown_controller.cpp @@ -0,0 +1,19 @@ +/* + * @file shutdown_controller.cpp + * + */ + +#include "shutdown_controller.h" +#include "loggingcentral.h" +#include "limp_manager.h" + +void doScheduleStopEngine() { + efiPrintf("Starting doScheduleStopEngine"); + getLimpManager()->shutdownController.stopEngine(); + // todo: initiate stepper motor parking + // make sure we have stored all the info +#if EFI_PROD_CODE + //todo: FIX kinetis build with this line + //backupRamFlush(); +#endif // EFI_PROD_CODE +} diff --git a/firmware/controllers/shutdown_controller.h b/firmware/controllers/shutdown_controller.h new file mode 100644 index 0000000000..e71641bd87 --- /dev/null +++ b/firmware/controllers/shutdown_controller.h @@ -0,0 +1,38 @@ +/* + * @file shutdown_controller.h + * + */ + +#pragma once + +#include "timer.h" + +void doScheduleStopEngine(); + +class ShutdownController { +public: + void stopEngine() { + m_engineStopTimer.reset(); +// ignitionOnTimeNt = 0; + } + + float getTimeSinceEngineStop(efitick_t nowNt) const { + return m_engineStopTimer.getElapsedSeconds(nowNt); + } + + bool isEngineStop(efitick_t nowNt) const { + float timeSinceStop = getTimeSinceEngineStop(nowNt); + + // If there was stop requested in the past 5 seconds, we're in stop mode + return timeSinceStop < 5; + } + +private: + Timer m_engineStopTimer; + + /** + * this is needed by and checkShutdown() + */ + // is this an unused boolean value? +// efitick_t ignitionOnTimeNt = 0; +};