auto-sync

This commit is contained in:
rusEfi 2014-11-26 15:03:29 -06:00
parent ea348694e6
commit 9f73f4fb5e
5 changed files with 45 additions and 42 deletions

View File

@ -79,7 +79,7 @@ void hwHandleShaftSignal(trigger_event_e signal) {
maxTriggerReentraint = triggerReentraint; maxTriggerReentraint = triggerReentraint;
triggerReentraint++; triggerReentraint++;
efiAssertVoid(getRemainingStack(chThdSelf()) > 128, "lowstck#8"); efiAssertVoid(getRemainingStack(chThdSelf()) > 128, "lowstck#8");
triggerCentral.handleShaftSignal(signal, engine, engine->engineConfiguration); triggerCentral.handleShaftSignal(signal PASS_ENGINE_PARAMETER);
triggerReentraint--; triggerReentraint--;
triggerDuration = GET_TIMESTAMP() - triggerHanlderEntryTime; triggerDuration = GET_TIMESTAMP() - triggerHanlderEntryTime;
isInsideTriggerHandler = false; isInsideTriggerHandler = false;
@ -122,8 +122,7 @@ static ALWAYS_INLINE void reportEventToWaveChart(trigger_event_e ckpSignalType,
} }
} }
void TriggerCentral::handleShaftSignal(trigger_event_e signal, Engine *engine, void TriggerCentral::handleShaftSignal(trigger_event_e signal DECLARE_ENGINE_PARAMETER_S) {
engine_configuration_s *engineConfiguration) {
efiAssertVoid(engine!=NULL, "configuration"); efiAssertVoid(engine!=NULL, "configuration");
nowNt = getTimeNowNt(); nowNt = getTimeNowNt();
@ -149,12 +148,10 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, Engine *engine,
} }
previousShaftEventTimeNt = nowNt; previousShaftEventTimeNt = nowNt;
trigger_shape_s * triggerShape = &engine->triggerShape;
/** /**
* This invocation changes the state of triggerState * This invocation changes the state of triggerState
*/ */
triggerState.decodeTriggerEvent(&engineConfiguration->triggerConfig, signal, nowNt PASS_ENGINE_PARAMETER); triggerState.decodeTriggerEvent(signal, nowNt PASS_ENGINE_PARAMETER);
if (!triggerState.shaft_is_synchronized) { if (!triggerState.shaft_is_synchronized) {
// we should not propagate event if we do not know where we are // we should not propagate event if we do not know where we are
@ -172,12 +169,12 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, Engine *engine,
} else { } else {
bool isEven = triggerState.getTotalRevolutionCounter() & 1; bool isEven = triggerState.getTotalRevolutionCounter() & 1;
triggerIndexForListeners = triggerState.getCurrentIndex() + (isEven ? 0 : triggerShape->getSize()); triggerIndexForListeners = triggerState.getCurrentIndex() + (isEven ? 0 : TRIGGER_SHAPE(size));
} }
reportEventToWaveChart(signal, triggerIndexForListeners); reportEventToWaveChart(signal, triggerIndexForListeners);
if (triggerState.getCurrentIndex() >= engine->triggerShape.getSize()) { if (triggerState.current_index >= TRIGGER_SHAPE(size)) {
warning(OBD_PCM_Processor_Fault, "unexpected eventIndex=%d", triggerState.getCurrentIndex()); warning(OBD_PCM_Processor_Fault, "unexpected eventIndex=%d", triggerState.current_index);
} else { } else {
/** /**

View File

@ -25,7 +25,7 @@ class TriggerCentral {
public: public:
TriggerCentral(); TriggerCentral();
void addEventListener(ShaftPositionListener handler, const char *name, Engine *engine); void addEventListener(ShaftPositionListener handler, const char *name, Engine *engine);
void handleShaftSignal(trigger_event_e signal, Engine *engine, engine_configuration_s *engineConfiguration); void handleShaftSignal(trigger_event_e signal DECLARE_ENGINE_PARAMETER_S);
int getHwEventCounter(int index); int getHwEventCounter(int index);
TriggerState triggerState; TriggerState triggerState;
uint64_t nowNt; uint64_t nowNt;

View File

@ -97,9 +97,8 @@ static trigger_value_e eventType[6] = { TV_LOW, TV_HIGH, TV_LOW, TV_HIGH, TV_LOW
* @brief Trigger decoding happens here * @brief Trigger decoding happens here
* This method changes the state of trigger_state_s data structure according to the trigger event * This method changes the state of trigger_state_s data structure according to the trigger event
*/ */
void TriggerState::decodeTriggerEvent(trigger_config_s const*triggerConfig, void TriggerState::decodeTriggerEvent(
trigger_event_e const signal, uint64_t nowNt DECLARE_ENGINE_PARAMETER_S) { trigger_event_e const signal, uint64_t nowNt DECLARE_ENGINE_PARAMETER_S) {
(void) triggerConfig; // we might want this for logging?
efiAssertVoid(signal <= SHAFT_3RD_UP, "unexpected signal"); efiAssertVoid(signal <= SHAFT_3RD_UP, "unexpected signal");
trigger_wheel_e triggerWheel = eventIndex[signal]; trigger_wheel_e triggerWheel = eventIndex[signal];
@ -117,20 +116,27 @@ void TriggerState::decodeTriggerEvent(trigger_config_s const*triggerConfig,
int isLessImportant = (TRIGGER_SHAPE(useRiseEdge) && signal != SHAFT_PRIMARY_UP) int isLessImportant = (TRIGGER_SHAPE(useRiseEdge) && signal != SHAFT_PRIMARY_UP)
|| (!TRIGGER_SHAPE(useRiseEdge) && signal != SHAFT_PRIMARY_DOWN); || (!TRIGGER_SHAPE(useRiseEdge) && signal != SHAFT_PRIMARY_DOWN);
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.
*/
currentDuration = currentDurationLong > 10 * US2NT(US_PER_SECOND_LL) ? 10 * US2NT(US_PER_SECOND_LL) : currentDurationLong;
if (isLessImportant) { if (isLessImportant) {
/** /**
* For less important events we simply increment the index. * For less important events we simply increment the index.
*/ */
nextTriggerEvent(); nextTriggerEvent();
if (TRIGGER_SHAPE(gapBothDirections)) { if (TRIGGER_SHAPE(gapBothDirections)) {
toothed_previous_duration = getCurrentGapDuration(nowNt); toothed_previous_duration = currentDuration;
isFirstEvent = false; isFirstEvent = false;
toothed_previous_time = nowNt; toothed_previous_time = nowNt;
} }
return; return;
} }
currentDuration = getCurrentGapDuration(nowNt);
isFirstEvent = false; isFirstEvent = false;
efiAssertVoid(currentDuration >= 0, "decode: negative duration?"); efiAssertVoid(currentDuration >= 0, "decode: negative duration?");
@ -344,19 +350,19 @@ void TriggerStimulatorHelper::nextStep(TriggerState *state, trigger_shape_s * sh
if (primaryWheelState != newPrimaryWheelState) { if (primaryWheelState != newPrimaryWheelState) {
primaryWheelState = newPrimaryWheelState; primaryWheelState = newPrimaryWheelState;
trigger_event_e s = primaryWheelState ? SHAFT_PRIMARY_UP : SHAFT_PRIMARY_DOWN; trigger_event_e s = primaryWheelState ? SHAFT_PRIMARY_UP : SHAFT_PRIMARY_DOWN;
state->decodeTriggerEvent(triggerConfig, s, time PASS_ENGINE_PARAMETER); state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER);
} }
if (secondaryWheelState != newSecondaryWheelState) { if (secondaryWheelState != newSecondaryWheelState) {
secondaryWheelState = newSecondaryWheelState; secondaryWheelState = newSecondaryWheelState;
trigger_event_e s = secondaryWheelState ? SHAFT_SECONDARY_UP : SHAFT_SECONDARY_DOWN; trigger_event_e s = secondaryWheelState ? SHAFT_SECONDARY_UP : SHAFT_SECONDARY_DOWN;
state->decodeTriggerEvent(triggerConfig, s, time PASS_ENGINE_PARAMETER); state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER);
} }
if (thirdWheelState != new3rdWheelState) { if (thirdWheelState != new3rdWheelState) {
thirdWheelState = new3rdWheelState; thirdWheelState = new3rdWheelState;
trigger_event_e s = thirdWheelState ? SHAFT_3RD_UP : SHAFT_3RD_DOWN; trigger_event_e s = thirdWheelState ? SHAFT_3RD_UP : SHAFT_3RD_DOWN;
state->decodeTriggerEvent(triggerConfig, s, time PASS_ENGINE_PARAMETER); state->decodeTriggerEvent(s, time PASS_ENGINE_PARAMETER);
} }
} }

View File

@ -23,7 +23,7 @@ public:
int getTotalRevolutionCounter(); int getTotalRevolutionCounter();
uint64_t getTotalEventCounter(); uint64_t getTotalEventCounter();
uint64_t getStartOfRevolutionIndex(); uint64_t getStartOfRevolutionIndex();
void decodeTriggerEvent(trigger_config_s const*triggerConfig, trigger_event_e const signal, uint64_t nowUs DECLARE_ENGINE_PARAMETER_S); void decodeTriggerEvent(trigger_event_e const signal, uint64_t nowUs DECLARE_ENGINE_PARAMETER_S);
float getTriggerDutyCycle(int index); float getTriggerDutyCycle(int index);
TriggerStateCallback cycleCallback; TriggerStateCallback cycleCallback;
@ -52,12 +52,12 @@ public:
uint32_t totalTriggerErrorCounter; uint32_t totalTriggerErrorCounter;
uint32_t orderingErrorCounter; uint32_t orderingErrorCounter;
private:
void clear();
/** /**
* index within trigger revolution, from 0 to trigger event count * index within trigger revolution, from 0 to trigger event count
*/ */
uint32_t current_index; uint32_t current_index;
private:
void clear();
/** /**
* Number of actual events within current trigger cycle * Number of actual events within current trigger cycle
* see trigger_shape_s * see trigger_shape_s

View File

@ -145,28 +145,28 @@ static void test1995FordInline6TriggerDecoder(void) {
assertFalseM("shaft_is_synchronized", state.shaft_is_synchronized); assertFalseM("shaft_is_synchronized", state.shaft_is_synchronized);
int r = 10; int r = 10;
state.decodeTriggerEvent(&engineConfiguration->triggerConfig, SHAFT_PRIMARY_DOWN, r PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_DOWN, r PASS_ENGINE_PARAMETER);
assertFalseM("shaft_is_synchronized", state.shaft_is_synchronized); // still no synchronization assertFalseM("shaft_is_synchronized", state.shaft_is_synchronized); // still no synchronization
state.decodeTriggerEvent(&engineConfiguration->triggerConfig, SHAFT_PRIMARY_UP, ++r PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_UP, ++r PASS_ENGINE_PARAMETER);
assertTrue(state.shaft_is_synchronized); // first signal rise synchronize assertTrue(state.shaft_is_synchronized); // first signal rise synchronize
assertEquals(0, state.getCurrentIndex()); assertEquals(0, state.getCurrentIndex());
state.decodeTriggerEvent(&engineConfiguration->triggerConfig, SHAFT_PRIMARY_DOWN, r++ PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_DOWN, r++ PASS_ENGINE_PARAMETER);
assertEquals(1, state.getCurrentIndex()); assertEquals(1, state.getCurrentIndex());
for (int i = 2; i < 10;) { for (int i = 2; i < 10;) {
state.decodeTriggerEvent(&engineConfiguration->triggerConfig, SHAFT_PRIMARY_UP, r++ PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_UP, r++ PASS_ENGINE_PARAMETER);
assertEqualsM("even", i++, state.getCurrentIndex()); assertEqualsM("even", i++, state.getCurrentIndex());
state.decodeTriggerEvent(&engineConfiguration->triggerConfig, SHAFT_PRIMARY_DOWN, r++ PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_DOWN, r++ PASS_ENGINE_PARAMETER);
assertEqualsM("odd", i++, state.getCurrentIndex()); assertEqualsM("odd", i++, state.getCurrentIndex());
} }
state.decodeTriggerEvent(&engineConfiguration->triggerConfig, SHAFT_PRIMARY_UP, r++ PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_UP, r++ PASS_ENGINE_PARAMETER);
assertEquals(10, state.getCurrentIndex()); assertEquals(10, state.getCurrentIndex());
state.decodeTriggerEvent(&engineConfiguration->triggerConfig, SHAFT_PRIMARY_DOWN, r++ PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_DOWN, r++ PASS_ENGINE_PARAMETER);
assertEquals(11, state.getCurrentIndex()); assertEquals(11, state.getCurrentIndex());
state.decodeTriggerEvent(&engineConfiguration->triggerConfig, SHAFT_PRIMARY_UP, r++ PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_UP, r++ PASS_ENGINE_PARAMETER);
assertEquals(0, state.getCurrentIndex()); // new revolution assertEquals(0, state.getCurrentIndex()); // new revolution
assertEqualsM("running dwell", 0.5, getSparkDwellMsT(2000 PASS_ENGINE_PARAMETER)); assertEqualsM("running dwell", 0.5, getSparkDwellMsT(2000 PASS_ENGINE_PARAMETER));
@ -215,38 +215,38 @@ void testMazdaMianaNbDecoder(void) {
TriggerState state; TriggerState state;
int a = 0; int a = 0;
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 20 PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_DOWN, a + 20 PASS_ENGINE_PARAMETER);
assertFalseM("0a shaft_is_synchronized", state.shaft_is_synchronized); assertFalseM("0a shaft_is_synchronized", state.shaft_is_synchronized);
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_UP, a + 340 PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_UP, a + 340 PASS_ENGINE_PARAMETER);
assertFalseM("0b shaft_is_synchronized", state.shaft_is_synchronized); assertFalseM("0b shaft_is_synchronized", state.shaft_is_synchronized);
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 360 PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_DOWN, a + 360 PASS_ENGINE_PARAMETER);
assertFalseM("0c shaft_is_synchronized", state.shaft_is_synchronized); assertFalseM("0c shaft_is_synchronized", state.shaft_is_synchronized);
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_UP, a + 380 PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_UP, a + 380 PASS_ENGINE_PARAMETER);
assertFalseM("0d shaft_is_synchronized", state.shaft_is_synchronized); assertFalseM("0d shaft_is_synchronized", state.shaft_is_synchronized);
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 400 PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_DOWN, a + 400 PASS_ENGINE_PARAMETER);
assertTrueM("0e shaft_is_synchronized", state.shaft_is_synchronized); assertTrueM("0e shaft_is_synchronized", state.shaft_is_synchronized);
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_UP, a + 720 PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_UP, a + 720 PASS_ENGINE_PARAMETER);
assertTrueM("0f shaft_is_synchronized", state.shaft_is_synchronized); assertTrueM("0f shaft_is_synchronized", state.shaft_is_synchronized);
a = 720; a = 720;
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 20 PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_DOWN, a + 20 PASS_ENGINE_PARAMETER);
assertTrueM("1a shaft_is_synchronized", state.shaft_is_synchronized); assertTrueM("1a shaft_is_synchronized", state.shaft_is_synchronized);
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_UP, a + 340 PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_UP, a + 340 PASS_ENGINE_PARAMETER);
assertTrueM("1b shaft_is_synchronized", state.shaft_is_synchronized); assertTrueM("1b shaft_is_synchronized", state.shaft_is_synchronized);
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 360 PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_DOWN, a + 360 PASS_ENGINE_PARAMETER);
assertTrueM("1c shaft_is_synchronized", state.shaft_is_synchronized); assertTrueM("1c shaft_is_synchronized", state.shaft_is_synchronized);
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_UP, a + 380 PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_UP, a + 380 PASS_ENGINE_PARAMETER);
assertTrueM("1d shaft_is_synchronized", state.shaft_is_synchronized); assertTrueM("1d shaft_is_synchronized", state.shaft_is_synchronized);
assertEquals(5, state.getCurrentIndex()); assertEquals(5, state.getCurrentIndex());
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 400 PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_DOWN, a + 400 PASS_ENGINE_PARAMETER);
assertTrueM("1e shaft_is_synchronized", state.shaft_is_synchronized); assertTrueM("1e shaft_is_synchronized", state.shaft_is_synchronized);
assertEquals(0, state.getCurrentIndex()); assertEquals(0, state.getCurrentIndex());
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_UP, a + 720 PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_UP, a + 720 PASS_ENGINE_PARAMETER);
assertTrueM("1f shaft_is_synchronized", state.shaft_is_synchronized); assertTrueM("1f shaft_is_synchronized", state.shaft_is_synchronized);
event_trigger_position_s position; event_trigger_position_s position;
@ -320,11 +320,11 @@ void testGY6_139QMB(void) {
assertEquals(0, state.getCurrentIndex()); assertEquals(0, state.getCurrentIndex());
int now = 0; int now = 0;
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_UP, now++ PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_UP, now++ PASS_ENGINE_PARAMETER);
assertTrueM("shaft_is_synchronized", state.shaft_is_synchronized); assertTrueM("shaft_is_synchronized", state.shaft_is_synchronized);
assertEquals(0, state.getCurrentIndex()); assertEquals(0, state.getCurrentIndex());
state.decodeTriggerEvent(&ec->triggerConfig, SHAFT_PRIMARY_DOWN, now++ PASS_ENGINE_PARAMETER); state.decodeTriggerEvent(SHAFT_PRIMARY_DOWN, now++ PASS_ENGINE_PARAMETER);
assertTrueM("shaft_is_synchronized", state.shaft_is_synchronized); assertTrueM("shaft_is_synchronized", state.shaft_is_synchronized);
assertEquals(1, state.getCurrentIndex()); assertEquals(1, state.getCurrentIndex());
} }