diff --git a/libraries/AP_Baro/AP_Baro_SPx06.cpp b/libraries/AP_Baro/AP_Baro_SPx06.cpp index a3dcfcc6a82b7a..f54b8fbbf81c9a 100644 --- a/libraries/AP_Baro/AP_Baro_SPx06.cpp +++ b/libraries/AP_Baro/AP_Baro_SPx06.cpp @@ -47,13 +47,17 @@ extern const AP_HAL::HAL &hal; #define SPL06_REG_CALIB_COEFFS_END 0x21 #define SPA06_REG_CALIB_COEFFS_END 0x24 +// PRESSURE_CFG_REG +#define SPx06_PRES_RATE_32HZ (0x05 << 4) // TEMPERATURE_CFG_REG #define SPx06_TEMP_USE_EXT_SENSOR (1<<7) +#define SPx06_TEMP_RATE_32HZ (0x05 << 4) // MODE_AND_STATUS_REG #define SPx06_MEAS_PRESSURE (1<<0) // measure pressure #define SPx06_MEAS_TEMPERATURE (1<<1) // measure temperature +#define SPx06_MEAS_CON_PRE_TEM 0x07 #define SPx06_MEAS_CFG_CONTINUOUS (1<<2) #define SPx06_MEAS_CFG_PRESSURE_RDY (1<<4) @@ -71,6 +75,8 @@ extern const AP_HAL::HAL &hal; #define SPx06_OVERSAMPLING_TO_REG_VALUE(n) (ffs(n)-1) +#define SPx06_BACKGROUND_SAMPLE_RATE 32 + AP_Baro_SPx06::AP_Baro_SPx06(AP_Baro &baro, AP_HAL::OwnPtr dev) : AP_Baro_Backend(baro) , _dev(std::move(dev)) @@ -165,8 +171,17 @@ bool AP_Baro_SPx06::_init() // setup temperature and pressure measurements _dev->setup_checked_registers(3, 20); +#ifdef SPx06_BACKGROUND_ENABLE + //set rate and oversampling + _dev->write_register(SPx06_REG_TEMPERATURE_CFG, SPx06_TEMP_RATE_32HZ | SPx06_OVERSAMPLING_TO_REG_VALUE(SPx06_TEMPERATURE_OVERSAMPLING), true); + _dev->write_register(SPx06_REG_PRESSURE_CFG, SPx06_PRES_RATE_32HZ | SPx06_OVERSAMPLING_TO_REG_VALUE(SPx06_PRESSURE_OVERSAMPLING), true); + + //enable background mode + _dev->write_register(SPx06_REG_MODE_AND_STATUS, SPx06_MEAS_CON_PRE_TEM, true); +#else _dev->write_register(SPx06_REG_TEMPERATURE_CFG, SPx06_TEMP_USE_EXT_SENSOR | SPx06_OVERSAMPLING_TO_REG_VALUE(SPx06_TEMPERATURE_OVERSAMPLING), true); _dev->write_register(SPx06_REG_PRESSURE_CFG, SPx06_OVERSAMPLING_TO_REG_VALUE(SPx06_PRESSURE_OVERSAMPLING), true); +#endif //SPx06_BACKGROUND_ENABLE uint8_t int_and_fifo_reg_value = 0; if (SPx06_TEMPERATURE_OVERSAMPLING > 8) { @@ -184,7 +199,11 @@ bool AP_Baro_SPx06::_init() // request 50Hz update _timer_counter = -1; +#ifdef SPx06_BACKGROUND_ENABLE + _dev->register_periodic_callback(1000000/SPx06_BACKGROUND_SAMPLE_RATE, FUNCTOR_BIND_MEMBER(&AP_Baro_SPx06::_timer, void)); +#else _dev->register_periodic_callback(20 * AP_USEC_PER_MSEC, FUNCTOR_BIND_MEMBER(&AP_Baro_SPx06::_timer, void)); +#endif //SPx06_BACKGROUND_ENABLE return true; } @@ -211,7 +230,15 @@ void AP_Baro_SPx06::_timer(void) { uint8_t buf[3]; - if ((_timer_counter == -1) || (_timer_counter == 49)) { +#ifdef SPx06_BACKGROUND_ENABLE + _dev->read_registers(SPx06_REG_TEMPERATURE_START, buf, sizeof(buf)); + _update_temperature((int32_t)((buf[0] & 0x80 ? 0xFF000000 : 0) | ((uint32_t)buf[0] << 16) | ((uint32_t)buf[1] << 8) | buf[2])); + + _dev->read_registers(SPx06_REG_PRESSURE_START, buf, sizeof(buf)); + _update_pressure((int32_t)((buf[0] & 0x80 ? 0xFF000000 : 0) | ((uint32_t)buf[0] << 16) | ((uint32_t)buf[1] << 8) | buf[2])); +#else + //command mode + if ((_timer_counter == -1) || (_timer_counter == 49)) { // First call and every second start a temperature measurement (50Hz call) _dev->write_register(SPx06_REG_MODE_AND_STATUS, SPx06_MEAS_TEMPERATURE, false); _timer_counter = 0; // Next cycle we are reading the temperature @@ -228,6 +255,7 @@ void AP_Baro_SPx06::_timer(void) _dev->write_register(SPx06_REG_MODE_AND_STATUS, SPx06_MEAS_PRESSURE, false); _timer_counter += 1; } +#endif //SPx06_BACKGROUND_ENABLE _dev->check_next_register(); }