Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
dps310_i2c.c File Reference

Driver for the Infineon DPS310 barometer (I2C) More...

+ Include dependency graph for dps310_i2c.c:

Go to the source code of this file.

Macros

#define DPS310_BROKEN_RETRY_US   5000000
 retry a failed/absent sensor every 5s instead of giving up forever
 
#define DPS310_MAX_ERROR_CNT   10
 consecutive transaction failures before escalating
 
#define DPS310_PRESSURE_MIN_PA   30000.0f
 specified measurement range is 300-1200 hPa,
 
#define DPS310_PRESSURE_MAX_PA   120000.0f
 anything outside means the data got corrupted
 
#define DPS310_SCALE_FACTOR_KT   253952.0f
 temperature, 16x oversampling
 
#define DPS310_SCALE_FACTOR_KP   253952.0f
 pressure, 16x oversampling
 
#define DPS310_TEMP_FIX_SEQ_LEN   (sizeof(dps310_temp_fix_seq) / sizeof(dps310_temp_fix_seq[0]))
 

Functions

static int32_t getTwosComplement (uint32_t raw, uint8_t length)
 Sign-extend a raw two's complement value of arbitrary bit length.
 
static void parse_calib_data (struct Dps310_I2c *dps)
 Unpack the 18 calibration coefficient bytes (registers 0x10-0x21)
 
static void parse_sensor_data (struct Dps310_I2c *dps)
 Extract the raw 24-bit pressure and temperature measurements.
 
static void compensate_sensor (struct Dps310_I2c *dps)
 Convert raw measurements into pressure [Pa] and temperature [deg C].
 
void dps310_i2c_init (struct Dps310_I2c *dps, struct i2c_periph *i2c_p, uint8_t addr)
 Initialize the driver instance (no bus traffic yet)
 
void dps310_i2c_periodic (struct Dps310_I2c *dps)
 Run the driver state machine, submitting at most one I2C transaction.
 
void dps310_i2c_event (struct Dps310_I2c *dps)
 Consume finished I2C transactions and advance the state machine.
 

Variables

static const uint8_t dps310_temp_fix_seq [][2]
 DPS310 temperature errata fix sequence.
 

Detailed Description

Driver for the Infineon DPS310 barometer (I2C)

Non-blocking state machine driver:

* UNINIT -> GET_ID -> (TEMP_FIX) -> WAIT_RDY -> GET_COEF_SRCE -> GET_CALIB
*        -> CONFIGURE_REGS -> CONFIGURE_MEAS -> READ_DATA (looping)
* 

dps310_i2c_periodic() submits at most one I2C transaction per call and dps310_i2c_event() consumes the result and advances the state machine. The sensor runs in continuous background mode (16 Hz pressure and temperature at 16x oversampling); READ_DATA polls the latest results.

Definition in file dps310_i2c.c.

Macro Definition Documentation

◆ DPS310_BROKEN_RETRY_US

#define DPS310_BROKEN_RETRY_US   5000000

retry a failed/absent sensor every 5s instead of giving up forever

Definition at line 42 of file dps310_i2c.c.

◆ DPS310_MAX_ERROR_CNT

#define DPS310_MAX_ERROR_CNT   10

consecutive transaction failures before escalating

Definition at line 43 of file dps310_i2c.c.

◆ DPS310_PRESSURE_MAX_PA

#define DPS310_PRESSURE_MAX_PA   120000.0f

anything outside means the data got corrupted

Definition at line 45 of file dps310_i2c.c.

◆ DPS310_PRESSURE_MIN_PA

#define DPS310_PRESSURE_MIN_PA   30000.0f

specified measurement range is 300-1200 hPa,

Definition at line 44 of file dps310_i2c.c.

◆ DPS310_SCALE_FACTOR_KP

#define DPS310_SCALE_FACTOR_KP   253952.0f

pressure, 16x oversampling

Definition at line 50 of file dps310_i2c.c.

