drivers: sensor: ccs811: make measurement drive mode configurable

Allow application to choose the measurement rate.

Measurement mode 4 (250 ms rate) is not supported because it we have no
support for processing the raw data it produces.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
This commit is contained in:
Peter A. Bigot 2018-11-20 06:05:38 -06:00 committed by Maureen Helm
commit 59d41669b9
2 changed files with 47 additions and 5 deletions

View file

@ -1,10 +1,36 @@
# CCS811 Digital Gas sensor configuration options # CCS811 Digital Gas sensor configuration options
# Copyright (c) 2018 Linaro Ltd. # Copyright (c) 2018 Linaro Ltd.
# Copyright (c) 2018 Peter Bigot Consulting, LLC
#
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
config CCS811 menuconfig CCS811
bool "CCS811 Digital Gas Sensor" bool "CCS811 Digital Gas Sensor"
depends on I2C && HAS_DTS_I2C depends on I2C && HAS_DTS_I2C
help help
Enable driver for CCS811 Gas sensors. Enable driver for CCS811 Gas sensors.
if CCS811
choice CCS811_DRIVE_MODE
prompt "Measurement drive mode"
default CCS811_DRIVE_MODE_1
help
Specifies the measurement rate used by the driver.
config CCS811_DRIVE_MODE_0
bool "Measurements disabled"
config CCS811_DRIVE_MODE_1
bool "Measurement every second"
config CCS811_DRIVE_MODE_2
bool "Measurement every ten seconds"
config CCS811_DRIVE_MODE_3
bool "Measurement every sixty seconds"
endchoice
endif # CCS811

View file

@ -37,12 +37,20 @@ static void set_wake(struct ccs811_data *drv_data, bool enable)
static int ccs811_sample_fetch(struct device *dev, enum sensor_channel chan) static int ccs811_sample_fetch(struct device *dev, enum sensor_channel chan)
{ {
struct ccs811_data *drv_data = dev->driver_data; struct ccs811_data *drv_data = dev->driver_data;
int tries = 11; int tries;
u16_t buf[4]; u16_t buf[4];
u8_t status; u8_t status;
int rv = -EIO; int rv = -EIO;
/* Check data ready flag for the measurement interval of 1 seconds */ /* Check data ready flag for the measurement interval */
#ifdef CONFIG_CCS811_DRIVE_MODE_1
tries = 11;
#elif defined(CONFIG_CCS811_DRIVE_MODE_2)
tries = 101;
#elif defined(CONFIG_CCS811_DRIVE_MODE_3)
tries = 601;
#endif
while (tries-- > 0) { while (tries-- > 0) {
set_wake(drv_data, true); set_wake(drv_data, true);
if (i2c_reg_read_byte(drv_data->i2c, DT_INST_0_AMS_CCS811_BASE_ADDRESS, if (i2c_reg_read_byte(drv_data->i2c, DT_INST_0_AMS_CCS811_BASE_ADDRESS,
@ -258,10 +266,18 @@ int ccs811_init(struct device *dev)
goto out; goto out;
} }
/* Set Measurement mode for 1 second */ /* Configure measurement mode */
u8_t meas_mode = 0;
#ifdef CONFIG_CCS811_DRIVE_MODE_1
meas_mode = 0x10;
#elif defined(CONFIG_CCS811_DRIVE_MODE_2)
meas_mode = 0x20;
#elif defined(CONFIG_CCS811_DRIVE_MODE_3)
meas_mode = 0x30;
#endif
if (i2c_reg_write_byte(drv_data->i2c, DT_INST_0_AMS_CCS811_BASE_ADDRESS, if (i2c_reg_write_byte(drv_data->i2c, DT_INST_0_AMS_CCS811_BASE_ADDRESS,
CCS811_REG_MEAS_MODE, CCS811_REG_MEAS_MODE,
CCS811_MODE_IAQ_1SEC) < 0) { meas_mode) < 0) {
LOG_ERR("Failed to set Measurement mode"); LOG_ERR("Failed to set Measurement mode");
ret = -EIO; ret = -EIO;
goto out; goto out;