Skip to content

Commit d45a090

Browse files
authored
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
1 parent 24af10a commit d45a090

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/rp2_common/hardware_watchdog/include/hardware/watchdog.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,26 @@ bool watchdog_enable_caused_reboot(void);
134134
* \if rp2040_specific
135135
* On RP2040 this method returns the last value set instead of the remaining time due to a h/w bug.
136136
* \endif
137-
*
137+
*
138138
* @return The number of microseconds before the watchdog will reboot the chip.
139139
*/
140+
uint32_t watchdog_get_time_remaining_us(void);
141+
142+
/**
143+
* \brief Returns the number of milliseconds before the watchdog will reboot the chip.
144+
* \ingroup hardware_watchdog
145+
*
146+
* \if rp2040_specific
147+
* On RP2040 this method returns the last value set instead of the remaining time due to a h/w bug.
148+
* \endif
149+
*
150+
* @return The number of milliseconds before the watchdog will reboot the chip.
151+
*/
140152
uint32_t watchdog_get_time_remaining_ms(void);
141153

142154
// backwards compatibility with SDK < 2.0.0
143155
static inline uint32_t watchdog_get_count(void) {
144-
return watchdog_get_time_remaining_ms();
156+
return watchdog_get_time_remaining_us();
145157
}
146158
#ifdef __cplusplus
147159
}

src/rp2_common/hardware_watchdog/watchdog.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,21 @@ void watchdog_update(void) {
2828
}
2929
// end::watchdog_update[]
3030

31-
uint32_t watchdog_get_time_remaining_ms(void) {
32-
return watchdog_hw->ctrl & WATCHDOG_CTRL_TIME_BITS;
33-
}
34-
3531
#if PICO_RP2040
36-
// Note, we have x2 here as the watchdog HW currently decrements twice per tick
32+
// Note, we have x2 here as the watchdog HW currently decrements twice per tick (errata RP2040-E1)
3733
#define WATCHDOG_XFACTOR 2
3834
#else
3935
#define WATCHDOG_XFACTOR 1
4036
#endif
37+
38+
uint32_t watchdog_get_time_remaining_us(void) {
39+
return (watchdog_hw->ctrl & WATCHDOG_CTRL_TIME_BITS) / WATCHDOG_XFACTOR;
40+
}
41+
42+
uint32_t watchdog_get_time_remaining_ms(void) {
43+
return (watchdog_hw->ctrl & WATCHDOG_CTRL_TIME_BITS) / (1000 * WATCHDOG_XFACTOR);
44+
}
45+
4146
// tag::watchdog_enable[]
4247
// Helper function used by both watchdog_enable and watchdog_reboot
4348
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) {
6065
if (!delay_ms) {
6166
hw_set_bits(&watchdog_hw->ctrl, WATCHDOG_CTRL_TRIGGER_BITS);
6267
} else {
63-
load_value = delay_ms * 1000;
64-
#if PICO_RP2040
65-
load_value *= 2;
66-
#endif
68+
load_value = delay_ms * (1000 * WATCHDOG_XFACTOR);
6769
if (load_value > WATCHDOG_LOAD_BITS)
6870
load_value = WATCHDOG_LOAD_BITS;
6971

0 commit comments

Comments
 (0)