2 * This file is part of 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/.
31 #include "build/build_config.h"
32 #include "build/debug.h"
33 #include "common/utils.h"
35 #include "drivers/io.h"
36 #include "drivers/bus.h"
37 #include "drivers/time.h"
38 #include "drivers/barometer/barometer.h"
39 #include "drivers/barometer/barometer_dps310.h"
41 // See datasheet at https://www.infineon.com/dgdl/Infineon-DPS310-DataSheet-v01_02-EN.pdf?fileId=5546d462576f34750157750826c42242
43 #if defined(USE_BARO) && defined(USE_BARO_DPS310)
45 #define DPS310_REG_PSR_B2 0x00
46 #define DPS310_REG_PSR_B1 0x01
47 #define DPS310_REG_PSR_B0 0x02
48 #define DPS310_REG_TMP_B2 0x03
49 #define DPS310_REG_TMP_B1 0x04
50 #define DPS310_REG_TMP_B0 0x05
51 #define DPS310_REG_PRS_CFG 0x06
52 #define DPS310_REG_TMP_CFG 0x07
53 #define DPS310_REG_MEAS_CFG 0x08
54 #define DPS310_REG_CFG_REG 0x09
56 #define DPS310_REG_RESET 0x0C
57 #define DPS310_REG_ID 0x0D
59 #define DPS310_REG_COEF 0x10
60 #define DPS310_REG_COEF_SRCE 0x28
63 #define DPS310_ID_REV_AND_PROD_ID (0x10)
65 #define DPS310_RESET_BIT_SOFT_RST (0x09) // 0b1001
67 #define DPS310_MEAS_CFG_COEF_RDY (1 << 7)
68 #define DPS310_MEAS_CFG_SENSOR_RDY (1 << 6)
69 #define DPS310_MEAS_CFG_TMP_RDY (1 << 5)
70 #define DPS310_MEAS_CFG_PRS_RDY (1 << 4)
72 #define DPS310_MEAS_CFG_MEAS_CTRL_MASK (0x7)
73 #define DPS310_MEAS_CFG_MEAS_CTRL_CONT (0x7)
74 #define DPS310_MEAS_CFG_MEAS_TEMP_SING (0x2)
75 #define DPS310_MEAS_CFG_MEAS_IDLE (0x0)
77 #define DPS310_PRS_CFG_BIT_PM_RATE_32HZ (0x50) // 101 - 32 measurements pr. sec.
78 #define DPS310_PRS_CFG_BIT_PM_PRC_16 (0x04) // 0100 - 16 times (Standard).
80 #define DPS310_TMP_CFG_BIT_TMP_EXT (0x80)
81 #define DPS310_TMP_CFG_BIT_TMP_RATE_32HZ (0x50) // 101 - 32 measurements pr. sec.
82 #define DPS310_TMP_CFG_BIT_TMP_PRC_16 (0x04) // 0100 - 16 times (Standard).
84 #define DPS310_CFG_REG_BIT_P_SHIFT (0x04)
85 #define DPS310_CFG_REG_BIT_T_SHIFT (0x08)
87 #define DPS310_COEF_SRCE_BIT_TMP_COEF_SRCE (0x80)
99 } calibrationCoefficients_t
;
102 calibrationCoefficients_t calib
;
103 float pressure
; // Pa
104 float temperature
; // DegC
107 static baroState_t baroState
;
111 static uint8_t registerRead(busDevice_t
* busDev
, uint8_t reg
)
114 busRead(busDev
, reg
, &buf
);
118 static void registerWrite(busDevice_t
* busDev
, uint8_t reg
, uint8_t value
)
120 busWrite(busDev
, reg
, value
);
123 static void registerWriteBits(busDevice_t
* busDev
, uint8_t reg
, uint8_t mask
, uint8_t bits
)
125 uint8_t val
= registerRead(busDev
, reg
);
127 if ((val
& mask
) != bits
) {
128 val
= (val
& (~mask
)) | bits
;
129 registerWrite(busDev
, reg
, val
);
133 static void registerSetBits(busDevice_t
* busDev
, uint8_t reg
, uint8_t setbits
)
135 registerWriteBits(busDev
, reg
, setbits
, setbits
);
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(busDevice_t
* busDev
)
150 // Trigger a chip reset
151 registerSetBits(busDev
, DPS310_REG_RESET
, DPS310_RESET_BIT_SOFT_RST
);
156 uint8_t status
= registerRead(busDev
, 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.
171 if (!busReadBuf(busDev
, DPS310_REG_COEF
, coef
, sizeof(coef
))) {
175 // 0x11 c0 [3:0] + 0x10 c0 [11:4]
176 baroState
.calib
.c0
= getTwosComplement(((uint32_t)coef
[0] << 4) | (((uint32_t)coef
[1] >> 4) & 0x0F), 12);
178 // 0x11 c1 [11:8] + 0x12 c1 [7:0]
179 baroState
.calib
.c1
= getTwosComplement((((uint32_t)coef
[1] & 0x0F) << 8) | (uint32_t)coef
[2], 12);
181 // 0x13 c00 [19:12] + 0x14 c00 [11:4] + 0x15 c00 [3:0]
182 baroState
.calib
.c00
= getTwosComplement(((uint32_t)coef
[3] << 12) | ((uint32_t)coef
[4] << 4) | (((uint32_t)coef
[5] >> 4) & 0x0F), 20);
184 // 0x15 c10 [19:16] + 0x16 c10 [15:8] + 0x17 c10 [7:0]
185 baroState
.calib
.c10
= getTwosComplement((((uint32_t)coef
[5] & 0x0F) << 16) | ((uint32_t)coef
[6] << 8) | (uint32_t)coef
[7], 20);
187 // 0x18 c01 [15:8] + 0x19 c01 [7:0]
188 baroState
.calib
.c01
= getTwosComplement(((uint32_t)coef
[8] << 8) | (uint32_t)coef
[9], 16);
190 // 0x1A c11 [15:8] + 0x1B c11 [7:0]
191 baroState
.calib
.c11
= getTwosComplement(((uint32_t)coef
[10] << 8) | (uint32_t)coef
[11], 16);
193 // 0x1C c20 [15:8] + 0x1D c20 [7:0]
194 baroState
.calib
.c20
= getTwosComplement(((uint32_t)coef
[12] << 8) | (uint32_t)coef
[13], 16);
196 // 0x1E c21 [15:8] + 0x1F c21 [7:0]
197 baroState
.calib
.c21
= getTwosComplement(((uint32_t)coef
[14] << 8) | (uint32_t)coef
[15], 16);
199 // 0x20 c30 [15:8] + 0x21 c30 [7:0]
200 baroState
.calib
.c30
= getTwosComplement(((uint32_t)coef
[16] << 8) | (uint32_t)coef
[17], 16);
202 // MEAS_CFG: Make sure the device is in IDLE mode
203 registerWriteBits(busDev
, DPS310_REG_MEAS_CFG
, DPS310_MEAS_CFG_MEAS_CTRL_MASK
, DPS310_MEAS_CFG_MEAS_IDLE
);
205 // Fix IC with a fuse bit problem, which lead to a wrong temperature
206 // Should not affect ICs without this problem
207 registerWrite(busDev
, 0x0E, 0xA5);
208 registerWrite(busDev
, 0x0F, 0x96);
209 registerWrite(busDev
, 0x62, 0x02);
210 registerWrite(busDev
, 0x0E, 0x00);
211 registerWrite(busDev
, 0x0F, 0x00);
213 // Make ONE temperature measurement and flush it
214 registerWriteBits(busDev
, DPS310_REG_MEAS_CFG
, DPS310_MEAS_CFG_MEAS_CTRL_MASK
, DPS310_MEAS_CFG_MEAS_TEMP_SING
);
217 // PRS_CFG: pressure measurement rate (32 Hz) and oversampling (16 time standard)
218 registerSetBits(busDev
, DPS310_REG_PRS_CFG
, DPS310_PRS_CFG_BIT_PM_RATE_32HZ
| DPS310_PRS_CFG_BIT_PM_PRC_16
);
220 // TMP_CFG: temperature measurement rate (32 Hz) and oversampling (16 times)
221 const uint8_t TMP_COEF_SRCE
= registerRead(busDev
, DPS310_REG_COEF_SRCE
) & DPS310_COEF_SRCE_BIT_TMP_COEF_SRCE
;
222 registerSetBits(busDev
, DPS310_REG_TMP_CFG
, DPS310_TMP_CFG_BIT_TMP_RATE_32HZ
| DPS310_TMP_CFG_BIT_TMP_PRC_16
| TMP_COEF_SRCE
);
224 // CFG_REG: set pressure and temperature result bit-shift (required when the oversampling rate is >8 times)
225 registerSetBits(busDev
, DPS310_REG_CFG_REG
, DPS310_CFG_REG_BIT_T_SHIFT
| DPS310_CFG_REG_BIT_P_SHIFT
);
227 // MEAS_CFG: Continuous pressure and temperature measurement
228 registerWriteBits(busDev
, DPS310_REG_MEAS_CFG
, DPS310_MEAS_CFG_MEAS_CTRL_MASK
, DPS310_MEAS_CFG_MEAS_CTRL_CONT
);
233 static bool deviceReadMeasurement(baroDev_t
*baro
)
235 // 1. Check if pressure is ready
236 bool pressure_ready
= registerRead(baro
->busDev
, DPS310_REG_MEAS_CFG
) & DPS310_MEAS_CFG_PRS_RDY
;
237 if (!pressure_ready
) {
241 // 2. Choose scaling factors kT (for temperature) and kP (for pressure) based on the chosen precision rate.
242 // The scaling factors are listed in Table 9.
243 static float kT
= 253952; // 16 times (Standard)
244 static float kP
= 253952; // 16 times (Standard)
246 // 3. Read the pressure and temperature result from the registers
247 // Read PSR_B2, PSR_B1, PSR_B0, TMP_B2, TMP_B1, TMP_B0
249 if (!busReadBuf(baro
->busDev
, DPS310_REG_PSR_B2
, buf
, 6)) {
253 const int32_t Praw
= getTwosComplement((buf
[0] << 16) + (buf
[1] << 8) + buf
[2], 24);
254 const int32_t Traw
= getTwosComplement((buf
[3] << 16) + (buf
[4] << 8) + buf
[5], 24);
256 // 4. Calculate scaled measurement results.
257 const float Praw_sc
= Praw
/ kP
;
258 const float Traw_sc
= Traw
/ kT
;
260 // 5. Calculate compensated measurement results.
261 const float c00
= baroState
.calib
.c00
;
262 const float c01
= baroState
.calib
.c01
;
263 const float c10
= baroState
.calib
.c10
;
264 const float c11
= baroState
.calib
.c11
;
265 const float c20
= baroState
.calib
.c20
;
266 const float c21
= baroState
.calib
.c21
;
267 const float c30
= baroState
.calib
.c30
;
269 // See section 4.9.1, How to Calculate Compensated Pressure Values, of datasheet
270 baroState
.pressure
= c00
+ Praw_sc
* (c10
+ Praw_sc
* (c20
+ Praw_sc
* c30
)) + Traw_sc
* c01
+ Traw_sc
* Praw_sc
* (c11
+ Praw_sc
* c21
);
272 const float c0
= baroState
.calib
.c0
;
273 const float c1
= baroState
.calib
.c1
;
275 // See section 4.9.2, How to Calculate Compensated Temperature Values, of datasheet
276 baroState
.temperature
= c0
* 0.5f
+ c1
* Traw_sc
;
281 static bool deviceCalculate(baroDev_t
*baro
, int32_t *pressure
, int32_t *temperature
)
286 *pressure
= baroState
.pressure
;
290 *temperature
= (baroState
.temperature
* 100); // to centidegrees
298 #define DETECTION_MAX_RETRY_COUNT 5
299 static bool deviceDetect(busDevice_t
* busDev
)
301 for (int retry
= 0; retry
< DETECTION_MAX_RETRY_COUNT
; retry
++) {
306 bool ack
= busReadBuf(busDev
, DPS310_REG_ID
, chipId
, 1);
308 if (ack
&& chipId
[0] == DPS310_ID_REV_AND_PROD_ID
) {
316 bool baroDPS310Detect(baroDev_t
*baro
)
318 baro
->busDev
= busDeviceInit(BUSTYPE_ANY
, DEVHW_DPS310
, 0, OWNER_BARO
);
319 if (baro
->busDev
== NULL
) {
323 if (!deviceDetect(baro
->busDev
)) {
324 busDeviceDeInit(baro
->busDev
);
328 if (!deviceConfigure(baro
->busDev
)) {
329 busDeviceDeInit(baro
->busDev
);
333 const uint32_t baroDelay
= 1000000 / 32 / 2; // twice the sample rate to capture all new data
336 baro
->start_ut
= NULL
;
339 baro
->up_delay
= baroDelay
;
340 baro
->start_up
= NULL
;
341 baro
->get_up
= deviceReadMeasurement
;
343 baro
->calculate
= deviceCalculate
;