Skip to content

Commit 0cfac51

Browse files
pabigotMaureenHelm
authored andcommitted
sensor: dht: convert from Kconfig to devicetree
Define a binding for the Aosong DHT family of temperature/humidity sensors. Remove the Kconfig settings, and update the driver to use devicetree information. Signed-off-by: Peter Bigot <[email protected]>
1 parent 7666e1c commit 0cfac51

File tree

5 files changed

+113
-102
lines changed

5 files changed

+113
-102
lines changed

drivers/sensor/dht/Kconfig

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,8 @@
33
# Copyright (c) 2016 Intel Corporation
44
# SPDX-License-Identifier: Apache-2.0
55

6-
menuconfig DHT
6+
config DHT
77
bool "DHT Temperature and Humidity Sensor"
88
depends on GPIO
99
help
1010
Enable driver for the DHT temperature and humidity sensor family.
11-
12-
if DHT
13-
14-
choice
15-
prompt "Chip type"
16-
default DHT_CHIP_DHT11
17-
help
18-
Choose desired chip type from the DHT family.
19-
20-
config DHT_CHIP_DHT11
21-
bool "DHT11"
22-
help
23-
Choose this option to enable the DHT11 chip.
24-
25-
config DHT_CHIP_DHT22
26-
bool "DHT22"
27-
help
28-
Choose this option to enable the DHT22 chip.
29-
30-
endchoice
31-
32-
config DHT_NAME
33-
string "Driver name"
34-
default "DHT11" if DHT_CHIP_DHT11
35-
default "DHT22" if DHT_CHIP_DHT22
36-
help
37-
Device name with which the sensor is identified.
38-
39-
config DHT_GPIO_DEV_NAME
40-
string "GPIO device"
41-
default "GPIO_0"
42-
help
43-
The device name of the GPIO device to which the chip's data pin
44-
is connected.
45-
46-
config DHT_GPIO_PIN_NUM
47-
int "Interrupt GPIO pin number"
48-
default 0
49-
help
50-
The number of the GPIO on which the chip's data pin is connected.
51-
52-
endif # DHT

