2020-06-09 09:52:27 +00:00
|
|
|
#![no_main]
|
|
|
|
#![no_std]
|
|
|
|
|
|
|
|
use dk::{
|
|
|
|
peripheral::USBD,
|
|
|
|
usbd::{self, Event},
|
|
|
|
};
|
|
|
|
use panic_log as _; // panic handler
|
2020-07-03 10:04:05 +00:00
|
|
|
use usb::{Descriptor, Request};
|
2020-06-09 09:52:27 +00:00
|
|
|
|
2020-06-12 15:54:45 +00:00
|
|
|
#[rtic::app(device = dk)]
|
2020-06-09 09:52:27 +00:00
|
|
|
const APP: () = {
|
|
|
|
struct Resources {
|
|
|
|
usbd: USBD,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[init]
|
|
|
|
fn init(_cx: init::Context) -> init::LateResources {
|
|
|
|
let board = dk::init().unwrap();
|
|
|
|
|
|
|
|
usbd::init(board.power, &board.usbd);
|
|
|
|
|
2020-07-03 15:00:01 +00:00
|
|
|
init::LateResources { usbd: board.usbd }
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 10:04:05 +00:00
|
|
|
#[task(binds = USBD, resources = [usbd])]
|
2020-06-25 17:14:39 +00:00
|
|
|
fn main(cx: main::Context) {
|
2020-06-09 09:52:27 +00:00
|
|
|
let usbd = cx.resources.usbd;
|
|
|
|
|
|
|
|
while let Some(event) = usbd::next_event(usbd) {
|
2020-07-03 10:04:05 +00:00
|
|
|
on_event(usbd, event)
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-03 10:04:05 +00:00
|
|
|
fn on_event(usbd: &USBD, event: Event) {
|
2020-06-25 17:14:39 +00:00
|
|
|
log::info!("USB: {:?} @ {:?}", event, dk::uptime());
|
2020-06-09 09:52:27 +00:00
|
|
|
|
|
|
|
match event {
|
|
|
|
Event::UsbReset => {
|
|
|
|
// nothing to do here at the moment
|
|
|
|
}
|
|
|
|
|
2020-06-25 17:14:39 +00:00
|
|
|
Event::UsbEp0DataDone => todo!(),
|
2020-06-09 09:52:27 +00:00
|
|
|
|
|
|
|
Event::UsbEp0Setup => {
|
2020-07-13 12:47:36 +00:00
|
|
|
// the BMREQUESTTYPE register contains information about data recipient, transfer type and direction
|
2020-06-09 09:52:27 +00:00
|
|
|
let bmrequesttype = usbd.bmrequesttype.read().bits() as u8;
|
2020-07-13 12:47:36 +00:00
|
|
|
// the BREQUEST register stores the type of the current request (e.g. SET_ADDRESS, GET_DESCRIPTOR, ...)
|
2020-06-09 09:52:27 +00:00
|
|
|
let brequest = usbd.brequest.read().brequest().bits();
|
2020-07-13 12:47:36 +00:00
|
|
|
// wLength denotes the number of bytes to transfer (if any)
|
|
|
|
// composed of a high register (WLENGTHH) and a low register (WLENGTHL)
|
2020-06-25 17:14:39 +00:00
|
|
|
let wlength = (u16::from(usbd.wlengthh.read().wlengthh().bits()) << 8)
|
|
|
|
| u16::from(usbd.wlengthl.read().wlengthl().bits());
|
2020-07-13 12:47:36 +00:00
|
|
|
// wIndex is a generic index field whose meaning depends on the request type
|
|
|
|
// composed of a high register (WINDEXH) and a low register (WINDEXL)
|
2020-06-25 17:14:39 +00:00
|
|
|
let windex = (u16::from(usbd.windexh.read().windexh().bits()) << 8)
|
|
|
|
| u16::from(usbd.windexl.read().windexl().bits());
|
2020-07-13 12:47:36 +00:00
|
|
|
// wValue is a generic paremeter field meaning depends on the request type (e.g. contains the device
|
|
|
|
// address in SET_ADRESS requests)
|
|
|
|
// composed of a high register (WVALUEH) and a low register (WVALUEL)
|
2020-06-25 17:14:39 +00:00
|
|
|
let wvalue = (u16::from(usbd.wvalueh.read().wvalueh().bits()) << 8)
|
|
|
|
| u16::from(usbd.wvaluel.read().wvaluel().bits());
|
2020-06-09 09:52:27 +00:00
|
|
|
|
2020-07-16 10:06:57 +00:00
|
|
|
// 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);
|
|
|
|
|
2020-06-09 09:52:27 +00:00
|
|
|
log::info!(
|
2020-06-25 17:14:39 +00:00
|
|
|
"SETUP: bmrequesttype: {}, brequest: {}, wlength: {}, windex: {}, wvalue: {}",
|
2020-06-09 09:52:27 +00:00
|
|
|
bmrequesttype,
|
|
|
|
brequest,
|
|
|
|
wlength,
|
|
|
|
windex,
|
|
|
|
wvalue
|
|
|
|
);
|
|
|
|
|
2020-07-02 16:23:41 +00:00
|
|
|
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
|
|
|
|
.expect("Error parsing request");
|
|
|
|
match request {
|
|
|
|
Request::GetDescriptor { descriptor, length }
|
|
|
|
if descriptor == Descriptor::Device =>
|
|
|
|
{
|
|
|
|
log::info!("GET_DESCRIPTOR Device [length={}]", length);
|
2020-06-09 09:52:27 +00:00
|
|
|
|
2020-07-02 16:23:41 +00:00
|
|
|
log::info!("Goal reached; move to the next section");
|
|
|
|
dk::exit()
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
2020-07-03 10:04:05 +00:00
|
|
|
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.
|
2020-07-03 15:00:01 +00:00
|
|
|
}
|
2020-07-02 16:23:41 +00:00
|
|
|
_ => unreachable!(), // we don't handle any other Requests
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|