diff --git a/firmware/controllers/trigger/main_trigger_callback.cpp b/firmware/controllers/trigger/main_trigger_callback.cpp index 025a12e81d..8ef816d734 100644 --- a/firmware/controllers/trigger/main_trigger_callback.cpp +++ b/firmware/controllers/trigger/main_trigger_callback.cpp @@ -43,6 +43,7 @@ #include "histogram.h" #include "fuel_math.h" #include "histogram.h" +#include "cdm_ion_sense.h" #include "engine_controller.h" #include "efi_gpio.h" #if EFI_PROD_CODE @@ -448,6 +449,13 @@ void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t trgEventIndex D } efiAssertVoid(CUSTOM_STACK_6629, getCurrentRemainingStack() > 128, "lowstck#2"); +#if EFI_CDM_INTEGRATION + if (trgEventIndex == 0 && CONFIGB(cdmInputPin) != GPIO_UNASSIGNED) { + int cdmKnockValue = getCurrentCdmValue(engine->triggerCentral.triggerState.getTotalRevolutionCounter()); + engine->knockLogic(cdmKnockValue); + } +#endif /* EFI_CDM_INTEGRATION */ + if (trgEventIndex >= ENGINE(engineCycleEventCount)) { /** * this could happen in case of a trigger error, just exit silently since the trigger error is supposed to be handled already diff --git a/firmware/hw_layer/cdm_ion_sense.cpp b/firmware/hw_layer/cdm_ion_sense.cpp index 781f2e02e1..65277d210f 100644 --- a/firmware/hw_layer/cdm_ion_sense.cpp +++ b/firmware/hw_layer/cdm_ion_sense.cpp @@ -13,22 +13,36 @@ #include "engine.h" CdmState::CdmState() { - currentRevolution = 0; + accumilatingAtRevolution = 0; currentValue = 0; - accumulator = 0; + accumulatingCurrentValue = 0; } -int CdmState::getValue() { - return currentValue; +int CdmState::getValue(int currentRevolution) { + applyAccumulatedData(currentRevolution); + if (currentRevolution == currentValueAtIndex + 1) { + // returning total result of previous engine cycle + return currentValue; + } + // we are here if previous engine cycle had no knock events + return 0; +} + +void CdmState::applyAccumulatedData(int currentRevolution) { + if (currentRevolution > accumilatingAtRevolution) { + currentValue = accumulatingCurrentValue; + currentValueAtIndex = accumilatingAtRevolution; + } } void CdmState::onNewSignal(int currentRevolution) { - if (this->currentRevolution == currentRevolution) { - accumulator++; + if (this->accumilatingAtRevolution == currentRevolution) { + accumulatingCurrentValue++; } else { - this->currentRevolution = currentRevolution; - currentValue = accumulator; - accumulator = 1; + applyAccumulatedData(currentRevolution); + // start new accumulation + accumilatingAtRevolution = currentRevolution; + accumulatingCurrentValue = 1; } } @@ -41,6 +55,10 @@ EXTERN_ENGINE; static CdmState instance; +int getCurrentCdmValue(int currentRevolution) { + return instance.getValue(currentRevolution); +} + #if EFI_TUNER_STUDIO void ionPostState(TunerStudioOutputChannels *tsOutputChannels) { tsOutputChannels->debugIntField1 = instance.totalCdmEvents; @@ -56,8 +74,7 @@ static void extIonCallback(void *arg) { } void cdmIonInit(void) { - // todo: allow 'GPIOA_0' once we migrate to new mandatory configuration - if (CONFIGB(cdmInputPin) == GPIOA_0 || CONFIGB(cdmInputPin) == GPIO_UNASSIGNED) { + if (CONFIGB(cdmInputPin) == GPIO_UNASSIGNED) { return; } int pin = (int)CONFIGB(cdmInputPin); diff --git a/firmware/hw_layer/cdm_ion_sense.h b/firmware/hw_layer/cdm_ion_sense.h index 2bc9dd90ed..444f55aa96 100644 --- a/firmware/hw_layer/cdm_ion_sense.h +++ b/firmware/hw_layer/cdm_ion_sense.h @@ -1,7 +1,7 @@ /* - * cdm_ion_sense.h + * @file cdm_ion_sense.h * - * Created on: Dec 31, 2018 + * @date Dec 31, 2018 * @author Andrey Belomutskiy, (c) 2012-2019 */ @@ -13,18 +13,21 @@ class CdmState { public: CdmState(); - int currentRevolution; + int totalCdmEvents = 0; + + int accumilatingAtRevolution = -1; /** * accumulated value for engine cycle which is not over yet */ - int accumulator; - int totalCdmEvents = 0; + int accumulatingCurrentValue; /** * event counter for previous complete engine cycle */ - int currentValue; + int currentValue = 0; + int currentValueAtIndex = -1; void onNewSignal(int currentRevolution); - int getValue(); + void applyAccumulatedData(int currentRevolution); + int getValue(int currentRevolution); }; #if EFI_TUNER_STUDIO @@ -33,5 +36,6 @@ void ionPostState(TunerStudioOutputChannels *tsOutputChannels); #endif void cdmIonInit(void); +int getCurrentCdmValue(int currentRevolution); #endif /* HW_LAYER_CDM_ION_SENSE_H_ */ diff --git a/unit_tests/tests/test_ion.cpp b/unit_tests/tests/test_ion.cpp index 9858f199a2..4aec1141bf 100644 --- a/unit_tests/tests/test_ion.cpp +++ b/unit_tests/tests/test_ion.cpp @@ -14,18 +14,27 @@ TEST(ion, signalCounter) { CdmState state; - EXPECT_EQ(0, state.getValue()); + EXPECT_EQ(0, state.getValue(0)); 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()); + EXPECT_EQ(0, state.getValue(2)); + + // this invocation would flush current accumulation + EXPECT_EQ(3, state.getValue(3)); + + state.onNewSignal(3); + // returning previous full cycle value + EXPECT_EQ(3, state.getValue(3)); + + + EXPECT_EQ(1, state.getValue(4)); + EXPECT_EQ(0, state.getValue(5)); + - // this signals start of new cycle - state.onNewSignal(4); - EXPECT_EQ(3, state.getValue()); }