From 3edd81a94e7f3b45798e4b4a34c242ebb3eb76f5 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sat, 13 May 2023 00:33:00 +0200 Subject: [PATCH] rp/watchdog: fix overflow if period is longer than 4294 seconds. --- embassy-rp/src/watchdog.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/embassy-rp/src/watchdog.rs b/embassy-rp/src/watchdog.rs index f4f165b2..3631b2a9 100644 --- a/embassy-rp/src/watchdog.rs +++ b/embassy-rp/src/watchdog.rs @@ -89,14 +89,12 @@ impl Watchdog { pub fn start(&mut self, period: Duration) { const MAX_PERIOD: u32 = 0xFFFFFF; - let delay_us = period.as_micros() as u32; - if delay_us > MAX_PERIOD / 2 { - panic!( - "Period cannot exceed maximum load value of {} ({} microseconds))", - MAX_PERIOD, - MAX_PERIOD / 2 - ); + let delay_us = period.as_micros(); + if delay_us > (MAX_PERIOD / 2) as u64 { + panic!("Period cannot exceed {} microseconds", MAX_PERIOD / 2); } + let delay_us = delay_us as u32; + // Due to a logic error, the watchdog decrements by 2 and // the load value must be compensated; see RP2040-E1 self.load_value = delay_us * 2;