start converting advanced apps

This commit is contained in:
Lotte Steenbrink 2021-04-12 17:09:38 +02:00 committed by Mirabellensaft
parent 11f82978e9
commit 9c22c61c67
8 changed files with 64 additions and 25 deletions

View file

@ -2,6 +2,7 @@
# (..)
rustflags = [
"-C", "linker=flip-link", # adds stack overflow protection
"-C", "link-arg=-Tdefmt.x", # defmt support
# (..)
]

View file

@ -17,15 +17,16 @@ cortex-m-rt = "0.6.13"
cortex-m-rtic = "0.5.1"
dk = { path = "../../boards/dk", features = ["advanced"] }
heapless = "0.5.5"
log = "0.4.8"
panic-log = { path = "../../common/panic-log" }
panic-probe = { version = "0.2.0", features = ["print-defmt"] }
defmt = "0.2.0"
defmt-rtt = "0.2.0"
usb = { path = "../common/usb" }
usb2 = "0.0.1"
# optimize code in both profiles
[profile.dev]
codegen-units = 1
debug = 1
debug = 2
debug-assertions = true # !
incremental = false
lto = "fat"
@ -40,3 +41,18 @@ incremental = false
lto = "fat"
opt-level = 3
overflow-checks = false
[features]
# set defmt logging levels here
default = [
"defmt-default",
# "dependency-a/defmt-trace",
]
# do NOT modify these features
defmt-default = []
defmt-trace = []
defmt-debug = []
defmt-info = []
defmt-warn = []
defmt-error = []

View file

@ -2,7 +2,8 @@
#![no_std]
use cortex_m::asm;
use panic_log as _; // panic handler
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use firmware as _;
#[rtic::app(device = dk)]
const APP: () = {
@ -22,7 +23,7 @@ const APP: () = {
w.usbdetected().set_bit()
});
log::info!("USBDETECTED interrupt enabled");
defmt::info!("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
@ -30,19 +31,19 @@ const APP: () = {
// (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);
defmt::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();
// ^^^^^^^^^^ bitfield name
log::info!("USBREGSTATUS.VBUSDETECT: {}", vbusdetect);
defmt::info!("USBREGSTATUS.VBUSDETECT: {}", vbusdetect);
}
#[idle]
fn main(_cx: main::Context) -> ! {
log::info!("idle: going to sleep");
defmt::info!("idle: going to sleep");
// sleep in the background
loop {
@ -52,7 +53,7 @@ const APP: () = {
#[task(binds = POWER_CLOCK)]
fn on_power_event(_cx: on_power_event::Context) {
log::info!("POWER event occurred");
defmt::info!("POWER event occurred");
dk::exit()
}
};

View file

@ -3,14 +3,15 @@
use cortex_m::asm;
use cortex_m_rt::entry;
use panic_log as _; // panic handler
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use firmware as _;
#[entry]
fn main() -> ! {
// board initialization
dk::init().unwrap();
log::info!("Hello, world!");
defmt::info!("Hello, world!");
loop {
asm::bkpt();

View file

@ -3,7 +3,8 @@
use cortex_m::asm;
use dk::peripheral::POWER;
use panic_log as _; // panic handler
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use firmware as _;
#[rtic::app(device = dk)]
const APP: () = {
@ -20,7 +21,7 @@ const APP: () = {
power.intenset.write(|w| w.usbdetected().set_bit());
log::info!("USBDETECTED interrupt enabled");
defmt::info!("USBDETECTED interrupt enabled");
init::LateResources {
power,
@ -31,23 +32,23 @@ const APP: () = {
#[idle]
fn main(_cx: main::Context) -> ! {
loop {
log::info!("idle: going to sleep");
defmt::info!("idle: going to sleep");
asm::wfi();
log::info!("idle: woke up");
defmt::info!("idle: woke up");
}
}
#[task(binds = POWER_CLOCK, resources = [power, counter])]
// ^^^^^^^ we want to access the resource from here
fn on_power_event(cx: on_power_event::Context) {
log::debug!("POWER event occurred");
defmt::debug!("POWER event occurred");
let power = cx.resources.power;
let counter = cx.resources.counter;
*counter += 1;
let n = *counter;
log::info!(
defmt::info!(
"on_power_event: cable connected {} time{}",
n,
if n != 1 { "s" } else { "" }

View file

@ -3,7 +3,8 @@
use cortex_m::asm;
use dk::peripheral::POWER;
use panic_log as _; // panic handler
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use firmware as _;
#[rtic::app(device = dk)]
const APP: () = {
@ -19,7 +20,7 @@ const APP: () = {
power.intenset.write(|w| w.usbdetected().set_bit());
log::info!("USBDETECTED interrupt enabled");
defmt::info!("USBDETECTED interrupt enabled");
init::LateResources {
power, // <- resource initialization
@ -29,16 +30,16 @@ const APP: () = {
#[idle]
fn main(_cx: main::Context) -> ! {
loop {
log::info!("idle: going to sleep");
defmt::info!("idle: going to sleep");
asm::wfi();
log::info!("idle: woke up");
defmt::info!("idle: woke up");
}
}
#[task(binds = POWER_CLOCK, resources = [power])]
// ^^^^^^^ resource access list
fn on_power_event(cx: on_power_event::Context) {
log::info!("POWER event occurred");
defmt::info!("POWER event occurred");
// resources available to this task
let resources = cx.resources;

View file

@ -2,7 +2,8 @@
#![no_std]
use cortex_m::asm;
use panic_log as _; // panic handler
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use firmware as _;
#[rtic::app(device = dk)]
const APP: () = {
@ -10,12 +11,12 @@ const APP: () = {
fn init(_cx: init::Context) {
dk::init().unwrap();
log::info!("Hello");
defmt::info!("Hello");
}
#[idle]
fn main(_cx: main::Context) -> ! {
log::info!("world!");
defmt::info!("world!");
loop {
asm::bkpt();

View file

@ -0,0 +1,17 @@
#![no_std]
use panic_probe as _;
// same panicking *behavior* as `panic-probe` but doesn't print a panic message
// this prevents the panic message being printed *twice* when `defmt::panic` is invoked
#[defmt::panic_handler]
fn panic() -> ! {
cortex_m::asm::udf()
}
/// Terminates the application and makes `probe-run` exit with exit-code = 0
pub fn exit() -> ! {
loop {
cortex_m::asm::bkpt();
}
}