rp-hal-boards/boards/rp-pico/examples/pico_rtic_monotonic.rs
Wilfried Chauveau 305dfc9d1f
Add rtic-monotonic support for timer & alarms (feature gated) (#459)
* Add rtic-monotonic support for timer & alarms
* Force Alarm interrupt if timer is set too soon
* timer: Remove non_exhaustive attribute from ScheduleAlarmError
* timer: TooSoon is no longer emitted so it can be removed
2022-10-06 18:31:31 +01:00

93 lines
2.4 KiB
Rust

#![no_std]
#![no_main]
use panic_halt as _;
#[rtic::app(device = rp_pico::hal::pac, peripherals = true, dispatchers = [I2C0_IRQ])]
mod app {
use embedded_hal::digital::v2::OutputPin;
use fugit::ExtU64;
use fugit::MicrosDurationU32;
use rp_pico::{
hal::{
self,
clocks::init_clocks_and_plls,
timer::{monotonic::Monotonic, Alarm0},
watchdog::Watchdog,
Sio,
},
XOSC_CRYSTAL_FREQ,
};
const SCAN_TIME_US: MicrosDurationU32 = MicrosDurationU32::secs(1);
#[shared]
struct Shared {
led: hal::gpio::Pin<hal::gpio::pin::bank0::Gpio25, hal::gpio::PushPullOutput>,
}
#[monotonic(binds = TIMER_IRQ_0, default = true)]
type MyMono = Monotonic<Alarm0>;
#[local]
struct Local {}
#[init]
fn init(c: init::Context) -> (Shared, Local, init::Monotonics) {
// Soft-reset does not release the hardware spinlocks
// Release them now to avoid a deadlock after debug or watchdog reset
unsafe {
hal::sio::spinlock_reset();
}
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 alarm = timer.alarm_0().unwrap();
blink_led::spawn_after(500.millis()).unwrap();
(
Shared { led },
Local {},
init::Monotonics(Monotonic::new(timer, alarm)),
)
}
#[task(
shared = [led],
local = [tog: bool = true],
)]
fn blink_led(mut c: blink_led::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;
blink_led::spawn_after(500.millis()).unwrap();
}
}