From b1868123db2107a84ad093a6776e4f096829ba1a Mon Sep 17 00:00:00 2001 From: Samuel Sieb Date: Mon, 24 Jun 2024 04:21:28 -0700 Subject: [PATCH] fix potential hang (#6976) Co-authored-by: Samuel Sieb --- esphome/components/gpio/one_wire/gpio_one_wire.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/esphome/components/gpio/one_wire/gpio_one_wire.cpp b/esphome/components/gpio/one_wire/gpio_one_wire.cpp index b4e69e975a..36eaf2160a 100644 --- a/esphome/components/gpio/one_wire/gpio_one_wire.cpp +++ b/esphome/components/gpio/one_wire/gpio_one_wire.cpp @@ -94,13 +94,15 @@ bool HOT IRAM_ATTR GPIOOneWireBus::read_bit_() { // measure from start value directly, to get best accurate timing no matter // how long pin_mode/delayMicroseconds took - delayMicroseconds(12 - (micros() - start)); + uint32_t now = micros(); + if (now - start < 12) + delayMicroseconds(12 - (now - start)); // sample bus to read bit from peer bool r = pin_.digital_read(); // read slot is at least 60µs; get as close to 60µs to spend less time with interrupts locked - uint32_t now = micros(); + now = micros(); if (now - start < 60) delayMicroseconds(60 - (now - start));