diff --git a/src/main/build/debug.c b/src/main/build/debug.c index a54abe20e3..410dcf6da3 100644 --- a/src/main/build/debug.c +++ b/src/main/build/debug.c @@ -90,4 +90,5 @@ const char * const debugModeNames[DEBUG_COUNT] = { "CRSF_LINK_STATISTICS_UPLINK", "CRSF_LINK_STATISTICS_PWR", "CRSF_LINK_STATISTICS_DOWN", + "BARO", }; diff --git a/src/main/build/debug.h b/src/main/build/debug.h index c916e30721..3ac537eea3 100644 --- a/src/main/build/debug.h +++ b/src/main/build/debug.h @@ -106,6 +106,7 @@ typedef enum { DEBUG_CRSF_LINK_STATISTICS_UPLINK, DEBUG_CRSF_LINK_STATISTICS_PWR, DEBUG_CRSF_LINK_STATISTICS_DOWN, + DEBUG_BARO, DEBUG_COUNT } debugType_e; diff --git a/src/main/drivers/barometer/barometer_bmp085.c b/src/main/drivers/barometer/barometer_bmp085.c index d5758ce3c8..ac851c910a 100644 --- a/src/main/drivers/barometer/barometer_bmp085.c +++ b/src/main/drivers/barometer/barometer_bmp085.c @@ -39,7 +39,7 @@ #ifdef USE_BARO -#if defined(BARO_EOC_GPIO) +#if defined(BARO_EOC_PIN) static IO_t eocIO; @@ -53,7 +53,6 @@ void bmp085_extiHandler(extiCallbackRec_t* cb) isConversionComplete = true; } -bool bmp085TestEOCConnected(const bmp085Config_t *config); # endif typedef struct { @@ -164,7 +163,7 @@ bool bmp085Detect(const bmp085Config_t *config, baroDev_t *baro) bool ack; bool defaultAddressApplied = false; -#if defined(BARO_EOC_GPIO) +#if defined(BARO_EOC_PIN) IO_t eocIO = IO_NONE; #endif @@ -174,14 +173,13 @@ bool bmp085Detect(const bmp085Config_t *config, baroDev_t *baro) bmp085InitXclrIO(config); BMP085_ON; // enable baro -#if defined(BARO_EOC_GPIO) && defined(USE_EXTI) +#if defined(BARO_EOC_PIN) && defined(USE_EXTI) if (config && config->eocIO) { eocIO = IOGetByTag(config->eocIO); // EXTI interrupt for barometer EOC IOInit(eocIO, OWNER_BARO_EXTI, 0); - IOConfigGPIO(eocIO, Mode_IN_FLOATING); - EXTIHandlerInit(&bmp085_extiCallbackRec, bmp085_extiHandler); - EXTIConfig(eocIO, &bmp085_extiCallbackRec, NVIC_PRIO_BARO_EXTI, IOCFG_IN_FLOATING, EXTI_TRIGGER_RISING); + EXTIHandlerInit(&baro->exti, bmp085_extiHandler); + EXTIConfig(eocIO, &baro->exti, NVIC_PRIO_BARO_EXTI, IOCFG_IN_FLOATING, EXTI_TRIGGER_RISING); EXTIEnable(eocIO, true); } #else @@ -215,15 +213,15 @@ bool bmp085Detect(const bmp085Config_t *config, baroDev_t *baro) baro->start_up = bmp085_start_up; baro->get_up = bmp085_get_up; baro->calculate = bmp085_calculate; -#if defined(BARO_EOC_GPIO) - isEOCConnected = bmp085TestEOCConnected(config); +#if defined(BARO_EOC_PIN) + isEOCConnected = bmp085TestEOCConnected(baro, config); #endif bmp085InitDone = true; return true; } } -#if defined(BARO_EOC_GPIO) +#if defined(BARO_EOC_PIN) if (eocIO) EXTIRelease(eocIO); #endif @@ -292,7 +290,7 @@ static int32_t bmp085_get_pressure(uint32_t up) static void bmp085_start_ut(baroDev_t *baro) { -#if defined(BARO_EOC_GPIO) +#if defined(BARO_EOC_PIN) isConversionComplete = false; #endif busWriteRegister(&baro->busdev, BMP085_CTRL_MEAS_REG, BMP085_T_MEASURE); @@ -302,7 +300,7 @@ static void bmp085_get_ut(baroDev_t *baro) { uint8_t data[2]; -#if defined(BARO_EOC_GPIO) +#if defined(BARO_EOC_PIN) // return old baro value if conversion time exceeds datasheet max when EOC is connected if ((isEOCConnected) && (!isConversionComplete)) { return; @@ -319,7 +317,7 @@ static void bmp085_start_up(baroDev_t *baro) ctrl_reg_data = BMP085_P_MEASURE + (bmp085.oversampling_setting << 6); -#if defined(BARO_EOC_GPIO) +#if defined(BARO_EOC_PIN) isConversionComplete = false; #endif @@ -334,7 +332,7 @@ static void bmp085_get_up(baroDev_t *baro) { uint8_t data[3]; -#if defined(BARO_EOC_GPIO) +#if defined(BARO_EOC_PIN) // return old baro value if conversion time exceeds datasheet max when EOC is connected if ((isEOCConnected) && (!isConversionComplete)) { return; @@ -381,13 +379,13 @@ static void bmp085_get_cal_param(busDevice_t *busdev) bmp085.cal_param.md = (data[20] << 8) | data[21]; } -#if defined(BARO_EOC_GPIO) -bool bmp085TestEOCConnected(const bmp085Config_t *config) +#if defined(BARO_EOC_PIN) +bool bmp085TestEOCConnected(baroDev_t *baro, const bmp085Config_t *config) { UNUSED(config); if (!bmp085InitDone && eocIO) { - bmp085_start_ut(); + bmp085_start_ut(baro); delayMicroseconds(UT_DELAY * 2); // wait twice as long as normal, just to be sure // conversion should have finished now so check if EOC is high diff --git a/src/main/drivers/barometer/barometer_bmp085.h b/src/main/drivers/barometer/barometer_bmp085.h index 9f5a1e5530..7b3352dd66 100644 --- a/src/main/drivers/barometer/barometer_bmp085.h +++ b/src/main/drivers/barometer/barometer_bmp085.h @@ -32,8 +32,8 @@ typedef struct bmp085Config_s { bool bmp085Detect(const bmp085Config_t *config, baroDev_t *baro); void bmp085Disable(const bmp085Config_t *config); -#if defined(BARO_EOC_GPIO) -bool bmp085TestEOCConnected(const bmp085Config_t *config); +#if defined(BARO_EOC_PIN) +bool bmp085TestEOCConnected(baroDev_t *baro, const bmp085Config_t *config); #endif #ifdef UNIT_TEST diff --git a/src/main/drivers/barometer/barometer_bmp280.c b/src/main/drivers/barometer/barometer_bmp280.c index 4d660b5c66..62ebdd5905 100644 --- a/src/main/drivers/barometer/barometer_bmp280.c +++ b/src/main/drivers/barometer/barometer_bmp280.c @@ -52,8 +52,9 @@ typedef struct bmp280_calib_param_s { int16_t dig_P7; /* calibration P7 data */ int16_t dig_P8; /* calibration P8 data */ int16_t dig_P9; /* calibration P9 data */ - int32_t t_fine; /* calibration t_fine data */ -} bmp280_calib_param_t; +} __attribute__((packed)) bmp280_calib_param_t; // packed as we read directly from the device into this structure. + +STATIC_UNIT_TESTED int32_t t_fine; /* calibration t_fine data */ static uint8_t bmp280_chip_id = 0; STATIC_UNIT_TESTED bmp280_calib_param_t bmp280_cal; @@ -70,6 +71,8 @@ STATIC_UNIT_TESTED void bmp280_calculate(int32_t *pressure, int32_t *temperature void bmp280BusInit(busDevice_t *busdev) { + STATIC_ASSERT(sizeof(bmp280_calib_param_t) == BMP280_PRESSURE_TEMPERATURE_CALIB_DATA_LENGTH, bmp280_calibration_structure_incorrectly_packed); + #ifdef USE_BARO_SPI_BMP280 if (busdev->bustype == BUSTYPE_SPI) { IOHi(busdev->busdev_u.spi.csnPin); // Disable @@ -125,7 +128,7 @@ bool bmp280Detect(baroDev_t *baro) } // read calibration - busReadRegisterBuffer(busdev, BMP280_TEMPERATURE_CALIB_DIG_T1_LSB_REG, (uint8_t *)&bmp280_cal, 24); + busReadRegisterBuffer(busdev, BMP280_TEMPERATURE_CALIB_DIG_T1_LSB_REG, (uint8_t *)&bmp280_cal, sizeof(bmp280_calib_param_t)); // set oversampling + power mode (forced), and start sampling busWriteRegister(busdev, BMP280_CTRL_MEAS_REG, BMP280_MODE); @@ -180,8 +183,8 @@ static int32_t bmp280_compensate_T(int32_t adc_T) var1 = ((((adc_T >> 3) - ((int32_t)bmp280_cal.dig_T1 << 1))) * ((int32_t)bmp280_cal.dig_T2)) >> 11; var2 = (((((adc_T >> 4) - ((int32_t)bmp280_cal.dig_T1)) * ((adc_T >> 4) - ((int32_t)bmp280_cal.dig_T1))) >> 12) * ((int32_t)bmp280_cal.dig_T3)) >> 14; - bmp280_cal.t_fine = var1 + var2; - T = (bmp280_cal.t_fine * 5 + 128) >> 8; + t_fine = var1 + var2; + T = (t_fine * 5 + 128) >> 8; return T; } @@ -191,7 +194,7 @@ static int32_t bmp280_compensate_T(int32_t adc_T) static uint32_t bmp280_compensate_P(int32_t adc_P) { int64_t var1, var2, p; - var1 = ((int64_t)bmp280_cal.t_fine) - 128000; + var1 = ((int64_t)t_fine) - 128000; var2 = var1 * var1 * (int64_t)bmp280_cal.dig_P6; var2 = var2 + ((var1*(int64_t)bmp280_cal.dig_P5) << 17); var2 = var2 + (((int64_t)bmp280_cal.dig_P4) << 35); diff --git a/src/main/sensors/barometer.c b/src/main/sensors/barometer.c index 2eccc2d4b5..3b1fc60f0a 100644 --- a/src/main/sensors/barometer.c +++ b/src/main/sensors/barometer.c @@ -26,6 +26,8 @@ #ifdef USE_BARO +#include "build/debug.h" + #include "common/maths.h" #include "pg/pg.h" @@ -102,7 +104,7 @@ void pgResetFn_barometerConfig(barometerConfig_t *barometerConfig) barometerConfig->baro_spi_csn = IO_TAG(BARO_CS_PIN); barometerConfig->baro_i2c_device = I2C_DEV_TO_CFG(I2CINVALID); barometerConfig->baro_i2c_address = 0; -#elif defined(DEFAULT_BARO_MS5611) || defined(DEFAULT_BARO_BMP280) || defined(DEFAULT_BARO_BMP085)||defined(DEFAULT_BARO_QMP6988) +#elif defined(DEFAULT_BARO_MS5611) || defined(DEFAULT_BARO_BMP280) || defined(DEFAULT_BARO_BMP085) || defined(DEFAULT_BARO_QMP6988) // All I2C devices shares a default config with address = 0 (per device default) barometerConfig->baro_bustype = BUSTYPE_I2C; barometerConfig->baro_i2c_device = I2C_DEV_TO_CFG(BARO_I2C_INSTANCE); @@ -183,7 +185,7 @@ bool baroDetect(baroDev_t *dev, baroSensor_e baroHardwareToUse) { const bmp085Config_t *bmp085Config = NULL; -#if defined(BARO_XCLR_GPIO) && defined(BARO_EOC_GPIO) +#if defined(BARO_XCLR_PIN) && defined(BARO_EOC_PIN) static const bmp085Config_t defaultBMP085Config = { .xclrIO = IO_TAG(BARO_XCLR_PIN), .eocIO = IO_TAG(BARO_EOC_PIN), @@ -324,6 +326,10 @@ uint32_t baroUpdate(void) { static barometerState_e state = BAROMETER_NEEDS_SAMPLES; + if (debugMode == DEBUG_BARO) { + debug[0] = state; + } + switch (state) { default: case BAROMETER_NEEDS_SAMPLES: @@ -340,6 +346,13 @@ uint32_t baroUpdate(void) baro.baroPressure = baroPressure; baro.baroTemperature = baroTemperature; baroPressureSum = recalculateBarometerTotal(barometerConfig()->baro_sample_count, baroPressureSum, baroPressure); + + if (debugMode == DEBUG_BARO) { + debug[1] = baroTemperature; + debug[2] = baroPressure; + debug[3] = baroPressureSum; + } + state = BAROMETER_NEEDS_SAMPLES; return baro.dev.ut_delay; break; diff --git a/src/test/unit/baro_bmp280_unittest.cc b/src/test/unit/baro_bmp280_unittest.cc index cd9b2b4256..52d7ddd0cc 100644 --- a/src/test/unit/baro_bmp280_unittest.cc +++ b/src/test/unit/baro_bmp280_unittest.cc @@ -27,6 +27,7 @@ void bmp280_calculate(int32_t *pressure, int32_t *temperature); extern uint32_t bmp280_up; extern uint32_t bmp280_ut; +extern int32_t t_fine; /* calibration t_fine data */ typedef struct bmp280_calib_param_s { uint16_t dig_T1; /* calibration T1 data */ @@ -41,8 +42,7 @@ typedef struct bmp280_calib_param_s { int16_t dig_P7; /* calibration P7 data */ int16_t dig_P8; /* calibration P8 data */ int16_t dig_P9; /* calibration P9 data */ - int32_t t_fine; /* calibration t_fine data */ -} bmp280_calib_param_t; +} __attribute__((packed)) bmp280_calib_param_t; // packed as we read directly from the device into this structure. bmp280_calib_param_t bmp280_cal; } @@ -58,6 +58,7 @@ TEST(baroBmp280Test, TestBmp280Calculate) int32_t pressure, temperature; bmp280_up = 415148; // Digital pressure value bmp280_ut = 519888; // Digital temperature value + t_fine = 0; // and bmp280_cal.dig_T1 = 27504; @@ -87,6 +88,7 @@ TEST(baroBmp280Test, TestBmp280CalculateHighP) int32_t pressure, temperature; bmp280_up = 215148; // Digital pressure value bmp280_ut = 519888; // Digital temperature value + t_fine = 0; // and bmp280_cal.dig_T1 = 27504; @@ -116,6 +118,7 @@ TEST(baroBmp280Test, TestBmp280CalculateZeroP) int32_t pressure, temperature; bmp280_up = 415148; // Digital pressure value bmp280_ut = 519888; // Digital temperature value + t_fine = 0; // and bmp280_cal.dig_T1 = 27504;