Fix ms/us confusion around watchdog time_remaining functions (#2533)

Fix documention for watchdog_get_time_remaining_ms and add a new watchdog_get_time_remaining_us
Fixes #2496
This commit is contained in:
Andrew Scheller 2025-06-19 14:43:22 +01:00 committed by GitHub
parent 24af10a6a2
commit d45a09039c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 11 deletions

View file

@ -137,11 +137,23 @@ bool watchdog_enable_caused_reboot(void);
* *
* @return The number of microseconds before the watchdog will reboot the chip. * @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); uint32_t watchdog_get_time_remaining_ms(void);
// backwards compatibility with SDK < 2.0.0 // backwards compatibility with SDK < 2.0.0
static inline uint32_t watchdog_get_count(void) { static inline uint32_t watchdog_get_count(void) {
return watchdog_get_time_remaining_ms(); return watchdog_get_time_remaining_us();
} }
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -28,16 +28,21 @@ void watchdog_update(void) {
} }
// end::watchdog_update[] // end::watchdog_update[]
uint32_t watchdog_get_time_remaining_ms(void) {
return watchdog_hw->ctrl & WATCHDOG_CTRL_TIME_BITS;
}
#if PICO_RP2040 #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 #define WATCHDOG_XFACTOR 2
#else #else
#define WATCHDOG_XFACTOR 1 #define WATCHDOG_XFACTOR 1
#endif #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[] // tag::watchdog_enable[]
// Helper function used by both watchdog_enable and watchdog_reboot // Helper function used by both watchdog_enable and watchdog_reboot
void _watchdog_enable(uint32_t delay_ms, bool pause_on_debug) { 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) { if (!delay_ms) {
hw_set_bits(&watchdog_hw->ctrl, WATCHDOG_CTRL_TRIGGER_BITS); hw_set_bits(&watchdog_hw->ctrl, WATCHDOG_CTRL_TRIGGER_BITS);
} else { } else {
load_value = delay_ms * 1000; load_value = delay_ms * (1000 * WATCHDOG_XFACTOR);
#if PICO_RP2040
load_value *= 2;
#endif
if (load_value > WATCHDOG_LOAD_BITS) if (load_value > WATCHDOG_LOAD_BITS)
load_value = WATCHDOG_LOAD_BITS; load_value = WATCHDOG_LOAD_BITS;