From f2f50d0fc3770ae4b6509a282dac26369ebc74a5 Mon Sep 17 00:00:00 2001 From: camrbuss Date: Tue, 7 Dec 2021 19:24:35 -0500 Subject: [PATCH] update pico rtic example to use timer alarm hal --- boards/pico/Cargo.toml | 2 +- boards/pico/examples/pico_rtic.rs | 48 ++++++++++++------------------- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/boards/pico/Cargo.toml b/boards/pico/Cargo.toml index 52f0b16..5dc22e6 100644 --- a/boards/pico/Cargo.toml +++ b/boards/pico/Cargo.toml @@ -36,7 +36,7 @@ optional = true [dev-dependencies] panic-halt= "0.2.0" embedded-hal ="0.2.5" -cortex-m-rtic = "0.6.0-alpha.5" +cortex-m-rtic = "0.6.0-rc.4" nb = "1.0" i2c-pio = { git = "https://github.com/ithinuel/i2c-pio-rs", rev = "afc2dad0e955da2b712d7f7cd78c7af88ddc6a45" } diff --git a/boards/pico/examples/pico_rtic.rs b/boards/pico/examples/pico_rtic.rs index ef957bb..74ead0c 100644 --- a/boards/pico/examples/pico_rtic.rs +++ b/boards/pico/examples/pico_rtic.rs @@ -8,8 +8,9 @@ use rp2040_hal as hal; mod app { use embedded_hal::digital::v2::OutputPin; + use embedded_time::duration::Extensions; use pico::{ - hal::{self, clocks::init_clocks_and_plls, pac, watchdog::Watchdog, Sio}, + hal::{self, clocks::init_clocks_and_plls, watchdog::Watchdog, Sio}, XOSC_CRYSTAL_FREQ, }; @@ -17,7 +18,8 @@ mod app { #[shared] struct Shared { - scan_timer: pac::TIMER, + timer: hal::Timer, + alarm: hal::timer::Alarm0, led: hal::gpio::Pin, } @@ -41,40 +43,30 @@ mod app { .unwrap(); let sio = Sio::new(c.device.SIO); - let pins = hal::gpio::Pins::new( + let pins = pico::Pins::new( c.device.IO_BANK0, c.device.PADS_BANK0, sio.gpio_bank0, &mut resets, ); - let mut led = pins.gpio25.into_push_pull_output(); + let mut led = pins.led.into_push_pull_output(); led.set_low().unwrap(); - let timer = c.device.TIMER; - timer.dbgpause.write(|w| w.dbg0().set_bit()); - let current_time = timer.timelr.read().bits(); - timer - .alarm0 - .write(|w| unsafe { w.bits(current_time + SCAN_TIME_US) }); - timer.inte.write(|w| w.alarm_0().set_bit()); + let mut timer = hal::Timer::new(c.device.TIMER, &mut resets); + let mut alarm = timer.alarm_0().unwrap(); + let _ = alarm.schedule(SCAN_TIME_US.microseconds()); + alarm.enable_interrupt(&mut timer); - ( - Shared { - scan_timer: timer, - led, - }, - Local {}, - init::Monotonics(), - ) + (Shared { timer, alarm, led }, Local {}, init::Monotonics()) } #[task( binds = TIMER_IRQ_0, priority = 1, - shared = [scan_timer, led], - local = [tog: bool = false], + shared = [timer, alarm, led], + local = [tog: bool = true], )] - fn scan_timer_irq(mut c: scan_timer_irq::Context) { + fn timer_irq(mut c: timer_irq::Context) { if *c.local.tog { c.shared.led.lock(|l| l.set_high().unwrap()); } else { @@ -82,13 +74,11 @@ mod app { } *c.local.tog = !*c.local.tog; - let current_time = c.shared.scan_timer.lock(|t| t.timelr.read().bits()); - c.shared.scan_timer.lock(|t| { - t.alarm0 - .write(|w| unsafe { w.bits(current_time + SCAN_TIME_US) }) + let timer = c.shared.timer; + let alarm = c.shared.alarm; + (timer, alarm).lock(|t, a| { + a.clear_interrupt(t); + let _ = a.schedule(SCAN_TIME_US.microseconds()); }); - c.shared - .scan_timer - .lock(|t| t.intr.write(|w| w.alarm_0().set_bit())); } }