Skip to content

Commit 5c113b5

Browse files
Frogging101jic23
authored andcommitted
iio: dht11: Use usleep_range instead of msleep for start signal
The DHT22 (AM2302) datasheet specifies that the LOW start pulse should not exceed 20ms. However, observations with an oscilloscope of an RPi Model 2B (rev 1.1) communicating with a DHT22 sensor showed that the driver was consistently sending start pulses longer than 20ms: Kernel 4.7.10-v7+ (n=132): Minimum pulse length: 20.20ms Maximum: 29.84ms Mean: 24.96ms StDev: 2.82ms Sensor response rate: 100% Read success rate: 76% On kernel 4.8, the start pulse was so long that the sensor would not even respond 97% of the time: Kernel 4.8.16-v7+ (n=100): Minimum pulse length: 30.4ms Maximum: 74.4ms Mean: 39.3ms StDev: 10.2ms Sensor response rate: 3% Read success rate: 3% The driver would return ETIMEDOUT and write log messages like this: [ 51.430987] dht11 dht11@0: Only 1 signal edges detected [ 66.311019] dht11 dht11@0: Only 0 signal edges detected Replacing msleep(18) with usleep_range(18000, 20000) made the pulse length sane again and restored responsiveness: Kernel 4.8.16-v7+ with usleep_range (n=123): Minimum pulse length: 18.16ms Maximum: 20.20ms Mean: 19.85ms StDev: 0.51ms Sensor response rate: 100% Read success rate: 84% Cc: [email protected] Signed-off-by: John Brooks <[email protected]> Reviewed-by: Harald Geyer <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent d1aaf20 commit 5c113b5

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

drivers/iio/humidity/dht11.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
* a) select an implementation using busy loop polling on those systems
7272
* b) use the checksum to do some probabilistic decoding
7373
*/
74-
#define DHT11_START_TRANSMISSION 18 /* ms */
74+
#define DHT11_START_TRANSMISSION_MIN 18000 /* us */
75+
#define DHT11_START_TRANSMISSION_MAX 20000 /* us */
7576
#define DHT11_MIN_TIMERES 34000 /* ns */
7677
#define DHT11_THRESHOLD 49000 /* ns */
7778
#define DHT11_AMBIG_LOW 23000 /* ns */
@@ -228,7 +229,8 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
228229
ret = gpio_direction_output(dht11->gpio, 0);
229230
if (ret)
230231
goto err;
231-
msleep(DHT11_START_TRANSMISSION);
232+
usleep_range(DHT11_START_TRANSMISSION_MIN,
233+
DHT11_START_TRANSMISSION_MAX);
232234
ret = gpio_direction_input(dht11->gpio);
233235
if (ret)
234236
goto err;

0 commit comments

Comments
 (0)