Skip to content

Commit 14d3888

Browse files
pabigotgalak
authored andcommitted
drivers: sensor: sht3xd: convert to new GPIO APIO
Update ALERT active level in all devicetree files. Capture GPIO flags in static configuration. Add internal API to enable and disable interrupt, to release the handlers when an alert occurs, and to re-enable the signal when the handler completes. Check for alerts received during periods when the interrupt was disabled. Extend the example to handle both above and below range triggers and alerts that are present on startup. Signed-off-by: Peter Bigot <[email protected]>
1 parent a205c54 commit 14d3888

File tree

11 files changed

+101
-55
lines changed

11 files changed

+101
-55
lines changed

drivers/sensor/sht3xd/sht3xd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ static const struct sht3xd_config sht3xd0_cfg = {
232232
.base_address = DT_INST_0_SENSIRION_SHT3XD_BASE_ADDRESS,
233233
#ifdef CONFIG_SHT3XD_TRIGGER
234234
.alert_pin = DT_INST_0_SENSIRION_SHT3XD_ALERT_GPIOS_PIN,
235+
.alert_flags = DT_INST_0_SENSIRION_SHT3XD_ALERT_GPIOS_FLAGS,
235236
#endif
236237
};
237238

drivers/sensor/sht3xd/sht3xd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ struct sht3xd_config {
5151

5252
u8_t base_address;
5353
#ifdef CONFIG_SHT3XD_TRIGGER
54-
s8_t alert_pin;
54+
u8_t alert_pin;
55+
u8_t alert_flags;
5556
#endif /* CONFIG_SHT3XD_TRIGGER */
5657
};
5758

drivers/sensor/sht3xd/sht3xd_trigger.c

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,35 +81,86 @@ int sht3xd_attr_set(struct device *dev,
8181
return 0;
8282
}
8383

84-
static void sht3xd_gpio_callback(struct device *dev,
85-
struct gpio_callback *cb, u32_t pins)
84+
static inline void setup_alert(struct device *dev,
85+
bool enable)
8686
{
87-
struct sht3xd_data *data =
88-
CONTAINER_OF(cb, struct sht3xd_data, alert_cb);
89-
const struct sht3xd_config *cfg = data->dev->config->config_info;
90-
91-
ARG_UNUSED(pins);
87+
struct sht3xd_data *data = (struct sht3xd_data *)dev->driver_data;
88+
const struct sht3xd_config *cfg =
89+
(const struct sht3xd_config *)dev->config->config_info;
90+
unsigned int flags = enable
91+
? GPIO_INT_EDGE_TO_ACTIVE
92+
: GPIO_INT_DISABLE;
93+
94+
gpio_pin_interrupt_configure(data->alert_gpio, cfg->alert_pin, flags);
95+
}
9296

93-
gpio_pin_disable_callback(dev, cfg->alert_pin);
97+
static inline void handle_alert(struct device *dev)
98+
{
99+
setup_alert(dev, false);
94100

95101
#if defined(CONFIG_SHT3XD_TRIGGER_OWN_THREAD)
102+
struct sht3xd_data *data = (struct sht3xd_data *)dev->driver_data;
103+
96104
k_sem_give(&data->gpio_sem);
97105
#elif defined(CONFIG_SHT3XD_TRIGGER_GLOBAL_THREAD)
106+
struct sht3xd_data *data = (struct sht3xd_data *)dev->driver_data;
107+
98108
k_work_submit(&data->work);
99109
#endif
100110
}
101111

