Skip to content

Commit c5979a0

Browse files
committed
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 <[email protected]>
1 parent a6402d3 commit c5979a0

File tree

6 files changed

+94
-61
lines changed

6 files changed

+94
-61
lines changed

boards/arm/nrf52_pca20020/nrf52_pca20020.dts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@
113113
compatible = "ams,ccs811";
114114
reg = <0x5a>;
115115
label = "CCS811";
116-
irq-gpios = <&gpio0 22 0>;
117-
reset-gpios = <&sx1509b 11 0>;
118-
wake-gpios = <&sx1509b 12 0>;
116+
irq-gpios = <&gpio0 22 GPIO_ACTIVE_LOW>;
117+
reset-gpios = <&sx1509b 11 GPIO_ACTIVE_LOW>;
118+
wake-gpios = <&sx1509b 12 GPIO_ACTIVE_LOW>;
119119
};
120120
};
121121

drivers/sensor/ccs811/ccs811.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
#include "ccs811.h"
1919

20+
#define WAKE_PIN DT_INST_0_AMS_CCS811_WAKE_GPIOS_PIN
21+
#define RESET_PIN DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN
22+
2023
LOG_MODULE_REGISTER(CCS811, CONFIG_SENSOR_LOG_LEVEL);
2124

2225
#ifdef DT_INST_0_AMS_CCS811_WAKE_GPIOS_CONTROLLER
2326
static void set_wake(struct ccs811_data *drv_data, bool enable)
2427
{
25-
/* Always active-low */
26-
gpio_pin_write(drv_data->wake_gpio, DT_INST_0_AMS_CCS811_WAKE_GPIOS_PIN, !enable);
28+
gpio_pin_set(drv_data->wake_gpio, WAKE_PIN, enable);
2729
if (enable) {
2830
k_busy_wait(50); /* t_WAKE = 50 us */
2931
} else {
@@ -455,8 +457,9 @@ static int ccs811_init(struct device *dev)
455457
* any I2C transfer. If it has been tied to GND by
456458
* default, skip this part.
457459
*/
458-
gpio_pin_configure(drv_data->wake_gpio, DT_INST_0_AMS_CCS811_WAKE_GPIOS_PIN,
459-
GPIO_DIR_OUT);
460+
gpio_pin_configure(drv_data->wake_gpio, WAKE_PIN,
461+
GPIO_OUTPUT_INACTIVE
462+
| DT_INST_0_AMS_CCS811_WAKE_GPIOS_FLAGS);
460463

461464
set_wake(drv_data, true);
462465
k_sleep(1);
@@ -468,16 +471,16 @@ static int ccs811_init(struct device *dev)
468471
DT_INST_0_AMS_CCS811_RESET_GPIOS_CONTROLLER);
469472
return -EINVAL;
470473
}
471-
gpio_pin_configure(drv_data->reset_gpio, DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN,
472-
GPIO_DIR_OUT);
473-
gpio_pin_write(drv_data->reset_gpio, DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN, 1);
474+
gpio_pin_configure(drv_data->reset_gpio, RESET_PIN,
475+
GPIO_OUTPUT_ACTIVE
476+
| DT_INST_0_AMS_CCS811_RESET_GPIOS_FLAGS);
474477

475478
k_sleep(1);
476479
#endif
477480

