From 2470b8a3a11a65fb3d574e8078b144124a99797c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sluka?= Date: Sat, 25 Jun 2022 19:46:56 +0200 Subject: [PATCH] Fix infinite loop on STM32F103 When trying to read temperature from a DHT11 when the sensor was disconnected, the program would encounter an infinite loop in DHT::expectPulse, because count would keep overflowing. --- DHT.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DHT.cpp b/DHT.cpp index b364612..a48ac74 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -360,7 +360,9 @@ bool DHT::read(bool force) { // in the very latest IDE versions): // https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/wiring_pulse.c uint32_t DHT::expectPulse(bool level) { -#if (F_CPU > 16000000L) +// F_CPU is not be known at compile time on platforms such as STM32F103. +// The preprocessor seems to evaluate it to zero in that case. +#if (F_CPU > 16000000L) || (F_CPU == 0L) uint32_t count = 0; #else uint16_t count = 0; // To work fast enough on slower AVR boards