2020-06-09 09:52:27 +00:00
|
|
|
#![no_main]
|
|
|
|
#![no_std]
|
|
|
|
|
2021-04-14 09:49:34 +00:00
|
|
|
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
|
|
|
use firmware as _;
|
2020-06-09 09:52:27 +00:00
|
|
|
|
2022-01-07 16:24:21 +00:00
|
|
|
#[rtic::app(device = dk, peripherals = false)]
|
|
|
|
mod app {
|
|
|
|
use dk::{
|
|
|
|
peripheral::USBD,
|
|
|
|
usbd::{self, Ep0In, Event},
|
|
|
|
Peripherals,
|
|
|
|
};
|
|
|
|
use usb::{Descriptor, Request};
|
|
|
|
|
|
|
|
#[local]
|
|
|
|
struct MyLocalResources {
|
2020-06-09 09:52:27 +00:00
|
|
|
usbd: USBD,
|
|
|
|
ep0in: Ep0In,
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:24:21 +00:00
|
|
|
#[shared]
|
|
|
|
struct MySharedResources {
|
|
|
|
|
|
|
|
}
|
2020-06-09 16:17:30 +00:00
|
|
|
#[init]
|
2022-01-07 16:24:21 +00:00
|
|
|
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
2020-06-09 09:52:27 +00:00
|
|
|
let board = dk::init().unwrap();
|
|
|
|
|
|
|
|
usbd::init(board.power, &board.usbd);
|
|
|
|
|
2022-01-07 16:24:21 +00:00
|
|
|
(MySharedResources {}, MyLocalResources { usbd: board.usbd, ep0in: board.ep0in, }, init::Monotonics())
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:24:21 +00:00
|
|
|
#[task(binds = USBD, local = [usbd, ep0in])]
|
2020-06-25 17:14:39 +00:00
|
|
|
fn main(cx: main::Context) {
|
2022-01-07 16:24:21 +00:00
|
|
|
let usbd = cx.local.usbd;
|
|
|
|
let ep0in = cx.local.ep0in;
|
2020-06-09 09:52:27 +00:00
|
|
|
|
|
|
|
while let Some(event) = usbd::next_event(usbd) {
|
|
|
|
on_event(usbd, ep0in, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:24:21 +00:00
|
|
|
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,
|
|
|
|
brequest,
|
|
|
|
wlength,
|
|
|
|
windex,
|
|
|
|
wvalue
|
|
|
|
);
|
|
|
|
|
|
|
|
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength).expect(
|
|
|
|
"Error parsing request (goal achieved if GET_DESCRIPTOR Device was handled before)",
|
|
|
|
);
|
|
|
|
match request {
|
|
|
|
Request::GetDescriptor { descriptor, length }
|
|
|
|
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 = [];
|
|
|
|
ep0in.start(&resp, usbd);
|
|
|
|
}
|
|
|
|
Request::SetAddress { .. } => {
|
|
|
|
// On Mac OS 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)"
|
|
|
|
);
|
|
|
|
dk::exit()
|
|
|
|
}
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 16:24:21 +00:00
|
|
|
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
2022-01-07 16:24:21 +00:00
|
|
|
|