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

72 lines
2.4 KiB
Rust
Raw Normal View History

2020-06-09 09:52:27 +00:00
#![no_main]
#![no_std]
2022-01-07 17:23:35 +00:00
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
2022-01-07 17:23:35 +00:00
#[rtic::app(device = dk, peripherals = false)]
mod app {
use cortex_m::asm;
#[local]
struct MyLocalResources {
}
#[shared]
struct MySharedResources {
}
2020-06-09 09:52:27 +00:00
#[init]
2022-01-07 17:23:35 +00:00
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
2020-06-09 09:52:27 +00:00
let board = dk::init().unwrap();
// `POWER` is a peripheral, or register block
let power = board.power;
// INTENSET is one of POWER's registers
// 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()
});
2022-01-07 16:24:21 +00:00
defmt::println!("USBDETECTED interrupt enabled");
// 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
2022-01-07 16:24:21 +00:00
defmt::println!("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
2022-01-07 16:24:21 +00:00
defmt::println!("USBREGSTATUS.VBUSDETECT: {}", vbusdetect);
2022-01-07 17:23:35 +00:00
(MySharedResources {}, MyLocalResources {}, init::Monotonics())
2020-06-09 09:52:27 +00:00
}
#[idle]
2022-01-07 17:23:35 +00:00
fn idle(_cx: idle::Context) -> ! {
2022-01-07 16:24:21 +00:00
defmt::println!("idle: going to sleep");
2020-06-09 09:52:27 +00:00
// sleep in the background
loop {
asm::wfi();
}
}
#[task(binds = POWER_CLOCK)]
fn on_power_event(_cx: on_power_event::Context) {
2022-01-07 16:24:21 +00:00
defmt::println!("POWER event occurred");
asm::bkpt();
2020-06-09 09:52:27 +00:00
}
2022-01-07 17:23:35 +00:00
}