rusefi/firmware/init/sensor/init_sensors.cpp

199 lines
5.0 KiB
C++
Raw Normal View History

2019-09-21 12:33:13 -07:00
/**
* @file init_sensorss.cpp
*/
#include "pch.h"
#include "init.h"
#include "cli_registry.h"
#include "io_pins.h"
#include "lua_hooks.h"
static void initSensorCli();
void initIfValid(const char* msg, adc_channel_e channel) {
if (!isAdcChannelValid(channel)) {
return;
}
2023-05-31 20:11:49 -07:00
#if EFI_PROD_CODE && HAL_USE_ADC
2023-01-07 18:56:12 -08:00
/**
TODO: this code is similar to AdcSubscription::SubscribeSensor, what is the plan? shall we extract helper method or else?
*/
brain_pin_e pin = getAdcChannelBrainPin(msg, channel);
2023-01-07 18:56:12 -08:00
if (pin == Gpio::Invalid) {
// todo: external muxes for internal ADC #3350
return;
}
efiSetPadMode(msg, pin, PAL_MODE_INPUT_ANALOG);
2023-05-31 20:11:49 -07:00
#endif // EFI_PROD_CODE && HAL_USE_ADC
}
void deInitIfValid(const char* msg, adc_channel_e channel) {
if (!isAdcChannelValid(channel)) {
return;
}
2023-05-31 20:11:49 -07:00
#if EFI_PROD_CODE && HAL_USE_ADC
brain_pin_e pin = getAdcChannelBrainPin(msg, channel);
efiSetPadUnused(pin);
2023-05-31 20:11:49 -07:00
#endif // EFI_PROD_CODE && HAL_USE_ADC
}
static void initOldAnalogInputs() {
if (isAdcChannelValid(engineConfiguration->afr.hwChannel) && engineConfiguration->enableAemXSeries) {
criticalError("Please pick either analog AFR or CAN AFR input not both.");
}
initIfValid("AFR", engineConfiguration->afr.hwChannel);
initIfValid("AUXF#1", engineConfiguration->auxFastSensor1_adcChannel);
}
static void deInitOldAnalogInputs() {
deInitIfValid("AFR", activeConfiguration.afr.hwChannel);
deInitIfValid("AUXF#1", activeConfiguration.auxFastSensor1_adcChannel);
}
static void initAuxDigital() {
#if EFI_PROD_CODE
for (size_t i = 0;i<efi::size(engineConfiguration->luaDigitalInputPins);i++) {
efiSetPadMode("Lua Digital", engineConfiguration->luaDigitalInputPins[i], engineConfiguration->luaDigitalInputPinModes[i]);
}
#endif // EFI_PROD_CODE
}
void pokeAuxDigital() {
#if EFI_PROD_CODE
for (size_t i = 0;i<efi::size(engineConfiguration->luaDigitalInputPins);i++) {
engine->luaDigitalInputState[i].state.update(getAuxDigital(i));
}
#endif // EFI_PROD_CODE
}
static void deInitAuxDigital() {
for (size_t i = 0;i<efi::size(activeConfiguration.luaDigitalInputPins);i++) {
brain_pin_markUnused(activeConfiguration.luaDigitalInputPins[i]);
}
}
static LuaOverrideSensor overrideRpm(SensorType::DashOverrideRpm, SensorType::Rpm);
2024-04-11 14:32:49 -07:00
static LuaOverrideSensor overrideVehicleSpeed(SensorType::DashOverrideVehicleSpeed, SensorType::VehicleSpeed);
static LuaOverrideSensor overrideClt(SensorType::DashOverrideClt, SensorType::Clt);
static LuaOverrideSensor overrideBatteryVoltage(SensorType::DashOverrideBatteryVoltage, SensorType::BatteryVoltage);
void initOverrideSensors() {
overrideRpm.Register();
2024-04-11 14:32:49 -07:00
overrideVehicleSpeed.Register();
overrideClt.Register();
overrideBatteryVoltage.Register();
}
// todo: closer alignment with 'stopSensors'
static void sensorStartUpOrReconfiguration(bool isFirstTime) {
initVbatt();
initMap();
initTps();
initFluidPressure();
initThermistors();
initVehicleSpeedSensor();
initTurbochargerSpeedSensor();
initAuxSensors();
initAuxSpeedSensors();
initInputShaftSpeedSensor();
#if EFI_TCU
initRangeSensors();
#endif
initFlexSensor(isFirstTime);
}
2023-11-09 08:58:27 -08:00
// one-time start-up
// see also 'reconfigureSensors'
void initNewSensors() {
#if EFI_PROD_CODE && EFI_CAN_SUPPORT
initCanSensors();
#endif
initOverrideSensors();
sensorStartUpOrReconfiguration(true);
// todo:
initLambda();
// todo: 'isFirstTime' approach for initEgt vs startEgt
initEgt();
initBaro();
#if !EFI_UNIT_TEST
initFuelLevel();
initMaf();
#endif
initOldAnalogInputs();
initAuxDigital();
// Init CLI functionality for sensors (mocking)
initSensorCli();
#if defined(HARDWARE_CI) && !defined(HW_PROTEUS)
chThdSleepMilliseconds(100);
if (Sensor::getOrZero(SensorType::BatteryVoltage) < 8) {
// Fake that we have battery voltage, some tests rely on it
Sensor::setMockValue(SensorType::BatteryVoltage, 10);
}
#endif
#if EFI_SIMULATOR
// Simulator gets battery voltage so it detects ignition-on
Sensor::setMockValue(SensorType::BatteryVoltage, 14);
#endif // EFI_SIMULATOR
}
void stopSensors() {
deInitAuxDigital();
deInitOldAnalogInputs();
deinitVbatt();
deinitTps();
2022-12-26 21:13:13 -08:00
deinitFluidPressure();
deinitThermistors();
deInitFlexSensor();
deinitAuxSensors();
Vehicle speed switching to frequency sensor #3106 (#3148) * Add vehicle_speed_converter.h with realization. * Add test_vehicle_speed_converter.cpp with realization. * Add test_vehicle_speed_converter.cpp to Makefile * Edit "VehicleSpeedSensor" from function pointer sensor to frequency sensor * Fix "VehicleSpeedSensor" init function args * Remove "VehicleSpeedConverter" empty constructor * Edit vehicle_speed_converter.h code style * Fix args and engine injection in "VehicleSpeedSensor" initialization procedure * Remove old "Vehicle Speed" module initialization * Revert "Remove old "Vehicle Speed" module initialization" This reverts commit 100b7caa8c615c20eed3d0c23a49824b4e556148. * Remove old "Vehicle Speed" module initialization pieces * Revert "Revert "Remove old "Vehicle Speed" module initialization"" This reverts commit f559a726f1f7f5a7acacd1b6517e571743e84327. * Debug changes * Revert "Debug changes" This reverts commit f7e2be1a8a46c6f3f93f14f002b2f2db539da8e2. * Another debug changes * More debug changes * And more debug changes. * Revert "Another debug changes" This reverts commit 914fbb1df42c37e4b5ecbb119bb45e7cffdf4064. * Revert "More debug changes" This reverts commit 0b2d96d461c3cd9b0f1ae97ed110b8effe6b0cc6. * Revert "And more debug changes." This reverts commit 06ae8daded0e6e1c025e4e5058b47283f4adfe92. * Add "Vehicle Speed" sensor reconfiguration * Revert "Add "Vehicle Speed" sensor reconfiguration" This reverts commit a089a6d563dd710004ce1a7da1744b0b4b90c3b1. * Revert "Revert "Add "Vehicle Speed" sensor reconfiguration"" This reverts commit 7b2bb8af4b3caa547236ca59e4c4bdb3ac1200cc. * Debug changes * Another debug changes * Fix another debug changes * Another debug changes 2 * Revert "Another debug changes 2" This reverts commit cfad55141eec125b2f1d35fb95d9f46b54f666b6. * Revert "Fix another debug changes" This reverts commit 9bc2c74a3be6fd896827ff48cf4580e7657529c0. * Revert "Another debug changes" This reverts commit 4337ed8cad8dff508d093944eb3f75e9a69e4a77. * Edit class FrequencySensor, make it more RAII * Debug changes * Debug changes 2 * Debug changes 3 * Revert "Debug changes 3" This reverts commit 52a7054c122b5157540fe45a055a84b57478d722. * Revert "Debug changes 2" This reverts commit dec79fb913344ccb4b8614910ad62e6c129a243e. * Revert "Debug changes" This reverts commit eb08d7a529b55ba397a7dd2c154b76eab42f212b. * Revert "Edit class FrequencySensor, make it more RAII" This reverts commit 3a0bb1d3f1dd23c1b04e4cc1c526cfdc67ae86a3. * Remove VSS pin definition in "Citroen Berlingo" engine config. This reverts commit 3a0bb1d3f1dd23c1b04e4cc1c526cfdc67ae86a3. * Remove VSS pin definition in "Dodge Neon" engine config Co-authored-by: alxrMironov <330OMcorporative>
2021-08-23 21:55:41 -07:00
deInitVehicleSpeedSensor();
deinitTurbochargerSpeedSensor();
deinitAuxSpeedSensors();
deinitMap();
deinitInputShaftSpeedSensor();
stopEgt();
}
void reconfigureSensors() {
sensorStartUpOrReconfiguration(false);
startEgt();
initOldAnalogInputs();
}
// Mocking/testing helpers
static void initSensorCli() {
addConsoleActionSS("set_sensor_mock", [](const char* typeName, const char* valueStr) {
SensorType type = findSensorTypeByName(typeName);
if (type == SensorType::Invalid) {
efiPrintf("Invalid sensor type specified: %s", typeName);
return;
}
float value = atoff(valueStr);
Sensor::setMockValue(type, value);
});
addConsoleAction("reset_sensor_mocks", Sensor::resetAllMocks);
addConsoleAction("show_sensors", Sensor::showAllSensorInfo);
addConsoleActionI("show_sensor",
[](int idx) {
Sensor::showInfo(static_cast<SensorType>(idx));
});
}