rusefi/firmware/controllers/trigger/main_trigger_callback.cpp

594 lines
21 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
2015-12-31 13:02:30 -08:00
* @author Andrey Belomutskiy, (c) 2012-2016
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
#define chThdSelf() 0
#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;
static LocalVersionHolder triggerVersion;
2016-07-17 19:02:34 -07:00
static const char *prevOutputName = NULL;
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
static void startSimultaniousInjection(Engine *engine) {
for (int i = 0; i < engine->engineConfiguration->specs.cylindersCount; i++) {
turnPinHigh(&enginePins.injectors[i]);
}
}
static void endSimultaniousInjection(Engine *engine) {
for (int i = 0; i < engine->engineConfiguration->specs.cylindersCount; i++) {
turnPinLow(&enginePins.injectors[i]);
}
}
2016-11-30 15:02:19 -08:00
static 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) {
// if (output->cancelNextTurningInjectorOff) {
// // how comes AutoTest.testFordAspire ends up here?
// } else {
// /**
// * #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 */
// output->cancelNextTurningInjectorOff = true;
2016-10-01 21:02:11 -07:00
return;
2016-09-26 18:03:18 -07:00
// }
}
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
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 */
turnPinHigh(output);
}
2016-11-30 15:02:19 -08:00
// todo: make these macro? kind of a penny optimization if compiler is not smart to inline
void seTurnPinHigh(OutputSignalPair *pair) {
InjectorOutputPin *output = pair->outputs[0];
tempTurnPinHigh(output);
}
static 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 */
return;
}
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
const char * w = output->currentLogicValue == false ? "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 */
output->overlappingCounter--;
if (output->overlappingCounter > 0) {
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
printf("was overlapping, no need to touch pin %s %d\r\n", output->name, (int)getTimeNowUs());
#endif /* FUEL_MATH_EXTREME_LOGGING */
2016-10-01 21:02:11 -07:00
return;
2016-09-26 18:03:18 -07:00
}
turnPinLow(output);
}
2016-11-30 15:02:19 -08:00
void seTurnPinLow(OutputSignalPair *pair) {
pair->isScheduled = false;
InjectorOutputPin *output = pair->outputs[0];
tempTurnPinLow(output);
}
2016-10-01 10:01:50 -07:00
static void seScheduleByTime(const char *prefix, scheduling_s *scheduling, efitimeus_t time, schfunc_t callback, OutputSignalPair *pair) {
2016-11-30 15:02:19 -08:00
InjectorOutputPin *param = pair->outputs[0];
2016-09-26 18:03:18 -07:00
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
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
#endif /* FUEL_MATH_EXTREME_LOGGING */
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
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 */
2016-10-02 13:03:12 -07:00
scheduleByTime(true, prefix, scheduling, time, callback, pair);
2016-09-26 18:03:18 -07:00
}
2016-11-30 12:02:43 -08:00
static void scheduleFuelInjection(int rpm, OutputSignal *signal, efitimeus_t nowUs, floatus_t delayUs, floatus_t durationUs, InjectorOutputPin *output DECLARE_ENGINE_PARAMETER_S) {
2016-09-03 22:01:54 -07:00
if (durationUs < 0) {
warning(CUSTOM_OBD_3, "duration cannot be negative: %d", durationUs);
return;
}
if (cisnan(durationUs)) {
2016-11-02 20:01:48 -07:00
warning(CUSTOM_NAN_DURACTION, "NaN in scheduleFuelInjection", durationUs);
2016-09-03 22:01:54 -07:00
return;
}
2016-09-25 21:03:15 -07:00
#if EFI_PRINTF_FUEL_DETAILS || defined(__DOXYGEN__)
printf("fuelout %s duration %d total=%d\t\n", output->name, (int)durationUs,
(int)MS2US(getCrankshaftRevolutionTimeMs(rpm)));
#endif /*EFI_PRINTF_FUEL_DETAILS */
2016-09-03 22:01:54 -07:00
efiAssertVoid(signal!=NULL, "signal is NULL");
int index = getRevolutionCounter() % 2;
2016-10-01 10:01:50 -07:00
OutputSignalPair *pair = &signal->signalPair[index];
2016-10-01 21:02:11 -07:00
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 */
2016-10-01 13:01:55 -07:00
return; // this OutputSignalPair is still needed for an extremely long injection scheduled previously
2016-10-01 21:02:11 -07:00
}
2016-11-30 15:02:19 -08:00
pair->outputs[0] = output;
2016-10-01 10:01:50 -07:00
scheduling_s * sUp = &pair->signalTimerUp;
scheduling_s * sDown = &pair->signalTimerDown;
2016-09-03 22:01:54 -07:00
2016-10-01 13:01:55 -07:00
pair->isScheduled = true;
2016-09-03 22:01:54 -07:00
efitimeus_t turnOnTime = nowUs + (int) delayUs;
bool isSecondaryOverlapping = turnOnTime < output->overlappingScheduleOffTime;
if (isSecondaryOverlapping) {
output->cancelNextTurningInjectorOff = true;
2016-09-23 21:03:14 -07:00
#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 */
2016-09-03 22:01:54 -07:00
} else {
2016-10-02 12:03:03 -07:00
seScheduleByTime("out up1", sUp, turnOnTime, (schfunc_t) &seTurnPinHigh, pair);
2016-09-03 22:01:54 -07:00
}
efitimeus_t turnOffTime = nowUs + (int) (delayUs + durationUs);
2016-10-01 10:01:50 -07:00
seScheduleByTime("out down", sDown, turnOffTime, (schfunc_t) &seTurnPinLow, pair);
2016-09-03 22:01:54 -07:00
}
2016-09-04 22:03:25 -07:00
static ALWAYS_INLINE void handleFuelInjectionEvent(int injEventIndex, InjectionEvent *event,
2015-10-19 19:02:51 -07:00
int rpm DECLARE_ENGINE_PARAMETER_S) {
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?
*/
2016-11-30 16:01:43 -08:00
const floatms_t injectionDuration = ENGINE(wallFuel).adjust(event->outputs[0]->injectorIndex, ENGINE(fuelMs) PASS_ENGINE_PARAMETER);
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
2016-09-26 17:02:24 -07:00
if (injectionDuration > getCrankshaftRevolutionTimeMs(rpm)) {
2016-11-20 20:01:49 -08:00
warning(CUSTOM_TOO_LONG_FUEL_INJECTION, "Too long fuel injection");
2016-09-26 17:02:24 -07:00
}
2016-07-23 20:03:45 -07:00
// todo: pre-calculate 'numberOfInjections'
floatms_t totalPerCycle = injectionDuration * getNumberOfInjections(engineConfiguration->injectionMode PASS_ENGINE_PARAMETER);
floatus_t engineCycleDuration = engine->rpmCalculator.oneDegreeUs * engine->engineCycle;
if (MS2US(totalPerCycle) > engineCycleDuration) {
warning(CUSTOM_OBD_26, "injector duty cycle too high %fms @ %d", totalPerCycle,
getRevolutionCounter());
}
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;
}
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
2016-09-04 22:03:25 -07:00
OutputSignal *signal = &ENGINE(engineConfiguration2)->fuelActuators[injEventIndex];
2016-01-24 23:03:01 -08:00
2016-09-04 22:03:25 -07:00
engine->engineConfiguration2->wasOverlapping[injEventIndex] = event->isOverlapping;
2016-09-02 23:03:32 -07: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.
*/
efiAssertVoid(signal!=NULL, "signal is NULL");
int index = getRevolutionCounter() % 2;
2016-10-01 13:01:55 -07:00
OutputSignalPair *pair = &signal->signalPair[index];
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
2016-10-02 12:03:03 -07:00
scheduleTask(true, "out up s", sUp, (int) injectionStartDelayUs, (schfunc_t) &startSimultaniousInjection, engine);
2016-10-02 08:02:09 -07:00
scheduleTask(true, "out down", sDown, (int) injectionStartDelayUs + MS2US(injectionDuration),
2015-10-19 19:02:51 -07:00
(schfunc_t) &endSimultaniousInjection, engine);
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;
2016-07-17 19:02:34 -07:00
if (prevOutputName == outputName) {
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
}
2016-11-30 16:01:43 -08:00
scheduleFuelInjection(rpm, signal, getTimeNowUs(), injectionStartDelayUs, MS2US(injectionDuration), event->outputs[0] PASS_ENGINE_PARAMETER);
2016-09-03 22:01:54 -07:00
}
}
2016-09-26 18:03:18 -07:00
/**
*
* @param delay the number of ticks before the output signal
* immediate output if delay is zero
* @param dwell the number of ticks of output duration
*
*/
2016-09-26 21:02:27 -07:00
static void scheduleOutput2(OutputSignalPair *pair, efitimeus_t nowUs, float delayUs, float durationUs, InjectorOutputPin *output) {
2016-09-26 18:03:18 -07:00
#if EFI_GPIO || defined(__DOXYGEN__)
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
printf("scheduling output %s\r\n", output->name);
#endif /* EFI_UNIT_TEST */
efitimeus_t turnOnTime = nowUs + (int) delayUs;
2016-09-26 21:02:27 -07:00
scheduling_s *sUp = &pair->signalTimerUp;
2016-10-01 13:01:55 -07:00
pair->isScheduled = true;
2016-09-26 21:02:27 -07:00
scheduling_s *sDown = &pair->signalTimerDown;
2016-11-30 15:02:19 -08:00
pair->outputs[0] = output;
2016-10-02 12:03:03 -07:00
seScheduleByTime("out up2", sUp, turnOnTime, (schfunc_t) &seTurnPinHigh, pair);
2016-09-26 18:03:18 -07:00
efitimeus_t turnOffTime = nowUs + (int) (delayUs + durationUs);
2016-10-01 10:01:50 -07:00
seScheduleByTime("out down", sDown, turnOffTime, (schfunc_t) &seTurnPinLow, pair);
2016-09-26 18:03:18 -07:00
#endif /* EFI_GPIO */
}
2016-09-03 22:01:54 -07:00
static void handleFuelScheduleOverlap(InjectionEventList *injectionEvents DECLARE_ENGINE_PARAMETER_S) {
/**
* here we need to avoid a fuel miss due to changes between previous and current fuel schedule
* see https://sourceforge.net/p/rusefi/tickets/299/
* see testFuelSchedulerBug299smallAndLarge unit test
*/
//
for (int injEventIndex = 0; injEventIndex < injectionEvents->size; injEventIndex++) {
InjectionEvent *event = &injectionEvents->elements[injEventIndex];
if (!engine->engineConfiguration2->wasOverlapping[injEventIndex] && event->isOverlapping) {
// we are here if new fuel schedule is crossing engine cycle boundary with this event
2016-11-30 16:01:43 -08:00
InjectorOutputPin *output = event->outputs[0];
2016-09-03 22:01:54 -07:00
// todo: recalc fuel? account for wetting?
floatms_t injectionDuration = ENGINE(fuelMs);
2016-09-26 21:02:27 -07:00
OutputSignalPair* pair = &ENGINE(engineConfiguration2)->overlappingFuelActuator[injEventIndex];
2016-10-01 21:02:11 -07:00
if (pair->isScheduled) {
#if EFI_UNIT_TEST || EFI_SIMULATOR || defined(__DOXYGEN__)
printf("still used2 %s %d\r\n", output->name, (int)getTimeNowUs());
#endif /* EFI_UNIT_TEST || EFI_SIMULATOR */
2016-10-01 13:01:55 -07:00
continue; // this OutputSignalPair is still needed for an extremely long injection scheduled previously
2016-10-01 21:02:11 -07:00
}
2016-09-03 22:01:54 -07:00
efitimeus_t nowUs = getTimeNowUs();
output->overlappingScheduleOffTime = nowUs + MS2US(injectionDuration);
2016-09-26 21:02:27 -07:00
scheduleOutput2(pair, nowUs, 0, MS2US(injectionDuration), output);
2016-09-03 22:01:54 -07:00
}
2015-07-10 06:01:56 -07:00
}
}
2016-09-04 10:03:39 -07:00
static ALWAYS_INLINE void handleFuel(const bool limitedFuel, uint32_t trgEventIndex, int rpm DECLARE_ENGINE_PARAMETER_S) {
2015-07-10 06:01:56 -07:00
efiAssertVoid(getRemainingStack(chThdSelf()) > 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
2016-09-03 22:01:54 -07:00
if (!isInjectionEnabled(engineConfiguration) || limitedFuel) {
2016-09-03 05:03:07 -07:00
return;
}
2016-09-20 21:02:15 -07:00
if (engine->isCylinderCleanupMode) {
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
*/
2016-08-05 22:04:28 -07:00
FuelSchedule *fs = engine->fuelScheduleForThisEngineCycle;
2016-07-17 08:02:20 -07:00
InjectionEventList *injectionEvents = &fs->injectionEvents;
2015-07-10 06:01:56 -07:00
2016-09-04 10:03:39 -07:00
if (trgEventIndex == 0) {
2016-09-03 22:01:54 -07:00
handleFuelScheduleOverlap(injectionEvents PASS_ENGINE_PARAMETER);
2016-09-02 23:03:32 -07:00
}
2016-09-04 10:03:39 -07:00
if (!fs->hasEvents[trgEventIndex]) {
2016-09-03 22:01:54 -07:00
// that's a performance optimization
2015-07-10 06:01:56 -07:00
return;
2016-09-03 22:01:54 -07: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
2016-01-31 10:03:08 -08:00
ENGINE(tpsAccelEnrichment.onNewValue(getTPS(PASS_ENGINE_PARAMETER_F) PASS_ENGINE_PARAMETER));
2016-01-18 11:01:39 -08:00
ENGINE(engineLoadAccelEnrichment.onEngineCycle(PASS_ENGINE_PARAMETER_F));
2016-01-20 13:02:22 -08:00
2016-08-26 15:02:39 -07:00
ENGINE(fuelMs) = getInjectionDuration(rpm PASS_ENGINE_PARAMETER) * CONFIG(globalFuelCorrection);
2015-07-10 06:01:56 -07:00
2016-07-17 17:01:54 -07:00
for (int injEventIndex = 0; injEventIndex < injectionEvents->size; injEventIndex++) {
InjectionEvent *event = &injectionEvents->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
}
2016-09-03 05:03:07 -07:00
handleFuelInjectionEvent(injEventIndex, event, rpm PASS_ENGINE_PARAMETER);
2015-07-10 06:01:56 -07:00
}
}
static histogram_s mainLoopHisto;
void showMainHistogram(void) {
2015-09-06 09:02:27 -07:00
#if EFI_PROD_CODE || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
printHistogram(logger, &mainLoopHisto);
#endif
}
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.
*/
2016-09-04 10:03:39 -07:00
void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t trgEventIndex DECLARE_ENGINE_PARAMETER_S) {
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;
}
efiAssertVoid(getRemainingStack(chThdSelf()) > 128, "lowstck#2");
2016-11-14 12:04:44 -08: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
2016-07-13 19:02:35 -07:00
warning(CUSTOM_OBD_34, "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-01-18 10:13:26 -08:00
int revolutionIndex = ENGINE(rpmCalculator).getRevolutionCounter() % 2;
2015-07-10 06:01:56 -07:00
2016-09-04 10:03:39 -07:00
if (trgEventIndex == 0) {
2016-08-05 22:04:28 -07:00
// these two statements should be atomic, but in reality we should be fine, right?
engine->fuelScheduleForThisEngineCycle = ENGINE(engineConfiguration2)->injectionEvents;
engine->fuelScheduleForThisEngineCycle->usedAtEngineCycle = ENGINE(rpmCalculator).getRevolutionCounter();
2016-04-25 20:01:59 -07:00
if (triggerVersion.isOld()) {
2016-11-29 16:02:50 -08:00
engine->ignitionList()->isReady = false; // we need to rebuild ignition schedule
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?
2015-07-10 06:01:56 -07:00
prepareOutputSignals(PASS_ENGINE_PARAMETER_F);
2016-08-27 10:01:55 -07:00
// we need this to apply new 'triggerIndexByAngle' values
engine->periodicFastCallback(PASS_ENGINE_PARAMETER_F);
2016-04-25 20:01:59 -07: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
*/
2016-09-04 10:03:39 -07:00
handleFuel(limitedFuel, trgEventIndex, rpm PASS_ENGINE_PARAMETER);
2015-09-26 06:01:32 -07:00
/**
* For spark we schedule both start of coil charge and actual spark based on trigger angle
*/
2016-11-28 12:01:54 -08:00
handleSpark(limitedSpark, trgEventIndex, rpm PASS_ENGINE_PARAMETER);
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 diff = hal_lld_get_counter_value() - beforeCallback;
if (diff > 0)
hsAdd(&mainLoopHisto, diff);
#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__)
2016-01-18 09:03:32 -08:00
int rpm = engine->rpmCalculator.getRpm(PASS_ENGINE_PARAMETER_F);
2015-07-10 06:01:56 -07:00
float el = getEngineLoadT(PASS_ENGINE_PARAMETER_F);
scheduleMsg(logger, "rpm %d engine_load %f", rpm, el);
2016-08-26 15:02:39 -07:00
scheduleMsg(logger, "fuel %fms timing %f", getInjectionDuration(rpm PASS_ENGINE_PARAMETER), engine->engineState.timingAdvance);
2015-07-10 06:01:56 -07:00
#endif
}
void initMainEventListener(Logging *sharedLogger, Engine *engine) {
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());
2016-09-15 11:02:45 -07:00
if (!isInjectionEnabled(engine->engineConfiguration))
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__)
2015-07-10 06:01:56 -07:00
initHistogram(&mainLoopHisto, "main callback");
#endif /* EFI_HISTOGRAMS */
addTriggerEventListener(mainTriggerCallback, "main loop", engine);
}
#endif /* EFI_ENGINE_CONTROL */