Merge pull request #235 from camrbuss/rtic-timer-hal

Update Pico RTIC example to use timer alarm HAL
This commit is contained in:
Jonathan Pallant 2021-12-10 13:45:57 +00:00 committed by GitHub
commit 31285fe002
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 32 deletions

View file

@ -36,7 +36,7 @@ optional = true
[dev-dependencies] [dev-dependencies]
panic-halt= "0.2.0" panic-halt= "0.2.0"
embedded-hal ="0.2.5" embedded-hal ="0.2.5"
cortex-m-rtic = "0.6.0-alpha.5" cortex-m-rtic = "0.6.0-rc.4"
nb = "1.0" nb = "1.0"
i2c-pio = { git = "https://github.com/ithinuel/i2c-pio-rs", rev = "afc2dad0e955da2b712d7f7cd78c7af88ddc6a45" } i2c-pio = { git = "https://github.com/ithinuel/i2c-pio-rs", rev = "afc2dad0e955da2b712d7f7cd78c7af88ddc6a45" }

View file

@ -2,14 +2,14 @@
#![no_main] #![no_main]
use panic_halt as _; use panic_halt as _;
use rp2040_hal as hal;
#[rtic::app(device = crate::hal::pac, peripherals = true)] #[rtic::app(device = pico::hal::pac, peripherals = true)]
mod app { mod app {
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
use embedded_time::duration::Extensions;
use pico::{ 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, XOSC_CRYSTAL_FREQ,
}; };
@ -17,7 +17,8 @@ mod app {
#[shared] #[shared]
struct Shared { struct Shared {
scan_timer: pac::TIMER, timer: hal::Timer,
alarm: hal::timer::Alarm0,
led: hal::gpio::Pin<hal::gpio::pin::bank0::Gpio25, hal::gpio::PushPullOutput>, led: hal::gpio::Pin<hal::gpio::pin::bank0::Gpio25, hal::gpio::PushPullOutput>,
} }
@ -41,40 +42,30 @@ mod app {
.unwrap(); .unwrap();
let sio = Sio::new(c.device.SIO); let sio = Sio::new(c.device.SIO);
let pins = hal::gpio::Pins::new( let pins = pico::Pins::new(
c.device.IO_BANK0, c.device.IO_BANK0,
c.device.PADS_BANK0, c.device.PADS_BANK0,
sio.gpio_bank0, sio.gpio_bank0,
&mut resets, &mut resets,
); );
let mut led = pins.gpio25.into_push_pull_output(); let mut led = pins.led.into_push_pull_output();
led.set_low().unwrap(); led.set_low().unwrap();
let timer = c.device.TIMER; let mut timer = hal::Timer::new(c.device.TIMER, &mut resets);
timer.dbgpause.write(|w| w.dbg0().set_bit()); let mut alarm = timer.alarm_0().unwrap();
let current_time = timer.timelr.read().bits(); let _ = alarm.schedule(SCAN_TIME_US.microseconds());
timer alarm.enable_interrupt(&mut timer);
.alarm0
.write(|w| unsafe { w.bits(current_time + SCAN_TIME_US) });
timer.inte.write(|w| w.alarm_0().set_bit());
( (Shared { timer, alarm, led }, Local {}, init::Monotonics())
Shared {
scan_timer: timer,
led,
},
Local {},
init::Monotonics(),
)
} }
#[task( #[task(
binds = TIMER_IRQ_0, binds = TIMER_IRQ_0,
priority = 1, priority = 1,
shared = [scan_timer, led], shared = [timer, alarm, led],
local = [tog: bool = false], 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 { if *c.local.tog {
c.shared.led.lock(|l| l.set_high().unwrap()); c.shared.led.lock(|l| l.set_high().unwrap());
} else { } else {
@ -82,13 +73,11 @@ mod app {
} }
*c.local.tog = !*c.local.tog; *c.local.tog = !*c.local.tog;
let current_time = c.shared.scan_timer.lock(|t| t.timelr.read().bits()); let timer = c.shared.timer;
c.shared.scan_timer.lock(|t| { let alarm = c.shared.alarm;
t.alarm0 (timer, alarm).lock(|t, a| {
.write(|w| unsafe { w.bits(current_time + SCAN_TIME_US) }) 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()));
} }
} }