drivers: sensor: dht: Update driver to use gpio_dt_spec
Simplify driver by using gpio_dt_spec for bus access.
Signed-off-by: Benjamin Björnsson <benjamin.bjornsson@gmail.com>
diff --git a/drivers/sensor/dht/dht.c b/drivers/sensor/dht/dht.c
index 97a559b..1d935ca 100644
--- a/drivers/sensor/dht/dht.c
+++ b/drivers/sensor/dht/dht.c
@@ -31,7 +31,6 @@
static int8_t dht_measure_signal_duration(const struct device *dev,
bool active)
{
- struct dht_data *drv_data = dev->data;
const struct dht_config *cfg = dev->config;
uint32_t elapsed_cycles;
uint32_t max_wait_cycles = (uint32_t)(
@@ -43,7 +42,7 @@
int rc;
do {
- rc = gpio_pin_get(drv_data->gpio, cfg->pin);
+ rc = gpio_pin_get_dt(&cfg->dio_gpio);
elapsed_cycles = k_cycle_get_32() - start_cycles;
if ((rc < 0)
@@ -71,15 +70,14 @@
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
/* assert to send start signal */
- gpio_pin_set(drv_data->gpio, cfg->pin, true);
+ gpio_pin_set_dt(&cfg->dio_gpio, true);
k_busy_wait(DHT_START_SIGNAL_DURATION);
- gpio_pin_set(drv_data->gpio, cfg->pin, false);
+ gpio_pin_set_dt(&cfg->dio_gpio, false);
/* switch to DIR_IN to read sensor signals */
- gpio_pin_configure(drv_data->gpio, cfg->pin,
- GPIO_INPUT | cfg->flags);
+ gpio_pin_configure_dt(&cfg->dio_gpio, GPIO_INPUT);
/* wait for sensor active response */
if (dht_measure_signal_duration(dev, false) == -1) {
@@ -159,8 +157,7 @@
cleanup:
/* Switch to output inactive until next fetch. */
- gpio_pin_configure(drv_data->gpio, cfg->pin,
- GPIO_OUTPUT_INACTIVE | cfg->flags);
+ gpio_pin_configure_dt(&cfg->dio_gpio, GPIO_OUTPUT_INACTIVE);
return ret;
}
@@ -225,26 +222,21 @@
static int dht_init(const struct device *dev)
{
int rc = 0;
- struct dht_data *drv_data = dev->data;
const struct dht_config *cfg = dev->config;
- drv_data->gpio = device_get_binding(cfg->ctrl);
- if (drv_data->gpio == NULL) {
- LOG_ERR("Failed to get GPIO device %s.", cfg->ctrl);
- return -EINVAL;
+ if (!device_is_ready(cfg->dio_gpio.port)) {
+ LOG_ERR("GPIO device not ready");
+ return -ENODEV;
}
- rc = gpio_pin_configure(drv_data->gpio, cfg->pin,
- GPIO_OUTPUT_INACTIVE | cfg->flags);
+ rc = gpio_pin_configure_dt(&cfg->dio_gpio, GPIO_OUTPUT_INACTIVE);
return rc;
}
static struct dht_data dht_data;
static const struct dht_config dht_config = {
- .ctrl = DT_INST_GPIO_LABEL(0, dio_gpios),
- .flags = DT_INST_GPIO_FLAGS(0, dio_gpios),
- .pin = DT_INST_GPIO_PIN(0, dio_gpios),
+ .dio_gpio = GPIO_DT_SPEC_INST_GET(0, dio_gpios),
};
DEVICE_DT_INST_DEFINE(0, &dht_init, NULL,
diff --git a/drivers/sensor/dht/dht.h b/drivers/sensor/dht/dht.h
index 640831b..8aea426 100644
--- a/drivers/sensor/dht/dht.h
+++ b/drivers/sensor/dht/dht.h
@@ -14,14 +14,11 @@
#define DHT_DATA_BITS_NUM 40
struct dht_data {
- const struct device *gpio;
uint8_t sample[4];
};
struct dht_config {
- const char *ctrl;
- gpio_dt_flags_t flags;
- gpio_pin_t pin;
+ struct gpio_dt_spec dio_gpio;
};
#endif