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
27 // See datasheet at https://www.infineon.com/dgdl/Infineon-DPS310-DataSheet-v01_02-EN.pdf?fileId=5546d462576f34750157750826c42242
35 #include "build/build_config.h"
36 #include "build/debug.h"
37 #include "common/utils.h"
39 #include "drivers/io.h"
40 #include "drivers/bus.h"
41 #include "drivers/bus_spi.h"
42 #include "drivers/time.h"
43 #include "drivers/barometer/barometer.h"
44 #include "drivers/barometer/barometer_dps310.h"
45 #include "drivers/resource.h"
47 // 10 MHz max SPI frequency
48 #define DPS310_MAX_SPI_CLK_HZ 10000000
50 #if defined(USE_BARO) && (defined(USE_BARO_DPS310) || defined(USE_BARO_SPI_DPS310))
52 #define DPS310_I2C_ADDR 0x76
54 #define DPS310_REG_PSR_B2 0x00
55 #define DPS310_REG_PSR_B1 0x01
56 #define DPS310_REG_PSR_B0 0x02
57 #define DPS310_REG_TMP_B2 0x03
58 #define DPS310_REG_TMP_B1 0x04
59 #define DPS310_REG_TMP_B0 0x05
60 #define DPS310_REG_PRS_CFG 0x06
61 #define DPS310_REG_TMP_CFG 0x07
62 #define DPS310_REG_MEAS_CFG 0x08
63 #define DPS310_REG_CFG_REG 0x09
65 #define DPS310_REG_RESET 0x0C
66 #define DPS310_REG_ID 0x0D
68 #define DPS310_REG_COEF 0x10
69 #define DPS310_REG_COEF_SRCE 0x28
72 #define DPS310_ID_REV_AND_PROD_ID (0x10)
74 #define DPS310_RESET_BIT_SOFT_RST (0x09) // 0b1001
76 #define DPS310_MEAS_CFG_COEF_RDY (1 << 7)
77 #define DPS310_MEAS_CFG_SENSOR_RDY (1 << 6)
78 #define DPS310_MEAS_CFG_TMP_RDY (1 << 5)
79 #define DPS310_MEAS_CFG_PRS_RDY (1 << 4)
80 #define DPS310_MEAS_CFG_MEAS_CTRL_CONT (0x7)
82 #define DPS310_PRS_CFG_BIT_PM_RATE_32HZ (0x50) // 101 - 32 measurements pr. sec.
83 #define DPS310_PRS_CFG_BIT_PM_PRC_16 (0x04) // 0100 - 16 times (Standard).
85 #define DPS310_TMP_CFG_BIT_TMP_EXT (0x80) //
86 #define DPS310_TMP_CFG_BIT_TMP_RATE_32HZ (0x50) // 101 - 32 measurements pr. sec.
87 #define DPS310_TMP_CFG_BIT_TMP_PRC_16 (0x04) // 0100 - 16 times (Standard).
89 #define DPS310_CFG_REG_BIT_P_SHIFT (0x04)
90 #define DPS310_CFG_REG_BIT_T_SHIFT (0x08)
92 #define DPS310_COEF_SRCE_BIT_TMP_COEF_SRCE (0x80)
100 int16_t c11
; // 16bit
101 int16_t c20
; // 16bit
102 int16_t c21
; // 16bit
103 int16_t c30
; // 16bit
104 } calibrationCoefficients_t
;
107 calibrationCoefficients_t calib
;
108 float pressure
; // Pa
109 float temperature
; // DegC
112 static baroState_t baroState
;
114 #define busReadBuf busReadRegisterBuffer
115 #define busWrite busWriteRegister
117 static uint8_t buf
[6];
120 static uint8_t registerRead(const extDevice_t
*dev
, uint8_t reg
)
122 return busReadRegister(dev
, reg
);
125 static void registerWrite(const extDevice_t
*dev
, uint8_t reg
, uint8_t value
)
127 busWrite(dev
, reg
, value
);
130 static void registerSetBits(const extDevice_t
*dev
, uint8_t reg
, uint8_t setbits
)
132 uint8_t val
= registerRead(dev
, reg
);
134 if ((val
& setbits
) != setbits
) {
136 registerWrite(dev
, reg
, val
);
140 static int32_t getTwosComplement(uint32_t raw
, uint8_t length
)
142 if (raw
& ((int)1 << (length
- 1))) {
143 return ((int32_t)raw
) - ((int32_t)1 << length
);
150 static bool deviceConfigure(const extDevice_t
*dev
)
152 // Trigger a chip reset
153 registerSetBits(dev
, DPS310_REG_RESET
, DPS310_RESET_BIT_SOFT_RST
);
158 uint8_t status
= registerRead(dev
, DPS310_REG_MEAS_CFG
);
160 // Check if coefficients are available
161 if ((status
& DPS310_MEAS_CFG_COEF_RDY
) == 0) {
165 // Check if sensor initialization is complete
166 if ((status
& DPS310_MEAS_CFG_SENSOR_RDY
) == 0) {
170 // 1. Read the pressure calibration coefficients (c00, c10, c20, c30, c01, c11, and c21) from the Calibration Coefficient register.
171 // Note: The coefficients read from the coefficient register are 2's complement numbers.
172 // 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.
173 #define COEFFICIENT_LENGTH 18
174 #define READ_LENGTH (COEFFICIENT_LENGTH / 2)
176 uint8_t coef
[COEFFICIENT_LENGTH
];
177 if (!busReadBuf(dev
, DPS310_REG_COEF
, coef
, READ_LENGTH
)) {
180 if (!busReadBuf(dev
, DPS310_REG_COEF
+ READ_LENGTH
, coef
+ READ_LENGTH
, COEFFICIENT_LENGTH
- READ_LENGTH
)) {
184 // See section 8.11, Calibration Coefficients (COEF), of datasheet
186 // 0x11 c0 [3:0] + 0x10 c0 [11:4]
187 baroState
.calib
.c0
= getTwosComplement(((uint32_t)coef
[0] << 4) | (((uint32_t)coef
[1] >> 4) & 0x0F), 12);
189 // 0x11 c1 [11:8] + 0x12 c1 [7:0]
190 baroState
.calib
.c1
= getTwosComplement((((uint32_t)coef
[1] & 0x0F) << 8) | (uint32_t)coef
[2], 12);
192 // 0x13 c00 [19:12] + 0x14 c00 [11:4] + 0x15 c00 [3:0]
193 baroState
.calib
.c00
= getTwosComplement(((uint32_t)coef
[3] << 12) | ((uint32_t)coef
[4] << 4) | (((uint32_t)coef
[5] >> 4) & 0x0F), 20);
195 // 0x15 c10 [19:16] + 0x16 c10 [15:8] + 0x17 c10 [7:0]
196 baroState
.calib
.c10
= getTwosComplement((((uint32_t)coef
[5] & 0x0F) << 16) | ((uint32_t)coef
[6] << 8) | (uint32_t)coef
[7], 20);
198 // 0x18 c01 [15:8] + 0x19 c01 [7:0]
199 baroState
.calib
.c01
= getTwosComplement(((uint32_t)coef
[8] << 8) | (uint32_t)coef
[9], 16);
201 // 0x1A c11 [15:8] + 0x1B c11 [7:0]
202 baroState
.calib
.c11
= getTwosComplement(((uint32_t)coef
[10] << 8) | (uint32_t)coef
[11], 16);
204 // 0x1C c20 [15:8] + 0x1D c20 [7:0]
205 baroState
.calib
.c20
= getTwosComplement(((uint32_t)coef
[12] << 8) | (uint32_t)coef
[13], 16);
207 // 0x1E c21 [15:8] + 0x1F c21 [7:0]
208 baroState
.calib
.c21
= getTwosComplement(((uint32_t)coef
[14] << 8) | (uint32_t)coef
[15], 16);
210 // 0x20 c30 [15:8] + 0x21 c30 [7:0]
211 baroState
.calib
.c30
= getTwosComplement(((uint32_t)coef
[16] << 8) | (uint32_t)coef
[17], 16);
213 // PRS_CFG: pressure measurement rate (32 Hz) and oversampling (16 time standard)
214 registerSetBits(dev
, DPS310_REG_PRS_CFG
, DPS310_PRS_CFG_BIT_PM_RATE_32HZ
| DPS310_PRS_CFG_BIT_PM_PRC_16
);
216 // TMP_CFG: temperature measurement rate (32 Hz) and oversampling (16 times)
217 const uint8_t TMP_COEF_SRCE
= registerRead(dev
, DPS310_REG_COEF_SRCE
) & DPS310_COEF_SRCE_BIT_TMP_COEF_SRCE
;
218 registerSetBits(dev
, DPS310_REG_TMP_CFG
, DPS310_TMP_CFG_BIT_TMP_RATE_32HZ
| DPS310_TMP_CFG_BIT_TMP_PRC_16
| TMP_COEF_SRCE
);
220 // CFG_REG: set pressure and temperature result bit-shift (required when the oversampling rate is >8 times)
221 registerSetBits(dev
, DPS310_REG_CFG_REG
, DPS310_CFG_REG_BIT_T_SHIFT
| DPS310_CFG_REG_BIT_P_SHIFT
);
223 // MEAS_CFG: Continuous pressure and temperature measurement
224 registerSetBits(dev
, DPS310_REG_MEAS_CFG
, DPS310_MEAS_CFG_MEAS_CTRL_CONT
);
229 static bool dps310ReadUP(baroDev_t
*baro
)
231 if (busBusy(&baro
->dev
, NULL
)) {
236 // No need to poll for data ready as the conversion rate is 32Hz and this is sampling at 20Hz
237 // Read PSR_B2, PSR_B1, PSR_B0, TMP_B2, TMP_B1, TMP_B0
238 busReadRegisterBufferStart(&baro
->dev
, DPS310_REG_PSR_B2
, buf
, 6);
243 static bool dps310GetUP(baroDev_t
*baro
)
247 // 2. Choose scaling factors kT (for temperature) and kP (for pressure) based on the chosen precision rate.
248 // The scaling factors are listed in Table 9.
249 static float kT
= 253952; // 16 times (Standard)
250 static float kP
= 253952; // 16 times (Standard)
252 // 3. Read the pressure and temperature result from the registers
254 const int32_t Praw
= getTwosComplement((buf
[0] << 16) + (buf
[1] << 8) + buf
[2], 24);
255 const int32_t Traw
= getTwosComplement((buf
[3] << 16) + (buf
[4] << 8) + buf
[5], 24);
257 // 4. Calculate scaled measurement results.
258 const float Praw_sc
= Praw
/ kP
;
259 const float Traw_sc
= Traw
/ kT
;
261 // 5. Calculate compensated measurement results.
262 const float c00
= baroState
.calib
.c00
;
263 const float c01
= baroState
.calib
.c01
;
264 const float c10
= baroState
.calib
.c10
;
265 const float c11
= baroState
.calib
.c11
;
266 const float c20
= baroState
.calib
.c20
;
267 const float c21
= baroState
.calib
.c21
;
268 const float c30
= baroState
.calib
.c30
;
270 // See section 4.9.1, How to Calculate Compensated Pressure Values, of datasheet
271 baroState
.pressure
= c00
+ Praw_sc
* (c10
+ Praw_sc
* (c20
+ Praw_sc
* c30
)) + Traw_sc
* c01
+ Traw_sc
* Praw_sc
* (c11
+ Praw_sc
* c21
);
273 const float c0
= baroState
.calib
.c0
;
274 const float c1
= baroState
.calib
.c1
;
276 // See section 4.9.2, How to Calculate Compensated Temperature Values, of datasheet
277 baroState
.temperature
= c0
* 0.5f
+ c1
* Traw_sc
;
282 static void deviceCalculate(int32_t *pressure
, int32_t *temperature
)
285 *pressure
= baroState
.pressure
;
289 *temperature
= (baroState
.temperature
* 100); // to centidegrees
295 #define DETECTION_MAX_RETRY_COUNT 5
296 static bool deviceDetect(const extDevice_t
*dev
)
298 for (int retry
= 0; retry
< DETECTION_MAX_RETRY_COUNT
; retry
++) {
303 bool ack
= busReadBuf(dev
, DPS310_REG_ID
, chipId
, 1);
305 if (ack
&& chipId
[0] == DPS310_ID_REV_AND_PROD_ID
) {
313 static void dps310StartUT(baroDev_t
*baro
)
318 static bool dps310ReadUT(baroDev_t
*baro
)
325 static bool dps310GetUT(baroDev_t
*baro
)
332 static void dps310StartUP(baroDev_t
*baro
)
337 static void deviceInit(const extDevice_t
*dev
, resourceOwner_e owner
)
339 #ifdef USE_BARO_SPI_DPS310
340 if (dev
->bus
->busType
== BUS_TYPE_SPI
) {
341 IOHi(dev
->busType_u
.spi
.csnPin
); // Disable
342 IOInit(dev
->busType_u
.spi
.csnPin
, owner
, 0);
343 IOConfigGPIO(dev
->busType_u
.spi
.csnPin
, IOCFG_OUT_PP
);
344 spiSetClkDivisor(dev
, spiCalculateDivider(DPS310_MAX_SPI_CLK_HZ
));
352 static void deviceDeInit(const extDevice_t
*dev
)
354 #ifdef USE_BARO_SPI_DPS310
355 if (dev
->bus
->busType
== BUS_TYPE_SPI
) {
356 spiPreinitByIO(dev
->busType_u
.spi
.csnPin
);
363 bool baroDPS310Detect(baroDev_t
*baro
)
365 extDevice_t
*dev
= &baro
->dev
;
366 bool defaultAddressApplied
= false;
368 deviceInit(&baro
->dev
, OWNER_BARO_CS
);
370 if ((dev
->bus
->busType
== BUS_TYPE_I2C
) && (dev
->busType_u
.i2c
.address
== 0)) {
371 // Default address for BMP280
372 dev
->busType_u
.i2c
.address
= DPS310_I2C_ADDR
;
373 defaultAddressApplied
= true;
376 if (!deviceDetect(dev
)) {
378 if (defaultAddressApplied
) {
379 dev
->busType_u
.i2c
.address
= 0;
384 if (!deviceConfigure(dev
)) {
389 busDeviceRegister(dev
);
392 baro
->start_ut
= dps310StartUT
;
393 baro
->read_ut
= dps310ReadUT
;
394 baro
->get_ut
= dps310GetUT
;
396 baro
->up_delay
= 45000; // 45ms delay plus 5 1ms cycles 50ms
397 baro
->start_up
= dps310StartUP
;
398 baro
->read_up
= dps310ReadUP
;
399 baro
->get_up
= dps310GetUP
;
401 baro
->calculate
= deviceCalculate
;