dps310: driver for the Infineon DPS310 pressure sensor

The DPS310 measures pressure from 300 to 1200 hPa and temperature from -40 to +85 °C. Both values are compensated inside the driver with the calibration coefficients the sensor keeps in its OTP memory, so the application receives pascals and degrees Celsius, not raw counts. A pressure reading is also the basis of the altitude calculation the component provides.

Features

  • pressure in pascals and temperature in degrees Celsius, both compensated

  • 1 to 128 measurements per second, 1 to 128 times oversampling per value

  • command mode (one measurement on request) and background mode (continuous)

  • internal or external temperature sensor for the compensation

  • pressure and temperature ready flags, so polling can be avoided

  • an 8-bit FIFO with interrupts on a full FIFO or a ready measurement

  • altitude with a calibration against a known height

  • 1 MHz I2C (the sensor itself allows 3.4 MHz, the ESP-IDF driver stops at 1 MHz)

Wiring

The sensor uses I2C at address 0x76 when the SDO pin is tied low and 0x77 when it is tied high (DPS310_I2C_ADDRESS_0 and DPS310_I2C_ADDRESS_1); the examples use 0x77. The supply is 1.7 to 3.6 V.

DPS310

ESP32

VDD

3.3 V

GND

GND

SDA

CONFIG_EXAMPLE_I2C_MASTER_SDA (18 on ESP32/S2/S3)

SCL

CONFIG_EXAMPLE_I2C_MASTER_SCL (19 on ESP32/S2/S3)

SDO

GND for 0x76, VDD for 0x77

Usage

#include <dps310.h>

static dps310_t dev;

void app_main(void)
{
    ESP_ERROR_CHECK(i2cdev_init());

    ESP_ERROR_CHECK(dps310_init_desc(&dev, DPS310_I2C_ADDRESS_1, 0,
            CONFIG_EXAMPLE_I2C_MASTER_SDA, CONFIG_EXAMPLE_I2C_MASTER_SCL));

    dps310_config_t config = DPS310_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(dps310_init(&dev, &config));

    // one measurement on request
    ESP_ERROR_CHECK(dps310_set_mode(&dev, DPS310_MODE_COMMAND_PRESSURE));
    float pressure;
    if (dps310_read_pressure_wait(&dev, 10, 10, &pressure) == ESP_OK)
        printf("Pressure: %.1f Pa\n", pressure);

    dps310_free_desc(&dev);
    i2cdev_done();
}

dps310_init_desc() only fills the descriptor and creates the mutex; dps310_init() is what talks to the chip: it checks the product ID, resets the sensor, reads the 18 calibration coefficients and applies the configuration.

DPS310_CONFIG_DEFAULT() sets 1 measurement per second with 16 times oversampling for both pressure and temperature, takes the temperature from the external sensor (the one inside the pressure MEMS element) and enables the right shift of the results.

Modes

Mode

Meaning

DPS310_MODE_STANDBY

no measurements

DPS310_MODE_COMMAND_PRESSURE

one pressure measurement, then standby

DPS310_MODE_COMMAND_TEMPERATURE

one temperature measurement, then standby

DPS310_MODE_BACKGROUND_PRESSURE

continuous pressure

DPS310_MODE_BACKGROUND_TEMPERATURE

continuous temperature

DPS310_MODE_BACKGROUND_ALL

continuous pressure and temperature

dps310_backgorund_start() and dps310_backgorund_stop() are thin wrappers around dps310_set_mode() with the name stressed.

In command mode the result is not ready immediately: either poll dps310_is_ready_for_pressure() / dps310_is_ready_for_temp(), or use the ..._wait() variants, which retry for the given number of attempts with the given delay in milliseconds. The delay is rounded up to at least one FreeRTOS tick, so a ten-millisecond delay still sleeps with a 100 Hz tick.

dps310_read_raw() returns the uncompensated 24-bit value from either the pressure or the temperature register block. Use it only for diagnostics; the compensated functions are dps310_read_pressure() and dps310_read_temp().

Rates and oversampling

