2021-08-01 18:52:58 -07:00
|
|
|
/**
|
|
|
|
* @file frequency_sensor.cpp
|
|
|
|
*/
|
|
|
|
|
2021-08-01 12:58:39 -07:00
|
|
|
#include "pch.h"
|
|
|
|
|
|
|
|
#include "frequency_sensor.h"
|
|
|
|
|
|
|
|
#include "digital_input_exti.h"
|
|
|
|
|
2024-03-15 07:20:17 -07:00
|
|
|
#if EFI_PROD_CODE
|
2021-08-01 12:58:39 -07:00
|
|
|
// Callback adapter since we can't pass a member function to a callback
|
2021-11-19 03:37:52 -08:00
|
|
|
static void freqSensorExtiCallback(void* arg, efitick_t nowNt) {
|
|
|
|
reinterpret_cast<FrequencySensor*>(arg)->onEdge(nowNt);
|
2021-08-01 12:58:39 -07:00
|
|
|
}
|
2024-03-15 07:20:17 -07:00
|
|
|
#endif // EFI_PROD_CODE
|
2021-08-01 12:58:39 -07:00
|
|
|
|
2022-04-09 06:00:19 -07:00
|
|
|
void FrequencySensor::initIfValid(brain_pin_e pin, SensorConverter &converter, float filterParameter) {
|
2022-03-28 20:30:37 -07:00
|
|
|
if (!isBrainPinValid(pin)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-09 06:00:19 -07:00
|
|
|
|
2022-04-27 19:29:51 -07:00
|
|
|
// Filter parameter greater than or equal to 0.5 impossible as it causes filter instability, clamp
|
|
|
|
// far under that value.
|
|
|
|
if (filterParameter > 0.35f) {
|
|
|
|
filterParameter = 0.35f;
|
2022-04-09 06:00:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
m_filter.configureLowpass(1, filterParameter);
|
|
|
|
|
2022-03-28 20:30:37 -07:00
|
|
|
setFunction(converter);
|
|
|
|
|
2021-08-01 12:58:39 -07:00
|
|
|
#if EFI_PROD_CODE
|
2024-03-05 12:31:21 -08:00
|
|
|
if (efiExtiEnablePin(getSensorName(), pin, PAL_EVENT_MODE_FALLING_EDGE,
|
|
|
|
freqSensorExtiCallback, reinterpret_cast<void*>(this)) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-01 12:58:39 -07:00
|
|
|
#endif // EFI_PROD_CODE
|
2022-04-09 06:00:19 -07:00
|
|
|
|
2024-03-05 12:31:21 -08:00
|
|
|
m_pin = pin;
|
|
|
|
|
2022-04-09 06:00:19 -07:00
|
|
|
Register();
|
2021-08-01 12:58:39 -07:00
|
|
|
}
|
|
|
|
|
2021-08-20 23:02:05 -07:00
|
|
|
void FrequencySensor::deInit() {
|
|
|
|
if (!isBrainPinValid(m_pin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if EFI_PROD_CODE
|
|
|
|
efiExtiDisablePin(m_pin);
|
|
|
|
#endif
|
|
|
|
|
2022-04-28 14:32:39 -07:00
|
|
|
m_pin = Gpio::Unassigned;
|
2021-08-20 23:02:05 -07:00
|
|
|
}
|
|
|
|
|
2021-08-01 12:58:39 -07:00
|
|
|
void FrequencySensor::onEdge(efitick_t nowNt) {
|
2023-03-31 07:03:18 -07:00
|
|
|
// diagnostics
|
2022-05-14 05:39:08 -07:00
|
|
|
eventCounter++;
|
2021-08-01 12:58:39 -07:00
|
|
|
float frequency = 1 / m_edgeTimer.getElapsedSecondsAndReset(nowNt);
|
|
|
|
|
2022-09-20 20:00:42 -07:00
|
|
|
if (useBiQuad) {
|
|
|
|
frequency = m_filter.filter(frequency);
|
|
|
|
}
|
2022-01-26 14:33:04 -08:00
|
|
|
|
2021-08-01 12:58:39 -07:00
|
|
|
postRawValue(frequency, nowNt);
|
|
|
|
}
|