diff options
-rw-r--r-- | usbh/src/device.rs | 36 | ||||
-rwxr-xr-x | usbh/src/lib.rs | 12 | ||||
-rw-r--r-- | usbh/src/pipe.rs | 10 | ||||
-rw-r--r-- | usbh/src/usbproto.rs | 80 |
4 files changed, 71 insertions, 67 deletions
diff --git a/usbh/src/device.rs b/usbh/src/device.rs index d824aab..f300aa4 100644 --- a/usbh/src/device.rs +++ b/usbh/src/device.rs @@ -9,7 +9,7 @@ use log::{debug, error, info, trace}; use atsamd_hal::target_device::usb; const MAX_DEVICES: usize = 16; -const MAX_ENDPOINTS: usize = 8; +const MAX_ENDPOINTS: usize = 16; // How long to wait before talking to the device again after setting // its address. cf §9.2.6.3 of USB 2.0 @@ -139,11 +139,11 @@ impl Device { let mut pipe = pipe_table.pipe_for(host, self.addr, 0); let mut vol_descr = - ::vcell::VolatileCell::<USBDeviceDescriptor>::new(Default::default()); + ::vcell::VolatileCell::<DeviceDescriptor>::new(Default::default()); pipe.control_req( - BMRequestType::get_descr(), - USBRequest::GetDescriptor, - WValue::from((0, USBDescriptor::Device as u8)), + RequestType::get_descr(), + RequestCode::GetDescriptor, + WValue::from((0, DescriptorType::Device as u8)), 0, Some(DataBuf::from(&mut vol_descr)), self.millis, @@ -161,11 +161,11 @@ impl Device { let mut pipe = pipe_table.pipe_for(host, self.addr, 0); let mut vol_descr = - ::vcell::VolatileCell::<USBConfigurationDescriptor>::new(Default::default()); + ::vcell::VolatileCell::<ConfigurationDescriptor>::new(Default::default()); pipe.control_req( - BMRequestType::get_descr(), - USBRequest::GetDescriptor, - WValue::from((0, USBDescriptor::Configuration as u8)), + RequestType::get_descr(), + RequestCode::GetDescriptor, + WValue::from((0, DescriptorType::Configuration as u8)), 0, Some(DataBuf::from(&mut vol_descr)), self.millis, @@ -178,9 +178,9 @@ impl Device { let buf: [u8; 64] = [0; 64]; let mut tmp = &buf[..desc.w_total_length as usize]; pipe.control_req( - BMRequestType::get_descr(), - USBRequest::GetDescriptor, - WValue::from((0, USBDescriptor::Configuration as u8)), + RequestType::get_descr(), + RequestCode::GetDescriptor, + WValue::from((0, DescriptorType::Configuration as u8)), 0, Some(DataBuf::from(&mut tmp)), self.millis, @@ -195,8 +195,8 @@ impl Device { debug!("+++ setting configuration"); let conf: u8 = 1; pipe.control_req( - BMRequestType::set(), - USBRequest::SetConfiguration, + RequestType::set(), + RequestCode::SetConfiguration, WValue::from((conf, 0)), 0, None, @@ -206,12 +206,12 @@ impl Device { debug!("+++ setting idle"); pipe.control_req( - BMRequestType::from(( + RequestType::from(( USBSetupDirection::HostToDevice, USBSetupType::Class, USBSetupRecipient::Interface, )), - USBRequest::GetInterface, // This is also idle, but can't have two enums with the same value. + RequestCode::GetInterface, // This is also idle, but can't have two enums with the same value. WValue::from((0, 0)), 0, None, @@ -222,12 +222,12 @@ impl Device { debug!("+++ setting report"); let mut rep_res: u8 = 0; pipe.control_req( - BMRequestType::from(( + RequestType::from(( USBSetupDirection::HostToDevice, USBSetupType::Class, USBSetupRecipient::Interface, )), - USBRequest::SetConfiguration, + RequestCode::SetConfiguration, WValue::from((0, 2)), 0, Some(DataBuf::from(&mut rep_res)), diff --git a/usbh/src/lib.rs b/usbh/src/lib.rs index b0a103a..c4e2426 100755 --- a/usbh/src/lib.rs +++ b/usbh/src/lib.rs @@ -359,11 +359,11 @@ where fn configure_dev(&mut self) -> Result<(), PipeErr> { let mut pipe = self.pipe_table.pipe_for(self.usb.host_mut(), 0, 0); - let mut vol_descr = ::vcell::VolatileCell::<USBDeviceDescriptor>::new(Default::default()); + let mut vol_descr = ::vcell::VolatileCell::<DeviceDescriptor>::new(Default::default()); pipe.control_req( - BMRequestType::get_descr(), - USBRequest::GetDescriptor, - WValue::from((0, USBDescriptor::Device as u8)), + RequestType::get_descr(), + RequestCode::GetDescriptor, + WValue::from((0, DescriptorType::Device as u8)), 0, Some(DataBuf::from(&mut vol_descr)), self.millis, @@ -379,8 +379,8 @@ where device.max_packet_size = desc.b_max_packet_size; debug!("Setting address to {}.", device.addr); pipe.control_req( - BMRequestType::set(), - USBRequest::SetAddress, + RequestType::set(), + RequestCode::SetAddress, WValue::from((device.addr, 0)), 0, None, diff --git a/usbh/src/pipe.rs b/usbh/src/pipe.rs index e17bec5..936aa23 100644 --- a/usbh/src/pipe.rs +++ b/usbh/src/pipe.rs @@ -92,8 +92,8 @@ pub(crate) struct Pipe<'a, 'b> { impl Pipe<'_, '_> { pub(crate) fn control_req( &mut self, - bm_request_type: BMRequestType, - b_request: USBRequest, + bm_request_type: RequestType, + b_request: RequestCode, w_value: WValue, w_index: u16, buf: Option<DataBuf>, @@ -110,7 +110,7 @@ impl Pipe<'_, '_> { /* * Setup stage. */ - let mut setup_packet = USBSetupPacket { + let mut setup_packet = SetupPacket { bm_request_type: bm_request_type, b_request: b_request, w_value: w_value, @@ -133,6 +133,8 @@ impl Pipe<'_, '_> { */ self.dtgl_set(); if let Some(b) = buf { + // TODO: data stage, has up to 5,000ms (in 500ms + // per-packet chunks) to complete. cf §9.2.6.4 of USB 2.0. match bm_request_type.direction() { USBSetupDirection::DeviceToHost => { trace!("buf0: {:?}", &b); @@ -149,6 +151,8 @@ impl Pipe<'_, '_> { /* * Status stage. */ + // TODO: status stage has up to 50ms to complete. cf §9.2.6.4 + // of USB 2.0. self.dtgl_set(); self.desc.bank0.pcksize.write(|w| { unsafe { w.byte_count().bits(0) }; diff --git a/usbh/src/usbproto.rs b/usbh/src/usbproto.rs index be8ebfe..690cb40 100644 --- a/usbh/src/usbproto.rs +++ b/usbh/src/usbproto.rs @@ -8,9 +8,9 @@ #[derive(Copy, Clone, Debug, Default, PartialEq)] #[repr(C, packed)] -pub struct USBDeviceDescriptor { +pub struct DeviceDescriptor { pub b_length: u8, - pub b_descriptor_type: USBDescriptor, + pub b_descriptor_type: DescriptorType, pub bcd_usb: u16, pub b_device_class: u8, pub b_device_sub_class: u8, @@ -27,9 +27,9 @@ pub struct USBDeviceDescriptor { #[derive(Copy, Clone, Debug, Default, PartialEq)] #[repr(C, packed)] -pub struct USBConfigurationDescriptor { +pub struct ConfigurationDescriptor { pub b_length: u8, - pub b_descriptor_type: USBDescriptor, + pub b_descriptor_type: DescriptorType, pub w_total_length: u16, pub b_num_interfaces: u8, pub b_configuration_value: u8, @@ -40,9 +40,9 @@ pub struct USBConfigurationDescriptor { #[derive(Copy, Clone, Debug, Default, PartialEq)] #[repr(C, packed)] -pub struct USBInterfaceDescriptor { +pub struct InterfaceDescriptor { pub b_length: u8, - pub b_descriptor_type: USBDescriptor, + pub b_descriptor_type: DescriptorType, pub b_interface_number: u8, pub b_alternate_setting: u8, pub b_num_endpoints: u8, @@ -54,9 +54,9 @@ pub struct USBInterfaceDescriptor { #[derive(Copy, Clone, Debug, Default, PartialEq)] #[repr(C, packed)] -pub struct USBEndpointDescriptor { +pub struct EndpointDescriptor { pub b_length: u8, - pub b_descriptor_type: USBDescriptor, + pub b_descriptor_type: DescriptorType, pub b_endpoint_address: u8, pub bm_attributes: u8, pub w_max_packet_size: u16, @@ -65,9 +65,9 @@ pub struct USBEndpointDescriptor { #[derive(Copy, Clone, Debug, Default, PartialEq)] #[repr(C, packed)] -pub struct USBSetupPacket { - pub bm_request_type: BMRequestType, - pub b_request: USBRequest, +pub struct SetupPacket { + pub bm_request_type: RequestType, + pub b_request: RequestCode, pub w_value: WValue, pub w_index: u16, pub w_length: u16, @@ -136,8 +136,8 @@ where #[derive(Clone, Copy, Debug, Default, PartialEq)] #[repr(C)] -pub struct BMRequestType(u8); -impl BMRequestType { +pub struct RequestType(u8); +impl RequestType { // Get descriptor request type. pub fn get_descr() -> Self { Self::from(( @@ -204,12 +204,12 @@ impl BMRequestType { self.0 |= v as u8 & MASK; } } -impl From<u8> for BMRequestType { +impl From<u8> for RequestType { fn from(v: u8) -> Self { Self(v) } } -impl From<(USBSetupDirection, USBSetupType, USBSetupRecipient)> for BMRequestType { +impl From<(USBSetupDirection, USBSetupType, USBSetupRecipient)> for RequestType { fn from(v: (USBSetupDirection, USBSetupType, USBSetupRecipient)) -> Self { Self(v.0 as u8 | v.1 as u8 | v.2 as u8) } @@ -255,7 +255,7 @@ impl From<(u8, u8)> for WValue { } #[derive(Clone, Copy, Debug, PartialEq)] -pub enum USBRequest { +pub enum RequestCode { GetStatus = 0, ClearFeature = 1, SetFeature = 3, @@ -268,7 +268,7 @@ pub enum USBRequest { SetInterface = 11, SynchFrame = 12, } -impl<T> From<T> for USBRequest +impl<T> From<T> for RequestCode where T: Into<u8>, { @@ -289,7 +289,7 @@ where } } } -impl Default for USBRequest { +impl Default for RequestCode { fn default() -> Self { Self::GetStatus } @@ -303,18 +303,18 @@ pub enum USBFeature { } #[derive(Clone, Copy, Debug, PartialEq)] -pub enum USBDescriptor { - Device = 0x01, - Configuration = 0x02, - String = 0x03, - Interface = 0x04, - Endpoint = 0x05, - DeviceQualifier = 0x06, - OtherSpeed = 0x07, - InterfacePower = 0x08, - OTG = 0x09, +pub enum DescriptorType { + Device = 1, + Configuration = 2, + String = 3, + Interface = 4, + Endpoint = 5, + DeviceQualifier = 6, + OtherSpeed = 7, + InterfacePower = 8, + OTG = 9, } -impl Default for USBDescriptor { +impl Default for DescriptorType { fn default() -> Self { Self::Device } @@ -347,9 +347,9 @@ mod test { #[test] fn dev_desc_serialize() { - let desc = USBDeviceDescriptor { + let desc = DeviceDescriptor { b_length: 18, - b_descriptor_type: USBDescriptor::Device, + b_descriptor_type: DescriptorType::Device, bcd_usb: 0x0110, b_device_class: 0x02, b_device_sub_class: 0x03, @@ -373,9 +373,9 @@ mod test { #[test] fn config_desc_serialize() { - let desc = USBConfigurationDescriptor { + let desc = ConfigurationDescriptor { b_length: 18, - b_descriptor_type: USBDescriptor::Configuration, + b_descriptor_type: DescriptorType::Configuration, w_total_length: 0x20, b_num_interfaces: 5, b_configuration_value: 10, @@ -390,9 +390,9 @@ mod test { #[test] fn interface_desc_serialize() { - let desc = USBInterfaceDescriptor { + let desc = InterfaceDescriptor { b_length: 18, - b_descriptor_type: USBDescriptor::Interface, + b_descriptor_type: DescriptorType::Interface, b_interface_number: 2, b_alternate_setting: 0xaa, b_num_endpoints: 5, @@ -408,9 +408,9 @@ mod test { #[test] fn endpoint_desc_serialize() { - let desc = USBEndpointDescriptor { + let desc = EndpointDescriptor { b_length: 18, - b_descriptor_type: USBDescriptor::Endpoint, + b_descriptor_type: DescriptorType::Endpoint, b_endpoint_address: 1, bm_attributes: 0x22, w_max_packet_size: 0xdead, @@ -423,9 +423,9 @@ mod test { #[test] fn setup_packet_serialize() { - let setup_packet = USBSetupPacket { - bm_request_type: BMRequestType::get_descr(), - b_request: USBRequest::GetDescriptor, + let setup_packet = SetupPacket { + bm_request_type: RequestType::get_descr(), + b_request: RequestCode::GetDescriptor, w_value: WValue::from((0x00, 0x01)), w_index: 0xbeef, w_length: 18, |