2 * This file is part of Cleanflight, Betaflight and INAV.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6 * You can obtain one at http://mozilla.org/MPL/2.0/.
8 * Alternatively, the contents of this file may be used under the terms
9 * of the GNU General Public License Version 3, as described below:
11 * This file is free software: you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by the
13 * Free Software Foundation, either version 3 of the License, or (at your
14 * option) any later version.
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/.
24 * Copyright: INAVFLIGHT OU
33 #include "build/build_config.h"
34 #include "build/debug.h"
35 #include "common/utils.h"
37 #include "drivers/io.h"
38 #include "drivers/bus.h"
39 #include "drivers/bus_spi.h"
40 #include "drivers/time.h"
41 #include "drivers/barometer/barometer.h"
42 #include "drivers/barometer/barometer_dps310.h"
43 #include "drivers/resource.h"
45 // 10 MHz max SPI frequency
46 #define DPS310_MAX_SPI_CLK_HZ 10000000
48 #if defined(USE_BARO) && defined(USE_BARO_DPS310)
50 #define DPS310_I2C_ADDR 0x76
52 #define DPS310_REG_PSR_B2 0x00
53 #define DPS310_REG_PSR_B1 0x01
54 #define DPS310_REG_PSR_B0 0x02
55 #define DPS310_REG_TMP_B2 0x03
56 #define DPS310_REG_TMP_B1 0x04
57 #define DPS310_REG_TMP_B0 0x05
58 #define DPS310_REG_PRS_CFG 0x06
59 #define DPS310_REG_TMP_CFG 0x07
60 #define DPS310_REG_MEAS_CFG 0x08
61 #define DPS310_REG_CFG_REG 0x09
63 #define DPS310_REG_RESET 0x0C
64 #define DPS310_REG_ID 0x0D
66 #define DPS310_REG_COEF 0x10
67 #define DPS310_REG_COEF_SRCE 0x28
70 #define DPS310_ID_REV_AND_PROD_ID (0x10)
72 #define DPS310_RESET_BIT_SOFT_RST (0x09) // 0b1001
74 #define DPS310_MEAS_CFG_COEF_RDY (1 << 7)
75 #define DPS310_MEAS_CFG_SENSOR_RDY (1 << 6)
76 #define DPS310_MEAS_CFG_TMP_RDY (1 << 5)
77 #define DPS310_MEAS_CFG_PRS_RDY (1 << 4)
78 #define DPS310_MEAS_CFG_MEAS_CTRL_CONT (0x7)
80 #define DPS310_PRS_CFG_BIT_PM_RATE_32HZ (0x50) // 101 - 32 measurements pr. sec.
81 #define DPS310_PRS_CFG_BIT_PM_PRC_16 (0x04) // 0100 - 16 times (Standard).
83 #define DPS310_TMP_CFG_BIT_TMP_EXT (0x80) //
84 #define DPS310_TMP_CFG_BIT_TMP_RATE_32HZ (0x50) // 101 - 32 measurements pr. sec.
85 #define DPS310_TMP_CFG_BIT_TMP_PRC_16 (0x04) // 0100 - 16 times (Standard).
87 #define DPS310_CFG_REG_BIT_P_SHIFT (0x04)
88 #define DPS310_CFG_REG_BIT_T_SHIFT (0x08)
90 #define DPS310_COEF_SRCE_BIT_TMP_COEF_SRCE (0x80)
100 int16_t c21
; // 16bit
101 int16_t c30
; // 16bit
102 } calibrationCoefficients_t
;
105 calibrationCoefficients_t calib
;
106 float pressure
; // Pa
107 float temperature
; // DegC
110 static baroState_t baroState
;
112 #define busReadBuf busReadRegisterBuffer
113 #define busWrite busWriteRegister
115 static uint8_t buf
[6];
118 static uint8_t registerRead(const extDevice_t
*dev
, uint8_t reg
)
120 return busReadRegister(dev
, reg
);
123 static void registerWrite(const extDevice_t
*dev
, uint8_t reg
, uint8_t value
)
125 busWrite(dev
, reg
, value
);
128 static void registerSetBits(const extDevice_t
*dev
, uint8_t reg
, uint8_t setbits
)
130 uint8_t val
= registerRead(dev
, reg
);
132 if ((val
& setbits
) != setbits
) {
134 registerWrite(dev
, reg
, val
);
138 static int32_t getTwosComplement(uint32_t raw
, uint8_t length
)
140 if (raw
& ((int)1 << (length
- 1))) {
141 return ((int32_t)raw
) - ((int32_t)1 << length
);
148 static bool deviceConfigure(const extDevice_t
*dev
)
150 // Trigger a chip reset
151 registerSetBits(dev
, DPS310_REG_RESET
, DPS310_RESET_BIT_SOFT_RST
);
156 uint8_t status
= registerRead(dev
, DPS310_REG_MEAS_CFG
);
158 // Check if coefficients are available
159 if ((status
& DPS310_MEAS_CFG_COEF_RDY
) == 0) {
163 // Check if sensor initialization is complete
164 if ((status
& DPS310_MEAS_CFG_SENSOR_RDY
) == 0) {
168 // 1. Read the pressure calibration coefficients (c00, c10, c20, c30, c01, c11, and c21) from the Calibration Coefficient register.
169 // Note: The coefficients read from the coefficient register are 2's complement numbers.
170 // 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.
171 #define COEFFICIENT_LENGTH 18
172 #define READ_LENGTH (COEFFICIENT_LENGTH / 2)
174 uint8_t coef
[COEFFICIENT_LENGTH
];
175 if (!busReadBuf(dev
, DPS310_REG_COEF
, coef
, READ_LENGTH
)) {
178 if (!busReadBuf(dev
, DPS310_REG_COEF
+ READ_LENGTH
, coef
+ READ_LENGTH
, COEFFICIENT_LENGTH
- READ_LENGTH
)) {
182 // 0x11 c0 [3:0] + 0x10 c0 [11:4]
183 baroState
.calib
.c0
= getTwosComplement(((uint32_t)coef
[0] << 4) | (((uint32_t)coef
[1] >> 4) & 0x0F), 12);
185 // 0x11 c1 [11:8] + 0x12 c1 [7:0]
186 baroState
.calib
.c1
= getTwosComplement((((uint32_t)coef
[1] & 0x0F) << 8) | (uint32_t)coef
[2], 12);
188 // 0x13 c00 [19:12] + 0x14 c00 [11:4] + 0x15 c00 [3:0]
189 baroState
.calib
.c00
= getTwosComplement(((uint32_t)coef
[3] << 12) | ((uint32_t)coef
[4] << 4) | (((uint32_t)coef
[5] >> 4) & 0x0F), 20);
191 // 0x15 c10 [19:16] + 0x16 c10 [15:8] + 0x17 c10 [7:0]
192 baroState
.calib
.c10
= getTwosComplement((((uint32_t)coef
[5] & 0x0F) << 16) | ((uint32_t)coef
[6] << 8) | (uint32_t)coef
[7], 20);
194 // 0x18 c01 [15:8] + 0x19 c01 [7:0]
195 baroState
.calib
.c01
= getTwosComplement(((uint32_t)coef
[8] << 8) | (uint32_t)coef
[9], 16);
197 // 0x1A c11 [15:8] + 0x1B c11 [7:0]
198 baroState
.calib
.c11
= getTwosComplement(((uint32_t)coef
[8] << 8) | (uint32_t)coef
[9], 16);
200 // 0x1C c20 [15:8] + 0x1D c20 [7:0]
201 baroState
.calib
.c20
= getTwosComplement(((uint32_t)coef
[12] << 8) | (uint32_t)coef
[13], 16);
203 // 0x1E c21 [15:8] + 0x1F c21 [7:0]
204 baroState
.calib
.c21
= getTwosComplement(((uint32_t)coef
[14] << 8) | (uint32_t)coef
[15], 16);
206 // 0x20 c30 [15:8] + 0x21 c30 [7:0]
207 baroState
.calib
.c30
= getTwosComplement(((uint32_t)coef
[16] << 8) | (uint32_t)coef
[17], 16);
209 // PRS_CFG: pressure measurement rate (32 Hz) and oversampling (16 time standard)
210 registerSetBits(dev
, DPS310_REG_PRS_CFG
, DPS310_PRS_CFG_BIT_PM_RATE_32HZ
| DPS310_PRS_CFG_BIT_PM_PRC_16
);
212 // TMP_CFG: temperature measurement rate (32 Hz) and oversampling (16 times)
213 const uint8_t TMP_COEF_SRCE
= registerRead(dev
, DPS310_REG_COEF_SRCE
) & DPS310_COEF_SRCE_BIT_TMP_COEF_SRCE
;
214 registerSetBits(dev
, DPS310_REG_TMP_CFG
, DPS310_TMP_CFG_BIT_TMP_RATE_32HZ
| DPS310_TMP_CFG_BIT_TMP_PRC_16
| TMP_COEF_SRCE
);
216 // CFG_REG: set pressure and temperature result bit-shift (required when the oversampling rate is >8 times)
217 registerSetBits(dev
, DPS310_REG_CFG_REG
, DPS310_CFG_REG_BIT_T_SHIFT
| DPS310_CFG_REG_BIT_P_SHIFT
);
219 // MEAS_CFG: Continuous pressure and temperature measurement
220 registerSetBits(dev
, DPS310_REG_MEAS_CFG
, DPS310_MEAS_CFG_MEAS_CTRL_CONT
);
225 static bool dps310ReadUP(baroDev_t
*baro
)
227 if (busBusy(&baro
->dev
, NULL
)) {
232 // No need to poll for data ready as the conversion rate is 32Hz and this is sampling at 20Hz
233 // Read PSR_B2, PSR_B1, PSR_B0, TMP_B2, TMP_B1, TMP_B0
234 busReadRegisterBufferStart(&baro
->dev
, DPS310_REG_PSR_B2
, buf
, 6);
239 static bool dps310GetUP(baroDev_t
*baro
)
243 // 2. Choose scaling factors kT (for temperature) and kP (for pressure) based on the chosen precision rate.
244 // The scaling factors are listed in Table 9.
245 static float kT
= 253952; // 16 times (Standard)
246 static float kP
= 253952; // 16 times (Standard)
248 // 3. Read the pressure and temperature result from the registers
250 const int32_t Praw
= getTwosComplement((buf
[0] << 16) + (buf
[1] << 8) + buf
[2], 24);
251 const int32_t Traw
= getTwosComplement((buf
[3] << 16) + (buf
[4] << 8) + buf
[5], 24);
253 // 4. Calculate scaled measurement results.
254 const float Praw_sc
= Praw
/ kP
;
255 const float Traw_sc
= Traw
/ kT
;
257 // 5. Calculate compensated measurement results.
258 const float c00
= baroState
.calib
.c00
;
259 const float c01
= baroState
.calib
.c01
;
260 const float c10
= baroState
.calib
.c10
;
261 const float c11
= baroState
.calib
.c11
;
262 const float c20
= baroState
.calib
.c20
;
263 const float c21
= baroState
.calib
.c21
;
264 const float c30
= baroState
.calib
.c30
;
266 const float c0
= baroState
.calib
.c0
;
267 const float c1
= baroState
.calib
.c1
;
269 baroState
.pressure
= c00
+ Praw_sc
* (c10
+ Praw_sc
* (c20
+ Praw_sc
* c30
)) + Traw_sc
* c01
+ Traw_sc
* Praw_sc
* (c11
+ Praw_sc
* c21
);
270 baroState
.temperature
= c0
* 0.5f
+ c1
* Traw_sc
;
275 static void deviceCalculate(int32_t *pressure
, int32_t *temperature
)
278 *pressure
= baroState
.pressure
;
282 *temperature
= (baroState
.temperature
* 100); // to centidegrees
288 #define DETECTION_MAX_RETRY_COUNT 5
289 static bool deviceDetect(const extDevice_t
*dev
)
291 for (int retry
= 0; retry
< DETECTION_MAX_RETRY_COUNT
; retry
++) {
296 bool ack
= busReadBuf(dev
, DPS310_REG_ID
, chipId
, 1);
298 if (ack
&& chipId
[0] == DPS310_ID_REV_AND_PROD_ID
) {
306 static void dps310StartUT(baroDev_t
*baro
)
311 static bool dps310ReadUT(baroDev_t
*baro
)
318 static bool dps310GetUT(baroDev_t
*baro
)
325 static void dps310StartUP(baroDev_t
*baro
)
330 static void deviceInit(const extDevice_t
*dev
, resourceOwner_e owner
)
332 #ifdef USE_BARO_SPI_DPS310
333 if (dev
->bus
->busType
== BUS_TYPE_SPI
) {
334 IOHi(dev
->busType_u
.spi
.csnPin
); // Disable
335 IOInit(dev
->busType_u
.spi
.csnPin
, owner
, 0);
336 IOConfigGPIO(dev
->busType_u
.spi
.csnPin
, IOCFG_OUT_PP
);
337 spiSetClkDivisor(dev
, spiCalculateDivider(DPS310_MAX_SPI_CLK_HZ
));
345 static void deviceDeInit(const extDevice_t
*dev
)
347 #ifdef USE_BARO_SPI_DPS310
348 if (dev
->bus
->busType
== BUS_TYPE_SPI
) {
349 spiPreinitByIO(dev
->busType_u
.spi
.csnPin
);
356 bool baroDPS310Detect(baroDev_t
*baro
)
358 extDevice_t
*dev
= &baro
->dev
;
359 bool defaultAddressApplied
= false;
361 deviceInit(&baro
->dev
, OWNER_BARO_CS
);
363 if ((dev
->bus
->busType
== BUS_TYPE_I2C
) && (dev
->busType_u
.i2c
.address
== 0)) {
364 // Default address for BMP280
365 dev
->busType_u
.i2c
.address
= DPS310_I2C_ADDR
;
366 defaultAddressApplied
= true;
369 if (!deviceDetect(dev
)) {
371 if (defaultAddressApplied
) {
372 dev
->busType_u
.i2c
.address
= 0;
377 if (!deviceConfigure(dev
)) {
382 busDeviceRegister(dev
);
385 baro
->start_ut
= dps310StartUT
;
386 baro
->read_ut
= dps310ReadUT
;
387 baro
->get_ut
= dps310GetUT
;
389 baro
->up_delay
= 45000; // 45ms delay plus 5 1ms cycles 50ms
390 baro
->start_up
= dps310StartUP
;
391 baro
->read_up
= dps310ReadUP
;
392 baro
->get_up
= dps310GetUP
;
394 baro
->calculate
= deviceCalculate
;