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
|
2017-01-03 03:05:22 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2017
|
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 "main.h"
|
2015-09-06 09:02:27 -07:00
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
#include <nvic.h>
|
|
|
|
#endif
|
|
|
|
|
2015-09-06 09:02:27 -07:00
|
|
|
#if (!EFI_PROD_CODE && !EFI_SIMULATOR) || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-03-27 19:57:13 -07:00
|
|
|
#define chThdGetSelfX() 0
|
2015-07-10 06:01:56 -07:00
|
|
|
#define getRemainingStack(x) (999999)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if EFI_ENGINE_CONTROL || defined(__DOXYGEN__)
|
|
|
|
|
|
|
|
#include "main_trigger_callback.h"
|
|
|
|
#include "efiGpio.h"
|
|
|
|
#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 "signal_executor.h"
|
|
|
|
#include "engine_configuration.h"
|
|
|
|
#include "interpolation.h"
|
|
|
|
#include "advance_map.h"
|
|
|
|
#include "allsensors.h"
|
|
|
|
#include "cyclic_buffer.h"
|
|
|
|
#include "histogram.h"
|
|
|
|
#include "fuel_math.h"
|
|
|
|
#include "histogram.h"
|
2016-09-03 22:01:54 -07:00
|
|
|
#include "efiGpio.h"
|
2015-09-06 09:02:27 -07:00
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "rfiutil.h"
|
|
|
|
#endif /* EFI_HISTOGRAMS */
|
|
|
|
#include "LocalVersionHolder.h"
|
|
|
|
#include "event_queue.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#include "efilib2.h"
|
|
|
|
|
|
|
|
EXTERN_ENGINE
|
|
|
|
;
|
|
|
|
extern bool hasFirmwareErrorFlag;
|
|
|
|
|
2016-07-17 19:02:34 -07:00
|
|
|
static const char *prevOutputName = NULL;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static Logging *logger;
|
2017-01-22 13:04:09 -08:00
|
|
|
#if ! EFI_UNIT_TEST
|
|
|
|
static pid_s *fuelPidS = &persistentState.persistentConfiguration.engineConfiguration.fuelClosedLoopPid;
|
2017-05-29 20:15:07 -07:00
|
|
|
static Pid fuelPid(fuelPidS);
|
2017-01-22 14:03:31 -08:00
|
|
|
extern TunerStudioOutputChannels tsOutputChannels;
|
2017-01-22 13:04:09 -08:00
|
|
|
#endif
|
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
|
|
|
|
|
2016-11-30 19:06:43 -08:00
|
|
|
static void startSimultaniousInjection(InjectionEvent *event) {
|
2017-05-25 19:37:24 -07:00
|
|
|
(void)event;
|
2016-11-30 20:02:42 -08:00
|
|
|
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
|
2016-11-30 19:06:43 -08:00
|
|
|
Engine *engine = event->engine;
|
|
|
|
#endif
|
2015-07-10 06:01:56 -07:00
|
|
|
for (int i = 0; i < engine->engineConfiguration->specs.cylindersCount; i++) {
|
2017-04-21 16:23:20 -07:00
|
|
|
enginePins.injectors[i].setHigh();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 19:06:43 -08:00
|
|
|
static void endSimultaniousInjection(InjectionEvent *event) {
|
2016-11-30 20:02:42 -08:00
|
|
|
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
|
2016-11-30 19:06:43 -08:00
|
|
|
Engine *engine = event->engine;
|
|
|
|
EXPAND_Engine;
|
|
|
|
#endif
|
2015-07-10 06:01:56 -07:00
|
|
|
for (int i = 0; i < engine->engineConfiguration->specs.cylindersCount; i++) {
|
2017-04-21 16:23:20 -07:00
|
|
|
enginePins.injectors[i].setLow();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-05-25 19:37:24 -07:00
|
|
|
static inline void tempTurnPinHigh(InjectorOutputPin *output) {
|
2016-10-01 21:02:11 -07:00
|
|
|
output->overlappingCounter++;
|
2016-09-26 18:03:18 -07:00
|
|
|
|
|
|
|
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
|
|
|
|
printf("seTurnPinHigh %s %d %d\r\n", output->name, output->overlappingCounter, (int)getTimeNowUs());
|
|
|
|
#endif /* FUEL_MATH_EXTREME_LOGGING */
|
|
|
|
|
|
|
|
if (output->overlappingCounter > 1) {
|
|
|
|
// /**
|
|
|
|
// * #299
|
|
|
|
// * this is another kind of overlap which happens in case of a small duty cycle after a large duty cycle
|
|
|
|
// */
|
|
|
|
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
|
|
|
|
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 {
|
2016-09-26 18:03:18 -07:00
|
|
|
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
|
2017-05-25 19:31:29 -07:00
|
|
|
const char * w = output->currentLogicValue == true ? "err" : "";
|
2016-10-01 21:02:11 -07:00
|
|
|
// scheduleMsg(&sharedLogger, "^ %spin=%s eventIndex %d %d", w, output->name,
|
|
|
|
// getRevolutionCounter(), getTimeNowUs());
|
2016-09-26 18:03:18 -07:00
|
|
|
#endif /* FUEL_MATH_EXTREME_LOGGING */
|
|
|
|
|
2017-05-25 19:31:29 -07:00
|
|
|
output->setHigh();
|
|
|
|
}
|
2016-09-26 18:03:18 -07:00
|
|
|
}
|
|
|
|
|
2016-11-30 15:02:19 -08:00
|
|
|
// todo: make these macro? kind of a penny optimization if compiler is not smart to inline
|
2017-05-24 04:57:58 -07:00
|
|
|
void seTurnPinHigh(InjectionSignalPair *pair) {
|
2017-05-25 19:31:29 -07:00
|
|
|
for (int i = 0;i < MAX_WIRES_COUNT;i++) {
|
2016-11-30 17:02:41 -08:00
|
|
|
InjectorOutputPin *output = pair->outputs[i];
|
2016-11-30 19:06:43 -08:00
|
|
|
if (output != NULL) {
|
2016-11-30 17:02:41 -08:00
|
|
|
tempTurnPinHigh(output);
|
2016-11-30 19:06:43 -08:00
|
|
|
}
|
2016-11-30 17:02:41 -08:00
|
|
|
}
|
2016-11-30 15:02:19 -08:00
|
|
|
}
|
|
|
|
|
2017-05-25 19:37:24 -07:00
|
|
|
static inline void tempTurnPinLow(InjectorOutputPin *output) {
|
2016-09-26 18:03:18 -07:00
|
|
|
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
|
|
|
|
printf("seTurnPinLow %s %d %d\r\n", output->name, output->overlappingCounter, (int)getTimeNowUs());
|
|
|
|
#endif /* FUEL_MATH_EXTREME_LOGGING */
|
|
|
|
|
|
|
|
if (output->cancelNextTurningInjectorOff) {
|
|
|
|
/**
|
|
|
|
* in case of fuel schedule overlap between engine cycles,
|
|
|
|
* and if engine cycle is above say 75% for batch mode on 4 cylinders,
|
|
|
|
* we will get a secondary overlap between the special injection and a normal injection on the same injector.
|
|
|
|
* In such a case want to combine these two injection into one continues injection.
|
|
|
|
* Unneeded turn of injector on is handle while scheduling that second injection, but cancellation
|
|
|
|
* of special injection end has to be taken care of dynamically
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
output->cancelNextTurningInjectorOff = false;
|
|
|
|
#if EFI_SIMULATOR || defined(__DOXYGEN__)
|
|
|
|
printf("was cancelled %s %d\r\n", output->name, (int)getTimeNowUs());
|
|
|
|
#endif /* EFI_SIMULATOR */
|
2017-05-25 19:31:29 -07:00
|
|
|
} else {
|
2016-09-26 18:03:18 -07:00
|
|
|
|
2017-05-25 19:31:29 -07:00
|
|
|
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
|
|
|
|
const char * w = output->currentLogicValue == false ? "err" : "";
|
2016-09-26 18:03:18 -07:00
|
|
|
|
2016-10-01 21:02:11 -07:00
|
|
|
// scheduleMsg(&sharedLogger, "- %spin=%s eventIndex %d %d", w, output->name,
|
|
|
|
// getRevolutionCounter(), getTimeNowUs());
|
2016-09-26 18:03:18 -07:00
|
|
|
#endif /* FUEL_MATH_EXTREME_LOGGING */
|
|
|
|
|
2017-05-25 19:31:29 -07:00
|
|
|
output->overlappingCounter--;
|
|
|
|
if (output->overlappingCounter > 0) {
|
2016-09-26 18:03:18 -07:00
|
|
|
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
|
2017-05-25 19:31:29 -07:00
|
|
|
printf("was overlapping, no need to touch pin %s %d\r\n", output->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 {
|
|
|
|
output->setLow();
|
|
|
|
}
|
2016-09-26 18:03:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 04:57:58 -07:00
|
|
|
void seTurnPinLow(InjectionSignalPair *pair) {
|
2016-11-30 15:02:19 -08:00
|
|
|
pair->isScheduled = false;
|
2016-11-30 17:02:41 -08:00
|
|
|
for (int i = 0;i<MAX_WIRES_COUNT;i++) {
|
|
|
|
InjectorOutputPin *output = pair->outputs[i];
|
2016-11-30 19:06:43 -08:00
|
|
|
if (output != NULL) {
|
|
|
|
tempTurnPinLow(output);
|
|
|
|
}
|
2016-11-30 17:02:41 -08:00
|
|
|
}
|
2016-11-30 20:02:42 -08:00
|
|
|
efiAssertVoid(pair->event != NULL, "pair event");
|
|
|
|
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
|
|
|
|
Engine *engine = pair->event->engine;
|
|
|
|
EXPAND_Engine;
|
2016-11-30 19:06:43 -08:00
|
|
|
#endif
|
2017-05-25 19:37:24 -07:00
|
|
|
ENGINE(injectionEvents.addFuelEventsForCylinder(pair->event->ownIndex PASS_ENGINE_PARAMETER_SUFFIX));
|
2016-11-30 15:02:19 -08:00
|
|
|
}
|
|
|
|
|
2017-05-24 04:57:58 -07:00
|
|
|
static void seScheduleByTime(scheduling_s *scheduling, efitimeus_t time, schfunc_t callback, InjectionSignalPair *pair) {
|
2016-09-26 18:03:18 -07:00
|
|
|
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
|
2017-05-15 18:26:08 -07:00
|
|
|
InjectorOutputPin *param = pair->outputs[0];
|
2016-10-01 21:02:11 -07:00
|
|
|
// scheduleMsg(&sharedLogger, "schX %s %x %d", prefix, scheduling, time);
|
|
|
|
// scheduleMsg(&sharedLogger, "schX %s", param->name);
|
2016-09-26 18:03:18 -07:00
|
|
|
|
|
|
|
const char *direction = callback == (schfunc_t) &seTurnPinHigh ? "up" : "down";
|
|
|
|
printf("seScheduleByTime %s %s %d sch=%d\r\n", direction, param->name, (int)time, (int)scheduling);
|
|
|
|
#endif /* FUEL_MATH_EXTREME_LOGGING || EFI_UNIT_TEST */
|
|
|
|
|
2017-05-15 06:35:06 -07:00
|
|
|
scheduleByTime(scheduling, time, callback, pair);
|
2016-09-26 18:03:18 -07:00
|
|
|
}
|
|
|
|
|
2016-09-04 22:03:25 -07:00
|
|
|
static ALWAYS_INLINE void handleFuelInjectionEvent(int injEventIndex, InjectionEvent *event,
|
2017-05-15 20:28:49 -07:00
|
|
|
int rpm 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?
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
const floatms_t injectionDuration = ENGINE(wallFuel).adjust(event->outputs[0]->injectorIndex, ENGINE(fuelMs) PASS_ENGINE_PARAMETER_SUFFIX);
|
2016-09-25 21:03:15 -07:00
|
|
|
#if EFI_PRINTF_FUEL_DETAILS || defined(__DOXYGEN__)
|
|
|
|
printf("fuel fuelMs=%f adjusted=%f\t\n", ENGINE(fuelMs), injectionDuration);
|
|
|
|
#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)) {
|
2017-03-08 22:14:02 -08:00
|
|
|
warning(CUSTOM_TOO_LONG_FUEL_INJECTION, "Too long fuel injection %fms", 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)) {
|
2017-03-08 22:14:02 -08:00
|
|
|
warning(CUSTOM_TOO_LONG_CRANKING_FUEL_INJECTION, "Too long cranking fuel injection %fms", injectionDuration);
|
2016-07-23 20:03:45 -07: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) {
|
2016-07-17 07:13:22 -07:00
|
|
|
warning(CUSTOM_OBD_NEG_INJECTION, "Negative injection pulse %f", injectionDuration);
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
|
|
|
}
|
2017-05-24 19:46:45 -07:00
|
|
|
floatus_t durationUs = MS2US(injectionDuration);
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-09-03 22:01:54 -07:00
|
|
|
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
|
2016-08-26 14:02:37 -07:00
|
|
|
scheduleMsg(logger, "handleFuel totalPerCycle=%f", totalPerCycle);
|
|
|
|
scheduleMsg(logger, "handleFuel engineCycleDuration=%f", engineCycleDuration);
|
2016-09-03 22:01:54 -07:00
|
|
|
#endif /* FUEL_MATH_EXTREME_LOGGING */
|
2016-08-05 22:04:28 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
floatus_t injectionStartDelayUs = ENGINE(rpmCalculator.oneDegreeUs) * event->injectionStart.angleOffset;
|
|
|
|
|
2016-09-03 22:01:54 -07:00
|
|
|
#if EFI_DEFAILED_LOGGING || defined(__DOXYGEN__)
|
2016-08-26 14:02:37 -07:00
|
|
|
scheduleMsg(logger, "handleFuel pin=%s eventIndex %d duration=%fms %d", event->output->name,
|
|
|
|
eventIndex,
|
|
|
|
injectionDuration,
|
|
|
|
getRevolutionCounter());
|
|
|
|
scheduleMsg(logger, "handleFuel pin=%s delay=%f %d", event->output->name, injectionStartDelayUs,
|
|
|
|
getRevolutionCounter());
|
|
|
|
#endif /* EFI_DEFAILED_LOGGING */
|
2016-08-05 22:04:28 -07:00
|
|
|
|
2017-05-24 04:57:58 -07:00
|
|
|
InjectionSignalPair *pair = &ENGINE(fuelActuators[injEventIndex]);
|
2016-01-24 23:03:01 -08:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
if (event->isSimultanious) {
|
|
|
|
/**
|
|
|
|
* this is pretty much copy-paste of 'scheduleOutput'
|
|
|
|
* 'scheduleOutput' is currently only used for injection, so maybe it should be
|
|
|
|
* changed into 'scheduleInjection' and unified? todo: think about it.
|
|
|
|
*/
|
2016-12-01 07:01:50 -08:00
|
|
|
|
2016-10-01 13:01:55 -07:00
|
|
|
scheduling_s * sUp = &pair->signalTimerUp;
|
|
|
|
// todo: sequential need this logic as well, just do not forget to clear flag pair->isScheduled = true;
|
|
|
|
scheduling_s * sDown = &pair->signalTimerDown;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-15 06:35:06 -07:00
|
|
|
scheduleTask(sUp, (int) injectionStartDelayUs, (schfunc_t) &startSimultaniousInjection, event);
|
2017-05-24 19:46:45 -07:00
|
|
|
scheduleTask(sDown, (int) injectionStartDelayUs + durationUs,
|
2016-11-30 19:06:43 -08:00
|
|
|
(schfunc_t) &endSimultaniousInjection, event);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
} else {
|
2016-01-30 19:03:36 -08:00
|
|
|
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
|
|
|
|
printf("scheduling injection angle=%f/delay=%f injectionDuration=%f\r\n", event->injectionStart.angleOffset, injectionStartDelayUs, injectionDuration);
|
|
|
|
#endif
|
|
|
|
|
2016-07-17 17:01:54 -07:00
|
|
|
// we are in this branch of code only in case of NOT IM_SIMULTANEOUS injection
|
2016-09-02 23:03:32 -07:00
|
|
|
// we are ignoring low RPM in order not to handle "engine was stopped to engine now running" transition
|
2016-07-17 17:01:54 -07:00
|
|
|
if (rpm > 2 * engineConfiguration->cranking.rpm) {
|
2016-11-30 16:01:43 -08:00
|
|
|
const char *outputName = event->outputs[0]->name;
|
2017-02-22 19:27:44 -08:00
|
|
|
if (prevOutputName == outputName && engineConfiguration->injectionMode != IM_SIMULTANEOUS) {
|
2016-08-04 22:01:41 -07:00
|
|
|
warning(CUSTOM_OBD_SKIPPED_FUEL, "looks like skipped fuel event %d %s", getRevolutionCounter(), outputName);
|
2016-07-17 19:02:34 -07:00
|
|
|
}
|
|
|
|
prevOutputName = outputName;
|
2016-07-17 17:01:54 -07:00
|
|
|
}
|
|
|
|
|
2017-05-24 19:39:17 -07:00
|
|
|
efitimeus_t nowUs = getTimeNowUs();
|
2017-05-24 19:46:45 -07:00
|
|
|
|
|
|
|
InjectorOutputPin *output = event->outputs[0];
|
|
|
|
#if EFI_PRINTF_FUEL_DETAILS || defined(__DOXYGEN__)
|
|
|
|
printf("fuelout %s duration %d total=%d\t\n", output->name, (int)durationUs,
|
|
|
|
(int)MS2US(getCrankshaftRevolutionTimeMs(ENGINE(rpmCalculator.rpmValue))));
|
|
|
|
#endif /*EFI_PRINTF_FUEL_DETAILS */
|
|
|
|
|
|
|
|
|
|
|
|
if (pair->isScheduled) {
|
|
|
|
#if EFI_UNIT_TEST || EFI_SIMULATOR || defined(__DOXYGEN__)
|
|
|
|
printf("still used1 %s %d\r\n", output->name, (int)getTimeNowUs());
|
|
|
|
#endif /* EFI_UNIT_TEST || EFI_SIMULATOR */
|
|
|
|
return; // this InjectionSignalPair is still needed for an extremely long injection scheduled previously
|
|
|
|
}
|
|
|
|
pair->outputs[0] = output;
|
|
|
|
pair->outputs[1] = event->outputs[1];
|
|
|
|
scheduling_s * sUp = &pair->signalTimerUp;
|
|
|
|
scheduling_s * sDown = &pair->signalTimerDown;
|
|
|
|
|
|
|
|
pair->isScheduled = true;
|
|
|
|
pair->event = event;
|
|
|
|
efitimeus_t turnOnTime = nowUs + (int) injectionStartDelayUs;
|
|
|
|
bool isSecondaryOverlapping = turnOnTime < output->overlappingScheduleOffTime;
|
|
|
|
|
|
|
|
if (isSecondaryOverlapping) {
|
|
|
|
output->cancelNextTurningInjectorOff = true;
|
|
|
|
#if EFI_UNIT_TEST || EFI_SIMULATOR || defined(__DOXYGEN__)
|
|
|
|
printf("please cancel %s %d %d\r\n", output->name, (int)getTimeNowUs(), output->overlappingCounter);
|
|
|
|
#endif /* EFI_UNIT_TEST || EFI_SIMULATOR */
|
|
|
|
} else {
|
|
|
|
seScheduleByTime(sUp, turnOnTime, (schfunc_t) &seTurnPinHigh, pair);
|
|
|
|
}
|
|
|
|
efitimeus_t turnOffTime = nowUs + (int) (injectionStartDelayUs + durationUs);
|
|
|
|
seScheduleByTime(sDown, turnOffTime, (schfunc_t) &seTurnPinLow, pair);
|
2016-09-03 22:01:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
static void fuelClosedLoopCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2017-01-22 21:06:44 -08:00
|
|
|
#if ! EFI_UNIT_TEST
|
|
|
|
if (ENGINE(rpmCalculator.rpmValue) < CONFIG(fuelClosedLoopRpmThreshold) ||
|
2017-03-06 23:24:57 -08:00
|
|
|
ENGINE(sensors.clt) < CONFIG(fuelClosedLoopCltThreshold) ||
|
2017-05-15 20:28:49 -07:00
|
|
|
getTPS(PASS_ENGINE_PARAMETER_SIGNATURE) > CONFIG(fuelClosedLoopTpsThreshold) ||
|
2017-05-28 19:32:32 -07:00
|
|
|
ENGINE(sensors.currentAfr) < boardConfiguration->fuelClosedLoopAfrLowThreshold ||
|
2017-03-06 23:24:57 -08:00
|
|
|
ENGINE(sensors.currentAfr) > engineConfiguration->fuelClosedLoopAfrHighThreshold) {
|
2017-01-22 21:06:44 -08:00
|
|
|
engine->engineState.fuelPidCorrection = 0;
|
|
|
|
fuelPid.reset();
|
2017-01-22 13:04:09 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-06 23:24:57 -08:00
|
|
|
engine->engineState.fuelPidCorrection = fuelPid.getValue(ENGINE(engineState.targetAFR), ENGINE(sensors.currentAfr), 1);
|
2017-01-22 14:03:31 -08:00
|
|
|
if (engineConfiguration->debugMode == DBG_FUEL_PID_CORRECTION) {
|
|
|
|
tsOutputChannels.debugFloatField1 = engine->engineState.fuelPidCorrection;
|
|
|
|
fuelPid.postState(&tsOutputChannels);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2017-01-22 13:04:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
static ALWAYS_INLINE void handleFuel(const bool limitedFuel, uint32_t trgEventIndex, int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2017-03-27 19:57:13 -07:00
|
|
|
efiAssertVoid(getRemainingStack(chThdGetSelfX()) > 128, "lowstck#3");
|
2016-11-14 12:04:44 -08:00
|
|
|
efiAssertVoid(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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-09-03 22:01:54 -07:00
|
|
|
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
|
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
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
ENGINE(tpsAccelEnrichment.onNewValue(getTPS(PASS_ENGINE_PARAMETER_SIGNATURE) PASS_ENGINE_PARAMETER_SUFFIX));
|
|
|
|
ENGINE(engineLoadAccelEnrichment.onEngineCycle(PASS_ENGINE_PARAMETER_SIGNATURE));
|
2016-01-20 13:02:22 -08:00
|
|
|
|
2017-05-24 20:48:03 -07:00
|
|
|
ENGINE(fuelMs) = getInjectionDuration(rpm PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07: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];
|
2016-08-27 09:02:29 -07:00
|
|
|
uint32_t eventIndex = event->injectionStart.eventIndex;
|
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
|
|
|
|
// efiAssertVoid(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
|
|
|
}
|
2017-05-15 20:28:49 -07:00
|
|
|
handleFuelInjectionEvent(injEventIndex, event, rpm PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 08:02:49 -08:00
|
|
|
#if EFI_HISTOGRAMS || defined(__DOXYGEN__)
|
|
|
|
static histogram_s mainLoopHistogram;
|
|
|
|
#endif /* EFI_HISTOGRAMS */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
void showMainHistogram(void) {
|
2017-01-06 08:02:49 -08:00
|
|
|
#if EFI_HISTOGRAMS || defined(__DOXYGEN__)
|
|
|
|
printHistogram(logger, &mainLoopHistogram);
|
|
|
|
#endif /* EFI_HISTOGRAMS */
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2015-09-06 09:02:27 -07:00
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
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.
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t trgEventIndex DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2016-06-12 13:01:41 -07:00
|
|
|
(void) ckpSignalType;
|
|
|
|
|
2016-01-18 11:01:39 -08:00
|
|
|
ENGINE(m.beforeMainTrigger) = GET_TIMESTAMP();
|
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;
|
|
|
|
}
|
2017-03-27 19:57:13 -07:00
|
|
|
efiAssertVoid(getRemainingStack(chThdGetSelfX()) > 128, "lowstck#2");
|
2015-07-10 06:01:56 -07:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-18 10:13:26 -08:00
|
|
|
int rpm = ENGINE(rpmCalculator.rpmValue);
|
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) {
|
|
|
|
warning(OBD_Camshaft_Position_Sensor_Circuit_Range_Performance, "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;
|
|
|
|
}
|
2016-01-18 10:13:26 -08:00
|
|
|
bool limitedSpark = rpm > CONFIG(rpmHardLimit);
|
|
|
|
bool limitedFuel = rpm > CONFIG(rpmHardLimit);
|
2016-03-16 21:01:55 -07:00
|
|
|
|
|
|
|
if (CONFIG(boostCutPressure) !=0) {
|
|
|
|
if (getMap() > CONFIG(boostCutPressure)) {
|
|
|
|
limitedSpark = true;
|
|
|
|
limitedFuel = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 18:03:09 -07:00
|
|
|
if (limitedSpark || limitedFuel) {
|
2016-07-17 08:02:20 -07:00
|
|
|
// todo: this is not really a warning
|
2017-04-19 19:03:14 -07:00
|
|
|
warning(CUSTOM_SKIPPING_STROKE, "skipping stroke due to rpm=%d", rpm);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2015-09-06 09:02:27 -07:00
|
|
|
#if (EFI_HISTOGRAMS && EFI_PROD_CODE) || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
int beforeCallback = hal_lld_get_counter_value();
|
|
|
|
#endif
|
|
|
|
|
2016-09-04 10:03:39 -07:00
|
|
|
if (trgEventIndex == 0) {
|
2016-08-05 22:04:28 -07:00
|
|
|
|
2017-06-26 11:31:10 -07:00
|
|
|
if (checkIfTriggerConfigChanged()) {
|
2016-12-18 07:02:38 -08:00
|
|
|
engine->ignitionEvents.isReady = false; // we need to rebuild ignition schedule
|
2016-12-18 09:03:48 -08:00
|
|
|
engine->injectionEvents.isReady = false;
|
2016-08-27 10:01:55 -07:00
|
|
|
// todo: move 'triggerIndexByAngle' change into trigger initialization, why is it invoked from here if it's only about trigger shape & optimization?
|
2017-05-15 20:28:49 -07:00
|
|
|
prepareOutputSignals(PASS_ENGINE_PARAMETER_SIGNATURE);
|
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
|
|
|
}
|
2017-01-22 13:04:09 -08:00
|
|
|
|
|
|
|
if (CONFIG(fuelClosedLoopCorrectionEnabled)) {
|
2017-05-15 20:28:49 -07:00
|
|
|
fuelClosedLoopCorrection(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2017-01-22 13:04:09 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2016-02-27 20:03:34 -08:00
|
|
|
efiAssertVoid(!CONFIG(useOnlyRisingEdgeForTrigger) || CONFIG(ignMathCalculateAtIndex) % 2 == 0, "invalid ignMathCalculateAtIndex");
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-09-04 10:03:39 -07:00
|
|
|
if (trgEventIndex == CONFIG(ignMathCalculateAtIndex)) {
|
2016-01-18 11:01:39 -08:00
|
|
|
if (CONFIG(externalKnockSenseAdc) != EFI_ADC_NONE) {
|
2015-07-10 06:01:56 -07:00
|
|
|
float externalKnockValue = getVoltageDivided("knock", engineConfiguration->externalKnockSenseAdc);
|
|
|
|
engine->knockLogic(externalKnockValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
handleFuel(limitedFuel, trgEventIndex, rpm 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
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
handleSpark(limitedSpark, trgEventIndex, rpm PASS_ENGINE_PARAMETER_SUFFIX);
|
2017-01-06 08:02:49 -08:00
|
|
|
#if EFI_HISTOGRAMS || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
int diff = hal_lld_get_counter_value() - beforeCallback;
|
|
|
|
if (diff > 0)
|
2017-01-06 08:02:49 -08:00
|
|
|
hsAdd(&mainLoopHistogram, diff);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* EFI_HISTOGRAMS */
|
|
|
|
|
2016-09-04 10:03:39 -07:00
|
|
|
if (trgEventIndex == 0) {
|
2016-01-18 11:01:39 -08:00
|
|
|
ENGINE(m.mainTriggerCallbackTime) = GET_TIMESTAMP() - ENGINE(m.beforeMainTrigger);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 18:01:45 -07:00
|
|
|
#if EFI_ENGINE_SNIFFER || defined(__DOXYGEN__)
|
|
|
|
#include "engine_sniffer.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void showTriggerHistogram(void) {
|
|
|
|
printAllCallbacksHistogram();
|
|
|
|
showMainHistogram();
|
2015-07-15 18:01:45 -07:00
|
|
|
#if EFI_ENGINE_SNIFFER || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
showWaveChartHistogram();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void showMainInfo(Engine *engine) {
|
2015-09-05 21:02:34 -07:00
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
2017-05-15 20:28:49 -07:00
|
|
|
int rpm = engine->rpmCalculator.getRpm(PASS_ENGINE_PARAMETER_SIGNATURE);
|
|
|
|
float el = getEngineLoadT(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(logger, "rpm %d engine_load %f", rpm, el);
|
2017-05-15 20:28:49 -07:00
|
|
|
scheduleMsg(logger, "fuel %fms timing %f", getInjectionDuration(rpm PASS_ENGINE_PARAMETER_SUFFIX), engine->engineState.timingAdvance);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
efiAssertVoid(engine!=NULL, "null engine");
|
2016-09-21 20:03:22 -07:00
|
|
|
initSparkLogic(logger);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
|
|
|
addConsoleAction("performanceinfo", showTriggerHistogram);
|
|
|
|
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
|
|
|
|
|
2015-09-06 08:01:20 -07:00
|
|
|
#if EFI_HISTOGRAMS || defined(__DOXYGEN__)
|
2017-01-06 08:02:49 -08:00
|
|
|
initHistogram(&mainLoopHistogram, "main callback");
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* EFI_HISTOGRAMS */
|
|
|
|
|
|
|
|
addTriggerEventListener(mainTriggerCallback, "main loop", engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* EFI_ENGINE_CONTROL */
|