dps310_set_rate_p()/dps310_set_rate_t() take DPS310_PM_RATE_1 … DPS310_PM_RATE_128 (DPS310_TMP_RATE_* for temperature), that is 1 to 128 measurements per second. dps310_set_oversampling_p()/..._t() take DPS310_PM_PRC_1 … DPS310_PM_PRC_128 (DPS310_TMP_PRC_*), the number of internal samples averaged into one result. More oversampling means less noise and a longer measurement, and the driver has to know the ratio: the pressure compensation divides the raw value by the scale factor that matches the oversampling of that measurement, so a value read with one rate and compensated with another is wrong. The driver keeps the last rates in dev->p_rate and dev->t_rate for exactly this reason; change the hardware registers through the setters, not behind the driver’s back.

Changing the oversampling while the FIFO is enabled empties the FIFO: the entries already in it were taken with the old resolution and cannot be compensated with the new one. The compensated reads also need a descriptor that went through dps310_init(), because they rely on the cached rates; before that they return ESP_ERR_INVALID_STATE instead of guessing.

Oversampling above 8 times needs the result bit-shift: the hardware shifts the result to a 24-bit value before the driver scales it. dps310_init() therefore returns ESP_ERR_INVALID_ARG when the oversampling is above 8 and the shift is disabled, and the setters reject values outside their enumerations instead of writing stray bits into the registers.

dps310_set_tmp_ext() selects whether the temperature comes from the ASIC (DPS310_TMP_SRC_INTERNAL) or from the MEMS element (DPS310_TMP_SRC_EXTERNAL). dps310_set_tmp_coef_ext() selects the source of the temperature coefficients. With the external source the coefficients are used together with the external temperature; if the coefficients were measured with the internal sensor, the result is less accurate. The default takes both from the external source.

Interrupts and FIFO

dps310_set_int_hl() sets the polarity of the interrupt pin, dps310_set_int_prs()/dps310_set_int_tmp() enable the “a measurement is ready” interrupts, dps310_set_int_fifo() enables the “FIFO is full” interrupt. The driver configures the sensor only; the pin itself is a plain GPIO that the application has to configure and read (or attach an interrupt handler to) on its own.

The FIFO holds up to 8 measurements. dps310_set_fifo_en() enables it, dps310_is_fifo_empty() tells whether there is something to read, dps310_read_fifo() returns one entry with its type (DPS310_MEASUREMENT_TEMPERATURE, DPS310_MEASUREMENT_PRESSURE or DPS310_MEASUREMENT_EMPTY) and the already compensated value, and dps310_flush_fifo() clears it.

A pressure entry is compensated with the cached temperature, and the cache is only valid for one oversampling: the value read with one averaging ratio cannot be used with another. dps310_read_fifo() returns ESP_ERR_INVALID_STATE for a pressure entry when no temperature sample with the current oversampling has been read yet, so keep the FIFO’s temperature entries enabled or call dps310_read_temp() first.

Altitude

ESP_ERROR_CHECK(dps310_calibrate_altitude(&dev, 120.0f));   // known height
float altitude;
ESP_ERROR_CHECK(dps310_read_altitude(&dev, &altitude));

By default the altitude is calculated from the average sea-level pressure DPS310_AVERAGE_SEA_LEVEL_PRESSURE_Pa. dps310_calibrate_altitude() measures the current pressure, compares it with the known height and keeps the difference in dev->offset; dps310_calc_altitude() and dps310_read_altitude() add that offset. Calibration is worth doing because the local sea-level pressure differs from the average by more than the sensor error.

The known altitude must be finite and lie between -1000 and 44330 metres, the height where the formula divides by zero; anything else returns ESP_ERR_INVALID_ARG. During the calibration the function raises the oversampling to 64 times, enables both result shifts, switches the chip to the command modes and then restores the previous oversampling, shifts and operation mode, so a sensor running in the background mode keeps measuring. If a restore step fails, the function returns an error: the remaining steps are still tried, so the sensor is left with some of the calibration settings and some of its own.

A note on the quirk

dps310_init() calls dps310_quirk(), which writes undocumented values to undocumented registers. The issue and the workaround come from Infineon’s own DPS310 library; the data sheet and the errata do not mention it. After the magic writes the function re-reads the coefficients and performs one temperature measurement. The coefficients are read again after the coefficient source is selected, and the temperature taken by the quirk is dropped once dps310_init() applies the real configuration: it belongs to the previous oversampling. The function is public in case a sensor needs it again after a reset.