◆ DPS310_SCALE_FACTOR_KT

#define DPS310_SCALE_FACTOR_KT   253952.0f

temperature, 16x oversampling

Definition at line 49 of file dps310_i2c.c.

◆ DPS310_TEMP_FIX_SEQ_LEN

#define DPS310_TEMP_FIX_SEQ_LEN   (sizeof(dps310_temp_fix_seq) / sizeof(dps310_temp_fix_seq[0]))

Definition at line 74 of file dps310_i2c.c.

Function Documentation

◆ compensate_sensor()

static void compensate_sensor ( struct Dps310_I2c *  dps)
static

Convert raw measurements into pressure [Pa] and temperature [deg C].

Parameters
dpsThe dps310 instance

Implements the compensation formulas of the datasheet (section 4.9) using the factory calibration coefficients. The scale factors kT/kP belong to the configured 16x oversampling; keep them in sync with the PRC settings written in DPS310_STATUS_CONFIGURE_REGS.

Definition at line 153 of file dps310_i2c.c.

References dps310_reg_calib_data::c0, dps310_reg_calib_data::c00, dps310_reg_calib_data::c01, dps310_reg_calib_data::c1, dps310_reg_calib_data::c10, dps310_reg_calib_data::c11, dps310_reg_calib_data::c20, dps310_reg_calib_data::c21, dps310_reg_calib_data::c30, DPS310_SCALE_FACTOR_KP, DPS310_SCALE_FACTOR_KT, and foo.

Referenced by dps310_i2c_event().

+ Here is the caller graph for this function:

◆ dps310_i2c_event()

void dps310_i2c_event ( struct Dps310_I2c *  dps)

Consume finished I2C transactions and advance the state machine.

Parameters
dpsThe dps310 instance

Should be called from the event loop. On success it parses the responses and publishes plausibility-checked measurements through data_available. On failure it counts consecutive errors, escalating from a simple retry to a full re-initialization (DPS310_MAX_ERROR_CNT) and, while uninitialized, to a timed backoff.

Definition at line 290 of file dps310_i2c.c.

References compensate_sensor(), DPS310_CHIP_ID, DPS310_COEF_SRCE_BIT_TMP_COEF_SRCE, DPS310_MAX_ERROR_CNT, DPS310_MEAS_CFG_COEF_RDY, DPS310_MEAS_CFG_SENSOR_RDY, DPS310_PRESSURE_MAX_PA, DPS310_PRESSURE_MIN_PA, DPS310_STATUS_CONFIGURE_MEAS, DPS310_STATUS_CONFIGURE_REGS, DPS310_STATUS_GET_CALIB, DPS310_STATUS_GET_COEF_SRCE, DPS310_STATUS_GET_ID, DPS310_STATUS_READ_DATA, DPS310_STATUS_TEMP_FIX, DPS310_STATUS_UNINIT, DPS310_STATUS_WAIT_RDY, DPS310_TEMP_FIX_SEQ_LEN, foo, get_sys_time_usec(), I2CTransDone, I2CTransFailed, I2CTransSuccess, parse_calib_data(), and parse_sensor_data().

Referenced by baro_dps310_event().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dps310_i2c_init()

void dps310_i2c_init ( struct Dps310_I2c *  dps,
struct i2c_periph *  i2c_p,
uint8_t  addr 
)

Initialize the driver instance (no bus traffic yet)

Parameters
dpsThe dps310 instance
i2c_pI2C peripheral the sensor is connected to
addr8-bit I2C slave address (DPS310_I2C_ADDR or DPS310_I2C_ADDR_ALT)

Sensor detection and configuration happen asynchronously afterwards through dps310_i2c_periodic() and dps310_i2c_event().

Definition at line 174 of file dps310_i2c.c.

References DPS310_STATUS_UNINIT, foo, I2CTransDone, and i2c_periph::status.

Referenced by baro_dps310_init().

+ Here is the caller graph for this function:

◆ dps310_i2c_periodic()

