diff --git a/firmware/controllers/algo/engine_configuration.cpp b/firmware/controllers/algo/engine_configuration.cpp index 4083b0d1c1..ea72064f5f 100644 --- a/firmware/controllers/algo/engine_configuration.cpp +++ b/firmware/controllers/algo/engine_configuration.cpp @@ -403,6 +403,9 @@ void resetConfigurationExt(Logging * logger, engine_type_e engineType, engine_co * Let's apply global defaults first */ setDefaultConfiguration(engineConfiguration, boardConfiguration); +#if EFI_SIMULATOR + engineConfiguration->directSelfStimulation = true; +#endif /* */ engineConfiguration->engineType = engineType; engineConfiguration->headerMagicValue = HEADER_MAGIC_NUMBER; /** diff --git a/firmware/controllers/trigger/trigger_emulator_algo.cpp b/firmware/controllers/trigger/trigger_emulator_algo.cpp index 3cb0a4ba4a..e72bf5072e 100644 --- a/firmware/controllers/trigger/trigger_emulator_algo.cpp +++ b/firmware/controllers/trigger/trigger_emulator_algo.cpp @@ -9,6 +9,40 @@ #include "engine_configuration.h" #include "LocalVersionHolder.h" #include "ec2.h" +#include "trigger_central.h" + +#if EFI_PROD_CODE +#include "pwm_generator.h" +#endif + +TriggerEmulatorHelper::TriggerEmulatorHelper() { + primaryWheelState = false; + secondaryWheelState = false; + thirdWheelState = false; +} + +void TriggerEmulatorHelper::handleEmulatorCallback(PwmConfig *state, int stateIndex) { + int newPrimaryWheelState = state->multiWave.waves[0].pinStates[stateIndex]; + int newSecondaryWheelState = state->multiWave.waves[1].pinStates[stateIndex]; + int new3rdWheelState = state->multiWave.waves[2].pinStates[stateIndex]; + + if (primaryWheelState != newPrimaryWheelState) { + primaryWheelState = newPrimaryWheelState; + hwHandleShaftSignal(primaryWheelState ? SHAFT_PRIMARY_UP : SHAFT_PRIMARY_DOWN); + } + + if (secondaryWheelState != newSecondaryWheelState) { + secondaryWheelState = newSecondaryWheelState; + hwHandleShaftSignal(secondaryWheelState ? SHAFT_SECONDARY_UP : SHAFT_SECONDARY_DOWN); + } + + if (thirdWheelState != new3rdWheelState) { + thirdWheelState = new3rdWheelState; + hwHandleShaftSignal(thirdWheelState ? SHAFT_3RD_UP : SHAFT_3RD_DOWN); + } + + // print("hello %d\r\n", chTimeNow()); +} extern engine_configuration_s *engineConfiguration; extern engine_configuration2_s *engineConfiguration2; @@ -20,13 +54,19 @@ extern engine_configuration2_s *engineConfiguration2; static int pinStates1[PWM_PHASE_MAX_COUNT]; static int pinStates2[PWM_PHASE_MAX_COUNT]; static int pinStates3[PWM_PHASE_MAX_COUNT]; -static single_wave_s waves[PWM_PHASE_MAX_WAVE_PER_PWM] = {single_wave_s(pinStates1), single_wave_s(pinStates2), single_wave_s(pinStates3)}; -static single_wave_s sr[PWM_PHASE_MAX_WAVE_PER_PWM] = {waves[0], waves[1], waves[2]}; +static single_wave_s waves[PWM_PHASE_MAX_WAVE_PER_PWM] = { single_wave_s(pinStates1), single_wave_s(pinStates2), + single_wave_s(pinStates3) }; +static single_wave_s sr[PWM_PHASE_MAX_WAVE_PER_PWM] = { waves[0], waves[1], waves[2] }; static float swtchTms[PWM_PHASE_MAX_COUNT]; PwmConfig triggerSignal(swtchTms, sr); +#define DO_NOT_STOP 999999999 + +static int stopEmulationAtIndex = DO_NOT_STOP; +static bool isEmulating = true; + static Logging logger; static LocalVersionHolder localVersion; @@ -47,28 +87,63 @@ void setTriggerEmulatorRPM(int rpm) { static void updateTriggerShapeIfNeeded(PwmConfig *state) { if (localVersion.isOld()) { - scheduleMsg(&logger, "Stimulator: updating trigger shape: %d/%d %d", localVersion.getVersion(), getGlobalConfigurationVersion(), currentTimeMillis()); - + scheduleMsg(&logger, "Stimulator: updating trigger shape: %d/%d %d", localVersion.getVersion(), + getGlobalConfigurationVersion(), currentTimeMillis()); applyNonPersistentConfiguration(&logger, engineConfiguration, engineConfiguration2); trigger_shape_s *s = &engineConfiguration2->triggerShape; - int *pinStates[PWM_PHASE_MAX_WAVE_PER_PWM] = {s->wave.waves[0].pinStates, s->wave.waves[1].pinStates, s->wave.waves[2].pinStates}; + int *pinStates[PWM_PHASE_MAX_WAVE_PER_PWM] = { s->wave.waves[0].pinStates, s->wave.waves[1].pinStates, + s->wave.waves[2].pinStates }; copyPwmParameters(state, s->getSize(), s->wave.switchTimes, PWM_PHASE_MAX_WAVE_PER_PWM, pinStates); state->safe.periodNt = -1; // this would cause loop re-initialization } } -void initTriggerEmulatorLogic(pwm_gen_callback *stateChangeCallback) { - initLogging(&logger, "position sensor(s) emulator"); +static TriggerEmulatorHelper helper; +#if EFI_EMULATE_POSITION_SENSORS || defined(__DOXYGEN__) + +static void emulatorApplyPinState(PwmConfig *state, int stateIndex) { + if (stopEmulationAtIndex == stateIndex) { + isEmulating = false; + } + if (!isEmulating) { + return; + } +#if EFI_PROD_CODE + applyPinState(state, stateIndex); +#endif /* EFI_PROD_CODE */ + if (engineConfiguration->directSelfStimulation) { + /** + * this callback would invoke the input signal handlers directly + */ + helper.handleEmulatorCallback(state, stateIndex); + } +} +#endif /* EFI_EMULATE_POSITION_SENSORS */ + +static void setEmulatorAtIndex(int index) { + stopEmulationAtIndex = index; +} + +static void resumeStimulator(void) { + isEmulating = true; + stopEmulationAtIndex = DO_NOT_STOP; +} + +void initTriggerEmulatorLogic(void) { + initLogging(&logger, "position sensor(s) emulator"); trigger_shape_s *s = &engineConfiguration2->triggerShape; setTriggerEmulatorRPM(engineConfiguration->bc.triggerSimulatorFrequency); - int *pinStates[PWM_PHASE_MAX_WAVE_PER_PWM] = { s->wave.waves[0].pinStates, s->wave.waves[1].pinStates, s->wave.waves[2].pinStates}; - weComplexInit("position sensor", &triggerSignal, s->getSize(), s->wave.switchTimes, PWM_PHASE_MAX_WAVE_PER_PWM, pinStates, - updateTriggerShapeIfNeeded, stateChangeCallback); + int *pinStates[PWM_PHASE_MAX_WAVE_PER_PWM] = { s->wave.waves[0].pinStates, s->wave.waves[1].pinStates, + s->wave.waves[2].pinStates }; + weComplexInit("position sensor", &triggerSignal, s->getSize(), s->wave.switchTimes, PWM_PHASE_MAX_WAVE_PER_PWM, + pinStates, updateTriggerShapeIfNeeded, emulatorApplyPinState); addConsoleActionI("rpm", &setTriggerEmulatorRPM); + addConsoleActionI("stop_stimulator_at_index", setEmulatorAtIndex); + addConsoleAction("resume_stimulator", resumeStimulator); } diff --git a/firmware/controllers/trigger/trigger_emulator_algo.h b/firmware/controllers/trigger/trigger_emulator_algo.h index aae64a1a95..1b828ba443 100644 --- a/firmware/controllers/trigger/trigger_emulator_algo.h +++ b/firmware/controllers/trigger/trigger_emulator_algo.h @@ -10,6 +10,16 @@ #include "pwm_generator_logic.h" -void initTriggerEmulatorLogic(pwm_gen_callback *callback); +class TriggerEmulatorHelper { +public: + bool primaryWheelState; + bool secondaryWheelState; + bool thirdWheelState; + + TriggerEmulatorHelper(); + void handleEmulatorCallback(PwmConfig *state, int stateIndex); +}; + +void initTriggerEmulatorLogic(void); #endif /* TRIGGER_EMULATOR_ALGO_H_ */ diff --git a/firmware/emulation/trigger_emulator.cpp b/firmware/emulation/trigger_emulator.cpp index 354f2a39a9..a52c0b90fb 100644 --- a/firmware/emulation/trigger_emulator.cpp +++ b/firmware/emulation/trigger_emulator.cpp @@ -27,47 +27,6 @@ extern board_configuration_s *boardConfiguration; extern PwmConfig triggerSignal; -TriggerEmulatorHelper::TriggerEmulatorHelper() { - primaryWheelState = false; - secondaryWheelState = false; - thirdWheelState = false; -} - -void TriggerEmulatorHelper::handleEmulatorCallback(PwmConfig *state, int stateIndex) { - int newPrimaryWheelState = state->multiWave.waves[0].pinStates[stateIndex]; - int newSecondaryWheelState = state->multiWave.waves[1].pinStates[stateIndex]; - int new3rdWheelState = state->multiWave.waves[2].pinStates[stateIndex]; - - if (primaryWheelState != newPrimaryWheelState) { - primaryWheelState = newPrimaryWheelState; - hwHandleShaftSignal(primaryWheelState ? SHAFT_PRIMARY_UP : SHAFT_PRIMARY_DOWN); - } - - if (secondaryWheelState != newSecondaryWheelState) { - secondaryWheelState = newSecondaryWheelState; - hwHandleShaftSignal(secondaryWheelState ? SHAFT_SECONDARY_UP : SHAFT_SECONDARY_DOWN); - } - - if (thirdWheelState != new3rdWheelState) { - thirdWheelState = new3rdWheelState; - hwHandleShaftSignal(thirdWheelState ? SHAFT_3RD_UP : SHAFT_3RD_DOWN); - } - - // print("hello %d\r\n", chTimeNow()); -} - -static TriggerEmulatorHelper helper; - -#if EFI_EMULATE_POSITION_SENSORS || defined(__DOXYGEN__) - -static void emulatorApplyPinState(PwmConfig *state, int stateIndex) { - applyPinState(state, stateIndex); - if (engineConfiguration->directSelfStimulation) { - helper.handleEmulatorCallback(state, stateIndex); - } -} -#endif /* EFI_EMULATE_POSITION_SENSORS */ - void initTriggerEmulator(void) { #if EFI_EMULATE_POSITION_SENSORS || defined(__DOXYGEN__) print("Emulating %s\r\n", getConfigurationName(engineConfiguration->engineType)); @@ -76,6 +35,7 @@ void initTriggerEmulator(void) { triggerSignal.outputPins[1] = TRIGGER_EMULATOR_SECONDARY; triggerSignal.outputPins[2] = TRIGGER_EMULATOR_3RD; +#if EFI_PROD_CODE // todo: refactor, make this a loop outputPinRegisterExt2("distributor ch1", triggerSignal.outputPins[0], boardConfiguration->triggerSimulatorPins[0], &boardConfiguration->triggerSimulatorPinModes[0]); @@ -85,9 +45,9 @@ void initTriggerEmulator(void) { outputPinRegisterExt2("distributor ch3", triggerSignal.outputPins[2], boardConfiguration->triggerSimulatorPins[2], &boardConfiguration->triggerSimulatorPinModes[2]); +#endif /* EFI_PROD_CODE */ - - initTriggerEmulatorLogic(emulatorApplyPinState); + initTriggerEmulatorLogic(); #else print("No position sensor(s) emulation\r\n"); #endif /* EFI_EMULATE_POSITION_SENSORS */ diff --git a/firmware/emulation/trigger_emulator.h b/firmware/emulation/trigger_emulator.h index 8c7ed5b11c..e354f70d6b 100644 --- a/firmware/emulation/trigger_emulator.h +++ b/firmware/emulation/trigger_emulator.h @@ -15,19 +15,6 @@ #include "engine_configuration.h" #include "pwm_generator_logic.h" -class TriggerEmulatorHelper { -public: - bool primaryWheelState; - bool secondaryWheelState; - bool thirdWheelState; - - TriggerEmulatorHelper(); - - void handleEmulatorCallback(PwmConfig *state, int stateIndex); - -}; - - void initTriggerEmulator(void); void setTriggerEmulatorRPM(int value); diff --git a/win32_functional_tests/Makefile b/win32_functional_tests/Makefile index e1d2a2047c..88076652d6 100644 --- a/win32_functional_tests/Makefile +++ b/win32_functional_tests/Makefile @@ -131,6 +131,7 @@ CPPSRC = $(UTILSRC_CPP) \ $(CONTROLLERS_MATH_SRC_CPP) \ $(ENGINES_SRC_CPP) \ $(PROJECT_DIR)/simulator/rusEfiFunctionalTest.cpp \ + $(PROJECT_DIR)/simulator/framework.cpp \ $(PROJECT_DIR)/emulation/trigger_emulator.cpp \ $(TEST_SRC_CPP) # C sources to be compiled in ARM mode regardless of the global setting. diff --git a/win32_functional_tests/simulator/efifeatures.h b/win32_functional_tests/simulator/efifeatures.h index 75df464644..885eb3745e 100644 --- a/win32_functional_tests/simulator/efifeatures.h +++ b/win32_functional_tests/simulator/efifeatures.h @@ -16,6 +16,11 @@ #define EFI_SUPPORT_NISSAN_PRIMERA TRUE #define EFI_SUPPORT_1995_FORD_INLINE_6 TRUE +/** + * simulator works via self-stimulation which works via trigger emulation + */ +#define EFI_EMULATE_POSITION_SENSORS TRUE + #define EFI_SIGNAL_EXECUTOR_SLEEP TRUE #define EFI_INTERNAL_ADC FALSE diff --git a/win32_functional_tests/simulator/framework.cpp b/win32_functional_tests/simulator/framework.cpp new file mode 100644 index 0000000000..2844a01504 --- /dev/null +++ b/win32_functional_tests/simulator/framework.cpp @@ -0,0 +1,9 @@ +/** + * @file framework.cpp + * + * @date Sep 25, 2014 + * @author Andrey Belomutskiy, (c) 2012-2014 + */ + + + diff --git a/win32_functional_tests/simulator/framework.h b/win32_functional_tests/simulator/framework.h new file mode 100644 index 0000000000..d3fa103e5f --- /dev/null +++ b/win32_functional_tests/simulator/framework.h @@ -0,0 +1,14 @@ +/** + * @file framework.h + * + * @date Sep 25, 2014 + * @author Andrey Belomutskiy, (c) 2012-2014 + */ +#ifndef FRAMEWORK_H_ +#define FRAMEWORK_H_ + + + + + +#endif /* FRAMEWORK_H_ */ diff --git a/win32_functional_tests/simulator/rusEfiFunctionalTest.cpp b/win32_functional_tests/simulator/rusEfiFunctionalTest.cpp index d57d08da89..f25badc738 100644 --- a/win32_functional_tests/simulator/rusEfiFunctionalTest.cpp +++ b/win32_functional_tests/simulator/rusEfiFunctionalTest.cpp @@ -68,12 +68,6 @@ float getMap(void) { return getRawMap(); } -static TriggerEmulatorHelper helper; - -static void triggerEmulatorCallback(PwmConfig *state, int stateIndex) { - helper.handleEmulatorCallback(state, stateIndex); -} - void rusEfiFunctionalTest(void) { initializeConsole(); @@ -93,7 +87,7 @@ void rusEfiFunctionalTest(void) { initAnalogChart(); - initTriggerEmulatorLogic(triggerEmulatorCallback); + initTriggerEmulator(); initMainEventListener(&engine, engineConfiguration2);