2020-06-09 09:52:27 +00:00
|
|
|
#![no_main]
|
|
|
|
#![no_std]
|
|
|
|
|
|
|
|
use cortex_m::asm;
|
2020-06-16 08:16:25 +00:00
|
|
|
use dk::peripheral::POWER;
|
2020-06-09 09:52:27 +00:00
|
|
|
use panic_log as _; // panic handler
|
|
|
|
|
2020-06-12 15:54:45 +00:00
|
|
|
#[rtic::app(device = dk)]
|
2020-06-09 09:52:27 +00:00
|
|
|
const APP: () = {
|
|
|
|
struct Resources {
|
2020-06-16 08:16:25 +00:00
|
|
|
power: POWER,
|
2020-06-25 17:14:39 +00:00
|
|
|
counter: usize, // <- new resource
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[init]
|
|
|
|
fn init(_cx: init::Context) -> init::LateResources {
|
|
|
|
let board = dk::init().unwrap();
|
|
|
|
|
2020-06-16 08:16:25 +00:00
|
|
|
let power = board.power;
|
2020-06-09 09:52:27 +00:00
|
|
|
|
2020-06-16 08:16:25 +00:00
|
|
|
power.intenset.write(|w| w.usbdetected().set_bit());
|
2020-06-09 09:52:27 +00:00
|
|
|
|
2020-06-16 08:16:25 +00:00
|
|
|
log::info!("USBDETECTED interrupt enabled");
|
2020-06-09 09:52:27 +00:00
|
|
|
|
2020-06-25 17:14:39 +00:00
|
|
|
init::LateResources {
|
|
|
|
power,
|
|
|
|
counter: 0, // <- initialize the new resource
|
|
|
|
}
|
2020-06-16 08:16:25 +00:00
|
|
|
}
|
2020-06-09 09:52:27 +00:00
|
|
|
|
2020-06-16 08:16:25 +00:00
|
|
|
#[idle]
|
2020-06-25 17:14:39 +00:00
|
|
|
fn main(_cx: main::Context) -> ! {
|
2020-06-16 08:16:25 +00:00
|
|
|
loop {
|
|
|
|
log::info!("idle: going to sleep");
|
|
|
|
asm::wfi();
|
|
|
|
log::info!("idle: woke up");
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 09:52:27 +00:00
|
|
|
|
2020-06-25 17:14:39 +00:00
|
|
|
#[task(binds = POWER_CLOCK, resources = [power, counter])]
|
|
|
|
// ^^^^^^^ we want to access the resource from here
|
2020-06-16 08:16:25 +00:00
|
|
|
fn on_power_event(cx: on_power_event::Context) {
|
2020-06-25 17:14:39 +00:00
|
|
|
log::debug!("POWER event occurred");
|
|
|
|
|
|
|
|
let power = cx.resources.power;
|
|
|
|
let counter = cx.resources.counter;
|
|
|
|
|
|
|
|
*counter += 1;
|
|
|
|
let n = *counter;
|
|
|
|
log::info!(
|
|
|
|
"on_power_event: cable connected {} time{}",
|
|
|
|
n,
|
|
|
|
if n != 1 { "s" } else { "" }
|
|
|
|
);
|
2020-06-09 09:52:27 +00:00
|
|
|
|
2020-06-16 08:16:25 +00:00
|
|
|
// clear the interrupt flag; otherwise this task will run again after it returns
|
2020-06-25 17:14:39 +00:00
|
|
|
power.events_usbdetected.reset();
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
};
|