drivers: Added support for BME680

drivers/sensors/bme680.c: The bme680 driver
sensor.h: Added new type of sensor (Gas) to be used for retrieving the bme680 data
esp32/common/src/esp32_bme680.c: bme680 support on esp32
esp32/esp32-sparrow_kit/esp32_bringup.c: added support for the bme680
esp32/esp32-sparrow-kit/configs/nsh/defconfig: fixed defconfig ci problem

Signed-off-by: simonatoaca <simona.alexandra2000@gmail.com>

esp32-sparrow-kit: Fixed defconfig

Signed-off-by: simonatoaca <simona.alexandra2000@gmail.com>

Code is now C89 compatible

Signed-off-by: simonatoaca <simona.alexandra2000@gmail.com>

Reused already defined sensor data structs by registering every sub-sensor separately

Signed-off-by: simonatoaca <simona.alexandra2000@gmail.com>

Implemented suggestions

Signed-off-by: simonatoaca <simona.alexandra2000@gmail.com>
This commit is contained in:
simonatoaca 2023-07-14 15:57:13 +03:00 committed by Alan Carvalho de Assis
parent dcf3f9de75
commit 9794dc118f
12 changed files with 2173 additions and 1 deletions

View File

@ -0,0 +1,85 @@
/****************************************************************************
* boards/xtensa/esp32/common/include/esp32_bme680.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BME680_H
#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BME680_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Type Definitions
****************************************************************************/
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Inline Functions
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: board_bme680_initialize
*
* Description:
* Initialize and register the BME680 BME680 Temperature, Pressure,
* Humidity and Gas Resistance sensor.
*
* Input Parameters:
* devno - The device number, used to build the device path as /dev/pressN
* busno - The I2C bus number
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int board_bme680_initialize(int devno, int busno);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BME680_H */

View File

@ -80,6 +80,10 @@ ifeq ($(CONFIG_SENSORS_BMP280),y)
CSRCS += esp32_bmp280.c
endif
ifeq ($(CONFIG_SENSORS_BME680),y)
CSRCS += esp32_bme680.c
endif
ifeq ($(CONFIG_SENSORS_SHT3X),y)
CSRCS += esp32_sht3x.c
endif

View File

@ -0,0 +1,87 @@
/****************************************************************************
* boards/xtensa/esp32/common/src/esp32_bme680.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/sensors/bme680.h>
#include <nuttx/i2c/i2c_master.h>
#include "esp32_board_i2c.h"
#include "esp32_i2c.h"
#include "esp32_ltr308.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: board_bme680_initialize
*
* Description:
* Initialize and register the BME680 Temperature, Pressure, Humidity
* and Gas Resistance sensor.
*
* Input Parameters:
* devno - The device number, used to build the device path as
* /dev/uorb/sensor_gas0
* busno - The I2C bus number
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int board_bme680_initialize(int devno, int busno)
{
struct i2c_master_s *i2c;
int ret;
sninfo("Initializing BME680!\n");
/* Initialize BME680 */
i2c = esp32_i2cbus_initialize(busno);
if (i2c != NULL)
{
/* Then try to register the gas sensor in one of the two I2C
* available controllers.
*/
ret = bme680_register(devno, i2c);
if (ret < 0)
{
snerr("ERROR: Error registering BME680 in I2C%d\n", busno);
}
}
else
{
ret = -ENODEV;
}
return ret;
}

View File

@ -22,6 +22,7 @@ CONFIG_ARCH_CHIP_ESP32=y
CONFIG_ARCH_CHIP_ESP32WROVER=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_ARCH_XTENSA=y
CONFIG_BME680_ENABLE_IIR_FILTER=y
CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y
CONFIG_DRIVERS_VIDEO=y
@ -76,6 +77,7 @@ CONFIG_RGBLED_PWM_FREQ=200
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_WAITPID=y
CONFIG_SENSORS=y
CONFIG_SENSORS_BME680=y
CONFIG_SENSORS_LTR308=y
CONFIG_START_DAY=6
CONFIG_START_MONTH=12

View File

@ -84,6 +84,10 @@
# include "esp32_bmp180.h"
#endif
#ifdef CONFIG_SENSORS_BME680
# include "esp32_bme680.h"
#endif
#ifdef CONFIG_SENSORS_LTR308
# include "esp32_ltr308.h"
#endif
@ -350,6 +354,17 @@ int esp32_bringup(void)
}
#endif
#ifdef CONFIG_SENSORS_BME680
/* Try to register BME680 device in I2C0 */
ret = board_bme680_initialize(0, 0);
if (ret < 0)
{
syslog(LOG_ERR, "Failed to initialize BME680 driver: %d\n", ret);
}
#endif
#ifdef CONFIG_SENSORS_LTR308
/* Try to register LTR308 device in I2C0 */

View File

@ -133,6 +133,10 @@ if(CONFIG_SENSORS)
list(APPEND SRCS bmp280.c)
endif()
if(CONFIG_SENSORS_BME680)
list(APPEND SRCS bme680.c)
endif()
if(CONFIG_SENSORS_ISL29023)
list(APPEND SRCS isl29023.c)
endif()

View File

