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