embedded-trainings-2020/advanced/firmware/src/bin/resource.rs

54 lines
1.4 KiB
Rust
Raw Normal View History

2020-06-09 09:52:27 +00:00
#![no_main]
#![no_std]
use cortex_m::asm;
use dk::peripheral::POWER;
2021-04-12 15:09:38 +00:00
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use firmware as _;
2020-06-09 09:52:27 +00:00
#[rtic::app(device = dk)]
2020-06-09 09:52:27 +00:00
const APP: () = {
struct Resources {
power: POWER, // <- resource declaration
2020-06-09 09:52:27 +00:00
}
#[init]
fn init(_cx: init::Context) -> init::LateResources {
let board = dk::init().unwrap();
let power = board.power;
2020-06-09 09:52:27 +00:00
power.intenset.write(|w| w.usbdetected().set_bit());
2020-06-09 09:52:27 +00:00
2022-01-07 16:24:21 +00:00
defmt::println!("USBDETECTED interrupt enabled");
2020-06-09 09:52:27 +00:00
init::LateResources {
power, // <- resource initialization
}
}
2020-06-09 09:52:27 +00:00
#[idle]
fn main(_cx: main::Context) -> ! {
loop {
2022-01-07 16:24:21 +00:00
defmt::println!("idle: going to sleep");
asm::wfi();
2022-01-07 16:24:21 +00:00
defmt::println!("idle: woke up");
}
}
2020-06-09 09:52:27 +00:00
#[task(binds = POWER_CLOCK, resources = [power])]
// ^^^^^^^ resource access list
fn on_power_event(cx: on_power_event::Context) {
2022-01-07 16:24:21 +00:00
defmt::println!("POWER event occurred");
2020-06-09 09:52:27 +00:00
// resources available to this task
let resources = cx.resources;
// the POWER peripheral can be accessed through a reference
let power: &mut POWER = resources.power;
// clear the interrupt flag; otherwise this task will run again after it returns
power.events_usbdetected.reset();
2020-06-09 09:52:27 +00:00
}
};