From e89b7b4b11510ad3a732f1d9fb20f6bf2f77d598 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Fri, 24 Apr 2020 11:00:06 -0700 Subject: [PATCH] trigger emulator refactoring (#1358) * trigger gen refactoring * missed one * fix makefile * commentary Co-authored-by: Matthew Kennedy --- .../controllers/algo/engine_configuration.cpp | 2 +- firmware/controllers/settings.cpp | 2 +- .../trigger/trigger_emulator_algo.cpp | 64 +++++++++++++++---- .../trigger/trigger_emulator_algo.h | 10 ++- firmware/development/development.mk | 3 +- firmware/development/engine_emulator.cpp | 2 +- firmware/development/trigger_emulator.cpp | 63 ------------------ firmware/development/trigger_emulator.h | 19 ------ simulator/Makefile | 1 - simulator/simulator/rusEfiFunctionalTest.cpp | 1 - 10 files changed, 63 insertions(+), 104 deletions(-) delete mode 100644 firmware/development/trigger_emulator.cpp delete mode 100644 firmware/development/trigger_emulator.h diff --git a/firmware/controllers/algo/engine_configuration.cpp b/firmware/controllers/algo/engine_configuration.cpp index b6db9ec725..438a60e3ab 100644 --- a/firmware/controllers/algo/engine_configuration.cpp +++ b/firmware/controllers/algo/engine_configuration.cpp @@ -102,7 +102,7 @@ #endif /* EFI_PROD_CODE */ #if EFI_EMULATE_POSITION_SENSORS -#include "trigger_emulator.h" +#include "trigger_emulator_algo.h" #endif /* EFI_EMULATE_POSITION_SENSORS */ #if EFI_TUNER_STUDIO diff --git a/firmware/controllers/settings.cpp b/firmware/controllers/settings.cpp index d9ccc36e33..0659397e71 100644 --- a/firmware/controllers/settings.cpp +++ b/firmware/controllers/settings.cpp @@ -25,7 +25,7 @@ #include "idle_thread.h" #include "allsensors.h" #include "alternator_controller.h" -#include "trigger_emulator.h" +#include "trigger_emulator_algo.h" #include "sensor.h" #if EFI_PROD_CODE diff --git a/firmware/controllers/trigger/trigger_emulator_algo.cpp b/firmware/controllers/trigger/trigger_emulator_algo.cpp index adfefd0cbe..48b6b47826 100644 --- a/firmware/controllers/trigger/trigger_emulator_algo.cpp +++ b/firmware/controllers/trigger/trigger_emulator_algo.cpp @@ -35,6 +35,7 @@ bool needEvent(const int currentIndex, const int size, MultiChannelStateSequence #include "engine_configuration.h" #include "trigger_central.h" #include "trigger_simulator.h" +#include "settings.h" #include "pwm_generator.h" @@ -43,28 +44,27 @@ TriggerEmulatorHelper::TriggerEmulatorHelper() { EXTERN_ENGINE; +static OutputPin emulatorOutputs[3]; + void TriggerEmulatorHelper::handleEmulatorCallback(PwmConfig *state, int stateIndex) { efitick_t stamp = getTimeNowNt(); // todo: code duplication with TriggerStimulatorHelper::feedSimulatedEvent? MultiChannelStateSequence *multiChannelStateSequence = &state->multiChannelStateSequence; - if (needEvent(stateIndex, state->phaseCount, &state->multiChannelStateSequence, 0)) { - pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/0, stateIndex); - hwHandleShaftSignal(currentValue ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING, stamp); - } + for (size_t i = 0; i < efi::size(emulatorOutputs); i++) + { + if (needEvent(stateIndex, state->phaseCount, &state->multiChannelStateSequence, i)) { + pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/i, stateIndex); + + constexpr trigger_event_e riseEvents[] = { SHAFT_PRIMARY_RISING, SHAFT_SECONDARY_RISING, SHAFT_3RD_RISING }; + constexpr trigger_event_e fallEvents[] = { SHAFT_PRIMARY_FALLING, SHAFT_SECONDARY_FALLING, SHAFT_3RD_FALLING }; - if (needEvent(stateIndex, state->phaseCount, &state->multiChannelStateSequence, 1)) { - pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/1, stateIndex); - hwHandleShaftSignal(currentValue ? SHAFT_SECONDARY_RISING : SHAFT_SECONDARY_FALLING, stamp); - } + trigger_event_e event = (currentValue ? riseEvents : fallEvents)[i]; - if (needEvent(stateIndex, state->phaseCount, &state->multiChannelStateSequence, 2)) { - pin_state_t currentValue = multiChannelStateSequence->getChannelState(/*phaseIndex*/2, stateIndex); - hwHandleShaftSignal(currentValue ? SHAFT_3RD_RISING : SHAFT_3RD_FALLING, stamp); + hwHandleShaftSignal(event, stamp); + } } - - // print("hello %d\r\n", chTimeNow()); } /* @@ -131,6 +131,7 @@ static void updateTriggerWaveformIfNeeded(PwmConfig *state) { } static TriggerEmulatorHelper helper; +static bool hasStimPins = false; static void emulatorApplyPinState(int stateIndex, PwmConfig *state) /* pwm_gen_callback */ { if (stopEmulationAtIndex == stateIndex) { @@ -148,7 +149,10 @@ static void emulatorApplyPinState(int stateIndex, PwmConfig *state) /* pwm_gen_c } #if EFI_PROD_CODE - applyPinState(stateIndex, state); + // Only set pins if they're configured - no need to waste the cycles otherwise + if (hasStimPins) { + applyPinState(stateIndex, state); + } #endif /* EFI_PROD_CODE */ } @@ -180,4 +184,36 @@ void initTriggerEmulatorLogic(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUF addConsoleActionI("stop_stimulator_at_index", setEmulatorAtIndex); addConsoleAction("resume_stimulator", resumeStimulator); } + +void onConfigurationChangeRpmEmulatorCallback(engine_configuration_s *previousConfiguration) { + if (engineConfiguration->triggerSimulatorFrequency == + previousConfiguration->triggerSimulatorFrequency) { + return; + } + setTriggerEmulatorRPM(engineConfiguration->triggerSimulatorFrequency); +} + +void initTriggerEmulator(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) { + print("Emulating %s\r\n", getConfigurationName(engineConfiguration->engineType)); + + for (size_t i = 0; i < efi::size(emulatorOutputs); i++) + { + triggerSignal.outputPins[i] = &emulatorOutputs[i]; + + brain_pin_e pin = CONFIG(triggerSimulatorPins)[i]; + + // Only bother trying to set output pins if they're configured + if (pin != GPIO_UNASSIGNED) { + hasStimPins = true; + } + +#if EFI_PROD_CODE + triggerSignal.outputPins[i]->initPin("Trigger emulator", pin, + &CONFIG(triggerSimulatorPinModes)[i]); +#endif + } + + initTriggerEmulatorLogic(sharedLogger); +} + #endif /* EFI_EMULATE_POSITION_SENSORS */ diff --git a/firmware/controllers/trigger/trigger_emulator_algo.h b/firmware/controllers/trigger/trigger_emulator_algo.h index a143e93cc8..cd5c89c07f 100644 --- a/firmware/controllers/trigger/trigger_emulator_algo.h +++ b/firmware/controllers/trigger/trigger_emulator_algo.h @@ -8,7 +8,15 @@ #pragma once #include "engine.h" -#include "pwm_generator_logic.h" + +class Logging; +class PwmConfig; +class MultiChannelStateSequence; + +void initTriggerEmulator(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX); +void setTriggerEmulatorRPM(int value DECLARE_ENGINE_PARAMETER_SUFFIX); +void onConfigurationChangeRpmEmulatorCallback(engine_configuration_s *previousConfiguration); + class TriggerEmulatorHelper { public: diff --git a/firmware/development/development.mk b/firmware/development/development.mk index 4ccc15e29a..b1421eacb0 100644 --- a/firmware/development/development.mk +++ b/firmware/development/development.mk @@ -3,8 +3,7 @@ DEVELOPMENT_DIR=$(PROJECT_DIR)/development DEV_SRC = $(DEVELOPMENT_DIR)/test/test.c \ $(DEVELOPMENT_DIR)/test/testbmk.c -DEV_SRC_CPP = $(DEVELOPMENT_DIR)/trigger_emulator.cpp \ - $(DEVELOPMENT_DIR)/hw_layer/poten.cpp \ +DEV_SRC_CPP = $(DEVELOPMENT_DIR)/hw_layer/poten.cpp \ $(DEVELOPMENT_DIR)/sensor_chart.cpp \ $(DEVELOPMENT_DIR)/rfi_perftest.cpp \ $(DEVELOPMENT_DIR)/engine_emulator.cpp \ diff --git a/firmware/development/engine_emulator.cpp b/firmware/development/engine_emulator.cpp index 6c0929dd01..eb994d1bbc 100644 --- a/firmware/development/engine_emulator.cpp +++ b/firmware/development/engine_emulator.cpp @@ -20,7 +20,7 @@ #include "pwm_generator_logic.h" #include "poten.h" -#include "trigger_emulator.h" +#include "trigger_emulator_algo.h" EXTERN_ENGINE; diff --git a/firmware/development/trigger_emulator.cpp b/firmware/development/trigger_emulator.cpp deleted file mode 100644 index c5369db2ff..0000000000 --- a/firmware/development/trigger_emulator.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/** - * @file trigger_emulator.cpp - * @brief Position sensor(s) emulation code - * - * This file is mostly about initialization, the configuration is - * provided by the configureShaftPositionEmulatorShape method - * - * @date Dec 9, 2012 - * @author Andrey Belomutskiy, (c) 2012-2020 - */ - -#include "trigger_emulator.h" -#include "eficonsole.h" -#include "main_trigger_callback.h" -#include "io_pins.h" -#include "trigger_emulator_algo.h" -#include "trigger_central.h" -#include "settings.h" - -#if EFI_PROD_CODE -#include "pwm_generator.h" -#include "pin_repository.h" -#endif /* EFI_PROD_CODE */ - -extern PwmConfig triggerSignal; - -static OutputPin emulatorOutputs[3]; - -EXTERN_ENGINE; - -void onConfigurationChangeRpmEmulatorCallback(engine_configuration_s *previousConfiguration) { - if (engineConfiguration->triggerSimulatorFrequency == - previousConfiguration->triggerSimulatorFrequency) { - return; - } - setTriggerEmulatorRPM(engineConfiguration->triggerSimulatorFrequency); -} - -void initTriggerEmulator(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) { -#if EFI_EMULATE_POSITION_SENSORS - print("Emulating %s\r\n", getConfigurationName(engineConfiguration->engineType)); - - triggerSignal.outputPins[0] = &emulatorOutputs[0]; - triggerSignal.outputPins[1] = &emulatorOutputs[1]; - triggerSignal.outputPins[2] = &emulatorOutputs[2]; - -#if EFI_PROD_CODE - // todo: refactor, make this a loop - triggerSignal.outputPins[0]->initPin("Trigger emulator 1", CONFIG(triggerSimulatorPins)[0], - &CONFIG(triggerSimulatorPinModes)[0]); - - triggerSignal.outputPins[1]->initPin("Trigger emulator 2", CONFIG(triggerSimulatorPins)[1], - &CONFIG(triggerSimulatorPinModes)[1]); - - triggerSignal.outputPins[2]->initPin("Trigger emulator 3", CONFIG(triggerSimulatorPins)[2], - &CONFIG(triggerSimulatorPinModes)[2]); -#endif /* EFI_PROD_CODE */ - - initTriggerEmulatorLogic(sharedLogger); -#else - print("No position sensor(s) emulation\r\n"); -#endif /* EFI_EMULATE_POSITION_SENSORS */ -} diff --git a/firmware/development/trigger_emulator.h b/firmware/development/trigger_emulator.h deleted file mode 100644 index c4d3193beb..0000000000 --- a/firmware/development/trigger_emulator.h +++ /dev/null @@ -1,19 +0,0 @@ -/** - * @file trigger_emulator.h - * @brief Position sensor(s) emulation header - * - * @date Dec 9, 2012 - * @author Andrey Belomutskiy, (c) 2012-2020 - */ - -#pragma once - -#include "global.h" - -#include "trigger_structure.h" -#include "engine.h" - -void initTriggerEmulator(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX); -void setTriggerEmulatorRPM(int value DECLARE_ENGINE_PARAMETER_SUFFIX); -void onConfigurationChangeRpmEmulatorCallback(engine_configuration_s *previousConfiguration); - diff --git a/simulator/Makefile b/simulator/Makefile index 4073f86510..42041676f4 100644 --- a/simulator/Makefile +++ b/simulator/Makefile @@ -156,7 +156,6 @@ CPPSRC = $(UTILSRC_CPP) \ $(CONTROLLERS_ALGO_SRC_CPP) \ $(CONTROLLERS_SRC_CPP) \ $(PROJECT_DIR)/development/sensor_chart.cpp \ - $(PROJECT_DIR)/development/trigger_emulator.cpp \ $(HW_LAYER_EMS_CPP) \ $(HW_SENSORS_SRC) \ $(TRIGGER_SRC_CPP) \ diff --git a/simulator/simulator/rusEfiFunctionalTest.cpp b/simulator/simulator/rusEfiFunctionalTest.cpp index 26a1c8aba9..2f56efea66 100644 --- a/simulator/simulator/rusEfiFunctionalTest.cpp +++ b/simulator/simulator/rusEfiFunctionalTest.cpp @@ -25,7 +25,6 @@ #include "bench_test.h" #include "engine.h" #include "tunerstudio.h" -#include "trigger_emulator.h" #include "engine_controller.h" #include "map_averaging.h" #include "memstreams.h"