2015-07-10 06:01:56 -07:00
|
|
|
/*
|
|
|
|
* @file trigger_central.cpp
|
|
|
|
*
|
|
|
|
* @date Feb 23, 2014
|
2017-01-03 03:05:22 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2017
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__)
|
|
|
|
|
|
|
|
#include "trigger_central.h"
|
|
|
|
#include "trigger_decoder.h"
|
|
|
|
#include "main_trigger_callback.h"
|
|
|
|
#include "engine_configuration.h"
|
|
|
|
#include "listener_array.h"
|
|
|
|
#include "data_buffer.h"
|
|
|
|
#include "histogram.h"
|
|
|
|
#include "pwm_generator_logic.h"
|
|
|
|
#include "efilib2.h"
|
|
|
|
#include "settings.h"
|
2016-08-20 20:02:09 -07:00
|
|
|
#include "engine_math.h"
|
2017-06-28 08:06:40 -07:00
|
|
|
#include "LocalVersionHolder.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#include "rpm_calculator.h"
|
2015-09-08 19:03:12 -07:00
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "rfiutil.h"
|
|
|
|
#include "pin_repository.h"
|
2016-08-23 20:03:01 -07:00
|
|
|
#include "tunerstudio.h"
|
|
|
|
extern TunerStudioOutputChannels tsOutputChannels;
|
|
|
|
#endif /* EFI_PROD_CODE */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-09-08 19:03:12 -07:00
|
|
|
#if EFI_ENGINE_SNIFFER || defined(__DOXYGEN__)
|
2015-07-15 18:01:45 -07:00
|
|
|
#include "engine_sniffer.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
WaveChart waveChart;
|
2015-07-15 18:01:45 -07:00
|
|
|
#endif /* EFI_ENGINE_SNIFFER */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-09-13 14:02:44 -07:00
|
|
|
EXTERN_ENGINE;
|
|
|
|
|
2017-01-06 08:02:49 -08:00
|
|
|
#if EFI_HISTOGRAMS || defined(__DOXYGEN__)
|
|
|
|
static histogram_s triggerCallbackHistogram;
|
|
|
|
#endif /* EFI_HISTOGRAMS */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static Logging *logger;
|
2017-06-28 08:06:40 -07:00
|
|
|
static LocalVersionHolder triggerVersion;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
efitime_t getCrankEventCounter(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2015-09-13 14:02:44 -07:00
|
|
|
return engine->triggerCentral.triggerState.getTotalEventCounter();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
efitime_t getStartOfRevolutionIndex(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2015-09-13 14:02:44 -07:00
|
|
|
return engine->triggerCentral.triggerState.getStartOfRevolutionIndex();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerCentral::addEventListener(ShaftPositionListener listener, const char *name, Engine *engine) {
|
|
|
|
print("registerCkpListener: %s\r\n", name);
|
|
|
|
triggerListeneres.registerCallback((VoidInt) listener, engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Adds a trigger event listener
|
|
|
|
*
|
|
|
|
* Trigger event listener would be invoked on each trigger event. For example, for a 60/2 wheel
|
2016-02-27 20:03:34 -08:00
|
|
|
* that would be 116 events: 58 SHAFT_PRIMARY_RISING and 58 SHAFT_PRIMARY_FALLING events.
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
void addTriggerEventListener(ShaftPositionListener listener, const char *name, Engine *engine) {
|
2015-09-13 14:02:44 -07:00
|
|
|
engine->triggerCentral.addEventListener(listener, name, engine);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t triggerHanlderEntryTime;
|
|
|
|
|
|
|
|
#if (EFI_PROD_CODE || EFI_SIMULATOR) || defined(__DOXYGEN__)
|
|
|
|
EXTERN_ENGINE
|
|
|
|
;
|
|
|
|
|
|
|
|
int triggerReentraint = 0;
|
|
|
|
int maxTriggerReentraint = 0;
|
|
|
|
uint32_t triggerDuration;
|
|
|
|
uint32_t triggerMaxDuration = 0;
|
|
|
|
|
2017-01-02 12:03:26 -08:00
|
|
|
static bool isInsideTriggerHandler = false;
|
|
|
|
|
2017-06-26 11:31:10 -07:00
|
|
|
static bool isTriggerConfigChanged = false;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-11-11 20:02:49 -08:00
|
|
|
efitick_t previousVvtCamTime = 0;
|
|
|
|
efitick_t previousVvtCamDuration = 0;
|
|
|
|
|
2016-08-20 19:02:12 -07:00
|
|
|
void hwHandleVvtCamSignal(trigger_value_e front) {
|
2016-08-23 20:03:01 -07:00
|
|
|
if (ENGINE(isEngineChartEnabled)) {
|
|
|
|
// this is a performance optimization - array index is cheaper then invoking a method with 'switch'
|
|
|
|
addEngineSniffferEvent(VVT_NAME, front == TV_RISE ? WC_UP : WC_DOWN);
|
|
|
|
}
|
|
|
|
|
2016-11-12 21:01:42 -08:00
|
|
|
if (boardConfiguration->vvtCamSensorUseRise ^ (front != TV_FALL)) {
|
2016-08-23 20:03:01 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TriggerCentral *tc = &engine->triggerCentral;
|
2016-08-20 19:02:12 -07:00
|
|
|
|
2016-11-11 20:02:49 -08:00
|
|
|
efitick_t nowNt = getTimeNowNt();
|
|
|
|
|
2016-11-14 20:01:47 -08:00
|
|
|
if (engineConfiguration->vvtMode == MIATA_NB2) {
|
2016-11-11 20:02:49 -08:00
|
|
|
uint32_t currentDuration = nowNt - previousVvtCamTime;
|
|
|
|
float ratio = ((float) currentDuration) / previousVvtCamDuration;
|
|
|
|
|
|
|
|
|
|
|
|
previousVvtCamDuration = currentDuration;
|
|
|
|
previousVvtCamTime = nowNt;
|
|
|
|
|
|
|
|
if (engineConfiguration->isPrintTriggerSynchDetails) {
|
|
|
|
scheduleMsg(logger, "vvt ratio %f", ratio);
|
|
|
|
}
|
|
|
|
if (ratio < boardConfiguration->nb2ratioFrom || ratio > boardConfiguration->nb2ratioTo) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-14 20:01:47 -08:00
|
|
|
if (engineConfiguration->isPrintTriggerSynchDetails) {
|
|
|
|
scheduleMsg(logger, "looks good: vvt ratio %f", ratio);
|
|
|
|
}
|
2016-11-11 20:02:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
efitick_t offsetNt = nowNt - tc->timeAtVirtualZeroNt;
|
2016-08-20 20:02:09 -07:00
|
|
|
|
|
|
|
angle_t vvtPosition = NT2US(offsetNt) / engine->rpmCalculator.oneDegreeUs;
|
|
|
|
|
|
|
|
// convert engine cycle angle into trigger cycle angle
|
2016-08-22 20:04:55 -07:00
|
|
|
vvtPosition -= tdcPosition();
|
2016-12-05 19:01:54 -08:00
|
|
|
fixAngle(vvtPosition, "vvtPosition");
|
2016-08-20 20:02:09 -07:00
|
|
|
|
2017-01-22 09:06:10 -08:00
|
|
|
tc->vvtPosition = (engineConfiguration->vvtDisplayInverted ? -vvtPosition : vvtPosition) + engineConfiguration->vvtOffset;
|
2016-08-23 20:03:01 -07:00
|
|
|
|
|
|
|
if (engineConfiguration->vvtMode == VVT_FIRST_HALF) {
|
|
|
|
bool isEven = tc->triggerState.isEvenRevolution();
|
|
|
|
if (!isEven) {
|
|
|
|
/**
|
|
|
|
* we are here if we've detected the cam sensor within the wrong crank phase
|
|
|
|
* let's increase the trigger event counter, that would adjust the state of
|
|
|
|
* virtual crank-based trigger
|
|
|
|
*/
|
|
|
|
tc->triggerState.intTotalEventCounter();
|
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
2016-09-17 16:02:34 -07:00
|
|
|
if (engineConfiguration->debugMode == DBG_VVT) {
|
2016-08-23 20:03:01 -07:00
|
|
|
tsOutputChannels.debugIntField1++;
|
|
|
|
}
|
|
|
|
#endif /* EFI_PROD_CODE */
|
|
|
|
}
|
|
|
|
} else if (engineConfiguration->vvtMode == VVT_SECOND_HALF) {
|
|
|
|
bool isEven = tc->triggerState.isEvenRevolution();
|
|
|
|
if (isEven) {
|
|
|
|
// see above comment
|
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
|
|
|
tc->triggerState.intTotalEventCounter();
|
2016-09-17 16:02:34 -07:00
|
|
|
if (engineConfiguration->debugMode == DBG_VVT) {
|
2016-08-23 20:03:01 -07:00
|
|
|
tsOutputChannels.debugIntField1++;
|
|
|
|
}
|
|
|
|
#endif /* EFI_PROD_CODE */
|
|
|
|
}
|
2016-08-20 19:02:12 -07:00
|
|
|
|
2016-11-14 20:01:47 -08:00
|
|
|
} else if (engineConfiguration->vvtMode == MIATA_NB2) {
|
|
|
|
/**
|
|
|
|
* NB2 is a symmetrical crank, there are four phases total
|
|
|
|
*/
|
2016-11-17 20:02:17 -08:00
|
|
|
while (tc->triggerState.getTotalRevolutionCounter() % 4 != engineConfiguration->nbVvtIndex) {
|
2016-11-14 20:01:47 -08:00
|
|
|
tc->triggerState.intTotalEventCounter();
|
|
|
|
}
|
2016-08-20 19:02:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void hwHandleShaftSignal(trigger_event_e signal) {
|
|
|
|
triggerHanlderEntryTime = GET_TIMESTAMP();
|
|
|
|
isInsideTriggerHandler = true;
|
|
|
|
if (triggerReentraint > maxTriggerReentraint)
|
|
|
|
maxTriggerReentraint = triggerReentraint;
|
|
|
|
triggerReentraint++;
|
2017-03-30 05:58:48 -07:00
|
|
|
efiAssertVoid(getRemainingStack(chThdGetSelfX()) > 128, "lowstck#8");
|
2017-05-15 20:28:49 -07:00
|
|
|
engine->triggerCentral.handleShaftSignal(signal PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
triggerReentraint--;
|
|
|
|
triggerDuration = GET_TIMESTAMP() - triggerHanlderEntryTime;
|
|
|
|
isInsideTriggerHandler = false;
|
|
|
|
if (triggerDuration > triggerMaxDuration)
|
|
|
|
triggerMaxDuration = triggerDuration;
|
|
|
|
}
|
|
|
|
#endif /* EFI_PROD_CODE */
|
|
|
|
|
|
|
|
TriggerCentral::TriggerCentral() {
|
|
|
|
nowNt = 0;
|
2016-08-20 20:02:09 -07:00
|
|
|
vvtPosition = 0;
|
2016-08-22 20:04:55 -07:00
|
|
|
timeAtVirtualZeroNt = 0;
|
2016-01-26 19:03:19 -08:00
|
|
|
// we need this initial to have not_running at first invocation
|
|
|
|
previousShaftEventTimeNt = (efitimems_t) -10 * US2NT(US_PER_SECOND_LL);
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
memset(hwEventCounters, 0, sizeof(hwEventCounters));
|
|
|
|
clearCallbacks(&triggerListeneres);
|
2015-09-13 14:02:44 -07:00
|
|
|
triggerState.reset();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int TriggerCentral::getHwEventCounter(int index) {
|
|
|
|
return hwEventCounters[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerCentral::resetCounters() {
|
|
|
|
memset(hwEventCounters, 0, sizeof(hwEventCounters));
|
|
|
|
triggerState.resetRunningCounters();
|
|
|
|
}
|
|
|
|
|
|
|
|
static char shaft_signal_msg_index[15];
|
|
|
|
|
2016-01-11 14:01:33 -08:00
|
|
|
static bool isUpEvent[6] = { false, true, false, true, false, true };
|
2015-07-10 06:01:56 -07:00
|
|
|
static const char *eventId[6] = { CRANK1, CRANK1, CRANK2, CRANK2, CRANK3, CRANK3 };
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
static ALWAYS_INLINE void reportEventToWaveChart(trigger_event_e ckpSignalType, int index DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2017-05-25 20:23:22 -07:00
|
|
|
if (!ENGINE(isEngineChartEnabled)) { // this is here just as a shortcut so that we avoid engine sniffer as soon as possible
|
|
|
|
return; // engineSnifferRpmThreshold is accounted for inside ENGINE(isEngineChartEnabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
itoa10(&shaft_signal_msg_index[2], index);
|
2016-01-11 14:01:33 -08:00
|
|
|
bool isUp = isUpEvent[(int) ckpSignalType];
|
2015-07-10 06:01:56 -07:00
|
|
|
shaft_signal_msg_index[0] = isUp ? 'u' : 'd';
|
|
|
|
|
2016-01-30 19:03:36 -08:00
|
|
|
addEngineSniffferEvent(eventId[(int )ckpSignalType], (char* ) shaft_signal_msg_index);
|
2016-02-27 20:03:34 -08:00
|
|
|
if (engineConfiguration->useOnlyRisingEdgeForTrigger) {
|
2015-07-10 06:01:56 -07:00
|
|
|
// let's add the opposite event right away
|
|
|
|
shaft_signal_msg_index[0] = isUp ? 'd' : 'u';
|
2016-01-30 19:03:36 -08:00
|
|
|
addEngineSniffferEvent(eventId[(int )ckpSignalType], (char* ) shaft_signal_msg_index);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
void TriggerCentral::handleShaftSignal(trigger_event_e signal DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2015-07-10 06:01:56 -07:00
|
|
|
efiAssertVoid(engine!=NULL, "configuration");
|
|
|
|
|
2017-03-01 19:37:10 -08:00
|
|
|
if (triggerShape.shapeDefinitionError) {
|
2017-02-23 17:24:06 -08:00
|
|
|
// trigger is broken, we cannot do anything here
|
2017-03-05 11:13:47 -08:00
|
|
|
warning(CUSTOM_ERR_UNEXPECTED_SHAFT_EVENT, "Shaft event while trigger is mis-configured");
|
2017-02-23 17:24:06 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
nowNt = getTimeNowNt();
|
|
|
|
|
|
|
|
engine->onTriggerEvent(nowNt);
|
|
|
|
|
|
|
|
#if EFI_HISTOGRAMS && EFI_PROD_CODE
|
|
|
|
int beforeCallback = hal_lld_get_counter_value();
|
|
|
|
#endif
|
|
|
|
int eventIndex = (int) signal;
|
|
|
|
efiAssertVoid(eventIndex >= 0 && eventIndex < HW_EVENT_TYPES, "signal type");
|
|
|
|
hwEventCounters[eventIndex]++;
|
|
|
|
|
|
|
|
if (nowNt - previousShaftEventTimeNt > US2NT(US_PER_SECOND_LL)) {
|
|
|
|
/**
|
|
|
|
* We are here if there is a time gap between now and previous shaft event - that means the engine is not runnig.
|
|
|
|
* That means we have lost synchronization since the engine is not running :)
|
|
|
|
*/
|
|
|
|
triggerState.shaft_is_synchronized = false;
|
|
|
|
}
|
|
|
|
previousShaftEventTimeNt = nowNt;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This invocation changes the state of triggerState
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
triggerState.decodeTriggerEvent(signal, nowNt PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If we only have a crank position sensor with four stroke, here we are extending crank revolutions with a 360 degree
|
|
|
|
* cycle into a four stroke, 720 degrees cycle.
|
|
|
|
*/
|
|
|
|
int triggerIndexForListeners;
|
2016-11-13 20:02:33 -08:00
|
|
|
if (engineConfiguration->operationMode == FOUR_STROKE_CAM_SENSOR ||
|
|
|
|
engineConfiguration->operationMode == TWO_STROKE) {
|
2015-07-10 06:01:56 -07:00
|
|
|
// That's easy - trigger cycle matches engine cycle
|
|
|
|
triggerIndexForListeners = triggerState.getCurrentIndex();
|
|
|
|
} else {
|
2016-11-13 20:02:33 -08:00
|
|
|
int crankDivider = engineConfiguration->operationMode == FOUR_STROKE_CRANK_SENSOR ? 2 : 4;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-11-13 20:02:33 -08:00
|
|
|
int crankInternalIndex = triggerState.getTotalRevolutionCounter() % crankDivider;
|
|
|
|
|
|
|
|
triggerIndexForListeners = triggerState.getCurrentIndex() + (crankInternalIndex * TRIGGER_SHAPE(size));
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2016-08-22 20:04:55 -07:00
|
|
|
if (triggerIndexForListeners == 0) {
|
|
|
|
timeAtVirtualZeroNt = nowNt;
|
|
|
|
}
|
2017-05-15 20:28:49 -07:00
|
|
|
reportEventToWaveChart(signal, triggerIndexForListeners PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-07-15 17:01:33 -07:00
|
|
|
if (!triggerState.shaft_is_synchronized) {
|
|
|
|
// we should not propagate event if we do not know where we are
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
if (triggerState.isValidIndex(PASS_ENGINE_PARAMETER_SIGNATURE)) {
|
2016-10-31 17:02:09 -07:00
|
|
|
#if TRIGGER_EXTREME_LOGGING || defined(__DOXYGEN__)
|
|
|
|
scheduleMsg(logger, "trigger %d %d %d", triggerIndexForListeners, getRevolutionCounter(), (int)getTimeNowUs());
|
|
|
|
#endif /* FUEL_MATH_EXTREME_LOGGING */
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* Here we invoke all the listeners - the main engine control logic is inside these listeners
|
|
|
|
*/
|
|
|
|
for (int i = 0; i < triggerListeneres.currentListenersCount; i++) {
|
|
|
|
ShaftPositionListener listener = (ShaftPositionListener) triggerListeneres.callbacks[i];
|
2017-05-15 20:28:49 -07:00
|
|
|
(listener)(signal, triggerIndexForListeners 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__)
|
2015-07-10 06:01:56 -07:00
|
|
|
int afterCallback = hal_lld_get_counter_value();
|
|
|
|
int diff = afterCallback - beforeCallback;
|
|
|
|
// this counter is only 32 bits so it overflows every minute, let's ignore the value in case of the overflow for simplicity
|
|
|
|
if (diff > 0) {
|
2017-01-06 08:02:49 -08:00
|
|
|
hsAdd(&triggerCallbackHistogram, diff);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
#endif /* EFI_HISTOGRAMS */
|
|
|
|
}
|
|
|
|
|
|
|
|
void printAllCallbacksHistogram(void) {
|
2017-01-06 08:02:49 -08:00
|
|
|
#if EFI_HISTOGRAMS || defined(__DOXYGEN__)
|
|
|
|
printHistogram(logger, &triggerCallbackHistogram);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
EXTERN_ENGINE
|
|
|
|
;
|
|
|
|
|
|
|
|
static void triggerShapeInfo(void) {
|
|
|
|
#if (EFI_PROD_CODE || EFI_SIMULATOR) || defined(__DOXYGEN__)
|
2017-03-01 19:18:25 -08:00
|
|
|
TriggerShape *s = &engine->triggerCentral.triggerShape;
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(logger, "useRise=%s", boolToString(s->useRiseEdge));
|
|
|
|
scheduleMsg(logger, "gap from %f to %f", s->syncRatioFrom, s->syncRatioTo);
|
|
|
|
|
|
|
|
for (int i = 0; i < s->getSize(); i++) {
|
|
|
|
scheduleMsg(logger, "event %d %f", i, s->eventAngles[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-08-29 10:04:24 -07:00
|
|
|
#define TRIGGERS_FILE_NAME "triggers.txt"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/**
|
2015-08-29 10:04:24 -07:00
|
|
|
* This is used to generate trigger info which is later used by TriggerImage java class
|
|
|
|
* to generate images for documentation
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
2015-09-10 20:01:32 -07:00
|
|
|
extern bool printTriggerDebug;
|
2015-07-10 06:01:56 -07:00
|
|
|
void printAllTriggers() {
|
|
|
|
|
2015-08-29 10:04:24 -07:00
|
|
|
FILE * fp = fopen (TRIGGERS_FILE_NAME, "w+");
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-08-30 10:02:46 -07:00
|
|
|
fprintf(fp, "# Generated by rusEfi\r\n");
|
2015-09-10 20:01:32 -07:00
|
|
|
printTriggerDebug = true;
|
2015-07-10 06:01:56 -07:00
|
|
|
for (int triggerId = 1; triggerId < TT_UNUSED; triggerId++) {
|
|
|
|
trigger_type_e tt = (trigger_type_e) triggerId;
|
|
|
|
|
2015-12-15 19:01:31 -08:00
|
|
|
// if (triggerId != 20)
|
|
|
|
// continue;
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
printf("Exporting %s\r\n", getTrigger_type_e(tt));
|
|
|
|
|
|
|
|
persistent_config_s pc;
|
|
|
|
Engine e(&pc);
|
|
|
|
Engine *engine = &e;
|
|
|
|
persistent_config_s *config = &pc;
|
|
|
|
engine_configuration_s *engineConfiguration = &pc.engineConfiguration;
|
|
|
|
board_configuration_s *boardConfiguration = &engineConfiguration->bc;
|
|
|
|
|
|
|
|
engineConfiguration->trigger.type = tt;
|
|
|
|
engineConfiguration->operationMode = FOUR_STROKE_CAM_SENSOR;
|
|
|
|
|
2017-03-01 19:18:25 -08:00
|
|
|
TriggerShape *s = &engine->triggerCentral.triggerShape;
|
2017-05-15 20:28:49 -07:00
|
|
|
s->initializeTriggerShape(NULL PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-02-21 08:46:49 -08:00
|
|
|
fprintf(fp, "TRIGGERTYPE %d %d %s %f\r\n", triggerId, s->getLength(), getTrigger_type_e(tt), s->tdcPosition);
|
2015-08-30 10:02:46 -07:00
|
|
|
|
|
|
|
fprintf(fp, "# duty %f %f\r\n", s->dutyCycle[0], s->dutyCycle[1]);
|
|
|
|
|
2015-12-15 19:01:31 -08:00
|
|
|
for (int i = 0; i < s->getLength(); i++) {
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-12-15 19:01:31 -08:00
|
|
|
int triggerDefinitionCoordinate = (s->getTriggerShapeSynchPointIndex() + i) % s->getSize();
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
|
2015-08-30 11:01:28 -07:00
|
|
|
fprintf(fp, "event %d %d %f\r\n", i, s->triggerSignals[triggerDefinitionCoordinate], s->eventAngles[i]);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2015-08-29 10:04:24 -07:00
|
|
|
printf("All triggers exported to %s\r\n", TRIGGERS_FILE_NAME);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
|
|
|
extern PwmConfig triggerSignal;
|
|
|
|
#endif /* #if EFI_PROD_CODE */
|
|
|
|
|
|
|
|
extern uint32_t hipLastExecutionCount;
|
2017-05-21 07:25:35 -07:00
|
|
|
extern uint32_t hwSetTimerDuration;
|
|
|
|
|
|
|
|
extern uint32_t maxLockedDuration;
|
|
|
|
extern uint32_t maxEventCallbackDuration;
|
|
|
|
|
|
|
|
#if (EFI_PROD_CODE) || defined(__DOXYGEN__)
|
|
|
|
extern uint32_t maxPrecisionCallbackDuration;
|
|
|
|
#endif /* EFI_PROD_CODE */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-21 08:09:47 -07:00
|
|
|
extern uint32_t maxSchedulingPrecisionLoss;
|
2015-07-10 06:01:56 -07:00
|
|
|
extern uint32_t *cyccnt;
|
|
|
|
|
2016-09-17 08:03:00 -07:00
|
|
|
extern int vvtEventRiseCounter;
|
|
|
|
extern int vvtEventFallCounter;
|
|
|
|
|
2017-05-19 18:52:10 -07:00
|
|
|
void resetMaxValues() {
|
2017-05-19 19:30:21 -07:00
|
|
|
#if (EFI_PROD_CODE || EFI_SIMULATOR) || defined(__DOXYGEN__)
|
2017-05-21 07:25:35 -07:00
|
|
|
maxEventCallbackDuration = triggerMaxDuration = 0;
|
2017-05-19 19:30:21 -07:00
|
|
|
#endif /* EFI_PROD_CODE || EFI_SIMULATOR */
|
2017-05-21 07:46:43 -07:00
|
|
|
|
2017-05-21 08:09:47 -07:00
|
|
|
maxSchedulingPrecisionLoss = 0;
|
|
|
|
|
2017-05-21 07:46:43 -07:00
|
|
|
#if EFI_CLOCK_LOCKS || defined(__DOXYGEN__)
|
2017-05-21 07:25:35 -07:00
|
|
|
maxLockedDuration = 0;
|
2017-05-21 07:46:43 -07:00
|
|
|
#endif /* EFI_CLOCK_LOCKS */
|
2017-05-21 07:25:35 -07:00
|
|
|
|
|
|
|
#if (EFI_PROD_CODE) || defined(__DOXYGEN__)
|
|
|
|
maxPrecisionCallbackDuration = 0;
|
|
|
|
#endif /* EFI_PROD_CODE */
|
2017-05-19 18:52:10 -07:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void triggerInfo(void) {
|
|
|
|
#if (EFI_PROD_CODE || EFI_SIMULATOR) || defined(__DOXYGEN__)
|
|
|
|
|
2017-03-01 19:18:25 -08:00
|
|
|
TriggerShape *ts = &engine->triggerCentral.triggerShape;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-03-18 17:42:13 -07:00
|
|
|
scheduleMsg(logger, "Template %s (%d) trigger %s (%d) useRiseEdge=%s onlyFront=%s useOnlyFirstChannel=%s tdcOffset=%d",
|
2015-07-10 06:01:56 -07:00
|
|
|
getConfigurationName(engineConfiguration->engineType), engineConfiguration->engineType,
|
|
|
|
getTrigger_type_e(engineConfiguration->trigger.type), engineConfiguration->trigger.type,
|
2016-02-27 20:03:34 -08:00
|
|
|
boolToString(TRIGGER_SHAPE(useRiseEdge)), boolToString(engineConfiguration->useOnlyRisingEdgeForTrigger),
|
2017-03-01 19:18:25 -08:00
|
|
|
boolToString(engineConfiguration->trigger.useOnlyFirstChannel), TRIGGER_SHAPE(tdcPosition));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
if (engineConfiguration->trigger.type == TT_TOOTHED_WHEEL) {
|
|
|
|
scheduleMsg(logger, "total %d/skipped %d", engineConfiguration->trigger.customTotalToothCount,
|
|
|
|
engineConfiguration->trigger.customSkippedToothCount);
|
|
|
|
}
|
|
|
|
|
2015-09-13 14:02:44 -07:00
|
|
|
scheduleMsg(logger, "trigger#1 event counters up=%d/down=%d", engine->triggerCentral.getHwEventCounter(0),
|
|
|
|
engine->triggerCentral.getHwEventCounter(1));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-03-01 19:18:25 -08:00
|
|
|
if (ts->needSecondTriggerInput) {
|
2015-09-13 14:02:44 -07:00
|
|
|
scheduleMsg(logger, "trigger#2 event counters up=%d/down=%d", engine->triggerCentral.getHwEventCounter(2),
|
|
|
|
engine->triggerCentral.getHwEventCounter(3));
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2017-03-01 19:18:25 -08:00
|
|
|
scheduleMsg(logger, "expected cycle events %d/%d/%d", TRIGGER_SHAPE(expectedEventCount[0]),
|
|
|
|
TRIGGER_SHAPE(expectedEventCount[1]), TRIGGER_SHAPE(expectedEventCount[2]));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
scheduleMsg(logger, "trigger type=%d/need2ndChannel=%s", engineConfiguration->trigger.type,
|
2017-03-01 19:18:25 -08:00
|
|
|
boolToString(TRIGGER_SHAPE(needSecondTriggerInput)));
|
|
|
|
scheduleMsg(logger, "expected duty #0=%f/#1=%f", TRIGGER_SHAPE(dutyCycle[0]), TRIGGER_SHAPE(dutyCycle[1]));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
scheduleMsg(logger, "synchronizationNeeded=%s/isError=%s/total errors=%d ord_err=%d/total revolutions=%d/self=%s",
|
|
|
|
boolToString(ts->isSynchronizationNeeded),
|
2015-09-13 14:02:44 -07:00
|
|
|
boolToString(isTriggerDecoderError()), engine->triggerCentral.triggerState.totalTriggerErrorCounter,
|
|
|
|
engine->triggerCentral.triggerState.orderingErrorCounter, engine->triggerCentral.triggerState.getTotalRevolutionCounter(),
|
2015-07-10 06:01:56 -07:00
|
|
|
boolToString(engineConfiguration->directSelfStimulation));
|
|
|
|
|
2017-03-01 19:18:25 -08:00
|
|
|
if (TRIGGER_SHAPE(isSynchronizationNeeded)) {
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(logger, "gap from %f to %f", ts->syncRatioFrom, ts->syncRatioTo);
|
|
|
|
}
|
|
|
|
|
2017-05-19 18:52:10 -07:00
|
|
|
#endif /* EFI_PROD_CODE || EFI_SIMULATOR */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
2016-08-20 16:02:07 -07:00
|
|
|
if (engineConfiguration->camInput != GPIO_UNASSIGNED) {
|
2016-11-14 20:01:47 -08:00
|
|
|
scheduleMsg(logger, "VVT input: %s mode %d", hwPortname(engineConfiguration->camInput),
|
|
|
|
engineConfiguration->vvtMode);
|
2016-09-17 08:03:00 -07:00
|
|
|
scheduleMsg(logger, "VVT event counters: %d/%d", vvtEventRiseCounter, vvtEventFallCounter);
|
2016-08-20 16:02:07 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(logger, "primary trigger input: %s", hwPortname(boardConfiguration->triggerInputPins[0]));
|
|
|
|
scheduleMsg(logger, "primary trigger simulator: %s %s freq=%d",
|
|
|
|
hwPortname(boardConfiguration->triggerSimulatorPins[0]),
|
|
|
|
getPin_output_mode_e(boardConfiguration->triggerSimulatorPinModes[0]),
|
|
|
|
boardConfiguration->triggerSimulatorFrequency);
|
|
|
|
|
2017-03-01 19:18:25 -08:00
|
|
|
if (ts->needSecondTriggerInput) {
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(logger, "secondary trigger input: %s", hwPortname(boardConfiguration->triggerInputPins[1]));
|
|
|
|
#if EFI_EMULATE_POSITION_SENSORS || defined(__DOXYGEN__)
|
|
|
|
scheduleMsg(logger, "secondary trigger simulator: %s %s phase=%d",
|
|
|
|
hwPortname(boardConfiguration->triggerSimulatorPins[1]),
|
|
|
|
getPin_output_mode_e(boardConfiguration->triggerSimulatorPinModes[1]), triggerSignal.safe.phaseIndex);
|
|
|
|
#endif /* EFI_EMULATE_POSITION_SENSORS */
|
|
|
|
}
|
|
|
|
// scheduleMsg(logger, "3rd trigger simulator: %s %s", hwPortname(boardConfiguration->triggerSimulatorPins[2]),
|
|
|
|
// getPin_output_mode_e(boardConfiguration->triggerSimulatorPinModes[2]));
|
|
|
|
|
|
|
|
scheduleMsg(logger, "trigger error extra LED: %s %s", hwPortname(boardConfiguration->triggerErrorPin),
|
|
|
|
getPin_output_mode_e(boardConfiguration->triggerErrorPinMode));
|
|
|
|
scheduleMsg(logger, "primary logic input: %s", hwPortname(boardConfiguration->logicAnalyzerPins[0]));
|
|
|
|
scheduleMsg(logger, "secondary logic input: %s", hwPortname(boardConfiguration->logicAnalyzerPins[1]));
|
|
|
|
|
2017-05-21 08:09:47 -07:00
|
|
|
scheduleMsg(logger, "zeroTestTime=%d maxSchedulingPrecisionLoss=%d", engine->m.zeroTestTime, maxSchedulingPrecisionLoss);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
scheduleMsg(logger, "advanceLookupTime=%d now=%d fuelCalcTime=%d",
|
|
|
|
engine->m.advanceLookupTime, *cyccnt,
|
|
|
|
engine->m.fuelCalcTime);
|
|
|
|
|
|
|
|
scheduleMsg(logger,
|
2016-11-30 12:02:43 -08:00
|
|
|
"ignitionSchTime=%d injectonSchTime=%d",
|
|
|
|
engine->m.ignitionSchTime,
|
2015-07-10 06:01:56 -07:00
|
|
|
engine->m.injectonSchTime);
|
|
|
|
|
|
|
|
scheduleMsg(logger, "mapTime=%d/hipTime=%d/rpmTime=%d/mainTriggerCallbackTime=%d",
|
|
|
|
engine->m.mapAveragingCbTime,
|
|
|
|
engine->m.hipCbTime,
|
|
|
|
engine->m.rpmCbTime,
|
|
|
|
engine->m.mainTriggerCallbackTime);
|
|
|
|
|
2017-05-21 07:46:43 -07:00
|
|
|
#if EFI_CLOCK_LOCKS || defined(__DOXYGEN__)
|
2017-05-21 07:25:35 -07:00
|
|
|
scheduleMsg(logger, "maxLockedDuration=%d / maxTriggerReentraint=%d", maxLockedDuration, maxTriggerReentraint);
|
2017-05-21 07:46:43 -07:00
|
|
|
#endif /* EFI_CLOCK_LOCKS */
|
|
|
|
|
2017-05-21 07:25:35 -07:00
|
|
|
scheduleMsg(logger, "maxEventCallbackDuration=%d", maxEventCallbackDuration);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
scheduleMsg(logger, "hipLastExecutionCount=%d", hipLastExecutionCount);
|
2017-05-21 07:25:35 -07:00
|
|
|
scheduleMsg(logger, "hwSetTimerDuration=%d", hwSetTimerDuration);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
scheduleMsg(logger, "totalTriggerHandlerMaxTime=%d", triggerMaxDuration);
|
2017-05-21 07:25:35 -07:00
|
|
|
scheduleMsg(logger, "maxPrecisionCallbackDuration=%d", maxPrecisionCallbackDuration);
|
2017-05-19 18:52:10 -07:00
|
|
|
resetMaxValues();
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#endif /* EFI_PROD_CODE */
|
|
|
|
}
|
|
|
|
|
2015-09-13 14:02:44 -07:00
|
|
|
#if ! EFI_UNIT_TEST
|
2015-07-10 06:01:56 -07:00
|
|
|
float getTriggerDutyCycle(int index) {
|
2015-09-13 14:02:44 -07:00
|
|
|
return engine->triggerCentral.triggerState.getTriggerDutyCycle(index);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2015-09-13 14:02:44 -07:00
|
|
|
#endif
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static void resetRunningTriggerCounters() {
|
2017-01-06 08:02:49 -08:00
|
|
|
#if !EFI_UNIT_TEST || defined(__DOXYGEN__)
|
2015-09-13 14:02:44 -07:00
|
|
|
engine->triggerCentral.resetCounters();
|
2015-07-10 06:01:56 -07:00
|
|
|
triggerInfo();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-06-26 11:31:10 -07:00
|
|
|
#define COMPARE_CONFIG_PARAMS(param) (engineConfiguration->param != previousConfiguration->param)
|
|
|
|
|
|
|
|
void onConfigurationChangeTriggerCallback(engine_configuration_s *previousConfiguration) {
|
2017-06-26 14:10:01 -07:00
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
2017-06-26 11:31:10 -07:00
|
|
|
isTriggerConfigChanged = COMPARE_CONFIG_PARAMS(trigger.type) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(operationMode) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(useOnlyRisingEdgeForTrigger) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(globalTriggerAngleOffset) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(trigger.customTotalToothCount) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(trigger.customSkippedToothCount) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(bc.triggerInputPins[0]) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(bc.triggerInputPins[1]) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(bc.triggerInputPins[2]) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(camInput) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(vvtMode) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(bc.vvtCamSensorUseRise) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(vvtOffset) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(vvtDisplayInverted) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(bc.nb2ratioFrom) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(bc.nb2ratioTo) ||
|
|
|
|
COMPARE_CONFIG_PARAMS(nbVvtIndex);
|
2017-06-26 14:10:01 -07:00
|
|
|
#endif /* EFI_PROD_CODE */
|
2017-06-26 11:31:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool checkIfTriggerConfigChanged(void) {
|
2017-06-26 14:10:01 -07:00
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
2017-06-26 11:31:10 -07:00
|
|
|
return isTriggerConfigChanged;
|
2017-06-26 14:10:01 -07:00
|
|
|
#else
|
2017-06-28 08:06:40 -07:00
|
|
|
return triggerVersion.isOld();
|
2017-06-26 14:10:01 -07:00
|
|
|
#endif /* EFI_PROD_CODE */
|
2017-06-26 11:31:10 -07:00
|
|
|
}
|
|
|
|
|
2017-05-15 17:31:16 -07:00
|
|
|
void initTriggerCentral(Logging *sharedLogger) {
|
2015-07-10 06:01:56 -07:00
|
|
|
logger = sharedLogger;
|
|
|
|
strcpy((char*) shaft_signal_msg_index, "x_");
|
|
|
|
|
2017-01-06 08:02:49 -08:00
|
|
|
#if EFI_ENGINE_SNIFFER || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
initWaveChart(&waveChart);
|
2015-07-15 18:01:45 -07:00
|
|
|
#endif /* EFI_ENGINE_SNIFFER */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-01-06 08:02:49 -08:00
|
|
|
#if EFI_PROD_CODE || EFI_SIMULATOR || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
addConsoleAction("triggerinfo", triggerInfo);
|
|
|
|
addConsoleAction("trigger_shape_info", triggerShapeInfo);
|
|
|
|
addConsoleAction("reset_trigger", resetRunningTriggerCounters);
|
|
|
|
#endif
|
|
|
|
|
2017-01-06 08:02:49 -08:00
|
|
|
#if EFI_HISTOGRAMS || defined(__DOXYGEN__)
|
|
|
|
initHistogram(&triggerCallbackHistogram, "all callbacks");
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* EFI_HISTOGRAMS */
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|