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 <peter.bigot@nordicsemi.no>
This commit is contained in:
Peter Bigot 2019-12-16 09:59:39 -06:00 committed by Maureen Helm
commit 0cfac519cc
5 changed files with 109 additions and 98 deletions

View file

@ -3,50 +3,8 @@
# Copyright (c) 2016 Intel Corporation # Copyright (c) 2016 Intel Corporation
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
menuconfig DHT config DHT
bool "DHT Temperature and Humidity Sensor" bool "DHT Temperature and Humidity Sensor"
depends on GPIO depends on GPIO
help help
Enable driver for the DHT temperature and humidity sensor family. Enable driver for the DHT temperature and humidity sensor family.
if DHT
choice
prompt "Chip type"
default DHT_CHIP_DHT11
help
Choose desired chip type from the DHT family.
config DHT_CHIP_DHT11
bool "DHT11"
help
Choose this option to enable the DHT11 chip.
config DHT_CHIP_DHT22
bool "DHT22"
help
Choose this option to enable the DHT22 chip.
endchoice
config DHT_NAME
string "Driver name"
default "DHT11" if DHT_CHIP_DHT11
default "DHT22" if DHT_CHIP_DHT22
help
Device name with which the sensor is identified.
config DHT_GPIO_DEV_NAME
string "GPIO device"
default "GPIO_0"
help
The device name of the GPIO device to which the chip's data pin
is connected.
config DHT_GPIO_PIN_NUM
int "Interrupt GPIO pin number"
default 0
help
The number of the GPIO on which the chip's data pin is connected.
endif # DHT

View file

