Merge pull request #107 from barafael/patch-2

Reduce diff of usb-4-solution.rs to usb-4.rs
This commit is contained in:
Lotte Steenbrink 2020-12-21 11:33:50 +01:00 committed by GitHub
commit 43c0c8a4e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 30 deletions

View file

@ -13,9 +13,9 @@ use usb2::{GetDescriptor as Descriptor, StandardRequest as Request, State};
#[rtic::app(device = dk)] #[rtic::app(device = dk)]
const APP: () = { const APP: () = {
struct Resources { struct Resources {
usbd: USBD,
ep0in: Ep0In, ep0in: Ep0In,
state: State, state: State,
usbd: USBD,
} }
#[init] #[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) { fn main(cx: main::Context) {
let usbd = cx.resources.usbd; let usbd = cx.resources.usbd;
let state = cx.resources.state;
let ep0in = cx.resources.ep0in; let ep0in = cx.resources.ep0in;
let state = cx.resources.state;
while let Some(event) = usbd::next_event(usbd) { 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()); log::info!("USB: {:?} @ {:?}", event, dk::uptime());
match event { match event {
@ -54,11 +54,11 @@ fn on_event(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In, event: Event) {
Event::UsbEp0DataDone => { Event::UsbEp0DataDone => {
log::info!("EP0IN: transfer complete"); log::info!("EP0IN: transfer complete");
ep0in.end(usbd) ep0in.end(usbd);
} }
Event::UsbEp0Setup => { Event::UsbEp0Setup => {
if ep0setup(usbd, state, ep0in).is_err() { if ep0setup(usbd, ep0in, state).is_err() {
log::warn!("EP0IN: unexpected request; stalling the endpoint"); log::warn!("EP0IN: unexpected request; stalling the endpoint");
usbd::ep0stall(usbd); usbd::ep0stall(usbd);
} }
@ -69,13 +69,22 @@ fn on_event(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In, event: Event) {
/// The `bConfigurationValue` of the only supported configuration /// The `bConfigurationValue` of the only supported configuration
const CONFIG_VAL: u8 = 42; 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 bmrequesttype = usbd.bmrequesttype.read().bits() as u8;
let brequest = usbd.brequest.read().brequest().bits(); let brequest = usbd.brequest.read().brequest().bits();
let wlength = usbd::wlength(usbd); let wlength = usbd::wlength(usbd);
let windex = usbd::windex(usbd); let windex = usbd::windex(usbd);
let wvalue = usbd::wvalue(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) let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
.expect("Error parsing request"); .expect("Error parsing request");
log::info!("EP0: {:?}", request); log::info!("EP0: {:?}", request);
@ -91,7 +100,7 @@ fn ep0setup(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In) -> Result<(), ()>
bDeviceSubClass: 0, bDeviceSubClass: 0,
bMaxPacketSize0: usb2::device::bMaxPacketSize0::B64, bMaxPacketSize0: usb2::device::bMaxPacketSize0::B64,
bNumConfigurations: core::num::NonZeroU8::new(1).unwrap(), bNumConfigurations: core::num::NonZeroU8::new(1).unwrap(),
bcdDevice: 0x0100, // 1.00 bcdDevice: 0x01_00, // 1.00
iManufacturer: None, iManufacturer: None,
iProduct: None, iProduct: None,
iSerialNumber: None, iSerialNumber: None,
@ -99,7 +108,7 @@ fn ep0setup(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In) -> Result<(), ()>
idVendor: consts::VID, idVendor: consts::VID,
}; };
let bytes = desc.bytes(); 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 } => { Descriptor::Configuration { index } => {

View file

@ -8,14 +8,14 @@ use dk::{
use panic_log as _; // panic handler use panic_log as _; // panic handler
// use one of these // 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)] #[rtic::app(device = dk)]
const APP: () = { const APP: () = {
struct Resources { struct Resources {
usbd: USBD, usbd: USBD,
ep0in: Ep0In, ep0in: Ep0In,
state: usb2::State, state: State,
} }
#[init] #[init]
@ -26,7 +26,7 @@ const APP: () = {
init::LateResources { init::LateResources {
usbd: board.usbd, usbd: board.usbd,
state: usb2::State::Default, state: State::Default,
ep0in: board.ep0in, ep0in: board.ep0in,
} }
} }
@ -43,26 +43,32 @@ 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()); log::info!("USB: {:?} @ {:?}", event, dk::uptime());
match event { match event {
// TODO change `state` as specified in chapter 9.1 USB Device States, of the USB specification // 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 => { Event::UsbEp0Setup => {
if ep0setup(usbd, ep0in, state).is_err() { if ep0setup(usbd, ep0in, state).is_err() {
// unsupported or invalid request: // unsupported or invalid request:
// TODO: add code to stall the endpoint // 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 bmrequesttype = usbd.bmrequesttype.read().bits() as u8;
let brequest = usbd.brequest.read().brequest().bits(); let brequest = usbd.brequest.read().brequest().bits();
let wlength = usbd::wlength(usbd); let wlength = usbd::wlength(usbd);

View file

@ -13,9 +13,9 @@ use usb2::{GetDescriptor as Descriptor, StandardRequest as Request, State};
#[rtic::app(device = dk)] #[rtic::app(device = dk)]
const APP: () = { const APP: () = {
struct Resources { struct Resources {
usbd: USBD,
ep0in: Ep0In, ep0in: Ep0In,
state: State, state: State,
usbd: USBD,
} }
#[init] #[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) { fn main(cx: main::Context) {
let usbd = cx.resources.usbd; let usbd = cx.resources.usbd;
let state = cx.resources.state;
let ep0in = cx.resources.ep0in; let ep0in = cx.resources.ep0in;
let state = cx.resources.state;
while let Some(event) = usbd::next_event(usbd) { 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()); log::info!("USB: {:?} @ {:?}", event, dk::uptime());
match event { match event {
@ -54,12 +54,12 @@ fn on_event(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In, event: Event) {
Event::UsbEp0DataDone => { Event::UsbEp0DataDone => {
log::info!("EP0IN: transfer complete"); log::info!("EP0IN: transfer complete");
ep0in.end(usbd) ep0in.end(usbd);
} }
Event::UsbEp0Setup => { Event::UsbEp0Setup => {
if ep0setup(usbd, state, ep0in).is_err() { if ep0setup(usbd, ep0in, state).is_err() {
log::warn!("EP0IN: stalled"); log::warn!("EP0IN: unexpected request; stalling the endpoint");
usbd::ep0stall(usbd); 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 /// The `bConfigurationValue` of the only supported configuration
const CONFIG_VAL: u8 = 42; 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 bmrequesttype = usbd.bmrequesttype.read().bits() as u8;
let brequest = usbd.brequest.read().brequest().bits(); let brequest = usbd.brequest.read().brequest().bits();
let wlength = usbd::wlength(usbd); let wlength = usbd::wlength(usbd);
let windex = usbd::windex(usbd); let windex = usbd::windex(usbd);
let wvalue = usbd::wvalue(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); log::info!("EP0: {:?}", request);
match request { match request {
@ -90,7 +100,7 @@ fn ep0setup(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In) -> Result<(), ()>
bDeviceSubClass: 0, bDeviceSubClass: 0,
bMaxPacketSize0: usb2::device::bMaxPacketSize0::B64, bMaxPacketSize0: usb2::device::bMaxPacketSize0::B64,
bNumConfigurations: core::num::NonZeroU8::new(1).unwrap(), bNumConfigurations: core::num::NonZeroU8::new(1).unwrap(),
bcdDevice: 0x0100, // 1.00 bcdDevice: 0x01_00, // 1.00
iManufacturer: None, iManufacturer: None,
iProduct: None, iProduct: None,
iSerialNumber: None, iSerialNumber: None,
@ -98,7 +108,7 @@ fn ep0setup(usbd: &USBD, state: &mut State, ep0in: &mut Ep0In) -> Result<(), ()>
idVendor: consts::VID, idVendor: consts::VID,
}; };
let bytes = desc.bytes(); 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 } => { Descriptor::Configuration { index } => {