diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 5b34d5fc76..869353c639 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -236,6 +236,11 @@ void Engine::periodicSlowCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { slowCallBackWasInvoked = true; +#if HW_PROTEUS + void baroUpdate(); + baroUpdate(); +#endif + #if ANALOG_HW_CHECK_MODE efiAssertVoid(OBD_PCM_Processor_Fault, isAdcChannelValid(CONFIG(clt).adcChannel), "No CLT setting"); efitimesec_t secondsNow = getTimeNowSeconds(); diff --git a/firmware/controllers/sensors/Lps25Sensor.cpp b/firmware/controllers/sensors/Lps25Sensor.cpp new file mode 100644 index 0000000000..6b3fd73c20 --- /dev/null +++ b/firmware/controllers/sensors/Lps25Sensor.cpp @@ -0,0 +1,18 @@ +#include "Lps25Sensor.h" +#include "global.h" + +Lps25Sensor::Lps25Sensor(Lps25& sensor) + : StoredValueSensor(SensorType::BarometricPressure, MS2NT(1000)) + , m_sensor(&sensor) +{ +} + +void Lps25Sensor::update() { + auto result = m_sensor->readPressureKpa(); + + if (result) { + setValidValue(result.Value, getTimeNowNt()); + } else { + invalidate(); + } +} diff --git a/firmware/controllers/sensors/Lps25Sensor.h b/firmware/controllers/sensors/Lps25Sensor.h new file mode 100644 index 0000000000..bdb9c83047 --- /dev/null +++ b/firmware/controllers/sensors/Lps25Sensor.h @@ -0,0 +1,15 @@ +#pragma once + +#include "stored_value_sensor.h" +#include "lps25.h" + +class Lps25Sensor : public StoredValueSensor { +public: + explicit Lps25Sensor(Lps25& sensor); + void update(); + + void showInfo(Logging* logger, const char* sensorName) const override; + +private: + Lps25* m_sensor; +}; diff --git a/firmware/controllers/sensors/sensor_info_printing.cpp b/firmware/controllers/sensors/sensor_info_printing.cpp index 24e9798022..be3b69ef58 100644 --- a/firmware/controllers/sensors/sensor_info_printing.cpp +++ b/firmware/controllers/sensors/sensor_info_printing.cpp @@ -3,6 +3,7 @@ #include "functional_sensor.h" #include "redundant_sensor.h" #include "rpm_calculator.h" +#include "Lps25Sensor.h" #include "linear_func.h" #include "resistance_func.h" #include "thermistor_func.h" @@ -46,6 +47,10 @@ void RpmCalculator::showInfo(Logging* logger, const char* /*sensorName*/) const ); } +void Lps25Sensor::showInfo(Logging* logger, const char* sensorName) const { + // TODO +} + void LinearFunc::showInfo(Logging* logger, float testRawValue) const { scheduleMsg(logger, " Linear function slope: %.2f offset: %.2f min: %.1f max: %.1f", m_a, m_b, m_minOutput, m_maxOutput); const auto [valid, value] = convert(testRawValue); diff --git a/firmware/controllers/sensors/sensors.mk b/firmware/controllers/sensors/sensors.mk index e04a9923d5..2c6fb02c47 100644 --- a/firmware/controllers/sensors/sensors.mk +++ b/firmware/controllers/sensors/sensors.mk @@ -16,6 +16,7 @@ CONTROLLERS_SENSORS_SRC_CPP = $(PROJECT_DIR)/controllers/sensors/thermistors.cp $(PROJECT_DIR)/controllers/sensors/AemXSeriesLambda.cpp \ $(PROJECT_DIR)/cotnrollers/sensors/flex_sensor.cpp \ $(PROJECT_DIR)/controllers/sensors/software_knock.cpp \ + $(PROJECT_DIR)/controllers/sensors/Lps25Sensor.cpp \ $(PROJECT_DIR)/controllers/sensors/converters/linear_func.cpp \ $(PROJECT_DIR)/controllers/sensors/converters/resistance_func.cpp \ $(PROJECT_DIR)/controllers/sensors/converters/thermistor_func.cpp diff --git a/firmware/hw_layer/i2c_bb.cpp b/firmware/hw_layer/i2c_bb.cpp index 1ea1ba7350..96c8a35b57 100644 --- a/firmware/hw_layer/i2c_bb.cpp +++ b/firmware/hw_layer/i2c_bb.cpp @@ -8,28 +8,35 @@ #include "i2c_bb.h" -#if EFI_PROD_CODE - #include "io_pins.h" #include "efi_gpio.h" void BitbangI2c::sda_high() { +#if EFI_PROD_CODE palSetPad(m_sdaPort, m_sdaPin); +#endif } void BitbangI2c::sda_low() { +#if EFI_PROD_CODE palClearPad(m_sdaPort, m_sdaPin); +#endif } void BitbangI2c::scl_high() { +#if EFI_PROD_CODE palSetPad(m_sclPort, m_sclPin); +#endif } void BitbangI2c::scl_low() { +#if EFI_PROD_CODE palClearPad(m_sclPort, m_sclPin); +#endif } void BitbangI2c::init(brain_pin_e scl, brain_pin_e sda) { +#if EFI_PROD_CODE if (m_sdaPort) return; efiSetPadMode("i2c", scl, PAL_MODE_OUTPUT_OPENDRAIN); //PAL_STM32_OTYPE_OPENDRAIN @@ -40,6 +47,7 @@ void BitbangI2c::init(brain_pin_e scl, brain_pin_e sda) { m_sdaPort = getHwPort("i2c", sda); m_sdaPin = getHwPin("i2c", sda); +#endif // Both lines idle high scl_high(); @@ -99,8 +107,12 @@ bool BitbangI2c::readBit() { waitQuarterBit(); waitQuarterBit(); +#if EFI_PROD_CODE // Read just before we set the clock low (ie, as late as possible) bool val = palReadPad(m_sdaPort, m_sdaPin); +#else + bool val = false; +#endif scl_low(); waitQuarterBit(); @@ -209,5 +221,3 @@ void BitbangI2c::writeRegister(uint8_t addr, uint8_t reg, uint8_t val) { write(addr, buf, 2); } - -#endif // EFI_PROD_CODE diff --git a/firmware/hw_layer/i2c_bb.h b/firmware/hw_layer/i2c_bb.h index 1f30af8b7a..4c25c39e29 100644 --- a/firmware/hw_layer/i2c_bb.h +++ b/firmware/hw_layer/i2c_bb.h @@ -9,8 +9,9 @@ #pragma once #if EFI_PROD_CODE - #include "hal.h" +#endif + #include "rusefi_hw_enums.h" #include #include @@ -53,10 +54,10 @@ private: // Wait for 1/4 of a bit time void waitQuarterBit(); +#if EFI_PROD_CODE ioportid_t m_sclPort = 0; ioportmask_t m_sclPin = 0; ioportid_t m_sdaPort = 0; ioportmask_t m_sdaPin = 0; +#endif }; - -#endif // EFI_PROD_CODE diff --git a/firmware/hw_layer/sensors/lps25.cpp b/firmware/hw_layer/sensors/lps25.cpp index 678ab3ac23..a65d4b5e57 100644 --- a/firmware/hw_layer/sensors/lps25.cpp +++ b/firmware/hw_layer/sensors/lps25.cpp @@ -6,8 +6,6 @@ * @author Matthew Kennedy, (c) 2020 */ -#if EFI_PROD_CODE - #include "lps25.h" static constexpr uint8_t addr = 0x5C; @@ -51,6 +49,10 @@ bool Lps25::init(brain_pin_e scl, brain_pin_e sda) { } expected Lps25::readPressureKpa() { + if (!m_hasInit) { + return unexpected; + } + // First read the status reg to check if there are data available uint8_t sr = m_i2c.readRegister(addr, REG_Status); @@ -99,5 +101,3 @@ expected Lps25::readPressureKpa() { return kilopascal; } - -#endif // EFI_PROD_CODE diff --git a/firmware/hw_layer/sensors/lps25.h b/firmware/hw_layer/sensors/lps25.h index 34efbe0542..d79f2e8436 100644 --- a/firmware/hw_layer/sensors/lps25.h +++ b/firmware/hw_layer/sensors/lps25.h @@ -8,8 +8,6 @@ #pragma once -#if EFI_PROD_CODE - #include "i2c_bb.h" #include "expected.h" @@ -25,5 +23,3 @@ private: BitbangI2c m_i2c; bool m_hasInit = false; }; - -#endif // EFI_PROD_CODE diff --git a/firmware/init/init.h b/firmware/init/init.h index 393373a57a..12dccfc524 100644 --- a/firmware/init/init.h +++ b/firmware/init/init.h @@ -27,6 +27,7 @@ void initThermistors(DECLARE_CONFIG_PARAMETER_SIGNATURE); void initCanSensors(); void initLambda(DECLARE_ENGINE_PARAMETER_SIGNATURE); void initFlexSensor(DECLARE_CONFIG_PARAMETER_SIGNATURE); +void initBaro(); // Sensor reconfiguration void reconfigureVbatt(DECLARE_CONFIG_PARAMETER_SIGNATURE); diff --git a/firmware/init/init.mk b/firmware/init/init.mk index b1142b3df8..c42e681851 100644 --- a/firmware/init/init.mk +++ b/firmware/init/init.mk @@ -8,3 +8,4 @@ INIT_SRC_CPP = $(PROJECT_DIR)/init/sensor/init_sensors.cpp \ $(PROJECT_DIR)/init/sensor/init_map.cpp \ $(PROJECT_DIR)/init/sensor/init_flex.cpp \ $(PROJECT_DIR)/init/sensor/init_vbatt.cpp \ + $(PROJECT_DIR)/init/sensor/init_baro.cpp \ diff --git a/firmware/init/sensor/init_baro.cpp b/firmware/init/sensor/init_baro.cpp new file mode 100644 index 0000000000..7af9a40501 --- /dev/null +++ b/firmware/init/sensor/init_baro.cpp @@ -0,0 +1,27 @@ +#include "engine.h" +#include "Lps25Sensor.h" + +#include "rusefi_hw_enums.h" + +static Lps25 device; +static Lps25Sensor sensor(device); + +void initBaro() { + // If there's already an external (analog) baro sensor configured, + // don't configure the internal one. + if (Sensor::hasSensor(SensorType::BarometricPressure)) { + return; + } + +#if HW_PROTEUS + if (device.init(GPIOB_10, GPIOB_11)) { + sensor.Register(); + } +#endif +} + +void baroUpdate() { +#if EFI_PROD_CODE + sensor.update(); +#endif +} diff --git a/firmware/init/sensor/init_sensors.cpp b/firmware/init/sensor/init_sensors.cpp index 8ac3360aae..1bf791b6fd 100644 --- a/firmware/init/sensor/init_sensors.cpp +++ b/firmware/init/sensor/init_sensors.cpp @@ -20,6 +20,7 @@ void initNewSensors(Logging* logger DECLARE_ENGINE_PARAMETER_SUFFIX) { initThermistors(PASS_CONFIG_PARAMETER_SIGNATURE); initLambda(PASS_ENGINE_PARAMETER_SIGNATURE); initFlexSensor(PASS_CONFIG_PARAMETER_SIGNATURE); + initBaro(); // Init CLI functionality for sensors (mocking) initSensorCli(logger);