2019-09-21 12:33:13 -07:00
|
|
|
/**
|
|
|
|
* @file init_sensorss.cpp
|
|
|
|
*/
|
|
|
|
|
2021-07-25 22:05:17 -07:00
|
|
|
#include "pch.h"
|
|
|
|
|
2019-09-21 11:33:38 -07:00
|
|
|
#include "init.h"
|
2020-04-05 06:10:08 -07:00
|
|
|
#include "cli_registry.h"
|
2023-01-03 18:46:56 -08:00
|
|
|
#include "io_pins.h"
|
2019-09-21 11:33:38 -07:00
|
|
|
|
2021-04-18 17:02:32 -07:00
|
|
|
static void initSensorCli();
|
2020-02-08 01:22:23 -08:00
|
|
|
|
2021-08-24 13:41:16 -07:00
|
|
|
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?
|
|
|
|
*/
|
|
|
|
|
2021-08-24 13:41:16 -07:00
|
|
|
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;
|
|
|
|
}
|
2021-11-16 01:15:29 -08:00
|
|
|
efiSetPadMode(msg, pin, PAL_MODE_INPUT_ANALOG);
|
2023-05-31 20:11:49 -07:00
|
|
|
#endif // EFI_PROD_CODE && HAL_USE_ADC
|
2021-08-24 13:41:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-08-24 13:41:16 -07:00
|
|
|
brain_pin_e pin = getAdcChannelBrainPin(msg, channel);
|
2021-11-16 01:15:29 -08:00
|
|
|
efiSetPadUnused(pin);
|
2023-05-31 20:11:49 -07:00
|
|
|
#endif // EFI_PROD_CODE && HAL_USE_ADC
|
2021-08-24 13:41:16 -07:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
static void initOldAnalogInputs() {
|
2023-04-10 08:31:29 -07:00
|
|
|
if (isAdcChannelValid(engineConfiguration->afr.hwChannel) && engineConfiguration->enableAemXSeries) {
|
2023-08-20 19:23:44 -07:00
|
|
|
criticalError("Please pick either analog AFR or CAN AFR input not both.");
|
2023-04-10 08:31:29 -07:00
|
|
|
}
|
2021-08-24 13:41:16 -07:00
|
|
|
initIfValid("AFR", engineConfiguration->afr.hwChannel);
|
|
|
|
initIfValid("AUXF#1", engineConfiguration->auxFastSensor1_adcChannel);
|
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
static void deInitOldAnalogInputs() {
|
2021-08-24 13:41:16 -07:00
|
|
|
deInitIfValid("AFR", activeConfiguration.afr.hwChannel);
|
|
|
|
deInitIfValid("AUXF#1", activeConfiguration.auxFastSensor1_adcChannel);
|
|
|
|
}
|
|
|
|
|
2023-01-03 18:16:05 -08:00
|
|
|
static void initAuxDigital() {
|
2023-01-03 18:54:28 -08:00
|
|
|
#if EFI_PROD_CODE
|
2023-01-03 18:16:05 -08:00
|
|
|
for (size_t i = 0;i<efi::size(engineConfiguration->luaDigitalInputPins);i++) {
|
2023-07-09 11:41:57 -07:00
|
|
|
efiSetPadMode("Lua Digital", engineConfiguration->luaDigitalInputPins[i], engineConfiguration->luaDigitalInputPinModes[i]);
|
2023-01-03 18:16:05 -08:00
|
|
|
}
|
2023-01-03 18:54:28 -08:00
|
|
|
#endif // EFI_PROD_CODE
|
2023-01-03 18:16:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void deInitAuxDigital() {
|
|
|
|
for (size_t i = 0;i<efi::size(activeConfiguration.luaDigitalInputPins);i++) {
|
|
|
|
brain_pin_markUnused(activeConfiguration.luaDigitalInputPins[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-09 08:58:27 -08:00
|
|
|
// one-time start-up
|
|
|
|
// see also 'reconfigureSensors'
|
2021-11-16 01:15:29 -08:00
|
|
|
void initNewSensors() {
|
2022-07-03 05:25:24 -07:00
|
|
|
#if EFI_PROD_CODE && EFI_CAN_SUPPORT
|
2020-03-31 20:21:05 -07:00
|
|
|
initCanSensors();
|
|
|
|
#endif
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
initVbatt();
|
|
|
|
initMap();
|
|
|
|
initTps();
|
2022-12-26 21:13:13 -08:00
|
|
|
initFluidPressure();
|
2021-11-16 01:15:29 -08:00
|
|
|
initThermistors();
|
|
|
|
initLambda();
|
2023-11-09 08:58:27 -08:00
|
|
|
initFlexSensor(true);
|
2021-11-16 01:15:29 -08:00
|
|
|
initBaro();
|
|
|
|
initAuxSensors();
|
|
|
|
initVehicleSpeedSensor();
|
|
|
|
initTurbochargerSpeedSensor();
|
2022-03-28 23:35:27 -07:00
|
|
|
initAuxSpeedSensors();
|
2022-04-21 06:07:52 -07:00
|
|
|
initInputShaftSpeedSensor();
|
2019-09-21 11:33:38 -07:00
|
|
|
|
2021-03-19 05:39:08 -07:00
|
|
|
#if !EFI_UNIT_TEST
|
2021-11-16 01:15:29 -08:00
|
|
|
initFuelLevel();
|
|
|
|
initMaf();
|
2021-03-19 05:39:08 -07:00
|
|
|
#endif
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
initOldAnalogInputs();
|
2023-01-03 18:16:05 -08:00
|
|
|
initAuxDigital();
|
2021-08-24 13:41:16 -07:00
|
|
|
|
2019-09-21 11:33:38 -07:00
|
|
|
// Init CLI functionality for sensors (mocking)
|
2021-04-18 17:02:32 -07:00
|
|
|
initSensorCli();
|
2022-10-04 17:36:03 -07:00
|
|
|
|
2022-10-17 12:36:02 -07:00
|
|
|
#if defined(HARDWARE_CI) && !defined(HW_PROTEUS)
|
|
|
|
chThdSleepMilliseconds(100);
|
2022-10-04 17:36:03 -07:00
|
|
|
|
|
|
|
if (Sensor::getOrZero(SensorType::BatteryVoltage) < 8) {
|
|
|
|
// Fake that we have battery voltage, some tests rely on it
|
|
|
|
Sensor::setMockValue(SensorType::BatteryVoltage, 10);
|
|
|
|
}
|
|
|
|
#endif
|
2023-01-10 16:07:44 -08:00
|
|
|
|
|
|
|
#if EFI_SIMULATOR
|
|
|
|
// Simulator gets battery voltage so it detects ignition-on
|
|
|
|
Sensor::setMockValue(SensorType::BatteryVoltage, 14);
|
|
|
|
#endif // EFI_SIMULATOR
|
2019-09-21 11:33:38 -07:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void stopSensors() {
|
2023-01-03 18:16:05 -08:00
|
|
|
deInitAuxDigital();
|
2021-11-16 01:15:29 -08:00
|
|
|
deInitOldAnalogInputs();
|
2021-08-24 13:41:16 -07:00
|
|
|
|
|
|
|
deinitTps();
|
2022-12-26 21:13:13 -08:00
|
|
|
deinitFluidPressure();
|
2021-08-24 13:41:16 -07:00
|
|
|
deinitVbatt();
|
|
|
|
deinitThermistors();
|
2021-08-20 23:02:05 -07:00
|
|
|
deInitFlexSensor();
|
2021-08-23 21:55:41 -07:00
|
|
|
deInitVehicleSpeedSensor();
|
2021-09-19 17:50:11 -07:00
|
|
|
deinitTurbochargerSpeedSensor();
|
2022-03-28 23:35:27 -07:00
|
|
|
deinitAuxSpeedSensors();
|
2021-10-04 14:18:08 -07:00
|
|
|
deinitMap();
|
2022-04-21 06:07:52 -07:00
|
|
|
deinitInputShaftSpeedSensor();
|
2021-08-24 13:41:16 -07:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void reconfigureSensors() {
|
|
|
|
initMap();
|
|
|
|
initTps();
|
2022-12-26 21:13:13 -08:00
|
|
|
initFluidPressure();
|
2021-11-16 01:15:29 -08:00
|
|
|
initVbatt();
|
|
|
|
initThermistors();
|
2023-11-09 08:58:27 -08:00
|
|
|
initFlexSensor(false);
|
2021-11-16 01:15:29 -08:00
|
|
|
initVehicleSpeedSensor();
|
|
|
|
initTurbochargerSpeedSensor();
|
2022-04-21 06:07:52 -07:00
|
|
|
initInputShaftSpeedSensor();
|
2021-11-16 01:15:29 -08:00
|
|
|
|
|
|
|
initOldAnalogInputs();
|
2020-02-08 01:22:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mocking/testing helpers
|
2021-04-18 17:02:32 -07:00
|
|
|
static void initSensorCli() {
|
2023-07-31 22:04:06 -07:00
|
|
|
addConsoleActionIF(CMD_SET_SENSOR_MOCK, Sensor::setMockValue);
|
|
|
|
addConsoleAction(CMD_RESET_SENSOR_MOCKS, Sensor::resetAllMocks);
|
2021-04-18 17:02:32 -07:00
|
|
|
addConsoleAction("show_sensors", Sensor::showAllSensorInfo);
|
|
|
|
addConsoleActionI("show_sensor",
|
2020-03-30 15:29:42 -07:00
|
|
|
[](int idx) {
|
2021-04-18 17:02:32 -07:00
|
|
|
Sensor::showInfo(static_cast<SensorType>(idx));
|
2020-03-30 15:29:42 -07:00
|
|
|
});
|
2019-09-21 11:33:38 -07:00
|
|
|
}
|