From b3a9417d118844c9ce2c5b98e1438870d16c338c Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Fri, 21 May 2021 23:38:00 -0300 Subject: [PATCH 01/32] Add dps310 to F722 matek board --- src/main/drivers/barometer/barometer_dps310.c | 390 ++++++++++++++++++ src/main/drivers/barometer/barometer_dps310.h | 29 ++ src/main/sensors/barometer.c | 8 +- src/main/target/MAMBAF722/target.h | 2 + 4 files changed, 428 insertions(+), 1 deletion(-) create mode 100644 src/main/drivers/barometer/barometer_dps310.c create mode 100644 src/main/drivers/barometer/barometer_dps310.h diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c new file mode 100644 index 0000000000..5fee6f1b8f --- /dev/null +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -0,0 +1,390 @@ +/* + * This file is part of Cleanflight, Betaflight and INAV. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Alternatively, the contents of this file may be used under the terms + * of the GNU General Public License Version 3, as described below: + * + * This file is free software: you may copy, redistribute and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + * + * Copyright: INAVFLIGHT + */ + +#include +#include +#include + +#include "platform.h" + +#include "build/build_config.h" +#include "build/debug.h" +#include "common/utils.h" + +#include "drivers/io.h" +#include "drivers/bus.h" +#include "drivers/bus_spi.h" +#include "drivers/time.h" +#include "drivers/barometer/barometer.h" +#include "drivers/barometer/barometer_dps310.h" +#include "drivers/resource.h" + +// 10 MHz max SPI frequency +#define DPS310_MAX_SPI_CLK_HZ 10000000 + +#if defined(USE_BARO) && defined(USE_BARO_DPS310) + +#define DPS310_I2C_ADDR 0x76 + +#define DPS310_REG_PSR_B2 0x00 +#define DPS310_REG_PSR_B1 0x01 +#define DPS310_REG_PSR_B0 0x02 +#define DPS310_REG_TMP_B2 0x03 +#define DPS310_REG_TMP_B1 0x04 +#define DPS310_REG_TMP_B0 0x05 +#define DPS310_REG_PRS_CFG 0x06 +#define DPS310_REG_TMP_CFG 0x07 +#define DPS310_REG_MEAS_CFG 0x08 +#define DPS310_REG_CFG_REG 0x09 + +#define DPS310_REG_RESET 0x0C +#define DPS310_REG_ID 0x0D + +#define DPS310_REG_COEF 0x10 +#define DPS310_REG_COEF_SRCE 0x28 + + +#define DPS310_ID_REV_AND_PROD_ID (0x10) + +#define DPS310_RESET_BIT_SOFT_RST (0x09) // 0b1001 + +#define DPS310_MEAS_CFG_COEF_RDY (1 << 7) +#define DPS310_MEAS_CFG_SENSOR_RDY (1 << 6) +#define DPS310_MEAS_CFG_TMP_RDY (1 << 5) +#define DPS310_MEAS_CFG_PRS_RDY (1 << 4) +#define DPS310_MEAS_CFG_MEAS_CTRL_CONT (0x7) + +#define DPS310_PRS_CFG_BIT_PM_RATE_32HZ (0x50) // 101 - 32 measurements pr. sec. +#define DPS310_PRS_CFG_BIT_PM_PRC_16 (0x04) // 0100 - 16 times (Standard). + +#define DPS310_TMP_CFG_BIT_TMP_EXT (0x80) // +#define DPS310_TMP_CFG_BIT_TMP_RATE_32HZ (0x50) // 101 - 32 measurements pr. sec. +#define DPS310_TMP_CFG_BIT_TMP_PRC_16 (0x04) // 0100 - 16 times (Standard). + +#define DPS310_CFG_REG_BIT_P_SHIFT (0x04) +#define DPS310_CFG_REG_BIT_T_SHIFT (0x08) + +#define DPS310_COEF_SRCE_BIT_TMP_COEF_SRCE (0x80) + +typedef struct { + int16_t c0; // 12bit + int16_t c1; // 12bit + int32_t c00; // 20bit + int32_t c10; // 20bit + int16_t c01; // 16bit + int16_t c11; // 16bit + int16_t c20; // 16bit + int16_t c21; // 16bit + int16_t c30; // 16bit +} calibrationCoefficients_t; + +typedef struct { + calibrationCoefficients_t calib; + float pressure; // Pa + float temperature; // DegC +} baroState_t; + +static baroState_t baroState; + +#define busReadBuf busReadRegisterBuffer +#define busWrite busWriteRegister + +static uint8_t buf[6]; + +// Helper functions +static uint8_t registerRead(busDevice_t * busDev, uint8_t reg) +{ + return busReadRegister(busDev, reg); +} + +static void registerWrite(busDevice_t * busDev, uint8_t reg, uint8_t value) +{ + busWrite(busDev, reg, value); +} + +static void registerSetBits(busDevice_t * busDev, uint8_t reg, uint8_t setbits) +{ + uint8_t val = registerRead(busDev, reg); + + if ((val & setbits) != setbits) { + val |= setbits; + registerWrite(busDev, reg, val); + } +} + +static int32_t getTwosComplement(uint32_t raw, uint8_t length) +{ + if (raw & ((int)1 << (length - 1))) { + return ((int32_t)raw) - ((int32_t)1 << length); + } + else { + return raw; + } +} + +static bool deviceConfigure(busDevice_t * busDev) +{ + // Trigger a chip reset + registerSetBits(busDev, DPS310_REG_RESET, DPS310_RESET_BIT_SOFT_RST); + + // Sleep 40ms + delay(40); + + uint8_t status = registerRead(busDev, DPS310_REG_MEAS_CFG); + + // Check if coefficients are available + if ((status & DPS310_MEAS_CFG_COEF_RDY) == 0) { + return false; + } + + // Check if sensor initialization is complete + if ((status & DPS310_MEAS_CFG_SENSOR_RDY) == 0) { + return false; + } + + // 1. Read the pressure calibration coefficients (c00, c10, c20, c30, c01, c11, and c21) from the Calibration Coefficient register. + // Note: The coefficients read from the coefficient register are 2's complement numbers. + + // Do the read of the coefficients in multiple parts, as the chip will return a read failure when trying to read all at once over I2C. +#define COEFFICIENT_LENGTH 18 +#define READ_LENGTH (COEFFICIENT_LENGTH / 2) + + uint8_t coef[COEFFICIENT_LENGTH]; + if (!busReadBuf(busDev, DPS310_REG_COEF, coef, READ_LENGTH)) { + return false; + } + if (!busReadBuf(busDev, DPS310_REG_COEF + READ_LENGTH, coef + READ_LENGTH, COEFFICIENT_LENGTH - READ_LENGTH)) { + return false; + } + + // 0x11 c0 [3:0] + 0x10 c0 [11:4] + baroState.calib.c0 = getTwosComplement(((uint32_t)coef[0] << 4) | (((uint32_t)coef[1] >> 4) & 0x0F), 12); + + // 0x11 c1 [11:8] + 0x12 c1 [7:0] + baroState.calib.c1 = getTwosComplement((((uint32_t)coef[1] & 0x0F) << 8) | (uint32_t)coef[2], 12); + + // 0x13 c00 [19:12] + 0x14 c00 [11:4] + 0x15 c00 [3:0] + baroState.calib.c00 = getTwosComplement(((uint32_t)coef[3] << 12) | ((uint32_t)coef[4] << 4) | (((uint32_t)coef[5] >> 4) & 0x0F), 20); + + // 0x15 c10 [19:16] + 0x16 c10 [15:8] + 0x17 c10 [7:0] + baroState.calib.c10 = getTwosComplement((((uint32_t)coef[5] & 0x0F) << 16) | ((uint32_t)coef[6] << 8) | (uint32_t)coef[7], 20); + + // 0x18 c01 [15:8] + 0x19 c01 [7:0] + baroState.calib.c01 = getTwosComplement(((uint32_t)coef[8] << 8) | (uint32_t)coef[9], 16); + + // 0x1A c11 [15:8] + 0x1B c11 [7:0] + baroState.calib.c11 = getTwosComplement(((uint32_t)coef[8] << 8) | (uint32_t)coef[9], 16); + + // 0x1C c20 [15:8] + 0x1D c20 [7:0] + baroState.calib.c20 = getTwosComplement(((uint32_t)coef[12] << 8) | (uint32_t)coef[13], 16); + + // 0x1E c21 [15:8] + 0x1F c21 [7:0] + baroState.calib.c21 = getTwosComplement(((uint32_t)coef[14] << 8) | (uint32_t)coef[15], 16); + + // 0x20 c30 [15:8] + 0x21 c30 [7:0] + baroState.calib.c30 = getTwosComplement(((uint32_t)coef[16] << 8) | (uint32_t)coef[17], 16); + + // PRS_CFG: pressure measurement rate (32 Hz) and oversampling (16 time standard) + registerSetBits(busDev, DPS310_REG_PRS_CFG, DPS310_PRS_CFG_BIT_PM_RATE_32HZ | DPS310_PRS_CFG_BIT_PM_PRC_16); + + // TMP_CFG: temperature measurement rate (32 Hz) and oversampling (16 times) + const uint8_t TMP_COEF_SRCE = registerRead(busDev, DPS310_REG_COEF_SRCE) & DPS310_COEF_SRCE_BIT_TMP_COEF_SRCE; + registerSetBits(busDev, DPS310_REG_TMP_CFG, DPS310_TMP_CFG_BIT_TMP_RATE_32HZ | DPS310_TMP_CFG_BIT_TMP_PRC_16 | TMP_COEF_SRCE); + + // CFG_REG: set pressure and temperature result bit-shift (required when the oversampling rate is >8 times) + registerSetBits(busDev, DPS310_REG_CFG_REG, DPS310_CFG_REG_BIT_T_SHIFT | DPS310_CFG_REG_BIT_P_SHIFT); + + // MEAS_CFG: Continuous pressure and temperature measurement + registerSetBits(busDev, DPS310_REG_MEAS_CFG, DPS310_MEAS_CFG_MEAS_CTRL_CONT); + + return true; +} + +static bool dps310GetUP(baroDev_t *baro) +{ + UNUSED(baro); + + // 2. Choose scaling factors kT (for temperature) and kP (for pressure) based on the chosen precision rate. + // The scaling factors are listed in Table 9. + static float kT = 253952; // 16 times (Standard) + static float kP = 253952; // 16 times (Standard) + + // 3. Read the pressure and temperature result from the registers + + const int32_t Praw = getTwosComplement((buf[0] << 16) + (buf[1] << 8) + buf[2], 24); + const int32_t Traw = getTwosComplement((buf[3] << 16) + (buf[4] << 8) + buf[5], 24); + + // 4. Calculate scaled measurement results. + const float Praw_sc = Praw / kP; + const float Traw_sc = Traw / kT; + + // 5. Calculate compensated measurement results. + const float c00 = baroState.calib.c00; + const float c01 = baroState.calib.c01; + const float c10 = baroState.calib.c10; + const float c11 = baroState.calib.c11; + const float c20 = baroState.calib.c20; + const float c21 = baroState.calib.c21; + const float c30 = baroState.calib.c30; + + const float c0 = baroState.calib.c0; + const float c1 = baroState.calib.c1; + + baroState.pressure = c00 + Praw_sc * (c10 + Praw_sc * (c20 + Praw_sc * c30)) + Traw_sc * c01 + Traw_sc * Praw_sc * (c11 + Praw_sc * c21); + baroState.temperature = c0 * 0.5f + c1 * Traw_sc; + + return true; +} + +static void deviceCalculate(int32_t *pressure, int32_t *temperature) +{ + if (pressure) { + *pressure = baroState.pressure; + } + + if (temperature) { + *temperature = (baroState.temperature * 100); // to centidegrees + } +} + + + +#define DETECTION_MAX_RETRY_COUNT 5 +static bool deviceDetect(busDevice_t * busDev) +{ + for (int retry = 0; retry < DETECTION_MAX_RETRY_COUNT; retry++) { + uint8_t chipId[1]; + + delay(100); + + bool ack = busReadBuf(busDev, DPS310_REG_ID, chipId, 1); + + if (ack && chipId[0] == DPS310_ID_REV_AND_PROD_ID) { + return true; + } + }; + + return false; +} + +static void dps310StartUT(baroDev_t *baro) +{ + UNUSED(baro); +} + +static bool dps310GetUT(baroDev_t *baro) +{ + UNUSED(baro); + + return true; +} + +static void dps310StartUP(baroDev_t *baro) +{ + if (busBusy(&baro->busdev, NULL)) { + return false; + } + + // 1. Kick off read + // No need to poll for data ready as the conversion rate is 32Hz and this is sampling at 20Hz + // Read PSR_B2, PSR_B1, PSR_B0, TMP_B2, TMP_B1, TMP_B0 + busReadRegisterBufferStart(&baro->busdev, DPS310_REG_PSR_B2, buf, 6); + + return true; +} + +static void busDeviceInit(busDevice_t *busdev, resourceOwner_e owner) +{ +#ifdef USE_BARO_SPI_DPS310 + if (busdev->bustype == BUSTYPE_SPI) { + IOHi(busdev->busdev_u.spi.csnPin); // Disable + IOInit(busdev->busdev_u.spi.csnPin, owner, 0); + IOConfigGPIO(busdev->busdev_u.spi.csnPin, IOCFG_OUT_PP); +#ifdef USE_SPI_TRANSACTION + spiBusTransactionInit(busdev, SPI_MODE0_POL_LOW_EDGE_1ST, spiCalculateDivider(DPS310_MAX_SPI_CLK_HZ)); // DPS310 supports Mode 0 or 3 +#else + spiBusSetDivisor(busdev, spiCalculateDivider(DPS310_MAX_SPI_CLK_HZ)); +#endif + } +#else + UNUSED(busdev); + UNUSED(owner); +#endif +} + +static void busDeviceDeInit(busDevice_t *busdev) +{ +#ifdef USE_BARO_SPI_DPS310 + if (busdev->bustype == BUSTYPE_SPI) { + spiPreinitByIO(busdev->busdev_u.spi.csnPin); + } +#else + UNUSED(busdev); +#endif +} + +bool baroDPS310Detect(baroDev_t *baro) +{ + busDevice_t *busdev = &baro->busdev; + bool defaultAddressApplied = false; + + busDeviceInit(&baro->busdev, OWNER_BARO_CS); + + if ((busdev->bustype == BUSTYPE_I2C) && (busdev->busdev_u.i2c.address == 0)) { + // Default address for BMP280 + busdev->busdev_u.i2c.address = DPS310_I2C_ADDR; + defaultAddressApplied = true; + } + + if (!deviceDetect(busdev)) { + busDeviceDeInit(busdev); + if (defaultAddressApplied) { + busdev->busdev_u.i2c.address = 0; + } + return false; + } + + if (!deviceConfigure(busdev)) { + busDeviceDeInit(busdev); + return false; + } + + busDeviceRegister(busdev); + + baro->ut_delay = 0; + baro->start_ut = dps310StartUT; + baro->get_ut = dps310GetUT; + + baro->up_delay = 45000; // 45ms delay plus 5 1ms cycles 50ms + baro->start_up = dps310StartUP; + baro->get_up = dps310GetUP; + + baro->calculate = deviceCalculate; + + return true; +} + +#endif diff --git a/src/main/drivers/barometer/barometer_dps310.h b/src/main/drivers/barometer/barometer_dps310.h new file mode 100644 index 0000000000..eccf15dfbc --- /dev/null +++ b/src/main/drivers/barometer/barometer_dps310.h @@ -0,0 +1,29 @@ +/* + * This file is part of Cleanflight, Betaflight and INAV. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Alternatively, the contents of this file may be used under the terms + * of the GNU General Public License Version 3, as described below: + * + * This file is free software: you may copy, redistribute and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + * + * Copyright: INAVFLIGHT + */ + +#pragma once + +bool baroDPS310Detect(baroDev_t *baro); diff --git a/src/main/sensors/barometer.c b/src/main/sensors/barometer.c index 5056d2e0c4..381fa72b73 100644 --- a/src/main/sensors/barometer.c +++ b/src/main/sensors/barometer.c @@ -67,7 +67,13 @@ void pgResetFn_barometerConfig(barometerConfig_t *barometerConfig) { // a. Precedence is in the order of popularity; BMP280, MS5611 then BMP085, then // b. If SPI variant is specified, it is likely onboard, so take it. #if !(defined(DEFAULT_BARO_SPI_BMP280) || defined(DEFAULT_BARO_BMP280) || defined(DEFAULT_BARO_SPI_MS5611) || defined(DEFAULT_BARO_MS5611) || defined(DEFAULT_BARO_BMP085) || defined(DEFAULT_BARO_SPI_LPS) || defined(DEFAULT_BARO_SPI_QMP6988) || defined(DEFAULT_BARO_QMP6988)) -#if defined(USE_BARO_BMP280) || defined(USE_BARO_SPI_BMP280) +#if defined(USE_BARO_DPS310) || defined(USE_BARO_SPI_DPS310) +#if defined(USE_BARO_SPI_DPS310) +#define DEFAULT_BARO_SPI_DPS310 +#else +#define DEFAULT_BARO_DPS310 +#endif +#elif defined(USE_BARO_BMP280) || defined(USE_BARO_SPI_BMP280) #if defined(USE_BARO_SPI_BMP280) #define DEFAULT_BARO_SPI_BMP280 #else diff --git a/src/main/target/MAMBAF722/target.h b/src/main/target/MAMBAF722/target.h index f4b0a3fb82..b55a0734df 100644 --- a/src/main/target/MAMBAF722/target.h +++ b/src/main/target/MAMBAF722/target.h @@ -67,6 +67,8 @@ #define USE_BARO_MS5611 #define USE_BARO_BMP280 #define USE_BARO_BMP085 +#define USE_BARO_DPS310 +#define USE_BARO_SPI_DPS310 #define USE_I2C #define USE_I2C_DEVICE_1 From 93dab35159d03f5bfc1c2fe2da401209bb497c4d Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Fri, 21 May 2021 23:45:58 -0300 Subject: [PATCH 02/32] parse DPS310 to hardware detect --- src/main/sensors/barometer.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/sensors/barometer.c b/src/main/sensors/barometer.c index 381fa72b73..5934af2020 100644 --- a/src/main/sensors/barometer.c +++ b/src/main/sensors/barometer.c @@ -40,6 +40,7 @@ #include "drivers/barometer/barometer_fake.h" #include "drivers/barometer/barometer_ms5611.h" #include "drivers/barometer/barometer_lps.h" +#include "drivers/barometer/barometer_dps310.h" #include "fc/runtime_config.h" @@ -151,7 +152,7 @@ static uint32_t baroPressureSum = 0; bool baroDetect(baroDev_t *dev, baroSensor_e baroHardwareToUse) { // Detect what pressure sensors are available. baro->update() is set to sensor-specific update function baroSensor_e baroHardware = baroHardwareToUse; -#if !defined(USE_BARO_BMP085) && !defined(USE_BARO_MS5611) && !defined(USE_BARO_SPI_MS5611) && !defined(USE_BARO_BMP280) && !defined(USE_BARO_SPI_BMP280)&& !defined(USE_BARO_QMP6988) && !defined(USE_BARO_SPI_QMP6988) +#if !defined(USE_BARO_BMP085) && !defined(USE_BARO_MS5611) && !defined(USE_BARO_SPI_MS5611) && !defined(USE_BARO_BMP280) && !defined(USE_BARO_SPI_BMP280)&& !defined(USE_BARO_QMP6988) && !defined(USE_BARO_SPI_QMP6988) && !defined(USE_BARO_SPI_DPS310) UNUSED(dev); #endif switch (barometerConfig()->baro_bustype) { @@ -207,6 +208,16 @@ bool baroDetect(baroDev_t *dev, baroSensor_e baroHardwareToUse) { baroHardware = BARO_LPS; break; } +#endif + FALLTHROUGH; + case BARO_DPS310: +#if defined(USE_BARO_DPS310) || defined(USE_BARO_SPI_DPS310) + { + if (baroDPS310Detect(dev)) { + baroHardware = BARO_DPS310; + break; + } + } #endif FALLTHROUGH; case BARO_BMP280: From 9dd3944fcfe099ee2da6272459a775e22eee0768 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Fri, 21 May 2021 23:47:54 -0300 Subject: [PATCH 03/32] parse DPS310 to settings.c --- src/main/interface/settings.c | 2 +- src/main/sensors/barometer.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/interface/settings.c b/src/main/interface/settings.c index cfa753d80f..9b7a347ef1 100644 --- a/src/main/interface/settings.c +++ b/src/main/interface/settings.c @@ -119,7 +119,7 @@ const char * const lookupTableGyroHardware[] = { #if defined(USE_SENSOR_NAMES) || defined(USE_BARO) // sync with baroSensor_e const char * const lookupTableBaroHardware[] = { - "AUTO", "NONE", "BMP085", "MS5611", "BMP280", "LPS", "QMP6988" + "AUTO", "NONE", "BMP085", "MS5611", "BMP280", "LPS", "QMP6988", "DPS310" }; #endif #if defined(USE_SENSOR_NAMES) || defined(USE_MAG) diff --git a/src/main/sensors/barometer.h b/src/main/sensors/barometer.h index 11533a57b3..e48df1ed90 100644 --- a/src/main/sensors/barometer.h +++ b/src/main/sensors/barometer.h @@ -30,7 +30,8 @@ typedef enum { BARO_MS5611 = 3, BARO_BMP280 = 4, BARO_LPS = 5, - BARO_QMP6988 = 6 + BARO_QMP6988 = 6, + BARO_DPS310 = 7, } baroSensor_e; #define BARO_SAMPLE_COUNT_MAX 48 From 3d83deda33662f9da8e849b4be0eb8175c317988 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Fri, 21 May 2021 23:57:02 -0300 Subject: [PATCH 04/32] correct name of i2c and spi bus functions that are different from betaflight --- src/main/drivers/barometer/barometer_dps310.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index 5fee6f1b8f..2379668a05 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -311,7 +311,7 @@ static void dps310StartUP(baroDev_t *baro) // 1. Kick off read // No need to poll for data ready as the conversion rate is 32Hz and this is sampling at 20Hz // Read PSR_B2, PSR_B1, PSR_B0, TMP_B2, TMP_B1, TMP_B0 - busReadRegisterBufferStart(&baro->busdev, DPS310_REG_PSR_B2, buf, 6); + busReadRegisterBuffer(&baro->busdev, DPS310_REG_PSR_B2, buf, 6); return true; } @@ -326,7 +326,7 @@ static void busDeviceInit(busDevice_t *busdev, resourceOwner_e owner) #ifdef USE_SPI_TRANSACTION spiBusTransactionInit(busdev, SPI_MODE0_POL_LOW_EDGE_1ST, spiCalculateDivider(DPS310_MAX_SPI_CLK_HZ)); // DPS310 supports Mode 0 or 3 #else - spiBusSetDivisor(busdev, spiCalculateDivider(DPS310_MAX_SPI_CLK_HZ)); + spiSetDivisor(busdev, spiCalculateDivider(DPS310_MAX_SPI_CLK_HZ)); #endif } #else @@ -339,7 +339,7 @@ static void busDeviceDeInit(busDevice_t *busdev) { #ifdef USE_BARO_SPI_DPS310 if (busdev->bustype == BUSTYPE_SPI) { - spiPreinitByIO(busdev->busdev_u.spi.csnPin); + spiPreinitCsByIO(busdev->busdev_u.spi.csnPin); } #else UNUSED(busdev); From 9b2c18f776e254fbf734af08596d2c67928307c6 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sat, 22 May 2021 00:24:36 -0300 Subject: [PATCH 05/32] add new function to bus_spi.c to use with DPS310 --- src/main/drivers/barometer/barometer_dps310.c | 4 ---- src/main/drivers/bus_spi.c | 19 +++++++++++++++++++ src/main/drivers/bus_spi.h | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index 2379668a05..9d65e49901 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -323,11 +323,7 @@ static void busDeviceInit(busDevice_t *busdev, resourceOwner_e owner) IOHi(busdev->busdev_u.spi.csnPin); // Disable IOInit(busdev->busdev_u.spi.csnPin, owner, 0); IOConfigGPIO(busdev->busdev_u.spi.csnPin, IOCFG_OUT_PP); -#ifdef USE_SPI_TRANSACTION - spiBusTransactionInit(busdev, SPI_MODE0_POL_LOW_EDGE_1ST, spiCalculateDivider(DPS310_MAX_SPI_CLK_HZ)); // DPS310 supports Mode 0 or 3 -#else spiSetDivisor(busdev, spiCalculateDivider(DPS310_MAX_SPI_CLK_HZ)); -#endif } #else UNUSED(busdev); diff --git a/src/main/drivers/bus_spi.c b/src/main/drivers/bus_spi.c index 93cec8510a..236ed0b0b2 100644 --- a/src/main/drivers/bus_spi.c +++ b/src/main/drivers/bus_spi.c @@ -251,4 +251,23 @@ void spiBusSetInstance(busDevice_t *bus, SPI_TypeDef *instance) { bus->bustype = BUSTYPE_SPI; bus->busdev_u.spi.instance = instance; } + +uint16_t spiCalculateDivider(uint32_t freq) +{ +#if defined(STM32F4) || defined(STM32G4) || defined(STM32F7) + uint32_t spiClk = SystemCoreClock / 2; +#elif defined(STM32H7) + uint32_t spiClk = 100000000; +#else +#error "Base SPI clock not defined for this architecture" +#endif + + uint16_t divisor = 2; + + spiClk >>= 1; + + for (; (spiClk > freq) && (divisor < 256); divisor <<= 1, spiClk >>= 1); + + return divisor; +} #endif diff --git a/src/main/drivers/bus_spi.h b/src/main/drivers/bus_spi.h index 12d8d068a7..a487b089cb 100644 --- a/src/main/drivers/bus_spi.h +++ b/src/main/drivers/bus_spi.h @@ -118,6 +118,7 @@ bool spiBusWriteRegister(const busDevice_t *bus, uint8_t reg, uint8_t data); bool spiBusReadRegisterBuffer(const busDevice_t *bus, uint8_t reg, uint8_t *data, uint8_t length); uint8_t spiBusReadRegister(const busDevice_t *bus, uint8_t reg); void spiBusSetInstance(busDevice_t *bus, SPI_TypeDef *instance); +uint16_t spiCalculateDivider(uint32_t freq); struct spiPinConfig_s; void spiPinConfigure(const struct spiPinConfig_s *pConfig); From 961a429a760bd87c3752f08557496cbe34c940a8 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sat, 22 May 2021 00:28:53 -0300 Subject: [PATCH 06/32] remove new function of bus_spi.c and use the SPI_CLOCK_STANDARD --- src/main/drivers/barometer/barometer_dps310.c | 2 +- src/main/drivers/bus_spi.c | 19 ------------------- src/main/drivers/bus_spi.h | 1 - 3 files changed, 1 insertion(+), 21 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index 9d65e49901..9cffd06096 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -323,7 +323,7 @@ static void busDeviceInit(busDevice_t *busdev, resourceOwner_e owner) IOHi(busdev->busdev_u.spi.csnPin); // Disable IOInit(busdev->busdev_u.spi.csnPin, owner, 0); IOConfigGPIO(busdev->busdev_u.spi.csnPin, IOCFG_OUT_PP); - spiSetDivisor(busdev, spiCalculateDivider(DPS310_MAX_SPI_CLK_HZ)); + spiSetDivisor(busdev, SPI_CLOCK_STANDARD); } #else UNUSED(busdev); diff --git a/src/main/drivers/bus_spi.c b/src/main/drivers/bus_spi.c index 236ed0b0b2..93cec8510a 100644 --- a/src/main/drivers/bus_spi.c +++ b/src/main/drivers/bus_spi.c @@ -251,23 +251,4 @@ void spiBusSetInstance(busDevice_t *bus, SPI_TypeDef *instance) { bus->bustype = BUSTYPE_SPI; bus->busdev_u.spi.instance = instance; } - -uint16_t spiCalculateDivider(uint32_t freq) -{ -#if defined(STM32F4) || defined(STM32G4) || defined(STM32F7) - uint32_t spiClk = SystemCoreClock / 2; -#elif defined(STM32H7) - uint32_t spiClk = 100000000; -#else -#error "Base SPI clock not defined for this architecture" -#endif - - uint16_t divisor = 2; - - spiClk >>= 1; - - for (; (spiClk > freq) && (divisor < 256); divisor <<= 1, spiClk >>= 1); - - return divisor; -} #endif diff --git a/src/main/drivers/bus_spi.h b/src/main/drivers/bus_spi.h index a487b089cb..12d8d068a7 100644 --- a/src/main/drivers/bus_spi.h +++ b/src/main/drivers/bus_spi.h @@ -118,7 +118,6 @@ bool spiBusWriteRegister(const busDevice_t *bus, uint8_t reg, uint8_t data); bool spiBusReadRegisterBuffer(const busDevice_t *bus, uint8_t reg, uint8_t *data, uint8_t length); uint8_t spiBusReadRegister(const busDevice_t *bus, uint8_t reg); void spiBusSetInstance(busDevice_t *bus, SPI_TypeDef *instance); -uint16_t spiCalculateDivider(uint32_t freq); struct spiPinConfig_s; void spiPinConfigure(const struct spiPinConfig_s *pConfig); From 67ca5634ea1d14467086c1105596032a49b86fd6 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sat, 22 May 2021 12:26:20 -0300 Subject: [PATCH 07/32] remove owner and #define --- src/main/drivers/barometer/barometer_dps310.c | 2 +- src/main/target/MAMBAF722/target.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index 9cffd06096..6f946fefeb 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -316,7 +316,7 @@ static void dps310StartUP(baroDev_t *baro) return true; } -static void busDeviceInit(busDevice_t *busdev, resourceOwner_e owner) +static void busDeviceInit(busDevice_t *busdev) { #ifdef USE_BARO_SPI_DPS310 if (busdev->bustype == BUSTYPE_SPI) { diff --git a/src/main/target/MAMBAF722/target.h b/src/main/target/MAMBAF722/target.h index b55a0734df..8f5a4249fe 100644 --- a/src/main/target/MAMBAF722/target.h +++ b/src/main/target/MAMBAF722/target.h @@ -68,7 +68,6 @@ #define USE_BARO_BMP280 #define USE_BARO_BMP085 #define USE_BARO_DPS310 -#define USE_BARO_SPI_DPS310 #define USE_I2C #define USE_I2C_DEVICE_1 From 4ad909ecf7a438aea0aee33959e10670e258e0f2 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sat, 22 May 2021 20:36:31 -0300 Subject: [PATCH 08/32] add extension in makefile --- src/main/target/MAMBAF722/target.mk | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/target/MAMBAF722/target.mk b/src/main/target/MAMBAF722/target.mk index 916eedfa23..9045434025 100644 --- a/src/main/target/MAMBAF722/target.mk +++ b/src/main/target/MAMBAF722/target.mk @@ -7,6 +7,7 @@ TARGET_SRC = \ drivers/barometer/barometer_bmp085.c \ drivers/barometer/barometer_bmp280.c \ drivers/barometer/barometer_ms5611.c \ + drivers/barometer/barometer_dps310.h \ drivers/compass/compass_hmc5883l.c \ drivers/compass/compass_qmc5883l.c \ drivers/max7456.c From 6cdb8905b1938151baf25d1c0ed1bdfa98ef272a Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sat, 22 May 2021 20:48:12 -0300 Subject: [PATCH 09/32] fix --- make/source.mk | 1 + src/main/target/MAMBAF722/target.mk | 1 - src/main/target/MATEKF722/target.mk | 1 + 3 files changed, 2 insertions(+), 1 deletion(-) diff --git a/make/source.mk b/make/source.mk index 88ee3ece83..c91383e06c 100644 --- a/make/source.mk +++ b/make/source.mk @@ -252,6 +252,7 @@ SIZE_OPTIMISED_SRC := $(SIZE_OPTIMISED_SRC) \ drivers/barometer/barometer_bmp280.c \ drivers/barometer/barometer_fake.c \ drivers/barometer/barometer_ms5611.c \ + drivers/barometer/barometer_dps310.h \ drivers/barometer/barometer_lps.c \ drivers/barometer/barometer_qmp6988.c \ drivers/beesign.c \ diff --git a/src/main/target/MAMBAF722/target.mk b/src/main/target/MAMBAF722/target.mk index 9045434025..916eedfa23 100644 --- a/src/main/target/MAMBAF722/target.mk +++ b/src/main/target/MAMBAF722/target.mk @@ -7,7 +7,6 @@ TARGET_SRC = \ drivers/barometer/barometer_bmp085.c \ drivers/barometer/barometer_bmp280.c \ drivers/barometer/barometer_ms5611.c \ - drivers/barometer/barometer_dps310.h \ drivers/compass/compass_hmc5883l.c \ drivers/compass/compass_qmc5883l.c \ drivers/max7456.c diff --git a/src/main/target/MATEKF722/target.mk b/src/main/target/MATEKF722/target.mk index 7934b618a3..bddd0a2c8e 100644 --- a/src/main/target/MATEKF722/target.mk +++ b/src/main/target/MATEKF722/target.mk @@ -8,6 +8,7 @@ TARGET_SRC = \ drivers/barometer/barometer_bmp085.c \ drivers/barometer/barometer_bmp280.c \ drivers/barometer/barometer_ms5611.c \ + drivers/barometer/barometer_dps310.h \ drivers/compass/compass_hmc5883l.c \ drivers/compass/compass_qmc5883l.c \ drivers/max7456.c From 2fa32f1b3ec4adefe4b26e308baf9a9449f6ac46 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sat, 22 May 2021 21:02:53 -0300 Subject: [PATCH 10/32] to *c. --- make/source.mk | 2 +- src/main/target/MATEKF722/target.mk | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/make/source.mk b/make/source.mk index c91383e06c..297fe697ff 100644 --- a/make/source.mk +++ b/make/source.mk @@ -252,7 +252,7 @@ SIZE_OPTIMISED_SRC := $(SIZE_OPTIMISED_SRC) \ drivers/barometer/barometer_bmp280.c \ drivers/barometer/barometer_fake.c \ drivers/barometer/barometer_ms5611.c \ - drivers/barometer/barometer_dps310.h \ + drivers/barometer/barometer_dps310.c \ drivers/barometer/barometer_lps.c \ drivers/barometer/barometer_qmp6988.c \ drivers/beesign.c \ diff --git a/src/main/target/MATEKF722/target.mk b/src/main/target/MATEKF722/target.mk index bddd0a2c8e..25f5c3d230 100644 --- a/src/main/target/MATEKF722/target.mk +++ b/src/main/target/MATEKF722/target.mk @@ -8,7 +8,7 @@ TARGET_SRC = \ drivers/barometer/barometer_bmp085.c \ drivers/barometer/barometer_bmp280.c \ drivers/barometer/barometer_ms5611.c \ - drivers/barometer/barometer_dps310.h \ + drivers/barometer/barometer_dps310.c \ drivers/compass/compass_hmc5883l.c \ drivers/compass/compass_qmc5883l.c \ drivers/max7456.c From 3732941316ade877dd488d4600dfe07bbd597561 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sat, 22 May 2021 21:08:13 -0300 Subject: [PATCH 11/32] force new build --- src/main/drivers/barometer/barometer_dps310.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index 6f946fefeb..e68906eeaa 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -327,7 +327,6 @@ static void busDeviceInit(busDevice_t *busdev) } #else UNUSED(busdev); - UNUSED(owner); #endif } From a8957925b81158b777de0bb5599edf4a10747055 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sat, 22 May 2021 21:26:46 -0300 Subject: [PATCH 12/32] move #define to MATEKF722 --- src/main/target/MAMBAF722/target.h | 1 - src/main/target/MATEKF722/target.h | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/target/MAMBAF722/target.h b/src/main/target/MAMBAF722/target.h index 8f5a4249fe..f4b0a3fb82 100644 --- a/src/main/target/MAMBAF722/target.h +++ b/src/main/target/MAMBAF722/target.h @@ -67,7 +67,6 @@ #define USE_BARO_MS5611 #define USE_BARO_BMP280 #define USE_BARO_BMP085 -#define USE_BARO_DPS310 #define USE_I2C #define USE_I2C_DEVICE_1 diff --git a/src/main/target/MATEKF722/target.h b/src/main/target/MATEKF722/target.h index 6da9f5f237..3589e16169 100644 --- a/src/main/target/MATEKF722/target.h +++ b/src/main/target/MATEKF722/target.h @@ -78,6 +78,7 @@ #define USE_BARO_BMP280 #define USE_BARO_MS5611 #define USE_BARO_BMP085 +#define USE_BARO_DPS310 //*********** Magnetometer / Compass ************* #define USE_MAG From ff621a1691356ec3db143d5796c961c3cd7671aa Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sat, 22 May 2021 22:01:55 -0300 Subject: [PATCH 13/32] fix compilation error --- src/main/drivers/barometer/barometer_dps310.c | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index e68906eeaa..fd009550a4 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -223,7 +223,7 @@ static bool deviceConfigure(busDevice_t * busDev) return true; } -static bool dps310GetUP(baroDev_t *baro) +static void dps310GetUP(baroDev_t *baro) { UNUSED(baro); @@ -255,8 +255,6 @@ static bool dps310GetUP(baroDev_t *baro) baroState.pressure = c00 + Praw_sc * (c10 + Praw_sc * (c20 + Praw_sc * c30)) + Traw_sc * c01 + Traw_sc * Praw_sc * (c11 + Praw_sc * c21); baroState.temperature = c0 * 0.5f + c1 * Traw_sc; - - return true; } static void deviceCalculate(int32_t *pressure, int32_t *temperature) @@ -295,25 +293,17 @@ static void dps310StartUT(baroDev_t *baro) UNUSED(baro); } -static bool dps310GetUT(baroDev_t *baro) +static void dps310GetUT(baroDev_t *baro) { UNUSED(baro); - - return true; } static void dps310StartUP(baroDev_t *baro) { - if (busBusy(&baro->busdev, NULL)) { - return false; - } - // 1. Kick off read // No need to poll for data ready as the conversion rate is 32Hz and this is sampling at 20Hz // Read PSR_B2, PSR_B1, PSR_B0, TMP_B2, TMP_B1, TMP_B0 busReadRegisterBuffer(&baro->busdev, DPS310_REG_PSR_B2, buf, 6); - - return true; } static void busDeviceInit(busDevice_t *busdev) @@ -321,7 +311,7 @@ static void busDeviceInit(busDevice_t *busdev) #ifdef USE_BARO_SPI_DPS310 if (busdev->bustype == BUSTYPE_SPI) { IOHi(busdev->busdev_u.spi.csnPin); // Disable - IOInit(busdev->busdev_u.spi.csnPin, owner, 0); + IOInit(busdev->busdev_u.spi.csnPin, OWNER_BARO_CS, 0); IOConfigGPIO(busdev->busdev_u.spi.csnPin, IOCFG_OUT_PP); spiSetDivisor(busdev, SPI_CLOCK_STANDARD); } @@ -346,7 +336,7 @@ bool baroDPS310Detect(baroDev_t *baro) busDevice_t *busdev = &baro->busdev; bool defaultAddressApplied = false; - busDeviceInit(&baro->busdev, OWNER_BARO_CS); + busDeviceInit(&baro->busdev); if ((busdev->bustype == BUSTYPE_I2C) && (busdev->busdev_u.i2c.address == 0)) { // Default address for BMP280 @@ -367,8 +357,6 @@ bool baroDPS310Detect(baroDev_t *baro) return false; } - busDeviceRegister(busdev); - baro->ut_delay = 0; baro->start_ut = dps310StartUT; baro->get_ut = dps310GetUT; From b05453b2cb742bd25cb4ae278bb49f51d2c88c33 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sat, 22 May 2021 22:44:11 -0300 Subject: [PATCH 14/32] remove unused #define --- src/main/drivers/barometer/barometer_dps310.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index fd009550a4..350b649580 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -42,9 +42,6 @@ #include "drivers/barometer/barometer_dps310.h" #include "drivers/resource.h" -// 10 MHz max SPI frequency -#define DPS310_MAX_SPI_CLK_HZ 10000000 - #if defined(USE_BARO) && defined(USE_BARO_DPS310) #define DPS310_I2C_ADDR 0x76 From 8ec625ed07faf266646dde8eb0ae92edfa70d575 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Wed, 26 May 2021 12:08:13 -0300 Subject: [PATCH 15/32] version SE --- src/main/target/MATEKF722SE/target.h | 1 + src/main/target/MATEKF722SE/target.mk | 1 + 2 files changed, 2 insertions(+) diff --git a/src/main/target/MATEKF722SE/target.h b/src/main/target/MATEKF722SE/target.h index df30407eda..683f29d304 100755 --- a/src/main/target/MATEKF722SE/target.h +++ b/src/main/target/MATEKF722SE/target.h @@ -89,6 +89,7 @@ #define USE_BARO_BMP280 #define USE_BARO_MS5611 #define USE_BARO_BMP085 +#define USE_BARO_DPS310 #define MAG_I2C_INSTANCE (I2CDEV_1) #define USE_MAG diff --git a/src/main/target/MATEKF722SE/target.mk b/src/main/target/MATEKF722SE/target.mk index db67145925..6180b7b01c 100755 --- a/src/main/target/MATEKF722SE/target.mk +++ b/src/main/target/MATEKF722SE/target.mk @@ -9,6 +9,7 @@ TARGET_SRC = \ drivers/barometer/barometer_bmp280.c \ drivers/barometer/barometer_bmp085.c \ drivers/barometer/barometer_ms5611.c \ + drivers/barometer/barometer_dps310.c \ drivers/compass/compass_hmc5883l.c \ drivers/compass/compass_qmc5883l.c \ drivers/max7456.c From 52e273bd5139b6dba48fec392a180dddbf64040b Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Wed, 26 May 2021 19:09:02 -0300 Subject: [PATCH 16/32] fix laggy and steppy --- src/main/drivers/barometer/barometer_dps310.c | 87 +++++-------------- 1 file changed, 23 insertions(+), 64 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index 350b649580..d863cad5f3 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -42,7 +42,7 @@ #include "drivers/barometer/barometer_dps310.h" #include "drivers/resource.h" -#if defined(USE_BARO) && defined(USE_BARO_DPS310) +//#if defined(USE_BARO) && defined(USE_BARO_DPS310) #define DPS310_I2C_ADDR 0x76 @@ -106,9 +106,6 @@ typedef struct { static baroState_t baroState; -#define busReadBuf busReadRegisterBuffer -#define busWrite busWriteRegister - static uint8_t buf[6]; // Helper functions @@ -119,7 +116,7 @@ static uint8_t registerRead(busDevice_t * busDev, uint8_t reg) static void registerWrite(busDevice_t * busDev, uint8_t reg, uint8_t value) { - busWrite(busDev, reg, value); + busWriteRegister(busDev, reg, value); } static void registerSetBits(busDevice_t * busDev, uint8_t reg, uint8_t setbits) @@ -170,10 +167,10 @@ static bool deviceConfigure(busDevice_t * busDev) #define READ_LENGTH (COEFFICIENT_LENGTH / 2) uint8_t coef[COEFFICIENT_LENGTH]; - if (!busReadBuf(busDev, DPS310_REG_COEF, coef, READ_LENGTH)) { + if (!busReadRegisterBuffer(busDev, DPS310_REG_COEF, coef, READ_LENGTH)) { return false; } - if (!busReadBuf(busDev, DPS310_REG_COEF + READ_LENGTH, coef + READ_LENGTH, COEFFICIENT_LENGTH - READ_LENGTH)) { + if (!busReadRegisterBuffer(busDev, DPS310_REG_COEF + READ_LENGTH, coef + READ_LENGTH, COEFFICIENT_LENGTH - READ_LENGTH)) { return false; } @@ -220,9 +217,13 @@ static bool deviceConfigure(busDevice_t * busDev) return true; } -static void dps310GetUP(baroDev_t *baro) +static bool deviceReadMeasurement(baroDev_t *baro) { - UNUSED(baro); + // 1. Check if pressure is ready + bool pressure_ready = registerRead(&baro->busdev, DPS310_REG_MEAS_CFG) & DPS310_MEAS_CFG_PRS_RDY; + if (!pressure_ready) { + return false; + } // 2. Choose scaling factors kT (for temperature) and kP (for pressure) based on the chosen precision rate. // The scaling factors are listed in Table 9. @@ -230,6 +231,11 @@ static void dps310GetUP(baroDev_t *baro) static float kP = 253952; // 16 times (Standard) // 3. Read the pressure and temperature result from the registers + // Read PSR_B2, PSR_B1, PSR_B0, TMP_B2, TMP_B1, TMP_B0 + uint8_t buf[6]; + if (!busReadRegisterBuffer(&baro->busdev, DPS310_REG_PSR_B2, buf, 6)) { + return false; + } const int32_t Praw = getTwosComplement((buf[0] << 16) + (buf[1] << 8) + buf[2], 24); const int32_t Traw = getTwosComplement((buf[3] << 16) + (buf[4] << 8) + buf[5], 24); @@ -265,8 +271,6 @@ static void deviceCalculate(int32_t *pressure, int32_t *temperature) } } - - #define DETECTION_MAX_RETRY_COUNT 5 static bool deviceDetect(busDevice_t * busDev) { @@ -285,56 +289,11 @@ static bool deviceDetect(busDevice_t * busDev) return false; } -static void dps310StartUT(baroDev_t *baro) -{ - UNUSED(baro); -} - -static void dps310GetUT(baroDev_t *baro) -{ - UNUSED(baro); -} - -static void dps310StartUP(baroDev_t *baro) -{ - // 1. Kick off read - // No need to poll for data ready as the conversion rate is 32Hz and this is sampling at 20Hz - // Read PSR_B2, PSR_B1, PSR_B0, TMP_B2, TMP_B1, TMP_B0 - busReadRegisterBuffer(&baro->busdev, DPS310_REG_PSR_B2, buf, 6); -} - -static void busDeviceInit(busDevice_t *busdev) -{ -#ifdef USE_BARO_SPI_DPS310 - if (busdev->bustype == BUSTYPE_SPI) { - IOHi(busdev->busdev_u.spi.csnPin); // Disable - IOInit(busdev->busdev_u.spi.csnPin, OWNER_BARO_CS, 0); - IOConfigGPIO(busdev->busdev_u.spi.csnPin, IOCFG_OUT_PP); - spiSetDivisor(busdev, SPI_CLOCK_STANDARD); - } -#else - UNUSED(busdev); -#endif -} - -static void busDeviceDeInit(busDevice_t *busdev) -{ -#ifdef USE_BARO_SPI_DPS310 - if (busdev->bustype == BUSTYPE_SPI) { - spiPreinitCsByIO(busdev->busdev_u.spi.csnPin); - } -#else - UNUSED(busdev); -#endif -} - bool baroDPS310Detect(baroDev_t *baro) { busDevice_t *busdev = &baro->busdev; bool defaultAddressApplied = false; - busDeviceInit(&baro->busdev); - if ((busdev->bustype == BUSTYPE_I2C) && (busdev->busdev_u.i2c.address == 0)) { // Default address for BMP280 busdev->busdev_u.i2c.address = DPS310_I2C_ADDR; @@ -342,7 +301,6 @@ bool baroDPS310Detect(baroDev_t *baro) } if (!deviceDetect(busdev)) { - busDeviceDeInit(busdev); if (defaultAddressApplied) { busdev->busdev_u.i2c.address = 0; } @@ -350,21 +308,22 @@ bool baroDPS310Detect(baroDev_t *baro) } if (!deviceConfigure(busdev)) { - busDeviceDeInit(busdev); return false; } + const uint32_t baroDelay = 1000000 / 32 / 2; // twice the sample rate to capture all new data + baro->ut_delay = 0; - baro->start_ut = dps310StartUT; - baro->get_ut = dps310GetUT; + baro->start_ut = NULL; + baro->get_ut = NULL; - baro->up_delay = 45000; // 45ms delay plus 5 1ms cycles 50ms - baro->start_up = dps310StartUP; - baro->get_up = dps310GetUP; + baro->up_delay = baroDelay; + baro->start_up = NULL; + baro->get_up = deviceReadMeasurement; baro->calculate = deviceCalculate; return true; } -#endif +//#endif From 66b0869a7889b7f426d5351f233874b192b955a7 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Wed, 26 May 2021 19:10:04 -0300 Subject: [PATCH 17/32] #ifdef --- src/main/drivers/barometer/barometer_dps310.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index d863cad5f3..86427a6b0d 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -42,7 +42,7 @@ #include "drivers/barometer/barometer_dps310.h" #include "drivers/resource.h" -//#if defined(USE_BARO) && defined(USE_BARO_DPS310) +#if defined(USE_BARO) && defined(USE_BARO_DPS310) #define DPS310_I2C_ADDR 0x76 @@ -326,4 +326,4 @@ bool baroDPS310Detect(baroDev_t *baro) return true; } -//#endif +#endif From 45c689f9d4691a17db5e9b869e85cce397b80aac Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Wed, 26 May 2021 19:14:32 -0300 Subject: [PATCH 18/32] remover variable duplication --- src/main/drivers/barometer/barometer_dps310.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index 86427a6b0d..e55600b796 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -106,8 +106,6 @@ typedef struct { static baroState_t baroState; -static uint8_t buf[6]; - // Helper functions static uint8_t registerRead(busDevice_t * busDev, uint8_t reg) { From 2bde7f90ab37c2c49afe5354a716949035e48417 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Wed, 26 May 2021 19:24:26 -0300 Subject: [PATCH 19/32] redo the function deviceConfigure --- src/main/drivers/barometer/barometer_dps310.c | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index e55600b796..dce1f1cb48 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -72,7 +72,11 @@ #define DPS310_MEAS_CFG_SENSOR_RDY (1 << 6) #define DPS310_MEAS_CFG_TMP_RDY (1 << 5) #define DPS310_MEAS_CFG_PRS_RDY (1 << 4) + +#define DPS310_MEAS_CFG_MEAS_CTRL_MASK (0x7) #define DPS310_MEAS_CFG_MEAS_CTRL_CONT (0x7) +#define DPS310_MEAS_CFG_MEAS_TEMP_SING (0x2) +#define DPS310_MEAS_CFG_MEAS_IDLE (0x0) #define DPS310_PRS_CFG_BIT_PM_RATE_32HZ (0x50) // 101 - 32 measurements pr. sec. #define DPS310_PRS_CFG_BIT_PM_PRC_16 (0x04) // 0100 - 16 times (Standard). @@ -159,16 +163,8 @@ static bool deviceConfigure(busDevice_t * busDev) // 1. Read the pressure calibration coefficients (c00, c10, c20, c30, c01, c11, and c21) from the Calibration Coefficient register. // Note: The coefficients read from the coefficient register are 2's complement numbers. - - // Do the read of the coefficients in multiple parts, as the chip will return a read failure when trying to read all at once over I2C. -#define COEFFICIENT_LENGTH 18 -#define READ_LENGTH (COEFFICIENT_LENGTH / 2) - - uint8_t coef[COEFFICIENT_LENGTH]; - if (!busReadRegisterBuffer(busDev, DPS310_REG_COEF, coef, READ_LENGTH)) { - return false; - } - if (!busReadRegisterBuffer(busDev, DPS310_REG_COEF + READ_LENGTH, coef + READ_LENGTH, COEFFICIENT_LENGTH - READ_LENGTH)) { + uint8_t coef[18]; + if (!busReadBuf(busDev, DPS310_REG_COEF, coef, sizeof(coef))) { return false; } @@ -199,6 +195,21 @@ static bool deviceConfigure(busDevice_t * busDev) // 0x20 c30 [15:8] + 0x21 c30 [7:0] baroState.calib.c30 = getTwosComplement(((uint32_t)coef[16] << 8) | (uint32_t)coef[17], 16); + // MEAS_CFG: Make sure the device is in IDLE mode + registerWriteBits(busDev, DPS310_REG_MEAS_CFG, DPS310_MEAS_CFG_MEAS_CTRL_MASK, DPS310_MEAS_CFG_MEAS_IDLE); + + // Fix IC with a fuse bit problem, which lead to a wrong temperature + // Should not affect ICs without this problem + registerWrite(busDev, 0x0E, 0xA5); + registerWrite(busDev, 0x0F, 0x96); + registerWrite(busDev, 0x62, 0x02); + registerWrite(busDev, 0x0E, 0x00); + registerWrite(busDev, 0x0F, 0x00); + + // Make ONE temperature measurement and flush it + registerWriteBits(busDev, DPS310_REG_MEAS_CFG, DPS310_MEAS_CFG_MEAS_CTRL_MASK, DPS310_MEAS_CFG_MEAS_TEMP_SING); + delay(40); + // PRS_CFG: pressure measurement rate (32 Hz) and oversampling (16 time standard) registerSetBits(busDev, DPS310_REG_PRS_CFG, DPS310_PRS_CFG_BIT_PM_RATE_32HZ | DPS310_PRS_CFG_BIT_PM_PRC_16); @@ -210,7 +221,7 @@ static bool deviceConfigure(busDevice_t * busDev) registerSetBits(busDev, DPS310_REG_CFG_REG, DPS310_CFG_REG_BIT_T_SHIFT | DPS310_CFG_REG_BIT_P_SHIFT); // MEAS_CFG: Continuous pressure and temperature measurement - registerSetBits(busDev, DPS310_REG_MEAS_CFG, DPS310_MEAS_CFG_MEAS_CTRL_CONT); + registerWriteBits(busDev, DPS310_REG_MEAS_CFG, DPS310_MEAS_CFG_MEAS_CTRL_MASK, DPS310_MEAS_CFG_MEAS_CTRL_CONT); return true; } From 9918bb8c5735fbbb77e4f90b25102f68f8b8d156 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Wed, 26 May 2021 19:31:03 -0300 Subject: [PATCH 20/32] add functions --- src/main/drivers/barometer/barometer_dps310.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index dce1f1cb48..fb3be3deae 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -121,16 +121,21 @@ static void registerWrite(busDevice_t * busDev, uint8_t reg, uint8_t value) busWriteRegister(busDev, reg, value); } -static void registerSetBits(busDevice_t * busDev, uint8_t reg, uint8_t setbits) +static void registerWriteBits(busDevice_t * busDev, uint8_t reg, uint8_t mask, uint8_t bits) { uint8_t val = registerRead(busDev, reg); - if ((val & setbits) != setbits) { - val |= setbits; - registerWrite(busDev, reg, val); + if ((val & mask) != bits) { + val = (val & (~mask)) | bits; + busWriteRegister(busDev, reg, val); } } +static void registerSetBits(busDevice_t * busDev, uint8_t reg, uint8_t setbits) +{ + registerWriteBits(busDev, reg, setbits, setbits); +} + static int32_t getTwosComplement(uint32_t raw, uint8_t length) { if (raw & ((int)1 << (length - 1))) { From 66576f651db96227587fc2970dd65b8415f74e01 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Wed, 26 May 2021 19:34:09 -0300 Subject: [PATCH 21/32] move --- src/main/drivers/barometer/barometer_dps310.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index fb3be3deae..85771f071d 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -127,7 +127,7 @@ static void registerWriteBits(busDevice_t * busDev, uint8_t reg, uint8_t mask, u if ((val & mask) != bits) { val = (val & (~mask)) | bits; - busWriteRegister(busDev, reg, val); + registerWrite(busDev, reg, val); } } From c37f7769f14b27339b3d16415fd91c312b672ac4 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Wed, 26 May 2021 19:54:07 -0300 Subject: [PATCH 22/32] fix compilation error --- src/main/drivers/barometer/barometer_dps310.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index 85771f071d..10c4f8885d 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -169,7 +169,7 @@ static bool deviceConfigure(busDevice_t * busDev) // 1. Read the pressure calibration coefficients (c00, c10, c20, c30, c01, c11, and c21) from the Calibration Coefficient register. // Note: The coefficients read from the coefficient register are 2's complement numbers. uint8_t coef[18]; - if (!busReadBuf(busDev, DPS310_REG_COEF, coef, sizeof(coef))) { + if (!busReadRegisterBuffer(busDev, DPS310_REG_COEF, coef, sizeof(coef))) { return false; } @@ -293,7 +293,7 @@ static bool deviceDetect(busDevice_t * busDev) delay(100); - bool ack = busReadBuf(busDev, DPS310_REG_ID, chipId, 1); + bool ack = busReadRegisterBuffer(busDev, DPS310_REG_ID, chipId, 1); if (ack && chipId[0] == DPS310_ID_REV_AND_PROD_ID) { return true; From 9fa0bc55caa69fdb2700ce0ff174063087a59759 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Wed, 26 May 2021 20:11:07 -0300 Subject: [PATCH 23/32] bool to void --- src/main/drivers/barometer/barometer_dps310.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index 10c4f8885d..9ae2d7d792 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -231,12 +231,12 @@ static bool deviceConfigure(busDevice_t * busDev) return true; } -static bool deviceReadMeasurement(baroDev_t *baro) +static void deviceReadMeasurement(baroDev_t *baro) { // 1. Check if pressure is ready bool pressure_ready = registerRead(&baro->busdev, DPS310_REG_MEAS_CFG) & DPS310_MEAS_CFG_PRS_RDY; if (!pressure_ready) { - return false; + return; } // 2. Choose scaling factors kT (for temperature) and kP (for pressure) based on the chosen precision rate. @@ -248,7 +248,7 @@ static bool deviceReadMeasurement(baroDev_t *baro) // Read PSR_B2, PSR_B1, PSR_B0, TMP_B2, TMP_B1, TMP_B0 uint8_t buf[6]; if (!busReadRegisterBuffer(&baro->busdev, DPS310_REG_PSR_B2, buf, 6)) { - return false; + return; } const int32_t Praw = getTwosComplement((buf[0] << 16) + (buf[1] << 8) + buf[2], 24); From 2af0eeaa26a5cfe04409da0ffc03bd159bef6915 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Thu, 27 May 2021 13:30:57 -0300 Subject: [PATCH 24/32] add dps310 in hd and px version --- src/main/target/MATEKF722HD/target.h | 1 + src/main/target/MATEKF722HD/target.mk | 1 + src/main/target/MATEKF722PX/target.h | 1 + src/main/target/MATEKF722PX/target.mk | 1 + 4 files changed, 4 insertions(+) diff --git a/src/main/target/MATEKF722HD/target.h b/src/main/target/MATEKF722HD/target.h index b3762308c3..1bcd85d0f5 100644 --- a/src/main/target/MATEKF722HD/target.h +++ b/src/main/target/MATEKF722HD/target.h @@ -78,6 +78,7 @@ #define USE_BARO_BMP280 #define USE_BARO_MS5611 #define USE_BARO_BMP085 +#define USE_BARO_DPS310 #define MAG_I2C_INSTANCE (I2CDEV_1) #define USE_MAG diff --git a/src/main/target/MATEKF722HD/target.mk b/src/main/target/MATEKF722HD/target.mk index 986d937c05..a6437e1fb9 100644 --- a/src/main/target/MATEKF722HD/target.mk +++ b/src/main/target/MATEKF722HD/target.mk @@ -8,5 +8,6 @@ TARGET_SRC = \ drivers/barometer/barometer_bmp280.c \ drivers/barometer/barometer_bmp085.c \ drivers/barometer/barometer_ms5611.c \ + drivers/barometer/barometer_dps310.c \ drivers/compass/compass_hmc5883l.c \ drivers/compass/compass_qmc5883l.c diff --git a/src/main/target/MATEKF722PX/target.h b/src/main/target/MATEKF722PX/target.h index a017c0de6f..5148651774 100644 --- a/src/main/target/MATEKF722PX/target.h +++ b/src/main/target/MATEKF722PX/target.h @@ -78,6 +78,7 @@ #define USE_BARO_BMP280 #define USE_BARO_MS5611 #define USE_BARO_BMP085 +#define USE_BARO_DPS310 #define MAG_I2C_INSTANCE (I2CDEV_1) #define USE_MAG diff --git a/src/main/target/MATEKF722PX/target.mk b/src/main/target/MATEKF722PX/target.mk index df84f4fab2..c05ee36b66 100644 --- a/src/main/target/MATEKF722PX/target.mk +++ b/src/main/target/MATEKF722PX/target.mk @@ -8,6 +8,7 @@ TARGET_SRC = \ drivers/barometer/barometer_bmp280.c \ drivers/barometer/barometer_bmp085.c \ drivers/barometer/barometer_ms5611.c \ + drivers/barometer/barometer_dps310.c \ drivers/compass/compass_hmc5883l.c \ drivers/compass/compass_qmc5883l.c \ drivers/max7456.c From f185c9a2658538e01f4cff009561deba53e85807 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Thu, 27 May 2021 18:14:54 -0300 Subject: [PATCH 25/32] maybe it works --- src/main/drivers/barometer/barometer_dps310.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index 9ae2d7d792..27d2b7cec2 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -110,6 +110,8 @@ typedef struct { static baroState_t baroState; +static uint8_t dps310_chip_id = 0; + // Helper functions static uint8_t registerRead(busDevice_t * busDev, uint8_t reg) { @@ -308,19 +310,24 @@ bool baroDPS310Detect(baroDev_t *baro) busDevice_t *busdev = &baro->busdev; bool defaultAddressApplied = false; + delay(10); // No idea how long the chip takes to power-up, but let's make it 10ms + if ((busdev->bustype == BUSTYPE_I2C) && (busdev->busdev_u.i2c.address == 0)) { - // Default address for BMP280 busdev->busdev_u.i2c.address = DPS310_I2C_ADDR; defaultAddressApplied = true; } - if (!deviceDetect(busdev)) { - if (defaultAddressApplied) { - busdev->busdev_u.i2c.address = 0; - } + busReadRegisterBuffer(busdev, DPS310_REG_ID, &dps310_chip_id, 1); /* read Chip Id */ + if (dps310_chip_id != DPS310_ID_REV_AND_PROD_ID) { + //if (!deviceDetect(busdev)) + { + if (defaultAddressApplied) { + busdev->busdev_u.i2c.address = 0; + } return false; + } } - + if (!deviceConfigure(busdev)) { return false; } From 5d1cded74a2566badc2abaa62cc69b380ea6e7c6 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Thu, 27 May 2021 18:27:08 -0300 Subject: [PATCH 26/32] add #define DEFAULT_BARO_DPS310 --- src/main/sensors/barometer.c | 2 +- src/main/target/MATEKF722HD/target.h | 1 + src/main/target/MATEKF722PX/target.h | 1 + src/main/target/MATEKF722SE/target.h | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/sensors/barometer.c b/src/main/sensors/barometer.c index 5934af2020..8b85901b2a 100644 --- a/src/main/sensors/barometer.c +++ b/src/main/sensors/barometer.c @@ -122,7 +122,7 @@ void pgResetFn_barometerConfig(barometerConfig_t *barometerConfig) { barometerConfig->baro_spi_csn = IO_TAG(LPS_CS_PIN); barometerConfig->baro_i2c_device = I2C_DEV_TO_CFG(I2CINVALID); barometerConfig->baro_i2c_address = 0; -#elif defined(DEFAULT_BARO_MS5611) || defined(DEFAULT_BARO_BMP280) || defined(DEFAULT_BARO_BMP085)||defined(DEFAULT_BARO_QMP6988) +#elif defined(DEFAULT_BARO_MS5611) || defined(DEFAULT_BARO_BMP280) || defined(DEFAULT_BARO_BMP085)|| defined(DEFAULT_BARO_QMP6988) || defined(DEFAULT_BARO_DPS310) // All I2C devices shares a default config with address = 0 (per device default) barometerConfig->baro_bustype = BUSTYPE_I2C; barometerConfig->baro_i2c_device = I2C_DEV_TO_CFG(BARO_I2C_INSTANCE); diff --git a/src/main/target/MATEKF722HD/target.h b/src/main/target/MATEKF722HD/target.h index 1bcd85d0f5..5224510d16 100644 --- a/src/main/target/MATEKF722HD/target.h +++ b/src/main/target/MATEKF722HD/target.h @@ -79,6 +79,7 @@ #define USE_BARO_MS5611 #define USE_BARO_BMP085 #define USE_BARO_DPS310 +#define DEFAULT_BARO_DPS310 #define MAG_I2C_INSTANCE (I2CDEV_1) #define USE_MAG diff --git a/src/main/target/MATEKF722PX/target.h b/src/main/target/MATEKF722PX/target.h index 5148651774..5e69e990f7 100644 --- a/src/main/target/MATEKF722PX/target.h +++ b/src/main/target/MATEKF722PX/target.h @@ -79,6 +79,7 @@ #define USE_BARO_MS5611 #define USE_BARO_BMP085 #define USE_BARO_DPS310 +#define DEFAULT_BARO_DPS310 #define MAG_I2C_INSTANCE (I2CDEV_1) #define USE_MAG diff --git a/src/main/target/MATEKF722SE/target.h b/src/main/target/MATEKF722SE/target.h index 683f29d304..64b913105d 100755 --- a/src/main/target/MATEKF722SE/target.h +++ b/src/main/target/MATEKF722SE/target.h @@ -90,6 +90,7 @@ #define USE_BARO_MS5611 #define USE_BARO_BMP085 #define USE_BARO_DPS310 +#define DEFAULT_BARO_DPS310 #define MAG_I2C_INSTANCE (I2CDEV_1) #define USE_MAG From be7b986a32acfe89dac48b7552d4e9d1bedc23cf Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Thu, 27 May 2021 18:40:24 -0300 Subject: [PATCH 27/32] #if check dps310 --- src/main/sensors/barometer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/sensors/barometer.c b/src/main/sensors/barometer.c index 8b85901b2a..16019eb3e7 100644 --- a/src/main/sensors/barometer.c +++ b/src/main/sensors/barometer.c @@ -67,7 +67,7 @@ void pgResetFn_barometerConfig(barometerConfig_t *barometerConfig) { // 2. Determine default based on USE_BARO_xxx // a. Precedence is in the order of popularity; BMP280, MS5611 then BMP085, then // b. If SPI variant is specified, it is likely onboard, so take it. -#if !(defined(DEFAULT_BARO_SPI_BMP280) || defined(DEFAULT_BARO_BMP280) || defined(DEFAULT_BARO_SPI_MS5611) || defined(DEFAULT_BARO_MS5611) || defined(DEFAULT_BARO_BMP085) || defined(DEFAULT_BARO_SPI_LPS) || defined(DEFAULT_BARO_SPI_QMP6988) || defined(DEFAULT_BARO_QMP6988)) +#if !(defined(DEFAULT_BARO_SPI_BMP280) || defined(DEFAULT_BARO_BMP280) || defined(DEFAULT_BARO_SPI_MS5611) || defined(DEFAULT_BARO_MS5611) || defined(DEFAULT_BARO_BMP085) || defined(DEFAULT_BARO_SPI_LPS) || defined(DEFAULT_BARO_SPI_QMP6988) || defined(DEFAULT_BARO_QMP6988) || defined(DEFAULT_BARO_DPS310)) #if defined(USE_BARO_DPS310) || defined(USE_BARO_SPI_DPS310) #if defined(USE_BARO_SPI_DPS310) #define DEFAULT_BARO_SPI_DPS310 From 34f30e943210de857e511e866f8b8d08d3dfd671 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sat, 29 May 2021 18:00:06 -0300 Subject: [PATCH 28/32] sub func --- src/main/drivers/barometer/barometer_dps310.c | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index 27d2b7cec2..e04be5f63a 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -170,8 +170,17 @@ static bool deviceConfigure(busDevice_t * busDev) // 1. Read the pressure calibration coefficients (c00, c10, c20, c30, c01, c11, and c21) from the Calibration Coefficient register. // Note: The coefficients read from the coefficient register are 2's complement numbers. - uint8_t coef[18]; - if (!busReadRegisterBuffer(busDev, DPS310_REG_COEF, coef, sizeof(coef))) { + + // Do the read of the coefficients in multiple parts, as the chip will return a read failure when trying to read all at once over I2C. +#define COEFFICIENT_LENGTH 18 +#define READ_LENGTH (COEFFICIENT_LENGTH / 2) + + uint8_t coef[COEFFICIENT_LENGTH]; + if (!busReadBuf(busDev, DPS310_REG_COEF, coef, READ_LENGTH)) { + return false; + } + + if (!busReadBuf(busDev, DPS310_REG_COEF + READ_LENGTH, coef + READ_LENGTH, COEFFICIENT_LENGTH - READ_LENGTH)) { return false; } @@ -202,21 +211,6 @@ static bool deviceConfigure(busDevice_t * busDev) // 0x20 c30 [15:8] + 0x21 c30 [7:0] baroState.calib.c30 = getTwosComplement(((uint32_t)coef[16] << 8) | (uint32_t)coef[17], 16); - // MEAS_CFG: Make sure the device is in IDLE mode - registerWriteBits(busDev, DPS310_REG_MEAS_CFG, DPS310_MEAS_CFG_MEAS_CTRL_MASK, DPS310_MEAS_CFG_MEAS_IDLE); - - // Fix IC with a fuse bit problem, which lead to a wrong temperature - // Should not affect ICs without this problem - registerWrite(busDev, 0x0E, 0xA5); - registerWrite(busDev, 0x0F, 0x96); - registerWrite(busDev, 0x62, 0x02); - registerWrite(busDev, 0x0E, 0x00); - registerWrite(busDev, 0x0F, 0x00); - - // Make ONE temperature measurement and flush it - registerWriteBits(busDev, DPS310_REG_MEAS_CFG, DPS310_MEAS_CFG_MEAS_CTRL_MASK, DPS310_MEAS_CFG_MEAS_TEMP_SING); - delay(40); - // PRS_CFG: pressure measurement rate (32 Hz) and oversampling (16 time standard) registerSetBits(busDev, DPS310_REG_PRS_CFG, DPS310_PRS_CFG_BIT_PM_RATE_32HZ | DPS310_PRS_CFG_BIT_PM_PRC_16); @@ -228,7 +222,7 @@ static bool deviceConfigure(busDevice_t * busDev) registerSetBits(busDev, DPS310_REG_CFG_REG, DPS310_CFG_REG_BIT_T_SHIFT | DPS310_CFG_REG_BIT_P_SHIFT); // MEAS_CFG: Continuous pressure and temperature measurement - registerWriteBits(busDev, DPS310_REG_MEAS_CFG, DPS310_MEAS_CFG_MEAS_CTRL_MASK, DPS310_MEAS_CFG_MEAS_CTRL_CONT); + registerSetBits(busDev, DPS310_REG_MEAS_CFG, DPS310_MEAS_CFG_MEAS_CTRL_CONT); return true; } From 01f3143f96d965d9fe60ea96904f802c087b3441 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sat, 29 May 2021 18:04:10 -0300 Subject: [PATCH 29/32] fix compilation error --- src/main/drivers/barometer/barometer_dps310.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index e04be5f63a..e5418915c9 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -176,11 +176,11 @@ static bool deviceConfigure(busDevice_t * busDev) #define READ_LENGTH (COEFFICIENT_LENGTH / 2) uint8_t coef[COEFFICIENT_LENGTH]; - if (!busReadBuf(busDev, DPS310_REG_COEF, coef, READ_LENGTH)) { + if (!busReadRegisterBuffer(busDev, DPS310_REG_COEF, coef, READ_LENGTH)) { return false; } - if (!busReadBuf(busDev, DPS310_REG_COEF + READ_LENGTH, coef + READ_LENGTH, COEFFICIENT_LENGTH - READ_LENGTH)) { + if (!busReadRegisterBuffer(busDev, DPS310_REG_COEF + READ_LENGTH, coef + READ_LENGTH, COEFFICIENT_LENGTH - READ_LENGTH)) { return false; } From 63cee293050077e5f6a5e0208aa8ec0b3a6c8ea4 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sun, 19 Sep 2021 12:24:41 -0300 Subject: [PATCH 30/32] add matek f405 se --- src/main/target/MATEKF405SE/target.h | 2 +- src/main/target/MATEKF405SE/target.mk | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/target/MATEKF405SE/target.h b/src/main/target/MATEKF405SE/target.h index 006cbe36cd..828665bf4d 100644 --- a/src/main/target/MATEKF405SE/target.h +++ b/src/main/target/MATEKF405SE/target.h @@ -97,7 +97,7 @@ #define USE_BARO_BMP280 #define USE_BARO_MS5611 #define USE_BARO_BMP085 - +#define USE_BARO_DPS310 // *************** SD Card ************************** #define USE_SDCARD diff --git a/src/main/target/MATEKF405SE/target.mk b/src/main/target/MATEKF405SE/target.mk index 7bdb4a76e2..cace81a698 100644 --- a/src/main/target/MATEKF405SE/target.mk +++ b/src/main/target/MATEKF405SE/target.mk @@ -8,6 +8,7 @@ TARGET_SRC = \ drivers/barometer/barometer_bmp085.c \ drivers/barometer/barometer_bmp280.c \ drivers/barometer/barometer_ms5611.c \ + drivers/barometer/barometer_dps310.c \ drivers/compass/compass_hmc5883l.c \ drivers/compass/compass_qmc5883l.c \ drivers/max7456.c From ae846550342b0de50c2f63c15f4fe75443bd44ae Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sun, 19 Sep 2021 13:57:02 -0300 Subject: [PATCH 31/32] iflight f745 --- src/main/target/IFRC_IFLIGHT_F745_AIO/target.mk | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/target/IFRC_IFLIGHT_F745_AIO/target.mk b/src/main/target/IFRC_IFLIGHT_F745_AIO/target.mk index 52b07087de..a091d58793 100644 --- a/src/main/target/IFRC_IFLIGHT_F745_AIO/target.mk +++ b/src/main/target/IFRC_IFLIGHT_F745_AIO/target.mk @@ -5,6 +5,7 @@ TARGET_SRC = \ drivers/accgyro/accgyro_spi_mpu6000.c \ drivers/barometer/barometer_ms5611.c \ drivers/barometer/barometer_bmp280.c \ + drivers/barometer/barometer_dps310.c \ drivers/compass/compass_hmc5883l.c \ drivers/compass/compass_qmc5883l.c \ drivers/light_ws2811strip.c \ From c67d44a0ba4a9443384d5a294cb448a85cf28833 Mon Sep 17 00:00:00 2001 From: JuliooCesarMDM Date: Sun, 19 Sep 2021 15:01:22 -0300 Subject: [PATCH 32/32] force new build --- src/main/drivers/barometer/barometer_dps310.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/drivers/barometer/barometer_dps310.c b/src/main/drivers/barometer/barometer_dps310.c index e5418915c9..282eca402f 100644 --- a/src/main/drivers/barometer/barometer_dps310.c +++ b/src/main/drivers/barometer/barometer_dps310.c @@ -321,11 +321,11 @@ bool baroDPS310Detect(baroDev_t *baro) return false; } } - + /* if (!deviceConfigure(busdev)) { return false; } - +*/ const uint32_t baroDelay = 1000000 / 32 / 2; // twice the sample rate to capture all new data baro->ut_delay = 0;