mirror of
https://github.com/ferrous-systems/embedded-trainings-2020.git
synced 2025-01-09 07:45:34 +00:00
Merge pull request #107 from barafael/patch-2
Reduce diff of usb-4-solution.rs to usb-4.rs
This commit is contained in:
commit
43c0c8a4e6
3 changed files with 55 additions and 30 deletions
|
@ -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,11 +54,11 @@ 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() {
|
||||
if ep0setup(usbd, ep0in, state).is_err() {
|
||||
log::warn!("EP0IN: unexpected request; stalling the endpoint");
|
||||
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
|
||||
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);
|
||||
|
||||
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);
|
||||
|
@ -91,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,
|
||||
|
@ -99,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 } => {
|
||||
|
|
|
@ -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,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());
|
||||
|
||||
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() {
|
||||
// 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);
|
||||
|
|
|
@ -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 } => {
|
||||
|
|
Loading…
Reference in a new issue