2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
29 #include "build/debug.h"
31 #include "common/maths.h"
32 #include "common/filter.h"
35 #include "pg/pg_ids.h"
37 #include "drivers/barometer/barometer.h"
38 #include "drivers/barometer/barometer_bmp085.h"
39 #include "drivers/barometer/barometer_bmp280.h"
40 #include "drivers/barometer/barometer_bmp388.h"
41 #include "drivers/barometer/barometer_dps310.h"
42 #include "drivers/barometer/barometer_qmp6988.h"
43 #include "drivers/barometer/barometer_virtual.h"
44 #include "drivers/barometer/barometer_ms5611.h"
45 #include "drivers/barometer/barometer_lps.h"
46 #include "drivers/barometer/barometer_2smpb_02b.h"
47 #include "drivers/barometer/barometer_lps22df.h"
48 #include "drivers/bus.h"
49 #include "drivers/bus_i2c_busdev.h"
50 #include "drivers/bus_spi.h"
51 #include "drivers/io.h"
52 #include "drivers/time.h"
54 #include "fc/runtime_config.h"
56 #include "sensors/sensors.h"
58 #include "scheduler/scheduler.h"
60 #include "barometer.h"
62 baro_t baro
; // barometer access functions
64 PG_REGISTER_WITH_RESET_FN(barometerConfig_t
, barometerConfig
, PG_BAROMETER_CONFIG
, 3);
66 #ifndef DEFAULT_BARO_DEVICE
67 #define DEFAULT_BARO_DEVICE BARO_DEFAULT
70 void pgResetFn_barometerConfig(barometerConfig_t
*barometerConfig
)
72 barometerConfig
->baro_hardware
= DEFAULT_BARO_DEVICE
;
74 // For backward compatibility; ceate a valid default value for bus parameters
76 // 1. If DEFAULT_BARO_xxx is defined, use it.
77 // 2. Determine default based on USE_BARO_xxx
78 // a. Precedence is in the order of popularity; BMP388, BMP280, MS5611 then BMP085, then
79 // b. If SPI variant is specified, it is likely onboard, so take it.
81 #if !(defined(DEFAULT_BARO_SPI_BMP388) || defined(DEFAULT_BARO_BMP388) || defined(DEFAULT_BARO_SPI_BMP280) || \
82 defined(DEFAULT_BARO_BMP280) || defined(DEFAULT_BARO_SPI_MS5611) || defined(DEFAULT_BARO_MS5611) || \
83 defined(DEFAULT_BARO_BMP085) || defined(DEFAULT_BARO_SPI_LPS) || defined(DEFAULT_BARO_SPI_QMP6988) || \
84 defined(DEFAULT_BARO_QMP6988)) || defined(DEFAULT_BARO_DPS310) || defined(DEFAULT_BARO_SPI_DPS310) || \
85 defined(DEFAULT_BARO_LPS22DF) || defined(DEFAULT_BARO_SPI_LPS22DF)
87 #if defined(USE_BARO_DPS310) || defined(USE_BARO_SPI_DPS310)
88 #if defined(USE_BARO_SPI_DPS310)
89 #define DEFAULT_BARO_SPI_DPS310
91 #define DEFAULT_BARO_DPS310
93 #elif defined(USE_BARO_BMP388) || defined(USE_BARO_SPI_BMP388)
94 #if defined(USE_BARO_SPI_BMP388)
95 #define DEFAULT_BARO_SPI_BMP388
97 #define DEFAULT_BARO_BMP388
99 #elif defined(USE_BARO_BMP280) || defined(USE_BARO_SPI_BMP280)
100 #if defined(USE_BARO_SPI_BMP280)
101 #define DEFAULT_BARO_SPI_BMP280
103 #define DEFAULT_BARO_BMP280
105 #elif defined(USE_BARO_MS5611) || defined(USE_BARO_SPI_MS5611)
106 #if defined(USE_BARO_SPI_MS5611)
107 #define DEFAULT_BARO_SPI_MS5611
109 #define DEFAULT_BARO_MS5611
111 #elif defined(USE_BARO_QMP6988) || defined(USE_BARO_SPI_QMP6988)
112 #if defined(USE_BARO_SPI_QMP6988)
113 #define DEFAULT_BARO_SPI_QMP6988
115 #define DEFAULT_BARO_QMP6988
117 #elif defined(USE_BARO_SPI_LPS)
118 #define DEFAULT_BARO_SPI_LPS
119 #elif defined(DEFAULT_BARO_BMP085)
120 #define DEFAULT_BARO_BMP085
122 #elif defined(USE_BARO_2SMBP_02B) || defined(USE_BARO_SPI_2SMBP_02B)
123 #if defined(USE_BARO_SPI_2SMBP_02B)
124 #define DEFAULT_BARO_SPI_2SMBP_02B
126 #define DEFAULT_BARO_2SMBP_02B
128 #elif defined(USE_BARO_LPS22DF) || defined(USE_BARO_SPI_LPS22DF)
129 #if defined(USE_BARO_LPS22DF)
130 #define DEFAULT_BARO_SPI_LPS22DF
132 #define DEFAULT_BARO_LPS22DF
136 #ifndef DEFAULT_BARO_I2C_ADDRESS
137 #define DEFAULT_BARO_I2C_ADDRESS 0
140 #if defined(DEFAULT_BARO_SPI_BMP388) || defined(DEFAULT_BARO_SPI_BMP280) || defined(DEFAULT_BARO_SPI_MS5611) || defined(DEFAULT_BARO_SPI_QMP6988) || defined(DEFAULT_BARO_SPI_LPS) || defined(DEFAULT_BARO_SPI_DPS310) || defined(DEFAULT_BARO_SPI_2SMBP_02B)
141 barometerConfig
->baro_busType
= BUS_TYPE_SPI
;
142 barometerConfig
->baro_spi_device
= SPI_DEV_TO_CFG(spiDeviceByInstance(BARO_SPI_INSTANCE
));
143 barometerConfig
->baro_spi_csn
= IO_TAG(BARO_CS_PIN
);
144 barometerConfig
->baro_i2c_device
= I2C_DEV_TO_CFG(I2CINVALID
);
145 barometerConfig
->baro_i2c_address
= 0;
146 #elif defined(DEFAULT_BARO_MS5611) || defined(DEFAULT_BARO_BMP388) || defined(DEFAULT_BARO_BMP280) || defined(DEFAULT_BARO_BMP085) ||defined(DEFAULT_BARO_QMP6988) || defined(DEFAULT_BARO_DPS310) || defined(DEFAULT_BARO_2SMBP_02B)
147 // All I2C devices shares a default config with address = 0 (per device default)
148 barometerConfig
->baro_busType
= BUS_TYPE_I2C
;
149 barometerConfig
->baro_i2c_device
= I2C_DEV_TO_CFG(BARO_I2C_INSTANCE
);
150 barometerConfig
->baro_i2c_address
= DEFAULT_BARO_I2C_ADDRESS
;
151 barometerConfig
->baro_spi_device
= SPI_DEV_TO_CFG(SPIINVALID
);
152 barometerConfig
->baro_spi_csn
= IO_TAG_NONE
;
154 barometerConfig
->baro_hardware
= BARO_NONE
;
155 barometerConfig
->baro_busType
= BUS_TYPE_NONE
;
156 barometerConfig
->baro_i2c_device
= I2C_DEV_TO_CFG(I2CINVALID
);
157 barometerConfig
->baro_i2c_address
= 0;
158 barometerConfig
->baro_spi_device
= SPI_DEV_TO_CFG(SPIINVALID
);
159 barometerConfig
->baro_spi_csn
= IO_TAG_NONE
;
163 #define BARO_EOC_PIN NONE
166 barometerConfig
->baro_eoc_tag
= IO_TAG(BARO_EOC_PIN
);
167 barometerConfig
->baro_xclr_tag
= IO_TAG(BARO_XCLR_PIN
);
170 #define NUM_CALIBRATION_CYCLES 100 // 10 seconds init_delay + 100 * 25 ms = 12.5 seconds before valid baro altitude
171 #define NUM_GROUND_LEVEL_CYCLES 10 // calibrate baro to new ground level (10 * 25 ms = ~250 ms non blocking)
173 static uint16_t calibrationCycles
= 0; // baro calibration = get new ground pressure value
174 static uint16_t calibrationCycleCount
= 0;
175 static float baroGroundAltitude
= 0.0f
;
176 static bool baroCalibrated
= false;
177 static bool baroReady
= false;
179 void baroPreInit(void)
182 if (barometerConfig()->baro_busType
== BUS_TYPE_SPI
) {
183 spiPreinitRegister(barometerConfig()->baro_spi_csn
, IOCFG_IPU
, 1);
188 static bool baroDetect(baroDev_t
*baroDev
, baroSensor_e baroHardwareToUse
)
190 extDevice_t
*dev
= &baroDev
->dev
;
192 // Detect what pressure sensors are available. baro->update() is set to sensor-specific update function
194 baroSensor_e baroHardware
= baroHardwareToUse
;
196 #if !defined(USE_BARO_BMP085) && !defined(USE_BARO_MS5611) && !defined(USE_BARO_SPI_MS5611) && !defined(USE_BARO_BMP388) && !defined(USE_BARO_BMP280) && !defined(USE_BARO_SPI_BMP280)&& !defined(USE_BARO_QMP6988) && !defined(USE_BARO_SPI_QMP6988) && !defined(USE_BARO_DPS310) && !defined(USE_BARO_SPI_DPS310) && !defined(DEFAULT_BARO_SPI_2SMBP_02B) && !defined(DEFAULT_BARO_2SMBP_02B)
200 #ifndef USE_VIRTUAL_BARO
201 switch (barometerConfig()->baro_busType
) {
204 i2cBusSetInstance(dev
, barometerConfig()->baro_i2c_device
);
205 dev
->busType_u
.i2c
.address
= barometerConfig()->baro_i2c_address
;
212 if (!spiSetBusInstance(dev
, barometerConfig()->baro_spi_device
)) {
216 dev
->busType_u
.spi
.csnPin
= IOGetByTag(barometerConfig()->baro_spi_csn
);
223 #endif // USE_VIRTUAL_BARO
225 switch (baroHardware
) {
230 #ifdef USE_BARO_BMP085
232 static bmp085Config_t defaultBMP085Config
;
233 defaultBMP085Config
.xclrTag
= barometerConfig()->baro_xclr_tag
;
234 defaultBMP085Config
.eocTag
= barometerConfig()->baro_eoc_tag
;
236 static const bmp085Config_t
*bmp085Config
= &defaultBMP085Config
;
238 if (bmp085Detect(bmp085Config
, baroDev
)) {
239 baroHardware
= BARO_BMP085
;
247 #if defined(USE_BARO_MS5611) || defined(USE_BARO_SPI_MS5611)
248 if (ms5611Detect(baroDev
)) {
249 baroHardware
= BARO_MS5611
;
256 #if defined(USE_BARO_SPI_LPS)
257 if (lpsDetect(baroDev
)) {
258 baroHardware
= BARO_LPS
;
265 #if defined(USE_BARO_DPS310) || defined(USE_BARO_SPI_DPS310)
267 if (baroDPS310Detect(baroDev
)) {
268 baroHardware
= BARO_DPS310
;
276 #if defined(USE_BARO_BMP388) || defined(USE_BARO_SPI_BMP388)
278 static bmp388Config_t defaultBMP388Config
;
280 defaultBMP388Config
.eocTag
= barometerConfig()->baro_eoc_tag
;
282 static const bmp388Config_t
*bmp388Config
= &defaultBMP388Config
;
284 if (bmp388Detect(bmp388Config
, baroDev
)) {
285 baroHardware
= BARO_BMP388
;
293 #if defined(USE_BARO_BMP280) || defined(USE_BARO_SPI_BMP280)
294 if (bmp280Detect(baroDev
)) {
295 baroHardware
= BARO_BMP280
;
302 #if defined(USE_BARO_QMP6988) || defined(USE_BARO_SPI_QMP6988)
303 if (qmp6988Detect(baroDev
)) {
304 baroHardware
= BARO_QMP6988
;
311 #if defined(USE_BARO_2SMBP_02B) || defined(USE_BARO_SPI_2SMBP_02B)
312 if (baro2SMPB02BDetect(baroDev
)) {
313 baroHardware
= BARO_2SMPB_02B
;
320 #if defined(USE_BARO_LPS22DF) || defined(USE_BARO_SPI_LPS22DF)
321 if (lps22dfDetect(baroDev
)) {
322 baroHardware
= BARO_LPS22DF
;
329 #ifdef USE_VIRTUAL_BARO
330 if (virtualBaroDetect(baroDev
)) {
331 baroHardware
= BARO_VIRTUAL
;
338 baroHardware
= BARO_NONE
;
342 if (baroHardware
== BARO_NONE
) {
346 detectedSensors
[SENSOR_INDEX_BARO
] = baroHardware
;
347 sensorsSet(SENSOR_BARO
);
353 #ifndef USE_VIRTUAL_BARO
354 baroReady
= baroDetect(&baro
.dev
, barometerConfig()->baro_hardware
);
356 baroReady
= baroDetect(&baro
.dev
, BARO_VIRTUAL
);
360 bool baroIsCalibrated(void)
362 return baroCalibrated
;
365 void baroStartCalibration(void)
367 baroGroundAltitude
= 0;
368 baroCalibrated
= false;
369 calibrationCycles
= NUM_CALIBRATION_CYCLES
;
370 calibrationCycleCount
= 0;
373 void baroSetGroundLevel(void)
375 baroGroundAltitude
= 0;
376 baroCalibrated
= false;
377 calibrationCycles
= NUM_GROUND_LEVEL_CYCLES
;
378 calibrationCycleCount
= 0;
382 BARO_STATE_TEMPERATURE_READ
= 0,
383 BARO_STATE_TEMPERATURE_SAMPLE
,
384 BARO_STATE_PRESSURE_START
,
385 BARO_STATE_PRESSURE_READ
,
386 BARO_STATE_PRESSURE_SAMPLE
,
387 BARO_STATE_TEMPERATURE_START
,
392 bool isBaroReady(void)
397 static float pressureToAltitude(const float pressure
)
399 return (1.0f
- powf(pressure
/ 101325.0f
, 0.190295f
)) * 4433000.0f
;
402 static void performBaroCalibrationCycle(const float altitude
);
404 uint32_t baroUpdate(timeUs_t currentTimeUs
)
406 static timeUs_t baroStateDurationUs
[BARO_STATE_COUNT
];
407 static barometerState_e state
= BARO_STATE_PRESSURE_START
;
408 barometerState_e oldState
= state
;
409 timeUs_t executeTimeUs
;
410 timeUs_t sleepTime
= 1000; // Wait 1ms between states
412 DEBUG_SET(DEBUG_BARO
, 0, state
);
414 if (busBusy(&baro
.dev
.dev
, NULL
)) {
415 // If the bus is busy, simply return to have another go later
416 schedulerIgnoreTaskStateTime();
422 case BARO_STATE_TEMPERATURE_START
:
423 baro
.dev
.start_ut(&baro
.dev
);
424 state
= BARO_STATE_TEMPERATURE_READ
;
425 sleepTime
= baro
.dev
.ut_delay
;
428 case BARO_STATE_TEMPERATURE_READ
:
429 if (baro
.dev
.read_ut(&baro
.dev
)) {
430 state
= BARO_STATE_TEMPERATURE_SAMPLE
;
432 // No action was taken as the read has not completed
433 schedulerIgnoreTaskExecTime();
437 case BARO_STATE_TEMPERATURE_SAMPLE
:
438 if (baro
.dev
.get_ut(&baro
.dev
)) {
439 state
= BARO_STATE_PRESSURE_START
;
441 // No action was taken as the read has not completed
442 schedulerIgnoreTaskExecTime();
446 case BARO_STATE_PRESSURE_START
:
447 baro
.dev
.start_up(&baro
.dev
);
448 state
= BARO_STATE_PRESSURE_READ
;
449 sleepTime
= baro
.dev
.up_delay
;
452 case BARO_STATE_PRESSURE_READ
:
453 if (baro
.dev
.read_up(&baro
.dev
)) {
454 state
= BARO_STATE_PRESSURE_SAMPLE
;
456 // No action was taken as the read has not completed
457 schedulerIgnoreTaskExecTime();
461 case BARO_STATE_PRESSURE_SAMPLE
:
462 if (!baro
.dev
.get_up(&baro
.dev
)) {
463 // No action was taken as the read has not completed
464 schedulerIgnoreTaskExecTime();
469 baro
.dev
.calculate(&baro
.pressure
, &baro
.temperature
);
471 // If baro.pressure is invalid then skip altitude counting / call of calibration cycle
472 if (baro
.pressure
> 0) {
473 const float altitude
= pressureToAltitude(baro
.pressure
);
474 if (baroIsCalibrated()) {
475 // zero baro altitude
476 baro
.altitude
= altitude
- baroGroundAltitude
;
478 // establish stable baroGroundAltitude value to zero baro altitude with
479 performBaroCalibrationCycle(altitude
);
480 baro
.altitude
= 0.0f
;
483 // return 0 during calibration, reuse last value otherwise
484 if (!baroIsCalibrated()) {
485 baro
.altitude
= 0.0f
;
489 if (debugMode
== DEBUG_BARO
) {
490 DEBUG_SET(DEBUG_BARO
, 1, lrintf(baro
.pressure
/ 100.0f
)); // hPa
491 DEBUG_SET(DEBUG_BARO
, 2, baro
.temperature
); // c°C
492 DEBUG_SET(DEBUG_BARO
, 3, lrintf(baro
.altitude
)); // cm
495 if (baro
.dev
.combined_read
) {
496 state
= BARO_STATE_PRESSURE_START
;
498 state
= BARO_STATE_TEMPERATURE_START
;
503 // Where we are using a state machine call schedulerIgnoreTaskExecRate() for all states bar one
504 if (state
!= BARO_STATE_PRESSURE_START
) {
505 schedulerIgnoreTaskExecRate();
508 executeTimeUs
= micros() - currentTimeUs
;
510 if (executeTimeUs
> baroStateDurationUs
[oldState
]) {
511 baroStateDurationUs
[oldState
] = executeTimeUs
;
514 schedulerSetNextStateTime(baroStateDurationUs
[state
]);
519 float getBaroAltitude(void)
521 return baro
.altitude
;
524 static void performBaroCalibrationCycle(const float altitude
)
526 baroGroundAltitude
+= altitude
;
527 calibrationCycleCount
++;
529 if (calibrationCycleCount
>= calibrationCycles
) {
530 baroGroundAltitude
/= calibrationCycleCount
; // simple average
531 baroCalibrated
= true;
532 calibrationCycleCount
= 0;