MIT-licensed portable driver for Infineon DPS310 pressure and temperature sensor
The driver provides abstraction to register access in driver. You need to include dps310.h in your application, the header has functions for interacting with the sensor.
Driver requires the user application to provide functions for reading and writing data on the bus as well as sleeping. The functions are provided through function pointers.
/**
* @brief Sleep at least this many milliseconds.
*
* Function is allowed to sleep for longer, but not shorter time.
*/
typedef void (*dps310_sleep) (const uint32_t ms);
/**
* @brief Read data from bus.
*
* If the read function returns non-zero value, it will get returned to caller in application.
* Comm_ctx is given by application at dps310 initialization. Data is guaranteed to be non-NULL
* and data_len is guaranteed to be non-zero.
*
* @param[in] comm_ctx A pointer to context for platform. E.g. I2C address or SPI SS pin. May be NULL.
* @param[in] reg_addr Address of register to read.
* @param[out] data Pointer to which data will be written. Must be data_len bytes long.
* @param[in] data_len Length of data.
* @retval 0 on success.
* @return application error code on error. Bit DPS310_BUS_ERROR should be 0.
*/
typedef uint32_t (*dps310_read) (const void * const comm_ctx, const uint8_t reg_addr,
uint8_t * const data, const uint8_t data_len);
/**
* @brief Write data from bus.
*
* If the write function returns non-zero value, it will get returned to caller in application.
* Comm_ctx is given by application at dps310 initialization. Data is guaranteed to be non-NULL
* and data_len is guaranteed to be non-zero.
*
* @param[in] comm_ctx A pointer to context for platform. E.g. I2C address or SPI SS pin.
* @param[in] reg_addr Address of register to write.
* @param[in] data Pointer to which data will be written. Must be data_len bytes long.
* @param[in] data_len Length of data.
* @return 0 on success.
* @return application error code on error. Bit DPS310_BUS_ERROR should be 0.
*/
typedef uint32_t (*dps310_write) (const void * const comm_ctx, const uint8_t reg_addr,
const uint8_t * const data, const uint8_t data_len);
int main(void)
{
static platform_gpio_t spi_ss_pin = DPS310_SS;
dps310_ctx_t dps =
{
.sleep = &platform_sleep_function,
.read = &platform_read_function,
.write = &platform_write_function,
.comm_ctx = &platform_gpio_t // assuming SPI transfer needs only SS GPIO.
};
platform_spi_init();
int32_t dps_error = dps310_init(&dps);
// One high-precision temperature and pressure sample per second.
if(DPS_SUCCESS == dps_error)
{
dps_error = dps310_config_temp(&dps, DPS310_MR_1, DPS310_OS_64);
}
if(DPS_SUCCESS == dps_error)
{
dps_error = dps310_config_pres(&dps, DPS310_MR_1, DPS310_OS_64);
}
if(DPS_SUCCESS == dps_error)
{
dps_error = dps310_measure_continuous_async(&dsp310);
}
// As long as no error is detected, print sensor values
while(DPS310_SUCCESS == dps_error)
{
platform_sleep_function(1000); // Sleep for 1000 ms
float temp[2];
float pres[2];
uint8_t num_measurements = 2;
// Read up to 2 measurements from DPS FIFO
dps_error = dps310_get_cont_results(&dps, temp, pres, &num_measurements);
// Print results from FIFO
for(uint8_t ii = 0; ii < num_measurements; ii++)
{
// Temperature resolution of 0.01 C, pressure resolution 0.02 Pa
printf("Temperature: %.2f Celcius. Pressure: %.1f Pascals", temp[ii], pres[ii]);
}
}
dps310_uninit(&dps); // Try to uninit sensor after error.
return dps_error;
}
- Single temperature and pressure measurement with and without oversampling.
- Synchronous (get result on return) and asynchronous (measure now, get result later) operation.
- Continuous temperature and pressure measurement
- [] FIFO, with independent measurement rates
- [] Interrupts
Unit tests are implemented with Ceedling. Run the tests with
ceedling test:all
Ceedling can also generate Gcov reports with ceedling gcov:all utils:gcov
.
The report can be foun under build/artifacts/gcov.
Code shall be formatted with Artistic Style using .astylerc in project root as the ruleset.
astyle --project=.astylerc --recursive "./src/*.c" "./src/*.h" "./test/*.c"
Run make
to run PVS Studio on project. Report can be found at doxygen/pvs/fullhtml/index.html.
Here is a link to documentation through Doxygen-generated documentation.
Run make
to generate Doxygen docs with git version information.
The project is built on Travis and build status is reflected on a badge on top of the header. SonarCloud generates additional analytics and badges for the project.
GitHub actions run Artistic Style and PVS Studio as well as publishes the Doxygen to GitHub Pages. PVS Studio HTML-report can be found under documentation.
If you wish to use this project as a template for your own project, checkout tag v0.1.0 and change the project name and file names and setup the continuous integration pipeline for the project.