Limitations

  • only I2C is supported, although the chip also speaks SPI

  • the driver does not touch the interrupt pin, see above

  • the measurement rate and oversampling must be changed through the driver, see above

  • temperature accuracy is limited by the coefficient source, see above

  • the FIFO depth is 8 measurements, and the driver reads one entry per call

Examples

  • default — one pressure and one temperature reading

  • altitude — calibrated altitude

  • background — continuous measurements in background mode

Reference

Defines

DPS310_I2C_ADDRESS_0

I2C address when SDO pin is low.

DPS310_I2C_ADDRESS_1

I2C address when SDO pin is high.

DPS310_PRODUCT_ID

Product ID.

DPS310_REVISION_ID

Revision ID.

DPS310_AVERAGE_SEA_LEVEL_PRESSURE_hPa

Average sea-level pressure in hPa.

DPS310_AVERAGE_SEA_LEVEL_PRESSURE_Pa

Average sea-level pressure in Pa.

DPS310_CONFIG_DEFAULT()

A macro to set default dps310_config_t.

DPS310_REG_PRS_B2

DPS310 registers.

DPS310_REG_PRS_B1
DPS310_REG_PRS_B0
DPS310_REG_TMP_B2
DPS310_REG_TMP_B1
DPS310_REG_TMP_B0
DPS310_REG_PRS_CFG
DPS310_REG_TMP_CFG
DPS310_REG_MEAS_CFG
DPS310_REG_CFG_REG
DPS310_REG_INT_STS
DPS310_REG_FIFO_STS
DPS310_REG_RESET
DPS310_REG_ID
DPS310_REG_COEF
DPS310_REG_COEF_LEN
DPS310_REG_COEF_SRCE
DPS310_REG_ID_REV_MASK
DPS310_REG_ID_PROD_MASK
DPS310_REG_RESET_FIFO_FLUSH_MASK
DPS310_REG_RESET_SOFT_RST_MASK
DPS310_REG_PRS_CFG_PM_RATE_MASK
DPS310_REG_TMP_CFG_TMP_RATE_MASK
DPS310_REG_PRS_CFG_PM_PRC_MASK
DPS310_REG_TMP_CFG_TMP_EXT_MASK
DPS310_REG_TMP_CFG_TMP_PRC_MASK
DPS310_REG_CFG_REG_INT_HL_MASK
DPS310_REG_CFG_REG_INT_FIFO_MASK
DPS310_REG_CFG_REG_INT_TMP_MASK
DPS310_REG_CFG_REG_INT_PRS_MASK
DPS310_REG_CFG_REG_T_SHIFT_MASK
DPS310_REG_CFG_REG_P_SHIFT_MASK
DPS310_REG_CFG_REG_FIFO_EN_MASK
DPS310_REG_CFG_REG_SPI_MODE_MASK
DPS310_REG_MEAS_CFG_COEF_RDY_MASK
DPS310_REG_MEAS_CFG_SENSOR_RDY_MASK
DPS310_REG_MEAS_CFG_TMP_RDY_MASK
DPS310_REG_MEAS_CFG_PRS_RDY_MASK
DPS310_REG_MEAS_CFG_MEAS_CTRL_MASK
DPS310_REG_COEF_SRCE_MASK
DPS310_REG_FIFO_STS_FIFO_EMPTY_MASK
DPS310_REG_FIFO_STS_FIFO_FULL_MASK
DPS310_I2C_FREQ_MAX_HZ
DPS310_I2C_FREQ_MAX_ESP_IDF_HZ
DPS310_SPI_FREQ_MAX_HZ
DPS310_TRIM_READY_DELAY_MS
DPS310_SENSOR_READY_DELAY_MS
DPS310_COEFFICIENTS_READY_DELAY_MS
DPS310_STARTUP_DELAY_MS
DPS310_PROD_ID
DPS310_REG_SENSOR_VALUE_LEN
DPS310_REG_FIFO
DPS310_FIFO_EMPTY
DPS310_FIFO_FLUSH_VALUE
DPS310_SOFT_RST_VALUE

Enums

enum dps310_mode_t

Mode of DPS310 module operation.

