Panic if period exceeds maximum value, fmt

This commit is contained in:
jspaulsen 2021-05-17 12:06:12 -07:00 committed by 9names
parent cdd9a553ad
commit c568f0d3df

View file

@ -1,12 +1,8 @@
//! Watchdog //! Watchdog
// See [Chapter 4 Section 7](https://datasheets.raspberrypi.org/rp2040/rp2040_datasheet.pdf) for more details // See [Chapter 4 Section 7](https://datasheets.raspberrypi.org/rp2040/rp2040_datasheet.pdf) for more details
use embedded_hal::watchdog;
use embedded_time::{
duration,
fixed_point::FixedPoint,
};
use crate::pac::WATCHDOG; use crate::pac::WATCHDOG;
use embedded_hal::watchdog;
use embedded_time::{duration, fixed_point::FixedPoint};
/// ///
pub struct Watchdog { pub struct Watchdog {
@ -34,27 +30,22 @@ impl Watchdog {
/// ///
pub fn pause_on_debug(&mut self, pause: bool) { pub fn pause_on_debug(&mut self, pause: bool) {
self.watchdog self.watchdog.ctrl.write(|w| {
.ctrl w.pause_dbg0()
.write(|w| { .bit(pause)
w .pause_dbg1()
.pause_dbg0().bit(pause) .bit(pause)
.pause_dbg1().bit(pause) .pause_jtag()
.pause_jtag().bit(pause) .bit(pause)
}) })
} }
fn load_counter(&self, counter: u32) { fn load_counter(&self, counter: u32) {
self.watchdog self.watchdog.load.write(|w| unsafe { w.bits(counter) });
.load
.write(|w| unsafe { w.bits(counter)});
} }
fn enable(&self, bit: bool) { fn enable(&self, bit: bool) {
self.watchdog self.watchdog.ctrl.write(|w| w.enable().bit(bit))
.ctrl
.write(|w| w.enable().bit(bit))
} }
} }
@ -67,12 +58,16 @@ impl watchdog::Watchdog for Watchdog {
impl watchdog::WatchdogEnable for Watchdog { impl watchdog::WatchdogEnable for Watchdog {
type Time = duration::Microseconds; type Time = duration::Microseconds;
/// Due to a logic error, the watchdog decrements by 2 and
/// value must be compensated; see RP2040-E1
fn start<T: Into<Self::Time>>(&mut self, period: T) { fn start<T: Into<Self::Time>>(&mut self, period: T) {
self.delay_ms = period const MAX_PERIOD: u32 = 0x7FFFFF;
.into()
.integer() * 2; // Due to a logic error, the watchdog decrements by 2 and
// the load value must be compensated; see RP2040-E1
self.delay_ms = period.into().integer() * 2;
if self.delay_ms > MAX_PERIOD {
panic!("Period cannot exceed maximum load value of {}", MAX_PERIOD);
}
self.enable(false); self.enable(false);
self.load_counter(self.delay_ms); self.load_counter(self.delay_ms);