rusefi-1/firmware/hw_layer/cdm_ion_sense.cpp

84 lines
2.2 KiB
C++
Raw Normal View History

2018-12-31 13:00:41 -08:00
/*
* @file cdm_ion_sense.cpp
*
2019-05-05 14:21:36 -07:00
* Saab Ion Sensing Module integration
*
* See https://github.com/rusefi/rusefi/wiki/Saab_Trionic_8_Combustion-Detection-Module_on_Mazda_Miata_running_rusEfi
2018-12-31 13:00:41 -08:00
*
* Created on: Dec 31, 2018
2020-01-07 21:02:40 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2018-12-31 13:00:41 -08:00
*/
#include "pch.h"
2018-12-31 13:00:41 -08:00
#include "cdm_ion_sense.h"
2019-01-04 20:47:39 -08:00
CdmState::CdmState() {
2019-05-05 15:53:34 -07:00
accumilatingAtRevolution = 0;
2019-01-04 20:47:39 -08:00
currentValue = 0;
2019-05-05 15:53:34 -07:00
accumulatingCurrentValue = 0;
2019-01-04 20:47:39 -08:00
}
2019-05-05 15:53:34 -07:00
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;
}
2019-01-04 20:47:39 -08:00
}
void CdmState::onNewSignal(int currentRevolution) {
2019-05-05 15:53:34 -07:00
if (this->accumilatingAtRevolution == currentRevolution) {
accumulatingCurrentValue++;
2019-01-04 20:47:39 -08:00
} else {
2019-05-05 15:53:34 -07:00
applyAccumulatedData(currentRevolution);
// start new accumulation
accumilatingAtRevolution = currentRevolution;
accumulatingCurrentValue = 1;
2019-01-04 20:47:39 -08:00
}
}
2019-05-05 14:21:36 -07:00
// above logic compiles unconditionally so that unit tests are happy, but without an instance linker would have nothing to link
2019-01-04 20:47:39 -08:00
#if EFI_CDM_INTEGRATION
2019-05-05 14:21:36 -07:00
2019-01-04 20:47:39 -08:00
#include "digital_input_exti.h"
2019-05-05 14:21:36 -07:00
static CdmState instance;
2019-05-05 15:53:34 -07:00
int getCurrentCdmValue(int currentRevolution) {
return instance.getValue(currentRevolution);
}
2019-01-03 20:51:29 -08:00
#if EFI_TUNER_STUDIO
2018-12-31 13:00:41 -08:00
void ionPostState(TunerStudioOutputChannels *tsOutputChannels) {
2019-05-05 14:21:36 -07:00
tsOutputChannels->debugIntField1 = instance.totalCdmEvents;
2018-12-31 13:00:41 -08:00
}
2019-05-05 14:21:36 -07:00
#endif /* EFI_TUNER_STUDIO */
2019-01-04 20:47:39 -08:00
static void extIonCallback(void *arg) {
UNUSED(arg);
2019-05-05 14:21:36 -07:00
instance.totalCdmEvents++;
2019-01-04 20:47:39 -08:00
int currentRevolution = engine->triggerCentral.triggerState.getTotalRevolutionCounter();
instance.onNewSignal(currentRevolution);
}
void cdmIonInit(void) {
if (!isBrainPinValid(engineConfiguration->cdmInputPin)) {
2019-01-04 20:47:39 -08:00
return;
}
efiExtiEnablePin("ion", engineConfiguration->cdmInputPin, PAL_EVENT_MODE_RISING_EDGE, extIonCallback, NULL);
2019-01-04 20:47:39 -08:00
}
#endif /* EFI_CDM_INTEGRATION */