3.2.1. bmp280 — BMP280 temperature and pressure sensor

BMP280 is a temperature and pressure sensor from Bosch Sensortec.

../../../_images/MFG_BST-BMP280-DS001-12.jpg

This driver supports both I2C and SPI for device communication.

Datasheet: Datasheet BMP280

3.2.1.1. Example usage

This example illustrates how to initialize the driver with an I2C transport layer and then read temperature and pressure from the BMP280 device.

struct bmp280_driver_t bmp280;
struct i2c_driver_t i2c;
struct bmp280_transport_i2c_t transport;
float temperature;
float pressure;
float altitude;

/* Initialize and start a I2C driver. */
i2c_init(&i2c, &i2c_device[0], I2C_BAUDRATE_100KBPS, -1);
i2c_start(&i2c);

/* Initialize the BMP280 I2C transport layer. */
bmp280_transport_i2c_init(&transport,
                          &i2c,
                          BMP280_I2C_ADDRESS_AUTOMATIC);

/* Initialize and start the BMP280 driver with the I2C
   transport layer. */
bmp280_init(&bmp280,
            &transport.base,
            bmp280_mode_normal_t,
            bmp280_standby_time_500_us_t,
            bmp280_filter_off_t,
            bmp280_temperature_oversampling_1_t,
            bmp280_pressure_oversampling_1_t);
bmp280_start(&bmp280);

/* Read temperature and pressure from the BMP280. */
bmp280_read(&bmp280, &temperature, &pressure);

/* Calculate the altitude from read pressure. */
altitude = science_pressure_to_altitude(
    pressure,
    SCIENCE_SEA_LEVEL_STANDARD_PRESSURE);

std_printf(OSTR("Temperature: %f\r\n"
                "Pressure: %f\r\n"
                "Altitude: %f\r\n"),
           temperature,
           pressure,
           altitude);

Source code: src/drivers/sensors/bmp280.h, src/drivers/sensors/bmp280.c

Test code: tst/drivers/software/sensors/bmp280/main.c

Example code: examples/bmp280/main.c


Defines

BMP280_I2C_ADDRESS_0
BMP280_I2C_ADDRESS_1

I2C address #1.

BMP280_I2C_ADDRESS_AUTOMATIC

Automatic I2C address detection.

BMP280_SPI_POLARITY

Default SPI polarity and phase. Polarity 1 and phase 1 is also supported.

BMP280_SPI_PHASE

Enums

enum bmp280_mode_t

Mode configuration.

Values:

bmp280_mode_forced_t = 1
bmp280_mode_normal_t = 3
enum bmp280_standby_time_t

Standby time in normal mode configuration.

Values:

bmp280_standby_time_500_us_t = 0
bmp280_standby_time_62500_us_t
bmp280_standby_time_125_ms_t
bmp280_standby_time_250_ms_t
bmp280_standby_time_500_ms_t
bmp280_standby_time_1_s_t
bmp280_standby_time_2_s_t
bmp280_standby_time_4_s_t
enum bmp280_filter_t

Filter configuration.

Values:

bmp280_filter_off_t = 0
bmp280_filter_2_t
bmp280_filter_4_t
bmp280_filter_8_t
bmp280_filter_16_t
enum bmp280_temperature_oversampling_t

Temperature oversampling configuration.

Values:

bmp280_temperature_off_t = 0
bmp280_temperature_oversampling_1_t
bmp280_temperature_oversampling_2_t
bmp280_temperature_oversampling_4_t
bmp280_temperature_oversampling_8_t
bmp280_temperature_oversampling_16_t
enum bmp280_pressure_oversampling_t

Pressure oversampling configuration.

Values:

bmp280_pressure_off_t = 0
bmp280_pressure_oversampling_1_t
bmp280_pressure_oversampling_2_t
bmp280_pressure_oversampling_4_t
bmp280_pressure_oversampling_8_t
bmp280_pressure_oversampling_16_t

Functions

int bmp280_module_init(void)

Initialize the bmp280 module. This function must be called before calling any other function in this module.

The module will only be initialized once even if this function is called multiple times.

Return
zero(0) or negative error code.

int bmp280_init(struct bmp280_driver_t *self_p, struct bmp280_transport_t *transport_p, enum bmp280_mode_t mode, enum bmp280_standby_time_t standby_time, enum bmp280_filter_t filter, enum bmp280_temperature_oversampling_t temperature_oversampling, enum bmp280_pressure_oversampling_t pressure_oversampling)

Initialize given driver object.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to be initialized.
  • transport_p: Transport protocol to use. Reference to an I2C or SPI transport object.
  • mode: Normal or forced mode. In normal mode the device is periodically measuring temperature and pressure. In burst mode the device is in sleeping (low power consumption) and the MCU wakes it to request a measurement.
  • standby_time: Normal mode standby time.
  • filter: Filter configuration, normally only set when the device is in normal mode.
  • temperature_oversampling: Temperature oversampling.
  • pressure_oversampling: Pressure oversampling.

int bmp280_start(struct bmp280_driver_t *self_p)

Start given driver by entering normal mode (if mode is bmp280_mode_normal_t), and reading calibration data from the device.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to start.

int bmp280_stop(struct bmp280_driver_t *self_p)

Stop given driver by resetting it.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to stop.

int bmp280_read(struct bmp280_driver_t *self_p, float *temperature_p, float *pressure_p)

Read temperature and pressure from the device.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object.
  • temperature_p: Temperature in Celsius, or NULL.
  • pressure_p: Pressure in Pascal, or NULL.

int bmp280_read_fixed_point(struct bmp280_driver_t *self_p, long *temperature_p, long *pressure_p)

Read temperature and pressure from the device and return them as fixed point numbers with three decimal places.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object.
  • temperature_p: Temperature in milli-Celsius, or NULL.
  • pressure_p: Pressure in milli-Pascal, or NULL.

int bmp280_transport_i2c_init(struct bmp280_transport_i2c_t *self_p, struct i2c_driver_t *i2c_p, int i2c_address)

Initialize given I2C transport object.

Return
zero(0) or negative error code.
Parameters
  • self_p: I2C transport object to be initialized.
  • transport_p: I2C driver to use.
  • i2c_address: Device I2C address, one of BMP280_I2C_ADDRESS_0, BMP280_I2C_ADDRESS_1 and BMP280_I2C_ADDRESS_AUTOMATIC.

int bmp280_transport_spi_init(struct bmp280_transport_spi_t *self_p, struct spi_driver_t *spi_p)

Initialize given SPI transport object.

Return
zero(0) or negative error code.
Parameters
  • self_p: SPI transport object to be initialized.
  • spi_p: SPI driver to use.

struct bmp280_driver_t
#include <bmp280.h>

The BMP280 driver struct.

Public Members

struct bmp280_transport_t *transport_p
uint8_t ctrl_meas
uint8_t config
int16_t calibration[12]
struct log_object_t log
struct bmp280_transport_t

Public Members

struct bmp280_transport_protocol_t *protocol_p
struct bmp280_transport_i2c_t

Public Members

struct bmp280_transport_t base
struct i2c_driver_t *i2c_p
int i2c_address
struct bmp280_transport_spi_t

Public Members

struct bmp280_transport_t base
struct spi_driver_t *spi_p