diff --git a/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld b/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld index dfca0c6988b..02af58c14ee 100644 --- a/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld @@ -303,7 +303,7 @@ *(.text._ZN9LockGuardD1Ev) /* itcm-check-ignore */ *(.text._ZN4EKF213PublishStatesERKy) *(.text._ZN3ADC3RunEv) -*(.text._ZN6BMP38815compensate_dataEhPK16bmp3_uncomp_dataP9bmp3_data) +*(.text._ZNK6BMP38818CompensatePressureEmx) *(.text._ZN3Ekf20controlFakePosFusionEv) *(.text._ZN11calibration9Gyroscope13set_device_idEm) *(.text._ZN24MavlinkStreamOrbitStatus8get_sizeEv) @@ -500,7 +500,7 @@ *(.text.imxrt_dmach_getcount) *(.text.sem_clockwait) *(.text.inet_poll) -*(.text._ZN6BMP3887collectEv) +*(.text._ZN6BMP3887CollectEv) *(.text._ZNK15PositionControl24getLocalPositionSetpointER33vehicle_local_position_setpoint_s) *(.text._ZN3Ekf16controlGpsFusionERKN9estimator9imuSampleE) *(.text._ZN23MavlinkStreamRCChannels8get_sizeEv) @@ -527,7 +527,7 @@ *(.text._ZN21MavlinkStreamAltitude4sendEv) *(.text._ZN7sensors14VehicleAirData12UpdateStatusEv) *(.text.imxrt_padmux_map) -*(.text._ZN6BMP38815get_sensor_dataEhP9bmp3_data) +*(.text._ZNK6BMP38821CompensateTemperatureEmRx) *(.text._ZN18MavlinkRateLimiter5checkERKy) *(.text._ZThn24_N26MulticopterAttitudeControl3RunEv) *(.text.imxrt_periphclk_configure) @@ -603,7 +603,7 @@ *(.text._ZThn8_N3ADC3RunEv) *(.text._ZN11StickTiltXYC1EP12ModuleParams) *(.text._ZN12SafetyButton3RunEv) -*(.text._ZN6BMP38811set_op_modeEh) +*(.text._ZN6BMP38813RegisterWriteEN12Bosch_BMP3888RegisterEh) *(.text._ZN3GPS8callbackE15GPSCallbackTypePviS1_) *(.text._ZN13AnalogBattery19get_current_channelEv) *(.text._ZN15EstimatorChecks20checkEstimatorStatusERK7ContextR6ReportRK18estimator_status_s8NavModes) @@ -661,7 +661,7 @@ *(.text._ZN20ImuConsistencyChecks14checkAndReportERK7ContextR6Report) *(.text._ZN28MavlinkStreamGpsGlobalOrigin8get_sizeEv) *(.text.MEM_DataCopy2_1) -*(.text._ZN6BMP3887measureEv) +*(.text._ZN6BMP3887MeasureEv) *(.text._ZN4EKF217PublishRngHgtBiasERKy) *(.text._ZN36MavlinkStreamPositionTargetGlobalInt8get_sizeEv) *(.text._ZN28MavlinkStreamEstimatorStatus8get_sizeEv) @@ -677,7 +677,7 @@ *(.text._ZN32MavlinkStreamNavControllerOutput8get_sizeEv) *(.text._ZN6matrix8wrap_2piIfEET_S1_) *(.text._ZN4uORB7Manager30orb_remove_internal_subscriberEPv) -*(.text._ZN10BMP388_I2C7get_regEhPh) +*(.text._ZN6BMP38812RegisterReadEN12Bosch_BMP3888RegisterEPvj) *(.text._ZN4math17WelfordMeanVectorIfLj3EE5resetEv) *(.text._ZN27MavlinkStreamScaledPressure8get_sizeEv) *(.text._ZN3RTL17parameters_updateEv) diff --git a/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld b/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld index dfca0c6988b..02af58c14ee 100644 --- a/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld @@ -303,7 +303,7 @@ *(.text._ZN9LockGuardD1Ev) /* itcm-check-ignore */ *(.text._ZN4EKF213PublishStatesERKy) *(.text._ZN3ADC3RunEv) -*(.text._ZN6BMP38815compensate_dataEhPK16bmp3_uncomp_dataP9bmp3_data) +*(.text._ZNK6BMP38818CompensatePressureEmx) *(.text._ZN3Ekf20controlFakePosFusionEv) *(.text._ZN11calibration9Gyroscope13set_device_idEm) *(.text._ZN24MavlinkStreamOrbitStatus8get_sizeEv) @@ -500,7 +500,7 @@ *(.text.imxrt_dmach_getcount) *(.text.sem_clockwait) *(.text.inet_poll) -*(.text._ZN6BMP3887collectEv) +*(.text._ZN6BMP3887CollectEv) *(.text._ZNK15PositionControl24getLocalPositionSetpointER33vehicle_local_position_setpoint_s) *(.text._ZN3Ekf16controlGpsFusionERKN9estimator9imuSampleE) *(.text._ZN23MavlinkStreamRCChannels8get_sizeEv) @@ -527,7 +527,7 @@ *(.text._ZN21MavlinkStreamAltitude4sendEv) *(.text._ZN7sensors14VehicleAirData12UpdateStatusEv) *(.text.imxrt_padmux_map) -*(.text._ZN6BMP38815get_sensor_dataEhP9bmp3_data) +*(.text._ZNK6BMP38821CompensateTemperatureEmRx) *(.text._ZN18MavlinkRateLimiter5checkERKy) *(.text._ZThn24_N26MulticopterAttitudeControl3RunEv) *(.text.imxrt_periphclk_configure) @@ -603,7 +603,7 @@ *(.text._ZThn8_N3ADC3RunEv) *(.text._ZN11StickTiltXYC1EP12ModuleParams) *(.text._ZN12SafetyButton3RunEv) -*(.text._ZN6BMP38811set_op_modeEh) +*(.text._ZN6BMP38813RegisterWriteEN12Bosch_BMP3888RegisterEh) *(.text._ZN3GPS8callbackE15GPSCallbackTypePviS1_) *(.text._ZN13AnalogBattery19get_current_channelEv) *(.text._ZN15EstimatorChecks20checkEstimatorStatusERK7ContextR6ReportRK18estimator_status_s8NavModes) @@ -661,7 +661,7 @@ *(.text._ZN20ImuConsistencyChecks14checkAndReportERK7ContextR6Report) *(.text._ZN28MavlinkStreamGpsGlobalOrigin8get_sizeEv) *(.text.MEM_DataCopy2_1) -*(.text._ZN6BMP3887measureEv) +*(.text._ZN6BMP3887MeasureEv) *(.text._ZN4EKF217PublishRngHgtBiasERKy) *(.text._ZN36MavlinkStreamPositionTargetGlobalInt8get_sizeEv) *(.text._ZN28MavlinkStreamEstimatorStatus8get_sizeEv) @@ -677,7 +677,7 @@ *(.text._ZN32MavlinkStreamNavControllerOutput8get_sizeEv) *(.text._ZN6matrix8wrap_2piIfEET_S1_) *(.text._ZN4uORB7Manager30orb_remove_internal_subscriberEPv) -*(.text._ZN10BMP388_I2C7get_regEhPh) +*(.text._ZN6BMP38812RegisterReadEN12Bosch_BMP3888RegisterEPvj) *(.text._ZN4math17WelfordMeanVectorIfLj3EE5resetEv) *(.text._ZN27MavlinkStreamScaledPressure8get_sizeEv) *(.text._ZN3RTL17parameters_updateEv) diff --git a/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld b/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld index c9f5fe63516..34ade0d3cd3 100644 --- a/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld @@ -308,7 +308,7 @@ *(.text._ZN9LockGuardD1Ev) /* itcm-check-ignore */ *(.text._ZN4EKF213PublishStatesERKy) *(.text._ZN3ADC3RunEv) -*(.text._ZN6BMP38815compensate_dataEhPK16bmp3_uncomp_dataP9bmp3_data) +*(.text._ZNK6BMP38818CompensatePressureEmx) *(.text._ZN3Ekf20controlFakePosFusionEv) *(.text._ZN11calibration9Gyroscope13set_device_idEm) *(.text._ZN24MavlinkStreamOrbitStatus8get_sizeEv) @@ -508,7 +508,7 @@ *(.text.imxrt_dmach_getcount) *(.text.sem_clockwait) *(.text.inet_poll) -*(.text._ZN6BMP3887collectEv) +*(.text._ZN6BMP3887CollectEv) *(.text._ZN15ArchPX4IOSerial19_do_rx_dma_callbackEbi) *(.text._ZN15ArchPX4IOSerial10_abort_dmaEv) *(.text._ZNK15PositionControl24getLocalPositionSetpointER33vehicle_local_position_setpoint_s) @@ -537,7 +537,7 @@ *(.text._ZN21MavlinkStreamAltitude4sendEv) *(.text._ZN7sensors14VehicleAirData12UpdateStatusEv) *(.text.imxrt_padmux_map) -*(.text._ZN6BMP38815get_sensor_dataEhP9bmp3_data) +*(.text._ZNK6BMP38821CompensateTemperatureEmRx) *(.text._ZN18MavlinkRateLimiter5checkERKy) *(.text._ZThn24_N26MulticopterAttitudeControl3RunEv) *(.text._ZN15ArchPX4IOSerial10_interruptEiPvS0_) @@ -617,7 +617,7 @@ *(.text._ZThn8_N3ADC3RunEv) *(.text._ZN11StickTiltXYC1EP12ModuleParams) *(.text._ZN12SafetyButton3RunEv) -*(.text._ZN6BMP38811set_op_modeEh) +*(.text._ZN6BMP38813RegisterWriteEN12Bosch_BMP3888RegisterEh) *(.text._ZN3GPS8callbackE15GPSCallbackTypePviS1_) *(.text._ZN13AnalogBattery19get_current_channelEv) *(.text._ZN15EstimatorChecks20checkEstimatorStatusERK7ContextR6ReportRK18estimator_status_s8NavModes) @@ -675,7 +675,7 @@ *(.text._ZN20ImuConsistencyChecks14checkAndReportERK7ContextR6Report) *(.text._ZN28MavlinkStreamGpsGlobalOrigin8get_sizeEv) *(.text.MEM_DataCopy2_1) -*(.text._ZN6BMP3887measureEv) +*(.text._ZN6BMP3887MeasureEv) *(.text._ZN4EKF217PublishRngHgtBiasERKy) *(.text._ZN36MavlinkStreamPositionTargetGlobalInt8get_sizeEv) *(.text._ZN28MavlinkStreamEstimatorStatus8get_sizeEv) @@ -691,7 +691,7 @@ *(.text._ZN32MavlinkStreamNavControllerOutput8get_sizeEv) *(.text._ZN6matrix8wrap_2piIfEET_S1_) *(.text._ZN4uORB7Manager30orb_remove_internal_subscriberEPv) -*(.text._ZN10BMP388_I2C7get_regEhPh) +*(.text._ZN6BMP38812RegisterReadEN12Bosch_BMP3888RegisterEPvj) *(.text._ZN4math17WelfordMeanVectorIfLj3EE5resetEv) *(.text._ZN27MavlinkStreamScaledPressure8get_sizeEv) *(.text._ZN3RTL17parameters_updateEv) diff --git a/src/drivers/barometer/bmp388/CMakeLists.txt b/src/drivers/barometer/bmp388/CMakeLists.txt index 35474254de7..0552dcc1786 100644 --- a/src/drivers/barometer/bmp388/CMakeLists.txt +++ b/src/drivers/barometer/bmp388/CMakeLists.txt @@ -1,6 +1,6 @@ ############################################################################ # -# Copyright (c) 2019 PX4 Development Team. All rights reserved. +# Copyright (c) 2019-2026 PX4 Development Team. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions @@ -35,9 +35,8 @@ px4_add_module( MODULE drivers__barometer__bmp388 MAIN bmp388 SRCS - bmp388_spi.cpp - bmp388_i2c.cpp bmp388.cpp + bmp388.h bmp388_main.cpp DEPENDS drivers_barometer diff --git a/src/drivers/barometer/bmp388/bmp388.cpp b/src/drivers/barometer/bmp388/bmp388.cpp index 49874dd3477..e59d01c7e9c 100644 --- a/src/drivers/barometer/bmp388/bmp388.cpp +++ b/src/drivers/barometer/bmp388/bmp388.cpp @@ -1,6 +1,6 @@ /**************************************************************************** * - * Copyright (c) 2019 PX4 Development Team. All rights reserved. + * Copyright (c) 2019-2026 PX4 Development Team. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -31,578 +31,369 @@ * ****************************************************************************/ -/** - * @file bmp388.cpp - * - * Driver for the BMP388 barometric pressure sensor connected via SPI or I2C - * - * Refer to: https://github.com/BoschSensortec/BMP3-Sensor-API - */ - #include "bmp388.h" -BMP388::BMP388(const I2CSPIDriverConfig &config, IBMP388 *interface) : +#include + +#include +#include + +using namespace time_literals; + +BMP388::BMP388(const I2CSPIDriverConfig &config) : + I2C(config), I2CSPIDriver(config), - _px4_baro{interface->get_device_id()}, - _interface(interface), - _sample_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": read")), - _measure_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": measure")), - _comms_errors(perf_alloc(PC_COUNT, MODULE_NAME": comms errors")) + _px4_baro(get_device_id()) { + _retries = 1; } BMP388::~BMP388() { - /* free perf counters */ - perf_free(_sample_perf); - perf_free(_measure_perf); - perf_free(_comms_errors); - - delete _interface; + perf_free(_bad_transfer_perf); + perf_free(_conversion_timeout_perf); + perf_free(_reset_perf); } -int -BMP388::init() +int BMP388::init() { - if (!soft_reset()) { - PX4_DEBUG("failed to reset baro during init"); + const int ret = I2C::init(); + + if (ret != PX4_OK) { + DEVICE_DEBUG("I2C::init failed (%i)", ret); + return ret; + } + + // probe() may have refined the device type to BMP390 + _px4_baro.set_device_id(get_device_id()); + + _state = STATE::RESET; + ScheduleNow(); + + return PX4_OK; +} + +int BMP388::probe() +{ + for (int i = 0; i < 3; i++) { + const uint8_t chip_id = RegisterRead(Register::CHIP_ID); + + if ((chip_id == BMP388_CHIP_ID) || (chip_id == BMP390_CHIP_ID)) { + _chip_id = chip_id; + _rev_id = RegisterRead(Register::REV_ID); + + if (chip_id == BMP390_CHIP_ID) { + set_device_type(DRV_BARO_DEVTYPE_BMP390); + _item_name = "bmp390"; + } + + return PX4_OK; + } + + DEVICE_DEBUG("unexpected CHIP_ID 0x%02x", chip_id); + } + + return PX4_ERROR; +} + +void BMP388::RunImpl() +{ + switch (_state) { + case STATE::RESET: + + // the command decoder is not ready until tstartup (2 ms) after POR + if (RegisterRead(Register::STATUS) & STATUS_BIT::CMD_RDY) { + RegisterWrite(Register::CMD, CMD_VALUE::SOFTRESET); + _failure_count = 0; + _state = STATE::WAIT_FOR_RESET; + perf_count(_reset_perf); + } + + ScheduleDelayed(2_ms); + break; + + case STATE::WAIT_FOR_RESET: + + // a softreset issued while the decoder was busy is dropped with cmd_err + if ((RegisterRead(Register::CHIP_ID) == _chip_id) + && (RegisterRead(Register::STATUS) & STATUS_BIT::CMD_RDY) + && !(RegisterRead(Register::ERR_REG) & ERR_REG_BIT::CMD_ERR)) { + _state = STATE::CONFIGURE; + ScheduleNow(); + + } else { + PX4_DEBUG("reset failed, retrying"); + _state = STATE::RESET; + ScheduleDelayed(100_ms); + } + + break; + + case STATE::CONFIGURE: + if (Configure()) { + _state = STATE::MEASURE; + ScheduleNow(); + + } else { + PX4_DEBUG("configure failed, resetting"); + _state = STATE::RESET; + ScheduleDelayed(100_ms); + } + + break; + + case STATE::MEASURE: + if (Measure()) { + _state = STATE::COLLECT; + ScheduleDelayed(_conversion_time_typ_us); + + } else { + Failure(); + } + + break; + + case STATE::COLLECT: + switch (Collect()) { + case PX4_OK: + if (_failure_count > 0) { + _failure_count--; + } + + _state = STATE::MEASURE; + ScheduleNow(); + break; + + case -EAGAIN: + // conversion still running (DS typical < max) + ScheduleDelayed(2_ms); + break; + + default: + Failure(); + break; + } + + break; + } +} + +bool BMP388::Measure() +{ + // forced mode runs one conversion, then the chip returns to sleep + if (!RegisterWrite(Register::PWR_CTRL, PWR_CTRL_BIT::PRESS_EN | PWR_CTRL_BIT::TEMP_EN | PWR_CTRL_BIT::MODE_FORCED)) { + return false; + } + + // conversion starts when the write lands, not when this work item started + _measure_timestamp = hrt_absolute_time(); + + return true; +} + +int BMP388::Collect() +{ + status_data data{}; + + if (!RegisterRead(Register::STATUS, &data, sizeof(data))) { return -EIO; } - if (_interface->get_reg(BMP3_CHIP_ID_ADDR, &_chip_id) != OK) { - PX4_WARN("failed to get chip id"); - return -EIO; + if ((data.status & (STATUS_BIT::DRDY_PRESS | STATUS_BIT::DRDY_TEMP)) != (STATUS_BIT::DRDY_PRESS | STATUS_BIT::DRDY_TEMP)) { + if (hrt_elapsed_time(&_measure_timestamp) < _conversion_time_max_us) { + return -EAGAIN; + } + + perf_count(_conversion_timeout_perf); + return -ETIMEDOUT; } - if (_chip_id != BMP388_CHIP_ID && _chip_id != BMP390_CHIP_ID) { - PX4_WARN("id of your baro is not: 0x%02x or 0x%02x", BMP388_CHIP_ID, BMP390_CHIP_ID); - return -EIO; + const uint32_t uncomp_press = ((uint32_t)data.press_23_16 << 16) | ((uint32_t)data.press_15_8 << 8) | data.press_7_0; + const uint32_t uncomp_temp = ((uint32_t)data.temp_23_16 << 16) | ((uint32_t)data.temp_15_8 << 8) | data.temp_7_0; + + int64_t t_lin = 0; + const int64_t temperature = CompensateTemperature(uncomp_temp, t_lin); // 0.01 degC + const uint64_t pressure = CompensatePressure(uncomp_press, t_lin); // 0.01 Pa + + // pressure is averaged over the conversion window, so timestamp its midpoint + const hrt_abstime timestamp_sample = _measure_timestamp + (_conversion_time_typ_us / 2); + + _px4_baro.set_error_count(perf_event_count(_bad_transfer_perf) + perf_event_count(_conversion_timeout_perf)); + _px4_baro.set_temperature(temperature / 100.f); + _px4_baro.update(timestamp_sample, pressure / 100.f); + + return PX4_OK; +} + +void BMP388::Failure() +{ + // skip the sample; reset once failures are persistent + if (++_failure_count > 10) { + _state = STATE::RESET; + ScheduleDelayed(100_ms); + + } else { + _state = STATE::MEASURE; + ScheduleDelayed(10_ms); } +} + +bool BMP388::Configure() +{ + if (!ReadCalibration()) { + return false; + } + + // ultra high resolution: osr_p x16, osr_t x2. IIR bypass and ODR stay at their reset defaults (ODR is unused in forced mode). + static constexpr uint8_t kOsr = OSR_BIT::OSR_P_X16 | OSR_BIT::OSR_T_X2; + + if (!RegisterWrite(Register::OSR, kOsr) || (RegisterRead(Register::OSR) != kOsr)) { + return false; + } + + // forced-mode conversion time for this OSR: BMP388 DS table 21, BMP390 DS table 23 + static constexpr uint32_t kConversionTypUsBmp388 = 36900; + static constexpr uint32_t kConversionMaxUsBmp388 = 43300; + static constexpr uint32_t kConversionTypUsBmp390 = 37140; + static constexpr uint32_t kConversionMaxUsBmp390 = 41890; if (_chip_id == BMP390_CHIP_ID) { - _interface->set_device_type(DRV_BARO_DEVTYPE_BMP390); - this->_item_name = "bmp390"; + _conversion_time_typ_us = kConversionTypUsBmp390; + _conversion_time_max_us = kConversionMaxUsBmp390; + + } else { + _conversion_time_typ_us = kConversionTypUsBmp388; + _conversion_time_max_us = kConversionMaxUsBmp388; } - // device type may have been refined above (BMP388 vs BMP390); latch the final device id - _px4_baro.set_device_id(_interface->get_device_id()); - - if (_interface->get_reg(BMP3_REV_ID_ADDR, &_chip_rev_id) != OK) { - PX4_WARN("failed to get chip rev id"); - return -EIO; - } - - _cal = _interface->get_calibration(BMP3_CALIB_DATA_ADDR); - - if (!_cal) { - PX4_WARN("failed to get baro cal init"); - return -EIO; - } - - if (!validate_trimming_param()) { - PX4_WARN("failed to validate trim param"); - return -EIO; - } - - if (!set_sensor_settings()) { - PX4_WARN("failed to set sensor settings"); - return -EIO; - } - - start(); - - return OK; + return true; } -void -BMP388::print_status() +// bmp3_selftest.c +static uint8_t Crc8(uint8_t seed, uint8_t data) { - I2CSPIDriverBase::print_status(); - printf("chip id: 0x%02x rev id: 0x%02x\n", _chip_id, _chip_rev_id); - perf_print_counter(_sample_perf); - perf_print_counter(_measure_perf); - perf_print_counter(_comms_errors); - printf("measurement interval: %u us \n", _measure_interval); -} + static constexpr uint8_t kPoly = 0x1D; -void -BMP388::start() -{ - _collect_phase = false; + for (int i = 0; i < 8; i++) { + const bool feedback = (seed ^ data) & 0x80; + seed <<= 1; + data <<= 1; - // wait a bit longer for the first measurement, as otherwise the first readout might fail - ScheduleOnInterval(_measure_interval, _measure_interval * 3); -} - -void -BMP388::RunImpl() -{ - if (_collect_phase) { - collect(); - } - - measure(); -} - -int -BMP388::measure() -{ - _collect_phase = true; - - perf_begin(_measure_perf); - - /* start measurement */ - if (!set_op_mode(BMP3_FORCED_MODE)) { - PX4_DEBUG("failed to set operating mode"); - perf_count(_comms_errors); - perf_cancel(_measure_perf); - return -EIO; - } - - perf_end(_measure_perf); - - return OK; -} - -int -BMP388::collect() -{ - _collect_phase = false; - - /* enable pressure and temperature */ - uint8_t sensor_comp = BMP3_PRESS | BMP3_TEMP; - bmp3_data data{}; - - perf_begin(_sample_perf); - - /* Correct for measurement integration delay: the pressure was - * integrated over the preceding measurement_time window, so the - * effective sample midpoint is half the measurement time before now. */ - const hrt_abstime now = hrt_absolute_time(); - const hrt_abstime half_meas = get_measurement_time() / 2; - const hrt_abstime timestamp_sample = (now > half_meas) ? (now - half_meas) : now; - - if (!get_sensor_data(sensor_comp, &data)) { - perf_count(_comms_errors); - perf_cancel(_sample_perf); - return -EIO; - } - - float temperature = (float)(data.temperature / 100.0f); - float pressure = (float)(data.pressure / 100.0f); // to Pascal - - // publish - _px4_baro.set_error_count(perf_event_count(_comms_errors)); - _px4_baro.set_temperature(temperature); - _px4_baro.update(timestamp_sample, pressure); - - perf_end(_sample_perf); - - return OK; -} - -/*! - * @brief This API performs the soft reset of the sensor. - * - * Refer: https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/bmp3.c - */ -bool -BMP388::soft_reset() -{ - uint8_t status = 0; - - /* STATUS resets to 0, so cmd_rdy is 0 until the decoder is ready. */ - const hrt_abstime start = hrt_absolute_time(); - - while (true) { - if ((_interface->get_reg(BMP3_SENS_STATUS_REG_ADDR, &status) == OK) && - (status & BMP3_CMD_RDY)) { - break; + if (feedback) { + seed ^= kPoly; } - - if (hrt_elapsed_time(&start) > BMP3_CMD_RDY_WAIT_TIME) { - return false; - } - - px4_usleep(500); } - if (_interface->set_reg(BPM3_CMD_SOFT_RESET, BMP3_CMD_ADDR) != OK) { + return seed; +} + +bool BMP388::ReadCalibration() +{ + if (!RegisterRead(Register::NVM_PAR_T1, &_calib, sizeof(_calib))) { return false; } - px4_usleep(BMP3_POST_RESET_WAIT_TIME); - - if (_interface->get_reg(BMP3_ERR_REG_ADDR, &status) != OK) { - return false; - } - - return (status & BMP3_CMD_ERR) == 0; -} - -/* - * @brief function to calculate CRC for the trimming parameters - * - * Refer: https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/self-test/bmp3_selftest.c - * */ -static int8_t cal_crc(uint8_t seed, uint8_t data) -{ - int8_t poly = 0x1D; - - for (uint8_t i = 0; i < 8; i++) { - int8_t var2 = ((seed & 0x80) ^ (data & 0x80)) ? 1 : 0; - - seed = (seed & 0x7F) << 1; - data = (data & 0x7F) << 1; - seed = seed ^ (uint8_t)(poly * var2); - } - - return (int8_t)seed; -} - -/* - * @brief Function to verify the trimming parameters - * - * Refer: https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/self-test/bmp3_selftest.c - * */ -bool -BMP388::validate_trimming_param() -{ uint8_t crc = 0xFF; - uint8_t *trim_param = (uint8_t *)_cal; - static_assert(BMP3_CALIB_DATA_LEN <= sizeof(*_cal), "unexpected struct size"); - - for (int i = 0; i < BMP3_CALIB_DATA_LEN; i++) { - crc = (uint8_t)cal_crc(crc, trim_param[i]); + for (const uint8_t byte : reinterpret_cast(_calib)) { + crc = Crc8(crc, byte); } - crc = (crc ^ 0xFF); + crc ^= 0xFF; - uint8_t stored_crc; - - if (_interface->get_reg(BMP3_TRIM_CRC_DATA_ADDR, &stored_crc) != OK) { - return false; - } - - return stored_crc == crc; -} - -uint32_t -BMP388::get_measurement_time() -{ - /* - From BST-BMP388-DS001.pdf, page 25, table 21 - - Pressure Temperature Measurement Max Time - Oversample Oversample Time (Forced) - x1 1x 4.9 ms 5.7 ms - x2 1x 6.9 ms 8.7 ms - x4 1x 10.9 ms 13.3 ms - x8 1x 18.9 ms 22.5 ms - x16 2x 36.9 ms 43.3 ms - x32 2x 68.9 ms (not documented) - */ - - uint32_t meas_time_us = 0; // unsupported value by default - - if (osr_t == BMP3_NO_OVERSAMPLING) { - switch (osr_p) { - case BMP3_NO_OVERSAMPLING: - meas_time_us = 5700; - break; - - case BMP3_OVERSAMPLING_2X: - meas_time_us = 8700; - break; - - case BMP3_OVERSAMPLING_4X: - meas_time_us = 13300; - break; - - case BMP3_OVERSAMPLING_8X: - meas_time_us = 22500; - break; - - default: - break; - } - - } else if (osr_t == BMP3_OVERSAMPLING_2X) { - switch (osr_p) { - case BMP3_OVERSAMPLING_16X: - meas_time_us = 43300; - break; - - case BMP3_OVERSAMPLING_32X: - meas_time_us = 68900; - break; - - default: - break; - } - } - - return meas_time_us; -} - -/*! - * @brief This API sets the power control(pressure enable and - * temperature enable), over sampling, odr and filter - * settings in the sensor. - * - * Refer: https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/bmp3.c - */ -bool -BMP388::set_sensor_settings() -{ - _measure_interval = get_measurement_time(); - - if (_measure_interval == 0) { - PX4_WARN("unsupported oversampling selected"); - return false; - } - - /* Select the pressure and temperature sensor to be enabled */ - uint8_t pwc_ctl_reg = 0; - pwc_ctl_reg = BMP3_SET_BITS_POS_0(pwc_ctl_reg, BMP3_PRESS_EN, BMP3_ENABLE); - pwc_ctl_reg = BMP3_SET_BITS(pwc_ctl_reg, BMP3_TEMP_EN, BMP3_ENABLE); - - int ret = _interface->set_reg(pwc_ctl_reg, BMP3_PWR_CTRL_ADDR); - - if (ret != OK) { - PX4_WARN("failed to set settings BMP3_PWR_CTRL_ADDR"); - return false; - } - - /* Select the over sampling settings for pressure and temperature */ - uint8_t osr_ctl_reg = 0; - osr_ctl_reg = BMP3_SET_BITS_POS_0(osr_ctl_reg, BMP3_PRESS_OS, osr_p); - osr_ctl_reg = BMP3_SET_BITS(osr_ctl_reg, BMP3_TEMP_OS, osr_t); - - ret = _interface->set_reg(osr_ctl_reg, BMP3_OSR_ADDR); - - if (ret != OK) { - PX4_WARN("failed to set settings BMP3_OSR_ADDR"); - return false; - } - - /* Using 'forced mode' so this is not required but here for future use possibly */ - uint8_t odr_ctl_reg = 0; - odr_ctl_reg = BMP3_SET_BITS_POS_0(odr_ctl_reg, BMP3_ODR, odr); - - ret = _interface->set_reg(odr_ctl_reg, BMP3_ODR_ADDR); - - if (ret != OK) { - PX4_WARN("failed to set output data rate register"); - return false; - } - - uint8_t iir_ctl_reg = 0; - iir_ctl_reg = BMP3_SET_BITS(iir_ctl_reg, BMP3_IIR_FILTER, iir_coef); - ret = _interface->set_reg(iir_ctl_reg, BMP3_IIR_ADDR); - - if (ret != OK) { - PX4_WARN("failed to set IIR settings"); + if (RegisterRead(Register::TRIM_CRC) != crc) { + PX4_DEBUG("calibration CRC mismatch"); return false; } return true; } - -/*! - * @brief This API sets the power mode of the sensor. - * - * Refer: https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/bmp3.c - */ -bool -BMP388::set_op_mode(uint8_t op_mode) +// Integer compensation from the Bosch BMP3-Sensor-API (bmp3.c). t_lin carries the +// temperature into the pressure formula. +int64_t BMP388::CompensateTemperature(uint32_t uncomp_temp, int64_t &t_lin) const { - uint8_t op_mode_reg_val; - int ret = _interface->get_reg(BMP3_PWR_CTRL_ADDR, &op_mode_reg_val); + const int64_t partial_data1 = (int64_t)uncomp_temp - (256 * _calib.par_t1); + const int64_t partial_data2 = _calib.par_t2 * partial_data1; + const int64_t partial_data3 = partial_data1 * partial_data1; + const int64_t partial_data4 = partial_data3 * _calib.par_t3; + const int64_t partial_data5 = (partial_data2 * 262144) + partial_data4; - if (ret != OK) { + t_lin = partial_data5 / 4294967296; + + return (t_lin * 25) / 16384; +} + +uint64_t BMP388::CompensatePressure(uint32_t uncomp_press, int64_t t_lin) const +{ + int64_t partial_data1 = t_lin * t_lin; + int64_t partial_data2 = partial_data1 / 64; + int64_t partial_data3 = (partial_data2 * t_lin) / 256; + int64_t partial_data4 = (_calib.par_p8 * partial_data3) / 32; + int64_t partial_data5 = (_calib.par_p7 * partial_data1) * 16; + int64_t partial_data6 = (_calib.par_p6 * t_lin) * 4194304; + const int64_t offset = (_calib.par_p5 * 140737488355328) + partial_data4 + partial_data5 + partial_data6; + + partial_data2 = (_calib.par_p4 * partial_data3) / 32; + partial_data4 = (_calib.par_p3 * partial_data1) * 4; + partial_data5 = (_calib.par_p2 - 16384) * t_lin * 2097152; + const int64_t sensitivity = ((_calib.par_p1 - 16384) * 70368744177664) + partial_data2 + partial_data4 + partial_data5; + + partial_data1 = (sensitivity / 16777216) * uncomp_press; + partial_data2 = _calib.par_p10 * t_lin; + partial_data3 = partial_data2 + (65536 * _calib.par_p9); + partial_data4 = (partial_data3 * uncomp_press) / 8192; + // divide by 10 first so uncomp_press * partial_data4 cannot overflow + partial_data5 = (uncomp_press * (partial_data4 / 10)) / 512; + partial_data5 = partial_data5 * 10; + partial_data6 = (int64_t)((uint64_t)uncomp_press * (uint64_t)uncomp_press); + partial_data2 = (_calib.par_p11 * partial_data6) / 65536; + partial_data3 = (partial_data2 * uncomp_press) / 128; + partial_data4 = (offset / 4) + partial_data1 + partial_data5 + partial_data3; + + return ((uint64_t)partial_data4 * 25) / (uint64_t)1099511627776; +} + +bool BMP388::RegisterRead(Register reg, void *buf, size_t len) +{ + const uint8_t cmd = static_cast(reg); + + if (transfer(&cmd, 1, static_cast(buf), len) != PX4_OK) { + perf_count(_bad_transfer_perf); return false; } - uint8_t last_set_mode = BMP3_GET_BITS(op_mode_reg_val, BMP3_OP_MODE); + return true; +} - /* Device needs to be put in sleep mode to transition */ - if (last_set_mode != BMP3_SLEEP_MODE) { - op_mode_reg_val = op_mode_reg_val & (~(BMP3_OP_MODE_MSK)); - ret = _interface->set_reg(op_mode_reg_val, BMP3_PWR_CTRL_ADDR); +uint8_t BMP388::RegisterRead(Register reg) +{ + uint8_t value = 0; + RegisterRead(reg, &value, 1); + return value; +} - if (ret != OK) { - return false; - } +bool BMP388::RegisterWrite(Register reg, uint8_t value) +{ + const uint8_t buf[2] {static_cast(reg), value}; - px4_usleep(BMP3_POST_SLEEP_WAIT_TIME); + if (transfer(buf, sizeof(buf), nullptr, 0) != PX4_OK) { + perf_count(_bad_transfer_perf); + return false; } - bool result = false; - - if (ret == OK) { - ret = _interface->get_reg(BMP3_PWR_CTRL_ADDR, &op_mode_reg_val); - - if (ret != OK) { - return false; - } - - op_mode_reg_val = BMP3_SET_BITS(op_mode_reg_val, BMP3_OP_MODE, op_mode); - ret = _interface->set_reg(op_mode_reg_val, BMP3_PWR_CTRL_ADDR); - - if (ret != OK) { - return false; - } - - result = true; - } - - return result; + return true; } -/*! - * @brief This internal API is used to parse the pressure or temperature or - * both the data and store it in the bmp3_uncomp_data structure instance. - * - * Refer: https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/bmp3.c - */ -static void parse_sensor_data(const uint8_t *reg_data, struct bmp3_uncomp_data *uncomp_data) +void BMP388::print_status() { - uint32_t data_xlsb = (uint32_t)reg_data[0]; - uint32_t data_lsb = (uint32_t)reg_data[1] << 8; - uint32_t data_msb = (uint32_t)reg_data[2] << 16; - uncomp_data->pressure = data_msb | data_lsb | data_xlsb; + I2CSPIDriverBase::print_status(); - data_xlsb = (uint32_t)reg_data[3]; - data_lsb = (uint32_t)reg_data[4] << 8; - data_msb = (uint32_t)reg_data[5] << 16; - uncomp_data->temperature = data_msb | data_lsb | data_xlsb; -} + PX4_INFO("chip id: 0x%02x rev id: 0x%02x", _chip_id, _rev_id); + PX4_INFO("conversion time: %" PRIu32 " us typ, %" PRIu32 " us max", _conversion_time_typ_us, + _conversion_time_max_us); - -/*! - * @brief This internal API is used to compensate the raw temperature data and - * return the compensated temperature data in integer data type. - * For eg if returned temperature is 2426 then it is 2426/100 = 24.26 deg Celsius - * - * Refer: https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/bmp3.c - */ -static int64_t compensate_temperature(const struct bmp3_uncomp_data *uncomp_data, struct bmp3_calib_data *calib_data) -{ - int64_t partial_data1 = ((int64_t)uncomp_data->temperature - (256 * calib_data->reg_calib_data.par_t1)); - int64_t partial_data2 = calib_data->reg_calib_data.par_t2 * partial_data1; - int64_t partial_data3 = (partial_data1 * partial_data1); - int64_t partial_data4 = (int64_t)partial_data3 * calib_data->reg_calib_data.par_t3; - int64_t partial_data5 = ((int64_t)(partial_data2 * 262144) + partial_data4); - int64_t partial_data6 = partial_data5 / 4294967296; - - /* Store t_lin in dev. structure for pressure calculation */ - calib_data->reg_calib_data.t_lin = partial_data6; - int64_t comp_temp = (int64_t)((partial_data6 * 25) / 16384); - - return comp_temp; -} - -/*! - * @brief This internal API is used to compensate the raw pressure data and - * return the compensated pressure data in integer data type. - * for eg return if pressure is 9528709 which is 9528709/100 = 95287.09 Pascal or 952.8709 hecto Pascal - * - * Refer: https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/bmp3.c - */ -static uint64_t compensate_pressure(const struct bmp3_uncomp_data *uncomp_data, - const struct bmp3_calib_data *calib_data) -{ - const struct bmp3_reg_calib_data *reg_calib_data = &calib_data->reg_calib_data; - - int64_t partial_data1 = reg_calib_data->t_lin * reg_calib_data->t_lin; - int64_t partial_data2 = partial_data1 / 64; - int64_t partial_data3 = (partial_data2 * reg_calib_data->t_lin) / 256; - int64_t partial_data4 = (reg_calib_data->par_p8 * partial_data3) / 32; - int64_t partial_data5 = (reg_calib_data->par_p7 * partial_data1) * 16; - int64_t partial_data6 = (reg_calib_data->par_p6 * reg_calib_data->t_lin) * 4194304; - int64_t offset = (reg_calib_data->par_p5 * 140737488355328) + partial_data4 + partial_data5 + partial_data6; - partial_data2 = (reg_calib_data->par_p4 * partial_data3) / 32; - partial_data4 = (reg_calib_data->par_p3 * partial_data1) * 4; - partial_data5 = (reg_calib_data->par_p2 - 16384) * reg_calib_data->t_lin * 2097152; - int64_t sensitivity = ((reg_calib_data->par_p1 - 16384) * 70368744177664) + partial_data2 + partial_data4 + - partial_data5; - partial_data1 = (sensitivity / 16777216) * uncomp_data->pressure; - partial_data2 = reg_calib_data->par_p10 * reg_calib_data->t_lin; - partial_data3 = partial_data2 + (65536 * reg_calib_data->par_p9); - partial_data4 = (partial_data3 * uncomp_data->pressure) / 8192; - /*dividing by 10 followed by multiplying by 10 to avoid overflow caused by (uncomp_data->pressure * partial_data4) */ - partial_data5 = (uncomp_data->pressure * (partial_data4 / 10)) / 512; - partial_data5 = partial_data5 * 10; - partial_data6 = (int64_t)((uint64_t)uncomp_data->pressure * (uint64_t)uncomp_data->pressure); - partial_data2 = (reg_calib_data->par_p11 * partial_data6) / 65536; - partial_data3 = (partial_data2 * uncomp_data->pressure) / 128; - partial_data4 = (offset / 4) + partial_data1 + partial_data5 + partial_data3; - uint64_t comp_press = (((uint64_t)partial_data4 * 25) / (uint64_t)1099511627776); - - return comp_press; -} - -/*! - * @brief This internal API is used to compensate the pressure or temperature - * or both the data according to the component selected by the user. - * - * Refer: https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/bmp3.c - */ -bool -BMP388::compensate_data(uint8_t sensor_comp, - const struct bmp3_uncomp_data *uncomp_data, - struct bmp3_data *comp_data) -{ - int8_t rslt = OK; - struct bmp3_calib_data calib_data = {0}; - struct bmp3_reg_calib_data *reg_calib_data = &calib_data.reg_calib_data; - memcpy(reg_calib_data, _cal, 21); - - if ((uncomp_data != nullptr) && (comp_data != nullptr)) { - if (sensor_comp & (BMP3_PRESS | BMP3_TEMP)) { - comp_data->temperature = compensate_temperature(uncomp_data, &calib_data); - } - - if (sensor_comp & BMP3_PRESS) { - comp_data->pressure = compensate_pressure(uncomp_data, &calib_data); - } - - } else { - rslt = ERROR; - } - - return (rslt == OK); -} - -/*! - * @brief This API reads the pressure, temperature or both data from the - * sensor, compensates the data and store it in the bmp3_data structure - * instance passed by the user. - */ -bool -BMP388::get_sensor_data(uint8_t sensor_comp, struct bmp3_data *comp_data) -{ - uint8_t reg_data[BMP3_P_T_DATA_LEN] {}; - - int8_t rslt = _interface->get_reg_buf(BMP3_SENS_STATUS_REG_ADDR, reg_data, BMP3_P_T_DATA_LEN); - - bool result = false; - struct bmp3_uncomp_data uncomp_data {}; - - if (rslt == OK) { - uint8_t status = reg_data[0]; - - // check if data ready (both temp and pressure) - if ((status & (3 << 5)) != (3 << 5)) { - return false; - } - - parse_sensor_data(reg_data + 1, &uncomp_data); - result = compensate_data(sensor_comp, &uncomp_data, comp_data); - } - - return result; + perf_print_counter(_bad_transfer_perf); + perf_print_counter(_conversion_timeout_perf); + perf_print_counter(_reset_perf); } diff --git a/src/drivers/barometer/bmp388/bmp388.h b/src/drivers/barometer/bmp388/bmp388.h index 378747d4d22..f47f1d9153d 100644 --- a/src/drivers/barometer/bmp388/bmp388.h +++ b/src/drivers/barometer/bmp388/bmp388.h @@ -1,6 +1,6 @@ /**************************************************************************** * - * Copyright (C) 2019 PX4 Development Team. All rights reserved. + * Copyright (c) 2019-2026 PX4 Development Team. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -31,191 +31,78 @@ * ****************************************************************************/ -/** - * @file bmp388.h - * - * Shared defines for the bmp388 driver. - */ #pragma once -#include #include -#include #include -#include +#include +#include #include -#include -#include -#include -#include "board_config.h" +#include +#include -// From https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/bmp3_defs.h +namespace Bosch_BMP388 +{ -#define BMP388_CHIP_ID (0x50) -#define BMP390_CHIP_ID (0x60) +static constexpr uint32_t I2C_SPEED = 100 * 1000; -/* Over sampling macros */ -#define BMP3_NO_OVERSAMPLING (0x00) -#define BMP3_OVERSAMPLING_2X (0x01) -#define BMP3_OVERSAMPLING_4X (0x02) -#define BMP3_OVERSAMPLING_8X (0x03) -#define BMP3_OVERSAMPLING_16X (0x04) -#define BMP3_OVERSAMPLING_32X (0x05) +static constexpr uint8_t BMP388_CHIP_ID = 0x50; +static constexpr uint8_t BMP390_CHIP_ID = 0x60; -/* Filter setting macros */ -#define BMP3_IIR_FILTER_DISABLE (0x00) -#define BMP3_IIR_FILTER_COEFF_1 (0x01) -#define BMP3_IIR_FILTER_COEFF_3 (0x02) -#define BMP3_IIR_FILTER_COEFF_7 (0x03) -#define BMP3_IIR_FILTER_COEFF_15 (0x04) -#define BMP3_IIR_FILTER_COEFF_31 (0x05) -#define BMP3_IIR_FILTER_COEFF_63 (0x06) -#define BMP3_IIR_FILTER_COEFF_127 (0x07) +static constexpr uint8_t Bit0 = (1 << 0); +static constexpr uint8_t Bit1 = (1 << 1); +static constexpr uint8_t Bit2 = (1 << 2); +static constexpr uint8_t Bit3 = (1 << 3); +static constexpr uint8_t Bit4 = (1 << 4); +static constexpr uint8_t Bit5 = (1 << 5); +static constexpr uint8_t Bit6 = (1 << 6); -/* Odr setting macros */ -#define BMP3_ODR_200_HZ (0x00) -#define BMP3_ODR_100_HZ (0x01) -#define BMP3_ODR_50_HZ (0x02) -#define BMP3_ODR_25_HZ (0x03) +enum class Register : uint8_t { + CHIP_ID = 0x00, + REV_ID = 0x01, + ERR_REG = 0x02, + STATUS = 0x03, + PWR_CTRL = 0x1B, + OSR = 0x1C, + TRIM_CRC = 0x30, // CRC-8 over NVM_PAR_T1..NVM_PAR_P11 (bmp3_selftest.c) + NVM_PAR_T1 = 0x31, // calibration data 0x31..0x45 + CMD = 0x7E, +}; -/* Register Address */ -#define BMP3_CHIP_ID_ADDR (0x00) -#define BMP3_REV_ID_ADDR (0x01) -#define BMP3_ERR_REG_ADDR (0x02) -#define BMP3_SENS_STATUS_REG_ADDR (0x03) -#define BMP3_DATA_ADDR (0x04) -#define BMP3_PWR_CTRL_ADDR (0x1B) -#define BMP3_OSR_ADDR (0X1C) -#define BMP3_CALIB_DATA_ADDR (0x31) -#define BMP3_CMD_ADDR (0x7E) +// ERR_REG +enum ERR_REG_BIT : uint8_t { + CMD_ERR = Bit1, +}; -/* Error status macros */ -#define BMP3_FATAL_ERR (0x01) -#define BMP3_CMD_ERR (0x02) -#define BMP3_CONF_ERR (0x04) +// STATUS +enum STATUS_BIT : uint8_t { + DRDY_TEMP = Bit6, + DRDY_PRESS = Bit5, + CMD_RDY = Bit4, +}; -/* Status macros */ -#define BMP3_CMD_RDY (0x10) -#define BMP3_DRDY_PRESS (0x20) -#define BMP3_DRDY_TEMP (0x40) +// PWR_CTRL +enum PWR_CTRL_BIT : uint8_t { + MODE_FORCED = Bit4, // mode[5:4]: 00 sleep, 01/10 forced, 11 normal + TEMP_EN = Bit1, + PRESS_EN = Bit0, +}; -/* Power mode macros */ -#define BMP3_SLEEP_MODE (0x00) -#define BMP3_FORCED_MODE (0x01) -#define BMP3_NORMAL_MODE (0x03) +// OSR +enum OSR_BIT : uint8_t { + OSR_T_X2 = Bit3, // osr_t[5:3]: 2^n oversampling + OSR_P_X16 = Bit2, // osr_p[2:0]: 2^n oversampling +}; -#define BMP3_ENABLE (0x01) -#define BMP3_DISABLE (0x00) +// CMD +enum CMD_VALUE : uint8_t { + SOFTRESET = 0xB6, +}; -/* Sensor component selection macros. These values are internal for API implementation. - * Don't relate this t0 data sheet. - */ -#define BMP3_PRESS (1) -#define BMP3_TEMP (1 << 1) -#define BMP3_ALL (0x03) - -/* Macros related to size */ -#define BMP3_CALIB_DATA_LEN (21) -#define BMP3_P_T_DATA_LEN (7) - -/* Macros to select the which sensor settings are to be set by the user. - * These values are internal for API implementation. Don't relate this to - * data sheet. */ -#define BMP3_PRESS_EN_SEL (1 << 1) -#define BMP3_TEMP_EN_SEL (1 << 2) -#define BMP3_PRESS_OS_SEL (1 << 4) - -/* Macros for bit masking */ -#define BMP3_OP_MODE_MSK (0x30) -#define BMP3_OP_MODE_POS (0x04) - -#define BMP3_PRESS_EN_MSK (0x01) - -#define BMP3_TEMP_EN_MSK (0x02) -#define BMP3_TEMP_EN_POS (0x01) - -#define BMP3_IIR_FILTER_MSK (0x0E) -#define BMP3_IIR_FILTER_POS (0x01) - -#define BMP3_ODR_MSK (0x1F) - -#define BMP3_PRESS_OS_MSK (0x07) - -#define BMP3_TEMP_OS_MSK (0x38) -#define BMP3_TEMP_OS_POS (0x03) - -#define BMP3_SET_BITS(reg_data, bitname, data) \ - ((reg_data & ~(bitname##_MSK)) | \ - ((data << bitname##_POS) & bitname##_MSK)) - -/* Macro variant to handle the bitname position if it is zero */ -#define BMP3_SET_BITS_POS_0(reg_data, bitname, data) \ - ((reg_data & ~(bitname##_MSK)) | \ - (data & bitname##_MSK)) - -#define BMP3_GET_BITS(reg_data, bitname) ((reg_data & (bitname##_MSK)) >> \ - (bitname##_POS)) - -/* Macro variant to handle the bitname position if it is zero */ -#define BMP3_GET_BITS_POS_0(reg_data, bitname) (reg_data & (bitname##_MSK)) - -// From https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/self-test/bmp3_selftest.c -#define BMP3_POST_SLEEP_WAIT_TIME 5000 -#define BMP3_POST_RESET_WAIT_TIME 2000 -#define BMP3_CMD_RDY_WAIT_TIME 10000 /* STATUS.cmd_rdy after POR; tstartup is 2 ms */ -#define BMP3_POST_INIT_WAIT_TIME 40000 -#define BMP3_TRIM_CRC_DATA_ADDR 0x30 -#define BPM3_CMD_SOFT_RESET 0xB6 -#define BMP3_ODR_ADDR 0x1D -#define BMP3_IIR_ADDR 0x1F - -// https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/bmp3.c -/* Power control settings */ -#define POWER_CNTL (0x0006) -/* Odr and filter settings */ -#define ODR_FILTER (0x00F0) -/* Interrupt control settings */ -#define INT_CTRL (0x0708) -/* Advance settings */ -#define ADV_SETT (0x1800) - -#pragma pack(push,1) -struct calibration_s { - uint16_t par_t1; - uint16_t par_t2; - int8_t par_t3; - - int16_t par_p1; - int16_t par_p2; - int8_t par_p3; - int8_t par_p4; - uint16_t par_p5; - uint16_t par_p6; - int8_t par_p7; - int8_t par_p8; - int16_t par_p9; - int8_t par_p10; - int8_t par_p11; - -}; //calibration data - -struct data_s { - uint8_t p_msb; - uint8_t p_lsb; - uint8_t p_xlsb; - - uint8_t t_msb; - uint8_t t_lsb; - uint8_t t_xlsb; -}; // data - -struct bmp3_reg_calib_data { - /** - * @ Trim Variables - */ - - /**@{*/ +#pragma pack(push, 1) +// NVM_PAR_T1..NVM_PAR_P11 as laid out from 0x31, LSB first (BMP390 DS table 24) +struct calib_data { uint16_t par_t1; uint16_t par_t2; int8_t par_t3; @@ -230,140 +117,78 @@ struct bmp3_reg_calib_data { int16_t par_p9; int8_t par_p10; int8_t par_p11; - int64_t t_lin; +}; - /**@}*/ +// STATUS followed by DATA_0..DATA_5 (0x03..0x09) +struct status_data { + uint8_t status; + uint8_t press_7_0; + uint8_t press_15_8; + uint8_t press_23_16; + uint8_t temp_7_0; + uint8_t temp_15_8; + uint8_t temp_23_16; }; #pragma pack(pop) -/*! - * bmp3 sensor structure which comprises of temperature and pressure data. - */ -struct bmp3_data { - /* Compensated temperature */ - int64_t temperature; +static_assert(sizeof(calib_data) == 21); +static_assert(sizeof(status_data) == 7); - /* Compensated pressure */ - uint64_t pressure; -}; +} // namespace Bosch_BMP388 -/*! - * Calibration data - */ -struct bmp3_calib_data { - /*! Register data */ - struct bmp3_reg_calib_data reg_calib_data; -}; +using namespace Bosch_BMP388; -/*! - * bmp3 sensor structure which comprises of un-compensated temperature - * and pressure data. - */ -struct bmp3_uncomp_data { - /*! un-compensated pressure */ - uint32_t pressure; - - /*! un-compensated temperature */ - uint32_t temperature; -}; - -struct fcalibration_s { - float t1; - float t2; - float t3; - - float p1; - float p2; - float p3; - float p4; - float p5; - float p6; - float p7; - float p8; - float p9; -}; - -/* - * BMP388 internal constants and data structures. - */ -class IBMP388 +class BMP388 : public device::I2C, public I2CSPIDriver { public: - virtual ~IBMP388() = default; + BMP388(const I2CSPIDriverConfig &config); + ~BMP388() override; - virtual int init() = 0; - - // read reg value - virtual int get_reg(uint8_t addr, uint8_t *value) = 0; - - // bulk read reg value - virtual int get_reg_buf(uint8_t addr, uint8_t *buf, uint8_t len) = 0; - - // write reg value - virtual int set_reg(uint8_t value, uint8_t addr) = 0; - - // bulk read of calibration data into buffer, return same pointer - virtual calibration_s *get_calibration(uint8_t addr) = 0; - - virtual uint32_t get_device_id() const = 0; - - virtual uint8_t get_device_address() const = 0; - - virtual void set_device_type(uint8_t devtype) = 0; -}; - -class BMP388 : public I2CSPIDriver -{ -public: - BMP388(const I2CSPIDriverConfig &config, IBMP388 *interface); - virtual ~BMP388(); - - static I2CSPIDriverBase *instantiate(const I2CSPIDriverConfig &config, int runtime_instance); static void print_usage(); - virtual int init(); + int init() override; + void print_status() override; - void print_status(); + void RunImpl(); - void RunImpl(); private: - static constexpr uint8_t osr_t{BMP3_OVERSAMPLING_2X}; // oversampling rate, temperature - static constexpr uint8_t osr_p{BMP3_OVERSAMPLING_16X}; // oversampling rate, pressure - static constexpr uint8_t odr{BMP3_ODR_50_HZ}; // output data rate (not used) - static constexpr uint8_t iir_coef{BMP3_IIR_FILTER_DISABLE}; // IIR coefficient + int probe() override; + + bool Configure(); + bool Measure(); + int Collect(); + void Failure(); + + bool ReadCalibration(); + + int64_t CompensateTemperature(uint32_t uncomp_temp, int64_t &t_lin) const; + uint64_t CompensatePressure(uint32_t uncomp_press, int64_t t_lin) const; + + bool RegisterRead(Register reg, void *buf, size_t len); + uint8_t RegisterRead(Register reg); + bool RegisterWrite(Register reg, uint8_t value); PX4Barometer _px4_baro; - IBMP388 *_interface{nullptr}; - unsigned _measure_interval{0}; // interval in microseconds needed to measure + perf_counter_t _bad_transfer_perf{perf_alloc(PC_COUNT, MODULE_NAME": bad transfer")}; + perf_counter_t _conversion_timeout_perf{perf_alloc(PC_COUNT, MODULE_NAME": conversion timeout")}; + perf_counter_t _reset_perf{perf_alloc(PC_COUNT, MODULE_NAME": reset")}; - perf_counter_t _sample_perf; - perf_counter_t _measure_perf; - perf_counter_t _comms_errors; + enum class STATE : uint8_t { + RESET, + WAIT_FOR_RESET, + CONFIGURE, + MEASURE, + COLLECT, + } _state{STATE::RESET}; - calibration_s *_cal {nullptr}; // stored calibration constants + hrt_abstime _measure_timestamp{0}; + int _failure_count{0}; - bool _collect_phase{false}; + uint8_t _chip_id{0}; + uint8_t _rev_id{0}; + uint32_t _conversion_time_typ_us{0}; + uint32_t _conversion_time_max_us{0}; - uint8_t _chip_id{0}; - uint8_t _chip_rev_id{0}; - - void start(); - int measure(); - int collect(); //get results and publish - uint32_t get_measurement_time(); - - bool soft_reset(); - bool get_calib_data(); - bool validate_trimming_param(); - bool set_sensor_settings(); - bool set_op_mode(uint8_t op_mode); - - bool get_sensor_data(uint8_t sensor_comp, struct bmp3_data *comp_data); - bool compensate_data(uint8_t sensor_comp, const struct bmp3_uncomp_data *uncomp_data, struct bmp3_data *comp_data); + calib_data _calib{}; }; - - -/* interface factories */ -extern IBMP388 *bmp388_spi_interface(uint8_t busnum, uint32_t device, int bus_frequency, spi_mode_e spi_mode); -extern IBMP388 *bmp388_i2c_interface(uint8_t busnum, uint32_t device, int bus_frequency); diff --git a/src/drivers/barometer/bmp388/bmp388_i2c.cpp b/src/drivers/barometer/bmp388/bmp388_i2c.cpp deleted file mode 100644 index 8d47b8a09bc..00000000000 --- a/src/drivers/barometer/bmp388/bmp388_i2c.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/**************************************************************************** - * - * Copyright (c) 2019 PX4 Development Team. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name PX4 nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/** - * @file bmp388_i2c.cpp - * - * I2C interface for BMP388 - */ - -#include - -#include "bmp388.h" - -class BMP388_I2C: public device::I2C, public IBMP388 -{ -public: - BMP388_I2C(uint8_t bus, uint32_t device, int bus_frequency); - virtual ~BMP388_I2C() = default; - - int init(); - - int get_reg(uint8_t addr, uint8_t *value); - int get_reg_buf(uint8_t addr, uint8_t *buf, uint8_t len); - int set_reg(uint8_t value, uint8_t addr); - calibration_s *get_calibration(uint8_t addr); - - uint32_t get_device_id() const override { return device::I2C::get_device_id(); } - - uint8_t get_device_address() const override { return device::I2C::get_device_address(); } - - void set_device_type(uint8_t devtype); -private: - struct calibration_s _cal; -}; - -IBMP388 *bmp388_i2c_interface(uint8_t busnum, uint32_t device, int bus_frequency) -{ - return new BMP388_I2C(busnum, device, bus_frequency); -} - -BMP388_I2C::BMP388_I2C(uint8_t bus, uint32_t device, int bus_frequency) : - I2C(DRV_BARO_DEVTYPE_BMP388, MODULE_NAME, bus, device, bus_frequency) -{ - _retries = 1; -} - -int BMP388_I2C::init() -{ - for (int i = 0; i < 10; i++) { - if (I2C::init() == OK) { - return OK; - } - - px4_usleep(10000); - } - - return PX4_ERROR; -} - -int BMP388_I2C::get_reg(uint8_t addr, uint8_t *value) -{ - const uint8_t cmd = (uint8_t)(addr); - return transfer(&cmd, sizeof(cmd), value, 1); -} - -int BMP388_I2C::get_reg_buf(uint8_t addr, uint8_t *buf, uint8_t len) -{ - const uint8_t cmd = (uint8_t)(addr); - return transfer(&cmd, sizeof(cmd), buf, len); -} - -int BMP388_I2C::set_reg(uint8_t value, uint8_t addr) -{ - uint8_t cmd[2] = { (uint8_t)(addr), value}; - return transfer(cmd, sizeof(cmd), nullptr, 0); -} - -calibration_s *BMP388_I2C::get_calibration(uint8_t addr) -{ - const uint8_t cmd = (uint8_t)(addr); - - if (transfer(&cmd, sizeof(cmd), (uint8_t *)&_cal, sizeof(struct calibration_s)) == OK) { - return &(_cal); - - } else { - return nullptr; - } -} - -void BMP388_I2C::set_device_type(uint8_t devtype) -{ - device::Device::set_device_type(devtype); -} diff --git a/src/drivers/barometer/bmp388/bmp388_main.cpp b/src/drivers/barometer/bmp388/bmp388_main.cpp index fb82cc30e8f..3420d1e1f22 100644 --- a/src/drivers/barometer/bmp388/bmp388_main.cpp +++ b/src/drivers/barometer/bmp388/bmp388_main.cpp @@ -1,6 +1,6 @@ /**************************************************************************** * - * Copyright (c) 2019-2021 PX4 Development Team. All rights reserved. + * Copyright (c) 2019-2026 PX4 Development Team. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -31,67 +31,29 @@ * ****************************************************************************/ -#include -#include -#include - #include "bmp388.h" -void -BMP388::print_usage() +#include +#include + +#include + +void BMP388::print_usage() { PRINT_MODULE_USAGE_NAME("bmp388", "driver"); PRINT_MODULE_USAGE_SUBCATEGORY("baro"); PRINT_MODULE_USAGE_COMMAND("start"); - PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, true); + PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, false); PRINT_MODULE_USAGE_PARAMS_I2C_ADDRESS(0x76); PRINT_MODULE_USAGE_DEFAULT_COMMANDS(); } -I2CSPIDriverBase *BMP388::instantiate(const I2CSPIDriverConfig &config, int runtime_instance) -{ - IBMP388 *interface = nullptr; - - if (config.bus_type == BOARD_I2C_BUS) { - interface = bmp388_i2c_interface(config.bus, config.i2c_address, config.bus_frequency); - - } else if (config.bus_type == BOARD_SPI_BUS) { - interface = bmp388_spi_interface(config.bus, config.spi_devid, config.bus_frequency, config.spi_mode); - } - - if (interface == nullptr) { - PX4_ERR("failed creating interface for bus %i (devid 0x%" PRIx32 ")", config.bus, config.spi_devid); - return nullptr; - } - - if (interface->init() != OK) { - delete interface; - PX4_DEBUG("no device on bus %i (devid 0x%" PRIx32 ")", config.bus, config.spi_devid); - return nullptr; - } - - BMP388 *dev = new BMP388(config, interface); - - if (dev == nullptr) { - delete interface; - return nullptr; - } - - if (OK != dev->init()) { - delete dev; - return nullptr; - } - - return dev; -} - extern "C" int bmp388_main(int argc, char *argv[]) { using ThisDriver = BMP388; - BusCLIArguments cli{true, true}; + BusCLIArguments cli{true, false}; cli.i2c_address = 0x76; - cli.default_i2c_frequency = 100 * 1000; - cli.default_spi_frequency = 10 * 1000 * 1000; + cli.default_i2c_frequency = I2C_SPEED; const char *verb = cli.parseDefaultArguments(argc, argv); diff --git a/src/drivers/barometer/bmp388/bmp388_spi.cpp b/src/drivers/barometer/bmp388/bmp388_spi.cpp deleted file mode 100644 index 2970d2bc00b..00000000000 --- a/src/drivers/barometer/bmp388/bmp388_spi.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/**************************************************************************** - * - * Copyright (c) 2019 PX4 Development Team. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name PX4 nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/** - * @file bmp388_spi.cpp - * - * SPI interface for BMP388 (NOTE: untested!) - */ - -#include - -#include "bmp388.h" - - -/* SPI protocol address bits */ -#define DIR_READ (1<<7) //for set -#define DIR_WRITE ~(1<<7) //for clear - -#pragma pack(push,1) -struct spi_data_s { - uint8_t addr; - struct data_s data; -}; - -struct spi_calibration_s { - uint8_t addr; - struct calibration_s cal; -}; -#pragma pack(pop) - -class BMP388_SPI: public device::SPI, public IBMP388 -{ -public: - BMP388_SPI(uint8_t bus, uint32_t device, int bus_frequency, spi_mode_e spi_mode); - virtual ~BMP388_SPI() = default; - - int init(); - - int get_reg(uint8_t addr, uint8_t *value); - int get_reg_buf(uint8_t addr, uint8_t *buf, uint8_t len); - int set_reg(uint8_t value, uint8_t addr); - calibration_s *get_calibration(uint8_t addr); - - uint32_t get_device_id() const override { return device::SPI::get_device_id(); } - - uint8_t get_device_address() const override { return device::SPI::get_device_address(); } - - void set_device_type(uint8_t devtype); -private: - spi_calibration_s _cal; -}; - -IBMP388 *bmp388_spi_interface(uint8_t busnum, uint32_t device, int bus_frequency, spi_mode_e spi_mode) -{ - return new BMP388_SPI(busnum, device, bus_frequency, spi_mode); -} - -BMP388_SPI::BMP388_SPI(uint8_t bus, uint32_t device, int bus_frequency, spi_mode_e spi_mode) : - SPI(DRV_BARO_DEVTYPE_BMP388, MODULE_NAME, bus, device, spi_mode, bus_frequency) -{ -} - -int BMP388_SPI::init() -{ - return SPI::init(); -}; - -int BMP388_SPI::get_reg(uint8_t addr, uint8_t *value) -{ - uint8_t cmd[2] = { (uint8_t)(addr | DIR_READ), 0}; //set MSB bit - int ret = transfer(&cmd[0], &cmd[0], 2); - - if (ret == OK) { - *value = cmd[1]; - } - - return ret; -} - -int BMP388_SPI::get_reg_buf(uint8_t addr, uint8_t *buf, uint8_t len) -{ - uint8_t cmd[1] = {(uint8_t)(addr | DIR_READ)}; - return transfer(&cmd[0], buf, len); -} - -int BMP388_SPI::set_reg(uint8_t value, uint8_t addr) -{ - uint8_t cmd[2] = { (uint8_t)(addr & DIR_WRITE), value}; //clear MSB bit - return transfer(&cmd[0], nullptr, 2); -} - -calibration_s *BMP388_SPI::get_calibration(uint8_t addr) -{ - _cal.addr = addr | DIR_READ; - - if (transfer((uint8_t *)&_cal, (uint8_t *)&_cal, sizeof(struct spi_calibration_s)) == OK) { - return &(_cal.cal); - - } else { - return nullptr; - } -} - -void BMP388_SPI::set_device_type(uint8_t devtype) -{ - device::Device::set_device_type(devtype); -}