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 = [ rustflags = [
"-C", "linker=flip-link", # adds stack overflow protection "-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" cortex-m-rtic = "0.5.1"
dk = { path = "../../boards/dk", features = ["advanced"] } dk = { path = "../../boards/dk", features = ["advanced"] }
heapless = "0.5.5" heapless = "0.5.5"
log = "0.4.8" panic-probe = { version = "0.2.0", features = ["print-defmt"] }
panic-log = { path = "../../common/panic-log" } defmt = "0.2.0"
defmt-rtt = "0.2.0"
usb = { path = "../common/usb" } usb = { path = "../common/usb" }
usb2 = "0.0.1" usb2 = "0.0.1"
# optimize code in both profiles # optimize code in both profiles
[profile.dev] [profile.dev]
codegen-units = 1 codegen-units = 1
debug = 1 debug = 2
debug-assertions = true # ! debug-assertions = true # !
incremental = false incremental = false
lto = "fat" lto = "fat"
@ -40,3 +41,18 @@ incremental = false
lto = "fat" lto = "fat"
opt-level = 3 opt-level = 3
overflow-checks = false 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] #![no_std]
use cortex_m::asm; 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)] #[rtic::app(device = dk)]
const APP: () = { const APP: () = {
@ -22,7 +23,7 @@ const APP: () = {
w.usbdetected().set_bit() w.usbdetected().set_bit()
}); });
log::info!("USBDETECTED interrupt enabled"); defmt::info!("USBDETECTED interrupt enabled");
// read the whole 32-bit usb supply register // read the whole 32-bit usb supply register
// the `read()` method returns a reader which can then be used to access the register content // 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) // (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(); let regstatus: u32 = power.usbregstatus.read().bits();
// ^^^^ complete register content // ^^^^ 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 // read the 1-bit VBUSDETECT field that is part of the USBREGSTATUS register content
// to show that its contents reflect our usb connection status // to show that its contents reflect our usb connection status
// (the USBDETECTED event that will trigger `on_power_event()` is derived from this information) // (the USBDETECTED event that will trigger `on_power_event()` is derived from this information)
let vbusdetect: bool = power.usbregstatus.read().vbusdetect().bits(); let vbusdetect: bool = power.usbregstatus.read().vbusdetect().bits();
// ^^^^^^^^^^ bitfield name // ^^^^^^^^^^ bitfield name
log::info!("USBREGSTATUS.VBUSDETECT: {}", vbusdetect); defmt::info!("USBREGSTATUS.VBUSDETECT: {}", vbusdetect);
} }
#[idle] #[idle]
fn main(_cx: main::Context) -> ! { fn main(_cx: main::Context) -> ! {
log::info!("idle: going to sleep"); defmt::info!("idle: going to sleep");
// sleep in the background // sleep in the background
loop { loop {
@ -52,7 +53,7 @@ const APP: () = {
#[task(binds = POWER_CLOCK)] #[task(binds = POWER_CLOCK)]
fn on_power_event(_cx: on_power_event::Context) { fn on_power_event(_cx: on_power_event::Context) {
log::info!("POWER event occurred"); defmt::info!("POWER event occurred");
dk::exit() dk::exit()
} }
}; };

View file

@ -3,14 +3,15 @@
use cortex_m::asm; use cortex_m::asm;
use cortex_m_rt::entry; 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] #[entry]
fn main() -> ! { fn main() -> ! {
// board initialization // board initialization
dk::init().unwrap(); dk::init().unwrap();
log::info!("Hello, world!"); defmt::info!("Hello, world!");
loop { loop {
asm::bkpt(); asm::bkpt();

View file

@ -3,7 +3,8 @@
use cortex_m::asm; use cortex_m::asm;
use dk::peripheral::POWER; 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)] #[rtic::app(device = dk)]
const APP: () = { const APP: () = {
@ -20,7 +21,7 @@ const APP: () = {
power.intenset.write(|w| w.usbdetected().set_bit()); power.intenset.write(|w| w.usbdetected().set_bit());
log::info!("USBDETECTED interrupt enabled"); defmt::info!("USBDETECTED interrupt enabled");
init::LateResources { init::LateResources {
power, power,
@ -31,23 +32,23 @@ const APP: () = {
#[idle] #[idle]
fn main(_cx: main::Context) -> ! { fn main(_cx: main::Context) -> ! {
loop { loop {
log::info!("idle: going to sleep"); defmt::info!("idle: going to sleep");
asm::wfi(); asm::wfi();
log::info!("idle: woke up"); defmt::info!("idle: woke up");
} }
} }
#[task(binds = POWER_CLOCK, resources = [power, counter])] #[task(binds = POWER_CLOCK, resources = [power, counter])]
// ^^^^^^^ we want to access the resource from here // ^^^^^^^ we want to access the resource from here
fn on_power_event(cx: on_power_event::Context) { 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 power = cx.resources.power;
let counter = cx.resources.counter; let counter = cx.resources.counter;
*counter += 1; *counter += 1;
let n = *counter; let n = *counter;
log::info!( defmt::info!(
"on_power_event: cable connected {} time{}", "on_power_event: cable connected {} time{}",
n, n,
if n != 1 { "s" } else { "" } if n != 1 { "s" } else { "" }

View file

@ -3,7 +3,8 @@
use cortex_m::asm; use cortex_m::asm;
use dk::peripheral::POWER; 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)] #[rtic::app(device = dk)]
const APP: () = { const APP: () = {
@ -19,7 +20,7 @@ const APP: () = {
power.intenset.write(|w| w.usbdetected().set_bit()); power.intenset.write(|w| w.usbdetected().set_bit());
log::info!("USBDETECTED interrupt enabled"); defmt::info!("USBDETECTED interrupt enabled");
init::LateResources { init::LateResources {
power, // <- resource initialization power, // <- resource initialization
@ -29,16 +30,16 @@ const APP: () = {
#[idle] #[idle]
fn main(_cx: main::Context) -> ! { fn main(_cx: main::Context) -> ! {
loop { loop {
log::info!("idle: going to sleep"); defmt::info!("idle: going to sleep");
asm::wfi(); asm::wfi();
log::info!("idle: woke up"); defmt::info!("idle: woke up");
} }
} }
#[task(binds = POWER_CLOCK, resources = [power])] #[task(binds = POWER_CLOCK, resources = [power])]
// ^^^^^^^ resource access list // ^^^^^^^ resource access list
fn on_power_event(cx: on_power_event::Context) { fn on_power_event(cx: on_power_event::Context) {
log::info!("POWER event occurred"); defmt::info!("POWER event occurred");
// resources available to this task // resources available to this task
let resources = cx.resources; let resources = cx.resources;

View file

@ -2,7 +2,8 @@
#![no_std] #![no_std]
use cortex_m::asm; 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)] #[rtic::app(device = dk)]
const APP: () = { const APP: () = {
@ -10,12 +11,12 @@ const APP: () = {
fn init(_cx: init::Context) { fn init(_cx: init::Context) {
dk::init().unwrap(); dk::init().unwrap();
log::info!("Hello"); defmt::info!("Hello");
} }
#[idle] #[idle]
fn main(_cx: main::Context) -> ! { fn main(_cx: main::Context) -> ! {
log::info!("world!"); defmt::info!("world!");
loop { loop {
asm::bkpt(); 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();
}
}