From f10acec4c5581dbd6894c6a37d26352b142a7565 Mon Sep 17 00:00:00 2001 From: Tarim Date: Sun, 20 Jan 2019 02:51:15 +0000 Subject: [PATCH] Fix rare, intermittent, endless loop in timeouts Don't reduce millis() and timeout_start_ms to 16 bit as this causes wrapping every 65 seconds which can lead to endless loops from checkTimeoutExpired() if the sensor times out when the 16 bit millis() is within io_timeout of wrapping. --- VL53L0X.cpp | 2 +- VL53L0X.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/VL53L0X.cpp b/VL53L0X.cpp index fb2ed30..037e2b1 100644 --- a/VL53L0X.cpp +++ b/VL53L0X.cpp @@ -16,7 +16,7 @@ #define startTimeout() (timeout_start_ms = millis()) // Check if timeout is enabled (set to nonzero value) and has expired -#define checkTimeoutExpired() (io_timeout > 0 && ((uint16_t)millis() - timeout_start_ms) > io_timeout) +#define checkTimeoutExpired() (io_timeout > 0 && (millis() - timeout_start_ms) > io_timeout) // Decode VCSEL (vertical cavity surface emitting laser) pulse period in PCLKs // from register value diff --git a/VL53L0X.h b/VL53L0X.h index b531ff9..ef4c9ff 100644 --- a/VL53L0X.h +++ b/VL53L0X.h @@ -152,7 +152,7 @@ class VL53L0X uint8_t address; uint16_t io_timeout; bool did_timeout; - uint16_t timeout_start_ms; + uint32_t timeout_start_ms; uint8_t stop_variable; // read by init and used when starting measurement; is StopVariable field of VL53L0X_DevData_t structure in API uint32_t measurement_timing_budget_us;