2020-06-09 09:52:27 +00:00
|
|
|
#![no_main]
|
|
|
|
#![no_std]
|
|
|
|
|
|
|
|
use cortex_m::asm;
|
|
|
|
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: () = {
|
|
|
|
#[init]
|
|
|
|
fn init(_cx: init::Context) {
|
|
|
|
let board = dk::init().unwrap();
|
|
|
|
|
|
|
|
// `POWER` is a peripheral, or register block
|
|
|
|
let power = board.power;
|
|
|
|
|
|
|
|
// INTENSET is one of POWER's registers
|
2020-07-01 13:42:23 +00:00
|
|
|
// the `write()` method writes a (32-bit) value into the register
|
2020-06-09 09:52:27 +00:00
|
|
|
power.intenset.write(|w| {
|
|
|
|
// `w` is a "constructor" with methods to clear/set the bitfields of INTENSET
|
|
|
|
// `w` starts with all bitfields set to their reset value
|
|
|
|
// USBDETECTED is one of INTENSET's 1-bit fields
|
|
|
|
w.usbdetected().set_bit()
|
|
|
|
});
|
|
|
|
|
|
|
|
log::info!("USBDETECTED interrupt enabled");
|
2020-07-01 13:42:23 +00:00
|
|
|
|
|
|
|
// read the whole 32-bit usb supply register
|
|
|
|
// the `read()` method returns a reader which can then be used to access the register content
|
|
|
|
// in full or only specific bitfields (see below)
|
|
|
|
// (the layout of the USBREGSTATUS register can be found in section 5.3.7.13 of the PS)
|
|
|
|
let regstatus: u32 = power.usbregstatus.read().bits();
|
|
|
|
// ^^^^ complete register content
|
|
|
|
log::info!("USBREGSTATUS: {:b}", regstatus);
|
|
|
|
|
|
|
|
// read the 1-bit VBUSDETECT field that is part of the USBREGSTATUS register content
|
|
|
|
// to show that its contents reflect our usb connection status
|
|
|
|
// (the USBDETECTED event that will trigger `on_power_event()` is derived from this information)
|
|
|
|
let vbusdetect: bool = power.usbregstatus.read().vbusdetect().bits();
|
2020-07-10 15:30:19 +00:00
|
|
|
// ^^^^^^^^^^ bitfield name
|
2020-07-01 13:42:23 +00:00
|
|
|
log::info!("USBREGSTATUS.VBUSDETECT: {}", vbusdetect);
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[idle]
|
2020-06-25 17:14:39 +00:00
|
|
|
fn main(_cx: main::Context) -> ! {
|
2020-06-09 09:52:27 +00:00
|
|
|
log::info!("idle: going to sleep");
|
|
|
|
|
|
|
|
// sleep in the background
|
|
|
|
loop {
|
|
|
|
asm::wfi();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[task(binds = POWER_CLOCK)]
|
|
|
|
fn on_power_event(_cx: on_power_event::Context) {
|
|
|
|
log::info!("POWER event occurred");
|
2020-06-10 10:09:21 +00:00
|
|
|
dk::exit()
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
};
|