112+
int sht3xd_trigger_set(struct device *dev,
113+
const struct sensor_trigger *trig,
114+
sensor_trigger_handler_t handler)
115+
{
116+
struct sht3xd_data *data = (struct sht3xd_data *)dev->driver_data;
117+
const struct sht3xd_config *cfg =
118+
(const struct sht3xd_config *)dev->config->config_info;
119+
120+
setup_alert(dev, false);
121+
122+
if (trig->type != SENSOR_TRIG_THRESHOLD) {
123+
return -ENOTSUP;
124+
}
125+
126+
data->handler = handler;
127+
if (handler == NULL) {
128+
return 0;
129+
}
130+
131+
data->trigger = *trig;
132+
133+
setup_alert(dev, true);
134+
135+
/* If ALERT is active we probably won't get the rising edge,
136+
* so invoke the callback manually.
137+
*/
138+
if (gpio_pin_get(data->alert_gpio, cfg->alert_pin)) {
139+
handle_alert(dev);
140+
}
141+
142+
return 0;
143+
}
144+
145+
static void sht3xd_gpio_callback(struct device *dev,
146+
struct gpio_callback *cb, u32_t pins)
147+
{
148+
struct sht3xd_data *data =
149+
CONTAINER_OF(cb, struct sht3xd_data, alert_cb);
150+
151+
handle_alert(data->dev);
152+
}
153+
102154
static void sht3xd_thread_cb(void *arg)
103155
{
104-
struct device *dev = arg;
105-
struct sht3xd_data *data = dev->driver_data;
106-
const struct sht3xd_config *cfg = dev->config->config_info;
156+
struct device *dev = (struct device *)arg;
157+
struct sht3xd_data *data = (struct sht3xd_data *)dev->driver_data;
107158

108159
if (data->handler != NULL) {
109160
data->handler(dev, &data->trigger);
110161
}
111162

112-
gpio_pin_enable_callback(data->alert_gpio, cfg->alert_pin);
163+
setup_alert(dev, true);
113164
}
114165

115166
#ifdef CONFIG_SHT3XD_TRIGGER_OWN_THREAD
@@ -137,25 +188,6 @@ static void sht3xd_work_cb(struct k_work *work)
137188
}
138189
#endif
139190

140-
int sht3xd_trigger_set(struct device *dev,
141-
const struct sensor_trigger *trig,
142-
sensor_trigger_handler_t handler)
143-
{
144-
struct sht3xd_data *data = dev->driver_data;
145-
const struct sht3xd_config *cfg = dev->config->config_info;
146-
147-
if (trig->type != SENSOR_TRIG_THRESHOLD) {
148-
return -ENOTSUP;
149-
}
150-
151-
gpio_pin_disable_callback(data->alert_gpio, cfg->alert_pin);
152-
data->handler = handler;
153-
data->trigger = *trig;
154-
gpio_pin_enable_callback(data->alert_gpio, cfg->alert_pin);
155-
156-
return 0;
157-
}
158-
159191
int sht3xd_init_interrupt(struct device *dev)
160192
{
161193
struct sht3xd_data *data = dev->driver_data;
@@ -172,9 +204,7 @@ int sht3xd_init_interrupt(struct device *dev)
172204
data->alert_gpio = gpio;
173205

174206
rc = gpio_pin_configure(gpio, cfg->alert_pin,
175-
GPIO_DIR_IN | GPIO_INT |
176-
GPIO_INT_EDGE | GPIO_INT_DOUBLE_EDGE |
177-
GPIO_INT_ACTIVE_HIGH | GPIO_INT_DEBOUNCE);
207+
GPIO_INPUT | cfg->alert_flags);
178208
if (rc != 0) {
179209
LOG_DBG("Failed to configure alert pin %u!", cfg->alert_pin);
180210
return -EIO;

dts/bindings/sensor/sensirion,sht3xd.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ properties:
1515
alert-gpios:
1616
type: phandle-array
1717
required: false
18-
description: ALERT pin
18+
description: >
19+
ALERT pin.
20+
21+
This pin signals active high when produced by the sensor. The
22+
property value should ensure the flags properly describe the
23+
signal that is presented to the driver.

samples/sensor/sht3xd/efr32mg_sltb004a.overlay

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
compatible = "sensirion,sht3xd";
1010
reg = <0x44>;
1111
label = "SHT3XD";
12-
alert-gpios = <&gpiof 6 GPIO_INT_ACTIVE_HIGH>;
12+
alert-gpios = <&gpiof 6 GPIO_ACTIVE_HIGH>;
1313
};
1414
};

samples/sensor/sht3xd/frdm_k64f.overlay

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
compatible = "sensirion,sht3xd";
1010
reg = <0x44>;
1111
label = "SHT3XD";
12-
alert-gpios = <&gpioe 26 GPIO_INT_ACTIVE_HIGH>;
12+
alert-gpios = <&gpioe 26 GPIO_ACTIVE_HIGH>;
1313
};
1414
};

