Line data Source code
1 : /* 2 : * @file cdm_ion_sense.cpp 3 : * 4 : * Saab Ion Sensing Module integration 5 : * 6 : * See https://github.com/rusefi/rusefi/wiki/Saab_Trionic_8_Combustion-Detection-Module_on_Mazda_Miata_running_rusEfi 7 : * 8 : * Created on: Dec 31, 2018 9 : * @author Andrey Belomutskiy, (c) 2012-2020 10 : */ 11 : 12 : #include "pch.h" 13 : 14 : #include "cdm_ion_sense.h" 15 : 16 1 : CdmState::CdmState() { 17 1 : accumilatingAtRevolution = 0; 18 1 : currentValue = 0; 19 1 : accumulatingCurrentValue = 0; 20 1 : } 21 : 22 6 : int CdmState::getValue(int currentRevolution) { 23 6 : applyAccumulatedData(currentRevolution); 24 6 : if (currentRevolution == currentValueAtIndex + 1) { 25 : // returning total result of previous engine cycle 26 4 : return currentValue; 27 : } 28 : // we are here if previous engine cycle had no knock events 29 2 : return 0; 30 : } 31 : 32 8 : void CdmState::applyAccumulatedData(int currentRevolution) { 33 8 : if (currentRevolution > accumilatingAtRevolution) { 34 5 : currentValue = accumulatingCurrentValue; 35 5 : currentValueAtIndex = accumilatingAtRevolution; 36 : } 37 8 : } 38 : 39 4 : void CdmState::onNewSignal(int currentRevolution) { 40 4 : if (this->accumilatingAtRevolution == currentRevolution) { 41 2 : accumulatingCurrentValue++; 42 : } else { 43 2 : applyAccumulatedData(currentRevolution); 44 : // start new accumulation 45 2 : accumilatingAtRevolution = currentRevolution; 46 2 : accumulatingCurrentValue = 1; 47 : } 48 4 : } 49 : 50 : // above logic compiles unconditionally so that unit tests are happy, but without an instance linker would have nothing to link 51 : #if EFI_CDM_INTEGRATION 52 : 53 : #include "digital_input_exti.h" 54 : 55 : static CdmState instance; 56 : 57 : int getCurrentCdmValue(int currentRevolution) { 58 : return instance.getValue(currentRevolution); 59 : } 60 : 61 : #if EFI_TUNER_STUDIO 62 : void ionPostState(TunerStudioOutputChannels *tsOutputChannels) { 63 : tsOutputChannels->debugIntField1 = instance.totalCdmEvents; 64 : } 65 : #endif /* EFI_TUNER_STUDIO */ 66 : 67 : static void extIonCallback(void *arg) { 68 : UNUSED(arg); 69 : instance.totalCdmEvents++; 70 : 71 : int currentRevolution = engine->triggerCentral.triggerState.getSynchronizationCounter(); 72 : instance.onNewSignal(currentRevolution); 73 : } 74 : 75 : void cdmIonInit(void) { 76 : if (!isBrainPinValid(engineConfiguration->cdmInputPin)) { 77 : return; 78 : } 79 : 80 : efiExtiEnablePin("ion", engineConfiguration->cdmInputPin, PAL_EVENT_MODE_RISING_EDGE, extIonCallback, NULL); 81 : } 82 : 83 : #endif /* EFI_CDM_INTEGRATION */