mirror of
https://github.com/ferrous-systems/embedded-trainings-2020.git
synced 2025-01-10 08:15:36 +00:00
finish advanced defmt port
This commit is contained in:
parent
9c22c61c67
commit
9c1a7e5ee1
11 changed files with 82 additions and 69 deletions
|
@ -3,15 +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!("provoking stack overflow...");
|
||||
spam(0);
|
||||
defmt::info!("fib(100) = {:?}", fib(100));
|
||||
|
||||
loop {
|
||||
asm::bkpt();
|
||||
|
@ -19,16 +19,13 @@ fn main() -> ! {
|
|||
}
|
||||
|
||||
#[inline(never)]
|
||||
fn spam(n: u32) {
|
||||
// allocate and initialize 4 kilobytes of stack memory to provoke stack overflow
|
||||
let use_stack = [n; 1024];
|
||||
fn fib(n: u32) -> u32 {
|
||||
// allocate and initialize one kilobyte of stack memory to provoke stack overflow
|
||||
let _use_stack = [0xAA; 1024];
|
||||
|
||||
log::info!(
|
||||
"address of current `use_stack` at recursion depth {:?}: {:?}",
|
||||
use_stack[1023], // "use" use_stack to prevent it from being optimized out
|
||||
&use_stack as *const u32
|
||||
);
|
||||
|
||||
let next = n + 1;
|
||||
spam(next); // infinite recursion
|
||||
if n < 2 {
|
||||
1
|
||||
} else {
|
||||
fib(n - 1) + fib(n - 2) // recursion
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,8 @@ use dk::{
|
|||
peripheral::USBD,
|
||||
usbd::{self, Event},
|
||||
};
|
||||
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: () = {
|
||||
|
@ -33,18 +34,18 @@ const APP: () = {
|
|||
};
|
||||
|
||||
fn on_event(_usbd: &USBD, event: Event) {
|
||||
log::info!("USB: {:?}", event);
|
||||
defmt::info!("USB: {:?}", event);
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
// going from the Default state to the Default state is a no-operation
|
||||
log::info!("returning to the Default state");
|
||||
defmt::info!("returning to the Default state");
|
||||
}
|
||||
|
||||
Event::UsbEp0DataDone => todo!(),
|
||||
|
||||
Event::UsbEp0Setup => {
|
||||
log::info!("goal reached; move to the next section");
|
||||
defmt::info!("goal reached; move to the next section");
|
||||
dk::exit()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,8 @@ use dk::{
|
|||
peripheral::USBD,
|
||||
usbd::{self, Event},
|
||||
};
|
||||
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: () = {
|
||||
|
@ -21,7 +22,7 @@ const APP: () = {
|
|||
// NOTE this will block if the USB cable is not connected to port J3
|
||||
usbd::init(board.power, &board.usbd);
|
||||
|
||||
log::info!("USBD initialized");
|
||||
defmt::info!("USBD initialized");
|
||||
|
||||
init::LateResources { usbd: board.usbd }
|
||||
}
|
||||
|
@ -37,7 +38,7 @@ const APP: () = {
|
|||
};
|
||||
|
||||
fn on_event(_usbd: &USBD, event: Event) {
|
||||
log::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
defmt::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
|
||||
match event {
|
||||
Event::UsbReset => todo!(),
|
||||
|
@ -45,7 +46,7 @@ fn on_event(_usbd: &USBD, event: Event) {
|
|||
Event::UsbEp0DataDone => todo!(),
|
||||
// leave this at it is for now.
|
||||
Event::UsbEp0Setup => {
|
||||
log::info!("goal reached; move to the next section");
|
||||
defmt::info!("goal reached; move to the next section");
|
||||
dk::exit()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,8 @@ use dk::{
|
|||
peripheral::USBD,
|
||||
usbd::{self, Event},
|
||||
};
|
||||
use panic_log as _; // panic handler
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
use usb::{Descriptor, Request};
|
||||
|
||||
#[rtic::app(device = dk)]
|
||||
|
@ -34,7 +35,7 @@ const APP: () = {
|
|||
};
|
||||
|
||||
fn on_event(usbd: &USBD, event: Event) {
|
||||
log::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
defmt::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
|
@ -69,7 +70,7 @@ fn on_event(usbd: &USBD, event: Event) {
|
|||
// let windex = usbd::windex(usbd);
|
||||
// let wvalue = usbd::wvalue(usbd);
|
||||
|
||||
log::info!(
|
||||
defmt::info!(
|
||||
"SETUP: bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
brequest,
|
||||
|
@ -84,9 +85,9 @@ fn on_event(usbd: &USBD, event: Event) {
|
|||
Request::GetDescriptor { descriptor, length }
|
||||
if descriptor == Descriptor::Device =>
|
||||
{
|
||||
log::info!("GET_DESCRIPTOR Device [length={}]", length);
|
||||
defmt::info!("GET_DESCRIPTOR Device [length={}]", length);
|
||||
|
||||
log::info!("Goal reached; move to the next section");
|
||||
defmt::info!("Goal reached; move to the next section");
|
||||
dk::exit()
|
||||
}
|
||||
Request::SetAddress { .. } => {
|
||||
|
|
|
@ -5,7 +5,9 @@ use dk::{
|
|||
peripheral::USBD,
|
||||
usbd::{self, Event},
|
||||
};
|
||||
use panic_log as _; // panic handler
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
|
||||
use usb::{Descriptor, Request};
|
||||
|
||||
#[rtic::app(device = dk)]
|
||||
|
@ -34,7 +36,7 @@ const APP: () = {
|
|||
};
|
||||
|
||||
fn on_event(_usbd: &USBD, event: Event) {
|
||||
log::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
defmt::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
|
@ -61,7 +63,7 @@ fn on_event(_usbd: &USBD, event: Event) {
|
|||
// composed of a high register (WVALUEH) and a low register (WVALUEL)
|
||||
let wvalue: u16 = 0;
|
||||
|
||||
log::info!(
|
||||
defmt::info!(
|
||||
"SETUP: bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
brequest,
|
||||
|
@ -79,9 +81,9 @@ fn on_event(_usbd: &USBD, event: Event) {
|
|||
// TODO modify `Request::parse()` in `advanced/common/usb/lib.rs`
|
||||
// so that this branch is reached
|
||||
|
||||
log::info!("GET_DESCRIPTOR Device [length={}]", length);
|
||||
defmt::info!("GET_DESCRIPTOR Device [length={}]", length);
|
||||
|
||||
log::info!("Goal reached; move to the next section");
|
||||
defmt::info!("Goal reached; move to the next section");
|
||||
dk::exit()
|
||||
}
|
||||
Request::SetAddress { .. } => {
|
||||
|
|
|
@ -5,7 +5,8 @@ use dk::{
|
|||
peripheral::USBD,
|
||||
usbd::{self, Ep0In, Event},
|
||||
};
|
||||
use panic_log as _; // panic handler
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
use usb::{Descriptor, Request};
|
||||
|
||||
#[rtic::app(device = dk)]
|
||||
|
@ -39,7 +40,7 @@ const APP: () = {
|
|||
};
|
||||
|
||||
fn on_event(usbd: &USBD, ep0in: &mut Ep0In, event: Event) {
|
||||
log::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
defmt::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
|
@ -55,7 +56,7 @@ fn on_event(usbd: &USBD, ep0in: &mut Ep0In, event: Event) {
|
|||
let windex = usbd::windex(usbd);
|
||||
let wvalue = usbd::wvalue(usbd);
|
||||
|
||||
log::info!(
|
||||
defmt::info!(
|
||||
"SETUP: bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
brequest,
|
||||
|
@ -71,7 +72,7 @@ fn on_event(usbd: &USBD, ep0in: &mut Ep0In, event: Event) {
|
|||
Request::GetDescriptor { descriptor, length }
|
||||
if descriptor == Descriptor::Device =>
|
||||
{
|
||||
log::info!("GET_DESCRIPTOR Device [length={}]", length);
|
||||
defmt::info!("GET_DESCRIPTOR Device [length={}]", length);
|
||||
|
||||
let desc = usb2::device::Descriptor {
|
||||
bDeviceClass: 0,
|
||||
|
@ -96,7 +97,7 @@ fn on_event(usbd: &USBD, ep0in: &mut Ep0In, event: Event) {
|
|||
// but for now it's OK to do nothing.
|
||||
}
|
||||
_ => {
|
||||
log::error!(
|
||||
defmt::error!(
|
||||
"unknown request (goal achieved if GET_DESCRIPTOR Device was handled before)"
|
||||
);
|
||||
dk::exit()
|
||||
|
|
|
@ -5,7 +5,8 @@ use dk::{
|
|||
peripheral::USBD,
|
||||
usbd::{self, Ep0In, Event},
|
||||
};
|
||||
use panic_log as _; // panic handler
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
use usb::{Descriptor, Request};
|
||||
|
||||
#[rtic::app(device = dk)]
|
||||
|
@ -39,7 +40,7 @@ const APP: () = {
|
|||
};
|
||||
|
||||
fn on_event(usbd: &USBD, ep0in: &mut Ep0In, event: Event) {
|
||||
log::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
defmt::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
|
@ -55,7 +56,7 @@ fn on_event(usbd: &USBD, ep0in: &mut Ep0In, event: Event) {
|
|||
let windex = usbd::windex(usbd);
|
||||
let wvalue = usbd::wvalue(usbd);
|
||||
|
||||
log::info!(
|
||||
defmt::info!(
|
||||
"SETUP: bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
brequest,
|
||||
|
@ -71,7 +72,7 @@ fn on_event(usbd: &USBD, ep0in: &mut Ep0In, event: Event) {
|
|||
Request::GetDescriptor { descriptor, length }
|
||||
if descriptor == Descriptor::Device =>
|
||||
{
|
||||
log::info!("GET_DESCRIPTOR Device [length={}]", length);
|
||||
defmt::info!("GET_DESCRIPTOR Device [length={}]", length);
|
||||
|
||||
// TODO send back a valid device descriptor, truncated to `length` bytes
|
||||
// let desc = usb2::device::Descriptor { .. };
|
||||
|
@ -87,7 +88,7 @@ fn on_event(usbd: &USBD, ep0in: &mut Ep0In, event: Event) {
|
|||
// but for now it's OK to do nothing.
|
||||
}
|
||||
_ => {
|
||||
log::error!(
|
||||
defmt::error!(
|
||||
"unknown request (goal achieved if GET_DESCRIPTOR Device was handled before)"
|
||||
);
|
||||
dk::exit()
|
||||
|
|
|
@ -7,7 +7,8 @@ use dk::{
|
|||
peripheral::USBD,
|
||||
usbd::{self, Ep0In, Event},
|
||||
};
|
||||
use panic_log as _; // panic handler
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
use usb2::{GetDescriptor as Descriptor, StandardRequest as Request, State};
|
||||
|
||||
#[rtic::app(device = dk)]
|
||||
|
@ -44,22 +45,22 @@ const APP: () = {
|
|||
};
|
||||
|
||||
fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State, event: Event) {
|
||||
log::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
defmt::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
log::info!("USB reset condition detected");
|
||||
defmt::info!("USB reset condition detected");
|
||||
*state = State::Default;
|
||||
}
|
||||
|
||||
Event::UsbEp0DataDone => {
|
||||
log::info!("EP0IN: transfer complete");
|
||||
defmt::info!("EP0IN: transfer complete");
|
||||
ep0in.end(usbd);
|
||||
}
|
||||
|
||||
Event::UsbEp0Setup => {
|
||||
if ep0setup(usbd, ep0in, state).is_err() {
|
||||
log::warn!("EP0IN: unexpected request; stalling the endpoint");
|
||||
defmt::warn!("EP0IN: unexpected request; stalling the endpoint");
|
||||
usbd::ep0stall(usbd);
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +77,7 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
|
|||
let windex = usbd::windex(usbd);
|
||||
let wvalue = usbd::wvalue(usbd);
|
||||
|
||||
log::info!(
|
||||
defmt::info!(
|
||||
"bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
brequest,
|
||||
|
@ -87,7 +88,9 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
|
|||
|
||||
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
|
||||
.expect("Error parsing request");
|
||||
log::info!("EP0: {:?}", request);
|
||||
defmt::info!("EP0: {:?}", defmt::Debug2Format(&request));
|
||||
// ^^^^^^^^^^^^^^^^^^^ this adapter iscurrently needed to log
|
||||
// `StandardRequest` with `defmt`
|
||||
|
||||
match request {
|
||||
// section 9.4.3
|
||||
|
|
|
@ -5,7 +5,8 @@ use dk::{
|
|||
peripheral::USBD,
|
||||
usbd::{self, Ep0In, Event},
|
||||
};
|
||||
use panic_log as _; // panic handler
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
|
||||
use usb2::State;
|
||||
// HEADS UP to use *your* USB packet parser uncomment line 12 and remove line 13
|
||||
|
@ -46,17 +47,17 @@ const APP: () = {
|
|||
};
|
||||
|
||||
fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State, event: Event) {
|
||||
log::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
defmt::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
|
||||
match event {
|
||||
// TODO change `state` as specified in chapter 9.1 USB Device States, of the USB specification
|
||||
Event::UsbReset => {
|
||||
log::info!("USB reset condition detected");
|
||||
defmt::info!("USB reset condition detected");
|
||||
todo!();
|
||||
}
|
||||
|
||||
Event::UsbEp0DataDone => {
|
||||
log::info!("EP0IN: transfer complete");
|
||||
defmt::info!("EP0IN: transfer complete");
|
||||
ep0in.end(usbd);
|
||||
}
|
||||
|
||||
|
@ -64,7 +65,7 @@ fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State, event: Event) {
|
|||
if ep0setup(usbd, ep0in, state).is_err() {
|
||||
// unsupported or invalid request:
|
||||
// TODO: add code to stall the endpoint
|
||||
log::warn!("EP0IN: unexpected request; stalling the endpoint");
|
||||
defmt::warn!("EP0IN: unexpected request; stalling the endpoint");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -77,7 +78,7 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, _state: &mut State) -> Result<(), ()
|
|||
let windex = usbd::windex(usbd);
|
||||
let wvalue = usbd::wvalue(usbd);
|
||||
|
||||
log::info!(
|
||||
defmt::info!(
|
||||
"bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
brequest,
|
||||
|
@ -88,7 +89,9 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, _state: &mut State) -> Result<(), ()
|
|||
|
||||
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
|
||||
.expect("Error parsing request");
|
||||
log::info!("EP0: {:?}", request);
|
||||
defmt::info!("EP0: {:?}", defmt::Debug2Format(&request));
|
||||
// ^^^^^^^^^^^^^^^^^^^ this adapter iscurrently needed to log
|
||||
// `StandardRequest` with `defmt`
|
||||
|
||||
match request {
|
||||
Request::GetDescriptor { descriptor, length } => match descriptor {
|
||||
|
|
|
@ -7,7 +7,8 @@ use dk::{
|
|||
peripheral::USBD,
|
||||
usbd::{self, Ep0In, Event},
|
||||
};
|
||||
use panic_log as _; // panic handler
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
use usb2::{GetDescriptor as Descriptor, StandardRequest as Request, State};
|
||||
|
||||
#[rtic::app(device = dk)]
|
||||
|
@ -44,22 +45,22 @@ const APP: () = {
|
|||
};
|
||||
|
||||
fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State, event: Event) {
|
||||
log::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
defmt::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
log::info!("USB reset condition detected");
|
||||
defmt::info!("USB reset condition detected");
|
||||
*state = State::Default;
|
||||
}
|
||||
|
||||
Event::UsbEp0DataDone => {
|
||||
log::info!("EP0IN: transfer complete");
|
||||
defmt::info!("EP0IN: transfer complete");
|
||||
ep0in.end(usbd);
|
||||
}
|
||||
|
||||
Event::UsbEp0Setup => {
|
||||
if ep0setup(usbd, ep0in, state).is_err() {
|
||||
log::warn!("EP0IN: unexpected request; stalling the endpoint");
|
||||
defmt::warn!("EP0IN: unexpected request; stalling the endpoint");
|
||||
usbd::ep0stall(usbd);
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +77,7 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
|
|||
let windex = usbd::windex(usbd);
|
||||
let wvalue = usbd::wvalue(usbd);
|
||||
|
||||
log::info!(
|
||||
defmt::info!(
|
||||
"bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
brequest,
|
||||
|
@ -87,7 +88,9 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
|
|||
|
||||
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
|
||||
.expect("Error parsing request");
|
||||
log::info!("EP0: {:?}", request);
|
||||
defmt::info!("EP0: {:?}", defmt::Debug2Format(&request));
|
||||
// ^^^^^^^^^^^^^^^^^^^ this adapter iscurrently needed to log
|
||||
// `StandardRequest` with `defmt`
|
||||
|
||||
match request {
|
||||
// section 9.4.3
|
||||
|
@ -185,10 +188,10 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
|
|||
State::Address(address) => {
|
||||
if let Some(value) = value {
|
||||
if value.get() == CONFIG_VAL {
|
||||
log::info!("entering the configured state");
|
||||
defmt::info!("entering the configured state");
|
||||
*state = State::Configured { address, value };
|
||||
} else {
|
||||
log::error!("unsupported configuration value");
|
||||
defmt::error!("unsupported configuration value");
|
||||
return Err(());
|
||||
}
|
||||
} else {
|
||||
|
@ -203,18 +206,18 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
|
|||
if let Some(new_value) = value {
|
||||
if new_value.get() == CONFIG_VAL {
|
||||
if new_value != curr_value {
|
||||
log::info!("changing configuration");
|
||||
defmt::info!("changing configuration");
|
||||
*state = State::Configured {
|
||||
address,
|
||||
value: new_value,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
log::error!("unsupported configuration value");
|
||||
defmt::error!("unsupported configuration value");
|
||||
return Err(());
|
||||
}
|
||||
} else {
|
||||
log::info!("returned to the address state");
|
||||
defmt::info!("returned to the address state");
|
||||
*state = State::Address(address);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -161,7 +161,7 @@ pub fn ep0stall(usbd: &USBD) {
|
|||
}
|
||||
|
||||
/// USBD.EVENTS registers mapped to an enum
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, defmt::Format)]
|
||||
pub enum Event {
|
||||
/// `EVENTS_USBRESET` register was active
|
||||
UsbReset,
|
||||
|
|
Loading…
Reference in a new issue