Path: blob/master/libraries/AP_Baro/AP_Baro_BMP388.cpp
9592 views
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/14#include "AP_Baro_BMP388.h"1516#if AP_BARO_BMP388_ENABLED1718#include <AP_Math/AP_Math.h>1920extern const AP_HAL::HAL &hal;2122#define BMP388_MODE_SLEEP 023#define BMP388_MODE_FORCED 124#define BMP388_MODE_NORMAL 325#define BMP388_MODE BMP388_MODE_NORMAL2627#define BMP388_ID 0x5028#define BMP390_ID 0x602930#define BMP388_REG_ID 0x0031#define BMP388_REV_ID_ADDR 0x0132#define BMP388_REG_ERR 0x0233#define BMP388_REG_STATUS 0x0334#define BMP388_REG_PRESS 0x04 // 24 bit35#define BMP388_REG_TEMP 0x07 // 24 bit36#define BMP388_REG_TIME 0x0C // 24 bit37#define BMP388_REG_EVENT 0x1038#define BMP388_REG_INT_STS 0x1139#define BMP388_REG_FIFO_LEN 0x12 // 9 bit40#define BMP388_REG_FIFO_DATA 0x1441#define BMP388_REG_FIFO_WTMK 0x15 // 9 bit42#define BMP388_REG_FIFO_CNF1 0x1743#define BMP388_REG_FIFO_CNF2 0x1844#define BMP388_REG_INT_CTRL 0x1945#define BMP388_REG_PWR_CTRL 0x1B46#define BMP388_REG_OSR 0x1C47#define BMP388_REG_ODR 0x1D48#define BMP388_REG_CONFIG 0x1F49#define BMP388_REG_CMD 0x7E5051#define BMP388_REG_CAL_P 0x3652#define BMP388_REG_CAL_T 0x315354AP_Baro_BMP388::AP_Baro_BMP388(AP_Baro &baro, AP_HAL::Device &_dev)55: AP_Baro_Backend(baro)56, dev(&_dev)57{58}5960AP_Baro_Backend *AP_Baro_BMP388::probe(AP_Baro &baro, AP_HAL::Device &_dev)61{62AP_Baro_BMP388 *sensor = NEW_NOTHROW AP_Baro_BMP388(baro, _dev);63if (!sensor || !sensor->init()) {64delete sensor;65return nullptr;66}67return sensor;68}6970bool AP_Baro_BMP388::init()71{72if (!dev) {73return false;74}75WITH_SEMAPHORE(dev->get_semaphore());7677dev->set_speed(AP_HAL::Device::SPEED_HIGH);7879// setup to allow reads on SPI80if (dev->bus_type() == AP_HAL::Device::BUS_TYPE_SPI) {81dev->set_read_flag(0x80);82}8384// normal mode, temp and pressure85dev->write_register(BMP388_REG_PWR_CTRL, 0x33, true);8687uint8_t whoami;88if (!read_registers(BMP388_REG_ID, &whoami, 1)) {89return false;90}9192switch (whoami) {93case BMP388_ID:94dev->set_device_type(DEVTYPE_BARO_BMP388);95break;96case BMP390_ID:97dev->set_device_type(DEVTYPE_BARO_BMP390);98break;99default:100return false;101}102103// read the calibration data104read_registers(BMP388_REG_CAL_P, (uint8_t *)&calib_p, sizeof(calib_p));105read_registers(BMP388_REG_CAL_T, (uint8_t *)&calib_t, sizeof(calib_t));106107scale_calibration_data();108109dev->setup_checked_registers(4);110111// normal mode, temp and pressure112dev->write_register(BMP388_REG_PWR_CTRL, 0x33, true);113114instance = _frontend.register_sensor();115116set_bus_id(instance, dev->get_bus_id());117118// request 50Hz update119dev->register_periodic_callback(20 * AP_USEC_PER_MSEC, FUNCTOR_BIND_MEMBER(&AP_Baro_BMP388::timer, void));120121return true;122}123124125126// accumulate a new sensor reading127void AP_Baro_BMP388::timer(void)128{129uint8_t buf[7];130131if (!read_registers(BMP388_REG_STATUS, buf, sizeof(buf))) {132return;133}134const uint8_t status = buf[0];135if ((status & 0x20) != 0) {136// we have pressure data137update_pressure((buf[3] << 16) | (buf[2] << 8) | buf[1]);138}139if ((status & 0x40) != 0) {140// we have temperature data141update_temperature((buf[6] << 16) | (buf[5] << 8) | buf[4]);142}143144dev->check_next_register();145}146147// transfer data to the frontend148void AP_Baro_BMP388::update(void)149{150WITH_SEMAPHORE(_sem);151152if (pressure_count == 0) {153return;154}155156_copy_to_frontend(instance,157pressure_sum/pressure_count,158temperature);159160pressure_sum = 0;161pressure_count = 0;162}163164/*165convert calibration data from NVM values to values ready for166compensation calculations167*/168void AP_Baro_BMP388::scale_calibration_data(void)169{170// note that this assumes little-endian MCU171calib.par_t1 = calib_t.nvm_par_t1 * 256.0;172calib.par_t2 = calib_t.nvm_par_t2 / 1073741824.0f;173calib.par_t3 = calib_t.nvm_par_t3 / 281474976710656.0f;174175calib.par_p1 = (calib_p.nvm_par_p1 - 16384) / 1048576.0f;176calib.par_p2 = (calib_p.nvm_par_p2 - 16384) / 536870912.0f;177calib.par_p3 = calib_p.nvm_par_p3 / 4294967296.0f;178calib.par_p4 = calib_p.nvm_par_p4 / 137438953472.0;179calib.par_p5 = calib_p.nvm_par_p5 * 8.0f;180calib.par_p6 = calib_p.nvm_par_p6 / 64.0;181calib.par_p7 = calib_p.nvm_par_p7 / 256.0f;182calib.par_p8 = calib_p.nvm_par_p8 / 32768.0f;183calib.par_p9 = calib_p.nvm_par_p9 / 281474976710656.0f;184calib.par_p10 = calib_p.nvm_par_p10 / 281474976710656.0f;185calib.par_p11 = calib_p.nvm_par_p11 / 36893488147419103232.0f;186}187188/*189update temperature from raw sample190*/191void AP_Baro_BMP388::update_temperature(uint32_t data)192{193float partial1 = data - calib.par_t1;194float partial2 = partial1 * calib.par_t2;195196WITH_SEMAPHORE(_sem);197temperature = partial2 + sq(partial1) * calib.par_t3;198}199200/*201update pressure from raw pressure data202*/203void AP_Baro_BMP388::update_pressure(uint32_t data)204{205float partial1 = calib.par_p6 * temperature;206float partial2 = calib.par_p7 * powf(temperature, 2);207float partial3 = calib.par_p8 * powf(temperature, 3);208float partial_out1 = calib.par_p5 + partial1 + partial2 + partial3;209210partial1 = calib.par_p2 * temperature;211partial2 = calib.par_p3 * powf(temperature, 2);212partial3 = calib.par_p4 * powf(temperature, 3);213float partial_out2 = data * (calib.par_p1 + partial1 + partial2 + partial3);214215partial1 = powf(data, 2);216partial2 = calib.par_p9 + calib.par_p10 * temperature;217partial3 = partial1 * partial2;218float partial4 = partial3 + powf(data, 3) * calib.par_p11;219float press = partial_out1 + partial_out2 + partial4;220221WITH_SEMAPHORE(_sem);222223pressure_sum += press;224pressure_count++;225}226227/*228read registers, special SPI handling needed229*/230bool AP_Baro_BMP388::read_registers(uint8_t reg, uint8_t *data, uint8_t len)231{232// when on I2C we just read normally233if (dev->bus_type() != AP_HAL::Device::BUS_TYPE_SPI) {234return dev->read_registers(reg, data, len);235}236// for SPI we need to discard the first returned byte. See237// datasheet for explanation238uint8_t b[len+2];239b[0] = reg | 0x80;240memset(&b[1], 0, len+1);241if (!dev->transfer_fullduplex(b, len+2)) {242return false;243}244memcpy(data, &b[2], len);245return true;246}247248#endif // AP_BARO_BMP388_ENABLED249250251