Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions nrf52-code/usb-app-solutions/src/bin/usb-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,18 @@ fn on_event(usbd: &Usbd, event: Event) {
wvalue
);

let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
.expect("Error parsing request");
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength);
match request {
Request::GetDescriptor {
Ok(Request::GetDescriptor {
descriptor: Descriptor::Device,
length,
} => {
}) => {
defmt::info!("GET_DESCRIPTOR Device [length={}]", length);

defmt::println!("Goal reached; move to the next section");
dk::exit()
}
Request::SetAddress { .. } => {
Ok(Request::SetAddress { .. }) => {
// On macOS you'll get this request before the GET_DESCRIPTOR request so we
// need to catch it here. We'll properly handle this request later
// but for now it's OK to do nothing.
Expand Down
18 changes: 8 additions & 10 deletions nrf52-code/usb-app-solutions/src/bin/usb-3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, event: Event) {
Event::UsbEp0DataDone => ep0in.end(usbd),

Event::UsbEp0Setup => {
let bmrequesttype = usbd.bmrequesttype().read().0 as u8;
let brequest = usbd.brequest().read().brequest().to_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);
Expand All @@ -73,14 +73,12 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, event: Event) {
wvalue
);

let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength).expect(
"Error parsing request (goal achieved if GET_DESCRIPTOR Device was handled before)",
);
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength);
match request {
Request::GetDescriptor {
Ok(Request::GetDescriptor {
descriptor: Descriptor::Device,
length,
} => {
}) => {
defmt::info!("GET_DESCRIPTOR Device [length={}]", length);

let desc = usb2::device::Descriptor {
Expand All @@ -107,15 +105,15 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, event: Event) {
};
ep0in.start(subslice, usbd);
}
Request::SetAddress { .. } => {
Ok(Request::SetAddress { .. }) => {
// On macOS you'll get this request before the GET_DESCRIPTOR request so we
// need to catch it here. We'll properly handle this request later
// but for now it's OK to do nothing.
}
_ => {
defmt::error!(
"unknown request (goal achieved if GET_DESCRIPTOR Device was handled before)"
);
"unknown request (goal achieved if GET_DESCRIPTOR Device was handled before)"
);
dk::exit()
}
}
Expand Down
41 changes: 27 additions & 14 deletions nrf52-code/usb-app-solutions/src/bin/usb-4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State, event: Event) {

/// Handle a SETUP request on EP0
fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> {
let bmrequesttype = usbd.bmrequesttype().read().0 as u8;
let brequest = usbd.brequest().read().brequest().to_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);
Expand All @@ -94,18 +94,17 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
wvalue
);

let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
.expect("Error parsing request");
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength);
defmt::info!("EP0: {}", defmt::Debug2Format(&request));
// ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log
// `Request` with `defmt`
// ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log
// `Request` with `defmt`
match request {
// section 9.4.3
// this request is valid in any state
Request::GetDescriptor {
Ok(Request::GetDescriptor {
descriptor: Descriptor::Device,
length,
} => {
}) => {
let desc = usb2::device::Descriptor {
bDeviceClass: 0,
bDeviceProtocol: 0,
Expand All @@ -130,10 +129,10 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
};
ep0in.start(subslice, usbd);
}
Request::GetDescriptor {
Ok(Request::GetDescriptor {
descriptor: Descriptor::Configuration { index },
length,
} => {
}) => {
if index == 0 {
let mut resp = heapless::Vec::<u8, 64>::new();

Expand Down Expand Up @@ -169,7 +168,9 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
return Err(());
}
}
Request::SetAddress { address } => {
Ok(Request::SetAddress { address }) => {
// On macOS you'll get this request before the GET_DESCRIPTOR request so we
// need to catch it here.
match state {
State::Default => {
if let Some(address) = address {
Expand All @@ -194,9 +195,21 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>

// the response to this request is handled in hardware
}

// stall any other request
_ => return Err(()),
Ok(_) => {
// stall anything else
return Err(());
}
Err(_) => {
defmt::error!(
"Failed to parse: bmrequesttype: 0b{=u8:08b}, brequest: {=u8}, wlength: {=u16}, windex: 0x{=u16:04x}, wvalue: 0x{=u16:04x}",
bmrequesttype,
brequest,
wlength,
windex,
wvalue
);
return Err(());
}
}

Ok(())
Expand Down
37 changes: 24 additions & 13 deletions nrf52-code/usb-app-solutions/src/bin/usb-5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State, event: Event) {

/// Handle a SETUP request on EP0
fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> {
let bmrequesttype = usbd.bmrequesttype().read().0 as u8;
let brequest = usbd.brequest().read().brequest().to_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);
Expand All @@ -94,19 +94,18 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
wvalue
);

let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
.expect("Error parsing request");
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength);
defmt::info!("EP0: {}", defmt::Debug2Format(&request));
// ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log
// `Request` with `defmt`

match request {
// section 9.4.3
// this request is valid in any state
Request::GetDescriptor {
Ok(Request::GetDescriptor {
descriptor: Descriptor::Device,
length,
} => {
}) => {
let desc = usb2::device::Descriptor {
bDeviceClass: 0,
bDeviceProtocol: 0,
Expand All @@ -131,10 +130,10 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
};
ep0in.start(subslice, usbd);
}
Request::GetDescriptor {
Ok(Request::GetDescriptor {
descriptor: Descriptor::Configuration { index },
length,
} => {
}) => {
if index == 0 {
let mut resp = heapless::Vec::<u8, 64>::new();

Expand Down Expand Up @@ -170,7 +169,7 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
return Err(());
}
}
Request::SetAddress { address } => {
Ok(Request::SetAddress { address }) => {
match state {
State::Default => {
if let Some(address) = address {
Expand All @@ -195,7 +194,7 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>

// the response to this request is handled in hardware
}
Request::SetConfiguration { value } => {
Ok(Request::SetConfiguration { value }) => {
match *state {
// unspecified behavior
State::Default => return Err(()),
Expand Down Expand Up @@ -240,9 +239,21 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>

usbd.tasks_ep0status().write_value(1);
}

// stall any other request
_ => return Err(()),
Ok(_) => {
// stall anything else
return Err(());
}
Err(_) => {
defmt::error!(
"Failed to parse: bmrequesttype: 0b{=u8:08b}, brequest: {=u8}, wlength: {=u16}, windex: 0x{=u16:04x}, wvalue: 0x{=u16:04x}",
bmrequesttype,
brequest,
wlength,
windex,
wvalue
);
return Err(());
}
}

Ok(())
Expand Down
10 changes: 4 additions & 6 deletions nrf52-code/usb-app/src/bin/usb-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@ fn on_event(_usbd: &Usbd, event: Event) {
wvalue
);

let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
.expect("Error parsing request");
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength);
match request {
Request::GetDescriptor {
Ok(Request::GetDescriptor {
descriptor: Descriptor::Device,
length,
} => {
}) => {
// TODO modify `Request::parse()` in `nrf52-code/usb-lib/src/lib.rs`
// so that this branch is reached

Expand All @@ -92,12 +91,11 @@ fn on_event(_usbd: &Usbd, event: Event) {
defmt::println!("Goal reached; move to the next section");
dk::exit()
}
Request::SetAddress { .. } => {
Ok(Request::SetAddress { .. }) => {
// On macOS you'll get this request before the GET_DESCRIPTOR request so we
// need to catch it here. We'll properly handle this request later
// but for now it's OK to do nothing.
}
#[allow(unreachable_patterns)]
_ => unreachable!(), // we don't handle any other Requests
}
}
Expand Down
15 changes: 6 additions & 9 deletions nrf52-code/usb-app/src/bin/usb-3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,28 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, event: Event) {
wvalue
);

let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength).expect(
"Error parsing request (goal achieved if GET_DESCRIPTOR Device was handled before)",
);
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength);
match request {
Request::GetDescriptor {
Ok(Request::GetDescriptor {
descriptor: Descriptor::Device,
length,
} => {
}) => {
defmt::info!("GET_DESCRIPTOR Device [length={}]", length);

// TODO send back a valid device descriptor, truncated to `length` bytes
// let desc = usb2::device::Descriptor { .. };
let resp = [];
ep0in.start(&resp, usbd);
}
Request::SetAddress { .. } => {
Ok(Request::SetAddress { .. }) => {
// On macOS you'll get this request before the GET_DESCRIPTOR request so we
// need to catch it here. We'll properly handle this request later
// but for now it's OK to do nothing.
}
#[allow(unreachable_patterns)]
_ => {
defmt::error!(
"unknown request (goal achieved if GET_DESCRIPTOR Device was handled before)"
);
"unknown request (goal achieved if GET_DESCRIPTOR Device was handled before)"
);
dk::exit()
}
}
Expand Down
Loading