See 4.1 Operating Modes.

Values:

enumerator DPS310_MODE_STANDBY

Standby mode.

enumerator DPS310_MODE_COMMAND_PRESSURE

Command mode, pressure measurement.

enumerator DPS310_MODE_COMMAND_TEMPERATURE

Command mode, temperature measurement.

enumerator DPS310_MODE_BACKGROUND_PRESSURE

Background mode, continuous pressure measurement.

enumerator DPS310_MODE_BACKGROUND_TEMPERATURE

Background mode, continuous temperature measurement.

enumerator DPS310_MODE_BACKGROUND_ALL

Background mode, continuous pressure and temperature measurement.

enum dps310_pm_rate_t

Pressure measurement rate.

Values:

enumerator DPS310_PM_RATE_1

1 measurements / sec

enumerator DPS310_PM_RATE_2

2 measurements / sec

enumerator DPS310_PM_RATE_4

4 measurements / sec

enumerator DPS310_PM_RATE_8

8 measurements / sec

enumerator DPS310_PM_RATE_16

16 measurements / sec

enumerator DPS310_PM_RATE_32

32 measurements / sec

enumerator DPS310_PM_RATE_64

64 measurements / sec

enumerator DPS310_PM_RATE_128

128 measurements / sec

enum dps310_pm_oversampling_t

Pressure resolution, or oversampling rate.

Values:

enumerator DPS310_PM_PRC_1

Single (Low Precision)

enumerator DPS310_PM_PRC_2

2 times (Low Power).

enumerator DPS310_PM_PRC_4

4 times

enumerator DPS310_PM_PRC_8

8 times

enumerator DPS310_PM_PRC_16

16 times (Standard)

enumerator DPS310_PM_PRC_32

32 times

enumerator DPS310_PM_PRC_64

64 times (High Precision)

enumerator DPS310_PM_PRC_128

128 times

enum dps310_tmp_src_ext_t

Temperature measurement source.

Used for temperature measurement and temperature coefficients.

Values:

enumerator DPS310_TMP_SRC_INTERNAL

Internal sensor (in ASIC)

enumerator DPS310_TMP_SRC_EXTERNAL

External sensor (in pressure sensor MEMS element)

enum dps310_tmp_rate_t

Temperature measurement rate.

Values:

enumerator DPS310_TMP_RATE_1

1 measurements / sec

enumerator DPS310_TMP_RATE_2

2 measurements / sec

enumerator DPS310_TMP_RATE_4

4 measurements / sec

enumerator DPS310_TMP_RATE_8

8 measurements / sec

enumerator DPS310_TMP_RATE_16

16 measurements / sec

enumerator DPS310_TMP_RATE_32

32 measurements / sec

enumerator DPS310_TMP_RATE_64

64 measurements / sec

enumerator DPS310_TMP_RATE_128

128 measurements / sec

enum dps310_tmp_oversampling_t

Pressure resolution, or oversampling rate.

Values:

enumerator DPS310_TMP_PRC_1

Single (Low Precision)

enumerator DPS310_TMP_PRC_2

2 times (Low Power).

enumerator DPS310_TMP_PRC_4

4 times

enumerator DPS310_TMP_PRC_8

8 times

enumerator DPS310_TMP_PRC_16

16 times (Standard)

enumerator DPS310_TMP_PRC_32

32 times

enumerator DPS310_TMP_PRC_64

64 times (High Precision)

enumerator DPS310_TMP_PRC_128

128 times

enum dps310_int_hl_active_level_t

Interupt (on SDO pin) active level.

Values:

enumerator DPS310_INT_HL_ACTIVE_LOW

Active low.

enumerator DPS310_INT_HL_ACTIVE_HIGH

Active high.

enum dps310_int_fifo_mode_t

Mode of interupt when the FIFO is full.

Values:

enumerator DPS310_INT_FIFO_DISABLE

Disable interrupt when the FIFO is full.

enumerator DPS310_INT_FIFO_ENABLE

Enable interrupt when the FIFO is full.

enum dps310_int_tmp_mode_t

Mode of interupt when a temperature measurement is ready.

Values:

enumerator DPS310_INT_TMP_DISABLE

Disable interrupt when a temperature measurement is ready.

