From d3eab7c3b725390823bdf7560fcff923ce50a633 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 7 Sep 2022 21:29:11 -0400 Subject: [PATCH] brutal encapsulation --- .../engine_cycle/main_trigger_callback.cpp | 65 ------------------- .../engine_cycle/prime_injection.cpp | 64 ++++++++++++++++++ 2 files changed, 64 insertions(+), 65 deletions(-) diff --git a/firmware/controllers/engine_cycle/main_trigger_callback.cpp b/firmware/controllers/engine_cycle/main_trigger_callback.cpp index 6ff4626b09..358b34dc3d 100644 --- a/firmware/controllers/engine_cycle/main_trigger_callback.cpp +++ b/firmware/controllers/engine_cycle/main_trigger_callback.cpp @@ -346,69 +346,4 @@ void mainTriggerCallback(uint32_t trgEventIndex, efitick_t edgeTimestamp, angle_ onTriggerEventSparkLogic(trgEventIndex, rpm, edgeTimestamp); } -// Check if the engine is not stopped or cylinder cleanup is activated -static bool isPrimeInjectionPulseSkipped() { - if (!getEngineRotationState()->isStopped()) - return true; - return engineConfiguration->isCylinderCleanupEnabled && (Sensor::getOrZero(SensorType::Tps1) > CLEANUP_MODE_TPS); -} - -/** - * Prime injection pulse - */ -void PrimeController::onIgnitionStateChanged(bool ignitionOn) { - if (!ignitionOn) { - // don't prime on ignition-off - return; - } - - // First, we need a protection against 'fake' ignition switch on and off (i.e. no engine started), to avoid repeated prime pulses. - // So we check and update the ignition switch counter in non-volatile backup-RAM -#if EFI_PROD_CODE - uint32_t ignSwitchCounter = backupRamLoad(BACKUP_IGNITION_SWITCH_COUNTER); -#else /* EFI_PROD_CODE */ - uint32_t ignSwitchCounter = 0; -#endif /* EFI_PROD_CODE */ - - // if we're just toying with the ignition switch, give it another chance eventually... - if (ignSwitchCounter > 10) - ignSwitchCounter = 0; - // If we're going to skip this pulse, then save the counter as 0. - // That's because we'll definitely need the prime pulse next time (either due to the cylinder cleanup or the engine spinning) - if (isPrimeInjectionPulseSkipped()) - ignSwitchCounter = -1; - // start prime injection if this is a 'fresh start' - if (ignSwitchCounter == 0) { - auto primeDelayMs = engineConfiguration->primingDelay * 1000; - - auto startTime = getTimeNowNt() + MS2NT(primeDelayMs); - getExecutorInterface()->scheduleByTimestampNt("prime", &m_start, startTime, { PrimeController::onPrimeStartAdapter, this}); - } else { - efiPrintf("Skipped priming pulse since ignSwitchCounter = %d", ignSwitchCounter); - } -#if EFI_PROD_CODE - // we'll reset it later when the engine starts - backupRamSave(BACKUP_IGNITION_SWITCH_COUNTER, ignSwitchCounter + 1); -#endif /* EFI_PROD_CODE */ -} - -void PrimeController::onPrimeStart() { - auto durationMs = getPrimeDuration(); - - // Don't prime a zero-duration pulse - if (durationMs <= 0) { - efiPrintf("Skipped zero-duration priming pulse."); - return; - } - - efiPrintf("Firing priming pulse of %.2f ms", durationMs); - - auto endTime = getTimeNowNt() + MS2NT(durationMs); - - // Open all injectors, schedule closing later - m_isPriming = true; - startSimultaneousInjection(); - getExecutorInterface()->scheduleByTimestampNt("prime", &m_end, endTime, { onPrimeEndAdapter, this }); -} - #endif /* EFI_ENGINE_CONTROL */ diff --git a/firmware/controllers/engine_cycle/prime_injection.cpp b/firmware/controllers/engine_cycle/prime_injection.cpp index 186be5cb9e..a8863a673f 100644 --- a/firmware/controllers/engine_cycle/prime_injection.cpp +++ b/firmware/controllers/engine_cycle/prime_injection.cpp @@ -28,6 +28,70 @@ floatms_t PrimeController::getPrimeDuration() const { return engine->module()->getInjectionDuration(primeMass); } +// Check if the engine is not stopped or cylinder cleanup is activated +static bool isPrimeInjectionPulseSkipped() { + if (!getEngineRotationState()->isStopped()) + return true; + return engineConfiguration->isCylinderCleanupEnabled && (Sensor::getOrZero(SensorType::Tps1) > CLEANUP_MODE_TPS); +} + +/** + * Prime injection pulse + */ +void PrimeController::onIgnitionStateChanged(bool ignitionOn) { + if (!ignitionOn) { + // don't prime on ignition-off + return; + } + + // First, we need a protection against 'fake' ignition switch on and off (i.e. no engine started), to avoid repeated prime pulses. + // So we check and update the ignition switch counter in non-volatile backup-RAM +#if EFI_PROD_CODE + uint32_t ignSwitchCounter = backupRamLoad(BACKUP_IGNITION_SWITCH_COUNTER); +#else /* EFI_PROD_CODE */ + uint32_t ignSwitchCounter = 0; +#endif /* EFI_PROD_CODE */ + + // if we're just toying with the ignition switch, give it another chance eventually... + if (ignSwitchCounter > 10) + ignSwitchCounter = 0; + // If we're going to skip this pulse, then save the counter as 0. + // That's because we'll definitely need the prime pulse next time (either due to the cylinder cleanup or the engine spinning) + if (isPrimeInjectionPulseSkipped()) + ignSwitchCounter = -1; + // start prime injection if this is a 'fresh start' + if (ignSwitchCounter == 0) { + auto primeDelayMs = engineConfiguration->primingDelay * 1000; + + auto startTime = getTimeNowNt() + MS2NT(primeDelayMs); + getExecutorInterface()->scheduleByTimestampNt("prime", &m_start, startTime, { PrimeController::onPrimeStartAdapter, this}); + } else { + efiPrintf("Skipped priming pulse since ignSwitchCounter = %d", ignSwitchCounter); + } +#if EFI_PROD_CODE + // we'll reset it later when the engine starts + backupRamSave(BACKUP_IGNITION_SWITCH_COUNTER, ignSwitchCounter + 1); +#endif /* EFI_PROD_CODE */ +} + +void PrimeController::onPrimeStart() { + auto durationMs = getPrimeDuration(); + + // Don't prime a zero-duration pulse + if (durationMs <= 0) { + efiPrintf("Skipped zero-duration priming pulse."); + return; + } + + efiPrintf("Firing priming pulse of %.2f ms", durationMs); + + auto endTime = getTimeNowNt() + MS2NT(durationMs); + + // Open all injectors, schedule closing later + m_isPriming = true; + startSimultaneousInjection(); + getExecutorInterface()->scheduleByTimestampNt("prime", &m_end, endTime, { onPrimeEndAdapter, this }); +} void updatePrimeInjectionPulseState() { static bool counterWasReset = false;