samples/sensor/sht3xd/nrf51_ble400.overlay

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
compatible = "sensirion,sht3xd";
1010
reg = <0x44>;
1111
label = "SHT3XD";
12-
alert-gpios = <&gpio0 2 GPIO_INT_ACTIVE_HIGH>;
12+
alert-gpios = <&gpio0 2 GPIO_ACTIVE_HIGH>;
1313
};
1414
};

samples/sensor/sht3xd/nrf52840_pca10056.overlay

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
compatible = "sensirion,sht3xd";
1010
reg = <0x44>;
1111
label = "SHT3XD";
12-
alert-gpios = <&gpio1 10 GPIO_INT_ACTIVE_HIGH>;
12+
alert-gpios = <&gpio1 10 GPIO_ACTIVE_HIGH>;
1313
};
1414
};

samples/sensor/sht3xd/nucleo_l476rg.overlay

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
compatible = "sensirion,sht3xd";
1111
reg = <0x44>;
1212
label = "SHT3XD";
13-
alert-gpios = <&gpioa 9 GPIO_INT_ACTIVE_HIGH>;
13+
alert-gpios = <&gpioa 9 GPIO_ACTIVE_HIGH>;
1414
};
1515
};

samples/sensor/sht3xd/src/main.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#include <drivers/sensor.h>
1010
#include <stdio.h>
1111

12-
#define ALERT_HUMIDITY 40
12+
#define ALERT_HUMIDITY_LO 50
13+
#define ALERT_HUMIDITY_HI 60
1314

1415
#ifdef CONFIG_SHT3XD_TRIGGER
1516
static volatile bool alerted;
@@ -36,8 +37,8 @@ void main(void)
3637
.type = SENSOR_TRIG_THRESHOLD,
3738
.chan = SENSOR_CHAN_HUMIDITY,
3839
};
39-
struct sensor_value lo_thr = { 0 };
40-
struct sensor_value hi_thr = { ALERT_HUMIDITY };
40+
struct sensor_value lo_thr = { ALERT_HUMIDITY_LO };
41+
struct sensor_value hi_thr = { ALERT_HUMIDITY_HI };
4142
bool last_alerted = false;
4243

4344
rc = sensor_attr_set(dev, SENSOR_CHAN_HUMIDITY,
@@ -56,17 +57,6 @@ void main(void)
5657
while (true) {
5758
struct sensor_value temp, hum;
5859

59-
#ifdef CONFIG_SHT3XD_TRIGGER
60-
if (alerted != last_alerted) {
61-
static const char *const alert_str[] = {
62-
"below",
63-
"above",
64-
};
65-
printf("Humidity %s %d!\n", alert_str[alerted],
66-
hi_thr.val1);
67-
last_alerted = alerted;
68-
}
69-
#endif
7060
rc = sensor_sample_fetch(dev);
7161
if (rc == 0) {
7262
rc = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP,
@@ -80,6 +70,23 @@ void main(void)
8070
printf("SHT3XD: failed: %d\n", rc);
8171
break;
8272
}
73+
74+
#ifdef CONFIG_SHT3XD_TRIGGER
75+
if (alerted != last_alerted) {
76+
if (lo_thr.val1 > hum.val1) {
77+
printf("ALERT: humidity %d < %d\n",
78+
hum.val1, lo_thr.val1);
79+
} else if (hi_thr.val1 < hum.val1) {
80+
printf("ALERT: humidity %d > %d\n",
81+
hum.val1, hi_thr.val1);
82+
} else {
83+
printf("ALERT: humidity %d <= %d <= %d\n",
84+
lo_thr.val1, hum.val1, hi_thr.val1);
85+
}
86+
last_alerted = alerted;
87+
}
88+
#endif
89+
8390
printf("SHT3XD: %.2f Cel ; %0.2f %%RH\n",
8491
sensor_value_to_double(&temp),
8592
sensor_value_to_double(&hum));

0 commit comments

Comments
 (0)