@ -240,6 +240,63 @@ config BMP280_I2C_FREQUENCY
endif # SENSORS_BMP280
config SENSORS_BME680
bool "Bosch BME680 Temperature, Gas, Humidity and Pressure Sensor"
default n
select I2C
---help---
Enable driver for the Bosch BME680 sensor.
config BME680_I2C_FREQUENCY
int "BME680 I2C frequency"
depends on SENSORS_BME680
default 400000
config BME680_DISABLE_PRESS_MEAS
bool "Disable Pressure Measurement"
depends on SENSORS_BME680
default n
---help---
If enabled, the sensor will not measure pressure.
config BME680_DISABLE_HUM_MEAS
bool "Disable Humidity Measurement"
depends on SENSORS_BME680
default n
---help---
If enabled, the sensor will not measure humidity.
config BME680_DISABLE_GAS_MEAS
bool "Disable Gas Measurement"
depends on SENSORS_BME680
default n
---help---
If enabled, the sensor will not measure gas.
config BME680_ENABLE_IIR_FILTER
bool "Enable IIR Filter"
depends on SENSORS_BME680
default n
---help---
Enable IIR filtering for temperature and pressure
measurements.
config SENSORS_BME680_POLL_INTERVAL
int "Polling interval in microseconds, default 3 sec"
depends on SENSORS_BME680
default 3000000
range 0 4294967295
---help---
The interval until a new sensor measurement will be triggered.
config SENSORS_BME680_THREAD_STACKSIZE
int "Worker thread stack size"
depends on SENSORS_BME680
default 1024
---help---
The stack size for the worker thread.
config SENSORS_DHTXX
bool "DHTxx humidity/temperature Sensor support"
default n

View File

@ -148,6 +148,10 @@ ifeq ($(CONFIG_SENSORS_BMP280),y)
CSRCS += bmp280.c
endif
ifeq ($(CONFIG_SENSORS_BME680),y)
CSRCS += bme680.c
endif
ifeq ($(CONFIG_SENSORS_ISL29023),y)
CSRCS += isl29023.c
endif

1764
drivers/sensors/bme680.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -157,6 +157,7 @@ static const struct sensor_info_s g_sensor_info[] =
{sizeof(struct sensor_gps_satellite), "gps_satellite"},
{sizeof(struct sensor_wake_gesture), "wake_gesture"},
{sizeof(struct sensor_cap), "cap"},
{sizeof(struct sensor_gas), "gas"}
};
static const struct file_operations g_sensor_fops =

View File

@ -0,0 +1,136 @@
/****************************************************************************
* include/nuttx/sensors/bme680.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_SENSORS_BME680_H
#define __INCLUDE_NUTTX_SENSORS_BME680_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_BME680)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* Oversampling settings */
#define BME680_OS_SKIPPED (0x00) /* Output set to 0x8000 */
#define BME680_OS_1X (0x01)
#define BME680_OS_2X (0x02)
#define BME680_OS_4X (0x03)
#define BME680_OS_8X (0x04)
#define BME680_OS_16X (0x05)
/* IIR filter settings */
#define BME680_FILTER_COEF0 (0)
#define BME680_FILTER_COEF1 (1)
#define BME680_FILTER_COEF3 (2)
#define BME680_FILTER_COEF7 (3)
#define BME680_FILTER_COEF15 (4)
#define BME680_FILTER_COEF31 (5)
#define BME680_FILTER_COEF63 (6)
#define BME680_FILTER_COEF127 (7)
/* Prerequisites:
*
* CONFIG_BME680
* Enables support for the BME680 driver
*/
/****************************************************************************
* Public Types
****************************************************************************/
struct i2c_master_s;
struct bme680_config_s
{
/* Oversampling settings */
uint8_t temp_os;
#ifndef CONFIG_BME680_DISABLE_PRESS_MEAS
uint8_t press_os;
#endif
#ifndef CONFIG_BME680_DISABLE_HUM_MEAS
uint8_t hum_os;
#endif
#ifdef CONFIG_BME680_ENABLE_IIR_FILTER
/* Filter coefficient */
uint8_t filter_coef;
#endif
#ifndef CONFIG_BME680_DISABLE_GAS_MEAS
/* Gas settings */
int16_t target_temp; /* degrees Celsius */
uint16_t heater_duration; /* ms */
uint8_t nb_conv;
int16_t amb_temp; /* degrees Celsius */
#endif
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: bme680_register
*
* Description:
* Register the BME680 character device
*
* Input Parameters:
* devno - Instance number for driver
* i2c - An instance of the I2C interface to use to communicate with
* BME680
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int bme680_register(int devno, FAR struct i2c_master_s *i2c);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* CONFIG_I2C && CONFIG_SENSORS_BME680 */
#endif /* __INCLUDE_NUTTX_BME680_H */

View File

@ -290,9 +290,16 @@
#define SENSOR_TYPE_CAP 32
/* Gas sensor
* This sensor measures the gas resistance, indicating the presence
* of volatile organic compounds in the air.
*/
#define SENSOR_TYPE_GAS 33
/* The total number of sensor */
#define SENSOR_TYPE_COUNT 33
#define SENSOR_TYPE_COUNT 34
/* The additional sensor open flags */
@ -595,6 +602,12 @@ struct sensor_cap /* Type: Capacitance */
int32_t rawdata[4]; /* in SI units pF */
};
struct sensor_gas /* Type: Gas */
{
uint64_t timestamp; /* Units is microseconds */
float gas_resistance; /* Gas resistance in kOhm */
};
/* The sensor lower half driver interface */
struct sensor_lowerhalf_s;