From 1ad1d0cef7f86dba72307e7540deb1ff4186c65e Mon Sep 17 00:00:00 2001 From: Marin Doetterer Date: Wed, 4 Feb 2026 15:03:15 +0100 Subject: [PATCH] fix(drivers/differential_pressure/auav): validate full status byte Compare the complete status byte against the value the datasheet specifies for a completed reading without error (0x40 absolute, 0x50 differential). Signed-off-by: Marin Doetterer --- src/drivers/differential_pressure/auav/AUAV.cpp | 13 +++++++------ src/drivers/differential_pressure/auav/AUAV.hpp | 14 +++++++++++++- .../differential_pressure/auav/AUAV_Absolute.cpp | 2 +- .../differential_pressure/auav/AUAV_Absolute.hpp | 3 +++ .../auav/AUAV_Differential.cpp | 2 +- .../auav/AUAV_Differential.hpp | 2 ++ 6 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/drivers/differential_pressure/auav/AUAV.cpp b/src/drivers/differential_pressure/auav/AUAV.cpp index fb891481330..0fb2b7b2484 100644 --- a/src/drivers/differential_pressure/auav/AUAV.cpp +++ b/src/drivers/differential_pressure/auav/AUAV.cpp @@ -59,11 +59,12 @@ I2CSPIDriverBase *AUAV::instantiate(const I2CSPIDriverConfig &config, const int return instance; } -AUAV::AUAV(const I2CSPIDriverConfig &config) : +AUAV::AUAV(const I2CSPIDriverConfig &config, const uint8_t status_byte_expected) : I2C(config), I2CSPIDriver(config), _sample_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": read")), - _comms_errors(perf_alloc(PC_COUNT, MODULE_NAME": comms errors")) + _comms_errors(perf_alloc(PC_COUNT, MODULE_NAME": comms errors")), + _status_byte(status_byte_expected) { I2C::_retries = 5; } @@ -124,8 +125,8 @@ int AUAV::probe() res_data = 0; int status = transfer(nullptr, 0, &res_data, 1); - /* Check that the sensor is active. Reported in bit 6 of the status byte */ - if (status == PX4_OK && (res_data & 0x40)) { + /* Check that the sensor is powered on, error free and of the expected type (mode bits) */ + if (status == PX4_OK && res_data == _status_byte) { return PX4_OK; } @@ -201,8 +202,8 @@ void AUAV::handle_state_gather_measurement() uint8_t res_data[7]; int status = transfer(nullptr, 0, res_data, sizeof(res_data)); - /* Continue processing if the transfer was a success and bit 5 of the status is set to 0 (indicating the sensor is finished) */ - if (status == PX4_OK && (res_data[0] & 0x20) == 0) { + /* Continue processing if the transfer was a success and the status is as expected (powered on, not busy, no errors) */ + if (status == PX4_OK && res_data[0] == _status_byte) { const hrt_abstime timestamp_sample = hrt_absolute_time(); const uint32_t pressure_raw = (res_data[1] << 16) | (res_data[2] << 8) | (res_data[3]); diff --git a/src/drivers/differential_pressure/auav/AUAV.hpp b/src/drivers/differential_pressure/auav/AUAV.hpp index 7011e8b7480..4f28553a49f 100644 --- a/src/drivers/differential_pressure/auav/AUAV.hpp +++ b/src/drivers/differential_pressure/auav/AUAV.hpp @@ -49,7 +49,7 @@ static constexpr uint32_t I2C_SPEED = 100 * 1000; // 100 kHz I2C serial interfac class AUAV : public device::I2C, public I2CSPIDriver { public: - explicit AUAV(const I2CSPIDriverConfig &config); + explicit AUAV(const I2CSPIDriverConfig &config, const uint8_t status_byte_expected); virtual ~AUAV(); static I2CSPIDriverBase *instantiate(const I2CSPIDriverConfig &config, const int runtime_instance); @@ -126,6 +126,18 @@ protected: perf_counter_t _sample_perf; perf_counter_t _comms_errors; + /* + * Expected status byte of a healthy, idle sensor. Must match exactly. + * bit 0: ALU error (0 = ok) + * bit 1: reserved (0) + * bit 2: memory error (0 = ok) + * bit 3-4: mode (differential: 0x10, absolute: 0x00) + * bit 5: busy (0 = measurement ready) + * bit 6: power on (1) + * bit 7: reserved (0) + */ + const uint8_t _status_byte; + private: int probe() override; }; diff --git a/src/drivers/differential_pressure/auav/AUAV_Absolute.cpp b/src/drivers/differential_pressure/auav/AUAV_Absolute.cpp index 502f4989880..f660c5b9849 100644 --- a/src/drivers/differential_pressure/auav/AUAV_Absolute.cpp +++ b/src/drivers/differential_pressure/auav/AUAV_Absolute.cpp @@ -34,7 +34,7 @@ #include "AUAV_Absolute.hpp" AUAV_Absolute::AUAV_Absolute(const I2CSPIDriverConfig &config) : - AUAV(config), + AUAV(config, STATUS_BYTE_ABS), _baro_external(config.external) { } diff --git a/src/drivers/differential_pressure/auav/AUAV_Absolute.hpp b/src/drivers/differential_pressure/auav/AUAV_Absolute.hpp index cb32d18a2eb..10391d026fd 100644 --- a/src/drivers/differential_pressure/auav/AUAV_Absolute.hpp +++ b/src/drivers/differential_pressure/auav/AUAV_Absolute.hpp @@ -57,6 +57,9 @@ static_assert(ABS_CONVERSION_INTERVAL >= 7000, "Conversion interval is too fast" /* Conversions */ static constexpr float MBAR_TO_PA = 100.0f; +/* Status byte of a completed reading without error */ +static constexpr uint8_t STATUS_BYTE_ABS = 0x40; + class AUAV_Absolute : public AUAV { public: diff --git a/src/drivers/differential_pressure/auav/AUAV_Differential.cpp b/src/drivers/differential_pressure/auav/AUAV_Differential.cpp index e6351f78f9c..b4ae2dcc928 100644 --- a/src/drivers/differential_pressure/auav/AUAV_Differential.cpp +++ b/src/drivers/differential_pressure/auav/AUAV_Differential.cpp @@ -36,7 +36,7 @@ #include AUAV_Differential::AUAV_Differential(const I2CSPIDriverConfig &config) : - AUAV(config) + AUAV(config, STATUS_BYTE_DIFF) { /* Initialize cal_range from parameter as fallback value */ int32_t hw_model = 0; diff --git a/src/drivers/differential_pressure/auav/AUAV_Differential.hpp b/src/drivers/differential_pressure/auav/AUAV_Differential.hpp index a50de57a943..7ed3b30773c 100644 --- a/src/drivers/differential_pressure/auav/AUAV_Differential.hpp +++ b/src/drivers/differential_pressure/auav/AUAV_Differential.hpp @@ -55,6 +55,8 @@ static constexpr int64_t DIFF_CONVERSION_INTERVAL = (1000000 / DIFF_MEAS_RATE); /* reading too fast can yield all zero data -> incorrect sensor reading */ static_assert(DIFF_CONVERSION_INTERVAL >= 7000, "Conversion interval is too fast"); +static constexpr uint8_t STATUS_BYTE_DIFF = 0x50; + /* Valid AUAV types */ static constexpr int32_t AUAV_LD_05 = 5; static constexpr int32_t AUAV_LD_10 = 10;