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-02 16:23:41 +00:00
|
|
|
use usb::{Address, Descriptor, DeviceState, 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 {
|
2020-07-02 16:07:16 +00:00
|
|
|
state: DeviceState,
|
2020-06-09 09:52:27 +00:00
|
|
|
usbd: USBD,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[init]
|
|
|
|
fn init(_cx: init::Context) -> init::LateResources {
|
|
|
|
let board = dk::init().unwrap();
|
|
|
|
|
|
|
|
usbd::init(board.power, &board.usbd);
|
|
|
|
|
2020-07-02 16:07:16 +00:00
|
|
|
init::LateResources {
|
|
|
|
usbd: board.usbd,
|
|
|
|
state: DeviceState::Default,
|
|
|
|
}
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 16:07:16 +00:00
|
|
|
#[task(binds = USBD, resources = [state, 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;
|
2020-07-02 16:07:16 +00:00
|
|
|
let state = cx.resources.state; // used to store the device address sent by the host
|
2020-06-09 09:52:27 +00:00
|
|
|
|
|
|
|
while let Some(event) = usbd::next_event(usbd) {
|
2020-07-02 16:07:16 +00:00
|
|
|
on_event(usbd, state, event)
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-02 16:07:16 +00:00
|
|
|
fn on_event(usbd: &USBD, state: &mut DeviceState, 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 => {
|
|
|
|
let bmrequesttype = usbd.bmrequesttype.read().bits() as u8;
|
|
|
|
let brequest = usbd.brequest.read().brequest().bits();
|
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());
|
|
|
|
let windex = (u16::from(usbd.windexh.read().windexh().bits()) << 8)
|
|
|
|
| u16::from(usbd.windexl.read().windexl().bits());
|
|
|
|
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
|
|
|
|
|
|
|
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-02 16:23:41 +00:00
|
|
|
Request::SetAddress { address } => set_device_address(state, address),
|
|
|
|
_ => unreachable!(), // we don't handle any other Requests
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_device_address(state: &mut DeviceState, address: Option<Address>) {
|
|
|
|
match state {
|
|
|
|
DeviceState::Default | DeviceState::Address(..) => {
|
|
|
|
if let Some(address) = address {
|
|
|
|
log::info!("SETUP: assigning device address {}", address);
|
|
|
|
*state = DeviceState::Address(address);
|
2020-06-09 09:52:27 +00:00
|
|
|
} else {
|
2020-07-02 16:23:41 +00:00
|
|
|
log::info!("SETUP: address was None; assigning Default");
|
|
|
|
*state = DeviceState::Default;
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-02 16:23:41 +00:00
|
|
|
DeviceState::Configured { .. } => panic!(), // unspecified behavior
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
}
|