BMP390 pressure sensor driver and SPP producer implementation. More...
#include "spp/services/bmp390/bmp390.h"#include "spp/hal/spi/spi.h"#include "spp/hal/time/time.h"#include "spp/services/log/log.h"#include "spp/services/databank/databank.h"#include "spp/core/packet.h"#include "spp/core/pubsub/pubsub.h"#include "spp/services/fsm/fsm.h"#include <stdio.h>#include <string.h>#include <math.h>Go to the source code of this file.
Macros | |
| #define | K_BMP390_TASK_TIMEOUT_MS 5000U |
| #define | K_BMP390_TAG "BMP390" |
| #define | K_BMP390_PERFORMANCE_SAMPLES (10000U) |
| #define | K_BMP390_CALIBRATION_SAMPLES 500U |
Functions | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_init (void) |
| Initializes the BMP390 sensor and its hardware interfaces. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_acquireData (SPP_Kpid_t kpid) |
| Acquires BMP390 measurements and publishes them through PUBSUB. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_softReset (void *p_spiHandler) |
| Sends a soft-reset command to the sensor and waits 100 ms for it to reboot. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_enableSpiMode (void *p_spiHandler) |
| Writes the interface-config register to lock the sensor into SPI mode. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_configCheck (void *p_spiHandler) |
| Reads the chip-ID register and returns an error if the value is not 0x60. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_auxConfig (void *p_spiHandler) |
| Resets the sensor, enables SPI mode, and verifies the chip ID. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_prepareMeasure (void *p_spiHandler) |
| Writes OSR, ODR, IIR and power-control registers to start continuous measurements. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_readRawTempCoeffs (void *p_spiHandler, BMP390_temp_calib_t *tcalib) |
| Reads raw temperature calibration coefficients from the sensor's NVM registers. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_calibrateTempParams (void *p_spiHandler, BMP390_temp_params_t *out) |
| Reads raw temperature coefficients and scales them to floating-point parameters. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_readRawTemp (void *p_spiHandler, uint32_t *raw_temp) |
| Reads the 24-bit raw temperature ADC value from the sensor. | |
| static float | SPP_SERVICES_BMP390_compensateTemperature (spp_uint32_t raw_temp, BMP390_temp_params_t *params) |
| Applies the BMP390 double-quadratic temperature compensation formula. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_auxGetTemp (void *p_spiHandler, const BMP390_temp_params_t *temp_params, spp_uint32_t *raw_temp, float *comp_temp) |
| Reads the raw temperature ADC and returns the compensated value. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_readRawPressCoeffs (void *p_spiHandler, BMP390_press_calib_t *pcalib) |
| Reads raw pressure calibration coefficients from the sensor's NVM registers. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_calibratePressParams (void *p_spiHandler, BMP390_press_params_t *out) |
| Reads raw pressure coefficients and scales them to floating-point parameters. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_readRawPress (void *p_spiHandler, spp_uint32_t *raw_press) |
| Reads the 24-bit raw pressure ADC value from the sensor. | |
| static float | SPP_SERVICES_BMP390_compensatePressure (spp_uint32_t raw_press, float t_lin, BMP390_press_params_t *p) |
| Applies the BMP390 pressure compensation formula using the linearised temperature. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_auxGetPress (void *p_spiHandler, const BMP390_press_params_t *press_params, float t_lin, spp_uint32_t *raw_press, float *comp_press) |
| Reads raw pressure ADC and returns the compensated value in Pa. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_getAltitude (void *p_spiHandler, float *altitude_m, float *pressure_pa, float *temperature_c) |
| Reads compensated temperature and pressure, and computes relative altitude. | |
| static SPP_RetVal_t | SPP_SERVICES_BMP390_intEnableDrdy (void *p_spiHandler) |
| Enables the active-high data-ready interrupt output on the sensor. | |
| const SPP_SERVICE_ProducerContract_t * | SPP_SERVICES_BMP390_getProducerContract (void) |
| Returns the BMP390 producer contract for PUBSUB registration. | |
Variables | |
| static const SPP_SERVICE_ProducerContract_t | g_bmp390ProducerContract |
| static BMP390_t | s_bmpData |
| static const char *const | k_svcTag = "BMP_SVC" |
| static float | s_pd1 |
| static float | s_pd2 |
| static float | s_pd3 |
| static float | s_pd4 |
| static float | s_po1 |
| static float | s_po2 |
| static float | s_compPress |
| spp_bool_t | s_isCalibrated = false |
| spp_uint16_t | s_calibrationSamples = 0 |
| spp_float32_t | s_pressureSum = 0.0f |
| spp_float32_t | s_temperatureSum = 0.0f |
| spp_float32_t | s_referencePressure = 0.0f |
| spp_float32_t | s_referenceTemperature = 0.0f |
BMP390 pressure sensor driver and SPP producer implementation.
Handles sensor configuration, SPI acquisition, compensation, reference calibration and packet publication through PUBSUB.
Definition in file bmp390.c.
| #define K_BMP390_TAG "BMP390" |
Definition at line 32 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_acquireData(), and SPP_SERVICES_BMP390_configCheck().
| #define K_BMP390_CALIBRATION_SAMPLES 500U |
Definition at line 87 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_acquireData().
|
static |
Initializes the BMP390 sensor and its hardware interfaces.
Configures the GPIO interrupt, initializes the SPI device, configures the sensor and enables data-ready interrupts.
Definition at line 139 of file bmp390.c.
References BMP390_GpioConfig_t::drdyFlag, BMP390_t::gpioConfig, BMP390_GpioConfig_t::intIntrType, BMP390_GpioConfig_t::intPin, BMP390_GpioConfig_t::intPull, BMP390_GpioConfig_t::isrCtx, K_BMP390_INT_INTR_TYPE, K_BMP390_INT_PIN_NUM, K_BMP390_INT_PULL, K_BMP390_SPI_BUS_IDX, K_SPP_ERROR, K_SPP_OK, SPP_GpioIsrCtx_t::p_flag, BMP390_SpiConfig_t::p_spiHandler, s_bmpData, BMP390_t::spiConfig, BMP390_SpiConfig_t::spiDevIdx, SPP_HAL_GPIO_configInterrupt(), SPP_HAL_GPIO_registerIsr(), SPP_HAL_SPI_deviceInit(), SPP_HAL_SPI_getHandle(), SPP_SERVICES_BMP390_auxConfig(), SPP_SERVICES_BMP390_init(), SPP_SERVICES_BMP390_intEnableDrdy(), and SPP_SERVICES_BMP390_prepareMeasure().
Referenced by SPP_SERVICES_BMP390_init().
|
static |
Acquires BMP390 measurements and publishes them through PUBSUB.
Checks the DRDY flag, reads compensated measurements and performs reference calibration using the first 500 samples. After calibration, publishes altitude, pressure and temperature.
| kpid | Assigned producer KPID. |
Definition at line 202 of file bmp390.c.
References BMP390_GpioConfig_t::drdyFlag, BMP390_t::gpioConfig, K_BMP390_CALIBRATION_SAMPLES, K_BMP390_TAG, K_SPP_ERROR, K_SPP_OK, k_svcTag, BMP390_SpiConfig_t::p_spiHandler, s_bmpData, s_calibrationSamples, s_isCalibrated, s_pressureSum, s_referencePressure, s_referenceTemperature, s_temperatureSum, BMP390_t::seq, BMP390_t::spiConfig, SPP_LOGE, SPP_LOGI, SPP_LOGW, SPP_Packet_t, SPP_SERVICES_BMP390_acquireData(), SPP_SERVICES_BMP390_getAltitude(), SPP_SERVICES_DATABANK_getPacket(), SPP_SERVICES_DATABANK_packetData(), SPP_SERVICES_DATABANK_returnPacket(), SPP_SERVICES_PUBSUB_publish(), and SPP_Kpid_t::value.
Referenced by SPP_SERVICES_BMP390_acquireData().
|
static |
Sends a soft-reset command to the sensor and waits 100 ms for it to reboot.
| p_spiHandler | SPI device handle. |
Definition at line 307 of file bmp390.c.
References BMP390_SOFT_RESET_CMD, K_BMP390_SOFT_RESET_REG, SPP_HAL_SPI_transmit(), SPP_HAL_TIME_delayMs(), and SPP_SERVICES_BMP390_softReset().
Referenced by SPP_SERVICES_BMP390_auxConfig(), and SPP_SERVICES_BMP390_softReset().
|
static |
Writes the interface-config register to lock the sensor into SPI mode.
| p_spiHandler | SPI device handle. |
Definition at line 320 of file bmp390.c.
References BMP390_IF_CONF_SPI, K_BMP390_IF_CONF_REG, SPP_HAL_SPI_transmit(), SPP_HAL_TIME_delayMs(), and SPP_SERVICES_BMP390_enableSpiMode().
Referenced by SPP_SERVICES_BMP390_auxConfig(), and SPP_SERVICES_BMP390_enableSpiMode().
|
static |
Reads the chip-ID register and returns an error if the value is not 0x60.
| p_spiHandler | SPI device handle. |
Definition at line 333 of file bmp390.c.
References K_BMP390_CHIP_ID_REG, K_BMP390_IF_CONF_REG, K_BMP390_SOFT_RESET_REG, K_BMP390_SPI_READ, K_BMP390_SPI_WRITE, K_BMP390_TAG, K_SPP_ERROR, K_SPP_OK, SPP_HAL_SPI_transmit(), SPP_LOGE, SPP_LOGI, and SPP_SERVICES_BMP390_configCheck().
Referenced by SPP_SERVICES_BMP390_auxConfig(), and SPP_SERVICES_BMP390_configCheck().
|
static |
Resets the sensor, enables SPI mode, and verifies the chip ID.
| p_spiHandler | SPI device handle. |
Definition at line 363 of file bmp390.c.
References K_SPP_OK, SPP_SERVICES_BMP390_auxConfig(), SPP_SERVICES_BMP390_configCheck(), SPP_SERVICES_BMP390_enableSpiMode(), and SPP_SERVICES_BMP390_softReset().
Referenced by SPP_SERVICES_BMP390_auxConfig(), and SPP_SERVICES_BMP390_init().
|
static |
Writes OSR, ODR, IIR and power-control registers to start continuous measurements.
| p_spiHandler | SPI device handle. |
Definition at line 393 of file bmp390.c.
References BMP390_VALUE_IIR, BMP390_VALUE_ODR, BMP390_VALUE_OSR, BMP390_VALUE_PWRCTRL, K_BMP390_REG_IIR, K_BMP390_REG_ODR, K_BMP390_REG_OSR, K_BMP390_REG_PWRCTRL, SPP_HAL_SPI_transmit(), and SPP_SERVICES_BMP390_prepareMeasure().
Referenced by SPP_SERVICES_BMP390_init(), and SPP_SERVICES_BMP390_prepareMeasure().
|
static |
Reads raw temperature calibration coefficients from the sensor's NVM registers.
| p_spiHandler | SPI device handle. |
| tcalib | Output struct populated with raw par_t1/t2/t3 values. |
Definition at line 413 of file bmp390.c.
References K_BMP390_SPI_READ, K_BMP390_SPI_WRITE, K_BMP390_TEMP_CALIB_REG_START, K_SPP_OK, BMP390_temp_calib_t::par_t1, BMP390_temp_calib_t::par_t2, BMP390_temp_calib_t::par_t3, SPP_HAL_SPI_transmit(), and SPP_SERVICES_BMP390_readRawTempCoeffs().
Referenced by SPP_SERVICES_BMP390_calibrateTempParams(), and SPP_SERVICES_BMP390_readRawTempCoeffs().
|
static |
Reads raw temperature coefficients and scales them to floating-point parameters.
| p_spiHandler | SPI device handle. |
| out | Output struct populated with scaled PAR_T1/T2/T3. |
Definition at line 448 of file bmp390.c.
References K_SPP_OK, BMP390_temp_params_t::PAR_T1, BMP390_temp_calib_t::par_t1, BMP390_temp_params_t::PAR_T2, BMP390_temp_calib_t::par_t2, BMP390_temp_params_t::PAR_T3, BMP390_temp_calib_t::par_t3, SPP_SERVICES_BMP390_calibrateTempParams(), and SPP_SERVICES_BMP390_readRawTempCoeffs().
Referenced by SPP_SERVICES_BMP390_calibrateTempParams(), and SPP_SERVICES_BMP390_getAltitude().
|
static |
Reads the 24-bit raw temperature ADC value from the sensor.
| p_spiHandler | SPI device handle. |
| raw_temp | Output: raw 24-bit ADC reading. |
Definition at line 471 of file bmp390.c.
References K_BMP390_SPI_READ, K_BMP390_SPI_WRITE, K_BMP390_TEMP_RAW_REG, K_SPP_OK, SPP_HAL_SPI_transmit(), and SPP_SERVICES_BMP390_readRawTemp().
Referenced by SPP_SERVICES_BMP390_auxGetTemp(), and SPP_SERVICES_BMP390_readRawTemp().
|
static |
Applies the BMP390 double-quadratic temperature compensation formula.
| raw_temp | Raw 24-bit ADC reading from the sensor. |
| params | Scaled calibration parameters (PAR_T1/T2/T3). |
Definition at line 504 of file bmp390.c.
References BMP390_temp_params_t::PAR_T1, BMP390_temp_params_t::PAR_T2, BMP390_temp_params_t::PAR_T3, and SPP_SERVICES_BMP390_compensateTemperature().
Referenced by SPP_SERVICES_BMP390_auxGetTemp(), and SPP_SERVICES_BMP390_compensateTemperature().
|
static |
Reads the raw temperature ADC and returns the compensated value.
| p_spiHandler | SPI device handle. |
| temp_params | Scaled calibration parameters. |
| raw_temp | Output: raw ADC reading. |
| comp_temp | Output: compensated temperature in °C. |
Definition at line 521 of file bmp390.c.
References K_SPP_OK, SPP_SERVICES_BMP390_auxGetTemp(), SPP_SERVICES_BMP390_compensateTemperature(), and SPP_SERVICES_BMP390_readRawTemp().
Referenced by SPP_SERVICES_BMP390_auxGetTemp(), and SPP_SERVICES_BMP390_getAltitude().
|
static |
Reads raw pressure calibration coefficients from the sensor's NVM registers.
| p_spiHandler | SPI device handle. |
| pcalib | Output struct populated with raw par_p1..p11 values. |
Definition at line 545 of file bmp390.c.
References K_BMP390_PRESS_CALIB_REG_START, K_BMP390_SPI_READ, K_BMP390_SPI_WRITE, K_SPP_OK, BMP390_press_calib_t::par_p1, BMP390_press_calib_t::par_p10, BMP390_press_calib_t::par_p11, BMP390_press_calib_t::par_p2, BMP390_press_calib_t::par_p3, BMP390_press_calib_t::par_p4, BMP390_press_calib_t::par_p5, BMP390_press_calib_t::par_p6, BMP390_press_calib_t::par_p7, BMP390_press_calib_t::par_p8, BMP390_press_calib_t::par_p9, SPP_HAL_SPI_transmit(), and SPP_SERVICES_BMP390_readRawPressCoeffs().
Referenced by SPP_SERVICES_BMP390_calibratePressParams(), and SPP_SERVICES_BMP390_readRawPressCoeffs().
|
static |
Reads raw pressure coefficients and scales them to floating-point parameters.
| p_spiHandler | SPI device handle. |
| out | Output struct populated with scaled PAR_P1..P11. |
Definition at line 629 of file bmp390.c.
References K_SPP_OK, BMP390_press_params_t::PAR_P1, BMP390_press_calib_t::par_p1, BMP390_press_params_t::PAR_P10, BMP390_press_calib_t::par_p10, BMP390_press_params_t::PAR_P11, BMP390_press_calib_t::par_p11, BMP390_press_params_t::PAR_P2, BMP390_press_calib_t::par_p2, BMP390_press_params_t::PAR_P3, BMP390_press_calib_t::par_p3, BMP390_press_params_t::PAR_P4, BMP390_press_calib_t::par_p4, BMP390_press_params_t::PAR_P5, BMP390_press_calib_t::par_p5, BMP390_press_params_t::PAR_P6, BMP390_press_calib_t::par_p6, BMP390_press_params_t::PAR_P7, BMP390_press_calib_t::par_p7, BMP390_press_params_t::PAR_P8, BMP390_press_calib_t::par_p8, BMP390_press_params_t::PAR_P9, BMP390_press_calib_t::par_p9, SPP_SERVICES_BMP390_calibratePressParams(), and SPP_SERVICES_BMP390_readRawPressCoeffs().
Referenced by SPP_SERVICES_BMP390_calibratePressParams(), and SPP_SERVICES_BMP390_getAltitude().
|
static |
Reads the 24-bit raw pressure ADC value from the sensor.
| p_spiHandler | SPI device handle. |
| raw_press | Output: raw 24-bit ADC reading. |
Definition at line 660 of file bmp390.c.
References K_BMP390_PRESS_RAW_REG, K_BMP390_SPI_READ, K_BMP390_SPI_WRITE, K_SPP_OK, SPP_HAL_SPI_transmit(), and SPP_SERVICES_BMP390_readRawPress().
Referenced by SPP_SERVICES_BMP390_auxGetPress(), and SPP_SERVICES_BMP390_readRawPress().
|
static |
Applies the BMP390 pressure compensation formula using the linearised temperature.
| raw_press | Raw 24-bit ADC pressure reading. |
| t_lin | Linearised (compensated) temperature in °C. |
| p | Scaled pressure calibration parameters. |
Definition at line 694 of file bmp390.c.
References BMP390_press_params_t::PAR_P1, BMP390_press_params_t::PAR_P10, BMP390_press_params_t::PAR_P11, BMP390_press_params_t::PAR_P2, BMP390_press_params_t::PAR_P3, BMP390_press_params_t::PAR_P4, BMP390_press_params_t::PAR_P5, BMP390_press_params_t::PAR_P6, BMP390_press_params_t::PAR_P7, BMP390_press_params_t::PAR_P8, BMP390_press_params_t::PAR_P9, s_compPress, s_pd1, s_pd2, s_pd3, s_pd4, s_po1, s_po2, and SPP_SERVICES_BMP390_compensatePressure().
Referenced by SPP_SERVICES_BMP390_auxGetPress(), and SPP_SERVICES_BMP390_compensatePressure().
|
static |
Reads raw pressure ADC and returns the compensated value in Pa.
| p_spiHandler | SPI device handle. |
| press_params | Scaled pressure calibration parameters. |
| t_lin | Linearised temperature used for pressure compensation. |
| raw_press | Output: raw 24-bit ADC reading. |
| comp_press | Output: compensated pressure in Pa. |
Definition at line 725 of file bmp390.c.
References K_SPP_OK, SPP_SERVICES_BMP390_auxGetPress(), SPP_SERVICES_BMP390_compensatePressure(), and SPP_SERVICES_BMP390_readRawPress().
Referenced by SPP_SERVICES_BMP390_auxGetPress(), and SPP_SERVICES_BMP390_getAltitude().
|
static |
Reads compensated temperature and pressure, and computes relative altitude.
| p_spiHandler | SPI device handle. |
| altitude_m | Output: altitude relative to launch-site reference pressure and temperature. |
| pressure_pa | Output: compensated pressure in Pa. |
| temperature_c | Output: compensated temperature in °C. |
Definition at line 752 of file bmp390.c.
References K_SPP_ERROR_NULL_POINTER, K_SPP_OK, s_isCalibrated, s_referencePressure, s_referenceTemperature, SPP_SERVICES_BMP390_auxGetPress(), SPP_SERVICES_BMP390_auxGetTemp(), SPP_SERVICES_BMP390_calibratePressParams(), SPP_SERVICES_BMP390_calibrateTempParams(), and SPP_SERVICES_BMP390_getAltitude().
Referenced by SPP_SERVICES_BMP390_acquireData(), and SPP_SERVICES_BMP390_getAltitude().
|
static |
Enables the active-high data-ready interrupt output on the sensor.
| p_spiHandler | SPI device handle. |
Definition at line 824 of file bmp390.c.
References K_BMP390_INT_CTRL_DRDY_EN, K_BMP390_INT_CTRL_LEVEL, K_BMP390_REG_INT_CTRL, SPP_HAL_SPI_transmit(), and SPP_SERVICES_BMP390_intEnableDrdy().
Referenced by SPP_SERVICES_BMP390_init(), and SPP_SERVICES_BMP390_intEnableDrdy().
| const SPP_SERVICE_ProducerContract_t * SPP_SERVICES_BMP390_getProducerContract | ( | void | ) |
Returns the BMP390 producer contract for PUBSUB registration.
Definition at line 101 of file bmp390.c.
References g_bmp390ProducerContract, and SPP_SERVICES_BMP390_getProducerContract().
Referenced by CUSTOM_registerConsumerProducer(), guard_startInitializationOfModules(), and SPP_SERVICES_BMP390_getProducerContract().
|
static |
Definition at line 76 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_getProducerContract().
|
static |
Definition at line 80 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_acquireData(), and SPP_SERVICES_BMP390_init().
|
static |
Definition at line 81 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_acquireData().
|
static |
Definition at line 82 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_compensatePressure().
|
static |
Definition at line 82 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_compensatePressure().
|
static |
Definition at line 82 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_compensatePressure().
|
static |
Definition at line 82 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_compensatePressure().
|
static |
Definition at line 83 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_compensatePressure().
|
static |
Definition at line 83 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_compensatePressure().
|
static |
Definition at line 84 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_compensatePressure().
| spp_bool_t s_isCalibrated = false |
Definition at line 88 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_acquireData(), and SPP_SERVICES_BMP390_getAltitude().
| spp_uint16_t s_calibrationSamples = 0 |
Definition at line 89 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_acquireData().
| spp_float32_t s_pressureSum = 0.0f |
Definition at line 91 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_acquireData().
| spp_float32_t s_temperatureSum = 0.0f |
Definition at line 92 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_acquireData().
| spp_float32_t s_referencePressure = 0.0f |
Definition at line 94 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_acquireData(), and SPP_SERVICES_BMP390_getAltitude().
| spp_float32_t s_referenceTemperature = 0.0f |
Definition at line 95 of file bmp390.c.
Referenced by SPP_SERVICES_BMP390_acquireData(), and SPP_SERVICES_BMP390_getAltitude().