diff --git a/firmware/CHANGELOG.md b/firmware/CHANGELOG.md index 7c0be03639..c7e7eba4aa 100644 --- a/firmware/CHANGELOG.md +++ b/firmware/CHANGELOG.md @@ -26,7 +26,8 @@ Release template (copy/paste this for new release): All notable user-facing or behavior-altering changes will be documented in this file. ### Added -- Use board-specific output and digital input pin names in error messages #3886 + - Use board-specific output and digital input pin names in error messages #3886 + - Support LPS22 in addition to LPS25 baro sensors #3900 ### Fixed - SD card logging with SDIO hardware #3873 diff --git a/firmware/hw_layer/sensors/lps25.cpp b/firmware/hw_layer/sensors/lps25.cpp index f15f56731c..e57e20e8ac 100644 --- a/firmware/hw_layer/sensors/lps25.cpp +++ b/firmware/hw_layer/sensors/lps25.cpp @@ -9,7 +9,8 @@ #include "lps25.h" static constexpr uint8_t addr = 0x5C; -static constexpr uint8_t expectedWhoAmI = 0xBD; +static constexpr uint8_t expectedWhoAmILps22 = 0xB1; +static constexpr uint8_t expectedWhoAmILps25 = 0xBD; // Control register 1 #define LPS_CR1_PD (1 << 7) @@ -20,7 +21,10 @@ static constexpr uint8_t expectedWhoAmI = 0xBD; #define LPS_SR_P_DA (1 << 1) // Pressure data available #define REG_WhoAmI 0x0F -#define REG_Cr1 0x20 + +// register address different on LPS22 vs LPS25 +#define REG_Cr1_Lps22 0x10 +#define REG_Cr1_Lps25 0x20 #define REG_Status 0x27 #define REG_PressureOutXl 0x28 #define REG_PressureOutL 0x29 @@ -33,18 +37,33 @@ bool Lps25::init(brain_pin_e scl, brain_pin_e sda) { // Read ident register auto whoAmI = m_i2c.readRegister(addr, REG_WhoAmI); - if (whoAmI != expectedWhoAmI) { + + switch (whoAmI) + { + case expectedWhoAmILps22: + m_type = Type::Lps22; + break; + case expectedWhoAmILps25: + m_type = Type::Lps25; + break; + default: + // chip not detected return false; } - // Set the control registers - m_i2c.writeRegister(addr, REG_Cr1, - LPS_CR1_PD | // Set to active mode + uint8_t cr1 = LPS_CR1_ODR_25hz | // 25hz update rate - // TODO: should bdu be set? - LPS_CR1_BDU // Output registers update only when read - ); + LPS_CR1_BDU; // Output registers update only when read + + if (m_type == Type::Lps25) { + // Set to active mode + // this bit must be 0 on LPS22 + cr1 |= LPS_CR1_PD; + } + + // Set the control registers + m_i2c.writeRegister(addr, regCr1(), cr1); m_hasInit = true; return true; @@ -103,3 +122,14 @@ expected Lps25::readPressureKpa() { return kilopascal; } + +uint8_t Lps25::regCr1() const { + switch (m_type) + { + case Type::Lps22: + return REG_Cr1_Lps22; + case Type::Lps25: + default: + return REG_Cr1_Lps25; + } +} diff --git a/firmware/hw_layer/sensors/lps25.h b/firmware/hw_layer/sensors/lps25.h index bd8b6a8073..e458a3e49d 100644 --- a/firmware/hw_layer/sensors/lps25.h +++ b/firmware/hw_layer/sensors/lps25.h @@ -1,6 +1,6 @@ /** * @file lps25.h - * @brief Driver for the ST LPS25HB pressure sensor + * @brief Driver for the ST LPS22HB and LPS25HB pressure sensor * * @date February 6, 2020 * @author Matthew Kennedy, (c) 2020 @@ -25,5 +25,14 @@ public: private: BitbangI2c m_i2c; + enum class Type { + Lps22, + Lps25, + }; + + Type m_type; + bool m_hasInit = false; + + uint8_t regCr1() const; }; diff --git a/firmware/tunerstudio/rusefi.input b/firmware/tunerstudio/rusefi.input index 2d4f5877a1..6b077b8af3 100644 --- a/firmware/tunerstudio/rusefi.input +++ b/firmware/tunerstudio/rusefi.input @@ -2629,8 +2629,8 @@ cmd_set_engine_type_default = "@@TS_IO_TEST_COMMAND_char@@\x00\x31\x00\x00" field = "Low Value", baroSensor_lowValue, {baroSensor_hwChannel != @@ADC_CHANNEL_NONE@@ || lps25BaroSensorScl != 0 } field = "High Value", baroSensor_highValue, {baroSensor_hwChannel != @@ADC_CHANNEL_NONE@@ || lps25BaroSensorScl != 0 } field = "Type", baroSensor_type, {baroSensor_hwChannel != @@ADC_CHANNEL_NONE@@ || lps25BaroSensorScl != 0 } - field = "LPS25 Baro SCL", lps25BaroSensorScl, { baroSensor_hwChannel == @@ADC_CHANNEL_NONE@@} - field = "LPS25 Baro SDA", lps25BaroSensorSda, { baroSensor_hwChannel == @@ADC_CHANNEL_NONE@@} + field = "LPS2x Baro SCL", lps25BaroSensorScl, { baroSensor_hwChannel == @@ADC_CHANNEL_NONE@@} + field = "LPS2x Baro SDA", lps25BaroSensorSda, { baroSensor_hwChannel == @@ADC_CHANNEL_NONE@@} dialog = mapCurves, "MAP sampling", yAxis field = "isMapAveragingEnabled", isMapAveragingEnabled