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"
|
|
|
|
#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"
|
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
|
|
|
|
|
|
|
extern engine_pins_s enginePins;
|
|
|
|
static MainTriggerCallback mainTriggerCallbackInstance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In order to archive higher event precision, we are using a hybrid approach
|
|
|
|
* where we are scheduling events based on the closest trigger event with a time offset.
|
|
|
|
*
|
|
|
|
* This queue is using global trigger event index as 'time'
|
|
|
|
*/
|
|
|
|
//static EventQueue triggerEventsQueue;
|
|
|
|
static cyclic_buffer<int> ignitionErrorDetection;
|
|
|
|
|
|
|
|
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-01-24 23:03:01 -08:00
|
|
|
static ALWAYS_INLINE void handleFuelInjectionEvent(int eventIndex, bool limitedFuel, InjectionEvent *event,
|
2015-10-19 19:02:51 -07:00
|
|
|
int rpm DECLARE_ENGINE_PARAMETER_S) {
|
2016-07-17 17:01:54 -07:00
|
|
|
if (limitedFuel) {
|
2016-01-20 14:01:53 -08:00
|
|
|
return; // todo: move this check up
|
2016-07-17 17:01:54 -07:00
|
|
|
}
|
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-07-17 07:13:22 -07:00
|
|
|
const floatms_t injectionDuration = ENGINE(wallFuel).adjust(event->injectorIndex, ENGINE(fuelMs) PASS_ENGINE_PARAMETER);
|
2015-08-23 20:02:37 -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-08-05 22:04:28 -07:00
|
|
|
// scheduleMsg(logger, "handleFuel totalPerCycle=%f", totalPerCycle);
|
|
|
|
// scheduleMsg(logger, "handleFuel engineCycleDuration=%f", engineCycleDuration);
|
|
|
|
|
2016-07-17 17:01:54 -07:00
|
|
|
if (engine->isCylinderCleanupMode) {
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
2016-07-17 17:01:54 -07:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
floatus_t injectionStartDelayUs = ENGINE(rpmCalculator.oneDegreeUs) * event->injectionStart.angleOffset;
|
|
|
|
|
2016-08-05 22:04:28 -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());
|
|
|
|
|
2016-01-24 23:03:01 -08:00
|
|
|
OutputSignal *signal = &ENGINE(engineConfiguration2)->fuelActuators[eventIndex];
|
|
|
|
|
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;
|
|
|
|
scheduling_s * sUp = &signal->signalTimerUp[index];
|
|
|
|
scheduling_s * sDown = &signal->signalTimerDown[index];
|
|
|
|
|
2016-01-20 14:01:53 -08:00
|
|
|
scheduleTask("out up", sUp, (int) injectionStartDelayUs, (schfunc_t) &startSimultaniousInjection, engine);
|
|
|
|
scheduleTask("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
|
|
|
|
if (rpm > 2 * engineConfiguration->cranking.rpm) {
|
|
|
|
const char *outputName = event->output->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-01-24 23:03:01 -08:00
|
|
|
scheduleOutput(signal, getTimeNowUs(), injectionStartDelayUs, MS2US(injectionDuration), event->output);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 14:01:33 -08:00
|
|
|
static ALWAYS_INLINE void handleFuel(bool limitedFuel, uint32_t eventIndex, int rpm DECLARE_ENGINE_PARAMETER_S) {
|
2016-01-18 11:01:39 -08:00
|
|
|
if (!isInjectionEnabled(engineConfiguration))
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
|
|
|
efiAssertVoid(getRemainingStack(chThdSelf()) > 128, "lowstck#3");
|
2016-01-18 11:01:39 -08:00
|
|
|
efiAssertVoid(eventIndex < ENGINE(triggerShape.getLength()), "handleFuel/event index");
|
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;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-07-17 08:02:20 -07:00
|
|
|
InjectionEventList *injectionEvents = &fs->injectionEvents;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
if (!fs->hasEvents[eventIndex])
|
|
|
|
return;
|
|
|
|
|
2016-08-05 22:04:28 -07:00
|
|
|
// scheduleMsg(logger, "handleFuel ind=%d %d", eventIndex, getRevolutionCounter());
|
|
|
|
|
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-01-18 11:01:39 -08:00
|
|
|
ENGINE(fuelMs) = getFuelMs(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-04-26 18:02:55 -07:00
|
|
|
if (event->injectionStart.eventIndex != eventIndex) {
|
2015-07-10 06:01:56 -07:00
|
|
|
continue;
|
2016-04-26 18:02:55 -07:00
|
|
|
}
|
2016-07-17 17:01:54 -07:00
|
|
|
handleFuelInjectionEvent(injEventIndex, limitedFuel, event, rpm PASS_ENGINE_PARAMETER);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 20:01:59 -07:00
|
|
|
void turnSparkPinLow(NamedOutputPin *output) {
|
|
|
|
turnPinLow(output);
|
2016-04-26 18:02:55 -07:00
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
|
|
|
if (CONFIG(dizzySparkOutputPin) != GPIO_UNASSIGNED) {
|
|
|
|
doSetOutputPinValue2(&enginePins.dizzyOutput, false);
|
|
|
|
}
|
|
|
|
#endif
|
2016-04-25 20:01:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void turnSparkPinHigh(NamedOutputPin *output) {
|
|
|
|
turnPinHigh(output);
|
2016-04-26 18:02:55 -07:00
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
|
|
|
if (CONFIG(dizzySparkOutputPin) != GPIO_UNASSIGNED) {
|
|
|
|
doSetOutputPinValue2(&enginePins.dizzyOutput, true);
|
|
|
|
}
|
|
|
|
#endif
|
2016-04-25 20:01:59 -07:00
|
|
|
}
|
|
|
|
|
2016-01-11 14:01:33 -08:00
|
|
|
static ALWAYS_INLINE void handleSparkEvent(bool limitedSpark, uint32_t eventIndex, IgnitionEvent *iEvent,
|
2015-07-10 06:01:56 -07:00
|
|
|
int rpm DECLARE_ENGINE_PARAMETER_S) {
|
|
|
|
|
2016-01-18 11:01:39 -08:00
|
|
|
float dwellMs = ENGINE(engineState.sparkDwell);
|
2015-07-10 06:01:56 -07:00
|
|
|
if (cisnan(dwellMs) || dwellMs < 0) {
|
2016-07-17 08:02:20 -07:00
|
|
|
warning(CUSTOM_OBD_45, "invalid dwell: %f at %d", dwellMs, rpm);
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-18 11:01:39 -08:00
|
|
|
floatus_t chargeDelayUs = ENGINE(rpmCalculator.oneDegreeUs) * iEvent->dwellPosition.angleOffset;
|
2015-07-10 06:01:56 -07:00
|
|
|
int isIgnitionError = chargeDelayUs < 0;
|
|
|
|
ignitionErrorDetection.add(isIgnitionError);
|
|
|
|
if (isIgnitionError) {
|
2015-09-06 09:02:27 -07:00
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(logger, "Negative spark delay=%f", chargeDelayUs);
|
|
|
|
#endif
|
|
|
|
chargeDelayUs = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cisnan(dwellMs)) {
|
|
|
|
firmwareError("NaN in scheduleOutput", dwellMs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We are alternating two event lists in order to avoid a potential issue around revolution boundary
|
|
|
|
* when an event is scheduled within the next revolution.
|
|
|
|
*/
|
|
|
|
scheduling_s * sUp = &iEvent->signalTimerUp;
|
|
|
|
scheduling_s * sDown = &iEvent->signalTimerDown;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The start of charge is always within the current trigger event range, so just plain time-based scheduling
|
|
|
|
*/
|
2015-09-27 18:03:09 -07:00
|
|
|
if (!limitedSpark) {
|
2016-01-30 19:03:36 -08:00
|
|
|
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
|
|
|
|
printf("spark charge delay=%f\r\n", chargeDelayUs);
|
|
|
|
#endif
|
2015-09-27 18:03:09 -07:00
|
|
|
/**
|
|
|
|
* Note how we do not check if spark is limited or not while scheduling 'spark down'
|
|
|
|
* This way we make sure that coil dwell started while spark was enabled would fire and not burn
|
|
|
|
* the coil.
|
|
|
|
*/
|
2016-04-25 20:01:59 -07:00
|
|
|
scheduleTask("spark up", sUp, chargeDelayUs, (schfunc_t) &turnSparkPinHigh, iEvent->output);
|
2015-09-27 18:03:09 -07:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* Spark event is often happening during a later trigger event timeframe
|
|
|
|
* TODO: improve precision
|
|
|
|
*/
|
|
|
|
|
|
|
|
findTriggerPosition(&iEvent->sparkPosition, iEvent->advance PASS_ENGINE_PARAMETER);
|
|
|
|
|
|
|
|
if (iEvent->sparkPosition.eventIndex == eventIndex) {
|
|
|
|
/**
|
|
|
|
* Spark should be fired before the next trigger event - time-based delay is best precision possible
|
|
|
|
*/
|
2016-01-18 11:01:39 -08:00
|
|
|
float timeTillIgnitionUs = ENGINE(rpmCalculator.oneDegreeUs) * iEvent->sparkPosition.angleOffset;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-01-30 19:03:36 -08:00
|
|
|
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
|
|
|
|
printf("spark delay=%f angle=%f\r\n", timeTillIgnitionUs, iEvent->sparkPosition.angleOffset);
|
|
|
|
#endif
|
|
|
|
|
2016-04-25 20:01:59 -07:00
|
|
|
scheduleTask("spark1 down", sDown, (int) timeTillIgnitionUs, (schfunc_t) &turnSparkPinLow, iEvent->output);
|
2015-07-10 06:01:56 -07:00
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* Spark should be scheduled in relation to some future trigger event, this way we get better firing precision
|
|
|
|
*/
|
2016-01-26 20:01:44 -08:00
|
|
|
bool isPending = assertNotInList<IgnitionEvent>(ENGINE(iHead), iEvent);
|
2015-07-10 06:01:56 -07:00
|
|
|
if (isPending)
|
|
|
|
return;
|
|
|
|
|
2016-01-26 20:01:44 -08:00
|
|
|
LL_APPEND(ENGINE(iHead), iEvent);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 14:01:33 -08:00
|
|
|
static ALWAYS_INLINE void handleSpark(bool limitedSpark, uint32_t eventIndex, int rpm,
|
2015-07-10 06:01:56 -07:00
|
|
|
IgnitionEventList *list DECLARE_ENGINE_PARAMETER_S) {
|
2016-07-17 08:02:20 -07:00
|
|
|
if (!isValidRpm(rpm) || !CONFIG(isIgnitionEnabled)) {
|
|
|
|
// this might happen for instance in case of a single trigger event after a pause
|
|
|
|
return;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* Ignition schedule is defined once per revolution
|
|
|
|
* See initializeIgnitionActions()
|
|
|
|
*/
|
|
|
|
|
|
|
|
IgnitionEvent *current, *tmp;
|
|
|
|
|
2016-01-26 20:01:44 -08:00
|
|
|
LL_FOREACH_SAFE(ENGINE(iHead), current, tmp)
|
2015-07-10 06:01:56 -07:00
|
|
|
{
|
|
|
|
if (current->sparkPosition.eventIndex == eventIndex) {
|
|
|
|
// time to fire a spark which was scheduled previously
|
2016-01-26 20:01:44 -08:00
|
|
|
LL_DELETE(ENGINE(iHead), current);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
scheduling_s * sDown = ¤t->signalTimerDown;
|
|
|
|
|
|
|
|
float timeTillIgnitionUs = ENGINE(rpmCalculator.oneDegreeUs) * current->sparkPosition.angleOffset;
|
2016-04-25 20:01:59 -07:00
|
|
|
scheduleTask("spark 2down", sDown, (int) timeTillIgnitionUs, (schfunc_t) &turnSparkPinLow, current->output);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// scheduleSimpleMsg(&logger, "eventId spark ", eventIndex);
|
|
|
|
for (int i = 0; i < list->size; i++) {
|
|
|
|
IgnitionEvent *event = &list->elements[i];
|
|
|
|
if (event->dwellPosition.eventIndex != eventIndex)
|
|
|
|
continue;
|
2015-09-27 18:03:09 -07:00
|
|
|
handleSparkEvent(limitedSpark, eventIndex, 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
|
|
|
// todo: the method name is not correct any more - no calc is done here anymore
|
|
|
|
static ALWAYS_INLINE void ignitionMathCalc(int rpm DECLARE_ENGINE_PARAMETER_S) {
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* Within one engine cycle all cylinders are fired with same timing advance.
|
|
|
|
* todo: one day we can control cylinders individually?
|
|
|
|
*/
|
|
|
|
float dwellMs = ENGINE(engineState.sparkDwell);
|
|
|
|
|
|
|
|
if (cisnan(dwellMs) || dwellMs < 0) {
|
|
|
|
firmwareError("invalid dwell: %f at %d", dwellMs, rpm);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2015-09-06 09:02:27 -07:00
|
|
|
static ALWAYS_INLINE void scheduleIgnitionAndFuelEvents(int rpm, int revolutionIndex DECLARE_ENGINE_PARAMETER_S) {
|
|
|
|
|
|
|
|
engine->m.beforeIgnitionSch = GET_TIMESTAMP();
|
|
|
|
/**
|
|
|
|
* TODO: warning. there is a bit of a hack here, todo: improve.
|
|
|
|
* currently output signals/times signalTimerUp from the previous revolutions could be
|
|
|
|
* still used because they have crossed the revolution boundary
|
|
|
|
* but we are already re-purposing the output signals, but everything works because we
|
|
|
|
* are not affecting that space in memory. todo: use two instances of 'ignitionSignals'
|
|
|
|
*/
|
2015-11-12 10:02:38 -08:00
|
|
|
float maxAllowedDwellAngle = (int) (getEngineCycle(engineConfiguration->operationMode) / 2); // the cast is about making Coverity happy
|
2015-09-06 09:02:27 -07:00
|
|
|
|
|
|
|
if (engineConfiguration->ignitionMode == IM_ONE_COIL) {
|
2015-11-12 10:02:38 -08:00
|
|
|
maxAllowedDwellAngle = getEngineCycle(engineConfiguration->operationMode) / engineConfiguration->specs.cylindersCount / 1.1;
|
2015-09-06 09:02:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (engine->engineState.dwellAngle == 0) {
|
2016-07-13 19:02:35 -07:00
|
|
|
warning(CUSTOM_OBD_32, "dwell is zero?");
|
2015-09-06 09:02:27 -07:00
|
|
|
}
|
|
|
|
if (engine->engineState.dwellAngle > maxAllowedDwellAngle) {
|
2016-07-13 19:02:35 -07:00
|
|
|
warning(CUSTOM_OBD_33, "dwell angle too long: %f", engine->engineState.dwellAngle);
|
2015-09-06 09:02:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// todo: add some check for dwell overflow? like 4 times 6 ms while engine cycle is less then that
|
|
|
|
|
|
|
|
IgnitionEventList *list = &engine->engineConfiguration2->ignitionEvents[revolutionIndex];
|
|
|
|
|
2016-01-18 11:01:39 -08:00
|
|
|
if (cisnan(ENGINE(engineState.timingAdvance))) {
|
2015-09-06 09:02:27 -07:00
|
|
|
// error should already be reported
|
|
|
|
list->reset(); // reset is needed to clear previous ignition schedule
|
|
|
|
return;
|
|
|
|
}
|
2016-01-18 11:01:39 -08:00
|
|
|
initializeIgnitionActions(ENGINE(engineState.timingAdvance), ENGINE(engineState.dwellAngle), list PASS_ENGINE_PARAMETER);
|
2015-09-06 09:02:27 -07:00
|
|
|
engine->m.ignitionSchTime = GET_TIMESTAMP() - engine->m.beforeIgnitionSch;
|
|
|
|
}
|
|
|
|
|
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(trigger_event_e ckpSignalType, uint32_t eventIndex 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-06-12 13:01:41 -07:00
|
|
|
if (eventIndex >= ENGINE(triggerShape.getLength())) {
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
|
|
if (eventIndex == 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()) {
|
2015-07-10 06:01:56 -07:00
|
|
|
prepareOutputSignals(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-01-18 11:01:39 -08:00
|
|
|
if (eventIndex == CONFIG(ignMathCalculateAtIndex)) {
|
|
|
|
if (CONFIG(externalKnockSenseAdc) != EFI_ADC_NONE) {
|
2015-07-10 06:01:56 -07:00
|
|
|
float externalKnockValue = getVoltageDivided("knock", engineConfiguration->externalKnockSenseAdc);
|
|
|
|
engine->knockLogic(externalKnockValue);
|
|
|
|
}
|
|
|
|
|
2016-01-18 11:01:39 -08:00
|
|
|
ENGINE(m.beforeIgnitionMath) = GET_TIMESTAMP();
|
2015-09-06 09:02:27 -07:00
|
|
|
ignitionMathCalc(rpm PASS_ENGINE_PARAMETER);
|
2016-01-18 11:01:39 -08:00
|
|
|
ENGINE(m.ignitionMathTime) = GET_TIMESTAMP() - ENGINE(m.beforeIgnitionMath);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (eventIndex == 0) {
|
2015-09-06 09:02:27 -07:00
|
|
|
scheduleIgnitionAndFuelEvents(rpm, revolutionIndex PASS_ENGINE_PARAMETER);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// triggerEventsQueue.executeAll(getCrankEventCounter());
|
|
|
|
|
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
|
|
|
|
*/
|
2015-09-27 18:03:09 -07:00
|
|
|
handleFuel(limitedFuel, eventIndex, 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
|
|
|
|
*/
|
2015-10-19 19:02:51 -07:00
|
|
|
handleSpark(limitedSpark, eventIndex, rpm,
|
|
|
|
&engine->engineConfiguration2->ignitionEvents[revolutionIndex] 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 */
|
|
|
|
|
|
|
|
if (eventIndex == 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
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainTriggerCallback::init(Engine *engine) {
|
|
|
|
efiAssertVoid(engine!=NULL, "engine NULL");
|
|
|
|
this->engine = engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-10-19 19:02:51 -07:00
|
|
|
scheduleMsg(logger, "fuel %fms timing %f", getFuelMs(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");
|
|
|
|
|
|
|
|
mainTriggerCallbackInstance.init(engine);
|
|
|
|
|
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
|
|
|
addConsoleAction("performanceinfo", showTriggerHistogram);
|
|
|
|
addConsoleActionP("maininfo", (VoidPtr) showMainInfo, engine);
|
|
|
|
|
|
|
|
printMsg(logger, "initMainLoop: %d", currentTimeMillis());
|
|
|
|
if (!isInjectionEnabled(mainTriggerCallbackInstance.engine->engineConfiguration))
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
int isIgnitionTimingError(void) {
|
|
|
|
return ignitionErrorDetection.sum(6) > 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* EFI_ENGINE_CONTROL */
|