diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-12-24 02:23:57 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-12-24 02:23:57 +0000 |
| commit | d1dd66cfcacd9a519188d7799d9c045673fff738 (patch) | |
| tree | 706ba64221eab7f8bdc8332281fb3098ef4fd1ed | |
| parent | 67a6e5accfce0963060a20418bab04495c5bf50d (diff) | |
| parent | e090ab19151fbea6736d5eac0e1497ef9c36626b (diff) | |
Merge #1126
1126: embassy-rp: Add Watchdog r=kalkyl a=kalkyl
Co-authored-by: kalkyl <[email protected]>
| -rw-r--r-- | embassy-rp/src/lib.rs | 3 | ||||
| -rw-r--r-- | embassy-rp/src/watchdog.rs | 111 | ||||
| -rw-r--r-- | examples/rp/src/bin/watchdog.rs | 48 |
3 files changed, 162 insertions, 0 deletions
diff --git a/embassy-rp/src/lib.rs b/embassy-rp/src/lib.rs index 0ea97aecf..9e99b2fbb 100644 --- a/embassy-rp/src/lib.rs +++ b/embassy-rp/src/lib.rs | |||
| @@ -36,6 +36,7 @@ pub mod clocks; | |||
| 36 | pub mod flash; | 36 | pub mod flash; |
| 37 | pub mod multicore; | 37 | pub mod multicore; |
| 38 | mod reset; | 38 | mod reset; |
| 39 | pub mod watchdog; | ||
| 39 | 40 | ||
| 40 | // Reexports | 41 | // Reexports |
| 41 | 42 | ||
| @@ -119,6 +120,8 @@ embassy_hal_common::peripherals! { | |||
| 119 | 120 | ||
| 120 | PIO0, | 121 | PIO0, |
| 121 | PIO1, | 122 | PIO1, |
| 123 | |||
| 124 | WATCHDOG, | ||
| 122 | } | 125 | } |
| 123 | 126 | ||
| 124 | #[link_section = ".boot2"] | 127 | #[link_section = ".boot2"] |
diff --git a/embassy-rp/src/watchdog.rs b/embassy-rp/src/watchdog.rs new file mode 100644 index 000000000..f4f165b29 --- /dev/null +++ b/embassy-rp/src/watchdog.rs | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | //! Watchdog | ||
| 2 | //! | ||
| 3 | //! The watchdog is a countdown timer that can restart parts of the chip if it reaches zero. This can be used to restart the | ||
| 4 | //! processor if software gets stuck in an infinite loop. The programmer must periodically write a value to the watchdog to | ||
| 5 | //! stop it from reaching zero. | ||
| 6 | //! | ||
| 7 | //! Credit: based on `rp-hal` implementation (also licensed Apache+MIT) | ||
| 8 | |||
| 9 | use core::marker::PhantomData; | ||
| 10 | |||
| 11 | use embassy_time::Duration; | ||
| 12 | |||
| 13 | use crate::pac; | ||
| 14 | use crate::peripherals::WATCHDOG; | ||
| 15 | |||
| 16 | /// Watchdog peripheral | ||
| 17 | pub struct Watchdog { | ||
| 18 | phantom: PhantomData<WATCHDOG>, | ||
| 19 | load_value: u32, // decremented by 2 per tick (µs) | ||
| 20 | } | ||
| 21 | |||
| 22 | impl Watchdog { | ||
| 23 | /// Create a new `Watchdog` | ||
| 24 | pub fn new(_watchdog: WATCHDOG) -> Self { | ||
| 25 | Self { | ||
| 26 | phantom: PhantomData, | ||
| 27 | load_value: 0, | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | /// Start tick generation on clk_tick which is driven from clk_ref. | ||
| 32 | /// | ||
| 33 | /// # Arguments | ||
| 34 | /// | ||
| 35 | /// * `cycles` - Total number of tick cycles before the next tick is generated. | ||
| 36 | /// It is expected to be the frequency in MHz of clk_ref. | ||
| 37 | pub fn enable_tick_generation(&mut self, cycles: u8) { | ||
| 38 | unsafe { | ||
| 39 | let watchdog = pac::WATCHDOG; | ||
| 40 | watchdog.tick().write(|w| { | ||
| 41 | w.set_enable(true); | ||
| 42 | w.set_cycles(cycles.into()) | ||
| 43 | }); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | /// Defines whether or not the watchdog timer should be paused when processor(s) are in debug mode | ||
| 48 | /// or when JTAG is accessing bus fabric | ||
| 49 | pub fn pause_on_debug(&mut self, pause: bool) { | ||
| 50 | unsafe { | ||
| 51 | let watchdog = pac::WATCHDOG; | ||
| 52 | watchdog.ctrl().write(|w| { | ||
| 53 | w.set_pause_dbg0(pause); | ||
| 54 | w.set_pause_dbg1(pause); | ||
| 55 | w.set_pause_jtag(pause); | ||
| 56 | }) | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | fn load_counter(&self, counter: u32) { | ||
| 61 | unsafe { | ||
| 62 | let watchdog = pac::WATCHDOG; | ||
| 63 | watchdog.load().write_value(pac::watchdog::regs::Load(counter)); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | fn enable(&self, bit: bool) { | ||
| 68 | unsafe { | ||
| 69 | let watchdog = pac::WATCHDOG; | ||
| 70 | watchdog.ctrl().write(|w| w.set_enable(bit)) | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | // Configure which hardware will be reset by the watchdog | ||
| 75 | // (everything except ROSC, XOSC) | ||
| 76 | unsafe fn configure_wdog_reset_triggers(&self) { | ||
| 77 | let psm = pac::PSM; | ||
| 78 | psm.wdsel().write_value(pac::psm::regs::Wdsel( | ||
| 79 | 0x0001ffff & !(0x01 << 0usize) & !(0x01 << 1usize), | ||
| 80 | )); | ||
| 81 | } | ||
| 82 | |||
| 83 | /// Feed the watchdog timer | ||
| 84 | pub fn feed(&mut self) { | ||
| 85 | self.load_counter(self.load_value) | ||
| 86 | } | ||
| 87 | |||
| 88 | /// Start the watchdog timer | ||
| 89 | pub fn start(&mut self, period: Duration) { | ||
| 90 | const MAX_PERIOD: u32 = 0xFFFFFF; | ||
| 91 | |||
| 92 | let delay_us = period.as_micros() as u32; | ||
| 93 | if delay_us > MAX_PERIOD / 2 { | ||
| 94 | panic!( | ||
| 95 | "Period cannot exceed maximum load value of {} ({} microseconds))", | ||
| 96 | MAX_PERIOD, | ||
| 97 | MAX_PERIOD / 2 | ||
| 98 | ); | ||
| 99 | } | ||
| 100 | // Due to a logic error, the watchdog decrements by 2 and | ||
| 101 | // the load value must be compensated; see RP2040-E1 | ||
| 102 | self.load_value = delay_us * 2; | ||
| 103 | |||
| 104 | self.enable(false); | ||
| 105 | unsafe { | ||
| 106 | self.configure_wdog_reset_triggers(); | ||
| 107 | } | ||
| 108 | self.load_counter(self.load_value); | ||
| 109 | self.enable(true); | ||
| 110 | } | ||
| 111 | } | ||
diff --git a/examples/rp/src/bin/watchdog.rs b/examples/rp/src/bin/watchdog.rs new file mode 100644 index 000000000..ece5cfe38 --- /dev/null +++ b/examples/rp/src/bin/watchdog.rs | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::info; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_rp::gpio; | ||
| 8 | use embassy_rp::watchdog::*; | ||
| 9 | use embassy_time::{Duration, Timer}; | ||
| 10 | use gpio::{Level, Output}; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | #[embassy_executor::main] | ||
| 14 | async fn main(_spawner: Spawner) { | ||
| 15 | let p = embassy_rp::init(Default::default()); | ||
| 16 | info!("Hello world!"); | ||
| 17 | |||
| 18 | let mut watchdog = Watchdog::new(p.WATCHDOG); | ||
| 19 | let mut led = Output::new(p.PIN_25, Level::Low); | ||
| 20 | |||
| 21 | // Set the LED high for 2 seconds so we know when we're about to start the watchdog | ||
| 22 | led.set_high(); | ||
| 23 | Timer::after(Duration::from_secs(2)).await; | ||
| 24 | |||
| 25 | // Set to watchdog to reset if it's not fed within 1.05 seconds, and start it | ||
| 26 | watchdog.start(Duration::from_millis(1_050)); | ||
| 27 | info!("Started the watchdog timer"); | ||
| 28 | |||
| 29 | // Blink once a second for 5 seconds, feed the watchdog timer once a second to avoid a reset | ||
| 30 | for _ in 1..=5 { | ||
| 31 | led.set_low(); | ||
| 32 | Timer::after(Duration::from_millis(500)).await; | ||
| 33 | led.set_high(); | ||
| 34 | Timer::after(Duration::from_millis(500)).await; | ||
| 35 | info!("Feeding watchdog"); | ||
| 36 | watchdog.feed(); | ||
| 37 | } | ||
| 38 | |||
| 39 | info!("Stopped feeding, device will reset in 1.05 seconds"); | ||
| 40 | // Blink 10 times per second, not feeding the watchdog. | ||
| 41 | // The processor should reset in 1.05 seconds. | ||
| 42 | loop { | ||
| 43 | led.set_low(); | ||
| 44 | Timer::after(Duration::from_millis(100)).await; | ||
| 45 | led.set_high(); | ||
| 46 | Timer::after(Duration::from_millis(100)).await; | ||
| 47 | } | ||
| 48 | } | ||
