diff --git a/examples/bme680/README.md b/examples/bme680/README.md new file mode 100644 index 0000000..d8a1a30 --- /dev/null +++ b/examples/bme680/README.md @@ -0,0 +1,46 @@ +# BME680 Driver Examples + +These examples demonstrate the usage of the BME680 driver with only one and multiple BME680 sensors. + +## 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*__ + +This simple example uses only **one sensor** connected either to **I2C** or to **SPI**. Which of these interfaces is used is defined by constant **SPI_USED**. The user task triggers a measurement every second and uses function ```vTaskDelay``` to wait for the measurement results. + +__*bme680_two_sensors*__ + +This example uses **two sensors**. One sensor is connected to **I2C** bus 0 and one sensor is connected to **SPI**. It defines two different user tasks, one for each sensor. It demonstrate the possible approaches to wait for measurement results, active busy waiting using ```bme680_is_measuring``` and passive waiting using *vTaskDelay*. + +__*bme680_heating_profiles*__ + +This simple example uses one **only sensor** connected to **I2C** bus 0 and a sequence of heating profiles. The heating profile is changed with each cycle. diff --git a/examples/bme680/bme680_heating_profiles/Makefile b/examples/bme680/bme680_heating_profiles/Makefile new file mode 100644 index 0000000..c3158ba --- /dev/null +++ b/examples/bme680/bme680_heating_profiles/Makefile @@ -0,0 +1,3 @@ +PROGRAM=BME680_heating_profiles +EXTRA_COMPONENTS = extras/i2c extras/bme680 +include ../../../common.mk diff --git a/examples/bme680/bme680_heating_profiles/bme680_heating_profiles.c b/examples/bme680/bme680_heating_profiles/bme680_heating_profiles.c new file mode 100644 index 0000000..56d7eb5 --- /dev/null +++ b/examples/bme680/bme680_heating_profiles/bme680_heating_profiles.c @@ -0,0 +1,125 @@ +/** + * Simple example with one sensor connected to I2C bus 0 and a sequence of + * heating profiles. The heating profile is changed with each cycle. + * + * Harware configuration: + * + * I2C +-------------------------+ +----------+ + * | ESP8266 Bus 0 | | BME680 | + * | GPIO 5 (SCL) ------> SCL | + * | GPIO 4 (SDA) ------- SDA | + * +-------------------------+ +----------+ + */ + +#include "espressif/esp_common.h" +#include "esp/uart.h" + +#include "FreeRTOS.h" +#include "task.h" + +// include communication interface driver +#include "i2c/i2c.h" + +// include BME680 driver +#include "bme680/bme680.h" + +// define I2C interface for BME680 sensors +#define I2C_BUS 0 +#define I2C_SCL_PIN 5 +#define I2C_SDA_PIN 4 + +static bme680_sensor_t* sensor; + +/* + * User task that triggers measurements of sensor every seconds. It uses + * function *vTaskDelay* to wait for measurement results and changes the + * heating profile in each cycle. + */ +void user_task(void *pvParameters) +{ + bme680_values_float_t values; + + TickType_t last_wakeup = xTaskGetTickCount(); + + uint32_t count = 0; + + while (1) + { + if (count++ < 60) + // disable gas measurement for cycle counter < 60 + bme680_use_heater_profile (sensor, BME680_HEATER_NOT_USED); + else + // change heating profile in each cycle + switch (count % 5) + { + case 0: bme680_use_heater_profile (sensor, 0); break; + case 1: bme680_use_heater_profile (sensor, 1); break; + case 2: bme680_use_heater_profile (sensor, 2); break; + case 3: bme680_use_heater_profile (sensor, 3); break; + case 4: bme680_use_heater_profile (sensor, 4); break; + } + + // measurement duration changes in each cycle + uint32_t duration = bme680_get_measurement_duration(sensor); + + // trigger the sensor to start one TPHG measurement cycle + if (bme680_force_measurement (sensor)) + { + // passive waiting until measurement results are available + vTaskDelay (duration); + + // 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 to 4x oversampling for temperature + // and 2x oversampling for humidity. Pressure measurement is skipped. + bme680_set_oversampling_rates(sensor, osr_4x, osr_none, osr_2x); + + // Change the IIR filter size for temperature and pressure to 7. + bme680_set_filter_size(sensor, iir_size_7); + + // Define a number of different heating profiles + bme680_set_heater_profile (sensor, 0, 200, 100); + bme680_set_heater_profile (sensor, 1, 250, 120); + bme680_set_heater_profile (sensor, 2, 300, 140); + bme680_set_heater_profile (sensor, 3, 350, 160); + bme680_set_heater_profile (sensor, 4, 400, 180); + } +} diff --git a/examples/bme680/bme680_one_sensor/Makefile b/examples/bme680/bme680_one_sensor/Makefile new file mode 100644 index 0000000..dba740d --- /dev/null +++ b/examples/bme680/bme680_one_sensor/Makefile @@ -0,0 +1,3 @@ +PROGRAM=BME680_One_Sensor +EXTRA_COMPONENTS = extras/i2c extras/bme680 +include ../../../common.mk diff --git a/examples/bme680/bme680_one_sensor/bme680_one_sensor.c b/examples/bme680/bme680_one_sensor/bme680_one_sensor.c new file mode 100644 index 0000000..65de6f1 --- /dev/null +++ b/examples/bme680/bme680_one_sensor/bme680_one_sensor.c @@ -0,0 +1,131 @@ +/** + * 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 "FreeRTOS.h" +#include "task.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 5 +#define I2C_SDA_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; + + TickType_t last_wakeup = xTaskGetTickCount(); + + // as long as sensor configuration isn't changed, duration is constant + uint32_t duration = bme680_get_measurement_duration(sensor); + + while (1) + { + // trigger the sensor to start one TPHG measurement cycle + if (bme680_force_measurement (sensor)) + { + // passive waiting until measurement results are available + vTaskDelay (duration); + + // alternatively: busy waiting until measurement results are available + // while (bme680_is_measuring (sensor)) ; + + // 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 to 4x oversampling for temperature + // and 2x oversampling for humidity. Pressure measurement is skipped. + bme680_set_oversampling_rates(sensor, osr_4x, osr_none, osr_2x); + + // Change the IIR filter size for temperature and pressure to 7. + bme680_set_filter_size(sensor, iir_size_7); + + // Change the heater profile 0 to 200 degree Celcius for 100 ms. + bme680_set_heater_profile (sensor, 0, 200, 100); + bme680_use_heater_profile (sensor, 0); + + // Set ambient temperature to 10 degree Celsius + bme680_set_ambient_temperature (sensor, 10); + } +} diff --git a/examples/bme680/bme680_two_sensors/Makefile b/examples/bme680/bme680_two_sensors/Makefile new file mode 100644 index 0000000..e17e876 --- /dev/null +++ b/examples/bme680/bme680_two_sensors/Makefile @@ -0,0 +1,3 @@ +PROGRAM=BME680_Tow_Sensors +EXTRA_COMPONENTS = extras/i2c extras/bme680 +include ../../../common.mk diff --git a/examples/bme680/bme680_two_sensors/bme680_two_sensors.c b/examples/bme680/bme680_two_sensors/bme680_two_sensors.c new file mode 100644 index 0000000..5fa1999 --- /dev/null +++ b/examples/bme680/bme680_two_sensors/bme680_two_sensors.c @@ -0,0 +1,155 @@ +/** + * Simple example with two sensors, one sensor connected to I2C bus 0 and + * one sensor connected to SPI. It defines two different user tasks, one for + * each sensor. It demonstrate the possible approaches to wait for measurement + * results, active busy waiting using ```bme680_is_measuring``` and 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 "FreeRTOS.h" +#include "task.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 5 +#define I2C_SDA_PIN 4 + +static bme680_sensor_t* sensor1; +static bme680_sensor_t* sensor2; + +/* + * User task that triggers measurements of sensor1 every 5 seconds and + * uses *vTaskDelay* to wait for measurement results. + */ +void user_task_sensor1(void *pvParameters) +{ + bme680_values_float_t values; + + TickType_t last_wakeup = xTaskGetTickCount(); + + uint32_t duration = bme680_get_measurement_duration (sensor1); + + while (1) + { + // trigger the sensor to start one TPHG measurement cycle + if (bme680_force_measurement (sensor1)) + { + + // passive waiting until measurement results are available + 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 and + * uses *bme680_is_measuring* 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 + if (bme680_force_measurement (sensor2)) + { + // busy waiting until measurement results are available + while (bme680_is_measuring (sensor2)) ; + + // 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_4x, osr_2x, osr_1x); + bme680_set_oversampling_rates(sensor2, osr_8x, osr_8x, osr_8x); + + // Change the IIR filter size 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 heater profile 0 to 200 degree Celcius for 150 ms. + bme680_set_heater_profile (sensor1, 0, 200, 150); + bme680_set_heater_profile (sensor2, 0, 200, 150); + + // Activate the heater profile 0 + bme680_use_heater_profile (sensor1, 0); + bme680_use_heater_profile (sensor2, 0); + } +} diff --git a/extras/bme680/README.md b/extras/bme680/README.md new file mode 100644 index 0000000..629322f --- /dev/null +++ b/extras/bme680/README.md @@ -0,0 +1,555 @@ +# Driver for **BME680** digital **environmental sensor** + +This driver is written for usage with the ESP8266 and FreeRTOS. It supports multiple BME680 sensors which are either connected to the SPI or to different I2C interfaces with different addresses. + +## About the sensor + +BME680 is an ultra-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 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 by the sensor 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 modes, the **sleep mode** and the **forced mode**. + +The sensor starts after power-up automatically in the *sleep mode* where it does not perform any measurement and consumes only 0.15 μA. Measurements are only done in *forced mode*. + +**Please note:** There are two further undocumented modes, the *parallel* and the *sequential* mode. They can't be supported by the driver, since it is not clear what they do and how to use them. + +#### 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 cycle, **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, especially if gas measurement is enabled. + +To avoid the blocking of the user task during measurements, the measurement process is therefore 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 function ```vTaskDelay``` and the value returned from function ```bme680_get_measurement_duration``` or wait as long as function ```bme680_is_measuring``` returns true. + +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```. + +``` +... +// as long as sensor configuration isn't changed, the duration can be considered as constant +uint32_t duration = bme680_get_measurement_duration(sensor); +... +if (bme680_force_measurement (sensor)) // STEP 1 +{ + // STEP 2: passive waiting until measurement results are available + vTaskDelay (duration); + + // STEP 3: get the results and do something with them + if (bme680_get_results_float (sensor, &values)) + ... +} +... +``` +Alternatively, busy waiting can be realized using function ```bme680_is_measuring```. +``` +... +if (bme680_force_measurement (sensor)) // STEP 1 +{ + // STEP 2: busy waiting until measurement results are available + while (bme680_is_measuring (sensor)) ; + + // STEP 3: get the results and do something with them + if (bme680_get_results_float (sensor, &values)) + ... +} +... +``` + +For convenience, it is also possible to use the high-level functions ```bme680_measure_float``` or ```bme680_measure_fixed```. These functions combine all 3 steps above within a single function and are therefore very easy to use. **Please note** that these functions must not be used when they are called from a software timer callback function since the calling task is delayed using function *vTaskDelay*. + +``` +... +// ONE STEP: measure, wait, get the results and do something with them +if (bme680_measure_float (sensor, &values)) + ... +... +``` + +#### Measurement results + +Once the sensor has finished the measurement raw data are available at the sensor. Either function ```bme680_get_results_fixed``` or function ```bme680_get_results_float``` can be used to fetch the results. Both functions read raw data from the sensor and converts them into utilizable fixed point or floating point sensor values. + +**Please note:** Conversion of raw sensor data into the final sensor values is based on very complex calculations that use a large number of calibration parameters. Therefore, the driver does not provide functions that only return the raw sensor data. + +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 +| humidity | 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. + +If the TPHG measurement cycle or fetching the results fails, invalid sensor values are returned: + +| Invalid Value | Fixed Point | Floating Point | +| ----------- | ------------- | -------- | +| temperature | INT16_MIN | -327.68 | +| pressure | 0 | 0.0 | +| humidity | 0 | 0.0 | +| gas_resistance | 0 | 0.0 | + + +## Measurement settings + +The sensor allows to change a lot of measurement parameters. + +#### Oversampling rates + +To increase the resolution of raw sensor data, the sensor supports oversampling for temperature, pressure, and humidity measurements. Using function ```bme680_set_oversampling_rates```, individual **oversampling rates** can be defined for these measurements. With an oversampling rate *osr*, the resolution of the according raw sensor data can be increased from 16 bit to 16+ld(*osr*) bit. + +Possible oversampling rates are 1x (default by the driver) 2x, 4x, 8x and 16x. It is also possible to define an oversampling rate of 0. This **deactivates** the corresponding measurement and the output values become invalid. + +``` +... +// Changes the oversampling rate for temperature to 4x and for pressure to 2x. Humidity measurement is skipped. +bme680_set_oversampling_rates(sensor, osr_4x, osr_2x, osr_none); +... +``` + +#### IIR Filter + +The sensor also 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. With the IIR filter the resolution of pressure and temperature data increases 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**. + +``` +... +// Change the IIR filter size for temperature and pressure to 7. +bme680_set_filter_size(sensor, iir_size_7); +... +// Don't use IIR filter +bme680_set_filter_size(sensor, iir_size_0); +... +``` + +#### Heater profile + +For the gas measurement, the sensor integrates a heater. Parameters for this heater are defined by **heater profiles**. The sensor supports up to 10 such heater profiles, which are numbered from 0 to 9. Each profile consists of a temperature set-point (the target temperature) and a heating duration. By default, only the heater profile 0 with 320 degree Celsius as target temperature and 150 ms heating duration is defined. + +**Please note:** According to the data sheet, 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. + +Function ```bme680_set_heater_profile``` can be used to set the parameters for one of the heater profiles 0 ... 9. Once the parameters of a heater profile are defined, the gas measurement can be activated with that heater profile using function ```bme680_use_heater_profile```. If -1 or ```BME680_HEATER_NOT_USED``` is used as heater profile, gas measurement is deactivated completely. + +``` +... +// Change the heater profile 1 to 300 degree Celsius for 100 ms and activate it +bme680_set_heater_profile (sensor, 1, 300, 100); +bme680_use_heater_profile (sensor, 1); +... +// Deactivate gas measurement completely +bme680_use_heater_profile (sensor, BME680_HEATER_NOT_USED); +... + +``` + +If several heater profiles have been defined with function ```bme680_set_heater_profile```, a sequence of gas measurements with different heater parameters can be realized by a sequence of activations of different heater profiles for successive TPHG measurements using function ```bme680_use_heater_profile```. + +For example, if there were 5 heater profiles defined with following code during the setup +``` +bme680_set_heater_profile (sensor, 0, 200, 100); +bme680_set_heater_profile (sensor, 1, 250, 120); +bme680_set_heater_profile (sensor, 2, 300, 140); +bme680_set_heater_profile (sensor, 3, 350, 160); +bme680_set_heater_profile (sensor, 4, 400, 180); +``` + +the user task could use them as a sequence like following: + +``` +... +while (1) +{ + switch (count++ % 5) + { + case 0: bme680_use_heater_profile (sensor, 0); break; + case 1: bme680_use_heater_profile (sensor, 1); break; + case 2: bme680_use_heater_profile (sensor, 2); break; + case 3: bme680_use_heater_profile (sensor, 3); break; + case 4: bme680_use_heater_profile (sensor, 4); break; + } + + // measurement duration changes in each cycle + uint32_t duration = bme680_get_measurement_duration(sensor); + + // trigger the sensor to start one TPHG measurement cycle + if (bme680_force_measurement (sensor)) + { + vTaskDelay (duration); + + // get the results and do something with them + if (bme680_get_results_float (sensor, &values)) + ... + } + ... +} +... +``` + +#### Ambient temperature + +The heater resistance calculation algorithm takes into account the ambient temperature of the sensor. Using function ```bme680_set_ambient_temperature```, the ambient temperature either determined from the sensor itself or from another temperature sensor can be set. + +``` +... +bme680_set_ambient_temperature (sensor, ambient); +... +``` + +## 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 of the BME680 sensor itself. To test for a certain error, first 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. Then you can test the result for a certain error code. + +For example, error handling for ```bme680_get_results_float``` could look like: + +``` +if (bme680_get_results_float (sensor, &values)) +{ + // no error happened + ... +} +else +{ + // error happened + + switch (sensor->error_code & BME680_INT_ERROR_MASK) + { + case BME680_INT_BUSY: ... + case BME680_INT_READ_FAILED: ... + ... + } + switch (sensor->error_code & BME680_DRV_ERROR_MASK) + { + case BME680_MEAS_STILL_RUNNING: ... + case BME680_NO_NEW_DATA: ... + ... + } +} +``` + +## Usage + +First, the hardware configuration has to be established. This can differ dependent on the communication interface and the number of sensors used. + +### 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 the configuration with only one sensor at SPI bus using GPIO2 as CS signal. + +``` + +-------------------------+ +--------+ + | Bus 1 | | BME680 | + | 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 which is used as CS signal of SPI bus 1 does not work correctly together with the BME680. Therefore, the user has to specify another GPIO pin as CS signal, e.g., GPIO2. +Next figure shows a possible configuration with two I2C buses. In that case, the sensors can have same or different I2C slave addresses. + +``` + +-------------------------+ +----------+ + | 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 | + +-------------------------+ +----------+ +``` + +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 5 +#define I2C_SDA_PIN 4 +#endif + +``` + +### Main program + +If I2C interfaces are used, they have to be initialized first. + +``` +i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_100K)) +``` + +SPI interface has not to be initialized explicitly. + +Once the I2C interfaces are initialized, function ```bme680_init_sensor``` has to be called for each BME680 sensor in order to initialize the sensor and to check its availability as well as its error state. This function returns a pointer to a sensor device data structure or NULL in case of error. + +The parameter *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 data sheet of the sensor. + +``` +if (sensor) +{ + // Create a task that uses the sensor + xTaskCreate(user_task, "user_task", 256, NULL, 2, NULL); + + /** -- OPTIONAL PART -- */ + + // Changes the oversampling rates to 4x oversampling for temperature + // and 2x oversampling for humidity. Pressure measurement is skipped. + bme680_set_oversampling_rates(sensor, osr_4x, osr_none, osr_2x); + + // Change the IIR filter size for temperature and pressure to 7. + bme680_set_filter_size(sensor, iir_size_7); + + // Change the heater profile 0 to 200 degree Celsius for 100 ms. + bme680_set_heater_profile (sensor, 0, 200, 100); + bme680_use_heater_profile (sensor, 0); + + ... +} +``` + +Last, the user task that uses the sensor has to be created. + + +### User task + +BME680 supports only the *forced mode* that performs exactly one measurement. Therefore, the measurement has to be triggered in each cycle. The waiting for measurement results is also required in each cycle, 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; + + TickType_t last_wakeup = xTaskGetTickCount(); + + // as long as sensor configuration isn't changed, duration is constant + uint32_t duration = bme680_get_measurement_duration(sensor); + + while (1) + { + // trigger the sensor to start one TPHG measurement cycle + bme680_force_measurement (sensor); + + // passive waiting until measurement results are available + vTaskDelay (duration); + + // alternatively: busy waiting until measurement results are available + // while (bme680_is_measuring (sensor)) ; + + // 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 perform exactly one measurement in each cycle. + +The task is then delayed using function ```vTaskDelay``` and the value returned from function ```bme680_get_measurement_duration``` or as long as function ```bme680_is_measuring``` returns true. + +Since the measurement duration only depends on the current sensor configuration, it changes only when sensor configuration is changed. Therefore, it can be considered as constant as long as the sensor configuration isn't changed and can be determined with function ```bme680_get_measurement_duration``` outside the task loop. If the sensor configuration changes, the function has to be executed again. + +Once the measurement results are available, they can be fetched as fixed point oder floating point sensor values using function ```bme680_get_results_float``` and ```bme680_get_results_fixed```, respectively. + +## Full Example + +``` +// Uncomment to use SPI +// #define SPI_USED + +#include "espressif/esp_common.h" +#include "esp/uart.h" + +#include "FreeRTOS.h" +#include "task.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 5 +#define I2C_SDA_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 waiting + * alternative is shown in comments + */ +void user_task(void *pvParameters) +{ + bme680_values_float_t values; + + TickType_t last_wakeup = xTaskGetTickCount(); + + // as long as sensor configuration isn't changed, duration is constant + uint32_t duration = bme680_get_measurement_duration(sensor); + + while (1) + { + // trigger the sensor to start one TPHG measurement cycle + bme680_force_measurement (sensor); + + // passive waiting until measurement results are available + vTaskDelay (duration); + + // alternatively: busy waiting until measurement results are available + // while (bme680_is_measuring (sensor)) ; + + // 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 to 4x oversampling for temperature + // and 2x oversampling for humidity. Pressure measurement is skipped. + bme680_set_oversampling_rates(sensor, osr_4x, osr_none, osr_2x); + + // Change the IIR filter size for temperature and pressure to 7. + bme680_set_filter_size(sensor, iir_size_7); + + // Change the heater profile 0 to 200 degree Celcius for 100 ms. + bme680_set_heater_profile (sensor, 0, 200, 100); + bme680_use_heater_profile (sensor, 0); + } +} +``` diff --git a/extras/bme680/bme680.c b/extras/bme680/bme680.c new file mode 100644 index 0000000..f2742ee --- /dev/null +++ b/extras/bme680/bme680.c @@ -0,0 +1,1475 @@ +/* + * 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. + * + * The information provided is believed to be accurate and reliable. The + * copyright holder assumes no responsibility for the consequences of use + * of such information nor for any infringement of patents or other rights + * of third parties which may result from its use. No license is granted by + * implication or otherwise under any patent or patent rights of the copyright + * holder. + */ + +#include + +#include "FreeRTOS.h" +#include "task.h" + +#include "espressif/esp_common.h" +#include "espressif/sdk_private.h" + +#include "esp/spi.h" +#include "i2c/i2c.h" + +#include "bme680.h" + +#if defined(BME680_DEBUG_LEVEL_2) +#define debug(s, f, ...) printf("%s %s: " s "\n", "BME680", f, ## __VA_ARGS__) +#define debug_dev(s, f, d, ...) printf("%s %s: bus %d, addr %02x - " s "\n", "BME680", f, d->bus, d->addr, ## __VA_ARGS__) +#else +#define debug(s, f, ...) +#define debug_dev(s, f, d, ...) +#endif + +#if defined(BME680_DEBUG_LEVEL_1) || defined(BME680_DEBUG_LEVEL_2) +#define error(s, f, ...) printf("%s %s: " s "\n", "BME680", f, ## __VA_ARGS__) +#define error_dev(s, f, d, ...) printf("%s %s: bus %d, addr %02x - " s "\n", "BME680", f, d->bus, d->addr, ## __VA_ARGS__) +#else +#define error(s, f, ...) +#define error_dev(s, f, d, ...) +#endif + +// modes: unfortunatly, only SLEEP_MODE and FORCED_MODE are documented +#define BME680_SLEEP_MODE 0x00 // low power sleeping +#define BME680_FORCED_MODE 0x01 // perform one TPHG cycle (field data 0 filled) +#define BME680_PARALLEL_MODE 0x02 // no information what it does :-( +#define BME680_SQUENTUAL_MODE 0x02 // no information what it does (field data 0+1+2 filled) + +// register addresses +#define BME680_REG_RES_HEAT_VAL 0x00 +#define BME680_REG_RES_HEAT_RANGE 0x02 +#define BME680_REG_RANGE_SW_ERROR 0x06 + +#define BME680_REG_IDAC_HEAT_BASE 0x50 // 10 regsrs idac_heat_0 ... idac_heat_9 +#define BME680_REG_RES_HEAT_BASE 0x5a // 10 registers res_heat_0 ... res_heat_9 +#define BME680_REG_GAS_WAIT_BASE 0x64 // 10 registers gas_wait_0 ... gas_wait_9 +#define BME680_REG_CTRL_GAS_0 0x70 +#define BME680_REG_CTRL_GAS_1 0x71 +#define BME680_REG_CTRL_HUM 0x72 +#define BME680_REG_STATUS 0x73 +#define BME680_REG_CTRL_MEAS 0x74 +#define BME680_REG_CONFIG 0x75 +#define BME680_REG_ID 0xd0 +#define BME680_REG_RESET 0xe0 + +// field data 0 registers +#define BME680_REG_MEAS_STATUS_0 0x1d +#define BME680_REG_MEAS_INDEX_0 0x1e +#define BME680_REG_PRESS_MSB_0 0x1f +#define BME680_REG_PRESS_LSB_0 0x20 +#define BME680_REG_PRESS_XLSB_0 0x21 +#define BME680_REG_TEMP_MSB_0 0x22 +#define BME680_REG_TEMP_LSB_0 0x23 +#define BME680_REG_TEMP_XLSB_0 0x24 +#define BME680_REG_HUM_MSB_0 0x25 +#define BME680_REG_HUM_LSB_0 0x26 +#define BME680_REG_GAS_R_MSB_0 0x2a +#define BME680_REG_GAS_R_LSB_0 0x2b + +// field data 1 registers (not documented, used in SEQUENTIAL_MODE) +#define BME680_REG_MEAS_STATUS_1 0x2e +#define BME680_REG_MEAS_INDEX_1 0x2f + +// field data 2 registers (not documented, used in SEQUENTIAL_MODE) +#define BME680_REG_MEAS_STATUS_2 0x3f +#define BME680_REG_MEAS_INDEX_2 0x40 + +// field data addresses +#define BME680_REG_RAW_DATA_0 BME680_REG_MEAS_STATUS_0 // 0x1d ... 0x2b +#define BME680_REG_RAW_DATA_1 BME680_REG_MEAS_STATUS_1 // 0x2e ... 0x3c +#define BME680_REG_RAW_DATA_2 BME680_REG_MEAS_STATUS_2 // 0x40 ... 0x4d +#define BME680_REG_RAW_DATA_LEN (BME680_REG_GAS_R_LSB_0 - BME680_REG_MEAS_STATUS_0 + 1) + +// calibration data registers +#define BME680_REG_CD1_ADDR 0x89 // 25 byte calibration data +#define BME680_REG_CD1_LEN 25 +#define BME680_REG_CD2_ADDR 0xe1 // 16 byte calibration data +#define BME680_REG_CD2_LEN 16 +#define BME680_REG_CD3_ADDR 0x00 // 8 byte device specific calibration data +#define BME680_REG_CD3_LEN 8 + +// register structure definitions +#define BME680_NEW_DATA_BITS 0x80 // BME680_REG_MEAS_STATUS<7> +#define BME680_NEW_DATA_SHIFT 7 // BME680_REG_MEAS_STATUS<7> +#define BME680_GAS_MEASURING_BITS 0x40 // BME680_REG_MEAS_STATUS<6> +#define BME680_GAS_MEASURING_SHIFT 6 // BME680_REG_MEAS_STATUS<6> +#define BME680_MEASURING_BITS 0x20 // BME680_REG_MEAS_STATUS<5> +#define BME680_MEASURING_SHIFT 5 // BME680_REG_MEAS_STATUS<5> +#define BME680_GAS_MEAS_INDEX_BITS 0x0f // BME680_REG_MEAS_STATUS<3:0> +#define BME680_GAS_MEAS_INDEX_SHIFT 0 // BME680_REG_MEAS_STATUS<3:0> + +#define BME680_GAS_R_LSB_BITS 0xc0 // BME680_REG_GAS_R_LSB<7:6> +#define BME680_GAS_R_LSB_SHIFT 6 // BME680_REG_GAS_R_LSB<7:6> +#define BME680_GAS_VALID_BITS 0x20 // BME680_REG_GAS_R_LSB<5> +#define BME680_GAS_VALID_SHIFT 5 // BME680_REG_GAS_R_LSB<5> +#define BME680_HEAT_STAB_R_BITS 0x10 // BME680_REG_GAS_R_LSB<4> +#define BME680_HEAT_STAB_R_SHIFT 4 // BME680_REG_GAS_R_LSB<4> +#define BME680_GAS_RANGE_R_BITS 0x0f // BME680_REG_GAS_R_LSB<3:0> +#define BME680_GAS_RANGE_R_SHIFT 0 // BME680_REG_GAS_R_LSB<3:0> + +#define BME680_HEAT_OFF_BITS 0x04 // BME680_REG_CTRL_GAS_0<3> +#define BME680_HEAT_OFF_SHIFT 3 // BME680_REG_CTRL_GAS_0<3> + +#define BME680_RUN_GAS_BITS 0x10 // BME680_REG_CTRL_GAS_1<4> +#define BME680_RUN_GAS_SHIFT 4 // BME680_REG_CTRL_GAS_1<4> +#define BME680_NB_CONV_BITS 0x0f // BME680_REG_CTRL_GAS_1<3:0> +#define BME680_NB_CONV_SHIFT 0 // BME680_REG_CTRL_GAS_1<3:0> + +#define BME680_SPI_3W_INT_EN_BITS 0x40 // BME680_REG_CTRL_HUM<6> +#define BME680_SPI_3W_INT_EN_SHIFT 6 // BME680_REG_CTRL_HUM<6> +#define BME680_OSR_H_BITS 0x07 // BME680_REG_CTRL_HUM<2:0> +#define BME680_OSR_H_SHIFT 0 // BME680_REG_CTRL_HUM<2:0> + +#define BME680_OSR_T_BITS 0xe0 // BME680_REG_CTRL_MEAS<7:5> +#define BME680_OSR_T_SHIFT 5 // BME680_REG_CTRL_MEAS<7:5> +#define BME680_OSR_P_BITS 0x1c // BME680_REG_CTRL_MEAS<4:2> +#define BME680_OSR_P_SHIFT 2 // BME680_REG_CTRL_MEAS<4:2> +#define BME680_MODE_BITS 0x03 // BME680_REG_CTRL_MEAS<1:0> +#define BME680_MODE_SHIFT 0 // BME680_REG_CTRL_MEAS<1:0> + +#define BME680_FILTER_BITS 0x1c // BME680_REG_CONFIG<4:2> +#define BME680_FILTER_SHIFT 2 // BME680_REG_CONFIG<4:2> +#define BME680_SPI_3W_EN_BITS 0x01 // BME680_REG_CONFIG<0> +#define BME680_SPI_3W_EN_SHIFT 0 // BME680_REG_CONFIG<0> + +#define BME680_SPI_MEM_PAGE_BITS 0x10 // BME680_REG_STATUS<4> +#define BME680_SPI_MEM_PAGE_SHIFT 4 // BME680_REG_STATUS<4> + +#define BME680_GAS_WAIT_BITS 0x3f // BME680_REG_GAS_WAIT+x<5:0> +#define BME680_GAS_WAIT_SHIFT 0 // BME680_REG_GAS_WAIT+x<5:0> +#define BME680_GAS_WAIT_MULT_BITS 0xc0 // BME680_REG_GAS_WAIT+x<7:6> +#define BME680_GAS_WAIT_MULT_SHIFT 6 // BME680_REG_GAS_WAIT+x<7:6> + +// commands +#define BME680_RESET_CMD 0xb6 // BME680_REG_RESET<7:0> +#define BME680_RESET_PERIOD 5 // reset time in ms + +#define BME680_RHR_BITS 0x30 // BME680_REG_RES_HEAT_RANGE<5:4> +#define BME680_RHR_SHIFT 4 // BME680_REG_RES_HEAT_RANGE<5:4> +#define BME680_RSWE_BITS 0xf0 // BME680_REG_RANGE_SW_ERROR<7:4> +#define BME680_RSWE_SHIFT 4 // BME680_REG_RANGE_SW_ERROR<7:4> + +// calibration data are stored in a calibration data map +#define BME680_CDM_SIZE (BME680_REG_CD1_LEN + BME680_REG_CD2_LEN + BME680_REG_CD3_LEN) +#define BME680_CDM_OFF1 0 +#define BME680_CDM_OFF2 BME680_REG_CD1_LEN +#define BME680_CDM_OFF3 BME680_CDM_OFF2 + BME680_REG_CD2_LEN + +// calibration parameter offsets in calibration data map +// calibration data from 0x89 +#define BME680_CDM_T2 1 +#define BME680_CDM_T3 3 +#define BME680_CDM_P1 5 +#define BME680_CDM_P2 7 +#define BME680_CDM_P3 9 +#define BME680_CDM_P4 11 +#define BME680_CDM_P5 13 +#define BME680_CDM_P7 15 +#define BME680_CDM_P6 16 +#define BME680_CDM_P8 19 +#define BME680_CDM_P9 21 +#define BME680_CDM_P10 23 +// calibration data from 0e1 +#define BME680_CDM_H2 25 +#define BME680_CDM_H1 26 +#define BME680_CDM_H3 28 +#define BME680_CDM_H4 29 +#define BME680_CDM_H5 30 +#define BME680_CDM_H6 31 +#define BME680_CDM_H7 32 +#define BME680_CDM_T1 33 +#define BME680_CDM_GH2 35 +#define BME680_CDM_GH1 37 +#define BME680_CDM_GH3 38 +// device specific calibration data from 0x00 +#define BME680_CDM_RHV 41 // 0x00 - res_heat_val +#define BME680_CDM_RHR 43 // 0x02 - res_heat_range +#define BME680_CDM_RSWE 45 // 0x04 - range_sw_error + + +/** + * @brief Raw data (integer values) read from sensor + */ +typedef struct { + + bool gas_valid; // indicate that gas measurement results are valid + bool heater_stable; // indicate that heater temperature was stable + + uint32_t temperature; // degree celsius x100 + uint32_t pressure; // pressure in Pascal + uint16_t humidity; // relative humidity x1000 in % + uint16_t gas_resistance; // gas resistance data + uint8_t gas_range; // gas resistance range + + uint8_t gas_index; // heater profile used (0 ... 9) + uint8_t meas_index; + + +} bme680_raw_data_t; + + +/** Forward declaration of functions for internal use */ + +static bool bme680_set_mode (bme680_sensor_t* dev, uint8_t mode); +static bool bme680_get_raw_data (bme680_sensor_t* dev, bme680_raw_data_t* raw); +static int16_t bme680_convert_temperature (bme680_sensor_t *dev, uint32_t raw_temperature); +static uint32_t bme680_convert_pressure (bme680_sensor_t *dev, uint32_t raw_pressure); +static uint32_t bme680_convert_humidity (bme680_sensor_t *dev, uint16_t raw_humidity); +static uint32_t bme680_convert_gas (bme680_sensor_t *dev, uint16_t raw_gas, uint8_t gas_range); + +static uint8_t bme680_heater_resistance (const bme680_sensor_t* dev, uint16_t temperature); +static uint8_t bme680_heater_duration (uint16_t duration); + +static bool bme680_reset (bme680_sensor_t* dev); +static bool bme680_is_available (bme680_sensor_t* dev); +static void bme680_delay_ms(uint32_t delay); + +static bool bme680_read_reg (bme680_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len); +static bool bme680_write_reg (bme680_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len); +static bool bme680_i2c_read (bme680_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len); +static bool bme680_i2c_write (bme680_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len); +static bool bme680_spi_read (bme680_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len); +static bool bme680_spi_write (bme680_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len); + +/** */ + +#define lsb_msb_to_type(t,b,o) (t)(((t)b[o+1] << 8) | b[o]) +#define lsb_to_type(t,b,o) (t)(b[o]) + +bme680_sensor_t* bme680_init_sensor(uint8_t bus, uint8_t addr, uint8_t cs) +{ + bme680_sensor_t* dev; + + if ((dev = malloc (sizeof(bme680_sensor_t))) == NULL) + return NULL; + + // init sensor data structure + dev->bus = bus; + dev->addr = addr; + dev->spi_cs_pin = cs; + dev->meas_started = false; + dev->meas_status = 0; + dev->settings.ambient_temperature = 0; + dev->settings.osr_temperature = osr_none; + dev->settings.osr_pressure = osr_none; + dev->settings.osr_humidity = osr_none; + dev->settings.filter_size = iir_size_0; + dev->settings.heater_profile = BME680_HEATER_NOT_USED; + memset(dev->settings.heater_temperature, 0, sizeof(uint16_t)*10); + memset(dev->settings.heater_duration, 0, sizeof(uint16_t)*10); + + if (!addr) + { + // SPI interface used + gpio_enable(dev->spi_cs_pin, GPIO_OUTPUT); + gpio_write (dev->spi_cs_pin, true); + } + + // reset the sensor + if (!bme680_reset(dev)) + { + error_dev ("Could not reset the sensor device.", __FUNCTION__, dev); + free (dev); + return NULL; + } + + // check availability of the sensor + if (!bme680_is_available (dev)) + { + error_dev ("Sensor is not available.", __FUNCTION__, dev); + free (dev); + return NULL; + } + + uint8_t buf[BME680_CDM_SIZE]; + + // read all calibration parameters from sensor + if (bme680_read_reg(dev, BME680_REG_CD1_ADDR, buf+BME680_CDM_OFF1, BME680_REG_CD1_LEN) && + bme680_read_reg(dev, BME680_REG_CD2_ADDR, buf+BME680_CDM_OFF2, BME680_REG_CD2_LEN) && + bme680_read_reg(dev, BME680_REG_CD3_ADDR, buf+BME680_CDM_OFF3, BME680_REG_CD3_LEN)) + { + dev->calib_data.par_t1 = lsb_msb_to_type (uint16_t, buf, BME680_CDM_T1); + dev->calib_data.par_t2 = lsb_msb_to_type ( int16_t, buf, BME680_CDM_T2); + dev->calib_data.par_t3 = lsb_to_type ( int8_t, buf, BME680_CDM_T3); + + // pressure compensation parameters + dev->calib_data.par_p1 = lsb_msb_to_type (uint16_t, buf, BME680_CDM_P1); + dev->calib_data.par_p2 = lsb_msb_to_type ( int16_t, buf, BME680_CDM_P2); + dev->calib_data.par_p3 = lsb_to_type ( int8_t, buf, BME680_CDM_P3); + dev->calib_data.par_p4 = lsb_msb_to_type ( int16_t, buf, BME680_CDM_P4); + dev->calib_data.par_p5 = lsb_msb_to_type ( int16_t, buf, BME680_CDM_P5); + dev->calib_data.par_p6 = lsb_to_type ( int8_t, buf, BME680_CDM_P6); + dev->calib_data.par_p7 = lsb_to_type ( int8_t, buf, BME680_CDM_P7); + dev->calib_data.par_p8 = lsb_msb_to_type ( int16_t, buf, BME680_CDM_P8); + dev->calib_data.par_p9 = lsb_msb_to_type ( int16_t, buf, BME680_CDM_P9); + dev->calib_data.par_p10 = lsb_to_type ( uint8_t, buf, BME680_CDM_P10); + + // humidity compensation parameters + dev->calib_data.par_h1 = (uint16_t)(((uint16_t)buf[BME680_CDM_H1+1] << 4) | + (buf[BME680_CDM_H1] & 0x0F)); + dev->calib_data.par_h2 = (uint16_t)(((uint16_t)buf[BME680_CDM_H2] << 4) | + (buf[BME680_CDM_H2+1] >> 4)); + dev->calib_data.par_h3 = lsb_to_type ( int8_t, buf, BME680_CDM_H3); + dev->calib_data.par_h4 = lsb_to_type ( int8_t, buf, BME680_CDM_H4); + dev->calib_data.par_h5 = lsb_to_type ( int8_t, buf, BME680_CDM_H5); + dev->calib_data.par_h6 = lsb_to_type ( uint8_t, buf, BME680_CDM_H6); + dev->calib_data.par_h7 = lsb_to_type ( int8_t, buf, BME680_CDM_H7); + + // gas sensor compensation parameters + dev->calib_data.par_gh1 = lsb_to_type ( int8_t, buf, BME680_CDM_GH1); + dev->calib_data.par_gh2 = lsb_msb_to_type ( int16_t, buf, BME680_CDM_GH2); + dev->calib_data.par_gh3 = lsb_to_type ( int8_t, buf, BME680_CDM_GH3); + + dev->calib_data.res_heat_range = (lsb_to_type (uint8_t, buf ,BME680_CDM_RHR) & + BME680_RHR_BITS) >> + BME680_RHR_SHIFT; + dev->calib_data.res_heat_val = (lsb_to_type ( int8_t, buf, BME680_CDM_RHV)); + dev->calib_data.range_sw_err = (lsb_to_type ( int8_t, buf, BME680_CDM_RSWE) & + BME680_RSWE_BITS) >> + BME680_RSWE_SHIFT; + } + else + { + error_dev ("Could not read in calibration parameters.", __FUNCTION__, dev); + dev->error_code |= BME680_READ_CALIB_DATA_FAILED; + free (dev); + return NULL; + + } + + // Set the default temperature, pressure and humidity settings + if (!bme680_set_oversampling_rates (dev, osr_1x, osr_1x, osr_1x) || + !bme680_set_filter_size (dev, iir_size_3)) + { + error_dev ("Could not configure default sensor settings for TPH.", __FUNCTION__, dev); + free (dev); + return NULL; + } + + // Set ambient temperature of sensor to default value (25 degree C) + dev->settings.ambient_temperature = 25; + + // Set heater default profile 0 to 320 degree Celcius for 150 ms + if (!bme680_set_heater_profile (dev, 0, 320, 150)) + { + error_dev ("Could not configure default heater profile settings.", __FUNCTION__, dev); + free (dev); + return NULL; + } + + if (!bme680_use_heater_profile (dev, 0)) + { + error_dev ("Could not configure default heater profile.", __FUNCTION__, dev); + free (dev); + return NULL; + } + + return dev; +} + +bool bme680_force_measurement (bme680_sensor_t* dev) +{ + if (!dev) return false; + + dev->error_code = BME680_OK; + + // return remaining time when measurement is already running + if (dev->meas_started) + { + debug_dev ("Measurement is already running.", __FUNCTION__, dev); + dev->error_code |= BME680_MEAS_ALREADY_RUNNING; + return false; + } + + // Set the power mode to forced mode to trigger one TPHG measurement cycle + if (!bme680_set_mode(dev, BME680_FORCED_MODE)) + { + error_dev ("Could not set forced mode to start TPHG measurement cycle.", __FUNCTION__, dev); + dev->error_code |= BME680_FORCE_MODE_FAILED; + return false; + } + + dev->meas_started = true; + dev->meas_start_tick = xTaskGetTickCount (); // system time in RTOS ticks + dev->meas_status = 0; + + debug_dev ("Started measurement at %.3f.", __FUNCTION__, dev, + (double)sdk_system_get_time()*1e-3); + + return true; +} + + +/** + * @brief Estimate the measuerment duration in ms + * + * Timing formulas extracted from BME280 datasheet and test in some + * experiments. They represent the maximum measurement duration. + * + * @return estimated measurument duration in RTOS ticks or -1 on error + */ +uint32_t bme680_get_measurement_duration (const bme680_sensor_t *dev) +{ + if (!dev) return 0; + + int32_t duration = 0; /* Calculate in us */ + + // wake up duration from sleep into forced mode + duration += 1250; + + // THP cycle duration which consumes 1963 µs for each measurement at maximum + if (dev->settings.osr_temperature) duration += (1 << (dev->settings.osr_temperature-1)) * 2300; + if (dev->settings.osr_pressure ) duration += (1 << (dev->settings.osr_pressure-1)) * 2300 + 575; + if (dev->settings.osr_humidity ) duration += (1 << (dev->settings.osr_humidity-1)) * 2300 + 575; + + // if gas measurement is used + if (dev->settings.heater_profile != BME680_HEATER_NOT_USED && + dev->settings.heater_duration[dev->settings.heater_profile] && + dev->settings.heater_temperature[dev->settings.heater_profile]) + { + // gas heating time + duration += dev->settings.heater_duration[dev->settings.heater_profile]*1000; + // gas measurement duration; + duration += 2300 + 575; + } + + // round up to next ms (1 us ... 1000 us => 1 ms) + duration += 999; + duration /= 1000; + + // some ms tolerance + duration += 5; + + // ceil to next integer value that is divisible by portTICK_PERIOD_MS and + // compute RTOS ticks (1 ... portTICK_PERIOD_MS = 1 tick) + duration = (duration + portTICK_PERIOD_MS-1) / portTICK_PERIOD_MS; + + // Since first RTOS tick can be shorter than the half of defined tick period, + // the delay caused by vTaskDelay(duration) might be 1 or 2 ms shorter than + // computed duration in rare cases. Since the duration is computed for maximum + // and not for the typical durations and therefore tends to be too long, this + // should not be a problem. Therefore, only one additional tick used. + return duration + 1; +} + + +bool bme680_is_measuring (bme680_sensor_t* dev) +{ + if (!dev) return false; + + dev->error_code = BME680_OK; + + // if measurement wasn't started, it is of course not measuring + if (!dev->meas_started) + { + dev->error_code |= BME680_MEAS_NOT_RUNNING; + return false; + } + + uint8_t raw[2]; + + // read maesurment status from sensor + if (!bme680_read_reg(dev, BME680_REG_MEAS_STATUS_0, raw, 2)) + { + error_dev ("Could not read measurement status from sensor.", __FUNCTION__, dev); + return false; + } + + dev->meas_status = raw[0]; + + // test whether measuring bit is set + return (dev->meas_status & BME680_MEASURING_BITS); +} + + +bool bme680_get_results_fixed (bme680_sensor_t* dev, bme680_values_fixed_t* results) +{ + if (!dev || !results) return false; + + dev->error_code = BME680_OK; + + // fill data structure with invalid values + results->temperature = INT16_MIN; + results->pressure = 0; + results->humidity = 0; + results->gas_resistance = 0; + + bme680_raw_data_t raw; + + if (!bme680_get_raw_data(dev, &raw)) + // return invalid values + return false; + + // use compensation algorithms to compute sensor values in fixed point format + + if (dev->settings.osr_temperature) + results->temperature = bme680_convert_temperature (dev, raw.temperature); + + if (dev->settings.osr_pressure) + results->pressure = bme680_convert_pressure (dev, raw.pressure); + + if (dev->settings.osr_humidity) + results->humidity = bme680_convert_humidity (dev, raw.humidity); + + if (dev->settings.heater_profile != BME680_HEATER_NOT_USED) + { + // convert gas only if raw data are valid and heater was stable + if (raw.gas_valid && raw.heater_stable) + results->gas_resistance = bme680_convert_gas (dev, raw.gas_resistance, + raw.gas_range); + else if (!raw.gas_valid) + dev->error_code = BME680_MEAS_GAS_NOT_VALID; + else + dev->error_code = BME680_HEATER_NOT_STABLE; + } + + debug_dev ("Fixed point sensor valus - %d ms: %d/100 C, %d/1000 Percent, %d Pascal, %d Ohm", + __FUNCTION__, dev, sdk_system_get_time (), + results->temperature, + results->humidity, + results->pressure, + results->gas_resistance); + + return true; +} + + +bool bme680_get_results_float (bme680_sensor_t* dev, bme680_values_float_t* results) +{ + if (!dev || !results) return false; + + bme680_values_fixed_t fixed; + + if (!bme680_get_results_fixed (dev, &fixed)) + return false; + + results->temperature = fixed.temperature / 100.0f; + results->pressure = fixed.pressure / 100.0f; + results->humidity = fixed.humidity / 1000.0f; + results->gas_resistance = fixed.gas_resistance; + + return true; +} + + +bool bme680_measure_fixed (bme680_sensor_t* dev, bme680_values_fixed_t* results) +{ + int32_t duration = bme680_force_measurement (dev); + + if (duration == BME680_NOK) + return false; // measurment couldn't be started + + else if (duration > 0) // wait for results + vTaskDelay (duration); + + return bme680_get_results_fixed (dev, results); +} + + +bool bme680_measure_float (bme680_sensor_t* dev, bme680_values_float_t* results) +{ + int32_t duration = bme680_force_measurement (dev); + + if (duration == BME680_NOK) + return false; // measurment couldn't be started + + else if (duration > 0) // wait for results + vTaskDelay (duration); + + return bme680_get_results_float (dev, results); +} + + + +#define bme_set_reg_bit(byte, bitname, bit) ( (byte & ~bitname##_BITS) | \ + ((bit << bitname##_SHIFT) & bitname##_BITS) ) +#define bme_get_reg_bit(byte, bitname) ( (byte & bitname##_BITS) >> bitname##_SHIFT ) + +bool bme680_set_oversampling_rates (bme680_sensor_t* dev, + bme680_oversampling_rate_t ost, + bme680_oversampling_rate_t osp, + bme680_oversampling_rate_t osh) +{ + if (!dev) return false; + + dev->error_code = BME680_OK; + + bool ost_changed = dev->settings.osr_temperature != ost; + bool osp_changed = dev->settings.osr_pressure != osp; + bool osh_changed = dev->settings.osr_humidity != osh; + + if (!ost_changed && !osp_changed && !osh_changed) + return true; + + // Set the temperature, pressure and humidity oversampling + dev->settings.osr_temperature = ost; + dev->settings.osr_pressure = osp; + dev->settings.osr_humidity = osh; + + uint8_t reg; + + if (ost_changed || osp_changed) + { + // read the current register value + if (!bme680_read_reg(dev, BME680_REG_CTRL_MEAS, ®, 1)) + return false; + + // set changed bit values + if (ost_changed) reg = bme_set_reg_bit (reg, BME680_OSR_T, ost); + if (osp_changed) reg = bme_set_reg_bit (reg, BME680_OSR_P, osp); + + // write back the new register value + if (!bme680_write_reg(dev, BME680_REG_CTRL_MEAS, ®, 1)) + return false; + } + + if (osh_changed) + { + // read the current register value + if (!bme680_read_reg(dev, BME680_REG_CTRL_HUM, ®, 1)) + return false; + + // set changed bit value + reg = bme_set_reg_bit (reg, BME680_OSR_H, osh); + + // write back the new register value + if (!bme680_write_reg(dev, BME680_REG_CTRL_HUM, ®, 1)) + return false; + } + + debug_dev ("Setting oversampling rates done: osrt=%d osp=%d osrh=%d", + __FUNCTION__, dev, + dev->settings.osr_temperature, + dev->settings.osr_pressure, + dev->settings.osr_humidity); + + return true; +} + + +bool bme680_set_filter_size(bme680_sensor_t* dev, bme680_filter_size_t size) +{ + if (!dev) return false; + + dev->error_code = BME680_OK; + + bool size_changed = dev->settings.filter_size != size; + + if (!size_changed) return true; + + /* Set the temperature, pressure and humidity settings */ + dev->settings.filter_size = size; + + uint8_t reg; + + // read the current register value + if (!bme680_read_reg(dev, BME680_REG_CONFIG, ®, 1)) + return false; + + // set changed bit value + reg = bme_set_reg_bit (reg, BME680_FILTER, size); + + // write back the new register value + if (!bme680_write_reg(dev, BME680_REG_CONFIG, ®, 1)) + return false; + + debug_dev ("Setting filter size done: size=%d", __FUNCTION__, dev, + dev->settings.filter_size); + + return true; +} + +bool bme680_set_heater_profile (bme680_sensor_t* dev, uint8_t profile, + uint16_t temperature, uint16_t duration) +{ + if (!dev) return false; + + if (profile > BME680_HEATER_PROFILES-1) + { + error_dev("Wrong heater profile id %d.", __FUNCTION__, dev, profile); + dev->error_code = BME680_WRONG_HEAT_PROFILE; + return false; + } + + dev->error_code = BME680_OK; + + bool temperature_changed = dev->settings.heater_temperature[profile] != temperature; + bool duration_changed = dev->settings.heater_duration[profile] != duration; + + if (!temperature_changed && !duration_changed) + return true; + + // set external gas sensor configuration + dev->settings.heater_temperature[profile] = temperature; // degree Celsius + dev->settings.heater_duration [profile] = duration; // milliseconds + + // compute internal gas sensor configuration parameters + uint8_t heat_dur = bme680_heater_duration(duration); // internal duration value + uint8_t heat_res = bme680_heater_resistance(dev, temperature); // internal temperature value + + // set internal gas sensor configuration parameters if changed + if (temperature_changed && + !bme680_write_reg(dev, BME680_REG_RES_HEAT_BASE+profile, &heat_res, 1)) + return false; + + if (duration_changed && + !bme680_write_reg(dev, BME680_REG_GAS_WAIT_BASE+profile, &heat_dur, 1)) + return false; + + debug_dev ("Setting heater profile %d done: temperature=%d duration=%d " + "heater_resistance=%02x heater_duration=%02x", + __FUNCTION__, dev, profile, + dev->settings.heater_temperature[profile], + dev->settings.heater_duration[profile], + heat_dur, heat_res); + + return true; +} + +bool bme680_use_heater_profile (bme680_sensor_t* dev, int8_t profile) +{ + if (!dev) return false; + + if (profile != BME680_HEATER_NOT_USED && (profile < 0 || profile > BME680_HEATER_PROFILES-1)) + { + error_dev("Wrong heater profile id %d.", __FUNCTION__, dev, profile); + dev->error_code = BME680_WRONG_HEAT_PROFILE; + return false; + } + + if (dev->settings.heater_profile == profile) + return false; + + dev->settings.heater_profile = profile; + + uint8_t reg = 0; // set + // set active profile + reg = bme_set_reg_bit (reg, BME680_NB_CONV, profile != BME680_HEATER_NOT_USED ? profile : 0); + + // enable or disable gas measurement + reg = bme_set_reg_bit (reg, BME680_RUN_GAS, (profile != BME680_HEATER_NOT_USED && + dev->settings.heater_temperature[profile] && + dev->settings.heater_duration[profile])); + + if (!bme680_write_reg(dev, BME680_REG_CTRL_GAS_1, ®, 1)) + return false; + + return true; +} + + +bool bme680_set_ambient_temperature (bme680_sensor_t* dev, int16_t ambient) +{ + if (!dev) return false; + + dev->error_code = BME680_OK; + + bool ambient_changed = dev->settings.ambient_temperature != ambient; + + if (!ambient_changed) + return true; + + // set ambient temperature configuration + dev->settings.ambient_temperature = ambient; // degree Celsius + + + // update all valid heater profiles + + // takes 894 us for only one defined profile and 1585 us for 10 defined profiles + uint8_t data[10]; + for (int i = 0; i < BME680_HEATER_PROFILES; i++) + { + data[i] = dev->settings.heater_temperature[i] ? + bme680_heater_resistance(dev, dev->settings.heater_temperature[i]) : 0; + } + if (!bme680_write_reg(dev, BME680_REG_RES_HEAT_BASE, data, 10)) + return false; + + /* + // takes 346 us for only one defined profile and 3316 us for 10 defined profiles + for (int i = 0; i < BME680_HEATER_PROFILES; i++) + if (dev->settings.heater_temperature[i]) + { + uint8_t heat_res = bme680_heater_resistance(dev, dev->settings.heater_temperature[i]); + if (!bme680_write_reg(dev, BME680_REG_RES_HEAT_BASE+i, &heat_res, 1)) + return false; + } + */ + + debug_dev ("Setting heater ambient temperature done: ambient=%d", + __FUNCTION__, dev, dev->settings.ambient_temperature); + + return true; +} + +bool bme680_set_mode (bme680_sensor_t *dev, uint8_t mode) +{ + if (!dev) return false; + + dev->error_code = BME680_OK; + + uint8_t reg; + + if (!bme680_read_reg(dev, BME680_REG_CTRL_MEAS, ®, 1)) + return false; + + reg = bme_set_reg_bit (reg, BME680_MODE, mode); + + if (!bme680_write_reg(dev, BME680_REG_CTRL_MEAS, ®, 1)) + return false; + + return true; +} + + +/** Functions for internal use only */ + +/** + * @brief Check the chip ID to test whether sensor is available + */ +static bool bme680_is_available (bme680_sensor_t* dev) +{ + uint8_t chip_id; + + if (!dev) return false; + + dev->error_code = BME680_OK; + + if (!bme680_read_reg (dev, BME680_REG_ID, &chip_id, 1)) + return false; + + if (chip_id != 0x61) + { + error_dev ("Chip id %02x is wrong, should be 0x61.", __FUNCTION__, dev, chip_id); + dev->error_code = BME680_WRONG_CHIP_ID; + return false; + } + + return true; +} + + +static bool bme680_reset (bme680_sensor_t* dev) +{ + if (!dev) return false; + + dev->error_code = BME680_OK; + + uint8_t reg = BME680_RESET_CMD; + + // send reset command + if (!bme680_write_reg(dev, BME680_REG_RESET, ®, 1)) + return false; + + // wait the time the sensor needs for reset + bme680_delay_ms (BME680_RESET_PERIOD); + + // check whether the sensor is reachable again + if (!bme680_read_reg(dev, BME680_REG_STATUS, ®, 1)) + { + dev->error_code = BME680_RESET_CMD_FAILED; + return false; + } + + return true; +} + + +/** + * @brief Calculate temperature from raw temperature value + * @ref BME280 datasheet, page 50 + */ +static int16_t bme680_convert_temperature (bme680_sensor_t *dev, uint32_t raw_temperature) +{ + if (!dev) return 0; + + bme680_calib_data_t* cd = &dev->calib_data; + + int64_t var1; + int64_t var2; + int16_t temperature; + + var1 = ((((raw_temperature >> 3) - ((int32_t)cd->par_t1 << 1))) * + ((int32_t)cd->par_t2)) >> 11; + var2 = (((((raw_temperature >> 4) - ((int32_t)cd->par_t1)) * + ((raw_temperature >> 4) - ((int32_t)cd->par_t1))) >> 12) * + ((int32_t)cd->par_t3)) >> 14; + cd->t_fine = (int32_t)(var1 + var2); + temperature = (cd->t_fine * 5 + 128) >> 8; + + return temperature; +} + + +/** + * @brief Calculate pressure from raw pressure value + * @copyright Copyright (C) 2017 - 2018 Bosch Sensortec GmbH + * + * The algorithm was extracted from the original Bosch Sensortec BME680 driver + * published as open source. Divisions and multiplications by potences of 2 + * were replaced by shift operations for effeciency reasons. + * + * @ref [BME680_diver](https://github.com/BoschSensortec/BME680_driver) + * @ref BME280 datasheet, page 50 + */ +static uint32_t bme680_convert_pressure (bme680_sensor_t *dev, uint32_t raw_pressure) +{ + if (!dev) return 0; + + bme680_calib_data_t* cd = &dev->calib_data; + + int32_t var1; + int32_t var2; + int32_t var3; + int32_t var4; + int32_t pressure; + + var1 = (((int32_t) cd->t_fine) >> 1) - 64000; + var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * (int32_t) cd->par_p6) >> 2; + var2 = ((var2) * (int32_t) cd->par_p6) >> 2; + var2 = var2 + ((var1 * (int32_t)cd->par_p5) << 1); + var2 = (var2 >> 2) + ((int32_t) cd->par_p4 << 16); + var1 = (((var1 >> 2) * (var1 >> 2)) >> 13); + var1 = (((var1) * ((int32_t) cd->par_p3 << 5)) >> 3) + (((int32_t) cd->par_p2 * var1) >> 1); + var1 = var1 >> 18; + var1 = ((32768 + var1) * (int32_t) cd->par_p1) >> 15; + pressure = 1048576 - raw_pressure; + pressure = (int32_t)((pressure - (var2 >> 12)) * ((uint32_t)3125)); + var4 = (1 << 31); + pressure = (pressure >= var4) ? (( pressure / (uint32_t) var1) << 1) + : ((pressure << 1) / (uint32_t) var1); + var1 = ((int32_t) cd->par_p9 * (int32_t) (((pressure >> 3) * (pressure >> 3)) >> 13)) >> 12; + var2 = ((int32_t)(pressure >> 2) * (int32_t) cd->par_p8) >> 13; + var3 = ((int32_t)(pressure >> 8) * (int32_t)(pressure >> 8) + * (int32_t)(pressure >> 8) + * (int32_t)cd->par_p10) >> 17; + pressure = (int32_t)(pressure) + ((var1 + var2 + var3 + ((int32_t)cd->par_p7 << 7)) >> 4); + + return (uint32_t) pressure; +} + +/** + * @brief Calculate humidty from raw humidity data + * @copyright Copyright (C) 2017 - 2018 Bosch Sensortec GmbH + * + * The algorithm was extracted from the original Bosch Sensortec BME680 driver + * published as open source. Divisions and multiplications by potences of 2 + * were replaced by shift operations for effeciency reasons. + * + * @ref [BME680_diver](https://github.com/BoschSensortec/BME680_driver) + */ +static uint32_t bme680_convert_humidity (bme680_sensor_t *dev, uint16_t raw_humidity) +{ + if (!dev) return 0; + + bme680_calib_data_t* cd = &dev->calib_data; + + int32_t var1; + int32_t var2; + int32_t var3; + int32_t var4; + int32_t var5; + int32_t var6; + int32_t temp_scaled; + int32_t humidity; + + temp_scaled = (((int32_t) cd->t_fine * 5) + 128) >> 8; + var1 = (int32_t) (raw_humidity - ((int32_t) ((int32_t) cd->par_h1 << 4))) - + (((temp_scaled * (int32_t) cd->par_h3) / ((int32_t) 100)) >> 1); + var2 = ((int32_t) cd->par_h2 * + (((temp_scaled * (int32_t) cd->par_h4) / ((int32_t) 100)) + + (((temp_scaled * ((temp_scaled * (int32_t) cd->par_h5) / ((int32_t) 100))) >> 6) / + ((int32_t) 100)) + (int32_t) (1 << 14))) >> 10; + var3 = var1 * var2; + var4 = (int32_t) cd->par_h6 << 7; + var4 = ((var4) + ((temp_scaled * (int32_t) cd->par_h7) / ((int32_t) 100))) >> 4; + var5 = ((var3 >> 14) * (var3 >> 14)) >> 10; + var6 = (var4 * var5) >> 1; + humidity = (((var3 + var6) >> 10) * ((int32_t) 1000)) >> 12; + + if (humidity > 100000) /* Cap at 100%rH */ + humidity = 100000; + else if (humidity < 0) + humidity = 0; + + return (uint32_t) humidity; +} + + +/** + * @brief Lookup table for gas resitance computation + * @ref BME680 datasheet, page 19 + */ +static float lookup_table[16][2] = { // const1, const2 // gas_range + { 1.0 , 8000000.0 }, // 0 + { 1.0 , 4000000.0 }, // 1 + { 1.0 , 2000000.0 }, // 2 + { 1.0 , 1000000.0 }, // 3 + { 1.0 , 499500.4995 }, // 4 + { 0.99 , 248262.1648 }, // 5 + { 1.0 , 125000.0 }, // 6 + { 0.992, 63004.03226 }, // 7 + { 1.0 , 31281.28128 }, // 8 + { 1.0 , 15625.0 }, // 9 + { 0.998, 7812.5 }, // 10 + { 0.995, 3906.25 }, // 11 + { 1.0 , 1953.125 }, // 12 + { 0.99 , 976.5625 }, // 13 + { 1.0 , 488.28125 }, // 14 + { 1.0 , 244.140625 } // 15 + }; + +/** + * @brief Calculate gas resistance from raw gas resitance value and gas range + * @ref BME680 datasheet + */ +static uint32_t bme680_convert_gas (bme680_sensor_t *dev, uint16_t gas, uint8_t gas_range) +{ + if (!dev) return 0; + + bme680_calib_data_t* cd = &dev->calib_data; + + float var1 = (1340.0 + 5.0 * cd->range_sw_err) * lookup_table[gas_range][0]; + return var1 * lookup_table[gas_range][1] / (gas - 512.0 + var1); +} + +#define msb_lsb_xlsb_to_20bit(t,b,o) (t)((t) b[o] << 12 | (t) b[o+1] << 4 | b[o+2] >> 4) +#define msb_lsb_to_type(t,b,o) (t)(((t)b[o] << 8) | b[o+1]) + +#define BME680_RAW_P_OFF BME680_REG_PRESS_MSB_0-BME680_REG_MEAS_STATUS_0 +#define BME680_RAW_T_OFF (BME680_RAW_P_OFF + BME680_REG_TEMP_MSB_0 - BME680_REG_PRESS_MSB_0) +#define BME680_RAW_H_OFF (BME680_RAW_T_OFF + BME680_REG_HUM_MSB_0 - BME680_REG_TEMP_MSB_0) +#define BME680_RAW_G_OFF (BME680_RAW_H_OFF + BME680_REG_GAS_R_MSB_0 - BME680_REG_HUM_MSB_0) + +static bool bme680_get_raw_data(bme680_sensor_t *dev, bme680_raw_data_t* raw_data) +{ + if (!dev || !raw_data) return false; + + dev->error_code = BME680_OK; + + if (!dev->meas_started) + { + error_dev ("Measurement was not started.", __FUNCTION__, dev); + dev->error_code = BME680_MEAS_NOT_RUNNING; + return false; + } + + uint8_t raw[BME680_REG_RAW_DATA_LEN] = { 0 }; + + if (!(dev->meas_status & BME680_NEW_DATA_BITS)) + { + // read maesurment status from sensor + if (!bme680_read_reg(dev, BME680_REG_MEAS_STATUS_0, raw, 2)) + { + error_dev ("Could not read measurement status from sensor.", __FUNCTION__, dev); + return false; + } + + dev->meas_status = raw[0]; + if (dev->meas_status & BME680_MEASURING_BITS) + { + debug_dev ("Measurement is still running.", __FUNCTION__, dev); + dev->error_code = BME680_MEAS_STILL_RUNNING; + return false; + } + + // test whether there are new data + if (!(dev->meas_status & BME680_NEW_DATA_BITS)) + { + debug_dev ("No new data.", __FUNCTION__, dev); + dev->error_code = BME680_NO_NEW_DATA; + return false; + } + } + + dev->meas_started = false; + raw_data->gas_index = (dev->meas_status & BME680_GAS_MEAS_INDEX_BITS); + + // if there are new data, read raw data from sensor + + if (!bme680_read_reg(dev, BME680_REG_RAW_DATA_0, raw, BME680_REG_RAW_DATA_LEN)) + { + error_dev ("Could not read raw data from sensor.", __FUNCTION__, dev); + return false; + } + + raw_data->gas_valid = bme_get_reg_bit(raw[BME680_RAW_G_OFF+1],BME680_GAS_VALID); + raw_data->heater_stable = bme_get_reg_bit(raw[BME680_RAW_G_OFF+1],BME680_HEAT_STAB_R); + + raw_data->temperature = msb_lsb_xlsb_to_20bit (uint32_t, raw, BME680_RAW_T_OFF); + raw_data->pressure = msb_lsb_xlsb_to_20bit (uint32_t, raw, BME680_RAW_P_OFF); + raw_data->humidity = msb_lsb_to_type (uint16_t, raw, BME680_RAW_H_OFF); + raw_data->gas_resistance = ((uint16_t)raw[BME680_RAW_G_OFF] << 2) | raw[BME680_RAW_G_OFF+1] >> 6; + raw_data->gas_range = raw[BME680_RAW_G_OFF+1] & BME680_GAS_RANGE_R_BITS; + + /* + // These data are not documented and it is not really clear when they are filled + if (!bme680_read_reg(dev, BME680_REG_MEAS_STATUS_1, raw, BME680_REG_RAW_DATA_LEN)) + { + error_dev ("Could not read raw data from sensor.", __FUNCTION__, dev); + return false; + } + + if (!bme680_read_reg(dev, BME680_REG_MEAS_STATUS_2, raw, BME680_REG_RAW_DATA_LEN)) + { + error_dev ("Could not read raw data from sensor.", __FUNCTION__, dev); + return false; + } + */ + debug ("Raw data: %d %d %d %d %d",__FUNCTION__, + raw_data->temperature, raw_data->pressure, + raw_data->humidity, raw_data->gas_resistance, raw_data->gas_range); + + return true; +} + + +/** + * @brief Calculate internal duration representation + * + * Durations are internally representes as one byte + * + * duration = value<5:0> * multiplier<7:6> + * + * where the multiplier is 1, 4, 16, or 64. Maximum duration is therefore + * 64*64 = 4032 ms. The function takes a real world duration value given + * in milliseconds and computes the internal representation. + * + * @ref Datasheet + */ +static uint8_t bme680_heater_duration (uint16_t duration) +{ + uint8_t multiplier = 0; + + while (duration > 63) + { + duration = duration / 4; + multiplier++; + } + return (uint8_t) (duration | (multiplier << 6)); +} + + +/** + * @brief Calculate internal heater resistance value from real temperature. + * + * @ref Datasheet of BME680 + */ +static uint8_t bme680_heater_resistance (const bme680_sensor_t *dev, uint16_t temp) +{ + if (!dev) return 0; + + if (temp < BME680_HEATER_TEMP_MIN) + temp = BME680_HEATER_TEMP_MIN; + else if (temp > BME680_HEATER_TEMP_MAX) + temp = BME680_HEATER_TEMP_MAX; + + const bme680_calib_data_t* cd = &dev->calib_data; + + // from datasheet + double var1; + double var2; + double var3; + double var4; + double var5; + uint8_t res_heat_x; + + var1 = ((double)cd->par_gh1 / 16.0) + 49.0; + var2 = (((double)cd->par_gh2 / 32768.0) * 0.0005) + 0.00235; + var3 = (double)cd->par_gh3 / 1024.0; + var4 = var1 * (1.0 + (var2 * (double) temp)); + var5 = var4 + (var3 * (double)dev->settings.ambient_temperature); + res_heat_x = (uint8_t)(3.4 * ((var5 * (4.0 / (4.0 + (double)cd->res_heat_range)) * + (1.0/(1.0 + ((double)cd->res_heat_val * 0.002)))) - 25)); + return res_heat_x; + +} + + +static void bme680_delay_ms(uint32_t period) +{ + uint32_t start_time = sdk_system_get_time () / 1000; + + vTaskDelay((period + portTICK_PERIOD_MS-1) / portTICK_PERIOD_MS); + + while (sdk_system_get_time()/1000 - start_time < period) + vTaskDelay (1); +} + + +#define BME680_SPI_BUF_SIZE 64 // SPI register data buffer size of ESP866 + +static const spi_settings_t bus_settings = { + .mode = SPI_MODE3, + .freq_divider = SPI_FREQ_DIV_1M, + .msb = true, + .minimal_pins = false, + .endianness = SPI_LITTLE_ENDIAN +}; + + +static bool bme680_read_reg(bme680_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len) +{ + if (!dev || !data) return false; + + return (dev->addr) ? bme680_i2c_read (dev, reg, data, len) + : bme680_spi_read (dev, reg, data, len); +} + + +static bool bme680_write_reg(bme680_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len) +{ + if (!dev || !data) return false; + + return (dev->addr) ? bme680_i2c_write (dev, reg, data, len) + : bme680_spi_write (dev, reg, data, len); +} + +#define BME680_REG_SWITCH_MEM_PAGE BME680_REG_STATUS +#define BME680_BIT_SWITCH_MEM_PAGE_0 0x00 +#define BME680_BIT_SWITCH_MEM_PAGE_1 0x10 + +static bool bme680_spi_set_mem_page (bme680_sensor_t* dev, uint8_t reg) +{ + // mem pages (reg 0x00 .. 0x7f = 1, reg 0x80 ... 0xff = 0 + uint8_t mem_page = (reg < 0x80) ? BME680_BIT_SWITCH_MEM_PAGE_1 + : BME680_BIT_SWITCH_MEM_PAGE_0; + + debug_dev ("Set mem page for register %02x to %d.", __FUNCTION__, dev, reg, mem_page); + + if (!bme680_spi_write (dev, BME680_REG_SWITCH_MEM_PAGE, &mem_page, 1)) + { + dev->error_code |= BME680_SPI_SET_PAGE_FAILED; + return false; + } + // sdk_os_delay_us (100); + + return true; +} + + +static bool bme680_spi_read(bme680_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len) +{ + if (!dev || !data) return false; + + if (len >= BME680_SPI_BUF_SIZE) + { + dev->error_code |= BME680_SPI_BUFFER_OVERFLOW; + error_dev ("Error on read from SPI slave on bus 1. Tried to transfer " + "more than %d byte in one read operation.", + __FUNCTION__, dev, BME680_SPI_BUF_SIZE); + return false; + } + + // set mem page first + if (!bme680_spi_set_mem_page (dev, reg)) + { + error_dev ("Error on read from SPI slave on bus 1. Could not set mem page.", + __FUNCTION__, dev); + return false; + } + + reg &= 0x7f; + reg |= 0x80; + + spi_settings_t old_settings; + + static uint8_t mosi[BME680_SPI_BUF_SIZE]; + static uint8_t miso[BME680_SPI_BUF_SIZE]; + + memset (mosi, 0xff, BME680_SPI_BUF_SIZE); + memset (miso, 0xff, BME680_SPI_BUF_SIZE); + + mosi[0] = reg; + + spi_get_settings(dev->bus, &old_settings); + spi_set_settings(dev->bus, &bus_settings); + gpio_write(dev->spi_cs_pin, false); + + size_t transfered = spi_transfer (dev->bus, (const void*)mosi, (void*)miso, len+1, SPI_8BIT); + + gpio_write(dev->spi_cs_pin, true); + spi_set_settings(dev->bus, &old_settings); + + if (!transfered) + { + error_dev ("Could not read data from SPI", __FUNCTION__, dev); + dev->error_code |= BME680_SPI_READ_FAILED; + return false; + } + + // shift data one by left, first byte received while sending register address is invalid + for (int i=0; i < len; i++) + data[i] = miso[i+1]; + +# ifdef BME680_DEBUG_LEVEL_2 + printf("BME680 %s: read the following bytes: ", __FUNCTION__); + printf("%0x ", reg); + for (int i=0; i < len; i++) + printf("%0x ", data[i]); + printf("\n"); +# endif + + return true; +} + + +static bool bme680_spi_write(bme680_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len) +{ + if (!dev || !data) return false; + + static uint8_t mosi[BME680_SPI_BUF_SIZE]; + + if (len >= BME680_SPI_BUF_SIZE) + { + dev->error_code |= BME680_SPI_BUFFER_OVERFLOW; + error_dev ("Error on write to SPI slave on bus 1. Tried to transfer more" + "than %d byte in one write operation.", __FUNCTION__, dev, BME680_SPI_BUF_SIZE); + + return false; + } + + // set mem page first if not mem page register is used + if (reg != BME680_REG_STATUS && !bme680_spi_set_mem_page (dev, reg)) + { + error_dev ("Error on write from SPI slave on bus 1. Could not set mem page.", + __FUNCTION__, dev); + return false; + } + + reg &= 0x7f; + + // first byte in output is the register address + mosi[0] = reg; + + // shift data one byte right, first byte in output is the register address + for (int i = 0; i < len; i++) + mosi[i+1] = data[i]; + +# ifdef BME680_DEBUG_LEVEL_2 + printf("BME680 %s: Write the following bytes: ", __FUNCTION__); + for (int i = 0; i < len+1; i++) + printf("%0x ", mosi[i]); + printf("\n"); +# endif + + spi_settings_t old_settings; + + spi_get_settings(dev->bus, &old_settings); + spi_set_settings(dev->bus, &bus_settings); + gpio_write(dev->spi_cs_pin, false); + + size_t transfered = spi_transfer (dev->bus, (const void*)mosi, NULL, len+1, SPI_8BIT); + + gpio_write(dev->spi_cs_pin, true); + spi_set_settings(dev->bus, &old_settings); + + if (!transfered) + { + error_dev ("Could not write data to SPI.", __FUNCTION__, dev); + dev->error_code |= BME680_SPI_WRITE_FAILED; + return false; + } + + return true; +} + +static bool bme680_i2c_read(bme680_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len) +{ + if (!dev || !data) return false; + + debug_dev ("Read %d byte from i2c slave register %02x.", __FUNCTION__, dev, len, reg); + + int result = i2c_slave_read(dev->bus, dev->addr, ®, data, len); + + if (result) + { + dev->error_code |= (result == -EBUSY) ? BME680_I2C_BUSY : BME680_I2C_READ_FAILED; + error_dev ("Error %d on read %d byte from I2C slave register %02x.", + __FUNCTION__, dev, result, len, reg); + return false; + } + +# ifdef BME680_DEBUG_LEVEL_2 + printf("BME680 %s: Read following bytes: ", __FUNCTION__); + printf("%0x: ", reg); + for (int i=0; i < len; i++) + printf("%0x ", data[i]); + printf("\n"); +# endif + + return true; +} + + +static bool bme680_i2c_write(bme680_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len) +{ + if (!dev || !data) return false; + + debug_dev ("Write %d byte to i2c slave register %02x.", __FUNCTION__, dev, len, reg); + + int result = i2c_slave_write(dev->bus, dev->addr, ®, data, len); + + if (result) + { + dev->error_code |= (result == -EBUSY) ? BME680_I2C_BUSY : BME680_I2C_WRITE_FAILED; + error_dev ("Error %d on write %d byte to i2c slave register %02x.", + __FUNCTION__, dev, result, len, reg); + return false; + } + +# ifdef BME680_DEBUG_LEVEL_2 + printf("BME680 %s: Wrote the following bytes: ", __FUNCTION__); + printf("%0x: ", reg); + for (int i=0; i < len; i++) + printf("%0x ", data[i]); + printf("\n"); +# endif + + return true; +} diff --git a/extras/bme680/bme680.h b/extras/bme680/bme680.h new file mode 100644 index 0000000..21658f6 --- /dev/null +++ b/extras/bme680/bme680.h @@ -0,0 +1,384 @@ +/* + * 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_MEAS_STILL_RUNNING ( 6 << 8) +#define BME680_FORCE_MODE_FAILED ( 7 << 8) +#define BME680_NO_NEW_DATA ( 8 << 8) +#define BME680_WRONG_HEAT_PROFILE ( 9 << 8) +#define BME680_MEAS_GAS_NOT_VALID (10 << 8) +#define BME680_HEATER_NOT_STABLE (11 << 8) + +// Driver range definitions +#define BME680_HEATER_TEMP_MIN 200 // min. 200 degree Celsius +#define BME680_HEATER_TEMP_MAX 400 // max. 200 degree Celsius +#define BME680_HEATER_PROFILES 10 // max. 10 heater profiles 0 ... 9 +#define BME680_HEATER_NOT_USED -1 // heater not used profile + +#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 the + * the following default settings: + * + * - Oversampling rate for temperature, pressure, humidity is osr_1x + * - Filter size for pressure and temperature is iir_size 3 + * - Heater profile 0 with 320 degree C and 150 ms duration + * + * 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. + * Parameters for the measurement like oversampling rates, IIR filter sizes + * and heater profile can be configured before. + * + * Once the TPHG measurement is started, the user task has to wait for the + * results. The duration of the TPHG measurement can be determined with + * function *bme680_get_measurement_duration*. + * + * @param dev pointer to the sensor device data structure + * @return true on success, false on error + */ +bool bme680_force_measurement (bme680_sensor_t* dev); + + +/** + * @brief Get estimated duration of a TPHG measurement + * + * The function returns an estimated duration of the TPHG measurement cycle + * in RTOS ticks for the current configuration of the sensor. + * + * This duration is the time required by the sensor for one TPHG measurement + * until the results are available. It strongly depends on which measurements + * are performed in the THPG measurement cycle and what configuration + * parameters were set. It can vary from 1 RTOS (10 ms) tick up to 4500 RTOS + * ticks (4.5 seconds). + * + * If the measurement configuration is not changed, the duration can be + * considered as constant. + * + * @param dev pointer to the sensor device data structure + * @return duration of TPHG measurement cycle in ticks or 0 on error + */ +uint32_t bme680_get_measurement_duration (const bme680_sensor_t *dev); + +/** + * @brief Get the measurement status + * + * The function can be used to test whether a measurement that was started + * before is still running. + * + * @param dev pointer to the sensor device data structure + * @return true if measurement is still running or false otherwise + */ +bool 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 it waits + * 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 from 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 from 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 a heater profile for gas measurements + * + * The sensor integrates a heater for the gas measurement. Parameters for this + * heater are defined by so called heater profiles. The sensor supports up to + * 10 heater profiles, which are numbered from 0 to 9. Each profile consists of + * a temperature set-point (the target temperature) and a heating duration. + * + * This function sets the parameters for one of the heater profiles 0 ... 9. + * To activate the gas measurement with this profile, use function + * *bme680_use_heater_profile*, see below. + * + * Please note: According to the data sheet, a 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 profile heater profile 0 ... 9 + * @param temperature target 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, + uint8_t profile, + uint16_t temperature, + uint16_t duration); + +/** + * @brief Activate gas measurement with a given heater profile + * + * The function activates the gas measurement with one of the heater + * profiles 0 ... 9 or deactivates the gas measurement completely when + * -1 or BME680_HEATER_NOT_USED is used as heater profile. + * + * Parameters of the activated heater profile have to be set before with + * function *bme680_set_heater_profile* otherwise the function fails. + * + * If several heater profiles have been defined with function + * *bme680_set_heater_profile*, a sequence of gas measurements with different + * heater parameters can be realized by a sequence of activations of different + * heater profiles for successive TPHG measurements using this function. + * + * @param dev pointer to the sensor device data structure0 * + * @param profile 0 ... 9 to activate or -1 to deactivate gas measure + * @return true on success, false on error + */ +bool bme680_use_heater_profile (bme680_sensor_t* dev, int8_t profile); + +/** + * @brief Set ambient temperature + * + * The heater resistance calculation algorithm takes into account the ambient + * temperature of the sensor. This function can be used to set this ambient + * temperature. Either values determined from the sensor itself or from + * another temperature sensor can be used. The default ambient temperature + * is 25 degree Celsius. + * + * @param dev pointer to the sensor device data structure + * @param temperature ambient temperature in degree Celsius + * @return true on success, false on error + */ +bool bme680_set_ambient_temperature (bme680_sensor_t* dev, + int16_t temperature); + + +#ifdef __cplusplus +} +#endif /* End of CPP guard */ + +#endif /* __BME680_H__ */ diff --git a/extras/bme680/bme680_types.h b/extras/bme680/bme680_types.h new file mode 100644 index 0000000..8234135 --- /dev/null +++ b/extras/bme680/bme680_types.h @@ -0,0 +1,192 @@ +/* + * 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) + + int8_t heater_profile; // Heater profile used (default 0) + uint16_t heater_temperature[10]; // Heater temperature for G (default 320) + uint16_t heater_duration[10]; // 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 { + + 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 + uint8_t meas_status; // last sensor status (for internal use only) + + 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__ */ diff --git a/extras/bme680/component.mk b/extras/bme680/component.mk new file mode 100644 index 0000000..f890ad0 --- /dev/null +++ b/extras/bme680/component.mk @@ -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))