From af0c69fb9f0ee13c1ad794a79b90890a2a5d3416 Mon Sep 17 00:00:00 2001 From: "Peter A. Bigot" Date: Thu, 22 Nov 2018 16:10:48 -0600 Subject: [PATCH] drivers: sensor: ccs811: add interface for BASELINE management Proper use of the CCS811 requires maintenance of the BASELINE register value measured in clean air at various points in the sensor life cycle. The sensor driver abstraction has no facility to support this, so add an application header with functions to fetch and update the register value. Signed-off-by: Peter A. Bigot --- drivers/sensor/ccs811/ccs811.c | 36 +++++++++++++++++++- drivers/sensor/ccs811/ccs811.h | 1 + include/drivers/sensor/ccs811.h | 58 +++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 include/drivers/sensor/ccs811.h diff --git a/drivers/sensor/ccs811/ccs811.c b/drivers/sensor/ccs811/ccs811.c index 69d94783c62..197f0c067c0 100644 --- a/drivers/sensor/ccs811/ccs811.c +++ b/drivers/sensor/ccs811/ccs811.c @@ -70,6 +70,40 @@ static inline u8_t error_from_status(int status) return status >> 8; } +int ccs811_baseline_fetch(struct device *dev) +{ + const u8_t cmd = CCS811_REG_BASELINE; + struct ccs811_data *drv_data = dev->driver_data; + int rc; + u16_t baseline; + + set_wake(drv_data, true); + + rc = i2c_write_read(drv_data->i2c, DT_INST_0_AMS_CCS811_BASE_ADDRESS, + &cmd, sizeof(cmd), + (u8_t *)&baseline, sizeof(baseline)); + set_wake(drv_data, false); + if (rc <= 0) { + rc = baseline; + } + return rc; +} + +int ccs811_baseline_update(struct device *dev, + u16_t baseline) +{ + struct ccs811_data *drv_data = dev->driver_data; + u8_t buf[1 + sizeof(baseline)]; + int rc; + + buf[0] = CCS811_REG_BASELINE; + memcpy(buf + 1, &baseline, sizeof(baseline)); + set_wake(drv_data, true); + rc = i2c_write(drv_data->i2c, buf, sizeof(buf), DT_INST_0_AMS_CCS811_BASE_ADDRESS); + set_wake(drv_data, false); + return rc; +} + static int ccs811_sample_fetch(struct device *dev, enum sensor_channel chan) { struct ccs811_data *drv_data = dev->driver_data; @@ -285,7 +319,7 @@ int ccs811_set_thresholds(struct device *dev) #endif /* CONFIG_CCS811_TRIGGER */ -int ccs811_init(struct device *dev) +static int ccs811_init(struct device *dev) { struct ccs811_data *drv_data = dev->driver_data; int ret = 0; diff --git a/drivers/sensor/ccs811/ccs811.h b/drivers/sensor/ccs811/ccs811.h index 30390141ab2..21293dd060f 100644 --- a/drivers/sensor/ccs811/ccs811.h +++ b/drivers/sensor/ccs811/ccs811.h @@ -18,6 +18,7 @@ #define CCS811_REG_ALG_RESULT_DATA 0x02 #define CCS811_REG_RAW_DATA 0x03 #define CCS811_REG_THRESHOLDS 0x10 +#define CCS811_REG_BASELINE 0x11 #define CCS811_REG_HW_ID 0x20 #define CCS811_REG_HW_VERSION 0x21 #define CCS811_REG_HW_VERSION_MASK 0xF0 diff --git a/include/drivers/sensor/ccs811.h b/include/drivers/sensor/ccs811.h new file mode 100644 index 00000000000..eefcf103af5 --- /dev/null +++ b/include/drivers/sensor/ccs811.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2018 Peter Bigot Consulting, LLC + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Extended public API for CCS811 Indoor Air Quality Sensor + * + * Some capabilities and operational requirements for this sensor + * cannot be expressed within the sensor driver abstraction. + */ + +#ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_CCS811_H_ +#define ZEPHYR_INCLUDE_DRIVERS_SENSOR_CCS811_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/** + * @brief Fetch the current value of the BASELINE register. + * + * The BASELINE register encodes data used to correct sensor readings + * based on individual device configuration and variation over time. + * + * For proper management of the BASELINE register see AN000370 + * "Baseline Save and Restore on CCS811". + * + * @param dev Pointer to the sensor device + * + * @return a non-negative 16-bit register value, or a negative errno + * code on failure. + */ +int ccs811_baseline_fetch(struct device *dev); + +/** + * @brief Update the BASELINE register. + * + * For proper management of the BASELINE register see AN000370 + * "Baseline Save and Restore on CCS811". + * + * @param dev Pointer to the sensor device + * + * @param baseline the value to be stored in the BASELINE register. + * + * @return 0 if successful, negative errno code if failure. + */ +int ccs811_baseline_update(struct device *dev, u16_t baseline); + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_CCS811_H_ */