From f83739372476eb8c39688b72cf246f9f699abee1 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Thu, 19 Jun 2025 11:25:57 +0100 Subject: [PATCH] Fix ms/us confusion around watchdog time_remaining functions Fix documention for watchdog_get_time_remaining_ms and add a new watchdog_get_time_remaining_us Fixes #2496 --- .../include/hardware/watchdog.h | 16 +++++++++++++-- src/rp2_common/hardware_watchdog/watchdog.c | 20 ++++++++++--------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/rp2_common/hardware_watchdog/include/hardware/watchdog.h b/src/rp2_common/hardware_watchdog/include/hardware/watchdog.h index 7ebfa60cc..b11fd78d5 100644 --- a/src/rp2_common/hardware_watchdog/include/hardware/watchdog.h +++ b/src/rp2_common/hardware_watchdog/include/hardware/watchdog.h @@ -134,14 +134,26 @@ bool watchdog_enable_caused_reboot(void); * \if rp2040_specific * On RP2040 this method returns the last value set instead of the remaining time due to a h/w bug. * \endif - * + * * @return The number of microseconds before the watchdog will reboot the chip. */ +uint32_t watchdog_get_time_remaining_us(void); + +/** + * \brief Returns the number of milliseconds before the watchdog will reboot the chip. + * \ingroup hardware_watchdog + * + * \if rp2040_specific + * On RP2040 this method returns the last value set instead of the remaining time due to a h/w bug. + * \endif + * + * @return The number of milliseconds before the watchdog will reboot the chip. + */ uint32_t watchdog_get_time_remaining_ms(void); // backwards compatibility with SDK < 2.0.0 static inline uint32_t watchdog_get_count(void) { - return watchdog_get_time_remaining_ms(); + return watchdog_get_time_remaining_us(); } #ifdef __cplusplus } diff --git a/src/rp2_common/hardware_watchdog/watchdog.c b/src/rp2_common/hardware_watchdog/watchdog.c index 45528ce43..c03620c14 100644 --- a/src/rp2_common/hardware_watchdog/watchdog.c +++ b/src/rp2_common/hardware_watchdog/watchdog.c @@ -28,16 +28,21 @@ void watchdog_update(void) { } // end::watchdog_update[] -uint32_t watchdog_get_time_remaining_ms(void) { - return watchdog_hw->ctrl & WATCHDOG_CTRL_TIME_BITS; -} - #if PICO_RP2040 -// Note, we have x2 here as the watchdog HW currently decrements twice per tick +// Note, we have x2 here as the watchdog HW currently decrements twice per tick (errata RP2040-E1) #define WATCHDOG_XFACTOR 2 #else #define WATCHDOG_XFACTOR 1 #endif + +uint32_t watchdog_get_time_remaining_us(void) { + return (watchdog_hw->ctrl & WATCHDOG_CTRL_TIME_BITS) / WATCHDOG_XFACTOR; +} + +uint32_t watchdog_get_time_remaining_ms(void) { + return (watchdog_hw->ctrl & WATCHDOG_CTRL_TIME_BITS) / (1000 * WATCHDOG_XFACTOR); +} + // tag::watchdog_enable[] // Helper function used by both watchdog_enable and watchdog_reboot void _watchdog_enable(uint32_t delay_ms, bool pause_on_debug) { @@ -60,10 +65,7 @@ void _watchdog_enable(uint32_t delay_ms, bool pause_on_debug) { if (!delay_ms) { hw_set_bits(&watchdog_hw->ctrl, WATCHDOG_CTRL_TRIGGER_BITS); } else { - load_value = delay_ms * 1000; -#if PICO_RP2040 - load_value *= 2; -#endif + load_value = delay_ms * (1000 * WATCHDOG_XFACTOR); if (load_value > WATCHDOG_LOAD_BITS) load_value = WATCHDOG_LOAD_BITS;