@ -26,9 +26,11 @@ LOG_MODULE_REGISTER(DHT, CONFIG_SENSOR_LOG_LEVEL);
* @return duration in usec of signal being measured, * @return duration in usec of signal being measured,
* -1 if duration exceeds DHT_SIGNAL_MAX_WAIT_DURATION * -1 if duration exceeds DHT_SIGNAL_MAX_WAIT_DURATION
*/ */
static s8_t dht_measure_signal_duration(struct dht_data *drv_data, static s8_t dht_measure_signal_duration(struct device *dev,
u32_t signal_val) u32_t signal_val)
{ {
struct dht_data *drv_data = dev->driver_data;
const struct dht_config *cfg = dev->config->config_info;
u32_t val; u32_t val;
u32_t elapsed_cycles; u32_t elapsed_cycles;
u32_t max_wait_cycles = (u32_t)( u32_t max_wait_cycles = (u32_t)(
@ -39,7 +41,7 @@ static s8_t dht_measure_signal_duration(struct dht_data *drv_data,
u32_t start_cycles = k_cycle_get_32(); u32_t start_cycles = k_cycle_get_32();
do { do {
gpio_pin_read(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, &val); gpio_pin_read(drv_data->gpio, cfg->pin, &val);
elapsed_cycles = k_cycle_get_32() - start_cycles; elapsed_cycles = k_cycle_get_32() - start_cycles;
if (elapsed_cycles > max_wait_cycles) { if (elapsed_cycles > max_wait_cycles) {
@ -55,6 +57,7 @@ static s8_t dht_measure_signal_duration(struct dht_data *drv_data,
static int dht_sample_fetch(struct device *dev, enum sensor_channel chan) static int dht_sample_fetch(struct device *dev, enum sensor_channel chan)
{ {
struct dht_data *drv_data = dev->driver_data; struct dht_data *drv_data = dev->driver_data;
const struct dht_config *cfg = dev->config->config_info;
int ret = 0; int ret = 0;
s8_t signal_duration[DHT_DATA_BITS_NUM]; s8_t signal_duration[DHT_DATA_BITS_NUM];
s8_t max_duration, min_duration, avg_duration; s8_t max_duration, min_duration, avg_duration;
@ -64,30 +67,30 @@ static int dht_sample_fetch(struct device *dev, enum sensor_channel chan)
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL); __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
/* send start signal */ /* send start signal */
gpio_pin_write(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, 0); gpio_pin_write(drv_data->gpio, cfg->pin, 0);
k_busy_wait(DHT_START_SIGNAL_DURATION); k_busy_wait(DHT_START_SIGNAL_DURATION);
gpio_pin_write(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, 1); gpio_pin_write(drv_data->gpio, cfg->pin, 1);
/* switch to DIR_IN to read sensor signals */ /* switch to DIR_IN to read sensor signals */
gpio_pin_configure(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, gpio_pin_configure(drv_data->gpio, cfg->pin,
GPIO_DIR_IN); GPIO_DIR_IN);
/* wait for sensor response */ /* wait for sensor response */
if (dht_measure_signal_duration(drv_data, 1) == -1) { if (dht_measure_signal_duration(dev, 1) == -1) {
ret = -EIO; ret = -EIO;
goto cleanup; goto cleanup;
} }
/* read sensor response */ /* read sensor response */
if (dht_measure_signal_duration(drv_data, 0) == -1) { if (dht_measure_signal_duration(dev, 0) == -1) {
ret = -EIO; ret = -EIO;
goto cleanup; goto cleanup;
} }
/* wait for sensor data */ /* wait for sensor data */
if (dht_measure_signal_duration(drv_data, 1) == -1) { if (dht_measure_signal_duration(dev, 1) == -1) {
ret = -EIO; ret = -EIO;
goto cleanup; goto cleanup;
} }
@ -95,13 +98,13 @@ static int dht_sample_fetch(struct device *dev, enum sensor_channel chan)
/* read sensor data */ /* read sensor data */
for (i = 0U; i < DHT_DATA_BITS_NUM; i++) { for (i = 0U; i < DHT_DATA_BITS_NUM; i++) {
/* LOW signal to indicate a new bit */ /* LOW signal to indicate a new bit */
if (dht_measure_signal_duration(drv_data, 0) == -1) { if (dht_measure_signal_duration(dev, 0) == -1) {
ret = -EIO; ret = -EIO;
goto cleanup; goto cleanup;
} }
/* HIGH signal duration indicates bit value */ /* HIGH signal duration indicates bit value */
signal_duration[i] = dht_measure_signal_duration(drv_data, 1); signal_duration[i] = dht_measure_signal_duration(dev, 1);
if (signal_duration[i] == -1) { if (signal_duration[i] == -1) {
ret = -EIO; ret = -EIO;
goto cleanup; goto cleanup;
@ -152,9 +155,8 @@ static int dht_sample_fetch(struct device *dev, enum sensor_channel chan)
cleanup: cleanup:
/* switch to DIR_OUT and leave pin to HIGH until next fetch */ /* switch to DIR_OUT and leave pin to HIGH until next fetch */
gpio_pin_configure(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, gpio_pin_configure(drv_data->gpio, cfg->pin, GPIO_DIR_OUT);
GPIO_DIR_OUT); gpio_pin_write(drv_data->gpio, cfg->pin, 1);
gpio_pin_write(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, 1);
return ret; return ret;
} }
@ -165,31 +167,25 @@ static int dht_channel_get(struct device *dev,
{ {
struct dht_data *drv_data = dev->driver_data; struct dht_data *drv_data = dev->driver_data;
__ASSERT_NO_MSG(chan == SENSOR_CHAN_AMBIENT_TEMP || chan == SENSOR_CHAN_HUMIDITY); __ASSERT_NO_MSG(chan == SENSOR_CHAN_AMBIENT_TEMP
|| chan == SENSOR_CHAN_HUMIDITY);
/* see data calculation example from datasheet */ /* see data calculation example from datasheet */
#if defined(CONFIG_DHT_CHIP_DHT11) if (IS_ENABLED(DT_INST_0_AOSONG_DHT_DHT22)) {
/* use only integral data byte */
if (chan == SENSOR_CHAN_HUMIDITY) {
val->val1 = drv_data->sample[0];
val->val2 = 0;
} else { /* chan == SENSOR_CHAN_AMBIENT_TEMP */
val->val1 = drv_data->sample[2];
val->val2 = 0;
}
#elif defined(CONFIG_DHT_CHIP_DHT22)
/* /*
* use both integral and decimal data bytes; resulted 16bit data has * use both integral and decimal data bytes; resulted
* a resolution of 0.1 units * 16bit data has a resolution of 0.1 units
*/ */
s16_t raw_val, sign; s16_t raw_val, sign;
if (chan == SENSOR_CHAN_HUMIDITY) { if (chan == SENSOR_CHAN_HUMIDITY) {
raw_val = (drv_data->sample[0] << 8) | drv_data->sample[1]; raw_val = (drv_data->sample[0] << 8)
+ drv_data->sample[1];
val->val1 = raw_val / 10; val->val1 = raw_val / 10;
val->val2 = (raw_val % 10) * 100000; val->val2 = (raw_val % 10) * 100000;
} else { /* chan == SENSOR_CHAN_AMBIENT_TEMP */ } else { /* chan == SENSOR_CHAN_AMBIENT_TEMP */
raw_val = (drv_data->sample[2] << 8) | drv_data->sample[3]; raw_val = (drv_data->sample[2] << 8)
+ drv_data->sample[3];
sign = raw_val & 0x8000; sign = raw_val & 0x8000;
raw_val = raw_val & ~0x8000; raw_val = raw_val & ~0x8000;
@ -203,7 +199,16 @@ static int dht_channel_get(struct device *dev,
val->val2 = -val->val2; val->val2 = -val->val2;
} }
} }
#endif } else {
/* use only integral data byte */
if (chan == SENSOR_CHAN_HUMIDITY) {
val->val1 = drv_data->sample[0];
val->val2 = 0;
} else { /* chan == SENSOR_CHAN_AMBIENT_TEMP */
val->val1 = drv_data->sample[2];
val->val2 = 0;
}
}
return 0; return 0;
} }
@ -215,23 +220,30 @@ static const struct sensor_driver_api dht_api = {
static int dht_init(struct device *dev) static int dht_init(struct device *dev)
{ {
int rc = 0;
struct dht_data *drv_data = dev->driver_data; struct dht_data *drv_data = dev->driver_data;
const struct dht_config *cfg = dev->config->config_info;
drv_data->gpio = device_get_binding(CONFIG_DHT_GPIO_DEV_NAME); drv_data->gpio = device_get_binding(cfg->ctrl);
if (drv_data->gpio == NULL) { if (drv_data->gpio == NULL) {
LOG_ERR("Failed to get GPIO device."); LOG_ERR("Failed to get GPIO device %s.", cfg->ctrl);
return -EINVAL; return -EINVAL;
} }
gpio_pin_configure(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, rc = gpio_pin_configure(drv_data->gpio, cfg->pin, GPIO_DIR_OUT);
GPIO_DIR_OUT); if (rc == 0) {
rc = gpio_pin_write(drv_data->gpio, cfg->pin, 1);
}
gpio_pin_write(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, 1); return rc;
return 0;
} }
struct dht_data dht_data; static struct dht_data dht_data;
static const struct dht_config dht_config = {
.ctrl = DT_INST_0_AOSONG_DHT_DIO_GPIOS_CONTROLLER,
.pin = DT_INST_0_AOSONG_DHT_DIO_GPIOS_PIN,
};
DEVICE_AND_API_INIT(dht_dev, CONFIG_DHT_NAME, &dht_init, &dht_data, DEVICE_AND_API_INIT(dht_dev, DT_INST_0_AOSONG_DHT_LABEL, &dht_init,
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &dht_api); &dht_data, &dht_config,
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &dht_api);

View file

@ -18,4 +18,9 @@ struct dht_data {
u8_t sample[4]; u8_t sample[4];
}; };
struct dht_config {
const char *ctrl;
u8_t pin;
};
#endif #endif

View file

@ -0,0 +1,29 @@
# Copyright (c) 2019 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
title: Aosong DHT Digital-output Humidity and Temperature Sensor
description: |
The Aosong DHT family of sensors provide temperature and humidity
measurements through a bidirectional serial digital signal. The
DHT11 uses a polymer humidity capacitor with NTC thermistor; the
DHT22 replaces the thermistor with a DS18B20 1-wire thermometer.
The DHT22 is also known as an AM2303.
compatible: "aosong,dht"
include: base.yaml
properties:
dio-gpios:
type: phandle-array
required: true
description: |
Pin on which sensor communication will be performed.
dht22:
type: boolean
description: |
Set to identify sensor as a DHT22/AM2303. Leave unset to identify
sensor as a DHT11.

View file

@ -62,6 +62,13 @@
#define DT_INST_0_AMS_CCS811_BASE_ADDRESS 0 #define DT_INST_0_AMS_CCS811_BASE_ADDRESS 0
#endif #endif
#ifndef DT_INST_0_AOSONG_DHT_LABEL
#define DT_INST_0_AOSONG_DHT_LABEL ""
#define DT_INST_0_AOSONG_DHT_DIO_GPIOS_CONTROLLER ""
#define DT_INST_0_AOSONG_DHT_DIO_GPIOS_PIN 0
#define DT_INST_0_AOSONG_DHT_DIO_GPIOS_FLAGS 0
#endif
#ifndef DT_INST_0_NXP_FXAS21002_LABEL #ifndef DT_INST_0_NXP_FXAS21002_LABEL
#define DT_INST_0_NXP_FXAS21002_LABEL "" #define DT_INST_0_NXP_FXAS21002_LABEL ""
#define DT_INST_0_NXP_FXAS21002_BASE_ADDRESS 0 #define DT_INST_0_NXP_FXAS21002_BASE_ADDRESS 0