diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index f322c3fec3..666e6203bf 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -79,7 +79,7 @@ void hwHandleShaftSignal(trigger_event_e signal) { maxTriggerReentraint = triggerReentraint; triggerReentraint++; efiAssertVoid(getRemainingStack(chThdSelf()) > 128, "lowstck#8"); - triggerCentral.handleShaftSignal(signal, engine, engine->engineConfiguration); + triggerCentral.handleShaftSignal(signal PASS_ENGINE_PARAMETER); triggerReentraint--; triggerDuration = GET_TIMESTAMP() - triggerHanlderEntryTime; isInsideTriggerHandler = false; @@ -122,8 +122,7 @@ static ALWAYS_INLINE void reportEventToWaveChart(trigger_event_e ckpSignalType, } } -void TriggerCentral::handleShaftSignal(trigger_event_e signal, Engine *engine, - engine_configuration_s *engineConfiguration) { +void TriggerCentral::handleShaftSignal(trigger_event_e signal DECLARE_ENGINE_PARAMETER_S) { efiAssertVoid(engine!=NULL, "configuration"); nowNt = getTimeNowNt(); @@ -149,12 +148,10 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, Engine *engine, } previousShaftEventTimeNt = nowNt; - trigger_shape_s * triggerShape = &engine->triggerShape; - /** * 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) { // 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 { bool isEven = triggerState.getTotalRevolutionCounter() & 1; - triggerIndexForListeners = triggerState.getCurrentIndex() + (isEven ? 0 : triggerShape->getSize()); + triggerIndexForListeners = triggerState.getCurrentIndex() + (isEven ? 0 : TRIGGER_SHAPE(size)); } reportEventToWaveChart(signal, triggerIndexForListeners); - if (triggerState.getCurrentIndex() >= engine->triggerShape.getSize()) { - warning(OBD_PCM_Processor_Fault, "unexpected eventIndex=%d", triggerState.getCurrentIndex()); + if (triggerState.current_index >= TRIGGER_SHAPE(size)) { + warning(OBD_PCM_Processor_Fault, "unexpected eventIndex=%d", triggerState.current_index); } else { /** diff --git a/firmware/controllers/trigger/trigger_central.h b/firmware/controllers/trigger/trigger_central.h index 00e0179b35..8dcd8907fb 100644 --- a/firmware/controllers/trigger/trigger_central.h +++ b/firmware/controllers/trigger/trigger_central.h @@ -25,7 +25,7 @@ class TriggerCentral { public: TriggerCentral(); 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); TriggerState triggerState; uint64_t nowNt; diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index 56bc975fd0..743d02ccc0 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -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 * 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) { - (void) triggerConfig; // we might want this for logging? efiAssertVoid(signal <= SHAFT_3RD_UP, "unexpected 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) || (!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) { /** * For less important events we simply increment the index. */ nextTriggerEvent(); if (TRIGGER_SHAPE(gapBothDirections)) { - toothed_previous_duration = getCurrentGapDuration(nowNt); + toothed_previous_duration = currentDuration; isFirstEvent = false; toothed_previous_time = nowNt; } return; } - currentDuration = getCurrentGapDuration(nowNt); isFirstEvent = false; efiAssertVoid(currentDuration >= 0, "decode: negative duration?"); @@ -344,19 +350,19 @@ void TriggerStimulatorHelper::nextStep(TriggerState *state, trigger_shape_s * sh if (primaryWheelState != newPrimaryWheelState) { primaryWheelState = newPrimaryWheelState; 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) { secondaryWheelState = newSecondaryWheelState; 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) { thirdWheelState = new3rdWheelState; 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); } } diff --git a/firmware/controllers/trigger/trigger_decoder.h b/firmware/controllers/trigger/trigger_decoder.h index 7ec6aa0058..3969b96007 100644 --- a/firmware/controllers/trigger/trigger_decoder.h +++ b/firmware/controllers/trigger/trigger_decoder.h @@ -23,7 +23,7 @@ public: int getTotalRevolutionCounter(); uint64_t getTotalEventCounter(); 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); TriggerStateCallback cycleCallback; @@ -52,12 +52,12 @@ public: uint32_t totalTriggerErrorCounter; uint32_t orderingErrorCounter; -private: - void clear(); /** * index within trigger revolution, from 0 to trigger event count */ uint32_t current_index; +private: + void clear(); /** * Number of actual events within current trigger cycle * see trigger_shape_s diff --git a/unit_tests/test_trigger_decoder.cpp b/unit_tests/test_trigger_decoder.cpp index d7d3cf18b3..83b11c57bd 100644 --- a/unit_tests/test_trigger_decoder.cpp +++ b/unit_tests/test_trigger_decoder.cpp @@ -145,28 +145,28 @@ static void test1995FordInline6TriggerDecoder(void) { assertFalseM("shaft_is_synchronized", state.shaft_is_synchronized); 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 - 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 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()); 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()); - 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()); } - state.decodeTriggerEvent(&engineConfiguration->triggerConfig, SHAFT_PRIMARY_UP, r++ PASS_ENGINE_PARAMETER); + state.decodeTriggerEvent(SHAFT_PRIMARY_UP, r++ PASS_ENGINE_PARAMETER); 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()); - 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 assertEqualsM("running dwell", 0.5, getSparkDwellMsT(2000 PASS_ENGINE_PARAMETER)); @@ -215,38 +215,38 @@ void testMazdaMianaNbDecoder(void) { TriggerState state; 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); - 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); - 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); - 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); - 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); - 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); 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); - 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); - 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); - 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); 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); 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); event_trigger_position_s position; @@ -320,11 +320,11 @@ void testGY6_139QMB(void) { assertEquals(0, state.getCurrentIndex()); 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); 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); assertEquals(1, state.getCurrentIndex()); }