Data Logger BLE Cellular Board
Loading...
Searching...
No Matches
bme68x.c File Reference

Driver implementation for the BME68X sensor. This file contains the core logic for initializing, configuring, and reading data from the Bosch BME68X gas, temperature, humidity, and pressure sensor. More...

#include "bme68x.h"
#include "common.h"
#include "jfet_common.h"
#include <stdio.h>

Go to the source code of this file.

Functions

static int8_t get_calib_data (struct bme68x_dev *dev)
 Read calibration coefficients from the BME68x sensor registers.
 
static int8_t read_variant_id (struct bme68x_dev *dev)
 Read the sensor variant ID from the device register and store it in the device structure.
 
static uint8_t calc_gas_wait (uint16_t dur)
 convert duration in milliseconds to encoded register value
 
static int16_t calc_temperature (uint32_t temp_adc, struct bme68x_dev *dev)
 This internal API is used to calculate the temperature value.
 
static uint32_t calc_pressure (uint32_t pres_adc, const struct bme68x_dev *dev)
 This internal API is used to calculate the pressure value.
 
static uint32_t calc_humidity (uint16_t hum_adc, const struct bme68x_dev *dev)
 Calculate relative humidity (in percent, float) from raw ADC value.
 
static uint32_t calc_gas_resistance_high (uint16_t gas_res_adc, uint8_t gas_range)
 This internal API is used to calculate the gas resistance value in float.
 
static uint32_t calc_gas_resistance_low (uint16_t gas_res_adc, uint8_t gas_range, const struct bme68x_dev *dev)
 This internal API is used to calculate the gas resistance low value in float.
 
static uint8_t calc_res_heat (uint16_t temp, const struct bme68x_dev *dev)
 
static int8_t read_field_data (uint8_t index, struct bme68x_data *data, struct bme68x_dev *dev)
 
static int8_t read_all_field_data (struct bme68x_data *const data[], struct bme68x_dev *dev)
 
static int8_t set_mem_page (uint8_t reg_addr, struct bme68x_dev *dev)
 
static int8_t get_mem_page (struct bme68x_dev *dev)
 
static int8_t null_ptr_check (const struct bme68x_dev *dev)
 
static int8_t set_conf (const struct bme68x_heatr_conf *conf, uint8_t op_mode, uint8_t *nb_conv, struct bme68x_dev *dev)
 
static int8_t boundary_check (uint8_t *value, uint8_t max, struct bme68x_dev *dev)
 
static uint8_t calc_heatr_dur_shared (uint16_t dur)
 
static void swap_fields (uint8_t index1, uint8_t index2, struct bme68x_data *field[])
 Swap two entries in the sensor data pointer array.
 
static void sort_sensor_data (uint8_t low_index, uint8_t high_index, struct bme68x_data *field[])
 
static int8_t analyze_sensor_data (const struct bme68x_data *data, uint8_t n_meas)
 Function to analyze the sensor data.
 
int8_t bme68x_init (struct bme68x_dev *dev)
 Initializes the BME68X sensor.
 
int8_t bme68x_set_regs (const uint8_t *reg_addr, const uint8_t *reg_data, uint32_t len, struct bme68x_dev *dev)
 This API writes the given data to the register address of the sensor.
 
int8_t bme68x_get_regs (uint8_t reg_addr, uint8_t *reg_data, uint32_t len, struct bme68x_dev *dev)
 This API reads the data from the given register address of sensor.
 
int8_t bme68x_soft_reset (struct bme68x_dev *dev)
 Triggers a software reset on the BME68X sensor.
 
int8_t bme68x_set_conf (struct bme68x_conf *conf, struct bme68x_dev *dev)
 Sets the oversampling, filter and ODR configurations of the sensor.
 
int8_t bme68x_get_conf (struct bme68x_conf *conf, struct bme68x_dev *dev)
 
int8_t bme68x_set_op_mode (const uint8_t op_mode, struct bme68x_dev *dev)
 
int8_t bme68x_get_op_mode (uint8_t *op_mode, struct bme68x_dev *dev)
 