void dps310_i2c_periodic ( struct Dps310_I2c *  dps)

Run the driver state machine, submitting at most one I2C transaction.

Parameters
dpsThe dps310 instance

Should be called periodically. The call rate is decoupled from the sensor-internal 16 Hz measurement rate: polling faster only re-reads the latest sample. Does nothing while a transaction is still in flight. A persistently failing or absent sensor is retried after a DPS310_BROKEN_RETRY_US backoff instead of spamming the bus.

Definition at line 199 of file dps310_i2c.c.

References DPS310_BROKEN_RETRY_US, DPS310_CFG_REG_P_SHIFT, DPS310_CFG_REG_T_SHIFT, DPS310_MEAS_CTRL_CONT, DPS310_PRS_CFG_PM_PRC_16, DPS310_PRS_CFG_PM_RATE_16HZ, DPS310_REG_COEF, DPS310_REG_COEF_SRCE, DPS310_REG_ID, DPS310_REG_MEAS_CFG, DPS310_REG_PRS_CFG, DPS310_REG_PSR_B2, DPS310_STATUS_CONFIGURE_MEAS, DPS310_STATUS_CONFIGURE_REGS, DPS310_STATUS_GET_CALIB, DPS310_STATUS_GET_COEF_SRCE, DPS310_STATUS_GET_ID, DPS310_STATUS_READ_DATA, DPS310_STATUS_TEMP_FIX, DPS310_STATUS_UNINIT, DPS310_STATUS_WAIT_RDY, dps310_temp_fix_seq, DPS310_TMP_CFG_TMP_PRC_16, DPS310_TMP_CFG_TMP_RATE_16HZ, foo, get_sys_time_usec(), i2c_transceive(), i2c_transmit(), and I2CTransDone.

Referenced by baro_dps310_periodic().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getTwosComplement()

static int32_t getTwosComplement ( uint32_t  raw,
uint8_t  length 
)
static

Sign-extend a raw two's complement value of arbitrary bit length.

Parameters
rawUnsigned register value holding the two's complement number
lengthNumber of significant bits (e.g. 12, 16, 20, 24)
Returns
Sign-extended signed value

Definition at line 82 of file dps310_i2c.c.

Referenced by parse_calib_data(), and parse_sensor_data().

+ Here is the caller graph for this function:

◆ parse_calib_data()

static void parse_calib_data ( struct Dps310_I2c *  dps)
static

Unpack the 18 calibration coefficient bytes (registers 0x10-0x21)

Parameters
dpsThe dps310 instance, expects the coefficient block in the transaction buffer

The coefficients are packed as mixed 12/16/20 bit two's complement values, see the COEF register description in the datasheet.

Definition at line 97 of file dps310_i2c.c.

References foo, and getTwosComplement().

Referenced by dps310_i2c_event().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_sensor_data()

static void parse_sensor_data ( struct Dps310_I2c *  dps)
static

Extract the raw 24-bit pressure and temperature measurements.

Parameters
dpsThe dps310 instance, expects the 6 result bytes (registers 0x00-0x05, burst-read in one transaction) in the transaction buffer

Definition at line 124 of file dps310_i2c.c.

References foo, and getTwosComplement().

Referenced by dps310_i2c_event().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ dps310_temp_fix_seq

const uint8_t dps310_temp_fix_seq[][2]
static
Initial value:
= {
{ 0x0E, 0xA5 },
{ 0x0F, 0x96 },
{ 0x62, 0x02 },
{ 0x0E, 0x00 },
{ 0x0F, 0x00 }
}

DPS310 temperature errata fix sequence.

Some DPS310 silicon revisions report temperature roughly 60 degC too high, which also corrupts the temperature-compensated pressure. Writing this specific sequence of undocumented register writes fixes the issue. The register addresses are non-contiguous, so each {register, value} pair must be written in its own I2C transaction.

Definition at line 66 of file dps310_i2c.c.

Referenced by dps310_i2c_periodic().