2021-09-14 23:59:55 +10:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
|
|
|
use panic_halt as _;
|
|
|
|
|
2021-12-23 22:18:52 +11:00
|
|
|
#[rtic::app(device = rp_pico::hal::pac, peripherals = true)]
|
2021-09-14 23:59:55 +10:00
|
|
|
mod app {
|
|
|
|
|
|
|
|
use embedded_hal::digital::v2::OutputPin;
|
2021-12-08 11:24:35 +11:00
|
|
|
use embedded_time::duration::Extensions;
|
2021-12-23 22:18:52 +11:00
|
|
|
use rp_pico::{
|
2021-12-08 11:24:35 +11:00
|
|
|
hal::{self, clocks::init_clocks_and_plls, watchdog::Watchdog, Sio},
|
2021-09-14 23:59:55 +10:00
|
|
|
XOSC_CRYSTAL_FREQ,
|
|
|
|
};
|
|
|
|
|
|
|
|
const SCAN_TIME_US: u32 = 1000000;
|
|
|
|
|
|
|
|
#[shared]
|
|
|
|
struct Shared {
|
2021-12-08 11:24:35 +11:00
|
|
|
timer: hal::Timer,
|
|
|
|
alarm: hal::timer::Alarm0,
|
2021-09-14 23:59:55 +10:00
|
|
|
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);
|
2021-12-23 22:18:52 +11:00
|
|
|
let pins = rp_pico::Pins::new(
|
2021-09-14 23:59:55 +10:00
|
|
|
c.device.IO_BANK0,
|
|
|
|
c.device.PADS_BANK0,
|
|
|
|
sio.gpio_bank0,
|
|
|
|
&mut resets,
|
|
|
|
);
|
2021-12-08 11:24:35 +11:00
|
|
|
let mut led = pins.led.into_push_pull_output();
|
2021-09-14 23:59:55 +10:00
|
|
|
led.set_low().unwrap();
|
|
|
|
|
2021-12-08 11:24:35 +11:00
|
|
|
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());
|
2022-01-30 23:04:16 +11:00
|
|
|
alarm.enable_interrupt();
|
2021-09-14 23:59:55 +10:00
|
|
|
|
2021-12-08 11:24:35 +11:00
|
|
|
(Shared { timer, alarm, led }, Local {}, init::Monotonics())
|
2021-09-14 23:59:55 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[task(
|
|
|
|
binds = TIMER_IRQ_0,
|
|
|
|
priority = 1,
|
2021-12-08 11:24:35 +11:00
|
|
|
shared = [timer, alarm, led],
|
|
|
|
local = [tog: bool = true],
|
2021-09-14 23:59:55 +10:00
|
|
|
)]
|
2021-12-08 11:24:35 +11:00
|
|
|
fn timer_irq(mut c: timer_irq::Context) {
|
2021-09-14 23:59:55 +10:00
|
|
|
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;
|
|
|
|
|
2022-01-30 23:04:16 +11:00
|
|
|
let mut alarm = c.shared.alarm;
|
|
|
|
(alarm).lock(|a| {
|
|
|
|
a.clear_interrupt();
|
2021-12-08 11:24:35 +11:00
|
|
|
let _ = a.schedule(SCAN_TIME_US.microseconds());
|
2021-09-14 23:59:55 +10:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|