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>
This commit is contained in:
parent
a8586e8409
commit
f06ea0e12e
2 changed files with 11 additions and 22 deletions
|
@ -31,7 +31,6 @@ LOG_MODULE_REGISTER(DHT, CONFIG_SENSOR_LOG_LEVEL);
|
|||
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 @@ static int8_t dht_measure_signal_duration(const struct device *dev,
|
|||
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 @@ static int dht_sample_fetch(const struct device *dev,
|
|||
__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 @@ static int dht_sample_fetch(const struct device *dev,
|
|||
|
||||
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 const struct sensor_driver_api dht_api = {
|
|||
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,
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue