Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/rp2_common/hardware_watchdog/include/hardware/watchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 11 additions & 9 deletions src/rp2_common/hardware_watchdog/watchdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;

Expand Down
Loading