sensors: ccs811: update to new API
Update devicetree sources and bindings, switch to new GPIO API. Use devicetree property name to identify interrupt signal. Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
This commit is contained in:
parent
988a681a16
commit
acb43b66da
6 changed files with 99 additions and 66 deletions
|
@ -121,9 +121,9 @@
|
||||||
compatible = "ams,ccs811";
|
compatible = "ams,ccs811";
|
||||||
reg = <0x5a>;
|
reg = <0x5a>;
|
||||||
label = "CCS811";
|
label = "CCS811";
|
||||||
irq-gpios = <&gpio0 22 0>;
|
irq-gpios = <&gpio0 22 GPIO_ACTIVE_LOW>;
|
||||||
reset-gpios = <&sx1509b 11 0>;
|
reset-gpios = <&sx1509b 11 GPIO_ACTIVE_LOW>;
|
||||||
wake-gpios = <&sx1509b 12 0>;
|
wake-gpios = <&sx1509b 12 GPIO_ACTIVE_LOW>;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -17,13 +17,15 @@
|
||||||
|
|
||||||
#include "ccs811.h"
|
#include "ccs811.h"
|
||||||
|
|
||||||
|
#define WAKE_PIN DT_INST_0_AMS_CCS811_WAKE_GPIOS_PIN
|
||||||
|
#define RESET_PIN DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(CCS811, CONFIG_SENSOR_LOG_LEVEL);
|
LOG_MODULE_REGISTER(CCS811, CONFIG_SENSOR_LOG_LEVEL);
|
||||||
|
|
||||||
#ifdef DT_INST_0_AMS_CCS811_WAKE_GPIOS_CONTROLLER
|
#ifdef DT_INST_0_AMS_CCS811_WAKE_GPIOS_CONTROLLER
|
||||||
static void set_wake(struct ccs811_data *drv_data, bool enable)
|
static void set_wake(struct ccs811_data *drv_data, bool enable)
|
||||||
{
|
{
|
||||||
/* Always active-low */
|
gpio_pin_set(drv_data->wake_gpio, WAKE_PIN, enable);
|
||||||
gpio_pin_write(drv_data->wake_gpio, DT_INST_0_AMS_CCS811_WAKE_GPIOS_PIN, !enable);
|
|
||||||
if (enable) {
|
if (enable) {
|
||||||
k_busy_wait(50); /* t_WAKE = 50 us */
|
k_busy_wait(50); /* t_WAKE = 50 us */
|
||||||
} else {
|
} else {
|
||||||
|
@ -455,8 +457,9 @@ static int ccs811_init(struct device *dev)
|
||||||
* any I2C transfer. If it has been tied to GND by
|
* any I2C transfer. If it has been tied to GND by
|
||||||
* default, skip this part.
|
* default, skip this part.
|
||||||
*/
|
*/
|
||||||
gpio_pin_configure(drv_data->wake_gpio, DT_INST_0_AMS_CCS811_WAKE_GPIOS_PIN,
|
gpio_pin_configure(drv_data->wake_gpio, WAKE_PIN,
|
||||||
GPIO_DIR_OUT);
|
GPIO_OUTPUT_INACTIVE
|
||||||
|
| DT_INST_0_AMS_CCS811_WAKE_GPIOS_FLAGS);
|
||||||
|
|
||||||
set_wake(drv_data, true);
|
set_wake(drv_data, true);
|
||||||
k_sleep(1);
|
k_sleep(1);
|
||||||
|
@ -468,16 +471,16 @@ static int ccs811_init(struct device *dev)
|
||||||
DT_INST_0_AMS_CCS811_RESET_GPIOS_CONTROLLER);
|
DT_INST_0_AMS_CCS811_RESET_GPIOS_CONTROLLER);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
gpio_pin_configure(drv_data->reset_gpio, DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN,
|
gpio_pin_configure(drv_data->reset_gpio, RESET_PIN,
|
||||||
GPIO_DIR_OUT);
|
GPIO_OUTPUT_ACTIVE
|
||||||
gpio_pin_write(drv_data->reset_gpio, DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN, 1);
|
| DT_INST_0_AMS_CCS811_RESET_GPIOS_FLAGS);
|
||||||
|
|
||||||
k_sleep(1);
|
k_sleep(1);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef DT_INST_0_AMS_CCS811_IRQ_GPIOS_CONTROLLER
|
#ifdef DT_INST_0_AMS_CCS811_IRQ_GPIOS_CONTROLLER
|
||||||
drv_data->int_gpio = device_get_binding(DT_INST_0_AMS_CCS811_IRQ_GPIOS_CONTROLLER);
|
drv_data->irq_gpio = device_get_binding(DT_INST_0_AMS_CCS811_IRQ_GPIOS_CONTROLLER);
|
||||||
if (drv_data->int_gpio == NULL) {
|
if (drv_data->irq_gpio == NULL) {
|
||||||
LOG_ERR("Failed to get pointer to INT device: %s",
|
LOG_ERR("Failed to get pointer to INT device: %s",
|
||||||
DT_INST_0_AMS_CCS811_IRQ_GPIOS_CONTROLLER);
|
DT_INST_0_AMS_CCS811_IRQ_GPIOS_CONTROLLER);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
@ -488,10 +491,10 @@ static int ccs811_init(struct device *dev)
|
||||||
* and validating any errors or configuration inconsistencies
|
* and validating any errors or configuration inconsistencies
|
||||||
* after a reset that left the device running.
|
* after a reset that left the device running.
|
||||||
*/
|
*/
|
||||||
#ifdef DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN
|
#ifdef DT_INST_0_AMS_CCS811_RESET_GPIOS_CONTROLLER
|
||||||
gpio_pin_write(drv_data->reset_gpio, DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN, 0);
|
gpio_pin_set(drv_data->reset_gpio, RESET_PIN, 1);
|
||||||
k_busy_wait(15); /* t_RESET */
|
k_busy_wait(15); /* t_RESET */
|
||||||
gpio_pin_write(drv_data->reset_gpio, DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN, 1);
|
gpio_pin_set(drv_data->reset_gpio, RESET_PIN, 0);
|
||||||
#else
|
#else
|
||||||
{
|
{
|
||||||
static u8_t const reset_seq[] = {
|
static u8_t const reset_seq[] = {
|
||||||
|
|
|
@ -50,8 +50,10 @@
|
||||||
struct ccs811_data {
|
struct ccs811_data {
|
||||||
struct device *i2c;
|
struct device *i2c;
|
||||||
#ifdef DT_INST_0_AMS_CCS811_IRQ_GPIOS_CONTROLLER
|
#ifdef DT_INST_0_AMS_CCS811_IRQ_GPIOS_CONTROLLER
|
||||||
struct device *int_gpio;
|
struct device *irq_gpio;
|
||||||
#ifdef CONFIG_CCS811_TRIGGER
|
#ifdef CONFIG_CCS811_TRIGGER
|
||||||
|
struct device *dev;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* DATARDY is configured through SENSOR_CHAN_ALL.
|
* DATARDY is configured through SENSOR_CHAN_ALL.
|
||||||
* THRESH would be configured through SENSOR_CHAN_CO2.
|
* THRESH would be configured through SENSOR_CHAN_CO2.
|
||||||
|
@ -65,7 +67,6 @@ struct ccs811_data {
|
||||||
struct k_thread thread;
|
struct k_thread thread;
|
||||||
#elif defined(CONFIG_CCS811_TRIGGER_GLOBAL_THREAD)
|
#elif defined(CONFIG_CCS811_TRIGGER_GLOBAL_THREAD)
|
||||||
struct k_work work;
|
struct k_work work;
|
||||||
struct device *dev;
|
|
||||||
#endif
|
#endif
|
||||||
u16_t co2_l2m;
|
u16_t co2_l2m;
|
||||||
u16_t co2_m2h;
|
u16_t co2_m2h;
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
#include <logging/log.h>
|
#include <logging/log.h>
|
||||||
LOG_MODULE_DECLARE(CCS811);
|
LOG_MODULE_DECLARE(CCS811);
|
||||||
|
|
||||||
|
#define IRQ_PIN DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN
|
||||||
|
|
||||||
int ccs811_attr_set(struct device *dev,
|
int ccs811_attr_set(struct device *dev,
|
||||||
enum sensor_channel chan,
|
enum sensor_channel chan,
|
||||||
enum sensor_attribute attr,
|
enum sensor_attribute attr,
|
||||||
|
@ -41,40 +43,57 @@ int ccs811_attr_set(struct device *dev,
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void setup_irq(struct device *dev,
|
||||||
|
bool enable)
|
||||||
|
{
|
||||||
|
struct ccs811_data *data = dev->driver_data;
|
||||||
|
unsigned int flags = enable
|
||||||
|
? GPIO_INT_LEVEL_ACTIVE
|
||||||
|
: GPIO_INT_DISABLE;
|
||||||
|
|
||||||
|
gpio_pin_interrupt_configure(data->irq_gpio, IRQ_PIN, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void handle_irq(struct device *dev)
|
||||||
|
{
|
||||||
|
struct ccs811_data *data = dev->driver_data;
|
||||||
|
|
||||||
|
setup_irq(dev, false);
|
||||||
|
|
||||||
|
#if defined(CONFIG_CCS811_TRIGGER_OWN_THREAD)
|
||||||
|
k_sem_give(&data->gpio_sem);
|
||||||
|
#elif defined(CONFIG_CCS811_TRIGGER_GLOBAL_THREAD)
|
||||||
|
k_work_submit(&data->work);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void process_irq(struct device *dev)
|
||||||
|
{
|
||||||
|
struct ccs811_data *data = dev->driver_data;
|
||||||
|
|
||||||
|
if (data->handler != NULL) {
|
||||||
|
data->handler(dev, &data->trigger);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data->handler != NULL) {
|
||||||
|
setup_irq(dev, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void gpio_callback(struct device *dev,
|
static void gpio_callback(struct device *dev,
|
||||||
struct gpio_callback *cb,
|
struct gpio_callback *cb,
|
||||||
u32_t pins)
|
u32_t pins)
|
||||||
{
|
{
|
||||||
struct ccs811_data *drv_data =
|
struct ccs811_data *data =
|
||||||
CONTAINER_OF(cb, struct ccs811_data, gpio_cb);
|
CONTAINER_OF(cb, struct ccs811_data, gpio_cb);
|
||||||
|
|
||||||
ARG_UNUSED(pins);
|
ARG_UNUSED(pins);
|
||||||
|
|
||||||
gpio_pin_disable_callback(dev, DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN);
|
handle_irq(data->dev);
|
||||||
|
|
||||||
#if defined(CONFIG_CCS811_TRIGGER_OWN_THREAD)
|
|
||||||
k_sem_give(&drv_data->gpio_sem);
|
|
||||||
#elif defined(CONFIG_CCS811_TRIGGER_GLOBAL_THREAD)
|
|
||||||
k_work_submit(&drv_data->work);
|
|
||||||
#else
|
|
||||||
#error Unhandled trigger configuration
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static void thread_cb(void *arg)
|
|
||||||
{
|
|
||||||
struct device *dev = arg;
|
|
||||||
struct ccs811_data *drv_data = dev->driver_data;
|
|
||||||
|
|
||||||
if (drv_data->handler != NULL) {
|
|
||||||
drv_data->handler(dev, &drv_data->trigger);
|
|
||||||
}
|
|
||||||
|
|
||||||
gpio_pin_enable_callback(drv_data->int_gpio, DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_CCS811_TRIGGER_OWN_THREAD
|
#ifdef CONFIG_CCS811_TRIGGER_OWN_THREAD
|
||||||
static void datardy_thread(int dev_ptr, int unused)
|
static void irq_thread(int dev_ptr, int unused)
|
||||||
{
|
{
|
||||||
struct device *dev = INT_TO_POINTER(dev_ptr);
|
struct device *dev = INT_TO_POINTER(dev_ptr);
|
||||||
struct ccs811_data *drv_data = dev->driver_data;
|
struct ccs811_data *drv_data = dev->driver_data;
|
||||||
|
@ -83,16 +102,15 @@ static void datardy_thread(int dev_ptr, int unused)
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
k_sem_take(&drv_data->gpio_sem, K_FOREVER);
|
k_sem_take(&drv_data->gpio_sem, K_FOREVER);
|
||||||
thread_cb(dev);
|
process_irq(dev);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#elif defined(CONFIG_CCS811_TRIGGER_GLOBAL_THREAD)
|
#elif defined(CONFIG_CCS811_TRIGGER_GLOBAL_THREAD)
|
||||||
static void work_cb(struct k_work *work)
|
static void work_cb(struct k_work *work)
|
||||||
{
|
{
|
||||||
struct ccs811_data *drv_data =
|
struct ccs811_data *data = CONTAINER_OF(work, struct ccs811_data, work);
|
||||||
CONTAINER_OF(work, struct ccs811_data, work);
|
|
||||||
|
|
||||||
thread_cb(drv_data->dev);
|
process_irq(data->dev);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#error Unhandled trigger configuration
|
#error Unhandled trigger configuration
|
||||||
|
@ -107,7 +125,13 @@ int ccs811_trigger_set(struct device *dev,
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
LOG_DBG("CCS811 trigger set");
|
LOG_DBG("CCS811 trigger set");
|
||||||
gpio_pin_disable_callback(drv_data->int_gpio, DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN);
|
setup_irq(dev, false);
|
||||||
|
|
||||||
|
drv_data->handler = handler;
|
||||||
|
if (handler == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (trig->type == SENSOR_TRIG_DATA_READY) {
|
if (trig->type == SENSOR_TRIG_DATA_READY) {
|
||||||
rc = ccs811_mutate_meas_mode(dev, CCS811_MODE_DATARDY,
|
rc = ccs811_mutate_meas_mode(dev, CCS811_MODE_DATARDY,
|
||||||
CCS811_MODE_THRESH);
|
CCS811_MODE_THRESH);
|
||||||
|
@ -128,11 +152,14 @@ int ccs811_trigger_set(struct device *dev,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rc == 0) {
|
if (rc == 0) {
|
||||||
drv_data->handler = handler;
|
|
||||||
drv_data->trigger = *trig;
|
drv_data->trigger = *trig;
|
||||||
gpio_pin_enable_callback(drv_data->int_gpio,
|
setup_irq(dev, true);
|
||||||
DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN);
|
|
||||||
|
if (gpio_pin_get(drv_data->irq_gpio, IRQ_PIN) > 0) {
|
||||||
|
handle_irq(dev);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
drv_data->handler = NULL;
|
||||||
(void)ccs811_mutate_meas_mode(dev, 0, drdy_thresh);
|
(void)ccs811_mutate_meas_mode(dev, 0, drdy_thresh);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,18 +170,14 @@ int ccs811_init_interrupt(struct device *dev)
|
||||||
{
|
{
|
||||||
struct ccs811_data *drv_data = dev->driver_data;
|
struct ccs811_data *drv_data = dev->driver_data;
|
||||||
|
|
||||||
#ifndef DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN
|
drv_data->dev = dev;
|
||||||
return -EINVAL;
|
|
||||||
#endif
|
|
||||||
gpio_pin_configure(drv_data->int_gpio, DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN,
|
|
||||||
GPIO_DIR_IN | GPIO_INT | GPIO_INT_LEVEL |
|
|
||||||
GPIO_INT_ACTIVE_LOW | GPIO_PUD_PULL_UP |
|
|
||||||
GPIO_INT_DEBOUNCE);
|
|
||||||
|
|
||||||
gpio_init_callback(&drv_data->gpio_cb, gpio_callback,
|
gpio_pin_configure(drv_data->irq_gpio, IRQ_PIN,
|
||||||
BIT(DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN));
|
GPIO_INPUT | DT_INST_0_AMS_CCS811_IRQ_GPIOS_FLAGS);
|
||||||
|
|
||||||
if (gpio_add_callback(drv_data->int_gpio, &drv_data->gpio_cb) < 0) {
|
gpio_init_callback(&drv_data->gpio_cb, gpio_callback, BIT(IRQ_PIN));
|
||||||
|
|
||||||
|
if (gpio_add_callback(drv_data->irq_gpio, &drv_data->gpio_cb) < 0) {
|
||||||
LOG_DBG("Failed to set gpio callback!");
|
LOG_DBG("Failed to set gpio callback!");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -164,12 +187,11 @@ int ccs811_init_interrupt(struct device *dev)
|
||||||
|
|
||||||
k_thread_create(&drv_data->thread, drv_data->thread_stack,
|
k_thread_create(&drv_data->thread, drv_data->thread_stack,
|
||||||
CONFIG_CCS811_THREAD_STACK_SIZE,
|
CONFIG_CCS811_THREAD_STACK_SIZE,
|
||||||
(k_thread_entry_t)datardy_thread, dev,
|
(k_thread_entry_t)irq_thread, dev,
|
||||||
0, NULL, K_PRIO_COOP(CONFIG_CCS811_THREAD_PRIORITY),
|
0, NULL, K_PRIO_COOP(CONFIG_CCS811_THREAD_PRIORITY),
|
||||||
0, 0);
|
0, 0);
|
||||||
#elif defined(CONFIG_CCS811_TRIGGER_GLOBAL_THREAD)
|
#elif defined(CONFIG_CCS811_TRIGGER_GLOBAL_THREAD)
|
||||||
drv_data->work.handler = work_cb;
|
drv_data->work.handler = work_cb;
|
||||||
drv_data->dev = dev;
|
|
||||||
#else
|
#else
|
||||||
#error Unhandled trigger configuration
|
#error Unhandled trigger configuration
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -11,14 +11,21 @@ properties:
|
||||||
wake-gpios:
|
wake-gpios:
|
||||||
type: phandle-array
|
type: phandle-array
|
||||||
required: false
|
required: false
|
||||||
description: WAKEn pin
|
description: |
|
||||||
|
The WAKEn pin is asserted to communicate with the sensor. The
|
||||||
|
sensor receives this as an active-low signal.
|
||||||
|
|
||||||
reset-gpios:
|
reset-gpios:
|
||||||
type: phandle-array
|
type: phandle-array
|
||||||
required: false
|
required: false
|
||||||
description: RESETn pin
|
description: |
|
||||||
|
The RESETn pin is asserted to disable the sensor causing a hard
|
||||||
|
reset. The sensor receives this as an active-low signal.
|
||||||
|
|
||||||
irq-gpios:
|
irq-gpios:
|
||||||
type: phandle-array
|
type: phandle-array
|
||||||
required: false
|
required: false
|
||||||
description: INTn pin
|
description: |
|
||||||
|
The INTn pin signals that a new reading is available. The
|
||||||
|
sensor generates an active-low level signal which remains
|
||||||
|
asserted until the data is read.
|
||||||
|
|
|
@ -14,8 +14,8 @@
|
||||||
compatible = "ams,ccs811";
|
compatible = "ams,ccs811";
|
||||||
reg = <0x5b>;
|
reg = <0x5b>;
|
||||||
label = "CCS811";
|
label = "CCS811";
|
||||||
irq-gpios = <&gpio0 2 GPIO_INT_ACTIVE_LOW>;
|
irq-gpios = <&gpio0 2 GPIO_ACTIVE_LOW>;
|
||||||
wake-gpios = <&gpio0 5 GPIO_INT_ACTIVE_LOW>;
|
wake-gpios = <&gpio0 5 GPIO_ACTIVE_LOW>;
|
||||||
reset-gpios = <&gpio0 6 GPIO_INT_ACTIVE_LOW>;
|
reset-gpios = <&gpio0 6 GPIO_ACTIVE_LOW>;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue