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/>.
|
|
|
|
*/
|
|
|
|
|
2018-09-16 19:26:57 -07:00
|
|
|
#include "global.h"
|
2019-07-04 00:57:21 -07:00
|
|
|
#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"
|
2019-03-29 06:11:13 -07:00
|
|
|
#include "efi_gpio.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "engine_math.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 "rpm_calculator.h"
|
|
|
|
#include "engine_configuration.h"
|
|
|
|
#include "interpolation.h"
|
|
|
|
#include "advance_map.h"
|
|
|
|
#include "allsensors.h"
|
|
|
|
#include "cyclic_buffer.h"
|
|
|
|
#include "fuel_math.h"
|
2019-05-05 15:53:34 -07:00
|
|
|
#include "cdm_ion_sense.h"
|
2018-01-31 05:56:13 -08:00
|
|
|
#include "engine_controller.h"
|
2019-03-29 06:11:13 -07:00
|
|
|
#include "efi_gpio.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"
|
2019-03-31 13:56:13 -07:00
|
|
|
#include "local_version_holder.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "event_queue.h"
|
|
|
|
#include "engine.h"
|
2019-10-13 13:14:08 -07:00
|
|
|
#include "perf_trace.h"
|
2020-04-05 06:11:25 -07:00
|
|
|
#include "sensor.h"
|
2019-03-31 14:44:34 -07:00
|
|
|
|
2017-12-23 11:15:35 -08:00
|
|
|
#include "backup_ram.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-03-29 16:06:03 -07:00
|
|
|
EXTERN_ENGINE;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-09-22 05:22:35 -07:00
|
|
|
static const char *prevOutputName = nullptr;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-12-23 11:15:35 -08:00
|
|
|
static InjectionEvent primeInjEvent;
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
static Logging *logger;
|
|
|
|
|
|
|
|
// todo: figure out if this even helps?
|
|
|
|
//#if defined __GNUC__
|
|
|
|
//#define RAM_METHOD_PREFIX __attribute__((section(".ram")))
|
|
|
|
//#else
|
|
|
|
//#define RAM_METHOD_PREFIX
|
|
|
|
//#endif
|
|
|
|
|
2018-03-04 19:32:02 -08:00
|
|
|
void startSimultaniousInjection(Engine *engine) {
|
2019-01-04 21:11:17 -08:00
|
|
|
for (int i = 0; i < engine->engineConfigurationPtr->specs.cylindersCount; i++) {
|
2020-07-19 13:46:28 -07:00
|
|
|
enginePins.injectors[i].open();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:45:20 -08:00
|
|
|
static void endSimultaniousInjectionOnlyTogglePins(Engine *engine) {
|
2019-01-04 21:11:17 -08:00
|
|
|
for (int i = 0; i < engine->engineConfigurationPtr->specs.cylindersCount; i++) {
|
2020-07-19 13:46:28 -07:00
|
|
|
enginePins.injectors[i].close();
|
2018-01-17 20:20:05 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:45:20 -08:00
|
|
|
void endSimultaniousInjection(InjectionEvent *event) {
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_UNIT_TEST
|
2016-11-30 19:06:43 -08:00
|
|
|
Engine *engine = event->engine;
|
|
|
|
EXPAND_Engine;
|
|
|
|
#endif
|
2020-01-10 23:26:20 -08:00
|
|
|
event->isScheduled = false;
|
|
|
|
|
2018-01-17 20:20:05 -08:00
|
|
|
endSimultaniousInjectionOnlyTogglePins(engine);
|
2017-05-15 20:28:49 -07:00
|
|
|
engine->injectionEvents.addFuelEventsForCylinder(event->ownIndex PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2020-07-19 13:46:28 -07:00
|
|
|
void InjectorOutputPin::open() {
|
|
|
|
overlappingCounter++;
|
2016-09-26 18:03:18 -07:00
|
|
|
|
2019-04-12 19:07:03 -07:00
|
|
|
#if FUEL_MATH_EXTREME_LOGGING
|
2020-07-19 13:46:28 -07:00
|
|
|
printf("turnInjectionPinHigh %s %d %d\r\n", name, overlappingCounter, (int)getTimeNowUs());
|
2016-09-26 18:03:18 -07:00
|
|
|
#endif /* FUEL_MATH_EXTREME_LOGGING */
|
|
|
|
|
2020-07-19 13:46:28 -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
|
2016-09-26 18:03:18 -07:00
|
|
|
printf("overlapping, no need to touch pin %s %d\r\n", output->name, (int)getTimeNowUs());
|
|
|
|
#endif /* FUEL_MATH_EXTREME_LOGGING */
|
2017-05-25 19:31:29 -07:00
|
|
|
} else {
|
2020-07-19 13:46:28 -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-05-25 21:07:18 -07:00
|
|
|
efitick_t nowNt = getTimeNowNt();
|
|
|
|
|
|
|
|
#if EFI_TOOTH_LOGGER
|
|
|
|
LogTriggerInjectorState(nowNt, true PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
#endif // EFI_TOOTH_LOGGER
|
|
|
|
|
2017-05-25 19:31:29 -07:00
|
|
|
for (int i = 0;i < MAX_WIRES_COUNT;i++) {
|
2019-10-07 23:56:19 -07:00
|
|
|
InjectorOutputPin *output = event->outputs[i];
|
2020-01-10 19:29:42 -08:00
|
|
|
|
|
|
|
if (output) {
|
2020-07-19 13:46:28 -07:00
|
|
|
output->open();
|
2016-11-30 19:06:43 -08:00
|
|
|
}
|
2016-11-30 17:02:41 -08:00
|
|
|
}
|
2016-11-30 15:02:19 -08:00
|
|
|
}
|
|
|
|
|
2020-07-19 13:46:28 -07:00
|
|
|
void InjectorOutputPin::close() {
|
2019-04-12 19:07:03 -07:00
|
|
|
#if FUEL_MATH_EXTREME_LOGGING
|
2020-07-19 13:46:28 -07:00
|
|
|
printf("turnInjectionPinLow %s %d %d\r\n", name, overlappingCounter, (int)getTimeNowUs());
|
2016-09-26 18:03:18 -07:00
|
|
|
#endif /* FUEL_MATH_EXTREME_LOGGING */
|
|
|
|
|
2020-07-19 13:46:28 -07:00
|
|
|
overlappingCounter--;
|
|
|
|
if (overlappingCounter > 0) {
|
2019-04-12 19:07:03 -07:00
|
|
|
#if FUEL_MATH_EXTREME_LOGGING
|
2020-07-19 13:46:28 -07:00
|
|
|
printf("was 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 */
|
2020-07-19 13:46:28 -07:00
|
|
|
} else {
|
|
|
|
setLow();
|
|
|
|
}
|
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();
|
|
|
|
|
|
|
|
#if EFI_TOOTH_LOGGER
|
|
|
|
LogTriggerInjectorState(nowNt, false PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
#endif // EFI_TOOTH_LOGGER
|
|
|
|
|
2019-10-07 23:56:19 -07:00
|
|
|
event->isScheduled = false;
|
2016-11-30 17:02:41 -08:00
|
|
|
for (int i = 0;i<MAX_WIRES_COUNT;i++) {
|
2019-10-07 23:56:19 -07:00
|
|
|
InjectorOutputPin *output = event->outputs[i];
|
2020-07-19 13:46:28 -07:00
|
|
|
if (output) {
|
|
|
|
output->close();
|
2016-11-30 19:06:43 -08:00
|
|
|
}
|
2016-11-30 17:02:41 -08:00
|
|
|
}
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_UNIT_TEST
|
2019-10-07 23:56:19 -07:00
|
|
|
Engine *engine = event->engine;
|
2016-11-30 20:02:42 -08:00
|
|
|
EXPAND_Engine;
|
2016-11-30 19:06:43 -08:00
|
|
|
#endif
|
2019-10-07 23:56:19 -07:00
|
|
|
ENGINE(injectionEvents.addFuelEventsForCylinder(event->ownIndex PASS_ENGINE_PARAMETER_SUFFIX));
|
2016-11-30 15:02:19 -08:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:25:49 -07:00
|
|
|
// todo: rename to 'scheduleInjectorOpenAndClose'?
|
2020-05-14 04:44:32 -07:00
|
|
|
void handleFuelInjectionEvent(int injEventIndex, InjectionEvent *event,
|
2020-01-19 21:12:18 -08:00
|
|
|
int rpm, efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2016-01-20 14:01:53 -08:00
|
|
|
|
2015-08-23 20:02:37 -07:00
|
|
|
/**
|
|
|
|
* todo: this is a bit tricky with batched injection. is it? Does the same
|
|
|
|
* wetting coefficient works the same way for any injection mode, or is something
|
|
|
|
* x2 or /2?
|
|
|
|
*/
|
2019-10-16 21:06:54 -07:00
|
|
|
|
|
|
|
size_t injectorIndex = event->outputs[0]->injectorIndex;
|
|
|
|
const floatms_t injectionDuration = ENGINE(wallFuel[injectorIndex]).adjust(ENGINE(injectionDuration) PASS_ENGINE_PARAMETER_SUFFIX);
|
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",
|
|
|
|
injEventIndex,
|
|
|
|
ENGINE(injectionDuration),
|
|
|
|
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
|
|
|
|
2017-07-06 14:26:32 -07:00
|
|
|
bool isCranking = ENGINE(rpmCalculator).isCranking(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2017-03-08 22:14:02 -08:00
|
|
|
/**
|
|
|
|
* todo: pre-calculate 'numberOfInjections'
|
|
|
|
* see also injectorDutyCycle
|
|
|
|
*/
|
2017-07-06 14:26:32 -07:00
|
|
|
if (!isCranking && injectionDuration * getNumberOfInjections(engineConfiguration->injectionMode PASS_ENGINE_PARAMETER_SUFFIX) > getEngineCycleDuration(rpm PASS_ENGINE_PARAMETER_SUFFIX)) {
|
2018-01-23 09:05:14 -08:00
|
|
|
warning(CUSTOM_TOO_LONG_FUEL_INJECTION, "Too long fuel injection %.2fms", injectionDuration);
|
2017-07-06 14:26:32 -07:00
|
|
|
} else if (isCranking && injectionDuration * getNumberOfInjections(engineConfiguration->crankingInjectionMode PASS_ENGINE_PARAMETER_SUFFIX) > getEngineCycleDuration(rpm PASS_ENGINE_PARAMETER_SUFFIX)) {
|
2018-01-23 09:05:14 -08:00
|
|
|
warning(CUSTOM_TOO_LONG_CRANKING_FUEL_INJECTION, "Too long cranking fuel injection %.2fms", injectionDuration);
|
2016-07-23 20:03:45 -07:00
|
|
|
}
|
|
|
|
|
2017-12-31 16:25:59 -08:00
|
|
|
// Store 'pure' injection duration (w/o injector lag) for fuel rate calc.
|
2019-08-26 20:41:04 -07:00
|
|
|
engine->engineState.fuelConsumption.addData(injectionDuration - ENGINE(engineState.running.injectorLag));
|
2017-12-31 16:25:59 -08:00
|
|
|
|
2015-08-23 20:02:37 -07:00
|
|
|
ENGINE(actualLastInjection) = 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;
|
|
|
|
}
|
2018-06-23 06:32:41 -07:00
|
|
|
|
|
|
|
// 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
|
2018-06-23 06:32:41 -07:00
|
|
|
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
|
|
|
|
2020-01-10 23:26:20 -08:00
|
|
|
// we are ignoring low RPM in order not to handle "engine was stopped to engine now running" transition
|
|
|
|
if (rpm > 2 * engineConfiguration->cranking.rpm) {
|
|
|
|
const char *outputName = event->outputs[0]->name;
|
|
|
|
if (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
|
|
|
}
|
2020-01-10 23:26:20 -08:00
|
|
|
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) {
|
|
|
|
InjectorOutputPin *output = event->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,
|
|
|
|
(int)MS2US(getCrankshaftRevolutionTimeMs(GET_RPM_VALUE)) / 1000.0);
|
2020-07-19 11:13:15 -07:00
|
|
|
}
|
2020-01-10 23:26:20 -08:00
|
|
|
#endif /*EFI_PRINTF_FUEL_DETAILS */
|
2017-05-24 19:46:45 -07:00
|
|
|
|
2020-01-10 23:26:20 -08:00
|
|
|
if (event->isScheduled) {
|
2020-07-19 11:13:15 -07:00
|
|
|
#if EFI_PRINTF_FUEL_DETAILS
|
|
|
|
if (printFuelDebug) {
|
2020-07-19 11:17:15 -07:00
|
|
|
InjectorOutputPin *output = event->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 */
|
2020-01-10 23:26:20 -08:00
|
|
|
return; // this InjectionEvent is still needed for an extremely long injection scheduled previously
|
|
|
|
}
|
2017-05-24 19:46:45 -07:00
|
|
|
|
2020-01-10 23:26:20 -08:00
|
|
|
event->isScheduled = true;
|
2017-05-24 19:46:45 -07:00
|
|
|
|
2020-01-10 23:26:20 -08:00
|
|
|
action_s startAction, endAction;
|
|
|
|
// We use different callbacks based on whether we're running sequential mode or not - everything else is the same
|
|
|
|
if (event->isSimultanious) {
|
|
|
|
startAction = { &startSimultaniousInjection, engine };
|
|
|
|
endAction = { &endSimultaniousInjection, event };
|
|
|
|
} else {
|
|
|
|
// sequential or batch
|
|
|
|
startAction = { &turnInjectionPinHigh, event };
|
|
|
|
endAction = { &turnInjectionPinLow, event };
|
2016-09-03 22:01:54 -07:00
|
|
|
}
|
2020-01-10 23:26:20 -08:00
|
|
|
|
2020-02-01 14:29:55 -08:00
|
|
|
efitick_t startTime = scheduleByAngle(&event->signalTimerUp, nowNt, event->injectionStart.angleOffsetFromTriggerEvent, startAction PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
efitick_t turnOffTime = startTime + US2NT((int)durationUs);
|
|
|
|
engine->executor.scheduleByTimestampNt(&event->endOfInjectionEvent, turnOffTime, endAction);
|
|
|
|
|
2020-01-10 23:26:20 -08:00
|
|
|
#if EFI_UNIT_TEST
|
2020-02-01 14:29:55 -08:00
|
|
|
printf("scheduling injection angle=%.2f/delay=%.2f injectionDuration=%.2f\r\n", event->injectionStart.angleOffsetFromTriggerEvent, NT2US(startTime - nowNt), injectionDuration);
|
2020-01-10 23:26:20 -08:00
|
|
|
#endif
|
2020-02-01 14:29:55 -08:00
|
|
|
#if EFI_DEFAILED_LOGGING
|
|
|
|
scheduleMsg(logger, "handleFuel pin=%s eventIndex %d duration=%.2fms %d", event->outputs[0]->name,
|
|
|
|
injEventIndex,
|
|
|
|
injectionDuration,
|
|
|
|
getRevolutionCounter());
|
|
|
|
scheduleMsg(logger, "handleFuel pin=%s delay=%.2f %d", event->outputs[0]->name, NT2US(startTime - nowNt),
|
|
|
|
getRevolutionCounter());
|
|
|
|
#endif /* EFI_DEFAILED_LOGGING */
|
2016-09-03 22:01:54 -07:00
|
|
|
}
|
|
|
|
|
2020-01-19 21:12:18 -08:00
|
|
|
static ALWAYS_INLINE void handleFuel(const bool limitedFuel, uint32_t trgEventIndex, int rpm, efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
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
|
|
|
|
2017-05-25 19:28:04 -07:00
|
|
|
if (!isInjectionEnabled(PASS_ENGINE_PARAMETER_SIGNATURE) || limitedFuel) {
|
2016-09-03 05:03:07 -07:00
|
|
|
return;
|
|
|
|
}
|
2017-06-08 21:02:12 -07:00
|
|
|
if (ENGINE(isCylinderCleanupMode)) {
|
2016-09-20 21:02:15 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-14 04:44:32 -07:00
|
|
|
// 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 PASS_ENGINE_PARAMETER_SUFFIX) > 96.0f) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-03 05:03:07 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* Ignition events are defined by addFuelEvents() according to selected
|
|
|
|
* fueling strategy
|
|
|
|
*/
|
2017-06-08 21:02:12 -07:00
|
|
|
FuelSchedule *fs = &ENGINE(injectionEvents);
|
2016-11-30 19:06:43 -08:00
|
|
|
if (!fs->isReady) {
|
2017-05-15 20:28:49 -07:00
|
|
|
fs->addFuelEvents(PASS_ENGINE_PARAMETER_SIGNATURE);
|
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
|
2016-09-04 10:03:39 -07:00
|
|
|
scheduleMsg(logger, "handleFuel ind=%d %d", trgEventIndex, getRevolutionCounter());
|
2016-09-03 22:01:54 -07:00
|
|
|
#endif /* FUEL_MATH_EXTREME_LOGGING */
|
2016-08-05 22:04:28 -07:00
|
|
|
|
2020-04-05 06:11:25 -07:00
|
|
|
ENGINE(tpsAccelEnrichment.onNewValue(Sensor::get(SensorType::Tps1).value_or(0) PASS_ENGINE_PARAMETER_SUFFIX));
|
2019-03-28 19:47:20 -07:00
|
|
|
if (trgEventIndex == 0) {
|
|
|
|
ENGINE(tpsAccelEnrichment.onEngineCycleTps(PASS_ENGINE_PARAMETER_SIGNATURE));
|
|
|
|
ENGINE(engineLoadAccelEnrichment.onEngineCycle(PASS_ENGINE_PARAMETER_SIGNATURE));
|
|
|
|
}
|
2016-01-20 13:02:22 -08:00
|
|
|
|
2016-11-30 18:06:24 -08:00
|
|
|
for (int injEventIndex = 0; injEventIndex < CONFIG(specs.cylindersCount); injEventIndex++) {
|
2016-11-30 19:06:43 -08:00
|
|
|
InjectionEvent *event = &fs->elements[injEventIndex];
|
2019-10-07 21:54:19 -07:00
|
|
|
uint32_t eventIndex = event->injectionStart.triggerEventIndex;
|
2016-08-27 10:01:55 -07:00
|
|
|
// 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
|
2018-07-25 20:03:04 -07:00
|
|
|
// efiAssertVoid(CUSTOM_ERR_ASSERT_VOID, eventIndex < ENGINE(triggerShape.getLength()), "handleFuel/event sch index");
|
2016-09-04 10:03:39 -07:00
|
|
|
if (eventIndex != trgEventIndex) {
|
2015-07-10 06:01:56 -07:00
|
|
|
continue;
|
2016-04-26 18:02:55 -07:00
|
|
|
}
|
2020-01-19 21:12:18 -08:00
|
|
|
handleFuelInjectionEvent(injEventIndex, event, rpm, nowNt PASS_ENGINE_PARAMETER_SUFFIX);
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the main trigger event handler.
|
|
|
|
* Both injection and ignition are controlled from this method.
|
|
|
|
*/
|
2020-01-09 12:45:13 -08:00
|
|
|
static void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t trgEventIndex, efitick_t edgeTimestamp DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2019-10-13 13:14:08 -07:00
|
|
|
ScopePerf perf(PE::MainTriggerCallback);
|
|
|
|
|
2016-06-12 13:01:41 -07:00
|
|
|
(void) ckpSignalType;
|
|
|
|
|
2020-04-26 17:50:49 -07:00
|
|
|
|
|
|
|
if (engineConfiguration->vvtMode == MIATA_NB2 && engine->triggerCentral.vvtSyncTimeNt == 0) {
|
|
|
|
// this is a bit spaghetti code for sure
|
|
|
|
// do not spark & do not fuel until we have VVT sync. NB2 is a special case
|
|
|
|
// due to symmetrical crank wheel and we need to make sure no spark happens out of sync
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2019-12-13 17:51:04 -08:00
|
|
|
efiAssertVoid(CUSTOM_STACK_6629, getCurrentRemainingStack() > EXPECTED_REMAINING_STACK, "lowstck#2a");
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-05-05 15:53:34 -07:00
|
|
|
#if EFI_CDM_INTEGRATION
|
2019-12-11 14:48:55 -08:00
|
|
|
if (trgEventIndex == 0 && CONFIG(cdmInputPin) != GPIO_UNASSIGNED) {
|
2019-05-05 15:53:34 -07:00
|
|
|
int cdmKnockValue = getCurrentCdmValue(engine->triggerCentral.triggerState.getTotalRevolutionCounter());
|
|
|
|
engine->knockLogic(cdmKnockValue);
|
|
|
|
}
|
|
|
|
#endif /* EFI_CDM_INTEGRATION */
|
|
|
|
|
2017-06-08 21:02:12 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:33:21 -08:00
|
|
|
int rpm = GET_RPM_VALUE;
|
2015-07-10 06:01:56 -07:00
|
|
|
if (rpm == 0) {
|
|
|
|
// this happens while we just start cranking
|
|
|
|
// 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) {
|
2019-05-10 20:52:55 -07:00
|
|
|
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;
|
|
|
|
}
|
2020-06-01 06:09:55 -07:00
|
|
|
bool limitedSpark = rpm > engine->getRpmHardLimit(PASS_ENGINE_PARAMETER_SIGNATURE);
|
|
|
|
bool limitedFuel = rpm > engine->getRpmHardLimit(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2016-03-16 21:01:55 -07:00
|
|
|
|
2020-05-22 21:40:20 -07:00
|
|
|
if (CONFIG(boostCutPressure) != 0) {
|
2019-05-27 12:56:12 -07:00
|
|
|
if (getMap(PASS_ENGINE_PARAMETER_SIGNATURE) > CONFIG(boostCutPressure)) {
|
2016-03-16 21:01:55 -07:00
|
|
|
limitedSpark = true;
|
|
|
|
limitedFuel = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 10:03:39 -07:00
|
|
|
if (trgEventIndex == 0) {
|
2019-05-10 21:21:37 -07:00
|
|
|
if (HAVE_CAM_INPUT()) {
|
2019-09-02 11:47:05 -07:00
|
|
|
engine->triggerCentral.validateCamVvtCounters();
|
2019-05-10 21:21:37 -07:00
|
|
|
}
|
2016-08-05 22:04:28 -07:00
|
|
|
|
2019-01-09 18:08:04 -08:00
|
|
|
if (checkIfTriggerConfigChanged(PASS_ENGINE_PARAMETER_SIGNATURE)) {
|
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;
|
2018-03-10 17:58:51 -08:00
|
|
|
// moved 'triggerIndexByAngle' into trigger initialization (why was it invoked from here if it's only about trigger shape & optimization?)
|
2019-12-07 22:09:39 -08:00
|
|
|
// see initializeTriggerWaveform() -> prepareOutputSignals(PASS_ENGINE_PARAMETER_SIGNATURE)
|
2018-03-10 17:58:51 -08:00
|
|
|
|
2016-08-27 10:01:55 -07:00
|
|
|
// we need this to apply new 'triggerIndexByAngle' values
|
2017-05-15 20:28:49 -07:00
|
|
|
engine->periodicFastCallback(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2016-04-25 20:01:59 -07:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2018-09-10 19:10:55 -07:00
|
|
|
efiAssertVoid(CUSTOM_IGN_MATH_STATE, !CONFIG(useOnlyRisingEdgeForTrigger) || CONFIG(ignMathCalculateAtIndex) % 2 == 0, "invalid ignMathCalculateAtIndex");
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-11-05 19:40:33 -08:00
|
|
|
if (trgEventIndex == (uint32_t)CONFIG(ignMathCalculateAtIndex)) {
|
2016-01-18 11:01:39 -08:00
|
|
|
if (CONFIG(externalKnockSenseAdc) != EFI_ADC_NONE) {
|
2019-09-22 06:56:06 -07:00
|
|
|
float externalKnockValue = getVoltageDivided("knock", engineConfiguration->externalKnockSenseAdc PASS_ENGINE_PARAMETER_SUFFIX);
|
2019-01-04 21:57:09 -08:00
|
|
|
engine->knockLogic(externalKnockValue PASS_ENGINE_PARAMETER_SUFFIX);
|
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
|
|
|
|
*/
|
2020-01-19 21:12:18 -08:00
|
|
|
handleFuel(limitedFuel, trgEventIndex, rpm, edgeTimestamp PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-09-26 06:01:32 -07:00
|
|
|
/**
|
|
|
|
* For spark we schedule both start of coil charge and actual spark based on trigger angle
|
|
|
|
*/
|
2020-01-10 13:01:54 -08:00
|
|
|
onTriggerEventSparkLogic(limitedSpark, trgEventIndex, rpm, edgeTimestamp PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2018-01-31 05:07:00 -08:00
|
|
|
// Check if the engine is not stopped or cylinder cleanup is activated
|
|
|
|
static bool isPrimeInjectionPulseSkipped(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2018-03-05 05:25:20 -08:00
|
|
|
if (!engine->rpmCalculator.isStopped(PASS_ENGINE_PARAMETER_SIGNATURE))
|
2018-01-31 05:07:00 -08:00
|
|
|
return true;
|
2020-04-05 06:11:25 -07:00
|
|
|
return CONFIG(isCylinderCleanupEnabled) && (Sensor::get(SensorType::Tps1).value_or(0) > CLEANUP_MODE_TPS);
|
2018-01-31 05:07:00 -08:00
|
|
|
}
|
|
|
|
|
2018-03-04 13:13:23 -08:00
|
|
|
/**
|
|
|
|
* Prime injection pulse, mainly needed for mono-injectors or long intake manifolds.
|
|
|
|
* See testStartOfCrankingPrimingPulse()
|
|
|
|
*/
|
2018-03-04 20:53:48 -08:00
|
|
|
void startPrimeInjectionPulse(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2017-12-23 11:15:35 -08: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
|
2017-12-23 11:15:35 -08:00
|
|
|
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 */
|
|
|
|
|
2017-12-23 11:15:35 -08:00
|
|
|
// if we're just toying with the ignition switch, give it another chance eventually...
|
|
|
|
if (ignSwitchCounter > 10)
|
|
|
|
ignSwitchCounter = 0;
|
2018-01-31 05:07:00 -08:00
|
|
|
// 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(PASS_ENGINE_PARAMETER_SIGNATURE))
|
|
|
|
ignSwitchCounter = -1;
|
2017-12-23 11:15:35 -08:00
|
|
|
// start prime injection if this is a 'fresh start'
|
|
|
|
if (ignSwitchCounter == 0) {
|
|
|
|
// fill-in the prime event struct
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_UNIT_TEST
|
2017-12-23 11:15:35 -08:00
|
|
|
primeInjEvent.engine = engine;
|
2018-03-04 20:53:48 -08:00
|
|
|
#endif /* EFI_UNIT_TEST */
|
2017-12-23 11:15:35 -08:00
|
|
|
primeInjEvent.ownIndex = 0;
|
|
|
|
primeInjEvent.isSimultanious = true;
|
|
|
|
|
2019-10-07 23:56:19 -07:00
|
|
|
scheduling_s *sDown = &ENGINE(injectionEvents.elements[0]).endOfInjectionEvent;
|
2018-01-03 05:00:39 -08:00
|
|
|
// When the engine is hot, basically we don't need prime inj.pulse, so we use an interpolation over temperature (falloff).
|
|
|
|
// If 'primeInjFalloffTemperature' is not specified (by default), we have a prime pulse deactivation at zero celsius degrees, which is okay.
|
|
|
|
const float maxPrimeInjAtTemperature = -40.0f; // at this temperature the pulse is maximal.
|
2018-01-17 20:20:05 -08:00
|
|
|
floatms_t pulseLength = interpolateClamped(maxPrimeInjAtTemperature, CONFIG(startOfCrankingPrimingPulse),
|
2020-04-18 15:45:30 -07:00
|
|
|
CONFIG(primeInjFalloffTemperature), 0.0f, Sensor::get(SensorType::Clt).value_or(70));
|
2018-01-17 20:20:05 -08:00
|
|
|
if (pulseLength > 0) {
|
|
|
|
startSimultaniousInjection(engine);
|
2018-01-18 09:11:12 -08:00
|
|
|
efitimeus_t turnOffDelayUs = (efitimeus_t)efiRound(MS2US(pulseLength), 1.0f);
|
2020-01-07 15:10:31 -08:00
|
|
|
engine->executor.scheduleForLater(sDown, turnOffDelayUs, { &endSimultaniousInjectionOnlyTogglePins, engine });
|
2018-01-17 20:20:05 -08:00
|
|
|
}
|
2017-12-23 11:15:35 -08:00
|
|
|
}
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_PROD_CODE
|
2017-12-23 11:15:35 -08:00
|
|
|
// we'll reset it later when the engine starts
|
|
|
|
backupRamSave(BACKUP_IGNITION_SWITCH_COUNTER, ignSwitchCounter + 1);
|
|
|
|
#endif /* EFI_PROD_CODE */
|
|
|
|
}
|
|
|
|
|
|
|
|
void updatePrimeInjectionPulseState(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_PROD_CODE
|
2017-12-23 11:15:35 -08:00
|
|
|
static bool counterWasReset = false;
|
|
|
|
if (counterWasReset)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!engine->rpmCalculator.isStopped(PASS_ENGINE_PARAMETER_SIGNATURE)) {
|
|
|
|
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();
|
2017-05-15 20:28:49 -07:00
|
|
|
float el = getEngineLoadT(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2018-01-23 09:05:14 -08:00
|
|
|
scheduleMsg(logger, "rpm %d engine_load %.2f", rpm, el);
|
|
|
|
scheduleMsg(logger, "fuel %.2fms timing %.2f", getInjectionDuration(rpm PASS_ENGINE_PARAMETER_SUFFIX), engine->engineState.timingAdvance);
|
2019-12-23 18:56:16 -08:00
|
|
|
#endif /* EFI_PROD_CODE */
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-05-25 19:28:04 -07:00
|
|
|
void initMainEventListener(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2015-07-10 06:01:56 -07:00
|
|
|
logger = sharedLogger;
|
2018-07-25 20:03:04 -07:00
|
|
|
efiAssertVoid(CUSTOM_ERR_6631, engine!=NULL, "null engine");
|
2017-11-25 22:17:37 -08:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
printMsg(logger, "initMainLoop: %d", currentTimeMillis());
|
2017-05-25 19:28:04 -07:00
|
|
|
if (!isInjectionEnabled(PASS_ENGINE_PARAMETER_SIGNATURE))
|
2015-07-10 06:01:56 -07:00
|
|
|
printMsg(logger, "!!!!!!!!!!!!!!!!!!! injection disabled");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
addTriggerEventListener(mainTriggerCallback, "main loop", engine);
|
2017-12-23 11:15:35 -08:00
|
|
|
|
|
|
|
// We start prime injection pulse at the early init stage - don't wait for the engine to start spinning!
|
|
|
|
if (CONFIG(startOfCrankingPrimingPulse) > 0)
|
|
|
|
startPrimeInjectionPulse(PASS_ENGINE_PARAMETER_SIGNATURE);
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* EFI_ENGINE_CONTROL */
|