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)
64 #define SPL07_003_CHIP_ID (0x11)
66 #define DPS310_RESET_BIT_SOFT_RST (0x09) // 0b1001
68 #define DPS310_MEAS_CFG_COEF_RDY (1 << 7)
69 #define DPS310_MEAS_CFG_SENSOR_RDY (1 << 6)
70 #define DPS310_MEAS_CFG_TMP_RDY (1 << 5)
71 #define DPS310_MEAS_CFG_PRS_RDY (1 << 4)
73 #define DPS310_MEAS_CFG_MEAS_CTRL_MASK (0x7)
74 #define DPS310_MEAS_CFG_MEAS_CTRL_CONT (0x7)
75 #define DPS310_MEAS_CFG_MEAS_TEMP_SING (0x2)
76 #define DPS310_MEAS_CFG_MEAS_IDLE (0x0)
78 #define DPS310_PRS_CFG_BIT_PM_RATE_32HZ (0x50) // 101 - 32 measurements pr. sec.
79 #define DPS310_PRS_CFG_BIT_PM_PRC_16 (0x04) // 0100 - 16 times (Standard).
81 #define DPS310_TMP_CFG_BIT_TMP_EXT (0x80)
82 #define DPS310_TMP_CFG_BIT_TMP_RATE_32HZ (0x50) // 101 - 32 measurements pr. sec.
83 #define DPS310_TMP_CFG_BIT_TMP_PRC_16 (0x04) // 0100 - 16 times (Standard).
85 #define DPS310_CFG_REG_BIT_P_SHIFT (0x04)
86 #define DPS310_CFG_REG_BIT_T_SHIFT (0x08)
88 #define DPS310_COEF_SRCE_BIT_TMP_COEF_SRCE (0x80)
100 int16_t c31
; // 12bit
101 int16_t c40
; // 12bit
102 } calibrationCoefficients_t
;
105 calibrationCoefficients_t calib
;
106 float pressure
; // Pa
107 float temperature
; // DegC
110 static baroState_t baroState
;
111 static uint8_t chipId
[1];
115 static uint8_t registerRead(busDevice_t
* busDev
, uint8_t reg
)
118 busRead(busDev
, reg
, &buf
);
122 static void registerWrite(busDevice_t
* busDev
, uint8_t reg
, uint8_t value
)
124 busWrite(busDev
, reg
, value
);
127 static void registerWriteBits(busDevice_t
* busDev
, uint8_t reg
, uint8_t mask
, uint8_t bits
)
129 uint8_t val
= registerRead(busDev
, reg
);
131 if ((val
& mask
) != bits
) {
132 val
= (val
& (~mask
)) | bits
;
133 registerWrite(busDev
, reg
, val
);
137 static void registerSetBits(busDevice_t
* busDev
, uint8_t reg
, uint8_t setbits
)
139 registerWriteBits(busDev
, reg
, setbits
, setbits
);
142 static int32_t getTwosComplement(uint32_t raw
, uint8_t length
)
144 if (raw
& ((int)1 << (length
- 1))) {
145 return ((int32_t)raw
) - ((int32_t)1 << length
);
152 static bool deviceConfigure(busDevice_t
* busDev
)
154 // Trigger a chip reset
155 registerSetBits(busDev
, DPS310_REG_RESET
, DPS310_RESET_BIT_SOFT_RST
);
160 uint8_t status
= registerRead(busDev
, DPS310_REG_MEAS_CFG
);
162 // Check if coefficients are available
163 if ((status
& DPS310_MEAS_CFG_COEF_RDY
) == 0) {
167 // Check if sensor initialization is complete
168 if ((status
& DPS310_MEAS_CFG_SENSOR_RDY
) == 0) {
172 // 1. Read the pressure calibration coefficients (c00, c10, c20, c30, c01, c11, and c21) from the Calibration Coefficient register.
173 // Note: The coefficients read from the coefficient register are 2's complement numbers.
175 unsigned coefficientLength
= chipId
[0] == SPL07_003_CHIP_ID
? 21 : 18;
176 uint8_t coef
[coefficientLength
];
178 if (!busReadBuf(busDev
, DPS310_REG_COEF
, coef
, sizeof(coef
))) {
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
[10] << 8) | (uint32_t)coef
[11], 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 if (chipId
[0] == SPL07_003_CHIP_ID
) {
210 // 0x23 c31 [3:0] + 0x22 c31 [11:4]
211 baroState
.calib
.c31
= getTwosComplement(((uint32_t)coef
[18] << 4) | (((uint32_t)coef
[19] >> 4) & 0x0F), 12);
213 // 0x23 c40 [11:8] + 0x24 c40 [7:0]
214 baroState
.calib
.c40
= getTwosComplement((((uint32_t)coef
[19] & 0x0F) << 8) | (uint32_t)coef
[20], 12);
216 baroState
.calib
.c31
= 0;
217 baroState
.calib
.c40
= 0;
220 // MEAS_CFG: Make sure the device is in IDLE mode
221 registerWriteBits(busDev
, DPS310_REG_MEAS_CFG
, DPS310_MEAS_CFG_MEAS_CTRL_MASK
, DPS310_MEAS_CFG_MEAS_IDLE
);
223 // Fix IC with a fuse bit problem, which lead to a wrong temperature
224 // Should not affect ICs without this problem
225 registerWrite(busDev
, 0x0E, 0xA5);
226 registerWrite(busDev
, 0x0F, 0x96);
227 registerWrite(busDev
, 0x62, 0x02);
228 registerWrite(busDev
, 0x0E, 0x00);
229 registerWrite(busDev
, 0x0F, 0x00);
231 // Make ONE temperature measurement and flush it
232 registerWriteBits(busDev
, DPS310_REG_MEAS_CFG
, DPS310_MEAS_CFG_MEAS_CTRL_MASK
, DPS310_MEAS_CFG_MEAS_TEMP_SING
);
235 // PRS_CFG: pressure measurement rate (32 Hz) and oversampling (16 time standard)
236 registerSetBits(busDev
, DPS310_REG_PRS_CFG
, DPS310_PRS_CFG_BIT_PM_RATE_32HZ
| DPS310_PRS_CFG_BIT_PM_PRC_16
);
238 // TMP_CFG: temperature measurement rate (32 Hz) and oversampling (16 times)
239 if (chipId
[0] == SPL07_003_CHIP_ID
) {
240 registerSetBits(busDev
, DPS310_REG_TMP_CFG
, DPS310_TMP_CFG_BIT_TMP_RATE_32HZ
| DPS310_TMP_CFG_BIT_TMP_PRC_16
);
242 const uint8_t TMP_COEF_SRCE
= registerRead(busDev
, DPS310_REG_COEF_SRCE
) & DPS310_COEF_SRCE_BIT_TMP_COEF_SRCE
;
243 registerSetBits(busDev
, DPS310_REG_TMP_CFG
, DPS310_TMP_CFG_BIT_TMP_RATE_32HZ
| DPS310_TMP_CFG_BIT_TMP_PRC_16
| TMP_COEF_SRCE
);
246 // CFG_REG: set pressure and temperature result bit-shift (required when the oversampling rate is >8 times)
247 registerSetBits(busDev
, DPS310_REG_CFG_REG
, DPS310_CFG_REG_BIT_T_SHIFT
| DPS310_CFG_REG_BIT_P_SHIFT
);
249 // MEAS_CFG: Continuous pressure and temperature measurement
250 registerWriteBits(busDev
, DPS310_REG_MEAS_CFG
, DPS310_MEAS_CFG_MEAS_CTRL_MASK
, DPS310_MEAS_CFG_MEAS_CTRL_CONT
);
255 static bool deviceReadMeasurement(baroDev_t
*baro
)
257 // 1. Check if pressure is ready
258 bool pressure_ready
= registerRead(baro
->busDev
, DPS310_REG_MEAS_CFG
) & DPS310_MEAS_CFG_PRS_RDY
;
259 if (!pressure_ready
) {
263 // 2. Choose scaling factors kT (for temperature) and kP (for pressure) based on the chosen precision rate.
264 // The scaling factors are listed in Table 9.
265 static float kT
= 253952; // 16 times (Standard)
266 static float kP
= 253952; // 16 times (Standard)
268 // 3. Read the pressure and temperature result from the registers
269 // Read PSR_B2, PSR_B1, PSR_B0, TMP_B2, TMP_B1, TMP_B0
271 if (!busReadBuf(baro
->busDev
, DPS310_REG_PSR_B2
, buf
, 6)) {
275 const int32_t Praw
= getTwosComplement((buf
[0] << 16) + (buf
[1] << 8) + buf
[2], 24);
276 const int32_t Traw
= getTwosComplement((buf
[3] << 16) + (buf
[4] << 8) + buf
[5], 24);
278 // 4. Calculate scaled measurement results.
279 const float Praw_sc
= Praw
/ kP
;
280 const float Traw_sc
= Traw
/ kT
;
282 // 5. Calculate compensated measurement results.
283 const float c00
= baroState
.calib
.c00
;
284 const float c01
= baroState
.calib
.c01
;
285 const float c10
= baroState
.calib
.c10
;
286 const float c11
= baroState
.calib
.c11
;
287 const float c20
= baroState
.calib
.c20
;
288 const float c21
= baroState
.calib
.c21
;
289 const float c30
= baroState
.calib
.c30
;
290 const float c31
= baroState
.calib
.c31
;
291 const float c40
= baroState
.calib
.c40
;
293 // See section 4.9.1, How to Calculate Compensated Pressure Values, of datasheet
294 // baroState.pressure = c00 + Praw_sc * (c10 + Praw_sc * (c20 + Praw_sc * c30)) + Traw_sc * c01 + Traw_sc * Praw_sc * (c11 + Praw_sc * c21);
295 if (chipId
[0] == SPL07_003_CHIP_ID
) {
296 baroState
.pressure
= c00
+ Praw_sc
* (c10
+ Praw_sc
* (c20
+ Praw_sc
* (c30
+ Praw_sc
* c40
))) + Traw_sc
* c01
+ Traw_sc
* Praw_sc
* (c11
+ Praw_sc
* (c21
+ Praw_sc
* c31
));
298 baroState
.pressure
= c00
+ Praw_sc
* (c10
+ Praw_sc
* (c20
+ Praw_sc
* c30
)) + Traw_sc
* c01
+ Traw_sc
* Praw_sc
* (c11
+ Praw_sc
* c21
);
302 const float c0
= baroState
.calib
.c0
;
303 const float c1
= baroState
.calib
.c1
;
305 // See section 4.9.2, How to Calculate Compensated Temperature Values, of datasheet
306 baroState
.temperature
= c0
* 0.5f
+ c1
* Traw_sc
;
311 static bool deviceCalculate(baroDev_t
*baro
, int32_t *pressure
, int32_t *temperature
)
316 *pressure
= baroState
.pressure
;
320 *temperature
= (baroState
.temperature
* 100); // to centidegrees
328 #define DETECTION_MAX_RETRY_COUNT 5
329 static bool deviceDetect(busDevice_t
* busDev
)
331 for (int retry
= 0; retry
< DETECTION_MAX_RETRY_COUNT
; retry
++) {
334 bool ack
= busReadBuf(busDev
, DPS310_REG_ID
, chipId
, 1);
336 if (ack
&& (chipId
[0] == DPS310_ID_REV_AND_PROD_ID
|| chipId
[0] == SPL07_003_CHIP_ID
)) {
344 bool baroDPS310Detect(baroDev_t
*baro
)
346 baro
->busDev
= busDeviceInit(BUSTYPE_ANY
, DEVHW_DPS310
, 0, OWNER_BARO
);
347 if (baro
->busDev
== NULL
) {
351 if (!deviceDetect(baro
->busDev
)) {
352 busDeviceDeInit(baro
->busDev
);
356 if (!deviceConfigure(baro
->busDev
)) {
357 busDeviceDeInit(baro
->busDev
);
361 const uint32_t baroDelay
= 1000000 / 32 / 2; // twice the sample rate to capture all new data
364 baro
->start_ut
= NULL
;
367 baro
->up_delay
= baroDelay
;
368 baro
->start_up
= NULL
;
369 baro
->get_up
= deviceReadMeasurement
;
371 baro
->calculate
= deviceCalculate
;