diff --git a/firmware/config/stm32f4ems/efifeatures.h b/firmware/config/stm32f4ems/efifeatures.h index fb424d1c8d..b69d88d2f8 100644 --- a/firmware/config/stm32f4ems/efifeatures.h +++ b/firmware/config/stm32f4ems/efifeatures.h @@ -14,6 +14,8 @@ #define EFI_FSIO TRUE +#define EFI_CDM_INTEGRATION TRUE + #define EFI_TEXT_LOGGING TRUE #define EFI_PWM_TESTER FALSE diff --git a/firmware/controllers/algo/engine_configuration.cpp b/firmware/controllers/algo/engine_configuration.cpp index c33cda8f19..eafdc898d1 100644 --- a/firmware/controllers/algo/engine_configuration.cpp +++ b/firmware/controllers/algo/engine_configuration.cpp @@ -311,6 +311,8 @@ void prepareVoidConfiguration(engine_configuration_s *engineConfiguration) { engineConfiguration->flexFuelSensor = GPIO_UNASSIGNED; engineConfiguration->test557pin = GPIO_UNASSIGNED; + boardConfiguration->cdmInputPin = GPIO_UNASSIGNED; + boardConfiguration->joystickCenterPin = GPIO_UNASSIGNED; boardConfiguration->joystickAPin = GPIO_UNASSIGNED; boardConfiguration->joystickBPin = GPIO_UNASSIGNED; diff --git a/firmware/hw_layer/cdm_ion_sense.cpp b/firmware/hw_layer/cdm_ion_sense.cpp index 7ae942aced..1ad2d7a755 100644 --- a/firmware/hw_layer/cdm_ion_sense.cpp +++ b/firmware/hw_layer/cdm_ion_sense.cpp @@ -8,6 +8,32 @@ */ #include "cdm_ion_sense.h" +#include "engine.h" + +CdmState::CdmState() { + currentRevolution = 0; + currentValue = 0; + accumulator = 0; +} + +int CdmState::getValue() { + return currentValue; +} + +void CdmState::onNewSignal(int currentRevolution) { + if (this->currentRevolution == currentRevolution) { + accumulator++; + } else { + this->currentRevolution = currentRevolution; + currentValue = accumulator; + accumulator = 1; + } +} + +#if EFI_CDM_INTEGRATION +#include "digital_input_exti.h" + +EXTERN_ENGINE; #if EFI_TUNER_STUDIO void ionPostState(TunerStudioOutputChannels *tsOutputChannels) { @@ -15,4 +41,34 @@ void ionPostState(TunerStudioOutputChannels *tsOutputChannels) { } #endif +static CdmState instance; + +static void extIonCallback(EXTDriver *extp, expchannel_t channel) { + UNUSED(extp); + + int currentRevolution = engine->triggerCentral.triggerState.getTotalRevolutionCounter(); + + instance.onNewSignal(currentRevolution); + +} + + +void cdmIonInit(void) { + // todo: allow 'GPIOA_0' once we migrate to new mandatory configuration + if (boardConfiguration->cdmInputPin == GPIOA_0 || boardConfiguration->cdmInputPin == GPIO_UNASSIGNED) { + return; + } + int pin = (int)boardConfiguration->cdmInputPin; + if (pin <= 0 || pin > (int)GPIO_UNASSIGNED) { + // todo: remove this protection once we migrate to new mandatory configuration + return; + } + + + enableExti(boardConfiguration->cdmInputPin, EXT_CH_MODE_RISING_EDGE, extIonCallback); + +} + + +#endif /* EFI_CDM_INTEGRATION */ diff --git a/firmware/hw_layer/cdm_ion_sense.h b/firmware/hw_layer/cdm_ion_sense.h index fd62fcbf49..71022a0ee5 100644 --- a/firmware/hw_layer/cdm_ion_sense.h +++ b/firmware/hw_layer/cdm_ion_sense.h @@ -10,9 +10,27 @@ #include "global.h" +class CdmState { +public: + CdmState(); + int currentRevolution; + /** + * accumulated value for engine cycle which is not over yet + */ + int accumulator; + /** + * event counter for previous complete engine cycle + */ + int currentValue; + void onNewSignal(int currentRevolution); + int getValue(); +}; + #if EFI_TUNER_STUDIO #include "tunerstudio_configuration.h" void ionPostState(TunerStudioOutputChannels *tsOutputChannels); #endif +void cdmIonInit(void); + #endif /* HW_LAYER_CDM_ION_SENSE_H_ */ diff --git a/firmware/hw_layer/hardware.cpp b/firmware/hw_layer/hardware.cpp index 2e66cdb077..9c57f806d9 100644 --- a/firmware/hw_layer/hardware.cpp +++ b/firmware/hw_layer/hardware.cpp @@ -42,6 +42,7 @@ #include "settings.h" #include "algo.h" #include "joystick.h" +#include "cdm_ion_sense.h" #include "trigger_central.h" #include "svnversion.h" #include "engine_configuration.h" @@ -490,6 +491,10 @@ void initHardware(Logging *l) { initVehicleSpeed(sharedLogger); #endif +#if EFI_CDM_INTEGRATION + cdmIonInit(); +#endif + #if HAL_USE_EXT || defined(__DOXYGEN__) initJoystick(sharedLogger); #endif diff --git a/simulator/simulator/efifeatures.h b/simulator/simulator/efifeatures.h index 01e158a2a6..17b9703a9a 100644 --- a/simulator/simulator/efifeatures.h +++ b/simulator/simulator/efifeatures.h @@ -14,6 +14,8 @@ #define EFI_PRINTF_FUEL_DETAILS TRUE +#define EFI_CDM_INTEGRATION FALSE + #define EFI_BLUETOOTH_SETUP FALSE #define EFI_GPIO_HARDWARE FALSE diff --git a/unit_tests/test.mk b/unit_tests/test.mk index c702d9a823..7da51abf62 100644 --- a/unit_tests/test.mk +++ b/unit_tests/test.mk @@ -5,6 +5,7 @@ TEST_SRC_CPP = test_util.cpp \ test_basic_math/test_find_index.cpp \ test_basic_math/test_interpolation_3d.cpp \ test_cj125.cpp \ + test_ion.cpp \ test_hip9011.cpp \ test_data_structures/test_engine_math.cpp \ test_startOfCrankingPrimingPulse.cpp \ diff --git a/unit_tests/test_c125.h b/unit_tests/test_c125.h deleted file mode 100644 index a7712c5abb..0000000000 --- a/unit_tests/test_c125.h +++ /dev/null @@ -1,11 +0,0 @@ -/* - * test_c125.h - * - * Created on: Jan 3, 2019 - * @author Andrey Belomutskiy, (c) 2012-2019 - */ - -#ifndef TEST_C125_H_ -#define TEST_C125_H_ - -#endif /* TEST_C125_H_ */ diff --git a/unit_tests/test_cj125.cpp b/unit_tests/test_cj125.cpp index b0f2b6c740..0c11835d4f 100644 --- a/unit_tests/test_cj125.cpp +++ b/unit_tests/test_cj125.cpp @@ -5,7 +5,6 @@ * @author Andrey Belomutskiy, (c) 2012-2019 */ -#include "test_c125.h" #include "gtest/gtest.h" TEST(testCJ125, todo) { diff --git a/unit_tests/test_ion.cpp b/unit_tests/test_ion.cpp new file mode 100644 index 0000000000..9858f199a2 --- /dev/null +++ b/unit_tests/test_ion.cpp @@ -0,0 +1,34 @@ +/* + * test_ion.cpp + * + * Created on: Jan 4, 2019 + * Author: user + */ + +#ifndef TEST_ION_CPP_ +#define TEST_ION_CPP_ + +#include "gtest/gtest.h" +#include "cdm_ion_sense.h" + +TEST(ion, signalCounter) { + CdmState state; + + EXPECT_EQ(0, state.getValue()); + + state.onNewSignal(2); + state.onNewSignal(2); + state.onNewSignal(2); + + // value is still '0' until we signal end of engine cycle + EXPECT_EQ(0, state.getValue()); + + // this signals start of new cycle + state.onNewSignal(4); + EXPECT_EQ(3, state.getValue()); + + +} + + +#endif /* TEST_ION_CPP_ */