From 613f46097828509061c8dd9c11a3eb6d538f7dcc Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 16 Jul 2020 12:06:57 +0200 Subject: [PATCH] align usb-2-solution and usb-3 closes #37 --- advanced/firmware/src/bin/usb-2-solution.rs | 7 +++++++ advanced/firmware/src/bin/usb-3.rs | 4 ++-- boards/dk/src/usbd.rs | 12 ++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/advanced/firmware/src/bin/usb-2-solution.rs b/advanced/firmware/src/bin/usb-2-solution.rs index 342472a..fe2ce55 100644 --- a/advanced/firmware/src/bin/usb-2-solution.rs +++ b/advanced/firmware/src/bin/usb-2-solution.rs @@ -62,6 +62,13 @@ fn on_event(usbd: &USBD, event: Event) { let wvalue = (u16::from(usbd.wvalueh.read().wvalueh().bits()) << 8) | u16::from(usbd.wvaluel.read().wvaluel().bits()); + // NOTE the `dk` crate contains helper functions for the above operations + // let bmrequesttype = usbd::bmrequesttype(usbd); + // let brequest = usbd::brequest(usbd); + // let wlength = usbd::wlength(usbd); + // let windex = usbd::windex(usbd); + // let wvalue = usbd::wvalue(usbd); + log::info!( "SETUP: bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}", bmrequesttype, diff --git a/advanced/firmware/src/bin/usb-3.rs b/advanced/firmware/src/bin/usb-3.rs index 8c89258..bd3799b 100644 --- a/advanced/firmware/src/bin/usb-3.rs +++ b/advanced/firmware/src/bin/usb-3.rs @@ -49,8 +49,8 @@ fn on_event(usbd: &USBD, ep0in: &mut Ep0In, event: Event) { Event::UsbEp0DataDone => todo!(), // <- TODO Event::UsbEp0Setup => { - let bmrequesttype = usbd.bmrequesttype.read().bits() as u8; - let brequest = usbd.brequest.read().brequest().bits(); + let bmrequesttype = usbd::bmrequesttype(usbd); + let brequest = usbd::brequest(usbd); let wlength = usbd::wlength(usbd); let windex = usbd::windex(usbd); let wvalue = usbd::wvalue(usbd); diff --git a/boards/dk/src/usbd.rs b/boards/dk/src/usbd.rs index d57766e..4973413 100644 --- a/boards/dk/src/usbd.rs +++ b/boards/dk/src/usbd.rs @@ -200,6 +200,18 @@ pub fn next_event(usbd: &USBD) -> Option { None } +/// Reads the BMREQUESTTYPE register and returns the 8-bit BMREQUESTTYPE component of a setup packet +pub fn bmrequesttype(usbd: &USBD) -> u8 { + // read the 32-bit register and extract the least significant byte + // (the alternative is to read the 3 bitfields of the register and merge them into one byte) + usbd.bmrequesttype.read().bits() as u8 +} + +/// Reads the BREQUEST register and returns the 8-bit BREQUEST component of a setup packet +pub fn brequest(usbd: &USBD) -> u8 { + usbd.brequest.read().brequest().bits() +} + /// Reads the WLENGTHL and WLENGTHH registers and returns the 16-bit WLENGTH component of a setup packet pub fn wlength(usbd: &USBD) -> u16 { u16::from(usbd.wlengthl.read().wlengthl().bits())