drivers/sensor/dht/dht.c

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ LOG_MODULE_REGISTER(DHT, CONFIG_SENSOR_LOG_LEVEL);
2626
* @return duration in usec of signal being measured,
2727
* -1 if duration exceeds DHT_SIGNAL_MAX_WAIT_DURATION
2828
*/
29-
static s8_t dht_measure_signal_duration(struct dht_data *drv_data,
30-
u32_t signal_val)
29+
static s8_t dht_measure_signal_duration(struct device *dev,
30+
u32_t signal_val)
3131
{
32+
struct dht_data *drv_data = dev->driver_data;
33+
const struct dht_config *cfg = dev->config->config_info;
3234
u32_t val;
3335
u32_t elapsed_cycles;
3436
u32_t max_wait_cycles = (u32_t)(
@@ -39,7 +41,7 @@ static s8_t dht_measure_signal_duration(struct dht_data *drv_data,
3941
u32_t start_cycles = k_cycle_get_32();
4042

4143
do {
42-
gpio_pin_read(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, &val);
44+
gpio_pin_read(drv_data->gpio, cfg->pin, &val);
4345
elapsed_cycles = k_cycle_get_32() - start_cycles;
4446

4547
if (elapsed_cycles > max_wait_cycles) {
@@ -55,6 +57,7 @@ static s8_t dht_measure_signal_duration(struct dht_data *drv_data,
5557
static int dht_sample_fetch(struct device *dev, enum sensor_channel chan)
5658
{
5759
struct dht_data *drv_data = dev->driver_data;
60+
const struct dht_config *cfg = dev->config->config_info;
5861
int ret = 0;
5962
s8_t signal_duration[DHT_DATA_BITS_NUM];
6063
s8_t max_duration, min_duration, avg_duration;
@@ -64,44 +67,44 @@ static int dht_sample_fetch(struct device *dev, enum sensor_channel chan)
6467
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
6568

6669
/* send start signal */
67-
gpio_pin_write(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, 0);
70+
gpio_pin_write(drv_data->gpio, cfg->pin, 0);
6871

6972
k_busy_wait(DHT_START_SIGNAL_DURATION);
7073

71-
gpio_pin_write(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, 1);
74+
gpio_pin_write(drv_data->gpio, cfg->pin, 1);
7275

7376
/* switch to DIR_IN to read sensor signals */
74-
gpio_pin_configure(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM,
77+
gpio_pin_configure(drv_data->gpio, cfg->pin,
7578
GPIO_DIR_IN);
7679

7780
/* wait for sensor response */
78-
if (dht_measure_signal_duration(drv_data, 1) == -1) {
81+
if (dht_measure_signal_duration(dev, 1) == -1) {
7982
ret = -EIO;
8083
goto cleanup;
8184
}
8285

8386
/* read sensor response */
84-
if (dht_measure_signal_duration(drv_data, 0) == -1) {
87+
if (dht_measure_signal_duration(dev, 0) == -1) {
8588
ret = -EIO;
8689
goto cleanup;
8790
}
8891

8992
/* wait for sensor data */
90-
if (dht_measure_signal_duration(drv_data, 1) == -1) {
93+
if (dht_measure_signal_duration(dev, 1) == -1) {
9194
ret = -EIO;
9295
goto cleanup;
9396
}
9497

9598
/* read sensor data */
9699
for (i = 0U; i < DHT_DATA_BITS_NUM; i++) {
97100
/* LOW signal to indicate a new bit */
98-
if (dht_measure_signal_duration(drv_data, 0) == -1) {
101+
if (dht_measure_signal_duration(dev, 0) == -1) {
99102
ret = -EIO;
100103
goto cleanup;
101104
}
102105

103106
/* HIGH signal duration indicates bit value */
104-
signal_duration[i] = dht_measure_signal_duration(drv_data, 1);
107+
signal_duration[i] = dht_measure_signal_duration(dev, 1);
105108
if (signal_duration[i] == -1) {
106109
ret = -EIO;
107110
goto cleanup;
@@ -152,9 +155,8 @@ static int dht_sample_fetch(struct device *dev, enum sensor_channel chan)
152155

153156
cleanup:
154157
/* switch to DIR_OUT and leave pin to HIGH until next fetch */
155-
gpio_pin_configure(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM,
156-
GPIO_DIR_OUT);
157-
gpio_pin_write(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, 1);
158+
gpio_pin_configure(drv_data->gpio, cfg->pin, GPIO_DIR_OUT);
159+
gpio_pin_write(drv_data->gpio, cfg->pin, 1);
158160

159161
return ret;
160162
}
@@ -165,45 +167,48 @@ static int dht_channel_get(struct device *dev,
165167
{
166168
struct dht_data *drv_data = dev->driver_data;
167169

168-
__ASSERT_NO_MSG(chan == SENSOR_CHAN_AMBIENT_TEMP || chan == SENSOR_CHAN_HUMIDITY);
170+
__ASSERT_NO_MSG(chan == SENSOR_CHAN_AMBIENT_TEMP
171+
|| chan == SENSOR_CHAN_HUMIDITY);
169172

170173
/* see data calculation example from datasheet */
171-
#if defined(CONFIG_DHT_CHIP_DHT11)
172-
/* use only integral data byte */
173-
if (chan == SENSOR_CHAN_HUMIDITY) {
174-
val->val1 = drv_data->sample[0];
175-
val->val2 = 0;
176-
} else { /* chan == SENSOR_CHAN_AMBIENT_TEMP */
177-
val->val1 = drv_data->sample[2];
178-
val->val2 = 0;
179-
}
180-
#elif defined(CONFIG_DHT_CHIP_DHT22)
181-
/*
182-
* use both integral and decimal data bytes; resulted 16bit data has
183-
* a resolution of 0.1 units
184-
*/
185-
s16_t raw_val, sign;
186-
187-
if (chan == SENSOR_CHAN_HUMIDITY) {
188-
raw_val = (drv_data->sample[0] << 8) | drv_data->sample[1];
189-
val->val1 = raw_val / 10;
190-
val->val2 = (raw_val % 10) * 100000;
191-
} else { /* chan == SENSOR_CHAN_AMBIENT_TEMP */
192-
raw_val = (drv_data->sample[2] << 8) | drv_data->sample[3];
193-
194-
sign = raw_val & 0x8000;
195-
raw_val = raw_val & ~0x8000;
196-
197-
val->val1 = raw_val / 10;
198-
val->val2 = (raw_val % 10) * 100000;
199-
200-
/* handle negative value */
201-
if (sign) {
202-
val->val1 = -val->val1;
203-
val->val2 = -val->val2;
174+
if (IS_ENABLED(DT_INST_0_AOSONG_DHT_DHT22)) {
175+
/*
176+
* use both integral and decimal data bytes; resulted
177+
* 16bit data has a resolution of 0.1 units
178+
*/
179+
s16_t raw_val, sign;
180+
181+
if (chan == SENSOR_CHAN_HUMIDITY) {
182+
raw_val = (drv_data->sample[0] << 8)
183+
+ drv_data->sample[1];
184+
val->val1 = raw_val / 10;
185+
val->val2 = (raw_val % 10) * 100000;
186+
} else { /* chan == SENSOR_CHAN_AMBIENT_TEMP */
187+
raw_val = (drv_data->sample[2] << 8)
188+
+ drv_data->sample[3];
189+
190+
sign = raw_val & 0x8000;
191+
raw_val = raw_val & ~0x8000;
192+
193+
val->val1 = raw_val / 10;
194+
val->val2 = (raw_val % 10) * 100000;
195+
196+
/* handle negative value */
197+
if (sign) {
198+
val->val1 = -val->val1;
199+
val->val2 = -val->val2;
200+
}
201+
}
202+
} else {
203+
/* use only integral data byte */
204+
if (chan == SENSOR_CHAN_HUMIDITY) {
205+
val->val1 = drv_data->sample[0];
206+
val->val2 = 0;
207+
} else { /* chan == SENSOR_CHAN_AMBIENT_TEMP */
208+
val->val1 = drv_data->sample[2];
209+
val->val2 = 0;
204210
}
205211
}
206-
#endif
207212

208213
return 0;
209214
}
@@ -215,23 +220,30 @@ static const struct sensor_driver_api dht_api = {
215220

216221
static int dht_init(struct device *dev)
217222
{
223+
int rc = 0;
218224
struct dht_data *drv_data = dev->driver_data;
225+
const struct dht_config *cfg = dev->config->config_info;
219226

220-
drv_data->gpio = device_get_binding(CONFIG_DHT_GPIO_DEV_NAME);
227+
drv_data->gpio = device_get_binding(cfg->ctrl);
221228
if (drv_data->gpio == NULL) {
222-
LOG_ERR("Failed to get GPIO device.");
229+
LOG_ERR("Failed to get GPIO device %s.", cfg->ctrl);
223230
return -EINVAL;
224231
}
225232

226-
gpio_pin_configure(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM,
227-
GPIO_DIR_OUT);
228-
229-
gpio_pin_write(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, 1);
233+
rc = gpio_pin_configure(drv_data->gpio, cfg->pin, GPIO_DIR_OUT);
234+
if (rc == 0) {
235+
rc = gpio_pin_write(drv_data->gpio, cfg->pin, 1);
236+
}
230237

231-
return 0;
238+
return rc;
232239
}
233240

234-
struct dht_data dht_data;
241+
static struct dht_data dht_data;
242+
static const struct dht_config dht_config = {
243+
.ctrl = DT_INST_0_AOSONG_DHT_DIO_GPIOS_CONTROLLER,
244+
.pin = DT_INST_0_AOSONG_DHT_DIO_GPIOS_PIN,
245+
};
235246

236-
DEVICE_AND_API_INIT(dht_dev, CONFIG_DHT_NAME, &dht_init, &dht_data,
237-
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &dht_api);
247+
DEVICE_AND_API_INIT(dht_dev, DT_INST_0_AOSONG_DHT_LABEL, &dht_init,
248+
&dht_data, &dht_config,
249+
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &dht_api);

drivers/sensor/dht/dht.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ struct dht_data {
1818
u8_t sample[4];
1919
};
2020

21+
struct dht_config {
22+
const char *ctrl;
23+
u8_t pin;
24+
};
25+
2126
#endif

dts/bindings/sensor/aosong,dht.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) 2019 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
title: Aosong DHT Digital-output Humidity and Temperature Sensor
5+
6+
description: |
7+
The Aosong DHT family of sensors provide temperature and humidity
8+
measurements through a bidirectional serial digital signal. The
9+
DHT11 uses a polymer humidity capacitor with NTC thermistor; the
10+
DHT22 replaces the thermistor with a DS18B20 1-wire thermometer.
11+
12+
The DHT22 is also known as an AM2303.
13+
14+
compatible: "aosong,dht"
15+
16+
include: base.yaml
17+
18+
properties:
19+
dio-gpios:
20+
type: phandle-array
21+
required: true
22+
description: |
23+
Pin on which sensor communication will be performed.
24+
25+
dht22:
26+
type: boolean
27+
description: |
28+
Set to identify sensor as a DHT22/AM2303. Leave unset to identify
29+
sensor as a DHT11.

tests/drivers/build_all/dts_fixup.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@
6262
#define DT_INST_0_AMS_CCS811_BASE_ADDRESS 0
6363
#endif
6464

65+
#ifndef DT_INST_0_AOSONG_DHT_LABEL
66+
#define DT_INST_0_AOSONG_DHT_LABEL ""
67+
#define DT_INST_0_AOSONG_DHT_DIO_GPIOS_CONTROLLER ""
68+
#define DT_INST_0_AOSONG_DHT_DIO_GPIOS_PIN 0
69+
#define DT_INST_0_AOSONG_DHT_DIO_GPIOS_FLAGS 0
70+
#endif
71+
6572
#ifndef DT_INST_0_NXP_FXAS21002_LABEL
6673
#define DT_INST_0_NXP_FXAS21002_LABEL ""
6774
#define DT_INST_0_NXP_FXAS21002_BASE_ADDRESS 0

0 commit comments

Comments
 (0)