mirror of
https://github.com/ferrous-systems/embedded-trainings-2020.git
synced 2025-01-21 13:28:09 +00:00
rustfmt
This commit is contained in:
parent
f8b38e1d88
commit
02e9012e8e
13 changed files with 188 additions and 166 deletions
|
@ -1,7 +1,6 @@
|
|||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
|
||||
|
@ -10,12 +9,10 @@ mod app {
|
|||
use cortex_m::asm;
|
||||
|
||||
#[local]
|
||||
struct MyLocalResources {
|
||||
}
|
||||
struct MyLocalResources {}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
}
|
||||
struct MySharedResources {}
|
||||
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
|
@ -50,7 +47,11 @@ mod app {
|
|||
// ^^^^^^^^^^ bitfield name
|
||||
defmt::println!("USBREGSTATUS.VBUSDETECT: {}", vbusdetect);
|
||||
|
||||
(MySharedResources {}, MyLocalResources {}, init::Monotonics())
|
||||
(
|
||||
MySharedResources {},
|
||||
MyLocalResources {},
|
||||
init::Monotonics(),
|
||||
)
|
||||
}
|
||||
|
||||
#[idle]
|
||||
|
|
|
@ -16,8 +16,7 @@ mod app {
|
|||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
}
|
||||
struct MySharedResources {}
|
||||
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
|
@ -30,15 +29,14 @@ mod app {
|
|||
defmt::println!("USBDETECTED interrupt enabled");
|
||||
|
||||
(
|
||||
MySharedResources {},
|
||||
MySharedResources {},
|
||||
MyLocalResources {
|
||||
power,
|
||||
counter: 0, // <- initialize the new resource
|
||||
},
|
||||
init::Monotonics()
|
||||
},
|
||||
init::Monotonics(),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
#[idle]
|
||||
fn idle(_cx: idle::Context) -> ! {
|
||||
|
|
|
@ -15,9 +15,7 @@ mod app {
|
|||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
|
||||
}
|
||||
struct MySharedResources {}
|
||||
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
|
@ -29,7 +27,11 @@ mod app {
|
|||
|
||||
defmt::println!("USBDETECTED interrupt enabled");
|
||||
|
||||
(MySharedResources {}, MyLocalResources {power}, init::Monotonics())
|
||||
(
|
||||
MySharedResources {},
|
||||
MyLocalResources { power },
|
||||
init::Monotonics(),
|
||||
)
|
||||
}
|
||||
|
||||
#[idle]
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
|
||||
|
@ -10,20 +9,21 @@ mod app {
|
|||
use cortex_m::asm;
|
||||
|
||||
#[local]
|
||||
struct MyLocalResources {
|
||||
}
|
||||
struct MyLocalResources {}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
|
||||
}
|
||||
struct MySharedResources {}
|
||||
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
dk::init().unwrap();
|
||||
|
||||
defmt::println!("Hello");
|
||||
(MySharedResources {}, MyLocalResources {}, init::Monotonics())
|
||||
(
|
||||
MySharedResources {},
|
||||
MyLocalResources {},
|
||||
init::Monotonics(),
|
||||
)
|
||||
}
|
||||
|
||||
#[idle]
|
||||
|
@ -34,4 +34,4 @@ mod app {
|
|||
asm::bkpt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
|
||||
|
@ -19,9 +18,7 @@ mod app {
|
|||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
|
||||
}
|
||||
struct MySharedResources {}
|
||||
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
|
@ -33,7 +30,11 @@ mod app {
|
|||
|
||||
defmt::println!("USBD initialized");
|
||||
|
||||
(MySharedResources {}, MyLocalResources {usbd: board.usbd }, init::Monotonics())
|
||||
(
|
||||
MySharedResources {},
|
||||
MyLocalResources { usbd: board.usbd },
|
||||
init::Monotonics(),
|
||||
)
|
||||
}
|
||||
|
||||
#[task(binds = USBD, local = [usbd])]
|
||||
|
@ -47,15 +48,15 @@ mod app {
|
|||
|
||||
fn on_event(_usbd: &USBD, event: Event) {
|
||||
defmt::println!("USB: {}", event);
|
||||
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
// going from the Default state to the Default state is a no-operation
|
||||
defmt::println!("returning to the Default state");
|
||||
}
|
||||
|
||||
|
||||
Event::UsbEp0DataDone => todo!(),
|
||||
|
||||
|
||||
Event::UsbEp0Setup => {
|
||||
defmt::println!("goal reached; move to the next section");
|
||||
dk::exit();
|
||||
|
@ -63,5 +64,3 @@ mod app {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -18,8 +18,7 @@ mod app {
|
|||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
}
|
||||
struct MySharedResources {}
|
||||
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
|
@ -31,7 +30,11 @@ mod app {
|
|||
|
||||
defmt::println!("USBD initialized");
|
||||
|
||||
(MySharedResources {}, MyLocalResources { usbd: board.usbd }, init::Monotonics())
|
||||
(
|
||||
MySharedResources {},
|
||||
MyLocalResources { usbd: board.usbd },
|
||||
init::Monotonics(),
|
||||
)
|
||||
}
|
||||
|
||||
#[task(binds = USBD, local = [usbd])]
|
||||
|
@ -45,10 +48,10 @@ mod app {
|
|||
|
||||
fn on_event(_usbd: &USBD, event: Event) {
|
||||
defmt::println!("USB: {} @ {}", event, dk::uptime());
|
||||
|
||||
|
||||
match event {
|
||||
Event::UsbReset => todo!(),
|
||||
|
||||
|
||||
Event::UsbEp0DataDone => todo!(),
|
||||
// leave this at it is for now.
|
||||
Event::UsbEp0Setup => {
|
||||
|
@ -58,5 +61,3 @@ mod app {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -19,8 +19,7 @@ mod app {
|
|||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
}
|
||||
struct MySharedResources {}
|
||||
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
|
@ -28,7 +27,11 @@ mod app {
|
|||
|
||||
usbd::init(board.power, &board.usbd);
|
||||
|
||||
(MySharedResources {}, MyLocalResources { usbd: board.usbd }, init::Monotonics())
|
||||
(
|
||||
MySharedResources {},
|
||||
MyLocalResources { usbd: board.usbd },
|
||||
init::Monotonics(),
|
||||
)
|
||||
}
|
||||
|
||||
#[task(binds = USBD, local = [usbd])]
|
||||
|
@ -42,14 +45,14 @@ mod app {
|
|||
|
||||
fn on_event(usbd: &USBD, event: Event) {
|
||||
defmt::println!("USB: {} @ {}", event, dk::uptime());
|
||||
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
// nothing to do here at the moment
|
||||
}
|
||||
|
||||
|
||||
Event::UsbEp0DataDone => todo!(),
|
||||
|
||||
|
||||
Event::UsbEp0Setup => {
|
||||
// the BMREQUESTTYPE register contains information about data recipient, transfer type and direction
|
||||
let bmrequesttype = usbd.bmrequesttype.read().bits() as u8;
|
||||
|
@ -68,14 +71,14 @@ mod app {
|
|||
// composed of a high register (WVALUEH) and a low register (WVALUEL)
|
||||
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);
|
||||
|
||||
|
||||
defmt::println!(
|
||||
"SETUP: bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
|
@ -84,7 +87,7 @@ mod app {
|
|||
windex,
|
||||
wvalue
|
||||
);
|
||||
|
||||
|
||||
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
|
||||
.expect("Error parsing request");
|
||||
match request {
|
||||
|
@ -92,7 +95,7 @@ mod app {
|
|||
if descriptor == Descriptor::Device =>
|
||||
{
|
||||
defmt::println!("GET_DESCRIPTOR Device [length={}]", length);
|
||||
|
||||
|
||||
defmt::println!("Goal reached; move to the next section");
|
||||
dk::exit()
|
||||
}
|
||||
|
@ -105,6 +108,5 @@ mod app {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
|
||||
|
@ -20,9 +19,7 @@ mod app {
|
|||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
|
||||
}
|
||||
struct MySharedResources {}
|
||||
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
|
@ -30,7 +27,11 @@ mod app {
|
|||
|
||||
usbd::init(board.power, &board.usbd);
|
||||
|
||||
(MySharedResources {}, MyLocalResources { usbd: board.usbd }, init::Monotonics())
|
||||
(
|
||||
MySharedResources {},
|
||||
MyLocalResources { usbd: board.usbd },
|
||||
init::Monotonics(),
|
||||
)
|
||||
}
|
||||
|
||||
#[task(binds = USBD, local = [usbd])]
|
||||
|
@ -44,17 +45,17 @@ mod app {
|
|||
|
||||
fn on_event(_usbd: &USBD, event: Event) {
|
||||
defmt::println!("USB: {} @ {}", event, dk::uptime());
|
||||
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
// nothing to do here at the moment
|
||||
}
|
||||
|
||||
|
||||
Event::UsbEp0DataDone => todo!(),
|
||||
|
||||
|
||||
Event::UsbEp0Setup => {
|
||||
// TODO read USBD registers
|
||||
|
||||
|
||||
// the BMREQUESTTYPE register contains information about data recipient, transfer type and direction
|
||||
let bmrequesttype: u8 = 0;
|
||||
// the BREQUEST register stores the type of the current request (e.g. SET_ADDRESS, GET_DESCRIPTOR, ...)
|
||||
|
@ -69,7 +70,7 @@ mod app {
|
|||
// address in SET_ADRESS requests)
|
||||
// composed of a high register (WVALUEH) and a low register (WVALUEL)
|
||||
let wvalue: u16 = 0;
|
||||
|
||||
|
||||
defmt::println!(
|
||||
"SETUP: bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
|
@ -78,7 +79,7 @@ mod app {
|
|||
windex,
|
||||
wvalue
|
||||
);
|
||||
|
||||
|
||||
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
|
||||
.expect("Error parsing request");
|
||||
match request {
|
||||
|
@ -87,9 +88,9 @@ mod app {
|
|||
{
|
||||
// TODO modify `Request::parse()` in `advanced/common/usb/lib.rs`
|
||||
// so that this branch is reached
|
||||
|
||||
|
||||
defmt::println!("GET_DESCRIPTOR Device [length={}]", length);
|
||||
|
||||
|
||||
defmt::println!("Goal reached; move to the next section");
|
||||
dk::exit()
|
||||
}
|
||||
|
@ -102,6 +103,5 @@ mod app {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,9 +20,7 @@ mod app {
|
|||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
|
||||
}
|
||||
struct MySharedResources {}
|
||||
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
|
@ -30,7 +28,14 @@ mod app {
|
|||
|
||||
usbd::init(board.power, &board.usbd);
|
||||
|
||||
(MySharedResources {}, MyLocalResources { usbd: board.usbd, ep0in: board.ep0in, }, init::Monotonics())
|
||||
(
|
||||
MySharedResources {},
|
||||
MyLocalResources {
|
||||
usbd: board.usbd,
|
||||
ep0in: board.ep0in,
|
||||
},
|
||||
init::Monotonics(),
|
||||
)
|
||||
}
|
||||
|
||||
#[task(binds = USBD, local = [usbd, ep0in])]
|
||||
|
@ -45,21 +50,21 @@ mod app {
|
|||
|
||||
fn on_event(usbd: &USBD, ep0in: &mut Ep0In, event: Event) {
|
||||
defmt::println!("USB: {} @ {}", event, dk::uptime());
|
||||
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
// nothing to do here at the moment
|
||||
}
|
||||
|
||||
|
||||
Event::UsbEp0DataDone => ep0in.end(usbd),
|
||||
|
||||
|
||||
Event::UsbEp0Setup => {
|
||||
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);
|
||||
|
||||
|
||||
defmt::println!(
|
||||
"SETUP: bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
|
@ -68,7 +73,7 @@ mod app {
|
|||
windex,
|
||||
wvalue
|
||||
);
|
||||
|
||||
|
||||
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength).expect(
|
||||
"Error parsing request (goal achieved if GET_DESCRIPTOR Device was handled before)",
|
||||
);
|
||||
|
@ -77,7 +82,7 @@ mod app {
|
|||
if descriptor == Descriptor::Device =>
|
||||
{
|
||||
defmt::println!("GET_DESCRIPTOR Device [length={}]", length);
|
||||
|
||||
|
||||
let desc = usb2::device::Descriptor {
|
||||
bDeviceClass: 0,
|
||||
bDeviceProtocol: 0,
|
||||
|
@ -92,7 +97,8 @@ mod app {
|
|||
idVendor: consts::VID,
|
||||
};
|
||||
let desc_bytes = desc.bytes();
|
||||
let resp = &desc_bytes[..core::cmp::min(desc_bytes.len(), usize::from(length))];
|
||||
let resp =
|
||||
&desc_bytes[..core::cmp::min(desc_bytes.len(), usize::from(length))];
|
||||
ep0in.start(&resp, usbd);
|
||||
}
|
||||
Request::SetAddress { .. } => {
|
||||
|
@ -110,6 +116,4 @@ mod app {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -19,16 +19,21 @@ mod app {
|
|||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
|
||||
}
|
||||
struct MySharedResources {}
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
let board = dk::init().unwrap();
|
||||
|
||||
usbd::init(board.power, &board.usbd);
|
||||
|
||||
(MySharedResources {}, MyLocalResources { usbd: board.usbd, ep0in: board.ep0in, }, init::Monotonics())
|
||||
(
|
||||
MySharedResources {},
|
||||
MyLocalResources {
|
||||
usbd: board.usbd,
|
||||
ep0in: board.ep0in,
|
||||
},
|
||||
init::Monotonics(),
|
||||
)
|
||||
}
|
||||
|
||||
#[task(binds = USBD, local = [usbd, ep0in])]
|
||||
|
@ -43,21 +48,21 @@ mod app {
|
|||
|
||||
fn on_event(usbd: &USBD, ep0in: &mut Ep0In, event: Event) {
|
||||
defmt::println!("USB: {} @ {}", event, dk::uptime());
|
||||
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
// nothing to do here at the moment
|
||||
}
|
||||
|
||||
|
||||
Event::UsbEp0DataDone => todo!(), // <- TODO
|
||||
|
||||
|
||||
Event::UsbEp0Setup => {
|
||||
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);
|
||||
|
||||
|
||||
defmt::println!(
|
||||
"SETUP: bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
|
@ -66,7 +71,7 @@ mod app {
|
|||
windex,
|
||||
wvalue
|
||||
);
|
||||
|
||||
|
||||
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength).expect(
|
||||
"Error parsing request (goal achieved if GET_DESCRIPTOR Device was handled before)",
|
||||
);
|
||||
|
@ -75,7 +80,7 @@ mod app {
|
|||
if descriptor == Descriptor::Device =>
|
||||
{
|
||||
defmt::println!("GET_DESCRIPTOR Device [length={}]", length);
|
||||
|
||||
|
||||
// TODO send back a valid device descriptor, truncated to `length` bytes
|
||||
// let desc = usb2::device::Descriptor { .. };
|
||||
let resp = [];
|
||||
|
@ -96,6 +101,4 @@ mod app {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -23,16 +23,22 @@ mod app {
|
|||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
|
||||
}
|
||||
struct MySharedResources {}
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
let board = dk::init().unwrap();
|
||||
|
||||
usbd::init(board.power, &board.usbd);
|
||||
|
||||
(MySharedResources {}, MyLocalResources { usbd: board.usbd, ep0in: board.ep0in, state: State::Default, }, init::Monotonics())
|
||||
|
||||
(
|
||||
MySharedResources {},
|
||||
MyLocalResources {
|
||||
usbd: board.usbd,
|
||||
ep0in: board.ep0in,
|
||||
state: State::Default,
|
||||
},
|
||||
init::Monotonics(),
|
||||
)
|
||||
}
|
||||
|
||||
#[task(binds = USBD, local = [usbd, ep0in, state])]
|
||||
|
@ -47,18 +53,18 @@ mod app {
|
|||
}
|
||||
fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State, event: Event) {
|
||||
defmt::println!("USB: {} @ {}", event, dk::uptime());
|
||||
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
defmt::println!("USB reset condition detected");
|
||||
*state = State::Default;
|
||||
}
|
||||
|
||||
|
||||
Event::UsbEp0DataDone => {
|
||||
defmt::println!("EP0IN: transfer complete");
|
||||
ep0in.end(usbd);
|
||||
}
|
||||
|
||||
|
||||
Event::UsbEp0Setup => {
|
||||
if ep0setup(usbd, ep0in, state).is_err() {
|
||||
defmt::warn!("EP0IN: unexpected request; stalling the endpoint");
|
||||
|
@ -67,17 +73,17 @@ mod app {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// The `bConfigurationValue` of the only supported configuration
|
||||
const CONFIG_VAL: u8 = 42;
|
||||
|
||||
|
||||
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);
|
||||
|
||||
|
||||
defmt::println!(
|
||||
"bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
|
@ -86,7 +92,7 @@ mod app {
|
|||
windex,
|
||||
wvalue
|
||||
);
|
||||
|
||||
|
||||
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
|
||||
.expect("Error parsing request");
|
||||
defmt::println!("EP0: {}", defmt::Debug2Format(&request));
|
||||
|
@ -113,11 +119,11 @@ mod app {
|
|||
let bytes = desc.bytes();
|
||||
let _ = ep0in.start(&bytes[..core::cmp::min(bytes.len(), length.into())], usbd);
|
||||
}
|
||||
|
||||
|
||||
Descriptor::Configuration { index } => {
|
||||
if index == 0 {
|
||||
let mut resp = heapless::Vec::<u8, 64>::new();
|
||||
|
||||
|
||||
let conf_desc = usb2::configuration::Descriptor {
|
||||
wTotalLength: (usb2::configuration::Descriptor::SIZE
|
||||
+ usb2::interface::Descriptor::SIZE)
|
||||
|
@ -131,7 +137,7 @@ mod app {
|
|||
},
|
||||
bMaxPower: 250, // 500 mA
|
||||
};
|
||||
|
||||
|
||||
let iface_desc = usb2::interface::Descriptor {
|
||||
bInterfaceNumber: 0,
|
||||
bAlternativeSetting: 0,
|
||||
|
@ -141,7 +147,7 @@ mod app {
|
|||
bInterfaceProtocol: 0,
|
||||
iInterface: None,
|
||||
};
|
||||
|
||||
|
||||
resp.extend_from_slice(&conf_desc.bytes()).unwrap();
|
||||
resp.extend_from_slice(&iface_desc.bytes()).unwrap();
|
||||
ep0in.start(&resp[..core::cmp::min(resp.len(), length.into())], usbd);
|
||||
|
@ -150,10 +156,10 @@ mod app {
|
|||
return Err(());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_ => return Err(()),
|
||||
},
|
||||
|
||||
|
||||
Request::SetAddress { address } => {
|
||||
match state {
|
||||
State::Default => {
|
||||
|
@ -163,7 +169,7 @@ mod app {
|
|||
// stay in the default state
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
State::Address(..) => {
|
||||
if let Some(address) = address {
|
||||
// use the new address
|
||||
|
@ -172,20 +178,18 @@ mod app {
|
|||
*state = State::Default;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// unspecified behavior
|
||||
State::Configured { .. } => return Err(()),
|
||||
}
|
||||
|
||||
|
||||
// the response to this request is handled in hardware
|
||||
}
|
||||
|
||||
|
||||
// stall any other request
|
||||
_ => return Err(()),
|
||||
}
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
|
||||
|
||||
#[rtic::app(device = dk, peripherals = false)]
|
||||
mod app {
|
||||
|
||||
|
@ -17,7 +16,7 @@ mod app {
|
|||
use usb2::State;
|
||||
// HEADS UP to use *your* USB packet parser uncomment line 12 and remove line 13
|
||||
// use usb::{Request, Descriptor};
|
||||
use usb2::{GetDescriptor as Descriptor, StandardRequest as Request};
|
||||
use usb2::{GetDescriptor as Descriptor, StandardRequest as Request};
|
||||
|
||||
#[local]
|
||||
struct MyLocalResources {
|
||||
|
@ -27,8 +26,7 @@ mod app {
|
|||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
}
|
||||
struct MySharedResources {}
|
||||
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
|
@ -36,7 +34,15 @@ mod app {
|
|||
|
||||
usbd::init(board.power, &board.usbd);
|
||||
|
||||
(MySharedResources {}, MyLocalResources { usbd: board.usbd, ep0in: board.ep0in, state: State::Default, }, init::Monotonics())
|
||||
(
|
||||
MySharedResources {},
|
||||
MyLocalResources {
|
||||
usbd: board.usbd,
|
||||
ep0in: board.ep0in,
|
||||
state: State::Default,
|
||||
},
|
||||
init::Monotonics(),
|
||||
)
|
||||
}
|
||||
|
||||
#[task(binds = USBD, local = [usbd, ep0in, state])]
|
||||
|
@ -52,19 +58,19 @@ mod app {
|
|||
|
||||
fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State, event: Event) {
|
||||
defmt::println!("USB: {} @ {}", event, dk::uptime());
|
||||
|
||||
|
||||
match event {
|
||||
// TODO change `state` as specified in chapter 9.1 USB Device States, of the USB specification
|
||||
Event::UsbReset => {
|
||||
defmt::println!("USB reset condition detected");
|
||||
todo!();
|
||||
}
|
||||
|
||||
|
||||
Event::UsbEp0DataDone => {
|
||||
defmt::println!("EP0IN: transfer complete");
|
||||
ep0in.end(usbd);
|
||||
}
|
||||
|
||||
|
||||
Event::UsbEp0Setup => {
|
||||
if ep0setup(usbd, ep0in, state).is_err() {
|
||||
// unsupported or invalid request:
|
||||
|
@ -74,14 +80,14 @@ mod app {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
||||
|
||||
defmt::println!(
|
||||
"bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
|
@ -90,13 +96,13 @@ mod app {
|
|||
windex,
|
||||
wvalue
|
||||
);
|
||||
|
||||
|
||||
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
|
||||
.expect("Error parsing request");
|
||||
defmt::println!("EP0: {}", defmt::Debug2Format(&request));
|
||||
// ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log
|
||||
// `StandardRequest` with `defmt`
|
||||
|
||||
|
||||
match request {
|
||||
Request::GetDescriptor { descriptor, length } => match descriptor {
|
||||
Descriptor::Device => {
|
||||
|
@ -116,27 +122,25 @@ mod app {
|
|||
let bytes = desc.bytes();
|
||||
let _ = ep0in.start(&bytes[..core::cmp::min(bytes.len(), length.into())], usbd);
|
||||
}
|
||||
|
||||
|
||||
// TODO implement Configuration descriptor
|
||||
// Descriptor::Configuration { .. } => todo!(),
|
||||
|
||||
|
||||
// stall any other request
|
||||
_ => return Err(()),
|
||||
},
|
||||
Request::SetAddress { .. } => {
|
||||
// On Mac OS you'll get this request before the GET_DESCRIPTOR request so we
|
||||
// need to catch it here.
|
||||
|
||||
|
||||
// TODO: handle this request properly now.
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
||||
// stall any other request
|
||||
_ => return Err(()),
|
||||
}
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -23,16 +23,22 @@ mod app {
|
|||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
|
||||
}
|
||||
struct MySharedResources {}
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
let board = dk::init().unwrap();
|
||||
|
||||
usbd::init(board.power, &board.usbd);
|
||||
|
||||
(MySharedResources {}, MyLocalResources { usbd: board.usbd, ep0in: board.ep0in, state: State::Default, }, init::Monotonics())
|
||||
(
|
||||
MySharedResources {},
|
||||
MyLocalResources {
|
||||
usbd: board.usbd,
|
||||
ep0in: board.ep0in,
|
||||
state: State::Default,
|
||||
},
|
||||
init::Monotonics(),
|
||||
)
|
||||
}
|
||||
|
||||
#[task(binds = USBD, local = [usbd, ep0in, state])]
|
||||
|
@ -47,18 +53,18 @@ mod app {
|
|||
}
|
||||
fn on_event(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State, event: Event) {
|
||||
defmt::println!("USB: {} @ {}", event, dk::uptime());
|
||||
|
||||
|
||||
match event {
|
||||
Event::UsbReset => {
|
||||
defmt::println!("USB reset condition detected");
|
||||
*state = State::Default;
|
||||
}
|
||||
|
||||
|
||||
Event::UsbEp0DataDone => {
|
||||
defmt::println!("EP0IN: transfer complete");
|
||||
ep0in.end(usbd);
|
||||
}
|
||||
|
||||
|
||||
Event::UsbEp0Setup => {
|
||||
if ep0setup(usbd, ep0in, state).is_err() {
|
||||
defmt::warn!("EP0IN: unexpected request; stalling the endpoint");
|
||||
|
@ -67,17 +73,17 @@ mod app {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// The `bConfigurationValue` of the only supported configuration
|
||||
const CONFIG_VAL: u8 = 42;
|
||||
|
||||
|
||||
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);
|
||||
|
||||
|
||||
defmt::println!(
|
||||
"bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
||||
bmrequesttype,
|
||||
|
@ -86,13 +92,13 @@ mod app {
|
|||
windex,
|
||||
wvalue
|
||||
);
|
||||
|
||||
|
||||
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
|
||||
.expect("Error parsing request");
|
||||
defmt::println!("EP0: {}", defmt::Debug2Format(&request));
|
||||
// ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log
|
||||
// `StandardRequest` with `defmt`
|
||||
|
||||
|
||||
match request {
|
||||
// section 9.4.3
|
||||
// this request is valid in any state
|
||||
|
@ -114,11 +120,11 @@ mod app {
|
|||
let bytes = desc.bytes();
|
||||
let _ = ep0in.start(&bytes[..core::cmp::min(bytes.len(), length.into())], usbd);
|
||||
}
|
||||
|
||||
|
||||
Descriptor::Configuration { index } => {
|
||||
if index == 0 {
|
||||
let mut resp = heapless::Vec::<u8, 64>::new();
|
||||
|
||||
|
||||
let conf_desc = usb2::configuration::Descriptor {
|
||||
wTotalLength: (usb2::configuration::Descriptor::SIZE
|
||||
+ usb2::interface::Descriptor::SIZE)
|
||||
|
@ -132,7 +138,7 @@ mod app {
|
|||
},
|
||||
bMaxPower: 250, // 500 mA
|
||||
};
|
||||
|
||||
|
||||
let iface_desc = usb2::interface::Descriptor {
|
||||
bInterfaceNumber: 0,
|
||||
bAlternativeSetting: 0,
|
||||
|
@ -142,7 +148,7 @@ mod app {
|
|||
bInterfaceProtocol: 0,
|
||||
iInterface: None,
|
||||
};
|
||||
|
||||
|
||||
resp.extend_from_slice(&conf_desc.bytes()).unwrap();
|
||||
resp.extend_from_slice(&iface_desc.bytes()).unwrap();
|
||||
ep0in.start(&resp[..core::cmp::min(resp.len(), length.into())], usbd);
|
||||
|
@ -151,10 +157,10 @@ mod app {
|
|||
return Err(());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_ => return Err(()),
|
||||
},
|
||||
|
||||
|
||||
Request::SetAddress { address } => {
|
||||
match state {
|
||||
State::Default => {
|
||||
|
@ -164,7 +170,7 @@ mod app {
|
|||
// stay in the default state
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
State::Address(..) => {
|
||||
if let Some(address) = address {
|
||||
// use the new address
|
||||
|
@ -173,19 +179,19 @@ mod app {
|
|||
*state = State::Default;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// unspecified behavior
|
||||
State::Configured { .. } => return Err(()),
|
||||
}
|
||||
|
||||
|
||||
// the response to this request is handled in hardware
|
||||
}
|
||||
|
||||
|
||||
Request::SetConfiguration { value } => {
|
||||
match *state {
|
||||
// unspecified behavior
|
||||
State::Default => return Err(()),
|
||||
|
||||
|
||||
State::Address(address) => {
|
||||
if let Some(value) = value {
|
||||
if value.get() == CONFIG_VAL {
|
||||
|
@ -199,7 +205,7 @@ mod app {
|
|||
// stay in the address mode
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
State::Configured {
|
||||
address,
|
||||
value: curr_value,
|
||||
|
@ -223,17 +229,15 @@ mod app {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
usbd.tasks_ep0status
|
||||
.write(|w| w.tasks_ep0status().set_bit());
|
||||
}
|
||||
|
||||
|
||||
// stall any other request
|
||||
_ => return Err(()),
|
||||
}
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue