mirror of
https://github.com/ferrous-systems/embedded-trainings-2020.git
synced 2025-01-10 08:15:36 +00:00
usb-2-solution.rs: cleanup, move addr setting into helper
This commit is contained in:
parent
e5728a440f
commit
351cce7c16
2 changed files with 32 additions and 30 deletions
|
@ -6,6 +6,9 @@
|
||||||
|
|
||||||
use core::num::NonZeroU8;
|
use core::num::NonZeroU8;
|
||||||
|
|
||||||
|
/// Device address assigned by the host; will be in the range 1..=127
|
||||||
|
pub type Address = NonZeroU8;
|
||||||
|
|
||||||
/// Standard USB request
|
/// Standard USB request
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
pub enum Request {
|
pub enum Request {
|
||||||
|
@ -22,7 +25,7 @@ pub enum Request {
|
||||||
// see section 9.4.6 of the USB specification
|
// see section 9.4.6 of the USB specification
|
||||||
SetAddress {
|
SetAddress {
|
||||||
/// New device address, in the range `1..=127`
|
/// New device address, in the range `1..=127`
|
||||||
address: Option<NonZeroU8>,
|
address: Option<Address>,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// SET_CONFIGURATION
|
/// SET_CONFIGURATION
|
||||||
|
@ -101,8 +104,6 @@ pub enum Descriptor {
|
||||||
// there are even more descriptor types but we don't need to support them
|
// there are even more descriptor types but we don't need to support them
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Device address assigned by the host; will be in the range 1..=127
|
|
||||||
pub type Address = NonZeroU8;
|
|
||||||
/// The state of the USB device
|
/// The state of the USB device
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
pub enum DeviceState {
|
pub enum DeviceState {
|
||||||
|
@ -122,6 +123,7 @@ pub enum DeviceState {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
#[cfg(TODO)]
|
||||||
use core::num::NonZeroU8;
|
use core::num::NonZeroU8;
|
||||||
|
|
||||||
use crate::{Descriptor, Request};
|
use crate::{Descriptor, Request};
|
||||||
|
|
|
@ -6,7 +6,7 @@ use dk::{
|
||||||
usbd::{self, Event},
|
usbd::{self, Event},
|
||||||
};
|
};
|
||||||
use panic_log as _; // panic handler
|
use panic_log as _; // panic handler
|
||||||
use usb::{Descriptor, Request, DeviceState};
|
use usb::{Address, Descriptor, DeviceState, Request};
|
||||||
|
|
||||||
#[rtic::app(device = dk)]
|
#[rtic::app(device = dk)]
|
||||||
const APP: () = {
|
const APP: () = {
|
||||||
|
@ -67,8 +67,8 @@ fn on_event(usbd: &USBD, state: &mut DeviceState, event: Event) {
|
||||||
wvalue
|
wvalue
|
||||||
);
|
);
|
||||||
|
|
||||||
// todo handle less indentedly?
|
let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
|
||||||
if let Ok(request) = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength) {
|
.expect("Error parsing request");
|
||||||
match request {
|
match request {
|
||||||
Request::GetDescriptor { descriptor, length }
|
Request::GetDescriptor { descriptor, length }
|
||||||
if descriptor == Descriptor::Device =>
|
if descriptor == Descriptor::Device =>
|
||||||
|
@ -78,24 +78,24 @@ fn on_event(usbd: &USBD, state: &mut DeviceState, event: Event) {
|
||||||
log::info!("Goal reached; move to the next section");
|
log::info!("Goal reached; move to the next section");
|
||||||
dk::exit()
|
dk::exit()
|
||||||
}
|
}
|
||||||
Request::SetAddress { address } => {
|
Request::SetAddress { address } => set_device_address(state, address),
|
||||||
log::info!("SETUP: device state is {:?}", state);
|
_ => unreachable!(), // we don't handle any other Requests
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// todo hide this in a helper?
|
fn set_device_address(state: &mut DeviceState, address: Option<Address>) {
|
||||||
// todo check for state configured -> err
|
match state {
|
||||||
|
DeviceState::Default | DeviceState::Address(..) => {
|
||||||
if let Some(address) = address {
|
if let Some(address) = address {
|
||||||
log::info!("SETUP: assigning address {}", address);
|
log::info!("SETUP: assigning device address {}", address);
|
||||||
*state = DeviceState::Address(address);
|
*state = DeviceState::Address(address);
|
||||||
} else {
|
} else {
|
||||||
log::info!("SETUP: address was None; assigning Default");
|
log::info!("SETUP: address was None; assigning Default");
|
||||||
*state = DeviceState::Default;
|
*state = DeviceState::Default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => unreachable!(), // we don't handle any other Requests
|
DeviceState::Configured { .. } => panic!(), // unspecified behavior
|
||||||
}
|
|
||||||
} else {
|
|
||||||
unreachable!() // don't care about this for now
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue