rusefi/firmware/controllers/engine_cycle/main_trigger_callback.cpp

567 lines
18 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file main_trigger_callback.cpp
* @brief Main logic is here!
*
* See http://rusefi.com/docs/html/
*
* @date Feb 7, 2013
2020-01-07 21:02:40 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2015-07-10 06:01:56 -07:00
*
* This file is part of rusEfi - see http://rusefi.com
*
* rusEfi is free software; you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "pch.h"
#include "os_access.h"
2015-07-10 06:01:56 -07:00
2020-07-19 11:13:15 -07:00
#if EFI_PRINTF_FUEL_DETAILS
bool printFuelDebug = false;
2020-07-19 11:26:28 -07:00
#endif // EFI_PRINTF_FUEL_DETAILS
2020-07-19 11:13:15 -07:00
2019-04-12 19:07:03 -07:00
#if EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT
2015-07-10 06:01:56 -07:00
#include "main_trigger_callback.h"
#include "trigger_central.h"
2016-09-15 15:02:36 -07:00
#include "spark_logic.h"
2015-07-10 06:01:56 -07:00
#include "advance_map.h"
#include "cyclic_buffer.h"
#include "fuel_math.h"
2019-05-05 15:53:34 -07:00
#include "cdm_ion_sense.h"
2020-05-25 21:07:18 -07:00
#include "tooth_logger.h"
2019-07-06 17:15:49 -07:00
#include "os_util.h"
#include "local_version_holder.h"
2015-07-10 06:01:56 -07:00
#include "event_queue.h"
#include "injector_model.h"
#if EFI_LAUNCH_CONTROL
#include "launch_control.h"
#endif
#include "backup_ram.h"
2015-07-10 06:01:56 -07:00
// todo: figure out if this even helps?
//#if defined __GNUC__
//#define RAM_METHOD_PREFIX __attribute__((section(".ram")))
//#else
//#define RAM_METHOD_PREFIX
//#endif
void startSimultaniousInjection(void*) {
2020-07-20 18:20:58 -07:00
efitick_t nowNt = getTimeNowNt();
for (size_t i = 0; i < engineConfiguration->specs.cylindersCount; i++) {
enginePins.injectors[i].open(nowNt);
2015-07-10 06:01:56 -07:00
}
}
static void endSimultaniousInjectionOnlyTogglePins(void* = nullptr) {
efitick_t nowNt = getTimeNowNt();
for (size_t i = 0; i < engineConfiguration->specs.cylindersCount; i++) {
enginePins.injectors[i].close(nowNt);
2018-01-17 20:20:05 -08:00
}
}
2018-03-04 19:45:20 -08:00
void endSimultaniousInjection(InjectionEvent *event) {
event->isScheduled = false;
2018-01-17 20:20:05 -08:00
endSimultaniousInjectionOnlyTogglePins(engine);
engine->injectionEvents.addFuelEventsForCylinder(event->ownIndex);
2015-07-10 06:01:56 -07:00
}
void InjectorOutputPin::open(efitick_t nowNt) {
2020-07-18 23:03:12 -07:00
overlappingCounter++;
2016-09-26 18:03:18 -07:00
2019-04-12 19:07:03 -07:00
#if FUEL_MATH_EXTREME_LOGGING
2020-07-20 10:38:33 -07:00
if (printFuelDebug) {
printf("InjectorOutputPin::open %s %d now=%0.1fms\r\n", name, overlappingCounter, (int)getTimeNowUs() / 1000.0);
2020-07-20 10:38:33 -07:00
}
2016-09-26 18:03:18 -07:00
#endif /* FUEL_MATH_EXTREME_LOGGING */
2020-07-18 23:03:12 -07:00
if (overlappingCounter > 1) {
2016-09-26 18:03:18 -07:00
// /**
// * #299
// * this is another kind of overlap which happens in case of a small duty cycle after a large duty cycle
// */
2019-04-12 19:07:03 -07:00
#if FUEL_MATH_EXTREME_LOGGING
2020-07-20 10:38:33 -07:00
if (printFuelDebug) {
printf("overlapping, no need to touch pin %s %d\r\n", name, (int)getTimeNowUs());
}
2016-09-26 18:03:18 -07:00
#endif /* FUEL_MATH_EXTREME_LOGGING */
2017-05-25 19:31:29 -07:00
} else {
2020-07-20 18:20:58 -07:00
#if EFI_TOOTH_LOGGER
LogTriggerInjectorState(nowNt, true);
2020-07-20 18:20:58 -07:00
#endif // EFI_TOOTH_LOGGER
2020-07-18 23:03:12 -07:00
setHigh();
2017-05-25 19:31:29 -07:00
}
2016-09-26 18:03:18 -07:00
}
2020-01-10 20:17:58 -08:00
void turnInjectionPinHigh(InjectionEvent *event) {
2020-07-20 18:20:58 -07:00
efitick_t nowNt = getTimeNowNt();
2017-05-25 19:31:29 -07:00
for (int i = 0;i < MAX_WIRES_COUNT;i++) {
InjectorOutputPin *output = event->outputs[i];
if (output) {
output->open(nowNt);
2016-11-30 19:06:43 -08:00
}
2016-11-30 17:02:41 -08:00
}
2016-11-30 15:02:19 -08:00
}
void InjectorOutputPin::close(efitick_t nowNt) {
2019-04-12 19:07:03 -07:00
#if FUEL_MATH_EXTREME_LOGGING
2020-07-20 10:38:33 -07:00
if (printFuelDebug) {
printf("InjectorOutputPin::close %s %d %d\r\n", name, overlappingCounter, (int)getTimeNowUs());
}
2016-09-26 18:03:18 -07:00
#endif /* FUEL_MATH_EXTREME_LOGGING */
2020-07-18 23:03:12 -07:00
overlappingCounter--;
if (overlappingCounter > 0) {
2019-04-12 19:07:03 -07:00
#if FUEL_MATH_EXTREME_LOGGING
2020-07-20 10:38:33 -07:00
if (printFuelDebug) {
2020-07-19 13:46:28 -07:00
printf("was overlapping, no need to touch pin %s %d\r\n", name, (int)getTimeNowUs());
2020-07-20 10:38:33 -07:00
}
2016-09-26 18:03:18 -07:00
#endif /* FUEL_MATH_EXTREME_LOGGING */
2020-07-18 23:03:12 -07:00
} else {
2020-07-20 18:20:58 -07:00
#if EFI_TOOTH_LOGGER
LogTriggerInjectorState(nowNt, false);
2020-07-20 18:20:58 -07:00
#endif // EFI_TOOTH_LOGGER
2020-07-18 23:03:12 -07:00
setLow();
}
2020-07-18 23:03:12 -07:00
// Don't allow negative overlap count
if (overlappingCounter < 0) {
overlappingCounter = 0;
}
2016-09-26 18:03:18 -07:00
}
2020-01-10 20:17:58 -08:00
void turnInjectionPinLow(InjectionEvent *event) {
2020-05-25 21:07:18 -07:00
efitick_t nowNt = getTimeNowNt();
engine->mostRecentTimeBetweenIgnitionEvents = nowNt - engine->mostRecentIgnitionEvent;
engine->mostRecentIgnitionEvent = nowNt;
event->isScheduled = false;
2016-11-30 17:02:41 -08:00
for (int i = 0;i<MAX_WIRES_COUNT;i++) {
InjectorOutputPin *output = event->outputs[i];
2020-07-19 13:46:28 -07:00
if (output) {
output->close(nowNt);
2016-11-30 19:06:43 -08:00
}
2016-11-30 17:02:41 -08:00
}
engine->injectionEvents.addFuelEventsForCylinder(event->ownIndex);
2016-11-30 15:02:19 -08:00
}
2020-07-24 18:26:24 -07:00
void InjectionEvent::onTriggerTooth(size_t trgEventIndex, int rpm, efitick_t nowNt) {
uint32_t eventIndex = injectionStart.triggerEventIndex;
// right after trigger change we are still using old & invalid fuel schedule. good news is we do not change trigger on the fly in real life
// efiAssertVoid(CUSTOM_ERR_ASSERT_VOID, eventIndex < engine->triggerShape.getLength(), "handleFuel/event sch index");
2020-07-24 18:26:24 -07:00
if (eventIndex != trgEventIndex) {
return;
}
2016-01-20 14:01:53 -08:00
// Select fuel mass from the correct cylinder
auto injectionMassGrams = engine->injectionMass[this->cylinderNumber];
// Perform wall wetting adjustment on fuel mass, not duration, so that
// it's correct during fuel pressure (injector flow) or battery voltage (deadtime) transients
injectionMassGrams = wallFuel.adjust(injectionMassGrams);
const floatms_t injectionDuration = engine->module<InjectorModel>()->getInjectionDuration(injectionMassGrams);
2019-10-16 21:06:54 -07:00
2019-04-12 19:07:03 -07:00
#if EFI_PRINTF_FUEL_DETAILS
2020-07-19 11:13:15 -07:00
if (printFuelDebug) {
2020-07-19 13:05:19 -07:00
printf("fuel index=%d injectionDuration=%.2fms adjusted=%.2fms\n",
2020-07-24 18:26:24 -07:00
eventIndex,
engine->injectionDuration,
2020-07-19 13:05:19 -07:00
injectionDuration);
2020-07-19 11:13:15 -07:00
}
2016-09-25 21:03:15 -07:00
#endif /*EFI_PRINTF_FUEL_DETAILS */
2015-08-23 20:02:37 -07:00
bool isCranking = engine->rpmCalculator.isCranking();
2017-03-08 22:14:02 -08:00
/**
* todo: pre-calculate 'numberOfInjections'
* see also injectorDutyCycle
*/
int numberOfInjections = isCranking ? getNumberOfInjections(engineConfiguration->crankingInjectionMode) : getNumberOfInjections(engineConfiguration->injectionMode);
if (injectionDuration * numberOfInjections > getEngineCycleDuration(rpm)) {
2018-01-23 09:05:14 -08:00
warning(CUSTOM_TOO_LONG_FUEL_INJECTION, "Too long fuel injection %.2fms", injectionDuration);
2016-07-23 20:03:45 -07:00
}
engine->engineState.fuelConsumption.consumeFuel(injectionMassGrams * numberOfInjections, nowNt);
engine->actualLastInjection[this->cylinderNumber] = injectionDuration;
2015-07-10 06:01:56 -07:00
if (cisnan(injectionDuration)) {
2016-07-17 07:13:22 -07:00
warning(CUSTOM_OBD_NAN_INJECTION, "NaN injection pulse");
2015-07-10 06:01:56 -07:00
return;
}
if (injectionDuration < 0) {
2018-01-23 09:05:14 -08:00
warning(CUSTOM_OBD_NEG_INJECTION, "Negative injection pulse %.2f", injectionDuration);
2015-07-10 06:01:56 -07:00
return;
}
// If somebody commanded an impossibly short injection, do nothing.
// Durations under 50us-ish aren't safe for the scheduler
// as their order may be swapped, resulting in a stuck open injector
2018-06-23 06:37:48 -07:00
// see https://github.com/rusefi/rusefi/pull/596 for more details
if (injectionDuration < 0.050f)
{
return;
}
2017-05-24 19:46:45 -07:00
floatus_t durationUs = MS2US(injectionDuration);
2016-08-05 22:04:28 -07:00
// we are ignoring low RPM in order not to handle "engine was stopped to engine now running" transition
/*
* Wall Wetting would totally skip fuel on sudden deceleration a
if (rpm > 2 * engineConfiguration->cranking.rpm) {
2020-07-24 18:26:24 -07:00
const char *outputName = outputs[0]->name;
if (engine->prevOutputName == outputName
&& engineConfiguration->injectionMode != IM_SIMULTANEOUS
&& engineConfiguration->injectionMode != IM_SINGLE_POINT) {
2020-07-19 12:47:21 -07:00
warning(CUSTOM_OBD_SKIPPED_FUEL, "looks like skipped fuel event revCounter=%d %s", getRevolutionCounter(), outputName);
2016-07-17 17:01:54 -07:00
}
engine->prevOutputName = outputName;
}
*/
2016-07-17 17:01:54 -07:00
2020-07-19 11:13:15 -07:00
#if EFI_PRINTF_FUEL_DETAILS
if (printFuelDebug) {
2020-07-24 18:26:24 -07:00
InjectorOutputPin *output = outputs[0];
2020-07-19 12:47:21 -07:00
printf("handleFuelInjectionEvent fuelout %s injection_duration %dus engineCycleDuration=%.1fms\t\n", output->name, (int)durationUs,
2020-09-03 16:29:15 -07:00
(int)MS2US(getCrankshaftRevolutionTimeMs(GET_RPM())) / 1000.0);
2020-07-19 11:13:15 -07:00
}
#endif /*EFI_PRINTF_FUEL_DETAILS */
2017-05-24 19:46:45 -07:00
2020-07-24 18:26:24 -07:00
if (isScheduled) {
2020-07-19 11:13:15 -07:00
#if EFI_PRINTF_FUEL_DETAILS
if (printFuelDebug) {
2020-07-24 18:26:24 -07:00
InjectorOutputPin *output = outputs[0];
2020-07-19 12:47:21 -07:00
printf("handleFuelInjectionEvent still used %s now=%.1fms\r\n", output->name, (int)getTimeNowUs() / 1000.0);
2020-07-19 11:13:15 -07:00
}
#endif /*EFI_PRINTF_FUEL_DETAILS */
return; // this InjectionEvent is still needed for an extremely long injection scheduled previously
}
2017-05-24 19:46:45 -07:00
2020-07-24 18:26:24 -07:00
isScheduled = true;
2017-05-24 19:46:45 -07:00
action_s startAction, endAction;
// We use different callbacks based on whether we're running sequential mode or not - everything else is the same
2020-07-24 18:26:24 -07:00
if (isSimultanious) {
startAction = startSimultaniousInjection;
2020-07-24 18:26:24 -07:00
endAction = { &endSimultaniousInjection, this };
} else {
// sequential or batch
2020-07-24 18:26:24 -07:00
startAction = { &turnInjectionPinHigh, this };
endAction = { &turnInjectionPinLow, this };
2016-09-03 22:01:54 -07:00
}
efitick_t startTime = scheduleByAngle(&signalTimerUp, nowNt, injectionStart.angleOffsetFromTriggerEvent, startAction);
efitick_t turnOffTime = startTime + US2NT((int)durationUs);
engine->executor.scheduleByTimestampNt("inj", &endOfInjectionEvent, turnOffTime, endAction);
#if EFI_UNIT_TEST
2020-07-24 18:26:24 -07:00
printf("scheduling injection angle=%.2f/delay=%.2f injectionDuration=%.2f\r\n", injectionStart.angleOffsetFromTriggerEvent, NT2US(startTime - nowNt), injectionDuration);
#endif
#if EFI_DEFAILED_LOGGING
efiPrintf("handleFuel pin=%s eventIndex %d duration=%.2fms %d", outputs[0]->name,
injEventIndex,
injectionDuration,
getRevolutionCounter());
efiPrintf("handleFuel pin=%s delay=%.2f %d", outputs[0]->name, NT2US(startTime - nowNt),
getRevolutionCounter());
#endif /* EFI_DEFAILED_LOGGING */
2016-09-03 22:01:54 -07:00
}
static void handleFuel(const bool limitedFuel, uint32_t trgEventIndex, int rpm, efitick_t nowNt) {
2019-10-13 13:14:08 -07:00
ScopePerf perf(PE::HandleFuel);
2019-04-15 17:47:06 -07:00
efiAssertVoid(CUSTOM_STACK_6627, getCurrentRemainingStack() > 128, "lowstck#3");
2018-07-25 20:03:04 -07:00
efiAssertVoid(CUSTOM_ERR_6628, trgEventIndex < engine->engineCycleEventCount, "handleFuel/event index");
2015-07-10 06:01:56 -07:00
engine->tpsAccelEnrichment.onNewValue(Sensor::getOrZero(SensorType::Tps1));
if (trgEventIndex == 0) {
engine->tpsAccelEnrichment.onEngineCycleTps();
}
if (limitedFuel) {
2016-09-03 05:03:07 -07:00
return;
}
if (engine->isCylinderCleanupMode) {
2016-09-20 21:02:15 -07:00
return;
}
// If duty cycle is high, impose a fuel cut rev limiter.
// This is safer than attempting to limp along with injectors or a pump that are out of flow.
if (getInjectorDutyCycle(rpm) > 96.0f) {
2021-09-26 17:58:10 -07:00
warning(CUSTOM_OBD_63, "Injector Duty cycle cut");
return;
}
2016-09-03 05:03:07 -07:00
2015-07-10 06:01:56 -07:00
/**
2020-07-24 18:26:24 -07:00
* Injection events are defined by addFuelEvents() according to selected
2015-07-10 06:01:56 -07:00
* fueling strategy
*/
FuelSchedule *fs = &engine->injectionEvents;
2016-11-30 19:06:43 -08:00
if (!fs->isReady) {
fs->addFuelEvents();
2016-11-30 19:06:43 -08:00
}
2015-07-10 06:01:56 -07:00
2019-04-12 19:07:03 -07:00
#if FUEL_MATH_EXTREME_LOGGING
2020-07-20 10:38:33 -07:00
if (printFuelDebug) {
efiPrintf("handleFuel ind=%d %d", trgEventIndex, getRevolutionCounter());
2020-07-20 10:38:33 -07:00
}
2016-09-03 22:01:54 -07:00
#endif /* FUEL_MATH_EXTREME_LOGGING */
2016-08-05 22:04:28 -07:00
fs->onTriggerTooth(trgEventIndex, rpm, nowNt);
2015-07-10 06:01:56 -07:00
}
2019-04-12 19:07:03 -07:00
#if EFI_PROD_CODE
2015-07-10 06:01:56 -07:00
/**
* this field is used as an Expression in IAR debugger
*/
2016-01-11 14:01:33 -08:00
uint32_t *cyccnt = (uint32_t*) &DWT->CYCCNT;
2015-07-10 06:01:56 -07:00
#endif
static bool noFiringUntilVvtSync(vvt_mode_e vvtMode) {
auto operationMode = engine->getOperationMode();
// V-Twin MAP phase sense needs to always wait for sync
if (vvtMode == VVT_MAP_V_TWIN_ANOTHER) {
return true;
}
// Symmetrical crank modes require cam sync before firing
// non-symmetrical cranks can use faster spin-up mode (firing in wasted/batch before VVT sync)
// Examples include Nissan MR/VQ, Miata NB, etc
return operationMode == FOUR_STROKE_SYMMETRICAL_CRANK_SENSOR || operationMode == FOUR_STROKE_THREE_TIMES_CRANK_SENSOR;
2021-11-24 20:06:52 -08:00
}
2015-07-10 06:01:56 -07:00
/**
* This is the main trigger event handler.
* Both injection and ignition are controlled from this method.
*/
void mainTriggerCallback(uint32_t trgEventIndex, efitick_t edgeTimestamp) {
2019-10-13 13:14:08 -07:00
ScopePerf perf(PE::MainTriggerCallback);
if (noFiringUntilVvtSync(engineConfiguration->vvtMode[0])
&& !engine->triggerCentral.triggerState.hasSynchronizedSymmetrical()) {
// Any engine that requires cam-assistance for a full crank sync (symmetrical crank) can't schedule until we have cam sync
// examples:
// NB2, Nissan VQ/MR: symmetrical crank wheel and we need to make sure no spark happens out of sync
// VTwin Harley: uneven firing order, so we need "cam" MAP sync to make sure no spark happens out of sync
return;
}
2020-09-19 01:39:32 -07:00
#if ! HW_CHECK_MODE
2015-07-10 06:01:56 -07:00
if (hasFirmwareError()) {
/**
* In case on a major error we should not process any more events.
2015-09-27 18:03:09 -07:00
* TODO: add 'pin shutdown' invocation somewhere - coils might be still open here!
2015-07-10 06:01:56 -07:00
*/
return;
}
2020-09-19 01:39:32 -07:00
#endif // HW_CHECK_MODE
2019-05-05 15:53:34 -07:00
#if EFI_CDM_INTEGRATION
if (trgEventIndex == 0 && isBrainPinValid(engineConfiguration->cdmInputPin)) {
2019-05-05 15:53:34 -07:00
int cdmKnockValue = getCurrentCdmValue(engine->triggerCentral.triggerState.getTotalRevolutionCounter());
engine->knockLogic(cdmKnockValue);
}
#endif /* EFI_CDM_INTEGRATION */
if (trgEventIndex >= engine->engineCycleEventCount) {
2016-06-12 13:01:41 -07:00
/**
* this could happen in case of a trigger error, just exit silently since the trigger error is supposed to be handled already
* todo: should this check be somewhere higher so that no trigger listeners are invoked with noise?
*/
return;
}
2020-09-03 16:29:15 -07:00
int rpm = GET_RPM();
2015-07-10 06:01:56 -07:00
if (rpm == 0) {
// this happens while we just start cranking
2015-07-10 06:01:56 -07:00
// todo: check for 'trigger->is_synchnonized?'
2015-09-27 18:03:09 -07:00
// TODO: add 'pin shutdown' invocation somewhere - coils might be still open here!
2015-07-10 06:01:56 -07:00
return;
}
if (rpm == NOISY_RPM) {
warning(OBD_Crankshaft_Position_Sensor_A_Circuit_Malfunction, "noisy trigger");
2015-09-27 18:03:09 -07:00
// TODO: add 'pin shutdown' invocation somewhere - coils might be still open here!
2015-07-10 06:01:56 -07:00
return;
}
2022-01-08 19:13:20 -08:00
LimpState limitedSparkState = engine->limpManager.allowIgnition();
engine->outputChannels.sparkCutReason = (int8_t)limitedSparkState.reason;
bool limitedSpark = !limitedSparkState.value;
LimpState limitedFuelState = engine->limpManager.allowInjection();
engine->outputChannels.fuelCutReason = (int8_t)limitedFuelState.reason;
bool limitedFuel = !limitedFuelState.value;
#if EFI_LAUNCH_CONTROL
2021-11-15 17:09:03 -08:00
if (engine->launchController.isLaunchCondition && !limitedSpark && !limitedFuel) {
/* in case we are not already on a limited conditions, check launch as well */
2021-11-15 16:54:34 -08:00
limitedSpark &= engine->launchController.isLaunchSparkRpmRetardCondition();
limitedFuel &= engine->launchController.isLaunchFuelRpmRetardCondition();
}
#endif
2016-09-04 10:03:39 -07:00
if (trgEventIndex == 0) {
if (HAVE_CAM_INPUT()) {
2019-09-02 11:47:05 -07:00
engine->triggerCentral.validateCamVvtCounters();
}
2016-08-05 22:04:28 -07:00
if (engine->triggerCentral.checkIfTriggerConfigChanged()) {
2019-11-23 12:53:22 -08:00
engine->ignitionEvents.isReady = false; // we need to rebuild complete ignition schedule
2016-12-18 09:03:48 -08:00
engine->injectionEvents.isReady = false;
// moved 'triggerIndexByAngle' into trigger initialization (why was it invoked from here if it's only about trigger shape & optimization?)
// see initializeTriggerWaveform() -> prepareOutputSignals()
2016-08-27 10:01:55 -07:00
// we need this to apply new 'triggerIndexByAngle' values
engine->periodicFastCallback();
2016-04-25 20:01:59 -07:00
}
2015-07-10 06:01:56 -07:00
}
2015-09-26 06:01:32 -07:00
/**
* For fuel we schedule start of injection based on trigger angle, and then inject for
* specified duration of time
*/
handleFuel(limitedFuel, trgEventIndex, rpm, edgeTimestamp);
engine->module<TriggerScheduler>()->scheduleEventsUntilNextTriggerTooth(
rpm, trgEventIndex, edgeTimestamp);
2015-09-26 06:01:32 -07:00
/**
* For spark we schedule both start of coil charge and actual spark based on trigger angle
*/
onTriggerEventSparkLogic(limitedSpark, trgEventIndex, rpm, edgeTimestamp);
2015-07-10 06:01:56 -07:00
}
// Check if the engine is not stopped or cylinder cleanup is activated
static bool isPrimeInjectionPulseSkipped() {
if (!engine->rpmCalculator.isStopped())
return true;
return engineConfiguration->isCylinderCleanupEnabled && (Sensor::getOrZero(SensorType::Tps1) > CLEANUP_MODE_TPS);
}
2018-03-04 13:13:23 -08:00
/**
* Prime injection pulse
2018-03-04 13:13:23 -08:00
*/
void PrimeController::onIgnitionStateChanged(bool ignitionOn) {
if (!ignitionOn) {
// don't prime on ignition-off
return;
}
2020-07-24 18:26:24 -07:00
// 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
2019-04-12 19:07:03 -07:00
#if EFI_PROD_CODE
uint32_t ignSwitchCounter = backupRamLoad(BACKUP_IGNITION_SWITCH_COUNTER);
2018-03-04 20:53:48 -08:00
#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);
engine->executor.scheduleByTimestampNt("prime", &m_start, startTime, { PrimeController::onPrimeStartAdapter, this});
} else {
efiPrintf("Skipped priming pulse since ignSwitchCounter = %d", ignSwitchCounter);
}
2019-04-12 19:07:03 -07:00
#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;
startSimultaniousInjection();
engine->executor.scheduleByTimestampNt("prime", &m_end, endTime, { onPrimeEndAdapter, this });
}
void PrimeController::onPrimeEnd() {
endSimultaniousInjectionOnlyTogglePins();
m_isPriming = false;
}
floatms_t PrimeController::getPrimeDuration() const {
auto clt = Sensor::get(SensorType::Clt);
// If the coolant sensor is dead, skip the prime. The engine will still start fine, but may take a little longer.
if (!clt) {
return 0;
}
auto primeMass =
0.001f * // convert milligram to gram
interpolate2d(clt.Value, engineConfiguration->primeBins, engineConfiguration->primeValues);
return engine->module<InjectorModel>()->getInjectionDuration(primeMass);
}
void updatePrimeInjectionPulseState() {
2019-04-12 19:07:03 -07:00
#if EFI_PROD_CODE
static bool counterWasReset = false;
if (counterWasReset)
return;
if (!engine->rpmCalculator.isStopped()) {
backupRamSave(BACKUP_IGNITION_SWITCH_COUNTER, 0);
counterWasReset = true;
}
#endif /* EFI_PROD_CODE */
}
2019-04-12 19:07:03 -07:00
#if EFI_ENGINE_SNIFFER
2015-07-15 18:01:45 -07:00
#include "engine_sniffer.h"
2015-07-10 06:01:56 -07:00
#endif
static void showMainInfo(Engine *engine) {
2019-04-12 19:07:03 -07:00
#if EFI_PROD_CODE
2019-01-21 18:48:58 -08:00
int rpm = GET_RPM();
float el = getFuelingLoad();
efiPrintf("rpm %d engine_load %.2f", rpm, el);
efiPrintf("fuel %.2fms timing %.2f", engine->injectionDuration, engine->engineState.timingAdvance[0]);
2019-12-23 18:56:16 -08:00
#endif /* EFI_PROD_CODE */
2015-07-10 06:01:56 -07:00
}
void initMainEventListener() {
2019-04-12 19:07:03 -07:00
#if EFI_PROD_CODE
2015-07-10 06:01:56 -07:00
addConsoleActionP("maininfo", (VoidPtr) showMainInfo, engine);
#endif
}
#endif /* EFI_ENGINE_CONTROL */