uint32_t bme68x_get_meas_dur (const uint8_t op_mode, struct bme68x_conf *conf, struct bme68x_dev *dev)
 
int8_t bme68x_get_data (uint8_t op_mode, struct bme68x_data *data, uint8_t *n_data, struct bme68x_dev *dev)
 
int8_t bme68x_set_heatr_conf (uint8_t op_mode, const struct bme68x_heatr_conf *conf, struct bme68x_dev *dev)
 
int8_t bme68x_get_heatr_conf (const struct bme68x_heatr_conf *conf, struct bme68x_dev *dev)
 This API is used to get the gas configuration of the sensor.
 
int8_t bme68x_selftest_check (const struct bme68x_dev *dev)
 
int initBME688 (void)
 Initializes the BME688 sensor over SPI. Performs interface initialization, device reset, and basic data verification.
 
int getBME688Data (int count, uint8_t quiet)
 Triggers and retrieves measurement data from the BME688.
 

Detailed Description

Driver implementation for the BME68X sensor. This file contains the core logic for initializing, configuring, and reading data from the Bosch BME68X gas, temperature, humidity, and pressure sensor.

Date
2023-02-07
Version
v4.4.8
Note
SPDX-License-Identifier: BSD-3-Clause
and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file bme68x.c.

Function Documentation

◆ analyze_sensor_data()

static int8_t analyze_sensor_data ( const struct bme68x_data * data,
uint8_t n_meas )
static

Function to analyze the sensor data.

Parameters
[in]dataArray of measurement data
[in]n_measNumber of measurements
Returns
Result of API execution status
Return values
0-> Success
<0 -> Fail

Definition at line 1695 of file bme68x.c.

Here is the caller graph for this function:

◆ bme68x_get_conf()

int8_t bme68x_get_conf ( struct bme68x_conf * conf,
struct bme68x_dev * dev )

Definition at line 468 of file bme68x.c.

Here is the call graph for this function:

◆ bme68x_get_data()

int8_t bme68x_get_data ( uint8_t op_mode,
struct bme68x_data * data,
uint8_t * n_data,
struct bme68x_dev * dev )

Definition at line 591 of file bme68x.c.

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

◆ bme68x_get_heatr_conf()

int8_t bme68x_get_heatr_conf ( const struct bme68x_heatr_conf * conf,
struct bme68x_dev * dev )

This API is used to get the gas configuration of the sensor.

Definition at line 703 of file bme68x.c.

Here is the call graph for this function:

◆ bme68x_get_meas_dur()

uint32_t bme68x_get_meas_dur ( const uint8_t op_mode,
struct bme68x_conf * conf,
struct bme68x_dev * dev )

Definition at line 547 of file bme68x.c.

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

◆ bme68x_get_op_mode()

int8_t bme68x_get_op_mode ( uint8_t * op_mode,
struct bme68x_dev * dev )

Definition at line 528 of file bme68x.c.

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

◆ bme68x_get_regs()

int8_t bme68x_get_regs ( uint8_t reg_addr,
uint8_t * reg_data,
uint32_t len,
struct bme68x_dev * dev )

This API reads the data from the given register address of sensor.

Parameters
reg_addrStarting register address to read from.
reg_dataPointer to the buffer to store the read data.
lenNumber of bytes to read.
devPointer to the device structure.
Returns
0 on success, or a negative error code.

Definition at line 318 of file bme68x.c.

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

◆ bme68x_init()

int8_t bme68x_init ( struct bme68x_dev * dev)

Initializes the BME68X sensor.

This function performs a soft reset, verifies the chip ID, reads the variant ID, and retrieves calibration data from the sensor.

Parameters
devPointer to the device structure.
Returns
0 on success, or a negative error code.

Definition at line 215 of file bme68x.c.

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

◆ bme68x_selftest_check()

int8_t bme68x_selftest_check ( const struct bme68x_dev * dev)

Definition at line 735 of file bme68x.c.

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

◆ bme68x_set_conf()

int8_t bme68x_set_conf ( struct bme68x_conf * conf,
struct bme68x_dev * dev )

Sets the oversampling, filter and ODR configurations of the sensor.

Parameters
confPointer to the configuration structure.
devPointer to the device structure.
Returns
0 on success, or a negative error code.

Definition at line 398 of file bme68x.c.

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

◆ bme68x_set_heatr_conf()

int8_t bme68x_set_heatr_conf ( uint8_t op_mode,
const struct bme68x_heatr_conf * conf,
struct bme68x_dev * dev )

Definition at line 659 of file bme68x.c.

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

◆ bme68x_set_op_mode()

int8_t bme68x_set_op_mode ( const uint8_t op_mode,
struct bme68x_dev * dev )

Definition at line 496 of file bme68x.c.

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

◆ bme68x_set_regs()

int8_t bme68x_set_regs ( const uint8_t * reg_addr,
const uint8_t * reg_data,
uint32_t len,
struct bme68x_dev * dev )

This API writes the given data to the register address of the sensor.

Parameters
reg_addrArray of register addresses to be written.
reg_dataArray of data bytes to be written.
lenNumber of registers to write.
devPointer to the device structure.
Returns
0 on success, or a negative error code.

Definition at line 253 of file bme68x.c.

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

◆ bme68x_soft_reset()

int8_t bme68x_soft_reset ( struct bme68x_dev * dev)

Triggers a software reset on the BME68X sensor.

Parameters
devPointer to the device structure.
Returns
0 on success, or a negative error code.

Definition at line 353 of file bme68x.c.

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

◆ boundary_check()

static int8_t boundary_check ( uint8_t * value,
uint8_t max,
struct bme68x_dev * dev )
static

Definition at line 1429 of file bme68x.c.

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

◆ calc_gas_resistance_high()

static uint32_t calc_gas_resistance_high ( uint16_t gas_res_adc,
uint8_t gas_range )
static

This internal API is used to calculate the gas resistance value in float.

Parameters
gas_res_adcRaw ADC value for gas resistance
gas_rangeGas range index used for compensation
Returns
Calculated gas resistance in ohms

Definition at line 986 of file bme68x.c.

Here is the caller graph for this function:

◆ calc_gas_resistance_low()

static uint32_t calc_gas_resistance_low ( uint16_t gas_res_adc,
uint8_t gas_range,
const struct bme68x_dev * dev )
static

This internal API is used to calculate the gas resistance low value in float.

Parameters
gas_res_adcRaw ADC value for gas resistance
gas_rangeGas range index used for compensation
devPointer to device structure containing calibration data
Returns
Calculated gas resistance in ohms

Definition at line 953 of file bme68x.c.

Here is the caller graph for this function:

◆ calc_gas_wait()

static uint8_t calc_gas_wait ( uint16_t dur)
static

convert duration in milliseconds to encoded register value

Parameters
durduration in milliseconds

The sensor expects gas wait time encoded in a specific format where the lower 6 bits store a base value and the upper 2 bits act as a multiplier (factor) representing powers of 4. Durations >= 0xFC0 are saturated to 0xFF (maximum encoded value).

Returns
Encoded gas wait time suitable for the sensor register

Definition at line 1220 of file bme68x.c.

Here is the caller graph for this function:

◆ calc_heatr_dur_shared()

static uint8_t calc_heatr_dur_shared ( uint16_t dur)
static

Definition at line 1538 of file bme68x.c.

Here is the caller graph for this function:

◆ calc_humidity()

static uint32_t calc_humidity ( uint16_t hum_adc,
const struct bme68x_dev * dev )
static

Calculate relative humidity (in percent, float) from raw ADC value.

Parameters
hum_adcraw humidity ADC value
devpointer to device structure containing calibration data
Returns
relative humidity in percent (0.0 - 100.0)

Definition at line 908 of file bme68x.c.

Here is the caller graph for this function:

◆ calc_pressure()

static uint32_t calc_pressure ( uint32_t pres_adc,
const struct bme68x_dev * dev )
static

This internal API is used to calculate the pressure value.

Parameters
pres_adcraw ADC pressure reading
devpointer to device context containing calibration data
Returns
: compensated pressure value in Pascals

