Skip to content

Commit 689496e

Browse files
committed
timer: Check the timer's source is ref_clk ÷ watchdog tick.
1 parent 4b9722c commit 689496e

File tree

7 files changed

+25
-9
lines changed

7 files changed

+25
-9
lines changed

rp2040-hal/examples/adc_fifo_dma.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn main() -> ! {
146146
adc_fifo.resume();
147147

148148
// initialize a timer, to measure the total sampling time (printed below)
149-
let timer = hal::Timer::new(pac.TIMER, &mut pac.RESETS, &clocks.reference_clock);
149+
let timer = hal::Timer::new(pac.TIMER, &mut pac.RESETS, &watchdog, &clocks.reference_clock);
150150

151151
// NOTE: in a real-world program, instead of calling `wait` now, you would probably:
152152
// 1. Enable one of the DMA interrupts for the channel (e.g. `dma.ch0.enable_irq0()`)

rp2040-hal/examples/adc_fifo_poll.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn main() -> ! {
133133
let mut i = 0;
134134

135135
// initialize a timer, to measure the total sampling time (printed below)
136-
let timer = hal::Timer::new(pac.TIMER, &mut pac.RESETS, &clocks.reference_clock);
136+
let timer = hal::Timer::new(pac.TIMER, &mut pac.RESETS, &watchdog, &clocks.reference_clock);
137137

138138
loop {
139139
// busy-wait until the FIFO contains at least two samples:

rp2040-hal/examples/blinky.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn main() -> ! {
6464
.ok()
6565
.unwrap();
6666

67-
let mut timer = rp2040_hal::Timer::new(pac.TIMER, &mut pac.RESETS, &clocks.reference_clock);
67+
let mut timer = rp2040_hal::Timer::new(pac.TIMER, &mut pac.RESETS, &watchdog, &clocks.reference_clock);
6868

6969
// The single-cycle I/O block controls our GPIO pins
7070
let sio = hal::Sio::new(pac.SIO);

rp2040-hal/examples/vector_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn main() -> ! {
110110
// Configure GPIO25 as an output
111111
let led_pin = pins.gpio25.into_push_pull_output();
112112

113-
let mut timer = hal::Timer::new(pac.TIMER, &mut pac.RESETS, &clocks.reference_clock);
113+
let mut timer = hal::Timer::new(pac.TIMER, &mut pac.RESETS, &watchdog, &clocks.reference_clock);
114114
critical_section::with(|cs| {
115115
let mut alarm = timer.alarm_0().unwrap();
116116
// Schedule an alarm in 1 second

rp2040-hal/src/clocks/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ pub fn init_clocks_and_plls(
330330
let xosc = setup_xosc_blocking(xosc_dev, xosc_crystal_freq.Hz()).map_err(InitError::XoscErr)?;
331331

332332
// Configure watchdog tick generation to tick over every microsecond
333-
watchdog.enable_tick_generation((xosc_crystal_freq / 1_000_000) as u8);
333+
watchdog.enable_tick_generation((xosc_crystal_freq / 1_000_000) as u16);
334334

335335
let mut clocks = ClocksManager::new(clocks_dev);
336336

rp2040-hal/src/timer.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use crate::{
1616
clocks::ReferenceClock,
1717
pac::{self, RESETS, TIMER},
1818
resets::SubsystemReset,
19-
typelevel::Sealed, Clock,
19+
typelevel::Sealed,
20+
Clock, Watchdog,
2021
};
2122

2223
/// Instant type used by the Timer & Alarm methods.
@@ -59,8 +60,16 @@ impl Timer {
5960
/// Make sure that clocks and watchdog are configured, so
6061
/// that timer ticks happen at a frequency of 1MHz.
6162
/// Otherwise, `Timer` won't work as expected.
62-
pub fn new(timer: TIMER, resets: &mut RESETS, clocks: &ReferenceClock) -> Self {
63-
assert_eq!(clocks.freq().to_Hz(), 1_000_000);
63+
pub fn new(
64+
timer: TIMER,
65+
resets: &mut RESETS,
66+
watchdog: &Watchdog,
67+
clocks: &ReferenceClock,
68+
) -> Self {
69+
assert_eq!(
70+
clocks.freq().to_Hz() / u32::from(watchdog.cycles_per_ticks()),
71+
1_000_000
72+
);
6473
timer.reset_bring_down(resets);
6574
timer.reset_bring_up(resets);
6675
Self { _private: () }

rp2040-hal/src/watchdog.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,21 @@ impl Watchdog {
6060
///
6161
/// * `cycles` - Total number of tick cycles before the next tick is generated.
6262
/// It is expected to be the frequency in MHz of clk_ref.
63-
pub fn enable_tick_generation(&mut self, cycles: u8) {
63+
pub fn enable_tick_generation(&mut self, cycles: u16) {
6464
const WATCHDOG_TICK_ENABLE_BITS: u32 = 0x200;
6565

66+
assert_eq!(cycles & !0x1FF, 0);
67+
6668
self.watchdog
6769
.tick
6870
.write(|w| unsafe { w.bits(WATCHDOG_TICK_ENABLE_BITS | cycles as u32) })
6971
}
7072

73+
/// Returns the number of `clk_ref` cycles between each watchdog (and Timer) ticks.
74+
pub fn cycles_per_ticks(&self) -> u16 {
75+
self.watchdog.tick.read().cycles().bits()
76+
}
77+
7178
/// Defines whether or not the watchdog timer should be paused when processor(s) are in debug mode
7279
/// or when JTAG is accessing bus fabric
7380
///

0 commit comments

Comments
 (0)