478481
#ifdef DT_INST_0_AMS_CCS811_IRQ_GPIOS_CONTROLLER
479-
drv_data->int_gpio = device_get_binding(DT_INST_0_AMS_CCS811_IRQ_GPIOS_CONTROLLER);
480-
if (drv_data->int_gpio == NULL) {
482+
drv_data->irq_gpio = device_get_binding(DT_INST_0_AMS_CCS811_IRQ_GPIOS_CONTROLLER);
483+
if (drv_data->irq_gpio == NULL) {
481484
LOG_ERR("Failed to get pointer to INT device: %s",
482485
DT_INST_0_AMS_CCS811_IRQ_GPIOS_CONTROLLER);
483486
return -EINVAL;
@@ -488,10 +491,10 @@ static int ccs811_init(struct device *dev)
488491
* and validating any errors or configuration inconsistencies
489492
* after a reset that left the device running.
490493
*/
491-
#ifdef DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN
492-
gpio_pin_write(drv_data->reset_gpio, DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN, 0);
494+
#ifdef DT_INST_0_AMS_CCS811_RESET_GPIOS_CONTROLLER
495+
gpio_pin_set(drv_data->reset_gpio, RESET_PIN, 1);
493496
k_busy_wait(15); /* t_RESET */
494-
gpio_pin_write(drv_data->reset_gpio, DT_INST_0_AMS_CCS811_RESET_GPIOS_PIN, 1);
497+
gpio_pin_set(drv_data->reset_gpio, RESET_PIN, 0);
495498
#else
496499
{
497500
static u8_t const reset_seq[] = {

drivers/sensor/ccs811/ccs811.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@
5050
struct ccs811_data {
5151
struct device *i2c;
5252
#ifdef DT_INST_0_AMS_CCS811_IRQ_GPIOS_CONTROLLER
53-
struct device *int_gpio;
53+
struct device *irq_gpio;
5454
#ifdef CONFIG_CCS811_TRIGGER
55+
struct device *dev;
56+
5557
/*
5658
* DATARDY is configured through SENSOR_CHAN_ALL.
5759
* THRESH would be configured through SENSOR_CHAN_CO2.
@@ -65,7 +67,6 @@ struct ccs811_data {
6567
struct k_thread thread;
6668
#elif defined(CONFIG_CCS811_TRIGGER_GLOBAL_THREAD)
6769
struct k_work work;
68-
struct device *dev;
6970
#endif
7071
u16_t co2_l2m;
7172
u16_t co2_m2h;

drivers/sensor/ccs811/ccs811_trigger.c

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <logging/log.h>
1212
LOG_MODULE_DECLARE(CCS811);
1313

14+
#define IRQ_PIN DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN
15+
1416
int ccs811_attr_set(struct device *dev,
1517
enum sensor_channel chan,
1618
enum sensor_attribute attr,
@@ -41,40 +43,57 @@ int ccs811_attr_set(struct device *dev,
4143
return rc;
4244
}
4345

44-
static void gpio_callback(struct device *dev,
45-
struct gpio_callback *cb,
46-
u32_t pins)
46+
static inline void setup_irq(struct device *dev,
47+
bool enable)
4748
{
48-
struct ccs811_data *drv_data =
49-
CONTAINER_OF(cb, struct ccs811_data, gpio_cb);
49+
struct ccs811_data *data = dev->driver_data;
50+
unsigned int flags = enable
51+
? GPIO_INT_LEVEL_ACTIVE
52+
: GPIO_INT_DISABLE;
5053

51-
ARG_UNUSED(pins);
54+
gpio_pin_interrupt_configure(data->irq_gpio, IRQ_PIN, flags);
55+
}
56+
57+
static inline void handle_irq(struct device *dev)
58+
{
59+
struct ccs811_data *data = dev->driver_data;
5260

53-
gpio_pin_disable_callback(dev, DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN);
61+
setup_irq(dev, false);
5462

5563
#if defined(CONFIG_CCS811_TRIGGER_OWN_THREAD)
56-
k_sem_give(&drv_data->gpio_sem);
64+
k_sem_give(&data->gpio_sem);
5765
#elif defined(CONFIG_CCS811_TRIGGER_GLOBAL_THREAD)
58-
k_work_submit(&drv_data->work);
59-
#else
60-
#error Unhandled trigger configuration
66+
k_work_submit(&data->work);
6167
#endif
6268
}
6369

64-
static void thread_cb(void *arg)
70+
static void process_irq(struct device *dev)
6571
{
66-
struct device *dev = arg;
67-
struct ccs811_data *drv_data = dev->driver_data;
72+
struct ccs811_data *data = dev->driver_data;
6873

69-
if (drv_data->handler != NULL) {
70-
drv_data->handler(dev, &drv_data->trigger);
74+
if (data->handler != NULL) {
75+
data->handler(dev, &data->trigger);
7176
}
7277

73-
gpio_pin_enable_callback(drv_data->int_gpio, DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN);
78+
if (data->handler != NULL) {
79+
setup_irq(dev, true);
80+
}
81+
}
82+
83+
static void gpio_callback(struct device *dev,
84+
struct gpio_callback *cb,
85+
u32_t pins)
86+
{
87+
struct ccs811_data *data =
88+
CONTAINER_OF(cb, struct ccs811_data, gpio_cb);
89+
90+
ARG_UNUSED(pins);
91+
92+
handle_irq(data->dev);
7493
}
7594

7695
#ifdef CONFIG_CCS811_TRIGGER_OWN_THREAD
77-
static void datardy_thread(int dev_ptr, int unused)
96+
static void irq_thread(int dev_ptr, int unused)
7897
{
7998
struct device *dev = INT_TO_POINTER(dev_ptr);
8099
struct ccs811_data *drv_data = dev->driver_data;
@@ -83,16 +102,15 @@ static void datardy_thread(int dev_ptr, int unused)
83102

84103
while (1) {
85104
k_sem_take(&drv_data->gpio_sem, K_FOREVER);
86-
thread_cb(dev);
105+
process_irq(dev);
87106
}
88107
}
89108
#elif defined(CONFIG_CCS811_TRIGGER_GLOBAL_THREAD)
90109
static void work_cb(struct k_work *work)
91110
{
92-
struct ccs811_data *drv_data =
93-
CONTAINER_OF(work, struct ccs811_data, work);
111+
struct ccs811_data *data = CONTAINER_OF(work, struct ccs811_data, work);
94112

95-
thread_cb(drv_data->dev);
113+
process_irq(data->dev);
96114
}
97115
#else
98116
#error Unhandled trigger configuration
@@ -107,7 +125,13 @@ int ccs811_trigger_set(struct device *dev,
107125
int rc;
108126

109127
LOG_DBG("CCS811 trigger set");
110-
gpio_pin_disable_callback(drv_data->int_gpio, DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN);
128+
setup_irq(dev, false);
129+
130+
drv_data->handler = handler;
131+
if (handler == NULL) {
132+
return 0;
133+
}
134+
111135
if (trig->type == SENSOR_TRIG_DATA_READY) {
112136
rc = ccs811_mutate_meas_mode(dev, CCS811_MODE_DATARDY,
113137
CCS811_MODE_THRESH);
@@ -128,11 +152,14 @@ int ccs811_trigger_set(struct device *dev,
128152
}
129153

130154
if (rc == 0) {
131-
drv_data->handler = handler;
132155
drv_data->trigger = *trig;
133-
gpio_pin_enable_callback(drv_data->int_gpio,
134-
DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN);
156+
setup_irq(dev, true);
157+
158+
if (gpio_pin_get(drv_data->irq_gpio, IRQ_PIN) > 0) {
159+
handle_irq(dev);
160+
}
135161
} else {
162+
drv_data->handler = NULL;
136163
(void)ccs811_mutate_meas_mode(dev, 0, drdy_thresh);
137164
}
138165

@@ -143,18 +170,14 @@ int ccs811_init_interrupt(struct device *dev)
143170
{
144171
struct ccs811_data *drv_data = dev->driver_data;
145172

146-
#ifndef DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN
147-
return -EINVAL;
148-
#endif
149-
gpio_pin_configure(drv_data->int_gpio, DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN,
150-
GPIO_DIR_IN | GPIO_INT | GPIO_INT_LEVEL |
151-
GPIO_INT_ACTIVE_LOW | GPIO_PUD_PULL_UP |
152-
GPIO_INT_DEBOUNCE);
173+
drv_data->dev = dev;
153174

154-
gpio_init_callback(&drv_data->gpio_cb, gpio_callback,
155-
BIT(DT_INST_0_AMS_CCS811_IRQ_GPIOS_PIN));
175+
gpio_pin_configure(drv_data->irq_gpio, IRQ_PIN,
176+
GPIO_INPUT | DT_INST_0_AMS_CCS811_IRQ_GPIOS_FLAGS);
156177

157-
if (gpio_add_callback(drv_data->int_gpio, &drv_data->gpio_cb) < 0) {
178+
gpio_init_callback(&drv_data->gpio_cb, gpio_callback, BIT(IRQ_PIN));
179+
180+
if (gpio_add_callback(drv_data->irq_gpio, &drv_data->gpio_cb) < 0) {
158181
LOG_DBG("Failed to set gpio callback!");
159182
return -EIO;
160183
}
@@ -164,12 +187,11 @@ int ccs811_init_interrupt(struct device *dev)
164187

165188
k_thread_create(&drv_data->thread, drv_data->thread_stack,
166189
CONFIG_CCS811_THREAD_STACK_SIZE,
167-
(k_thread_entry_t)datardy_thread, dev,
190+
(k_thread_entry_t)irq_thread, dev,
168191
0, NULL, K_PRIO_COOP(CONFIG_CCS811_THREAD_PRIORITY),
169192
0, 0);
170193
#elif defined(CONFIG_CCS811_TRIGGER_GLOBAL_THREAD)
171194
drv_data->work.handler = work_cb;
172-
drv_data->dev = dev;
173195
#else
174196
#error Unhandled trigger configuration
175197
#endif

dts/bindings/sensor/ams,ccs811.yaml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@ properties:
1111
wake-gpios:
1212
type: phandle-array
1313
required: false
14-
description: WAKEn pin
14+
description: |
15+
The WAKEn pin is asserted to communicate with the sensor. The
16+
sensor receives this as an active-low signal.
1517
1618
reset-gpios:
1719
type: phandle-array
1820
required: false
19-
description: RESETn pin
21+
description: |
22+
The RESETn pin is asserted to disable the sensor causing a hard
23+
reset. The sensor receives this as an active-low signal.
2024
2125
irq-gpios:
2226
type: phandle-array
2327
required: false
24-
description: INTn pin
28+
description: |
29+
The INTn pin signals that a new reading is available. The
30+
sensor generates an active-low level signal which remains
31+
asserted until the data is read.

samples/sensor/ccs811/boards/nrf51_ble400.overlay

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
compatible = "ams,ccs811";
1515
reg = <0x5b>;
1616
label = "CCS811";
17-
irq-gpios = <&gpio0 2 GPIO_INT_ACTIVE_LOW>;
18-
wake-gpios = <&gpio0 5 GPIO_INT_ACTIVE_LOW>;
19-
reset-gpios = <&gpio0 6 GPIO_INT_ACTIVE_LOW>;
17+
irq-gpios = <&gpio0 2 GPIO_ACTIVE_LOW>;
18+
wake-gpios = <&gpio0 5 GPIO_ACTIVE_LOW>;
19+
reset-gpios = <&gpio0 6 GPIO_ACTIVE_LOW>;
2020
};
2121
};

0 commit comments

Comments
 (0)