Definition at line 860 of file bme68x.c.

Here is the caller graph for this function:

◆ calc_res_heat()

static uint8_t calc_res_heat ( uint16_t temp,
const struct bme68x_dev * dev )
static

Definition at line 1002 of file bme68x.c.

Here is the caller graph for this function:

◆ calc_temperature()

static int16_t calc_temperature ( uint32_t temp_adc,
struct bme68x_dev * dev )
static

This internal API is used to calculate the temperature value.

Parameters
temp_adcraw ADC temperature reading
devpointer to device context containing calibration data
Returns
: compensated temperature in degrees Celsius

Definition at line 833 of file bme68x.c.

Here is the caller graph for this function:

◆ get_calib_data()

static int8_t get_calib_data ( struct bme68x_dev * dev)
static

Read calibration coefficients from the BME68x sensor registers.

The calibration data is stored across multiple coefficient blocks in the device register map. This function reads the blocks sequentially, stores the raw bytes into coeff_array, and converts them into the signed/unsigned calibration parameters required for temperature, pressure, humidity, and gas compensation calculations.

Parameters
devPointer to the BME68X device structure containing the interface and calibration storage.
Returns
BME68X_OK on success, otherwise an error code from the BME68X API.

Definition at line 1747 of file bme68x.c.

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

◆ get_mem_page()

static int8_t get_mem_page ( struct bme68x_dev * dev)
static

Definition at line 1410 of file bme68x.c.

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

◆ getBME688Data()

int getBME688Data ( int count,
uint8_t quiet )

Triggers and retrieves measurement data from the BME688.

Parameters
countNumber of samples to collect.
quietIf TRUE, suppresses debug print output.
Returns
0 on success, or a negative error code.

Definition at line 1916 of file bme68x.c.

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

◆ initBME688()

int initBME688 ( void )

Initializes the BME688 sensor over SPI. Performs interface initialization, device reset, and basic data verification.

Returns
0 on success, or a negative error code.

Definition at line 1831 of file bme68x.c.

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

◆ null_ptr_check()

static int8_t null_ptr_check ( const struct bme68x_dev * dev)
static

Definition at line 1448 of file bme68x.c.

Here is the caller graph for this function:

◆ read_all_field_data()

static int8_t read_all_field_data ( struct bme68x_data *const data[],
struct bme68x_dev * dev )
static

Definition at line 1311 of file bme68x.c.

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

◆ read_field_data()

static int8_t read_field_data ( uint8_t index,
struct bme68x_data * data,
struct bme68x_dev * dev )
static

Definition at line 1239 of file bme68x.c.

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

◆ read_variant_id()

static int8_t read_variant_id ( struct bme68x_dev * dev)
static

Read the sensor variant ID from the device register and store it in the device structure.

Parameters
[in,out]dev: Pointer to the device structure.
Returns
BME68X_OK on success, otherwise a negative error code.

Definition at line 1812 of file bme68x.c.

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

◆ set_conf()

static int8_t set_conf ( const struct bme68x_heatr_conf * conf,
uint8_t op_mode,
uint8_t * nb_conv,
struct bme68x_dev * dev )
static

Definition at line 1460 of file bme68x.c.

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

◆ set_mem_page()

static int8_t set_mem_page ( uint8_t reg_addr,
struct bme68x_dev * dev )
static

Definition at line 1374 of file bme68x.c.

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

◆ sort_sensor_data()

static void sort_sensor_data ( uint8_t low_index,
uint8_t high_index,
struct bme68x_data * field[] )
static

Definition at line 1559 of file bme68x.c.

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

◆ swap_fields()

static void swap_fields ( uint8_t index1,
uint8_t index2,
struct bme68x_data * field[] )
static

Swap two entries in the sensor data pointer array.

This helper is used to reorder bme68x_data pointers when the measured fields need to be sorted by timestamp or measurement order.

Parameters
[in]index1Index of the first field pointer.
[in]index2Index of the second field pointer.
[in,out]fieldArray of pointers to bme68x_data objects.

Definition at line 1686 of file bme68x.c.

Here is the caller graph for this function: