From 7af55237da50c82118ccd0c6311ac36634ba243c Mon Sep 17 00:00:00 2001 From: John Shelton Date: Fri, 9 May 2025 12:25:37 -0500 Subject: [PATCH] driver: sensor: aosong,dht: fix sensor protocol broken since change #83192 A recent change 9eeb60c improperly removed a critical line in the driver. This commit replaces that line and moves an irq_lock to above any pin value manipulation since the timing is sensitive with the DHT protocol. This should also fix the timing issue 9eeb60c attempted to fix. Signed-off-by: John Shelton --- drivers/sensor/aosong/dht/dht.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/drivers/sensor/aosong/dht/dht.c b/drivers/sensor/aosong/dht/dht.c index 88d16f1c6e7..58a282f4cb2 100644 --- a/drivers/sensor/aosong/dht/dht.c +++ b/drivers/sensor/aosong/dht/dht.c @@ -67,21 +67,26 @@ static int dht_sample_fetch(const struct device *dev, uint8_t buf[5]; unsigned int i, j; -#if defined(CONFIG_DHT_LOCK_IRQS) - int lock; -#endif - __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL); +#if defined(CONFIG_DHT_LOCK_IRQS) + /* Get the lock before any pin interaction since irq_lock has a */ + /* potential of causing delays itself. */ + int lock = irq_lock(); +#endif + /* assert to send start signal */ gpio_pin_set_dt(&cfg->dio_gpio, true); -#if defined(CONFIG_DHT_LOCK_IRQS) - lock = irq_lock(); -#endif - k_busy_wait(DHT_START_SIGNAL_DURATION); + /* set the state back to logic LOW (voltage HIGH) so that the */ + /* subsequent call to dht_measure_signal duration waits for the */ + /* DHT sensor to set it back to HIGH (voltage LOW). */ + /* Failure to do this would cause that subsequent call to */ + /* return immediately. */ + gpio_pin_set_dt(&cfg->dio_gpio, false); + /* switch to DIR_IN to read sensor signals */ gpio_pin_configure_dt(&cfg->dio_gpio, GPIO_INPUT);