enumerator DPS310_INT_TMP_ENABLE

Enable interrupt when a temperature measurement is ready.

enum dps310_int_prs_mode_t

Mode of interupt when a pressure measurement is ready.

Values:

enumerator DPS310_INT_PRS_DISABLE

Disable interrupt when a pressure measurement is ready.

enumerator DPS310_INT_PRS_ENABLE

Enable interrupt when a pressure measurement is ready.

enum dps310_t_shift_mode_t

Mode of temperature result bit-shift.

Values:

enumerator DPS310_T_SHIFT_DISABLE

No shift.

enumerator DPS310_T_SHIFT_ENABLE

Shift result right in data register.

enum dps310_p_shift_mode_t

Mode of pressure result bit-shift.

Values:

enumerator DPS310_P_SHIFT_DISABLE

No shift.

enumerator DPS310_P_SHIFT_ENABLE

Shift result right in data register.

enum dps310_fifo_en_mode_t

Mode of FIFO.

Values:

enumerator DPS310_FIFO_DISABLE

Disable FIFO.

enumerator DPS310_FIFO_ENABLE

Enable FIFO.

enum dps310_spi_mode_t

SPI mode.

Values:

enumerator DPS310_SPI_MODE_4WIRE

SPI 4-wires.

enumerator DPS310_SPI_MODE_3WIRE

SPI 3-wires.

enum dps310_fifo_measurement_type_t

Type of measurement result in FIFO.

When the type is DPS310_MEASUREMENT_EMPTY, the result is always zero. Otherwise, the result is the compensated value of each type.

Values:

enumerator DPS310_MEASUREMENT_TEMPERATURE

Temperature.

enumerator DPS310_MEASUREMENT_PRESSURE

Pressure.

enumerator DPS310_MEASUREMENT_EMPTY

Empty, no measurement available.

Functions

esp_err_t dps310_init_desc(dps310_t *dev, uint8_t addr, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)

Initialize device descriptor.

Parameters:
Returns:

ESP_OK on success

esp_err_t dps310_free_desc(dps310_t *dev)

Free the device descriptor.

The device descriptor must NOT be NULL. dps310_free_desc() does not free() the device descriptor on error.

Parameters:

dev – [out] Device descriptor

Returns:

ESP_OK on success

esp_err_t dps310_init(dps310_t *dev, dps310_config_t *config)

Initialize DPS310 module.

The function does the followings:

  • read the DPS310_REG_ID, and identify the product ID. Return ESP_FAIL if the product ID does not match expected product ID.

  • reset the chip

  • perform a quirk

Parameters:
  • dev – [in] Device descriptor

  • config – [in] Configuration

Returns:

ESP_OK on success

esp_err_t dps310_reset(dps310_t *dev)

Reset the device.

Perform “soft reset” and ensure the chip is fully functional by delaying DPS310_STARTUP_DELAY_MS.

Parameters:

dev – [in] The device descriptor

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev and/or config is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_rate_p(dps310_t *dev, dps310_pm_rate_t *value)

Get pressure measurement rate.

Parameters:
  • dev – [in] The device descriptor

  • value – [out] the value in the resister

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev and/or value is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_rate_p(dps310_t *dev, dps310_pm_rate_t value)

Set pressure measurement rate.

Parameters:
  • dev – [in] The device descriptor.

  • value – [in] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev and/or config is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_rate_t(dps310_t *dev, dps310_tmp_rate_t *value)

Get temperature measurement rate.

Parameters:
  • dev – [in] The device descriptor

  • value – [out] the value in the resister

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev and/or value is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_rate_t(dps310_t *dev, dps310_tmp_rate_t value)

Set temperature measurement rate.

Parameters:
  • dev – [in] The device descriptor.

  • value – [in] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev and/or config is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_oversampling_p(dps310_t *dev, dps310_pm_oversampling_t *value)

Get pressure oversampling rate.

Parameters:
  • dev – [in] The device descriptor

  • value – [out] the value in the resister

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev and/or value is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_oversampling_p(dps310_t *dev, dps310_pm_oversampling_t value)

Set pressure oversampling rate.

Parameters:
  • dev – [in] The device descriptor.

  • value – [in] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev and/or config is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_oversampling_t(dps310_t *dev, dps310_tmp_oversampling_t *value)

Get temperature oversampling rate.

Parameters:
  • dev – [in] The device descriptor

  • value – [out] the value in the resister

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev and/or value is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_oversampling_t(dps310_t *dev, dps310_tmp_oversampling_t value)

Set temperature oversampling rate.

Parameters:
  • dev – [in] The device descriptor.

  • value – [in] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev and/or config is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_tmp_ext(dps310_t *dev, dps310_tmp_src_ext_t *value)

Get temperature measurement source.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] the value in the resister.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev and/or value is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_tmp_ext(dps310_t *dev, dps310_tmp_src_ext_t value)

Set temperature measurement source.

Parameters:
  • dev – [in] The device descriptor.

  • value – [in] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_tmp_coef_ext(dps310_t *dev, dps310_tmp_src_ext_t value)

Set temperature coefficient source.

Parameters:
  • dev – [in] The device descriptor.

  • value – [in] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_int_hl(dps310_t *dev, dps310_int_hl_active_level_t *value)

Get interrupt active level.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] the value in the resister.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev and/or value is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_int_hl(dps310_t *dev, dps310_int_hl_active_level_t value)

Set interrupt active level.

Parameters:
  • dev – [in] The device descriptor.

  • value – [in] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_int_fifo(dps310_t *dev, dps310_int_fifo_mode_t *value)

Get the status of FIFO interrupt.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] Current configuration of INT_FIFO.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev and/or value is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_int_fifo(dps310_t *dev, dps310_int_fifo_mode_t value)

Set the status of FIFO interrupt.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_int_tmp(dps310_t *dev, dps310_int_tmp_mode_t *value)

Get the status of temperature interrupt.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] Current configuration of INT_TMP.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev and/or value is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_int_tmp(dps310_t *dev, dps310_int_tmp_mode_t value)

Set the status of temperature interrupt.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_int_prs(dps310_t *dev, dps310_int_prs_mode_t *value)

Get the status of pressure interrupt.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] Current configuration of INT_PRS.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev and/or value is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_int_prs(dps310_t *dev, dps310_int_prs_mode_t value)

Set the status of pressure interrupt.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_t_shift(dps310_t *dev, dps310_t_shift_mode_t *value)

Get the status of temperature result bit-shift.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] Current configuration of T_SHIFT.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev and/or value is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_t_shift(dps310_t *dev, dps310_t_shift_mode_t value)

Set the status of temperature result bit-shift.

Must be set to DPS310_T_SHIFT_ENABLE when the oversampling rate is >8 times.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_p_shift(dps310_t *dev, dps310_p_shift_mode_t *value)

Get the status of pressure result bit-shift.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] Current configuration of P_SHIFT.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev and/or value is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_p_shift(dps310_t *dev, dps310_p_shift_mode_t value)

Set the status of pressure result bit-shift.

Must be set to DPS310_P_SHIFT_ENABLE when the oversampling rate is >8 times.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_fifo_en(dps310_t *dev, dps310_fifo_en_mode_t *value)

Get the status of FIFO.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] Current configuration of FIFO_EN.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev and/or value is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_fifo_en(dps310_t *dev, dps310_fifo_en_mode_t value)

Set the status of FIFO.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_spi_mode(dps310_t *dev, dps310_spi_mode_t *value)

Get the mode of SPI.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] Current configuration of SPI_MODE.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev and/or value is NULL, or other errors when I2C communication fails.

esp_err_t dps310_set_spi_mode(dps310_t *dev, dps310_spi_mode_t value)

Set the mode of SPI.

Parameters:
  • dev – [in] The device descriptor.

  • value – [out] The value to set.

Returns:

ESP_OK on success, ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_coef(dps310_t *dev)

Get Calibration Coefficients (COEF), update COEF in the device descriptor.

Parameters:

dev – [in] The device descriptor.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_get_mode(dps310_t *dev, dps310_mode_t *mode)

Get operating mode.

Parameters:
  • dev – [in] The device descriptor.

  • mode – [out] The operating mode.

esp_err_t dps310_set_mode(dps310_t *dev, dps310_mode_t mode)

