2018-01-05 08:09:05 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Linaro Ltd.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <device.h>
|
2019-06-25 15:53:52 -04:00
|
|
|
#include <drivers/gpio.h>
|
2019-06-25 15:53:54 -04:00
|
|
|
#include <drivers/i2c.h>
|
2018-01-05 08:09:05 -08:00
|
|
|
#include <kernel.h>
|
2019-06-26 10:33:41 -04:00
|
|
|
#include <sys/byteorder.h>
|
2018-01-05 08:09:05 -08:00
|
|
|
#include <misc/util.h>
|
2019-06-25 15:54:00 -04:00
|
|
|
#include <drivers/sensor.h>
|
2019-06-26 10:33:39 -04:00
|
|
|
#include <sys/__assert.h>
|
2018-10-10 12:08:54 +05:30
|
|
|
#include <logging/log.h>
|
2018-01-05 08:09:05 -08:00
|
|
|
|
|
|
|
#include "ccs811.h"
|
|
|
|
|
2018-10-10 12:08:54 +05:30
|
|
|
#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL
|
|
|
|
LOG_MODULE_REGISTER(CCS811);
|
|
|
|
|
2018-01-05 08:09:05 -08:00
|
|
|
static int ccs811_sample_fetch(struct device *dev, enum sensor_channel chan)
|
|
|
|
{
|
|
|
|
struct ccs811_data *drv_data = dev->driver_data;
|
|
|
|
int tries = 11;
|
|
|
|
u16_t buf[4];
|
|
|
|
u8_t status;
|
|
|
|
|
|
|
|
/* Check data ready flag for the measurement interval of 1 seconds */
|
|
|
|
while (tries-- > 0) {
|
2019-06-11 14:20:32 -05:00
|
|
|
if (i2c_reg_read_byte(drv_data->i2c, DT_INST_0_AMS_CCS811_BASE_ADDRESS,
|
2018-01-05 08:09:05 -08:00
|
|
|
CCS811_REG_STATUS, &status) < 0) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Failed to read Status register");
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((status & CCS811_STATUS_DATA_READY) || tries == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_sleep(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(status & CCS811_STATUS_DATA_READY)) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Sensor data not available");
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:20:32 -05:00
|
|
|
if (i2c_burst_read(drv_data->i2c, DT_INST_0_AMS_CCS811_BASE_ADDRESS,
|
2018-01-05 08:09:05 -08:00
|
|
|
CCS811_REG_ALG_RESULT_DATA, (u8_t *)buf, 8) < 0) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Failed to read conversion data.");
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
drv_data->co2 = sys_be16_to_cpu(buf[0]);
|
|
|
|
drv_data->voc = sys_be16_to_cpu(buf[1]);
|
|
|
|
drv_data->status = buf[2] & 0xff;
|
|
|
|
drv_data->error = buf[2] >> 8;
|
|
|
|
drv_data->resistance = sys_be16_to_cpu(buf[3]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ccs811_channel_get(struct device *dev,
|
|
|
|
enum sensor_channel chan,
|
|
|
|
struct sensor_value *val)
|
|
|
|
{
|
|
|
|
struct ccs811_data *drv_data = dev->driver_data;
|
|
|
|
u32_t uval;
|
|
|
|
|
|
|
|
switch (chan) {
|
|
|
|
case SENSOR_CHAN_CO2:
|
2018-02-24 16:25:56 +02:00
|
|
|
val->val1 = drv_data->co2;
|
|
|
|
val->val2 = 0;
|
2018-01-05 08:09:05 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
case SENSOR_CHAN_VOC:
|
2018-02-24 16:25:56 +02:00
|
|
|
val->val1 = drv_data->voc;
|
|
|
|
val->val2 = 0;
|
2018-01-05 08:09:05 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
case SENSOR_CHAN_VOLTAGE:
|
|
|
|
/*
|
2018-02-24 16:25:56 +02:00
|
|
|
* Raw ADC readings are contained in least significant 10 bits
|
2018-01-05 08:09:05 -08:00
|
|
|
*/
|
|
|
|
uval = (drv_data->resistance & CCS811_VOLTAGE_MASK)
|
|
|
|
* CCS811_VOLTAGE_SCALE;
|
2019-03-26 19:57:45 -06:00
|
|
|
val->val1 = uval / 1000000U;
|
2018-01-05 08:09:05 -08:00
|
|
|
val->val2 = uval % 1000000;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case SENSOR_CHAN_CURRENT:
|
|
|
|
/*
|
|
|
|
* Current readings are contained in most
|
|
|
|
* significant 6 bits in microAmps
|
|
|
|
*/
|
|
|
|
uval = drv_data->resistance >> 10;
|
2019-03-26 19:57:45 -06:00
|
|
|
val->val1 = uval / 1000000U;
|
2018-01-05 08:09:05 -08:00
|
|
|
val->val2 = uval % 1000000;
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct sensor_driver_api ccs811_driver_api = {
|
|
|
|
.sample_fetch = ccs811_sample_fetch,
|
|
|
|
.channel_get = ccs811_channel_get,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int switch_to_app_mode(struct device *i2c)
|
|
|
|
{
|
|
|
|
u8_t status, buf;
|
|
|
|
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_DBG("Switching to Application mode...");
|
2018-01-05 08:09:05 -08:00
|
|
|
|
2019-06-11 14:20:32 -05:00
|
|
|
if (i2c_reg_read_byte(i2c, DT_INST_0_AMS_CCS811_BASE_ADDRESS,
|
2018-01-05 08:09:05 -08:00
|
|
|
CCS811_REG_STATUS, &status) < 0) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Failed to read Status register");
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for the application firmware */
|
|
|
|
if (!(status & CCS811_STATUS_APP_VALID)) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("No Application firmware loaded");
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = CCS811_REG_APP_START;
|
|
|
|
/* Set the device to application mode */
|
2019-06-11 14:20:32 -05:00
|
|
|
if (i2c_write(i2c, &buf, 1, DT_INST_0_AMS_CCS811_BASE_ADDRESS) < 0) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Failed to set Application mode");
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:20:32 -05:00
|
|
|
if (i2c_reg_read_byte(i2c, DT_INST_0_AMS_CCS811_BASE_ADDRESS,
|
2018-01-05 08:09:05 -08:00
|
|
|
CCS811_REG_STATUS, &status) < 0) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Failed to read Status register");
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for application mode */
|
|
|
|
if (!(status & CCS811_STATUS_FW_MODE)) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Failed to start Application firmware");
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_DBG("CCS811 Application firmware started!");
|
2018-01-05 08:09:05 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ccs811_init(struct device *dev)
|
|
|
|
{
|
|
|
|
struct ccs811_data *drv_data = dev->driver_data;
|
|
|
|
int ret;
|
|
|
|
u8_t hw_id, status;
|
|
|
|
|
2019-06-11 14:20:32 -05:00
|
|
|
drv_data->i2c = device_get_binding(DT_INST_0_AMS_CCS811_BUS_NAME);
|
2018-01-05 08:09:05 -08:00
|
|
|
if (drv_data->i2c == NULL) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Failed to get pointer to %s device!",
|
2019-06-11 14:20:32 -05:00
|
|
|
DT_INST_0_AMS_CCS811_BUS_NAME);
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:16:38 +02:00
|
|
|
#if defined(CONFIG_CCS811_GPIO_WAKEUP) || defined(CONFIG_CCS811_GPIO_RESET)
|
2018-01-05 08:09:05 -08:00
|
|
|
drv_data->gpio = device_get_binding(CONFIG_CCS811_GPIO_DEV_NAME);
|
|
|
|
if (drv_data->gpio == NULL) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Failed to get pointer to %s device!",
|
2018-01-05 08:09:05 -08:00
|
|
|
CONFIG_CCS811_GPIO_DEV_NAME);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-03-15 18:16:38 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_CCS811_GPIO_RESET
|
|
|
|
gpio_pin_configure(drv_data->gpio, CONFIG_CCS811_GPIO_RESET_PIN_NUM,
|
|
|
|
GPIO_DIR_OUT);
|
|
|
|
gpio_pin_write(drv_data->gpio, CONFIG_CCS811_GPIO_RESET_PIN_NUM, 1);
|
2018-01-05 08:09:05 -08:00
|
|
|
|
2018-03-15 18:16:38 +02:00
|
|
|
k_sleep(1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wakeup pin should be pulled low before initiating any I2C transfer.
|
|
|
|
* If it has been tied to GND by default, skip this part.
|
|
|
|
*/
|
|
|
|
#ifdef CONFIG_CCS811_GPIO_WAKEUP
|
2018-01-05 08:09:05 -08:00
|
|
|
gpio_pin_configure(drv_data->gpio, CONFIG_CCS811_GPIO_WAKEUP_PIN_NUM,
|
|
|
|
GPIO_DIR_OUT);
|
|
|
|
gpio_pin_write(drv_data->gpio, CONFIG_CCS811_GPIO_WAKEUP_PIN_NUM, 0);
|
|
|
|
|
|
|
|
k_sleep(1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Switch device to application mode */
|
|
|
|
ret = switch_to_app_mode(drv_data->i2c);
|
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check Hardware ID */
|
2019-06-11 14:20:32 -05:00
|
|
|
if (i2c_reg_read_byte(drv_data->i2c, DT_INST_0_AMS_CCS811_BASE_ADDRESS,
|
2018-01-05 08:09:05 -08:00
|
|
|
CCS811_REG_HW_ID, &hw_id) < 0) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Failed to read Hardware ID register");
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hw_id != CCS881_HW_ID) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Hardware ID mismatch!");
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set Measurement mode for 1 second */
|
2019-06-11 14:20:32 -05:00
|
|
|
if (i2c_reg_write_byte(drv_data->i2c, DT_INST_0_AMS_CCS811_BASE_ADDRESS,
|
2018-01-05 08:09:05 -08:00
|
|
|
CCS811_REG_MEAS_MODE,
|
|
|
|
CCS811_MODE_IAQ_1SEC) < 0) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Failed to set Measurement mode");
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for error */
|
2019-06-11 14:20:32 -05:00
|
|
|
if (i2c_reg_read_byte(drv_data->i2c, DT_INST_0_AMS_CCS811_BASE_ADDRESS,
|
2018-01-05 08:09:05 -08:00
|
|
|
CCS811_REG_STATUS, &status) < 0) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Failed to read Status register");
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status & CCS811_STATUS_ERROR) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_ERR("Error occurred during sensor configuration");
|
2018-01-05 08:09:05 -08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ccs811_data ccs811_driver;
|
|
|
|
|
2019-06-11 14:20:32 -05:00
|
|
|
DEVICE_AND_API_INIT(ccs811, DT_INST_0_AMS_CCS811_LABEL, ccs811_init, &ccs811_driver,
|
2018-01-05 08:09:05 -08:00
|
|
|
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
|
|
|
|
&ccs811_driver_api);
|