fome-fw/firmware/controllers/trigger/trigger_decoder.cpp

497 lines
15 KiB
C++
Raw Normal View History

2014-08-29 07:52:33 -07:00
/**
* @file trigger_decoder.cpp
*
* @date Dec 24, 2013
2015-01-12 15:04:10 -08:00
* @author Andrey Belomutskiy, (c) 2012-2015
2014-08-29 07:52:33 -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"
2014-12-24 10:05:36 -08:00
#if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
#include "obd_error_codes.h"
#include "trigger_decoder.h"
#include "cyclic_buffer.h"
#include "trigger_mazda.h"
#include "trigger_chrysler.h"
#include "trigger_gm.h"
#include "trigger_bmw.h"
#include "trigger_mitsubishi.h"
#include "trigger_structure.h"
2014-11-15 06:03:49 -08:00
#include "efiGpio.h"
2014-11-24 13:03:32 -08:00
#include "engine.h"
2014-08-29 07:52:33 -07:00
2015-01-07 17:04:09 -08:00
static OutputPin triggerDecoderErrorPin;
2014-12-14 11:03:57 -08:00
EXTERN_ENGINE
;
2014-11-26 12:04:04 -08:00
2014-08-29 07:52:33 -07:00
// todo: better name for this constant
#define HELPER_PERIOD 100000
2015-04-05 15:06:17 -07:00
#define NO_LEFT_FILTER -1
#define NO_RIGHT_FILTER 1000
2015-03-27 17:05:29 -07:00
static cyclic_buffer<int> errorDetection;
2014-08-29 07:52:33 -07:00
2014-10-01 17:02:58 -07:00
#if ! EFI_PROD_CODE
bool printGapRatio = false;
2014-12-16 17:04:36 -08:00
float actualSynchGap;
2014-11-02 12:03:03 -08:00
#endif /* ! EFI_PROD_CODE */
2014-10-01 17:02:58 -07:00
2015-01-14 18:03:44 -08:00
static Logging * logger;
2014-11-02 11:04:37 -08:00
2014-08-29 07:52:33 -07:00
/**
* @return TRUE is something is wrong with trigger decoding
*/
2014-11-14 08:03:49 -08:00
bool_t isTriggerDecoderError(void) {
2014-08-29 07:52:33 -07:00
return errorDetection.sum(6) > 4;
}
float TriggerState::getTriggerDutyCycle(int index) {
float time = prevTotalTime[index];
return 100 * time / prevCycleDuration;
}
static trigger_wheel_e eventIndex[6] = { T_PRIMARY, T_PRIMARY, T_SECONDARY, T_SECONDARY, T_CHANNEL_3, T_CHANNEL_3 };
static trigger_value_e eventType[6] = { TV_LOW, TV_HIGH, TV_LOW, TV_HIGH, TV_LOW, TV_HIGH };
2014-11-26 12:04:04 -08:00
#define getCurrentGapDuration(nowNt) \
(isFirstEvent ? 0 : (nowNt) - toothed_previous_time)
#define nextTriggerEvent() \
{ \
uint64_t prevTime = timeOfPreviousEventNt[triggerWheel]; \
if (prevTime != 0) { \
/* even event - apply the value*/ \
totalTimeNt[triggerWheel] += (nowNt - prevTime); \
timeOfPreviousEventNt[triggerWheel] = 0; \
} else { \
/* odd event - start accumulation */ \
timeOfPreviousEventNt[triggerWheel] = nowNt; \
} \
current_index++; \
}
#define nextRevolution() { \
if (cycleCallback != NULL) { \
cycleCallback(this); \
} \
memcpy(prevTotalTime, totalTimeNt, sizeof(prevTotalTime)); \
prevCycleDuration = nowNt - startOfCycleNt; \
startOfCycleNt = nowNt; \
clear(); \
totalRevolutionCounter++; \
2015-01-17 21:04:10 -08:00
runningRevolutionCounter++; \
2014-11-26 12:04:04 -08:00
totalEventCountBase += TRIGGER_SHAPE(size); \
}
2014-08-29 07:52:33 -07:00
/**
* @brief Trigger decoding happens here
* This method changes the state of trigger_state_s data structure according to the trigger event
*/
2014-12-14 11:03:57 -08:00
void TriggerState::decodeTriggerEvent(trigger_event_e const signal, uint64_t nowNt DECLARE_ENGINE_PARAMETER_S) {
2014-08-29 07:52:33 -07:00
efiAssertVoid(signal <= SHAFT_3RD_UP, "unexpected signal");
trigger_wheel_e triggerWheel = eventIndex[signal];
2014-11-15 11:03:19 -08:00
if (curSignal == prevSignal) {
orderingErrorCounter++;
}
prevSignal = curSignal;
curSignal = signal;
2014-08-29 07:52:33 -07:00
eventCount[triggerWheel]++;
2014-11-15 11:03:19 -08:00
eventCountExt[signal]++;
2014-08-29 07:52:33 -07:00
2015-03-21 21:04:52 -07:00
bool_t isLessImportant = (TRIGGER_SHAPE(useRiseEdge) && signal != SHAFT_PRIMARY_UP)
2014-11-26 12:04:04 -08:00
|| (!TRIGGER_SHAPE(useRiseEdge) && signal != SHAFT_PRIMARY_DOWN);
2014-08-29 07:52:33 -07:00
2014-11-26 13:03:29 -08:00
uint64_t currentDurationLong = getCurrentGapDuration(nowNt);
/**
* For performance reasons, we want to work with 32 bit values. If there has been more then
* 10 seconds since previous trigger event we do not really care.
*/
2014-12-14 11:03:57 -08:00
currentDuration =
currentDurationLong > 10 * US2NT(US_PER_SECOND_LL) ? 10 * US2NT(US_PER_SECOND_LL) : currentDurationLong;
2014-11-26 13:03:29 -08:00
2014-08-29 07:52:33 -07:00
if (isLessImportant) {
/**
* For less important events we simply increment the index.
*/
2014-12-14 11:03:57 -08:00
nextTriggerEvent()
;
2014-11-26 12:04:04 -08:00
if (TRIGGER_SHAPE(gapBothDirections)) {
2014-11-26 13:03:29 -08:00
toothed_previous_duration = currentDuration;
2014-11-11 08:04:06 -08:00
isFirstEvent = false;
2014-11-14 13:03:52 -08:00
toothed_previous_time = nowNt;
2014-10-29 14:03:10 -07:00
}
2014-08-29 07:52:33 -07:00
return;
}
2014-11-11 08:04:06 -08:00
isFirstEvent = false;
2014-08-29 07:52:33 -07:00
// todo: skip a number of signal from the beginning
#if EFI_PROD_CODE
// scheduleMsg(&logger, "from %f to %f %d %d", triggerConfig->syncRatioFrom, triggerConfig->syncRatioTo, currentDuration, shaftPositionState->toothed_previous_duration);
// scheduleMsg(&logger, "ratio %f", 1.0 * currentDuration/ shaftPositionState->toothed_previous_duration);
#else
if (toothed_previous_duration != 0) {
// printf("ratio %f: cur=%d pref=%d\r\n", 1.0 * currentDuration / shaftPositionState->toothed_previous_duration,
// currentDuration, shaftPositionState->toothed_previous_duration);
}
#endif
2014-11-26 12:04:04 -08:00
bool_t isSynchronizationPoint;
if (TRIGGER_SHAPE(isSynchronizationNeeded)) {
2014-12-16 17:04:36 -08:00
isSynchronizationPoint = currentDuration > toothed_previous_duration * TRIGGER_SHAPE(syncRatioFrom)
&& currentDuration < toothed_previous_duration * TRIGGER_SHAPE(syncRatioTo);
2014-11-26 12:04:04 -08:00
2014-12-16 17:04:36 -08:00
#if EFI_PROD_CODE
2014-12-14 11:03:57 -08:00
if (engineConfiguration->isPrintTriggerSynchDetails) {
2014-12-16 17:04:36 -08:00
#else
if (printGapRatio) {
#endif /* EFI_PROD_CODE */
2014-12-14 11:03:57 -08:00
float gap = 1.0 * currentDuration / toothed_previous_duration;
2014-12-16 17:04:36 -08:00
#if EFI_PROD_CODE
2015-01-14 18:03:44 -08:00
scheduleMsg(logger, "gap=%f @ %d", gap, current_index);
2014-12-16 17:04:36 -08:00
#else
actualSynchGap = gap;
print("current gap %f\r\n", gap);
#endif /* EFI_PROD_CODE */
2014-12-14 11:03:57 -08:00
}
2014-11-26 12:04:04 -08:00
} else {
/**
* in case of noise the counter could be above the expected number of events
*/
isSynchronizationPoint = !shaft_is_synchronized || (current_index >= TRIGGER_SHAPE(size) - 1);
}
if (isSynchronizationPoint) {
2014-08-29 07:52:33 -07:00
/**
* We can check if things are fine by comparing the number of events in a cycle with the expected number of event.
*/
2014-11-26 12:04:04 -08:00
bool isDecodingError = eventCount[0] != TRIGGER_SHAPE(expectedEventCount[0])
|| eventCount[1] != TRIGGER_SHAPE(expectedEventCount[1])
|| eventCount[2] != TRIGGER_SHAPE(expectedEventCount[2]);
2014-08-29 07:52:33 -07:00
2015-01-07 17:04:09 -08:00
triggerDecoderErrorPin.setValue(isDecodingError);
2014-11-13 13:03:09 -08:00
if (isDecodingError) {
totalTriggerErrorCounter++;
2014-12-14 11:03:57 -08:00
if (engineConfiguration->isPrintTriggerSynchDetails) {
#if EFI_PROD_CODE
2015-01-14 18:03:44 -08:00
scheduleMsg(logger, "error: synchronizationPoint @ index %d expected %d/%d/%d got %d/%d/%d", current_index,
2014-12-14 11:03:57 -08:00
TRIGGER_SHAPE(expectedEventCount[0]), TRIGGER_SHAPE(expectedEventCount[1]),
2014-12-16 17:04:36 -08:00
TRIGGER_SHAPE(expectedEventCount[2]), eventCount[0], eventCount[1], eventCount[2]);
2014-12-14 11:03:57 -08:00
#endif /* EFI_PROD_CODE */
}
2014-11-13 13:03:09 -08:00
}
2014-08-29 07:52:33 -07:00
errorDetection.add(isDecodingError);
if (isTriggerDecoderError()) {
warning(OBD_PCM_Processor_Fault, "trigger decoding issue. expected %d/%d/%d got %d/%d/%d",
2014-11-26 12:04:04 -08:00
TRIGGER_SHAPE(expectedEventCount[0]), TRIGGER_SHAPE(expectedEventCount[1]),
TRIGGER_SHAPE(expectedEventCount[2]), eventCount[0], eventCount[1], eventCount[2]);
2014-08-29 07:52:33 -07:00
}
shaft_is_synchronized = true;
// this call would update duty cycle values
2014-12-14 11:03:57 -08:00
nextTriggerEvent()
;
2014-08-29 07:52:33 -07:00
2014-11-26 12:04:04 -08:00
nextRevolution();
2014-08-29 07:52:33 -07:00
} else {
2014-12-14 11:03:57 -08:00
nextTriggerEvent()
;
2014-08-29 07:52:33 -07:00
}
toothed_previous_duration = currentDuration;
2014-11-14 13:03:52 -08:00
toothed_previous_time = nowNt;
2014-08-29 07:52:33 -07:00
}
2015-03-22 10:13:27 -07:00
float getEngineCycle(operation_mode_e operationMode) {
return operationMode == TWO_STROKE ? 360 : 720;
}
2015-04-05 15:06:17 -07:00
void addSkippedToothTriggerEvents(trigger_wheel_e wheel, TriggerShape *s, int totalTeethCount, int skippedCount,
operation_mode_e operationMode, float filterLeft, float filterRight) {
float toothWidth = 0.5;
float engineCycle = getEngineCycle(operationMode);
for (int i = 0; i < totalTeethCount - skippedCount - 1; i++) {
float angleDown = engineCycle / totalTeethCount * (i + toothWidth);
float angleUp = engineCycle / totalTeethCount * (i + 1);
s->addEvent(angleDown, wheel, TV_HIGH, filterLeft, filterRight);
s->addEvent(angleUp, wheel, TV_LOW);
}
float angleDown = engineCycle / totalTeethCount * (totalTeethCount - skippedCount - 1 + toothWidth);
s->addEvent(angleDown, wheel, TV_HIGH);
s->addEvent(engineCycle, wheel, TV_LOW);
}
2015-02-08 15:08:14 -08:00
void initializeSkippedToothTriggerShapeExt(TriggerShape *s, int totalTeethCount, int skippedCount,
2014-08-29 07:52:33 -07:00
operation_mode_e operationMode) {
2015-02-08 15:08:14 -08:00
efiAssertVoid(totalTeethCount > 0, "totalTeethCount is zero");
2015-04-05 15:06:17 -07:00
2015-02-08 15:08:14 -08:00
s->totalToothCount = totalTeethCount;
s->skippedToothCount = skippedCount;
s->setTriggerSynchronizationGap(skippedCount + 1);
s->isSynchronizationNeeded = (skippedCount != 0);
2015-01-13 05:04:00 -08:00
efiAssertVoid(s != NULL, "TriggerShape is NULL");
2015-02-02 21:04:02 -08:00
s->reset(operationMode, false);
2014-08-29 07:52:33 -07:00
2015-04-05 15:06:17 -07:00
addSkippedToothTriggerEvents(T_PRIMARY, s, totalTeethCount, skippedCount, operationMode, NO_LEFT_FILTER, NO_RIGHT_FILTER);
2014-08-29 07:52:33 -07:00
}
2015-03-22 11:12:45 -07:00
static void configureOnePlusOne(TriggerShape *s, operation_mode_e operationMode) {
float engineCycle = getEngineCycle(operationMode);
2015-02-02 21:04:02 -08:00
s->reset(FOUR_STROKE_CAM_SENSOR, true);
2015-02-02 20:07:37 -08:00
s->addEvent(180, T_PRIMARY, TV_HIGH);
s->addEvent(360, T_PRIMARY, TV_LOW);
s->addEvent(540, T_SECONDARY, TV_HIGH);
s->addEvent(720, T_SECONDARY, TV_LOW);
s->isSynchronizationNeeded = false;
}
2015-04-05 15:06:17 -07:00
static void configureOnePlus60_2(TriggerShape *s, operation_mode_e operationMode) {
s->reset(FOUR_STROKE_CAM_SENSOR, true);
s->addEvent(180, T_PRIMARY, TV_HIGH);
s->addEvent(360, T_PRIMARY, TV_LOW);
s->isSynchronizationNeeded = false;
}
2014-08-29 07:52:33 -07:00
/**
* External logger is needed because at this point our logger is not yet initialized
*/
2014-11-26 12:04:04 -08:00
void initializeTriggerShape(Logging *logger, engine_configuration_s const *engineConfiguration, Engine *engine) {
2014-08-29 07:52:33 -07:00
#if EFI_PROD_CODE
scheduleMsg(logger, "initializeTriggerShape()");
#endif
2015-01-23 09:04:28 -08:00
const trigger_config_s *triggerConfig = &engineConfiguration->trigger;
2015-01-13 05:04:00 -08:00
TriggerShape *triggerShape = &engine->triggerShape;
2014-08-29 07:52:33 -07:00
2015-02-04 16:04:18 -08:00
triggerShape->clear();
2015-01-23 09:04:28 -08:00
switch (triggerConfig->type) {
2014-08-29 07:52:33 -07:00
case TT_TOOTHED_WHEEL:
initializeSkippedToothTriggerShapeExt(triggerShape, triggerConfig->customTotalToothCount,
2015-03-22 12:04:59 -07:00
triggerConfig->customSkippedToothCount, engineConfiguration->operationMode);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
case TT_MAZDA_MIATA_NA:
initializeMazdaMiataNaShape(triggerShape);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
case TT_MAZDA_MIATA_NB:
initializeMazdaMiataNbShape(triggerShape);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
2014-09-27 16:02:54 -07:00
case TT_DODGE_NEON_1995:
2014-10-01 16:03:00 -07:00
configureNeon1995TriggerShape(triggerShape);
2014-10-01 18:03:03 -07:00
break;
2014-10-01 16:03:00 -07:00
case TT_DODGE_NEON_2003:
configureNeon2003TriggerShape(triggerShape);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
case TT_FORD_ASPIRE:
2014-09-12 18:05:24 -07:00
configureFordAspireTriggerShape(triggerShape);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
case TT_GM_7X:
2014-09-12 18:05:24 -07:00
configureGmTriggerShape(triggerShape);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
2015-02-02 08:10:08 -08:00
case TT_MAZDA_DOHC_1_4:
2014-08-29 07:52:33 -07:00
configureMazdaProtegeLx(triggerShape);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
2015-02-02 20:07:37 -08:00
case TT_ONE_PLUS_ONE:
2015-03-22 11:12:45 -07:00
configureOnePlusOne(triggerShape, engineConfiguration->operationMode);
2015-02-02 20:07:37 -08:00
break;
2015-04-05 15:06:17 -07:00
case TT_ONE_PLUS_TOOTHED_WHEEL_60_2:
configureOnePlus60_2(triggerShape, engineConfiguration->operationMode);
break;
2015-02-02 08:10:08 -08:00
case TT_MAZDA_SOHC_4:
2015-02-01 13:04:42 -08:00
configureMazdaProtegeSOHC(triggerShape);
break;
2014-08-29 07:52:33 -07:00
case TT_MINI_COOPER_R50:
configureMiniCooperTriggerShape(triggerShape);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
case TT_TOOTHED_WHEEL_60_2:
setToothedWheelConfiguration(triggerShape, 60, 2, engineConfiguration);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
case TT_TOOTHED_WHEEL_36_1:
setToothedWheelConfiguration(triggerShape, 36, 1, engineConfiguration);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
case TT_HONDA_ACCORD_CD_TWO_WIRES:
configureHondaAccordCD(triggerShape, false);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
case TT_HONDA_ACCORD_CD:
configureHondaAccordCD(triggerShape, true);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
case TT_HONDA_ACCORD_CD_DIP:
configureHondaAccordCDDip(triggerShape);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
case TT_MITSU:
initializeMitsubishi4g18(triggerShape);
2014-10-01 18:03:03 -07:00
break;
2014-08-29 07:52:33 -07:00
default:
2015-01-23 09:04:28 -08:00
firmwareError("initializeTriggerShape() not implemented: %d", triggerConfig->type);
2014-08-29 07:52:33 -07:00
;
2014-10-01 18:03:03 -07:00
return;
2014-08-29 07:52:33 -07:00
}
2014-11-24 13:03:32 -08:00
triggerShape->wave.checkSwitchTimes(triggerShape->getSize());
2014-08-29 07:52:33 -07:00
}
TriggerStimulatorHelper::TriggerStimulatorHelper() {
primaryWheelState = false;
secondaryWheelState = false;
thirdWheelState = false;
}
2015-01-13 05:04:00 -08:00
void TriggerStimulatorHelper::nextStep(TriggerState *state, TriggerShape * shape, int i,
2014-11-26 12:04:04 -08:00
trigger_config_s const*triggerConfig DECLARE_ENGINE_PARAMETER_S) {
2014-08-29 07:52:33 -07:00
int stateIndex = i % shape->getSize();
int loopIndex = i / shape->getSize();
int time = (int) (HELPER_PERIOD * (loopIndex + shape->wave.getSwitchTime(stateIndex)));
bool newPrimaryWheelState = shape->wave.getChannelState(0, stateIndex);
bool newSecondaryWheelState = shape->wave.getChannelState(1, stateIndex);
bool new3rdWheelState = shape->wave.getChannelState(2, stateIndex);
if (primaryWheelState != newPrimaryWheelState) {
primaryWheelState = newPrimaryWheelState;
trigger_event_e s = primaryWheelState ? SHAFT_PRIMARY_UP : SHAFT_PRIMARY_DOWN;
2014-11-26 13:03:29 -08:00
state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER);
2014-08-29 07:52:33 -07:00
}
if (secondaryWheelState != newSecondaryWheelState) {
secondaryWheelState = newSecondaryWheelState;
trigger_event_e s = secondaryWheelState ? SHAFT_SECONDARY_UP : SHAFT_SECONDARY_DOWN;
2014-11-26 13:03:29 -08:00
state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER);
2014-08-29 07:52:33 -07:00
}
if (thirdWheelState != new3rdWheelState) {
thirdWheelState = new3rdWheelState;
trigger_event_e s = thirdWheelState ? SHAFT_3RD_UP : SHAFT_3RD_DOWN;
2014-11-26 13:03:29 -08:00
state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER);
2014-08-29 07:52:33 -07:00
}
}
static void onFindIndex(TriggerState *state) {
for (int i = 0; i < PWM_PHASE_MAX_WAVE_PER_PWM; i++) {
// todo: that's not the best place for this intermediate data storage, fix it!
2014-11-14 14:03:57 -08:00
state->expectedTotalTime[i] = state->totalTimeNt[i];
2014-08-29 07:52:33 -07:00
}
}
2015-01-13 05:04:00 -08:00
static uint32_t doFindTrigger(TriggerStimulatorHelper *helper, TriggerShape * shape,
2014-11-26 12:04:04 -08:00
trigger_config_s const*triggerConfig, TriggerState *state DECLARE_ENGINE_PARAMETER_S) {
2014-08-29 07:52:33 -07:00
for (int i = 0; i < 4 * PWM_PHASE_MAX_COUNT; i++) {
2014-11-26 12:04:04 -08:00
helper->nextStep(state, shape, i, triggerConfig PASS_ENGINE_PARAMETER);
2014-08-29 07:52:33 -07:00
if (state->shaft_is_synchronized)
return i;
}
firmwareError("findTriggerZeroEventIndex() failed");
return EFI_ERROR_CODE;
}
/**
* Trigger shape is defined in a way which is convenient for trigger shape definition
* On the other hand, trigger decoder indexing begins from synchronization event.
*
2015-01-13 05:04:00 -08:00
* This function finds the index of synchronization event within TriggerShape
2014-08-29 07:52:33 -07:00
*/
2015-01-13 05:04:00 -08:00
uint32_t findTriggerZeroEventIndex(TriggerShape * shape, trigger_config_s const*triggerConfig
2014-12-16 17:04:36 -08:00
DECLARE_ENGINE_PARAMETER_S) {
2014-08-29 07:52:33 -07:00
2015-02-08 07:04:22 -08:00
// todo: should this variable be declared 'static' to reduce stack usage?
2014-08-29 07:52:33 -07:00
TriggerState state;
errorDetection.clear();
2015-02-08 07:04:22 -08:00
// todo: should this variable be declared 'static' to reduce stack usage?
2014-08-29 07:52:33 -07:00
TriggerStimulatorHelper helper;
2014-11-26 12:04:04 -08:00
uint32_t index = doFindTrigger(&helper, shape, triggerConfig, &state PASS_ENGINE_PARAMETER);
2014-08-29 07:52:33 -07:00
if (index == EFI_ERROR_CODE) {
return index;
}
efiAssert(state.getTotalRevolutionCounter() == 1, "totalRevolutionCounter", EFI_ERROR_CODE);
/**
* Now that we have just located the synch point, we can simulate the whole cycle
* in order to calculate expected duty cycle
2014-10-29 13:04:17 -07:00
*
* todo: add a comment why are we doing '2 * shape->getSize()' here?
2014-08-29 07:52:33 -07:00
*/
state.cycleCallback = onFindIndex;
for (uint32_t i = index + 1; i <= index + 2 * shape->getSize(); i++) {
2014-11-26 12:04:04 -08:00
helper.nextStep(&state, shape, i, triggerConfig PASS_ENGINE_PARAMETER);
2014-08-29 07:52:33 -07:00
}
2014-10-29 13:04:17 -07:00
efiAssert(state.getTotalRevolutionCounter() == 3, "totalRevolutionCounter2", EFI_ERROR_CODE);
2014-08-29 07:52:33 -07:00
for (int i = 0; i < PWM_PHASE_MAX_WAVE_PER_PWM; i++) {
shape->dutyCycle[i] = 1.0 * state.expectedTotalTime[i] / HELPER_PERIOD;
}
return index % shape->getSize();
}
2015-01-14 18:03:44 -08:00
void initTriggerDecoderLogger(Logging *sharedLogger) {
logger = sharedLogger;
2015-01-08 20:04:09 -08:00
}
2014-08-29 07:52:33 -07:00
void initTriggerDecoder(void) {
2014-11-02 12:03:03 -08:00
#if (EFI_PROD_CODE || EFI_SIMULATOR)
2015-01-07 17:04:09 -08:00
outputPinRegisterExt2("trg_err", &triggerDecoderErrorPin, boardConfiguration->triggerErrorPin, &boardConfiguration->triggerErrorPinMode);
2014-08-29 07:52:33 -07:00
#endif
}
2015-02-08 07:04:22 -08:00
#endif /* EFI_SHAFT_POSITION_INPUT */