Set operating mode.

Parameters:
  • dev – [in] The device descriptor.

  • mode – [in] The operating mode.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_flush_fifo(dps310_t *dev)

Flush FIFO.

Parameters:

dev – [in] The device descriptor.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_enable_fifo(dps310_t *dev, bool enable)

Enable or disable FIFO.

The function performs flush (dps310_flush_fifo()) before disabling FIFO.

Parameters:
  • dev – [in] The device descriptor.

  • enable – [in] Enable FIFO when true, disable FIFO when false.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_read_raw(dps310_t *dev, uint8_t reg, int32_t *value)

Read the raw sensor value from resisters.

The real raw value is 2’s complement. The function internally converts the value from the 2’s complement to an int32_t number.

Note

The function does not take the device mutex, so a concurrent call on the same descriptor interleaves with it. dps310_read_temp(), dps310_read_pressure() and dps310_read_fifo() serialize the whole exchange themselves.

Parameters:
  • dev – [in] The device descriptor.

  • reg – [in] Either DPS310_REG_TMP_B2 or DPS310_REG_PRS_B2.

  • value – [out] The raw value in the three resisters.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_read_pressure(dps310_t *dev, float *pressure)

Read compensated pressure value.

Parameters:
  • dev – [in] The device descriptor.

  • pressure – [out] Compensated pressure value in Pascal (not hPa).

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_read_pressure_wait(dps310_t *dev, uint16_t delay_ms, uint8_t max_attempt, float *pressure)

Read compensated pressure value after waiting for PRS_RDY bit.

Parameters:
  • dev – [in] The device descriptor.

  • delay_ms – [in] Delay in milliseconds between the attempts.

  • max_attempt – [in] Number of attempt to read.

  • pressure – [out] Compensated pressure value in Pascal (not hPa).

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL. ESP_ERR_TIMEOUT when failed to read the measurement within max_attempt, or other errors when I2C communication fails.

esp_err_t dps310_read_temp(dps310_t *dev, float *temperature)

Read compensated temperature value.

Parameters:
  • dev – [in] The device descriptor.

  • temperature – [out] Compensated temperature value.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_read_temp_wait(dps310_t *dev, uint16_t delay_ms, uint8_t max_attempt, float *temperature)

Read compensated temperature value after waiting for TMP_RDY bit.

Parameters:
  • dev – [in] The device descriptor.

  • delay_ms – [in] Delay in milliseconds between the attempts.

  • max_attempt – [in] Number of attempt to read.

  • temperature – [out] Compensated temperature value.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL. ESP_ERR_TIMEOUT when failed to read the measurement within max_attempt, or other errors when I2C communication fails.

esp_err_t dps310_is_ready_for(dps310_t *dev, uint8_t reg, uint8_t mask, bool *ready)

Test if a single bit in a resister is set.

Parameters:
  • dev – [in] The device descriptor.

  • reg – [in] The resister

  • mask – [in] bit mask to test

  • ready – [out] true when the bit is set, false when the bit is cleared.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_is_ready_for_coef(dps310_t *dev, bool *ready)

Test COEF_RDY in MEAS_CFG resister is set.

Parameters:
  • dev – [in] The device descriptor.

  • ready – [out] true when the bit is set, false when the bit is cleared.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_is_ready_for_sensor(dps310_t *dev, bool *ready)

Test SENSOR_RDY in MEAS_CFG resister is set.

Parameters:
  • dev – [in] The device descriptor.

  • ready – [out] true when the bit is set, false when the bit is cleared.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_is_ready_for_temp(dps310_t *dev, bool *ready)

Test TMP_RDY in MEAS_CFG resister is set.

Parameters:
  • dev – [in] The device descriptor.

  • ready – [out] true when the bit is set, false when the bit is cleared.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_is_ready_for_pressure(dps310_t *dev, bool *ready)

Test PRS_RDY in MEAS_CFG resister is set.

Parameters:
  • dev – [in] The device descriptor.

  • ready – [out] true when the bit is set, false when the bit is cleared.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_quirk(dps310_t *dev)

Reset undocumented internal resisters.

The function is supposed to fix an issue in the sensor by writing magic values to magic resisters. However, the issue is not documented. The latest data sheet does not mention the issue, nor an errata.

