Skip to content

Commit

Permalink
update log statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirabellensaft committed Jan 10, 2022
1 parent 618358e commit a89c516
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 37 deletions.
44 changes: 16 additions & 28 deletions advanced/common/usb/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Some USB 2.0 data types
// NOTE this is a partial solution to exercise `usb-2`
#![deny(missing_docs)]
#![deny(warnings)]
Expand Down Expand Up @@ -51,33 +50,22 @@ impl Request {
windex: u16,
wlength: u16,
) -> Result<Self, ()> {
// Request Codes
// see table 9-4 (USB specification)
const SET_ADDRESS: u8 = 5;
const GET_DESCRIPTOR: u8 = 6;


if bmrequesttype == 0b10000000 && brequest == GET_DESCRIPTOR {
// see table 9-5
const DEVICE: u8 = 1;

// 1. get descriptor type and descriptor index from wValue
let desc_ty = (wvalue >> 8) as u8;
let desc_index = wvalue as u8;
let langid = windex;

// 2. confirm that the descriptor
// - is of type DEVICE and
// - has descriptor index 0 (i.e. it is the first implemented descriptor for this type) and
// - has wIndex 0 (i.e. no language ID since it's not a string descriptor)
if desc_ty == DEVICE && desc_index == 0 && langid == 0 {
Ok(Request::GetDescriptor {
descriptor: Descriptor::Device,
length: wlength,
})
} else {
Err(())
}
} else if bmrequesttype == 0b00000000 && brequest == SET_ADDRESS {

// TODO implement another branch handling GET_DESCRIPTOR requests:
//
// 1. get descriptor type and descriptor index from `wValue`
//
// 2. confirm that
// - the descriptor type is DEVICE, i.e. of value 1 and
// - the descriptor index is 0 (i.e. it is the first implemented descriptor for this type) and
// - `wIndex` is 0 (i.e. no language ID since it's not a string descriptor)
//
// For more details, see https://embedded-trainings.ferrous-systems.com/setup-stage.html

if bmrequesttype == 0b00000000 && brequest == SET_ADDRESS {
// Set the device address for all future accesses.
// (Needed to successfully init when conected to Apple devices)
// Section 9.4.6 Set Address of the USB specification explains which values for wvalue,
Expand All @@ -90,6 +78,7 @@ impl Request {
Err(())
}
} else {
defmt::println!("unhandled case in `Request` parser");
Err(())
}
}
Expand Down Expand Up @@ -207,5 +196,4 @@ mod tests {
assert!(Request::parse(0b0000_0000, 0x09, 0x00_01, 0, 1).is_err());
// ^
}
}

}
2 changes: 1 addition & 1 deletion advanced/firmware/src/bin/usb-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod app {
}

fn on_event(_usbd: &USBD, event: Event) {
defmt::println!("USB: {:?} @ {:?}", event, dk::uptime());
defmt::println!("USB: {} @ {}", event, dk::uptime());

match event {
Event::UsbReset => todo!(),
Expand Down
2 changes: 1 addition & 1 deletion advanced/firmware/src/bin/usb-2-solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mod app {
}

fn on_event(usbd: &USBD, event: Event) {
defmt::println!("USB: {:?} @ {:?}", event, dk::uptime());
defmt::println!("USB: {} @ {}", event, dk::uptime());

match event {
Event::UsbReset => {
Expand Down
2 changes: 1 addition & 1 deletion advanced/firmware/src/bin/usb-4-solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod app {
}
}
fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State, event: Event) {
defmt::println!("USB: {:?} @ {:?}", event, dk::uptime());
defmt::println!("USB: {} @ {}", event, dk::uptime());

match event {
Event::UsbReset => {
Expand Down
14 changes: 8 additions & 6 deletions embedded-workshop-book/src/setup-stage.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ modify `usb-2.rs` to read `USBD` registers and parse the SETUP data when an EP0S
When you have successfully received a GET_DESCRIPTOR request for a Device descriptor you are done. You should see an output like this:

``` console
INFO:usb_2 -- USB: UsbReset @ 438.842772ms
INFO:usb_2 -- USB: UsbEp0Setup @ 514.984128ms
...
INFO:usb_2 -- SETUP: bmrequesttype: 128, brequest: 6, wlength: 64, windex: 0, wvalue: 256
INFO:usb_2 -- GET_DESCRIPTOR Device [length=64]
INFO:usb_2 -- Goal reached; move to the next section
USB: UsbReset @ Duration { secs: 0, nanos: 361145018 }
USB: UsbEp0Setup @ Duration { secs: 0, nanos: 402465820 }
SETUP: bmrequesttype: 0, brequest: 5, wlength: 0, windex: 0, wvalue: 10
USB: UsbEp0Setup @ Duration { secs: 0, nanos: 404754637 }
SETUP: bmrequesttype: 128, brequest: 6, wlength: 8, windex: 0, wvalue: 256
GET_DESCRIPTOR Device [length=8]
Goal reached; move to the next section
`dk::exit()` called; exiting ...
```

> Note: `wlength` / `length` can vary depending on the OS, USB port (USB 2.0 vs USB 3.0) or the presence of a USB hub so you may see a different value.
Expand Down

0 comments on commit a89c516

Please sign in to comment.