From 3582b93abd05b9fc3514fa75ee8e99ba8b274ec4 Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Sun, 18 Oct 2020 18:40:53 +0200 Subject: [PATCH 1/6] Reduce diff of usb-4-solution.rs to usb-4.rs While comparing my solution to the given solution, I found the diff between the two files is larger than it should be. I believe the differences are completely unrelated to the usb-4 topics. --- advanced/firmware/src/bin/usb-4-solution.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/advanced/firmware/src/bin/usb-4-solution.rs b/advanced/firmware/src/bin/usb-4-solution.rs index 7346880..3ac4c71 100644 --- a/advanced/firmware/src/bin/usb-4-solution.rs +++ b/advanced/firmware/src/bin/usb-4-solution.rs @@ -13,9 +13,9 @@ use usb2::{GetDescriptor as Descriptor, StandardRequest as Request, State}; #[rtic::app(device = dk)] const APP: () = { struct Resources { + usbd: USBD, ep0in: Ep0In, state: State, - usbd: USBD, } #[init] @@ -31,19 +31,19 @@ const APP: () = { } } - #[task(binds = USBD, resources = [state, usbd, ep0in])] + #[task(binds = USBD, resources = [usbd, ep0in, state])] fn main(cx: main::Context) { let usbd = cx.resources.usbd; - let state = cx.resources.state; let ep0in = cx.resources.ep0in; + let state = cx.resources.state; while let Some(event) = usbd::next_event(usbd) { - on_event(usbd, state, ep0in, event) + on_event(usbd, ep0in, state, event) } } }; -fn on_event(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In, event: Event) { +fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State, event: Event) { log::info!("USB: {:?} @ {:?}", event, dk::uptime()); match event { @@ -58,7 +58,7 @@ fn on_event(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In, event: Event) { } Event::UsbEp0Setup => { - if ep0setup(usbd, state, ep0in).is_err() { + if ep0setup(usbd, ep0in, state).is_err() { log::warn!("EP0IN: unexpected request; stalling the endpoint"); usbd::ep0stall(usbd); } @@ -69,7 +69,7 @@ fn on_event(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In, event: Event) { /// The `bConfigurationValue` of the only supported configuration const CONFIG_VAL: u8 = 42; -fn ep0setup(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In) -> Result<(), ()> { +fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> { let bmrequesttype = usbd.bmrequesttype.read().bits() as u8; let brequest = usbd.brequest.read().brequest().bits(); let wlength = usbd::wlength(usbd); @@ -91,7 +91,7 @@ fn ep0setup(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In) -> Result<(), ()> bDeviceSubClass: 0, bMaxPacketSize0: usb2::device::bMaxPacketSize0::B64, bNumConfigurations: core::num::NonZeroU8::new(1).unwrap(), - bcdDevice: 0x0100, // 1.00 + bcdDevice: 0x01_00, // 1.00 iManufacturer: None, iProduct: None, iSerialNumber: None, @@ -99,7 +99,7 @@ fn ep0setup(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In) -> Result<(), ()> idVendor: consts::VID, }; let bytes = desc.bytes(); - ep0in.start(&bytes[..core::cmp::min(bytes.len(), length.into())], usbd); + let _ = ep0in.start(&bytes[..core::cmp::min(bytes.len(), length.into())], usbd); } Descriptor::Configuration { index } => { From dfb8685d7311bcb4f18f6a2036d9231fec233229 Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Sun, 18 Oct 2020 18:45:49 +0200 Subject: [PATCH 2/6] Reduce diff of usb-4.rs to usb-4-solution.rs While comparing my solution to the given solution, I found the diff between the two files is larger than it should be. I believe the differences are completely unrelated to the usb-4 topics. --- advanced/firmware/src/bin/usb-4.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/advanced/firmware/src/bin/usb-4.rs b/advanced/firmware/src/bin/usb-4.rs index f79f668..79cc1e1 100644 --- a/advanced/firmware/src/bin/usb-4.rs +++ b/advanced/firmware/src/bin/usb-4.rs @@ -8,14 +8,14 @@ use dk::{ use panic_log as _; // panic handler // use one of these -use usb2::{GetDescriptor as Descriptor, StandardRequest as Request}; +use usb2::{GetDescriptor as Descriptor, StandardRequest as Request, State}; #[rtic::app(device = dk)] const APP: () = { struct Resources { usbd: USBD, ep0in: Ep0In, - state: usb2::State, + state: State, } #[init] @@ -26,7 +26,7 @@ const APP: () = { init::LateResources { usbd: board.usbd, - state: usb2::State::Default, + state: State::Default, ep0in: board.ep0in, } } @@ -43,7 +43,7 @@ const APP: () = { } }; -fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut usb2::State, event: Event) { +fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State, event: Event) { log::info!("USB: {:?} @ {:?}", event, dk::uptime()); match event { @@ -56,13 +56,13 @@ fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut usb2::State, event: Even if ep0setup(usbd, ep0in, state).is_err() { // unsupported or invalid request: // TODO: add code to stall the endpoint - log::warn!("EP0: unexpected request; stalling the endpoint"); + log::warn!("EP0IN: unexpected request; stalling the endpoint"); } } } } -fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, _state: &mut usb2::State) -> Result<(), ()> { +fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, _state: &mut State) -> Result<(), ()> { let bmrequesttype = usbd.bmrequesttype.read().bits() as u8; let brequest = usbd.brequest.read().brequest().bits(); let wlength = usbd::wlength(usbd); From b67660a753db8dc16b0b42a04ff93bbe6c664e91 Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Sun, 18 Oct 2020 19:06:13 +0200 Subject: [PATCH 3/6] Add logging from usb-4-solution.rs --- advanced/firmware/src/bin/usb-4.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/advanced/firmware/src/bin/usb-4.rs b/advanced/firmware/src/bin/usb-4.rs index 79cc1e1..3b91a5d 100644 --- a/advanced/firmware/src/bin/usb-4.rs +++ b/advanced/firmware/src/bin/usb-4.rs @@ -48,9 +48,15 @@ fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State, event: Event) { match event { // TODO change `state` as specified in chapter 9.1 USB Device States, of the USB specification - Event::UsbReset => todo!(), + Event::UsbReset => { + log::info!("USB reset condition detected"); + todo!(); + } - Event::UsbEp0DataDone => ep0in.end(usbd), + Event::UsbEp0DataDone => { + log::info!("EP0IN: transfer complete"); + ep0in.end(usbd); + } Event::UsbEp0Setup => { if ep0setup(usbd, ep0in, state).is_err() { From b48824231fc28d52ff3f64943b613c03fa1181bc Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Sun, 18 Oct 2020 19:07:19 +0200 Subject: [PATCH 4/6] Add logging from usb-4.rs --- advanced/firmware/src/bin/usb-4-solution.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/advanced/firmware/src/bin/usb-4-solution.rs b/advanced/firmware/src/bin/usb-4-solution.rs index 3ac4c71..0b8bd5d 100644 --- a/advanced/firmware/src/bin/usb-4-solution.rs +++ b/advanced/firmware/src/bin/usb-4-solution.rs @@ -76,6 +76,15 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> let windex = usbd::windex(usbd); let wvalue = usbd::wvalue(usbd); + log::info!( + "bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}", + bmrequesttype, + brequest, + wlength, + windex, + wvalue + ); + let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength) .expect("Error parsing request"); log::info!("EP0: {:?}", request); From ab7cfb8a48163e27d85582da4ebf984b8a26a0b2 Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Sun, 18 Oct 2020 19:50:36 +0200 Subject: [PATCH 5/6] Reduce diff of usb-5-solution.rs to usb-4-solution.rs --- advanced/firmware/src/bin/usb-5-solution.rs | 34 +++++++++++++-------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/advanced/firmware/src/bin/usb-5-solution.rs b/advanced/firmware/src/bin/usb-5-solution.rs index 986d00a..34f0228 100644 --- a/advanced/firmware/src/bin/usb-5-solution.rs +++ b/advanced/firmware/src/bin/usb-5-solution.rs @@ -13,9 +13,9 @@ use usb2::{GetDescriptor as Descriptor, StandardRequest as Request, State}; #[rtic::app(device = dk)] const APP: () = { struct Resources { + usbd: USBD, ep0in: Ep0In, state: State, - usbd: USBD, } #[init] @@ -31,19 +31,19 @@ const APP: () = { } } - #[task(binds = USBD, resources = [state, usbd, ep0in])] + #[task(binds = USBD, resources = [usbd, ep0in, state])] fn main(cx: main::Context) { let usbd = cx.resources.usbd; - let state = cx.resources.state; let ep0in = cx.resources.ep0in; + let state = cx.resources.state; while let Some(event) = usbd::next_event(usbd) { - on_event(usbd, state, ep0in, event) + on_event(usbd, ep0in, state, event) } } }; -fn on_event(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In, event: Event) { +fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State, event: Event) { log::info!("USB: {:?} @ {:?}", event, dk::uptime()); match event { @@ -54,12 +54,12 @@ fn on_event(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In, event: Event) { Event::UsbEp0DataDone => { log::info!("EP0IN: transfer complete"); - ep0in.end(usbd) + ep0in.end(usbd); } Event::UsbEp0Setup => { - if ep0setup(usbd, state, ep0in).is_err() { - log::warn!("EP0IN: stalled"); + if ep0setup(usbd, ep0in, state).is_err() { + log::warn!("EP0IN: unexpected request; stalling the endpoint"); usbd::ep0stall(usbd); } } @@ -69,14 +69,24 @@ fn on_event(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In, event: Event) { /// The `bConfigurationValue` of the only supported configuration const CONFIG_VAL: u8 = 42; -fn ep0setup(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In) -> Result<(), ()> { +fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> { let bmrequesttype = usbd.bmrequesttype.read().bits() as u8; let brequest = usbd.brequest.read().brequest().bits(); let wlength = usbd::wlength(usbd); let windex = usbd::windex(usbd); let wvalue = usbd::wvalue(usbd); - let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)?; + log::info!( + "bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}", + bmrequesttype, + brequest, + wlength, + windex, + wvalue + ); + + let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength) + .expect("Error parsing request"); log::info!("EP0: {:?}", request); match request { @@ -90,7 +100,7 @@ fn ep0setup(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In) -> Result<(), ()> bDeviceSubClass: 0, bMaxPacketSize0: usb2::device::bMaxPacketSize0::B64, bNumConfigurations: core::num::NonZeroU8::new(1).unwrap(), - bcdDevice: 0x0100, // 1.00 + bcdDevice: 0x01_00, // 1.00 iManufacturer: None, iProduct: None, iSerialNumber: None, @@ -98,7 +108,7 @@ fn ep0setup(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In) -> Result<(), ()> idVendor: consts::VID, }; let bytes = desc.bytes(); - ep0in.start(&bytes[..core::cmp::min(bytes.len(), length.into())], usbd); + let _ = ep0in.start(&bytes[..core::cmp::min(bytes.len(), length.into())], usbd); } Descriptor::Configuration { index } => { From 3a184749ee0d7a528f3dd784b9698128dfcb94c6 Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Sun, 18 Oct 2020 19:51:12 +0200 Subject: [PATCH 6/6] Add semicolon for consistency --- advanced/firmware/src/bin/usb-4-solution.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced/firmware/src/bin/usb-4-solution.rs b/advanced/firmware/src/bin/usb-4-solution.rs index 0b8bd5d..2301173 100644 --- a/advanced/firmware/src/bin/usb-4-solution.rs +++ b/advanced/firmware/src/bin/usb-4-solution.rs @@ -54,7 +54,7 @@ fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State, event: Event) { Event::UsbEp0DataDone => { log::info!("EP0IN: transfer complete"); - ep0in.end(usbd) + ep0in.end(usbd); } Event::UsbEp0Setup => {