After issuing magic commands, the function re-reads COEF and temperature once so that the subsequent pressure reads return compensated values with internal cached parameters.

See: https://github.com/Infineon/DPS310-Pressure-Sensor#temperature-measurement-issue https://github.com/Infineon/DPS310-Pressure-Sensor/blob/3edb0e58dfd7691491ae8d7f6a86277b001ad93f/src/DpsClass.cpp#L442-L461 https://github.com/Infineon/DPS310-Pressure-Sensor/blob/ed02f803fc780cbcab54ed8b35dd3d718f2ebbda/src/Dps310.cpp#L84-L86 https://github.com/Infineon/DPS310-Pressure-Sensor/issues/15#issuecomment-475394536

Parameters:

dev – [in] The device descriptor.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_is_fifo_empty(dps310_t *dev, bool *result)

See if FIFO is empty.

Parameters:
  • dev – [in] The device descriptor.

  • result – [out] The result. true if empty, false otherwise.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_read_fifo(dps310_t *dev, dps310_fifo_measurement_t *measurement)

Read measurement result from FIFO.

Parameters:
  • dev – [in] The device descriptor.

  • measurement – [out] Measured value.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_backgorund_start(dps310_t *dev, dps310_mode_t mode)

Start background measurement.

This function is a syntax-sugar of dps310_set_mode() just for readbility and for an emphasis on a fact that measurement starts immediately after this.

Parameters:
  • dev – [in] The device descriptor.

  • mode – [in] The mode of background measurement.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_backgorund_stop(dps310_t *dev)

Stop background measurement.

This function is a syntax-sugar of dps310_set_mode().

Parameters:

dev – [in] The device descriptor.

Returns:

ESP_OK on success. ESP_ERR_INVALID_ARG when dev is NULL, or other errors when I2C communication fails.

esp_err_t dps310_calibrate_altitude(dps310_t *dev, float altitude_real)

Calibrate altitude offset from the altitude of the device.

Call this function before dps310_read_altitude() for higher accuracy.

By default, the driver calculates altitude using average sea-level pressure. This function updates internal offset of altitude by reading pressure from the sensor, and given altitude. There are public web services that provide altitude at a specific location, such as Google Earth.

The function attempts to keep original oversampling rates during calibration. When it fails to do so due to errors, the oversampling rates might be different.

Parameters:
  • dev – [in] The device descriptor.

  • altitude_real – [in] Real (known) altitude.

esp_err_t dps310_calc_altitude(dps310_t *dev, float pressure, float *altitude)

Calculate altitude from pressure.

Calculates altitude from pressure given. Call dps310_calibrate_altitude() before this function for higher accuracy. The function adds the offset to calculated altitude.

Parameters:
  • dev – [in] The device descriptor.

  • pressure – [in] The pressure.

  • altitude – [out] The calicurated altitude.

esp_err_t dps310_read_altitude(dps310_t *dev, float *altitude)

Read pressure from the sensor, calculates altitude.

Make sure that pressure measurement value is available.

Parameters:
  • dev – [in] The device descriptor.

  • altitude – [out] The calculated altitude.

struct dps310_fifo_measurement_t
#include <dps310.h>

Public Members

dps310_fifo_measurement_type_t type
float result
struct dps310_config_t
#include <dps310.h>

Configuration parameters for DPS310.

struct dps310_coef_t
#include <dps310.h>

Calibration Coefficients (COEF).

Public Members

int32_t c0
int32_t c1
int32_t c00
int32_t c10
int32_t c01
int32_t c11
int32_t c20
int32_t c21
int32_t c30
struct dps310_t
#include <dps310.h>

Device descriptor.

Public Members

i2c_dev_t i2c_dev

I2C device descriptor.

uint8_t prod_id

Product ID.

uint8_t prod_rev

Product revision.

uint8_t t_rate

latest temperature oversampling, cached for the compensation

uint8_t p_rate

latest pressure oversampling, cached for the compensation

int32_t t_raw

latest T_raw

uint8_t t_raw_rate

oversampling of the cached T_raw, 0xFF when there is no valid sample

dps310_coef_t coef

coefficients

float offset

offset in meter

float pressure_s

calculated pressure at sea-level