Driver for Bosch Sensortec BME680 added
BME680 is an ulta-low-power environmental sensor that integrates temperature, pressure, humidity and gas sensors in only one unit.
This commit is contained in:
parent
546cc47121
commit
382022f507
10 changed files with 2658 additions and 0 deletions
42
examples/bme680/README.md
Normal file
42
examples/bme680/README.md
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
# BME680 Driver Examples
|
||||
|
||||
These examples references two addtional drivers **i2c** and **bme680**, which are provided in the `../../extras` folder.
|
||||
|
||||
If you plan to use one or both of this drivers in your own projects, please check the main development pages for updated versions or reported issues.
|
||||
|
||||
## Hardware setup
|
||||
|
||||
There are examples that are using either I2C or SPI with one or two sensors.
|
||||
|
||||
For examples using BME680 sensor as I2C slave, just use GPIO5 (SCL) and GPIO4 (SDA) to connect to the BME680 sensor's I2C interface.
|
||||
|
||||
```
|
||||
+-------------------------+ +--------+
|
||||
| ESP8266 Bus 0 | | BME680 |
|
||||
| GPIO 5 (SCL) +---->+ SCL |
|
||||
| GPIO 4 (SDA) +-----+ SDA |
|
||||
| | +--------+
|
||||
+-------------------------+
|
||||
```
|
||||
|
||||
For examples that are using SPI, BME680 sensor has to be connected to SPI bus 1. Since GPIO15 used as default CS signal of SPI bus 1 does not work correctly together with BME680, you have to connect CS to another GPIO pin, e.g., GPIO2.
|
||||
|
||||
+-------------------------+ +----------+
|
||||
| ESP8266 Bus 1 | | BME680 |
|
||||
| GPIO 12 (MISO) <-----< SDO |
|
||||
| GPIO 13 (MOSI) >-----> SDI |
|
||||
| GPIO 14 (SCK) >-----> SCK |
|
||||
| GPIO 2 (CS) >-----> CS |
|
||||
+-------------------------+ +----------+
|
||||
|
||||
The example with two sensors use the combination of I2C and SPI.
|
||||
|
||||
## Example description
|
||||
|
||||
__*bme680_one_sensor*__
|
||||
|
||||
In this simple example, only **one sensor** connected either to **I2C** or to **SPI** is used. Constant **SPI_USED** defines which interface is used.
|
||||
|
||||
__*bme680_two_sensors*__
|
||||
|
||||
Simple example with two sensors, one sensor connected to **I2C** bus 0 and one sensor connected to **SPI**. It defines two different user tasks that use the sensors as well as different approaches for the implementation of waiting for measurement results, one as busy waiting using **_bme680_is_measuring_** and one as passive waiting using *vTaskDelay*.
|
||||
3
examples/bme680/bme680_one_sensor/Makefile
Normal file
3
examples/bme680/bme680_one_sensor/Makefile
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
PROGRAM=BME680_One_Sensor
|
||||
EXTRA_COMPONENTS = extras/i2c extras/bme680
|
||||
include ../../../common.mk
|
||||
126
examples/bme680/bme680_one_sensor/bme680_one_sensor.c
Normal file
126
examples/bme680/bme680_one_sensor/bme680_one_sensor.c
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
/**
|
||||
* Simple example with one sensor connected either to I2C bus 0 or
|
||||
* SPI bus 1.
|
||||
*
|
||||
* Harware configuration:
|
||||
*
|
||||
* I2C +-------------------------+ +----------+
|
||||
* | ESP8266 Bus 0 | | BME680 |
|
||||
* | GPIO 5 (SCL) ------> SCL |
|
||||
* | GPIO 4 (SDA) ------- SDA |
|
||||
* +-------------------------+ +----------+
|
||||
*
|
||||
* SPI +-------------------------+ +----------+
|
||||
* | ESP8266 Bus 1 | | BME680 |
|
||||
* | GPIO 12 (MISO) <-----< SDO |
|
||||
* | GPIO 13 (MOSI) >-----> SDI |
|
||||
* | GPIO 14 (SCK) >-----> SCK |
|
||||
* | GPIO 2 (CS) >-----> CS |
|
||||
* +-------------------------+ +----------+
|
||||
*/
|
||||
|
||||
// Uncomment to use SPI
|
||||
// #define SPI_USED
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
|
||||
// include communication interface driver
|
||||
#include "esp/spi.h"
|
||||
#include "i2c/i2c.h"
|
||||
|
||||
// include BME680 driver
|
||||
#include "bme680/bme680.h"
|
||||
|
||||
#ifdef SPI_USED
|
||||
// define SPI interface for BME680 sensors
|
||||
#define SPI_BUS 1
|
||||
#define SPI_CS_GPIO 2 // GPIO 15, the default CS of SPI bus 1, can't be used
|
||||
#else
|
||||
// define I2C interface for BME680 sensors
|
||||
#define I2C_BUS 0
|
||||
#define I2C_SCL_PIN GPIO_ID_PIN((5))
|
||||
#define I2C_SDA_PIN GPIO_ID_PIN((4))
|
||||
#endif
|
||||
|
||||
static bme680_sensor_t* sensor;
|
||||
|
||||
/*
|
||||
* User task that triggers measurements of sensor every seconds. It uses
|
||||
* function *vTaskDelay* to wait for measurement results. Busy wating
|
||||
* alternative is shown in comments
|
||||
*/
|
||||
void user_task(void *pvParameters)
|
||||
{
|
||||
bme680_values_float_t values;
|
||||
int32_t duration;
|
||||
|
||||
TickType_t last_wakeup = xTaskGetTickCount();
|
||||
|
||||
while (1)
|
||||
{
|
||||
// trigger the sensor to start one TPHG measurement cycle
|
||||
duration = bme680_force_measurement (sensor);
|
||||
|
||||
// passive waiting until measurement results are available
|
||||
if (duration > 0) vTaskDelay (duration);
|
||||
|
||||
// busy waiting until measurement results are available
|
||||
// while (bme680_is_measuring (sensor) > 0) ;
|
||||
|
||||
// get the results and do something with them
|
||||
if (bme680_get_results_float (sensor, &values))
|
||||
printf("%.3f BME680 Sensor: %.2f °C, %.2f %%, %.2f hPa, %.2f Ohm\n",
|
||||
(double)sdk_system_get_time()*1e-3,
|
||||
values.temperature, values.humidity,
|
||||
values.pressure, values.gas_resistance);
|
||||
|
||||
// passive waiting until 1 second is over
|
||||
vTaskDelayUntil(&last_wakeup, 1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
// Set UART Parameter
|
||||
uart_set_baud(0, 115200);
|
||||
// Give the UART some time to settle
|
||||
sdk_os_delay_us(500);
|
||||
|
||||
/** -- MANDATORY PART -- */
|
||||
|
||||
|
||||
#ifdef SPI_USED
|
||||
// Init the sensor connected either to SPI.
|
||||
sensor = bme680_init_sensor (SPI_BUS, 0, SPI_CS_GPIO);
|
||||
#else
|
||||
// Init all I2C bus interfaces at which BME680 sensors are connected
|
||||
i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_100K);
|
||||
|
||||
// Init the sensor connected either to I2C.
|
||||
sensor = bme680_init_sensor (I2C_BUS, BME680_I2C_ADDRESS_2, 0);
|
||||
#endif
|
||||
|
||||
if (sensor)
|
||||
{
|
||||
// Create a task that uses the sensor
|
||||
xTaskCreate(user_task, "user_task", 256, NULL, 2, NULL);
|
||||
|
||||
/** -- OPTIONAL PART -- */
|
||||
|
||||
// Changes the oversampling rates (default os_1x) to 4x oversampling
|
||||
// for temperature and 2x oversampling for humidity. Pressure
|
||||
// measurement is skipped in this example.
|
||||
bme680_set_oversampling_rates(sensor, osr_4x, osr_none, osr_2x);
|
||||
|
||||
// Change the IIR filter size (default iir_size_3) for temperature and
|
||||
// and pressure to 7.
|
||||
bme680_set_filter_size(sensor, iir_size_7);
|
||||
|
||||
// Change the heaeter profile (default 320 degree Celcius for 150 ms)
|
||||
// to 200 degree Celcius for 100 ms.
|
||||
bme680_set_heater_profile (sensor, 320, 150);
|
||||
}
|
||||
}
|
||||
|
||||
3
examples/bme680/bme680_two_sensors/Makefile
Normal file
3
examples/bme680/bme680_two_sensors/Makefile
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
PROGRAM=BME680_Tow_Sensors
|
||||
EXTRA_COMPONENTS = extras/i2c extras/bme680
|
||||
include ../../../common.mk
|
||||
146
examples/bme680/bme680_two_sensors/bme680_two_sensors.c
Normal file
146
examples/bme680/bme680_two_sensors/bme680_two_sensors.c
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
/**
|
||||
* Simple example with two sensors, one sensor connected to I2C bus 0 and
|
||||
* one sensor connected to SPI. It also shows both approaches for the
|
||||
* implementation of waiting for measurement results, one as busy waiting
|
||||
* and one as passive waiting using *vTaskDelay*.
|
||||
*
|
||||
* Harware configuration:
|
||||
*
|
||||
* +-------------------------+ +----------+
|
||||
* | ESP8266 I2C Bus 0 | | BME680_1 |
|
||||
* | GPIO 5 (SCL) ------> SCL |
|
||||
* | GPIO 4 (SDA) ------- SDA |
|
||||
* | | +----------+
|
||||
* | SPI Bus 1 | | BME680_2 |
|
||||
* | GPIO 12 (MISO) <------ SDO |
|
||||
* | GPIO 13 (MOSI) >-----> SDI |
|
||||
* | GPIO 14 (SCK) >-----> SCK |
|
||||
* | GPIO 2 (CS) >-----> CS |
|
||||
* +-------------------------+ +----------+
|
||||
*/
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
|
||||
// include communication interface driver
|
||||
#include "esp/spi.h"
|
||||
#include "i2c/i2c.h"
|
||||
|
||||
// include BME680 driver
|
||||
#include "bme680/bme680.h"
|
||||
|
||||
// define I2C interface for BME680 sensor 1
|
||||
#define SPI_BUS 1
|
||||
#define SPI_CS_GPIO 2 // GPIO 15, the default CS of SPI bus 1, can't be used
|
||||
// define SPI interface for BME680 sensor 2
|
||||
#define I2C_BUS 0
|
||||
#define I2C_SCL_PIN GPIO_ID_PIN((5))
|
||||
#define I2C_SDA_PIN GPIO_ID_PIN((4))
|
||||
|
||||
static bme680_sensor_t* sensor1;
|
||||
static bme680_sensor_t* sensor2;
|
||||
|
||||
/*
|
||||
* User task that triggers measurements of sensor1 every 5 seconds. It
|
||||
* uses *vTaskDelay* to wait for measurement results.
|
||||
*/
|
||||
void user_task_sensor1(void *pvParameters)
|
||||
{
|
||||
bme680_values_float_t values;
|
||||
int32_t duration;
|
||||
|
||||
TickType_t last_wakeup = xTaskGetTickCount();
|
||||
|
||||
while (1)
|
||||
{
|
||||
// trigger the sensor to start one TPHG measurement cycle
|
||||
duration = bme680_force_measurement (sensor1);
|
||||
|
||||
// passive waiting until measurement results are available
|
||||
if (duration > 0) vTaskDelay (duration);
|
||||
|
||||
// get the results and so something with them
|
||||
if (bme680_get_results_float (sensor1, &values))
|
||||
printf("%.3f BME680 Sensor1: %.2f °C, %.2f %%, %.2f hPa, %.2f Ohm\n",
|
||||
(double)sdk_system_get_time()*1e-3,
|
||||
values.temperature, values.humidity,
|
||||
values.pressure, values.gas_resistance);
|
||||
|
||||
// passive waiting until 5 seconds are over
|
||||
vTaskDelayUntil(&last_wakeup, 5000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* User task that triggers measurements of sensor1 every 2 seconds. It
|
||||
* uses *vTaskDelay* to wait for measurement results.
|
||||
*/
|
||||
void user_task_sensor2(void *pvParameters)
|
||||
{
|
||||
bme680_values_float_t values;
|
||||
|
||||
TickType_t last_wakeup = xTaskGetTickCount();
|
||||
|
||||
while (1)
|
||||
{
|
||||
// trigger the sensor to start one TPHG measurement cycle
|
||||
bme680_force_measurement (sensor2);
|
||||
|
||||
// busy waiting until measurement results are available
|
||||
while (bme680_is_measuring (sensor2) > 0) ;
|
||||
|
||||
// get the results and so something with them
|
||||
if (bme680_get_results_float (sensor2, &values))
|
||||
printf("%.3f BME680 Sensor2: %.2f °C, %.2f %%, %.2f hPa, %.2f Ohm\n",
|
||||
(double)sdk_system_get_time()*1e-3,
|
||||
values.temperature, values.humidity,
|
||||
values.pressure, values.gas_resistance);
|
||||
|
||||
// passive waiting until 2 seconds are over
|
||||
vTaskDelayUntil(&last_wakeup, 2000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
// Set UART Parameter
|
||||
uart_set_baud(0, 115200);
|
||||
// Give the UART some time to settle
|
||||
sdk_os_delay_us(500);
|
||||
|
||||
/** -- MANDATORY PART -- */
|
||||
|
||||
// Init all I2C bus interfaces at which BME680 sensors are connected
|
||||
i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_100K);
|
||||
|
||||
// Init the sensors connected to different I2C buses with same address
|
||||
sensor1 = bme680_init_sensor (I2C_BUS, BME680_I2C_ADDRESS_2, 0);
|
||||
sensor2 = bme680_init_sensor (SPI_BUS, 0, SPI_CS_GPIO);
|
||||
|
||||
if (sensor1 && sensor2)
|
||||
{
|
||||
// Create the tasks that use the sensors
|
||||
xTaskCreate(user_task_sensor1, "user_task_sensor1", 256, NULL, 2, 0);
|
||||
xTaskCreate(user_task_sensor2, "user_task_sensor2", 256, NULL, 2, 0);
|
||||
|
||||
// That's it.
|
||||
|
||||
/** -- OPTIONAL PART -- */
|
||||
|
||||
// Changes the oversampling rates for both sensor to different values
|
||||
bme680_set_oversampling_rates(sensor1, osr_1x, osr_1x, osr_1x);
|
||||
bme680_set_oversampling_rates(sensor2, osr_16x, osr_16x, osr_16x);
|
||||
|
||||
// Change the IIR filter size (default iir_size_3) for temperature and
|
||||
// and pressure to 7.
|
||||
bme680_set_filter_size(sensor1, iir_size_7);
|
||||
bme680_set_filter_size(sensor2, iir_size_7);
|
||||
|
||||
// Change the heaeter profile (default 20 degree Celcius for 150 ms) to
|
||||
// 200 degree Celcius for 100 ms.
|
||||
bme680_set_heater_profile (sensor1, 200, 100);
|
||||
bme680_set_heater_profile (sensor2, 200, 100);
|
||||
}
|
||||
}
|
||||
|
||||
405
extras/bme680/README.md
Normal file
405
extras/bme680/README.md
Normal file
|
|
@ -0,0 +1,405 @@
|
|||
# Driver for **BME680** digital **environmental sensor**
|
||||
|
||||
This driver is written for usage with the ESP8266 and FreeRTOS. It supports multiple sensors that are either connected to the SPI or to different I2C interfaces with different addresses.
|
||||
|
||||
## About the sensor
|
||||
|
||||
BME680 is an ulta-low-power environmental sensor that integrates temperature, pressure, humidity and gas sensors in only one unit.
|
||||
|
||||
## Communication interfaces
|
||||
|
||||
The BME680 sensor can be connected using I2C or SPI. The driver supports both communication interfaces.
|
||||
|
||||
The I2C interface supports data rates up to 3.4 Mbps. It is possible to connect multiple BME680 sensors with different I2C slave addresses to the same I2C bus or to different I2C buses. Possible I2C slave addresses are 0x76 and 0x77.
|
||||
|
||||
The SPI interface allows clock rates up to 10 MHz and the SPI modes '00' (CPOL=CPHA=0) and '11' (CPOL=CPHA=1).
|
||||
|
||||
Interface selection is done automatically using the SPI CS signal. As long as the CS signal keeps high after power-on reset, the I2C interface is used. Once the CS signal has been pulled down, SPI interface is used until next power-on reset.
|
||||
|
||||
## Measurement process
|
||||
|
||||
Once the BME680 has been initialized, it can be used for measurements. The BME680 operates in two different low-level power modes, the **sleep mode** and the **forced mode**.
|
||||
|
||||
The *sleep mode* is the default mode. The sensor starts automatically in this mode after power-up sequence. It does not perform any measurement in this mode and therefore just consumes 0.15 μA.
|
||||
|
||||
#### Measurement cylce
|
||||
|
||||
To perform measurements, the BME680 sensor has to be triggered to switch to the **forced mode**. In this mode, it performs exactly one measurement of temperature, pressure, humidity, and gas in that order, the so-called **TPHG measurement cycle**. After the execution of this TPHG measurement cylce, the **raw sensor data** become available and the sensor returns automatically back to sleep mode.
|
||||
|
||||
Each of the individual measurements can be configured or skipped separately via the sensor settings, see section **Measurement settings**. Dependent on the configuration, the **duration of a TPHG measurement cycle** can vary from some milliseconds up to about 4.5 seconds, escpecially if gas measurement is enabled.
|
||||
|
||||
Therefore, the measurement process is separated into the following steps:
|
||||
|
||||
1. Trigger the sensor with function **_bme680_force_measurement_** to switch to *forced mode* in which it performs exactly one THPG measurement cycle.
|
||||
|
||||
2. Wait the measurement duration using either passive waiting with function **_vTaskDelay_** or busy waiting with function **_bme680_is_measuring_** until the results are available.
|
||||
|
||||
3. Fetch the results as fixed point values with function **_bme680_get_results_fixed_** or as floating point values with function **_bme680_get_results_float_**.
|
||||
|
||||
For convenience reasons, it is also possible to use either function **_bme680_measure_float_** or function **_bme680_measure_fixed_**, which combine all 3 steps above within a single function. **Please note**, these functions must not be used when the are called from a software timer callback function since the calling task is delayed using function *vTaskDelay*.
|
||||
|
||||
#### Measurement results
|
||||
|
||||
Once the sensor has finished the measurement, either function **_bme680_get_results_fixed_** or function **_bme680_get_results_float_** can be used to fetch the results. These functions read the raw data from the sensor and converts them into fixed point or floating point sensor values. This conversion process bases in very complex calculations using a large number of calibration parameters.
|
||||
|
||||
Dependent on sensor value representation, measurement results contain different dimensions:
|
||||
|
||||
| Value | Fixed Point | Floating Point | Conversion
|
||||
| ----- | ----------- | -------------- |-----------
|
||||
| temperature | 1/100 °C | °C | float = fixed / 100
|
||||
| pressure | Pascal | hPascal | float = fixed / 100
|
||||
| humdidity | 1/1000 % | % | float = fixed / 1000
|
||||
| gas_resistance | Ohm | Ohm | float = fixed
|
||||
|
||||
The gas resistance value in Ohm represents the resistance of sensor's gas sensitive layer.
|
||||
|
||||
## Measurement settings
|
||||
|
||||
The sensor allows to change different measurement parameters.
|
||||
|
||||
#### Oversampling rates
|
||||
|
||||
Using function **_bme680_set_oversampling_rates_**, it is possible to define individual oversampling rates for the measurements of temperature, pressure, and humidity. Possible oversampling rates are 1x (default by the driver) 2x, 4x, 8x and 16x. Using an oversampling rate *osr*, the resolution of raw sensor data can be increased from 16 bit to 16+ld(*osr*) bit.
|
||||
|
||||
It is also possible to define an oversampling rate of 0. This **deactivates** the corresponding measurement and the output values become invalid.
|
||||
|
||||
#### IIR Filter
|
||||
|
||||
The sensor integrates an internal IIR filter (low pass filter) to reduce short-term changes in sensor output values caused by external disturbances. It effectively reduces the bandwidth of the sensor output values.
|
||||
|
||||
The filter can optionally be used for pressure and temperature data that are subject to many short-term changes. Using the IIR filter, increases the resolution of pressure and temperature data to 20 bit. Humidity and gas inside the sensor does not fluctuate rapidly and does not require such a low pass filtering.
|
||||
|
||||
Using function **_bme680_set_filter_size_**, the user task can change the **size of the filter**. The default size is 3. If the size of the filter becomes 0, the filter is **not used**.
|
||||
|
||||
#### Heater profile
|
||||
|
||||
For gas measurement the sensor integrates a heater. The paremeters for this heater are defined by a heater profile. Such a heater profile consists of a temperature setting point (the target temperature) and the heating duration.
|
||||
|
||||
Even though the sensor supports up to 10 different such profiles at them same time, only one profile is used by the driver for simplicity reasons. The **temperature setting point** and the **heating duration** of this one profile can be defined by the user task using function **_bme680_set_heater_profile_**. Default values used by the driver are 320 degree Celsius as target temperature and 150 ms heating duration.
|
||||
|
||||
According to the datasheet, target temperatures between 200 and 400 degrees Celsius are typical and about 20 to 30 ms are necessary for the heater to reach the desired target temperature.
|
||||
|
||||
If heating duration is 0 ms, the gas measurement **is skipped** and output values become invalid.
|
||||
|
||||
## Error Handling
|
||||
|
||||
Most driver functions return a simple boolean value to indicate whether its execution was successful or an error happened. In the latter case, the member **_error_code_** of the sensor device data structure is set which indicates what error happened.
|
||||
|
||||
There are two different error levels that are ORed into one single *error_code*, errors in the I2C or SPI communication and errors with the BME680 sensor itself. To test for a certain error, you can AND the *error_code* with one of the error masks, **_BME680_INT_ERROR_MASK_** for I2C or SPI errors and **_BME680_DRV_ERROR_MASK_** for other errors and then test for a certain error code.
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
First, the hardware configuration has to be establisch. This can differ dependent on the communication interface used and the number of sensors.
|
||||
|
||||
### Hardware configurations
|
||||
|
||||
The driver supports multiple BME680 sensors at the same time that are connected either to I2C or SPI. Following figures show some possible hardware configurations.
|
||||
|
||||
First figure shows the configuration with only one sensor at I2C bus 0.
|
||||
|
||||
```
|
||||
+-------------------------+ +--------+
|
||||
| ESP8266 Bus 0 | | BME680 |
|
||||
| GPIO 5 (SCL) +---->+ SCL |
|
||||
| GPIO 4 (SDA) +-----+ SDA |
|
||||
| | +--------+
|
||||
+-------------------------+
|
||||
```
|
||||
Next figure shows a possible configuration with two I2C buses. In that case, the sensors can have same or different I2C slave adresses.
|
||||
|
||||
```
|
||||
+-------------------------+ +----------+
|
||||
| ESP8266 Bus 0 | | BME680_1 |
|
||||
| GPIO 5 (SCL) ------> SCL |
|
||||
| GPIO 4 (SDA) ------- SDA |
|
||||
| | +----------+
|
||||
| Bus 1 | | BME680_2 |
|
||||
| GPIO 14 (SCL) ------> SCL |
|
||||
| GPIO 12 (SDA) ------- SDA |
|
||||
+-------------------------+ +----------+
|
||||
```
|
||||
|
||||
Last figure shows a possible configuration using I2C bus 0 and SPI bus 1 at the same time.
|
||||
```
|
||||
+-------------------------+ +----------+
|
||||
| ESP8266 Bus 0 | | BME680_1 |
|
||||
| GPIO 5 (SCL) ------> SCL |
|
||||
| GPIO 4 (SDA) ------- SDA |
|
||||
| | +----------+
|
||||
| Bus 1 | | BME680_2 |
|
||||
| GPIO 12 (MISO) <------ SDO |
|
||||
| GPIO 13 (MOSI) >-----> SDI |
|
||||
| GPIO 14 (SCK) >-----> SCK |
|
||||
| GPIO 2 (CS) >-----> CS |
|
||||
+-------------------------+ +----------+
|
||||
```
|
||||
**Please note:**
|
||||
|
||||
1. Since the system flash memory is connected to SPI bus 0, the sensor has to be connected to SPI bus 1.
|
||||
|
||||
2. GPIO15 used as CS signal of SPI bus 1 by default, does not work correctly together with BME680. Therefore, the user has to specify another GPIO pin as CS signal when the sensor is initialized, e.g., GPIO2.
|
||||
|
||||
Further configurations are possible, e.g., two sensors that are connected at the same I2C bus with different slave addresses.
|
||||
|
||||
### Communication interface settings
|
||||
|
||||
Dependent on the hardware configuration, the communication interface settings have to be defined.
|
||||
|
||||
```
|
||||
// define SPI interface for BME680 sensors
|
||||
#define SPI_BUS 1
|
||||
#define SPI_CS_GPIO 2 // GPIO 15, the default CS of SPI bus 1, can't be used
|
||||
#else
|
||||
|
||||
// define I2C interface for BME680 sensors
|
||||
#define I2C_BUS 0
|
||||
#define I2C_SCL_PIN GPIO_ID_PIN((5))
|
||||
#define I2C_SDA_PIN GPIO_ID_PIN((4))
|
||||
#endif
|
||||
|
||||
```
|
||||
|
||||
### Main programm
|
||||
|
||||
If I2C interfaces are used, they have to be initialized first.
|
||||
|
||||
```
|
||||
i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_100K))
|
||||
```
|
||||
|
||||
Once I2C interfaces are initialized, function **_bme680_init_sensor_** has to be called for each BME680 sensor to initialize the sensor and to check its availability as well as its error state. This function returns a pointer to the sensor device data structure or NULL in case of error.
|
||||
|
||||
The parameters *bus* specifies the ID of the I2C or SPI bus to which the sensor is connected.
|
||||
|
||||
```
|
||||
static bme680_sensor_t* sensor;
|
||||
```
|
||||
|
||||
For sensors connected to an I2C interface, a valid I2C slave address has to be defined as parameter *addr*. In that case parameter *cs* is ignored.
|
||||
|
||||
```
|
||||
sensor = bme680_init_sensor (I2C_BUS, BME680_I2C_ADDRESS_2, 0);
|
||||
|
||||
```
|
||||
|
||||
If parameter *addr* is 0, the sensor is connected to a SPI bus. In that case, parameter *cs* defines the GPIO used as CS signal.
|
||||
|
||||
```
|
||||
sensor = bme680_init_sensor (SPI_BUS, 0, SPI_CS_GPIO);
|
||||
|
||||
```
|
||||
|
||||
The remaining part of the program is independent on the communication interface.
|
||||
|
||||
Optionally, you could wish to set some measurement parameters. For details see the section **Measurement settings** above, the header file of the driver **bme680.h**, and of course the datasheet of the sensor.
|
||||
|
||||
```
|
||||
if (sensor)
|
||||
{
|
||||
// Changes the oversampling rates (default os_1x) to 4x oversampling
|
||||
// for temperature and 2x oversampling for humidity. Pressure
|
||||
// measurement is skipped in this example.
|
||||
bme680_set_oversampling_rates(sensor, osr_4x, osr_none, osr_2x);
|
||||
|
||||
// Change the IIR filter size (default iir_size_3) for temperature and
|
||||
// and pressure to 7.
|
||||
bme680_set_filter_size(sensor, iir_size_7);
|
||||
|
||||
// Change the heaeter profile (default 320 degree Celcius for 150 ms)
|
||||
// to 200 degree Celcius for 100 ms.
|
||||
bme680_set_heater_profile (sensor, 320, 150);
|
||||
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Last, the user task that uses the sensor has to be created.
|
||||
|
||||
|
||||
### User task
|
||||
|
||||
BME680 supports only the *forced mode* that performs exactly one measuerement. Therefore, the measurement has to be triggered in each cycle. The waiting for measurement results is also required in each cylce, before the results can be fetched.
|
||||
|
||||
Thus the user task could look like the following:
|
||||
|
||||
|
||||
```
|
||||
void user_task(void *pvParameters)
|
||||
{
|
||||
bme680_values_float_t values;
|
||||
int32_t duration;
|
||||
|
||||
TickType_t last_wakeup = xTaskGetTickCount();
|
||||
|
||||
while (1)
|
||||
{
|
||||
// trigger the sensor to start one TPHG measurement cycle
|
||||
duration = bme680_force_measurement (sensor);
|
||||
|
||||
// passive waiting until measurement results are available
|
||||
if (duration > 0) vTaskDelay (duration);
|
||||
|
||||
// busy waiting until measurement results are available
|
||||
// while (bme680_is_measuring (sensor) > 0) ;
|
||||
|
||||
// get the results and do something with them
|
||||
if (bme680_get_results_float (sensor, &values))
|
||||
printf("%.3f BME680 Sensor: %.2f °C, %.2f %%, %.2f hPa, %.2f Ohm\n",
|
||||
(double)sdk_system_get_time()*1e-3,
|
||||
values.temperature, values.humidity,
|
||||
values.pressure, values.gas_resistance);
|
||||
|
||||
// passive waiting until 1 second is over
|
||||
vTaskDelayUntil(&last_wakeup, 1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Function **_bme680_force_measurement_** is called inside the task loop to start exactly one measurement in each cycle. It returns the estimated duration of the measurement dependent on the measurement settings. The task is then delayed by this duration using function **_vTaskDelay_** to wait passively before the results are fetched with function **_bme680_get_results_float**. The busy waiting alternative is shown in comments.
|
||||
|
||||
### Error Handling
|
||||
|
||||
The code could be extended by an error handling. In the event of an error, most driver functions set member **_error_code_** of the sensor device data structure. This indicates which error has occurred. Error codes are a combination of I2C and SPI communication error codes as well as BME680 sensor error codes. To test a particular error, the *error code* can be AND with one of the error masks **_BME680_INT_ERROR_MASK_** or **_BME680_DRV_ERROR_MASK_**.
|
||||
|
||||
For example, error handling for **_bme680_get_results_** could look like:
|
||||
```
|
||||
|
||||
if (bme680_get_results (sensor, &values))
|
||||
{
|
||||
// no error happened
|
||||
...
|
||||
}
|
||||
else
|
||||
{
|
||||
// error happened
|
||||
|
||||
switch (sensor->error_code & BME680_INT_ERROR_MASK)
|
||||
{
|
||||
case BME680_I2C_BUSY: ...
|
||||
case BME680_I2C_READ_FAILED: ...
|
||||
...
|
||||
}
|
||||
switch (sensor->error_code & BME680_DRV_ERROR_MASK)
|
||||
{
|
||||
case BME680_MEAS_NOT_RUNNING: ...
|
||||
case BME680_NO_NEW_DATA: ...
|
||||
...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Full Example
|
||||
|
||||
```
|
||||
// Uncomment to use SPI
|
||||
// #define SPI_USED
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
|
||||
// include communication interface driver
|
||||
#include "esp/spi.h"
|
||||
#include "i2c/i2c.h"
|
||||
|
||||
// include BME680 driver
|
||||
#include "bme680/bme680.h"
|
||||
|
||||
#ifdef SPI_USED
|
||||
// define SPI interface for BME680 sensors
|
||||
#define SPI_BUS 1
|
||||
#define SPI_CS_GPIO 2 // GPIO 15, the default CS of SPI bus 1, can't be used
|
||||
#else
|
||||
// define I2C interface for BME680 sensors
|
||||
#define I2C_BUS 0
|
||||
#define I2C_SCL_PIN GPIO_ID_PIN((5))
|
||||
#define I2C_SDA_PIN GPIO_ID_PIN((4))
|
||||
#endif
|
||||
|
||||
static bme680_sensor_t* sensor;
|
||||
|
||||
/*
|
||||
* User task that triggers measurements of sensor every seconds. It uses
|
||||
* function *vTaskDelay* to wait for measurement results. Busy wating
|
||||
* alternative is shown in comments
|
||||
*/
|
||||
void user_task(void *pvParameters)
|
||||
{
|
||||
bme680_values_float_t values;
|
||||
int32_t duration;
|
||||
|
||||
TickType_t last_wakeup = xTaskGetTickCount();
|
||||
|
||||
while (1)
|
||||
{
|
||||
// trigger the sensor to start one TPHG measurement cycle
|
||||
duration = bme680_force_measurement (sensor);
|
||||
|
||||
// passive waiting until measurement results are available
|
||||
if (duration > 0) vTaskDelay (duration);
|
||||
|
||||
// busy waiting until measurement results are available
|
||||
// while (bme680_is_measuring (sensor) > 0) ;
|
||||
|
||||
// get the results and do something with them
|
||||
if (bme680_get_results_float (sensor, &values))
|
||||
printf("%.3f BME680 Sensor: %.2f °C, %.2f %%, %.2f hPa, %.2f Ohm\n",
|
||||
(double)sdk_system_get_time()*1e-3,
|
||||
values.temperature, values.humidity,
|
||||
values.pressure, values.gas_resistance);
|
||||
|
||||
// passive waiting until 1 second is over
|
||||
vTaskDelayUntil(&last_wakeup, 1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
// Set UART Parameter
|
||||
uart_set_baud(0, 115200);
|
||||
// Give the UART some time to settle
|
||||
sdk_os_delay_us(500);
|
||||
|
||||
/** -- MANDATORY PART -- */
|
||||
|
||||
|
||||
#ifdef SPI_USED
|
||||
// Init the sensor connected either to SPI.
|
||||
sensor = bme680_init_sensor (SPI_BUS, 0, SPI_CS_GPIO);
|
||||
#else
|
||||
// Init all I2C bus interfaces at which BME680 sensors are connected
|
||||
i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_100K);
|
||||
|
||||
// Init the sensor connected either to I2C.
|
||||
sensor = bme680_init_sensor (I2C_BUS, BME680_I2C_ADDRESS_2, 0);
|
||||
#endif
|
||||
|
||||
if (sensor)
|
||||
{
|
||||
// Create a task that uses the sensor
|
||||
xTaskCreate(user_task, "user_task", 256, NULL, 2, NULL);
|
||||
|
||||
/** -- OPTIONAL PART -- */
|
||||
|
||||
// Changes the oversampling rates (default os_1x) to 4x oversampling
|
||||
// for temperature and 2x oversampling for humidity. Pressure
|
||||
// measurement is skipped in this example.
|
||||
bme680_set_oversampling_rates(sensor, osr_4x, osr_none, osr_2x);
|
||||
|
||||
// Change the IIR filter size (default iir_size_3) for temperature and
|
||||
// and pressure to 7.
|
||||
bme680_set_filter_size(sensor, iir_size_7);
|
||||
|
||||
// Change the heaeter profile (default 320 degree Celcius for 150 ms)
|
||||
// to 200 degree Celcius for 100 ms.
|
||||
bme680_set_heater_profile (sensor, 320, 150);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Further Examples
|
||||
|
||||
For further examples see [examples directory](../../examples/bme680/README.md)
|
||||
|
||||
|
||||
1380
extras/bme680/bme680.c
Normal file
1380
extras/bme680/bme680.c
Normal file
File diff suppressed because it is too large
Load diff
351
extras/bme680/bme680.h
Normal file
351
extras/bme680/bme680.h
Normal file
|
|
@ -0,0 +1,351 @@
|
|||
/*
|
||||
* Driver for Bosch Sensortec BME680 digital temperature, humidity, pressure and
|
||||
* gas sensor connected to I2C or SPI
|
||||
*
|
||||
* Part of esp-open-rtos [https://github.com/SuperHouse/esp-open-rtos]
|
||||
*
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* The BSD License (3-clause license)
|
||||
*
|
||||
* Copyright (c) 2017 Gunar Schorcht (https://github.com/gschorcht]
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution 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.
|
||||
*/
|
||||
|
||||
#ifndef __BME680_H__
|
||||
#define __BME680_H__
|
||||
|
||||
#include "bme680/bme680_types.h"
|
||||
|
||||
// Uncomment one of the following defines to enable debug output
|
||||
// #define BME680_DEBUG_LEVEL_1 // only error messages
|
||||
// #define BME680_DEBUG_LEVEL_2 // debug and error messages
|
||||
|
||||
// BME680 addresses
|
||||
#define BME680_I2C_ADDRESS_1 0x76 // SDO pin is low
|
||||
#define BME680_I2C_ADDRESS_2 0x77 // SDO pin is high
|
||||
|
||||
// BME680 chip id
|
||||
#define BME680_CHIP_ID 0x61 // BME680_REG_ID<7:0>
|
||||
|
||||
// Definition of error codes
|
||||
#define BME680_OK 0
|
||||
#define BME680_NOK -1
|
||||
|
||||
#define BME680_INT_ERROR_MASK 0x000f
|
||||
#define BME680_DRV_ERROR_MASK 0xfff0
|
||||
|
||||
// Error codes for I2C and SPI interfaces ORed with BME680 driver error codes
|
||||
#define BME680_I2C_READ_FAILED 1
|
||||
#define BME680_I2C_WRITE_FAILED 2
|
||||
#define BME680_I2C_BUSY 3
|
||||
#define BME680_SPI_WRITE_FAILED 4
|
||||
#define BME680_SPI_READ_FAILED 5
|
||||
#define BME680_SPI_BUFFER_OVERFLOW 6
|
||||
#define BME680_SPI_SET_PAGE_FAILED 7
|
||||
|
||||
// BME680 driver error codes ORed with error codes for I2C and SPI interfaces
|
||||
#define BME680_RESET_CMD_FAILED (1 << 8)
|
||||
#define BME680_WRONG_CHIP_ID (2 << 8)
|
||||
#define BME680_READ_CALIB_DATA_FAILED (3 << 8)
|
||||
#define BME680_MEAS_ALREADY_RUNNING (4 << 8)
|
||||
#define BME680_MEAS_NOT_RUNNING (5 << 8)
|
||||
#define BME680_FORCE_MODE_FAILED (6 << 8)
|
||||
#define BME680_NO_NEW_DATA (7 << 8)
|
||||
|
||||
// Driver range definitions
|
||||
#define BME680_HEATER_TEMP_MIN 200 // min. 200 degree Celsius
|
||||
#define BME680_HEATER_TEMP_MAX 400 // max. 200 degree Celsius
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/** --------------------------------------------------------------------------
|
||||
*
|
||||
* Functional Description of the BME680 sensor
|
||||
*
|
||||
* The BME680 sensor only support two modes, the sleep mode and the forced
|
||||
* mode in which measurements are done. After power-up sequence, the sensor
|
||||
* automatically starts in sleep mode. To start a measurement, the sensor has
|
||||
* to switch in the forced mode. In this mode it performs exactly one
|
||||
* measurement of temperature, pressure, humidity, and gas in that order,
|
||||
* the so-called TPHG measurement cycle. After the execution of this TPHG
|
||||
* measurement cycle, raw sensor data are available and the sensor returns
|
||||
* automatically back to sleep mode.
|
||||
*
|
||||
* Using the BME680 consists of the following steps
|
||||
*
|
||||
* 1. Trigger the sensor to switch into forced mode to perform one THPG cycle
|
||||
* 2. Wait until the THPG cycle has been finished (measurement duration)
|
||||
* 3. Fetch raw sensor data, compensate and convert them to sensor values
|
||||
*
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Initialize a BME680 sensor
|
||||
*
|
||||
* The function initializes the sensor device data structure, probes the
|
||||
* sensor, soft resets the sensor, and configures the sensor with default
|
||||
* settings:
|
||||
*
|
||||
* - Oversampling rate for temperature, pressure, humidity is osr_1x
|
||||
* - Filter size for pressure and temperature is iir_size 3
|
||||
* - Heater profile for gas is 320 degree Celsius for 150 milliseconds.
|
||||
*
|
||||
* The sensor can be connected either to an I2C or a SPI bus. In both cases,
|
||||
* the parameter *bus* specifies the ID of the corresponding bus. Please note
|
||||
* that in case of SPI, bus 1 has to be used since bus 0 is used for system
|
||||
* flash memory.
|
||||
*
|
||||
* If parameter *addr* is greater than 0, it defines a valid I2C slave address
|
||||
* and the sensor is connected to an I2C bus. In that case parameter *cs* is
|
||||
* ignored.
|
||||
*
|
||||
* If parameter *addr* is 0, the sensor is connected to a SPI bus. In that
|
||||
* case, parameter *cs* defines the GPIO used as CS signal
|
||||
*
|
||||
* @param bus I2C or SPI bus at which BME680 sensor is connected
|
||||
* @param addr I2C addr of the BME680 sensor, 0 for SPI
|
||||
* @param cs SPI CS GPIO, ignored for I2C
|
||||
* @return pointer to sensor data structure, or NULL on error
|
||||
*/
|
||||
bme680_sensor_t* bme680_init_sensor (uint8_t bus, uint8_t addr, uint8_t cs_pin);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Force one single TPHG measurement
|
||||
*
|
||||
* The function triggers the sensor to start one THPG measurement cycle in
|
||||
* forced mode, the only measurement mode supported by the BME680. Measurement
|
||||
* parameters for the measurement like oversampling rates, IIR filter sizes
|
||||
* and heater profile can be configured before.
|
||||
*
|
||||
* On success, the function returns an estimated measurement duration given
|
||||
* in RTOS ticks. This is the time in ticks needed by the sensor before
|
||||
* measurement results become available. The user task has to wait this
|
||||
* duration before it can use function *bme680_get_results_fixed* or
|
||||
* function *bme680_get_results_float* to fetch the measurement results.
|
||||
*
|
||||
* The measurement duration strongly depends on which measurements in the THPG
|
||||
* measurement cycle are performed and which configuration parameters were set.
|
||||
* It can vary from 1 RTOS (10 ms) tick up to 4500 RTOS ticks (4.5 seconds).
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @return measurement duration given in RTOS ticks or -1 on error
|
||||
*/
|
||||
int32_t bme680_force_measurement (bme680_sensor_t* dev);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get remaining duration of a running measurement
|
||||
*
|
||||
* The function can be used to test whether a measurement has been started
|
||||
* and how long it still takes before measurement results become
|
||||
* available. The return value is given in RTOS ticks and can be
|
||||
*
|
||||
* >0 in case the measurement is is still running,
|
||||
* 0 in case the measurement has been already finished, or
|
||||
* <0 in case of error.
|
||||
*
|
||||
* That is, a return value greater than 0 indicates that the measurement
|
||||
* results are still not available and the user task has to wait that time.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @return remaining measurement duration in RTOS ticks or -1 on error
|
||||
*/
|
||||
int32_t bme680_is_measuring (bme680_sensor_t* dev);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get results of a measurement in fixed point representation
|
||||
*
|
||||
* The function returns the results of a TPHG measurement that has been
|
||||
* started before. If the measurement is still running, the function fails
|
||||
* and returns invalid values (see type declaration).
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param results pointer to a data structure that is filled with results
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool bme680_get_results_fixed (bme680_sensor_t* dev,
|
||||
bme680_values_fixed_t* results);
|
||||
|
||||
/**
|
||||
* @brief Get results of a measurement in floating point representation
|
||||
*
|
||||
* The function returns the results of a TPHG measurement that has been
|
||||
* started before. If the measurement is still running, the function fails
|
||||
* and returns invalid values (see type declaration).
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param results pointer to a data structure that is filled with results
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool bme680_get_results_float (bme680_sensor_t* dev,
|
||||
bme680_values_float_t* results);
|
||||
|
||||
/**
|
||||
* @brief Start a measurement, wait and return the results (fixed point)
|
||||
*
|
||||
* This function is a combination of functions above. For convenience it
|
||||
* starts a TPHG measurement using *bme680_force_measurement*, then waits it
|
||||
* the measurement duration for the results using *vTaskDelay* and finally it
|
||||
* returns the results using function *bme680_get_results_fixed*.
|
||||
*
|
||||
* Note: Since the calling task is delayed using function *vTaskDelay*, this
|
||||
* function must not be used when it is called by a software timer callback
|
||||
* function.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param results pointer to a data structure that is filled with results
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool bme680_measure_fixed (bme680_sensor_t* dev,
|
||||
bme680_values_fixed_t* results);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Start a measurement, wait and return the results (floating point)
|
||||
*
|
||||
* This function is a combination of functions above. For convenience it
|
||||
* starts a TPHG measurement using *bme680_force_measurement*, then it waits
|
||||
* the measurement duration for the results using *vTaskDelay* and finally it
|
||||
* returns the results using function *bme680_get_results_float*.
|
||||
*
|
||||
* Note: Since the calling task is delayed using function *vTaskDelay*, this
|
||||
* function must not be used when it is called by a software timer callback
|
||||
* function.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param results pointer to a data structure that is filled with results
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool bme680_measure_float (bme680_sensor_t* dev,
|
||||
bme680_values_float_t* results);
|
||||
|
||||
/**
|
||||
* @brief Set the oversampling rates for measurements
|
||||
*
|
||||
* The BME680 sensor allows to define individual oversampling rates for
|
||||
* the measurements of temperature, pressure and humidity. Using an
|
||||
* oversampling rate of *osr*, the resolution of raw sensor data can be
|
||||
* increased by ld(*osr*) bits.
|
||||
*
|
||||
* Possible oversampling rates are 1x (default), 2x, 4x, 8x, 16x, see type
|
||||
* *bme680_oversampling_rate_t*. The default oversampling rate is 1.
|
||||
*
|
||||
* Please note: Use *osr_none* to skip the corresponding measurement.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param ost oversampling rate for temperature measurements
|
||||
* @param osp oversampling rate for pressure measurements
|
||||
* @param osh oversampling rate for humidity measurements
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool bme680_set_oversampling_rates (bme680_sensor_t* dev,
|
||||
bme680_oversampling_rate_t osr_t,
|
||||
bme680_oversampling_rate_t osr_p,
|
||||
bme680_oversampling_rate_t osr_h);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set the size of the IIR filter
|
||||
*
|
||||
* The sensor integrates an internal IIR filter (low pass filter) to reduce
|
||||
* short-term changes in sensor output values caused by external disturbances.
|
||||
* It effectively reduces the bandwidth of the sensor output values.
|
||||
*
|
||||
* The filter can optionally be used for pressure and temperature data that
|
||||
* are subject to many short-term changes. Using the IIR filter, increases the
|
||||
* resolution of pressure and temperature data to 20 bit. Humidity and gas
|
||||
* inside the sensor does not fluctuate rapidly and does not require such a
|
||||
* low pass filtering.
|
||||
*
|
||||
* The default filter size is 3 (*iir_size_3*).
|
||||
*
|
||||
* Please note: If the size of the filter is 0, the filter is not used.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param size IIR filter size
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool bme680_set_filter_size(bme680_sensor_t* dev, bme680_filter_size_t size);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set the heater profile for gas measurements
|
||||
*
|
||||
* For gas measurement the sensor integrates a heater. The paremeters for
|
||||
* this heater are defined by a heater profile. Such a heater profile
|
||||
* consists of a temperature setting point (the target temperature) and the
|
||||
* heating duration. The target temperature is converted to the heater
|
||||
* resistance value.
|
||||
*
|
||||
* Even though the sensor supports up to 10 different profiles, only one
|
||||
* profile is used by this driver for simplicity. The temperature setting
|
||||
* point and the heating duration of this profile can be defined by this
|
||||
* function. Default values are 320 degree Celsius as target temperature and
|
||||
* 150 ms heating duration.
|
||||
*
|
||||
* Please note: To disable the measurement of gas, set the heating duration
|
||||
* to 0 ms.
|
||||
*
|
||||
* Please note: According to the data sheet, target temperatures of between
|
||||
* 200 and 400 degrees Celsius are typical and about 20 to 30 ms are necessary
|
||||
* for the heater to reach the desired target temperature.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param temperature heating temperature in degree Celsius
|
||||
* @param duration heating duration in milliseconds
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool bme680_set_heater_profile (bme680_sensor_t* dev,
|
||||
uint16_t temperature,
|
||||
uint16_t duration);
|
||||
|
||||
/**
|
||||
* @brief Set ambient temperature
|
||||
*
|
||||
* The heater resistance conversion algorithm takes into account the ambient
|
||||
* temperature of the sensor. This function can be used to set this ambient
|
||||
* temperature. Either values determined from this or another temperature
|
||||
* sensor can be used. The default ambient temperature is 25 degree Celsius.
|
||||
*/
|
||||
bool bme680_set_ambient_temperature (bme680_sensor_t* dev,
|
||||
int16_t temperature);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* End of CPP guard */
|
||||
|
||||
#endif /* __BME680_H__ */
|
||||
193
extras/bme680/bme680_types.h
Normal file
193
extras/bme680/bme680_types.h
Normal file
|
|
@ -0,0 +1,193 @@
|
|||
/*
|
||||
* Driver for Bosch Sensortec BME680 digital temperature, humity, pressure and
|
||||
* gas sensor connected to I2C or SPI
|
||||
*
|
||||
* Part of esp-open-rtos [https://github.com/SuperHouse/esp-open-rtos]
|
||||
*
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* The BSD License (3-clause license)
|
||||
*
|
||||
* Copyright (c) 2017 Gunar Schorcht (https://github.com/gschorcht]
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution 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.
|
||||
*/
|
||||
|
||||
#ifndef __BME680_TYPES_H__
|
||||
#define __BME680_TYPES_H__
|
||||
|
||||
#include "stdint.h"
|
||||
#include "stdbool.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Fixed point sensor values (fixed THPG values)
|
||||
*/
|
||||
typedef struct { // invalid value
|
||||
int16_t temperature; // temperature in degree C * 100 (INT16_MIN)
|
||||
uint32_t pressure; // barometric pressure in Pascal (0)
|
||||
uint32_t humidity; // relative humidity in % * 1000 (0)
|
||||
uint32_t gas_resistance; // gas resistance in Ohm (0)
|
||||
} bme680_values_fixed_t;
|
||||
|
||||
/**
|
||||
* @brief Floating point sensor values (real THPG values)
|
||||
*/
|
||||
typedef struct { // invalid value
|
||||
float temperature; // temperature in degree C (-327.68)
|
||||
float pressure; // barometric pressure in hPascal (0.0)
|
||||
float humidity; // relative humidity in % (0.0)
|
||||
float gas_resistance; // gas resistance in Ohm (0.0)
|
||||
} bme680_values_float_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Oversampling rates
|
||||
*/
|
||||
typedef enum {
|
||||
osr_none = 0, // measurement is skipped, output values are invalid
|
||||
osr_1x = 1, // default oversampling rates
|
||||
osr_2x = 2,
|
||||
osr_4x = 3,
|
||||
osr_8x = 4,
|
||||
osr_16x = 5
|
||||
} bme680_oversampling_rate_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Filter sizes
|
||||
*/
|
||||
typedef enum {
|
||||
iir_size_0 = 0, // filter is not used
|
||||
iir_size_1 = 1,
|
||||
iir_size_3 = 2,
|
||||
iir_size_7 = 3,
|
||||
iir_size_15 = 4,
|
||||
iir_size_31 = 5,
|
||||
iir_size_63 = 6,
|
||||
iir_size_127 = 7
|
||||
} bme680_filter_size_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Sensor parameters that configure the TPHG measurement cycle
|
||||
*
|
||||
* T - temperature measurement
|
||||
* P - pressure measurement
|
||||
* H - humidity measurement
|
||||
* G - gas measurement
|
||||
*/
|
||||
typedef struct {
|
||||
|
||||
uint8_t osr_temperature; // T oversampling rate (default osr_1x)
|
||||
uint8_t osr_pressure; // P oversampling rate (default osr_1x)
|
||||
uint8_t osr_humidity; // H oversampling rate (default osr_1x)
|
||||
uint8_t filter_size; // IIR filter size (default iir_size_3)
|
||||
|
||||
uint16_t heater_temperature; // Heater temperature for G (default 320)
|
||||
uint16_t heater_duration; // Heater duration for G (default 150)
|
||||
|
||||
int8_t ambient_temperature; // Ambient temperature for G (default 25);
|
||||
|
||||
} bme680_settings_t;
|
||||
|
||||
/**
|
||||
* @brief Data structure for calibration parameters
|
||||
*
|
||||
* These calibration parameters are used in compensation algorithms to convert
|
||||
* raw sensor data to measurement results.
|
||||
*/
|
||||
typedef struct {
|
||||
|
||||
uint16_t par_t1; // calibration data for temperature compensation
|
||||
int16_t par_t2;
|
||||
int8_t par_t3;
|
||||
|
||||
uint16_t par_p1; // calibration data for pressure compensation
|
||||
int16_t par_p2;
|
||||
int8_t par_p3;
|
||||
int16_t par_p4;
|
||||
int16_t par_p5;
|
||||
int8_t par_p7;
|
||||
int8_t par_p6;
|
||||
int16_t par_p8;
|
||||
int16_t par_p9;
|
||||
uint8_t par_p10;
|
||||
|
||||
uint16_t par_h1; // calibration data for humidity compensation
|
||||
uint16_t par_h2;
|
||||
int8_t par_h3;
|
||||
int8_t par_h4;
|
||||
int8_t par_h5;
|
||||
uint8_t par_h6;
|
||||
int8_t par_h7;
|
||||
|
||||
int8_t par_gh1; // calibration data for gas compensation
|
||||
int16_t par_gh2;
|
||||
int8_t par_gh3;
|
||||
|
||||
int32_t t_fine; // temperatur correction factor for P and G
|
||||
uint8_t res_heat_range;
|
||||
int8_t res_heat_val;
|
||||
int8_t range_sw_err;
|
||||
|
||||
} bme680_calib_data_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief BME680 sensor device data structure type
|
||||
*/
|
||||
typedef struct {
|
||||
|
||||
bool active; // indicates whether sensor is active
|
||||
int error_code; // contains the error code of last operation
|
||||
|
||||
uint8_t bus; // I2C = x, SPI = 1
|
||||
uint8_t addr; // I2C = slave address, SPI = 0
|
||||
uint8_t spi_cs_pin; // GPIO used as SPI CS
|
||||
|
||||
bool meas_started; // indicates whether measurement started
|
||||
uint32_t meas_start_tick; // measurement start time in RTOS ticks
|
||||
uint32_t meas_duration; // measurement duration in RTOS ticks
|
||||
|
||||
bme680_settings_t settings; // sensor settings
|
||||
bme680_calib_data_t calib_data; // calibration data of the sensor
|
||||
|
||||
} bme680_sensor_t;
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* End of CPP guard */
|
||||
|
||||
#endif /* __BME680_TYPES_H__ */
|
||||
|
||||
9
extras/bme680/component.mk
Normal file
9
extras/bme680/component.mk
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# Component makefile for extras/bme60
|
||||
|
||||
# expected anyone using bme680 driver includes it as 'bme680/bme680.h'
|
||||
INC_DIRS += $(bme680_ROOT)..
|
||||
|
||||
# args for passing into compile rule generation
|
||||
bme680_SRC_DIR = $(bme680_ROOT)
|
||||
|
||||
$(eval $(call component_compile_rules,bme680))
|
||||
Loading…
Add table
Add a link
Reference in a new issue