From bdbee0d38b0795e5497c31f62d9a3a1e1cb592b4 Mon Sep 17 00:00:00 2001 From: rusefi Date: Mon, 24 Aug 2020 02:01:50 -0400 Subject: [PATCH] VVT support for VAG trigger #883 --- firmware/console/status_loop.cpp | 4 ++++ firmware/controllers/algo/engine.h | 9 +++---- firmware/controllers/algo/engine2.cpp | 12 ++++++---- .../controllers/trigger/trigger_central.cpp | 13 +++++++--- .../controllers/trigger/trigger_central.h | 4 ++-- .../controllers/trigger/trigger_decoder.cpp | 24 ++++--------------- .../controllers/trigger/trigger_decoder.h | 15 +++++------- .../controllers/trigger/trigger_simulator.cpp | 6 ++--- firmware/hw_layer/hardware.cpp | 4 ---- .../tests/trigger/test_trigger_decoder.cpp | 16 ++++++------- 10 files changed, 51 insertions(+), 56 deletions(-) diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index f6e3217647..07b1c9fde7 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -338,6 +338,10 @@ static void initStatusLeds(void) { enginePins.runningLedPin.initPin("led: running status", engineConfiguration->runningLedPin); enginePins.debugTriggerSync.initPin("debug: sync", CONFIG(debugTriggerSync)); +#if EFI_GPIO_HARDWARE && EFI_SHAFT_POSITION_INPUT + enginePins.triggerDecoderErrorPin.initPin("led: trigger debug", CONFIG(triggerErrorPin), + &CONFIG(triggerErrorPinMode)); +#endif /* EFI_GPIO_HARDWARE */ } #if EFI_PROD_CODE diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index 878eb434c8..c9489a9edf 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -60,10 +60,11 @@ public: class PrimaryTriggerConfiguration : public TriggerConfiguration { public: PrimaryTriggerConfiguration(Engine *engine); - bool isUseOnlyRisingEdgeForTrigger(); - bool isSilentTriggerError(); - bool isVerboseTriggerSynchDetails(); - debug_mode_e getDebugMode(); + bool isUseOnlyRisingEdgeForTrigger() const; + bool isSilentTriggerError() const; + bool isVerboseTriggerSynchDetails() const; + debug_mode_e getDebugMode() const; + trigger_type_e getType() const; private: Engine *engine; }; diff --git a/firmware/controllers/algo/engine2.cpp b/firmware/controllers/algo/engine2.cpp index 03f7b59e6d..2a524bcd4a 100644 --- a/firmware/controllers/algo/engine2.cpp +++ b/firmware/controllers/algo/engine2.cpp @@ -285,19 +285,23 @@ PrimaryTriggerConfiguration::PrimaryTriggerConfiguration(Engine *engine) { this->engine = engine; } -bool PrimaryTriggerConfiguration::isUseOnlyRisingEdgeForTrigger() { +bool PrimaryTriggerConfiguration::isUseOnlyRisingEdgeForTrigger() const { return engine->engineConfigurationPtr->useOnlyRisingEdgeForTrigger; } -debug_mode_e PrimaryTriggerConfiguration::getDebugMode() { +debug_mode_e PrimaryTriggerConfiguration::getDebugMode() const { return engine->engineConfigurationPtr->debugMode; } -bool PrimaryTriggerConfiguration::isSilentTriggerError() { +trigger_type_e PrimaryTriggerConfiguration::getType() const { + return engine->engineConfigurationPtr->trigger.type; +} + +bool PrimaryTriggerConfiguration::isSilentTriggerError() const { return engine->engineConfigurationPtr->silentTriggerError; } -bool PrimaryTriggerConfiguration::isVerboseTriggerSynchDetails() { +bool PrimaryTriggerConfiguration::isVerboseTriggerSynchDetails() const { return engine->engineConfigurationPtr->verboseTriggerSynchDetails; } diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index 1033f40f5c..5fffaccb6a 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -455,7 +455,7 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, efitick_t timesta nullptr, engine, &engine->primaryTriggerConfiguration, - signal, timestamp PASS_CONFIG_PARAMETER_SUFFIX); + signal, timestamp); /** * If we only have a crank position sensor with four stroke, here we are extending crank revolutions with a 360 degree @@ -792,8 +792,15 @@ void initTriggerCentral(Logging *sharedLogger) { addConsoleAction(CMD_TRIGGERINFO, triggerInfo); addConsoleAction("trigger_shape_info", triggerShapeInfo); addConsoleAction("reset_trigger", resetRunningTriggerCounters); -#endif +#endif // EFI_PROD_CODE || EFI_SIMULATOR } -#endif +/** + * @return TRUE is something is wrong with trigger decoding + */ +bool isTriggerDecoderError(DECLARE_ENGINE_PARAMETER_SIGNATURE) { + return engine->triggerErrorDetection.sum(6) > 4; +} + +#endif // EFI_SHAFT_POSITION_INPUT diff --git a/firmware/controllers/trigger/trigger_central.h b/firmware/controllers/trigger/trigger_central.h index 07a169d7a8..0312ac7f28 100644 --- a/firmware/controllers/trigger/trigger_central.h +++ b/firmware/controllers/trigger/trigger_central.h @@ -12,8 +12,6 @@ #include "trigger_decoder.h" #include "trigger_central_generated.h" - - class Engine; typedef void (*ShaftPositionListener)(trigger_event_e signal, uint32_t index, efitick_t edgeTimestamp DECLARE_ENGINE_PARAMETER_SUFFIX); @@ -87,4 +85,6 @@ void onConfigurationChangeTriggerCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE); bool checkIfTriggerConfigChanged(DECLARE_ENGINE_PARAMETER_SIGNATURE); bool isTriggerConfigChanged(DECLARE_ENGINE_PARAMETER_SIGNATURE); +bool isTriggerDecoderError(DECLARE_ENGINE_PARAMETER_SIGNATURE); + #define SYMMETRICAL_CRANK_SENSOR_DIVIDER 4 diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index bd32854fff..f53638c052 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -114,13 +114,6 @@ float actualSynchGap; static Logging * logger = nullptr; -/** - * @return TRUE is something is wrong with trigger decoding - */ -bool isTriggerDecoderError(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - return engine->triggerErrorDetection.sum(6) > 4; -} - void calculateTriggerSynchPoint(TriggerWaveform *shape, TriggerState *state DECLARE_ENGINE_PARAMETER_SUFFIX) { #if EFI_PROD_CODE efiAssertVoid(CUSTOM_TRIGGER_STACK, getCurrentRemainingStack() > EXPECTED_REMAINING_STACK, "calc s"); @@ -302,7 +295,7 @@ static trigger_value_e eventType[6] = { TV_FALL, TV_RISE, TV_FALL, TV_RISE, TV_F /* odd event - start accumulation */ \ currentCycle.timeOfPreviousEventNt[triggerWheel] = nowNt; \ } \ - if (engineConfiguration->useOnlyRisingEdgeForTrigger) {currentCycle.current_index++;} \ + if (triggerConfiguration->isUseOnlyRisingEdgeForTrigger()) {currentCycle.current_index++;} \ currentCycle.current_index++; \ PRINT_INC_INDEX; \ } @@ -389,7 +382,7 @@ void TriggerState::decodeTriggerEvent(TriggerWaveform *triggerShape, TriggerStateListener * triggerStateListener, const TriggerConfiguration * triggerConfiguration, const trigger_event_e signal, - const efitick_t nowNt DECLARE_CONFIG_PARAMETER_SUFFIX) { + const efitick_t nowNt) { ScopePerf perf(PE::DecodeTriggerEvent); if (nowNt - previousShaftEventTimeNt > NT_PER_SECOND) { @@ -439,7 +432,7 @@ void TriggerState::decodeTriggerEvent(TriggerWaveform *triggerShape, #if EFI_UNIT_TEST if (printTriggerTrace) { printf("%s isLessImportant %s now=%d index=%d\r\n", - getTrigger_type_e(engineConfiguration->trigger.type), + getTrigger_type_e(triggerConfiguration->getType()), getTrigger_event_e(signal), (int)nowNt, currentCycle.current_index); @@ -456,7 +449,7 @@ void TriggerState::decodeTriggerEvent(TriggerWaveform *triggerShape, #if EFI_UNIT_TEST if (printTriggerTrace) { printf("%s event %s %d\r\n", - getTrigger_type_e(engineConfiguration->trigger.type), + getTrigger_type_e(triggerConfiguration->getType()), getTrigger_event_e(signal), nowNt); } @@ -618,7 +611,7 @@ void TriggerState::decodeTriggerEvent(TriggerWaveform *triggerShape, #if EFI_UNIT_TEST if (printTriggerTrace) { printf("decodeTriggerEvent %s isSynchronizationPoint=%d index=%d %s\r\n", - getTrigger_type_e(engineConfiguration->trigger.type), + getTrigger_type_e(triggerConfiguration->getType()), isSynchronizationPoint, currentCycle.current_index, getTrigger_event_e(signal)); } @@ -727,11 +720,4 @@ void initTriggerDecoderLogger(Logging *sharedLogger) { logger = sharedLogger; } - void initTriggerDecoder(DECLARE_ENGINE_PARAMETER_SIGNATURE) { -#if EFI_GPIO_HARDWARE - enginePins.triggerDecoderErrorPin.initPin("led: trigger debug", CONFIG(triggerErrorPin), - &CONFIG(triggerErrorPinMode)); -#endif /* EFI_GPIO_HARDWARE */ -} - #endif /* EFI_SHAFT_POSITION_INPUT */ diff --git a/firmware/controllers/trigger/trigger_decoder.h b/firmware/controllers/trigger/trigger_decoder.h index bad973b47d..b7dcafae2e 100644 --- a/firmware/controllers/trigger/trigger_decoder.h +++ b/firmware/controllers/trigger/trigger_decoder.h @@ -25,10 +25,11 @@ struct TriggerStateListener { class TriggerConfiguration { public: - virtual bool isUseOnlyRisingEdgeForTrigger() = 0; - virtual bool isSilentTriggerError() = 0; - virtual bool isVerboseTriggerSynchDetails() = 0; - virtual debug_mode_e getDebugMode() = 0; + virtual bool isUseOnlyRisingEdgeForTrigger() const = 0; + virtual bool isSilentTriggerError() const = 0; + virtual bool isVerboseTriggerSynchDetails() const = 0; + virtual debug_mode_e getDebugMode() const = 0; + virtual trigger_type_e getType() const = 0; }; typedef void (*TriggerStateCallback)(TriggerState *); @@ -81,13 +82,12 @@ public: TriggerStateListener * triggerStateListener, const TriggerConfiguration * triggerConfiguration, const trigger_event_e signal, - const efitime_t nowUs DECLARE_CONFIG_PARAMETER_SUFFIX); + const efitime_t nowUs); bool validateEventCounters(TriggerWaveform *triggerShape) const; void onShaftSynchronization(const TriggerStateCallback triggerCycleCallback, efitick_t nowNt, TriggerWaveform *triggerShape); - bool isValidIndex(TriggerWaveform *triggerShape) const; float getTriggerDutyCycle(int index); @@ -188,10 +188,7 @@ angle_t getEngineCycle(operation_mode_e operationMode); class Engine; -void initTriggerDecoder(DECLARE_ENGINE_PARAMETER_SIGNATURE); void initTriggerDecoderLogger(Logging *sharedLogger); -bool isTriggerDecoderError(DECLARE_ENGINE_PARAMETER_SIGNATURE); - void calculateTriggerSynchPoint(TriggerWaveform *shape, TriggerState *state DECLARE_ENGINE_PARAMETER_SUFFIX); diff --git a/firmware/controllers/trigger/trigger_simulator.cpp b/firmware/controllers/trigger/trigger_simulator.cpp index 5ea9a4b916..81ba6c7de1 100644 --- a/firmware/controllers/trigger/trigger_simulator.cpp +++ b/firmware/controllers/trigger/trigger_simulator.cpp @@ -72,7 +72,7 @@ void TriggerStimulatorHelper::feedSimulatedEvent(const TriggerStateCallback trig triggerCycleCallback, /* override */ nullptr, triggerConfiguration, - s, time PASS_CONFIG_PARAMETER_SUFFIX); + s, time); } } @@ -84,7 +84,7 @@ void TriggerStimulatorHelper::feedSimulatedEvent(const TriggerStateCallback trig triggerCycleCallback, /* override */ nullptr, triggerConfiguration, - s, time PASS_CONFIG_PARAMETER_SUFFIX); + s, time); } } @@ -96,7 +96,7 @@ void TriggerStimulatorHelper::feedSimulatedEvent(const TriggerStateCallback trig triggerCycleCallback, /* override */ nullptr, triggerConfiguration, - s, time PASS_CONFIG_PARAMETER_SUFFIX); + s, time); } } } diff --git a/firmware/hw_layer/hardware.cpp b/firmware/hw_layer/hardware.cpp index b6e88a183f..3b540782b3 100644 --- a/firmware/hw_layer/hardware.cpp +++ b/firmware/hw_layer/hardware.cpp @@ -477,10 +477,6 @@ void initHardware(Logging *l) { return; } -#if EFI_SHAFT_POSITION_INPUT - initTriggerDecoder(PASS_ENGINE_PARAMETER_SIGNATURE); -#endif - #if HAL_USE_ADC initAdcInputs(); // wait for first set of ADC values so that we do not produce invalid sensor data diff --git a/unit_tests/tests/trigger/test_trigger_decoder.cpp b/unit_tests/tests/trigger/test_trigger_decoder.cpp index 80f7b58470..94324ebdf0 100644 --- a/unit_tests/tests/trigger/test_trigger_decoder.cpp +++ b/unit_tests/tests/trigger/test_trigger_decoder.cpp @@ -126,28 +126,28 @@ TEST(misc, testSomethingWeird) { ASSERT_FALSE(sta->shaft_is_synchronized) << "shaft_is_synchronized"; int r = 10; - sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_FALLING, r PASS_CONFIG_PARAMETER_SUFFIX); + sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_FALLING, r); ASSERT_FALSE(sta->shaft_is_synchronized) << "shaft_is_synchronized"; // still no synchronization - sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_RISING, ++r PASS_CONFIG_PARAMETER_SUFFIX); + sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_RISING, ++r); ASSERT_TRUE(sta->shaft_is_synchronized); // first signal rise synchronize ASSERT_EQ(0, sta->getCurrentIndex()); - sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_FALLING, r++ PASS_CONFIG_PARAMETER_SUFFIX); + sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_FALLING, r++); ASSERT_EQ(1, sta->getCurrentIndex()); for (int i = 2; i < 10;) { - sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_RISING, r++ PASS_CONFIG_PARAMETER_SUFFIX); + sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_RISING, r++); assertEqualsM("even", i++, sta->getCurrentIndex()); - sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_FALLING, r++ PASS_CONFIG_PARAMETER_SUFFIX); + sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_FALLING, r++); assertEqualsM("odd", i++, sta->getCurrentIndex()); } - sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_RISING, r++ PASS_CONFIG_PARAMETER_SUFFIX); + sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_RISING, r++); ASSERT_EQ(10, sta->getCurrentIndex()); - sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_FALLING, r++ PASS_CONFIG_PARAMETER_SUFFIX); + sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_FALLING, r++); ASSERT_EQ(11, sta->getCurrentIndex()); - sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_RISING, r++ PASS_CONFIG_PARAMETER_SUFFIX); + sta->decodeTriggerEvent(&ENGINE(triggerCentral.triggerShape), nullptr, /* override */ nullptr, triggerConfiguration, SHAFT_PRIMARY_RISING, r++); ASSERT_EQ(0, sta->getCurrentIndex()); // new revolution }