- int8_t bmp280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint8_t len, const struct bmp280_dev *dev)
- {
- int8_t rslt;
- rslt = null_ptr_check(dev);
- if ((rslt == BMP280_OK) && (reg_data != NULL))
- {
- /* Mask the register address' MSB if interface selected is SPI */
- if (dev->intf == BMP280_SPI_INTF)
- {
- reg_addr = reg_addr | 0x80;
- }
- rslt = dev->read(dev->dev_id, reg_addr, reg_data, len);
- /* Check for communication error and mask with an internal error code */
- if (rslt != BMP280_OK)
- {
- rslt = BMP280_E_COMM_FAIL;
- }
- }
- else
- {
- rslt = BMP280_E_NULL_PTR;
- }
- return rslt;
- }
设置寄存器:
- int8_t bmp280_set_regs(uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len, const struct bmp280_dev *dev)
- {
- int8_t rslt;
- uint8_t temp_buff[8]; /* Typically not to write more than 4 registers */
- uint16_t temp_len;
- uint8_t reg_addr_cnt;
- if (len > 4)
- {
- len = 4;
- }
- rslt = null_ptr_check(dev);
- if ((rslt == BMP280_OK) && (reg_addr != NULL) && (reg_data != NULL))
- {
- if (len != 0)
- {
- temp_buff[0] = reg_data[0];
- /* Mask the register address' MSB if interface selected is SPI */
- if (dev->intf == BMP280_SPI_INTF)
- {
- /* Converting all the reg address into proper SPI write address
- * i.e making MSB(R/`W) bit 0
- */
- for (reg_addr_cnt = 0; reg_addr_cnt < len; reg_addr_cnt++)
- {
- reg_addr[reg_addr_cnt] = reg_addr[reg_addr_cnt] & 0x7F;
- }
- }
- /* Burst write mode */
- if (len > 1)
- {
- /* Interleave register address w.r.t data for burst write*/
- interleave_data(reg_addr, temp_buff, reg_data, len);
- temp_len = ((len * 2) - 1);
- }
- else
- {
- temp_len = len;
- }
- rslt = dev->write(dev->dev_id, reg_addr[0], temp_buff, temp_len);
- /* Check for communication error and mask with an internal error code */
- if (rslt != BMP280_OK)
- {
- rslt = BMP280_E_COMM_FAIL;
- }
- }
- else
- {
- rslt = BMP280_E_INVALID_LEN;
- }
- }
- else
- {
- rslt = BMP280_E_NULL_PTR;
- }
- return rslt;
- }
软复位:
- int8_t bmp280_soft_reset(const struct bmp280_dev *dev)
- {
- int8_t rslt;
- uint8_t reg_addr = BMP280_SOFT_RESET_ADDR;
- uint8_t soft_rst_cmd = BMP280_SOFT_RESET_CMD;
- rslt = null_ptr_check(dev);
- if (rslt == BMP280_OK)
- {
- rslt = bmp280_set_regs(®_addr, &soft_rst_cmd, 1, dev);
- /* As per the datasheet, startup time is 2 ms. */
- dev->delay_ms(2);
- }
- return rslt;
- }
硬件初始化:
- int8_t bmp280_init(struct bmp280_dev *dev)
- {
- int8_t rslt;
- /* Maximum number of tries before timeout */
- uint8_t try_count = 5;
- rslt = null_ptr_check(dev);
- if (rslt == BMP280_OK)
- {
- while (try_count)
- {
- rslt = bmp280_get_regs(BMP280_CHIP_ID_ADDR, &dev->chip_id, 1, dev);
- /* Check for chip id validity */
- if ((rslt == BMP280_OK) &&
- (dev->chip_id == BMP280_CHIP_ID1 || dev->chip_id == BMP280_CHIP_ID2 || dev->chip_id == BMP280_CHIP_ID3))
- {
- rslt = bmp280_soft_reset(dev);
- if (rslt == BMP280_OK)
- {
- rslt = get_calib_param(dev);
- }
- break;
- }
- /* Wait for 10 ms */
- dev->delay_ms(10);
- --try_count;
- }
- /* Chip id check failed, and timed out */
- if (!try_count)
- {
- rslt = BMP280_E_DEV_NOT_FOUND;
- }
- if (rslt == BMP280_OK)
- {
- /* Set values to default */
- dev->conf.filter = BMP280_FILTER_OFF;
- dev->conf.os_pres = BMP280_OS_NONE;
- dev->conf.os_temp = BMP280_OS_NONE;
- dev->conf.odr = BMP280_ODR_0_5_MS;
- dev->conf.spi3w_en = BMP280_SPI3_WIRE_DISABLE;
- }
- }
- return rslt;
- }
获取配置:
- int8_t bmp280_get_config(struct bmp280_config *conf, struct bmp280_dev *dev)
- {
- int8_t rslt;
- uint8_t temp[2] = { 0, 0 };
- rslt = null_ptr_check(dev);
- if ((rslt == BMP280_OK) && (conf != NULL))
- {
- rslt = bmp280_get_regs(BMP280_CTRL_MEAS_ADDR, temp, 2, dev);
- if (rslt == BMP280_OK)
- {
- conf->os_temp = BMP280_GET_BITS(BMP280_OS_TEMP, temp[0]);
- conf->os_pres = BMP280_GET_BITS(BMP280_OS_PRES, temp[0]);
- conf->odr = BMP280_GET_BITS(BMP280_STANDBY_DURN, temp[1]);
- conf->filter = BMP280_GET_BITS(BMP280_FILTER, temp[1]);
- conf->spi3w_en = BMP280_GET_BITS_POS_0(BMP280_SPI3_ENABLE, temp[1]);
- dev->conf = *conf;
- }
- }
- else
- {
- rslt = BMP280_E_NULL_PTR;
- }
- return rslt;
- }
获取状态:
- int8_t bmp280_get_status(struct bmp280_status *status, const struct bmp280_dev *dev)
- {
- int8_t rslt;
- uint8_t temp;
- rslt = null_ptr_check(dev);
- if ((rslt == BMP280_OK) && (status != NULL))
- {
- rslt = bmp280_get_regs(BMP280_STATUS_ADDR, &temp, 1, dev);
- status->measuring = BMP280_GET_BITS(BMP280_STATUS_MEAS, temp);
- status->im_update = BMP280_GET_BITS_POS_0(BMP280_STATUS_IM_UPDATE, temp);
- }
- else
- {
- rslt = BMP280_E_NULL_PTR;
- }
- return rslt;
- }
获取电源模式
- int8_t bmp280_get_power_mode(uint8_t *mode, const struct bmp280_dev *dev)
- {
- int8_t rslt;
- uint8_t temp;
- rslt = null_ptr_check(dev);
- if ((rslt == BMP280_OK) && (mode != NULL))
- {
- rslt = bmp280_get_regs(BMP280_CTRL_MEAS_ADDR, &temp, 1, dev);
- *mode = BMP280_GET_BITS_POS_0(BMP280_POWER_MODE, temp);
- }
- else
- {
- rslt = BMP280_E_NULL_PTR;
- }
- return rslt;
- }
设置电源模式:
- int8_t bmp280_set_power_mode(uint8_t mode, struct bmp280_dev *dev)
- {
- int8_t rslt;
- rslt = null_ptr_check(dev);
- if (rslt == BMP280_OK)
- {
- rslt = conf_sensor(mode, &dev->conf, dev);
- }
- return rslt;
- }
获取原始数据:
- int8_t bmp280_get_uncomp_data(struct bmp280_uncomp_data *uncomp_data, const struct bmp280_dev *dev)
- {
- int8_t rslt;
- uint8_t temp[6] = { 0 };
- rslt = null_ptr_check(dev);
- if ((rslt == BMP280_OK) && (uncomp_data != NULL))
- {
- rslt = bmp280_get_regs(BMP280_PRES_MSB_ADDR, temp, 6, dev);
- if (rslt == BMP280_OK)
- {
- uncomp_data->uncomp_press =
- (int32_t) ((((uint32_t) (temp[0])) << 12) | (((uint32_t) (temp[1])) << 4) | ((uint32_t) temp[2] >> 4));
- uncomp_data->uncomp_temp =
- (int32_t) ((((int32_t) (temp[3])) << 12) | (((int32_t) (temp[4])) << 4) | (((int32_t) (temp[5])) >> 4));
- rslt = st_check_boundaries((int32_t)uncomp_data->uncomp_temp, (int32_t)uncomp_data->uncomp_press);
- }
- else
- {
- rslt = BMP280_E_UNCOMP_DATA_CALC;
- }
- }
- else
- {
- rslt = BMP280_E_NULL_PTR;
- }
- return rslt;
- }