fome-fw/firmware/hw_layer/cdm_ion_sense.cpp

73 lines
1.8 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
*
2018-12-31 13:00:41 -08:00
* See https://github.com/rusefi/rusefi_documentation/tree/master/misc/Saab_Trionic_8_Combustion%20Detection%20Module_on_Mazda_Miata_running_rusEfi
*
* Created on: Dec 31, 2018
* @author Andrey Belomutskiy, (c) 2012-2019
*/
#include "cdm_ion_sense.h"
2019-01-04 20:47:39 -08:00
#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;
}
}
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"
EXTERN_ENGINE;
2018-12-31 13:00:41 -08:00
2019-05-05 14:21:36 -07:00
static CdmState instance;
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) {
// todo: allow 'GPIOA_0' once we migrate to new mandatory configuration
if (CONFIGB(cdmInputPin) == GPIOA_0 || CONFIGB(cdmInputPin) == GPIO_UNASSIGNED) {
2019-01-04 20:47:39 -08:00
return;
}
int pin = (int)CONFIGB(cdmInputPin);
2019-01-04 20:47:39 -08:00
if (pin <= 0 || pin > (int)GPIO_UNASSIGNED) {
// todo: remove this protection once we migrate to new mandatory configuration
return;
}
efiExtiEnablePin("ion", CONFIGB(cdmInputPin), PAL_EVENT_MODE_RISING_EDGE, extIonCallback, NULL);
2019-01-04 20:47:39 -08:00
}
#endif /* EFI_CDM_INTEGRATION */