rp-hal-boards/boards/rp-pico/examples/pico_rtic.rs
Jonathan Pallant (Ferrous Systems) 8f0a2788eb Renamed pico to rp-pico.
2021-12-23 11:18:52 +00:00

84 lines
2.2 KiB
Rust

#![no_std]
#![no_main]
use panic_halt as _;
#[rtic::app(device = rp_pico::hal::pac, peripherals = true)]
mod app {
use embedded_hal::digital::v2::OutputPin;
use embedded_time::duration::Extensions;
use rp_pico::{
hal::{self, clocks::init_clocks_and_plls, watchdog::Watchdog, Sio},
XOSC_CRYSTAL_FREQ,
};
const SCAN_TIME_US: u32 = 1000000;
#[shared]
struct Shared {
timer: hal::Timer,
alarm: hal::timer::Alarm0,
led: hal::gpio::Pin<hal::gpio::pin::bank0::Gpio25, hal::gpio::PushPullOutput>,
}
#[local]
struct Local {}
#[init]
fn init(c: init::Context) -> (Shared, Local, init::Monotonics) {
let mut resets = c.device.RESETS;
let mut watchdog = Watchdog::new(c.device.WATCHDOG);
let _clocks = init_clocks_and_plls(
XOSC_CRYSTAL_FREQ,
c.device.XOSC,
c.device.CLOCKS,
c.device.PLL_SYS,
c.device.PLL_USB,
&mut resets,
&mut watchdog,
)
.ok()
.unwrap();
let sio = Sio::new(c.device.SIO);
let pins = rp_pico::Pins::new(
c.device.IO_BANK0,
c.device.PADS_BANK0,
sio.gpio_bank0,
&mut resets,
);
let mut led = pins.led.into_push_pull_output();
led.set_low().unwrap();
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 { timer, alarm, led }, Local {}, init::Monotonics())
}
#[task(
binds = TIMER_IRQ_0,
priority = 1,
shared = [timer, alarm, led],
local = [tog: bool = true],
)]
fn timer_irq(mut c: timer_irq::Context) {
if *c.local.tog {
c.shared.led.lock(|l| l.set_high().unwrap());
} else {
c.shared.led.lock(|l| l.set_low().unwrap());
}
*c.local.tog = !*c.local.tog;
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());
});
}
}