diff --git a/src/types/usb_device.rs b/src/types/usb_device.rs index 78b4dae..241a888 100644 --- a/src/types/usb_device.rs +++ b/src/types/usb_device.rs @@ -91,6 +91,51 @@ impl UsbDevice { Ok(()) } + pub fn configure_endpoint( + &mut self, + cs: CriticalSection<'_>, + endpoint_index: usize, + ) -> Result<(), UsbError> { + match self.select_endpoint(cs, endpoint_index) { + Ok(_) => { + let usb = self.usb.borrow(cs); + let current_endpoint = self.ep_table[endpoint_index]; + + // Clear interrupt. // + + usb.udint.modify(|_, w| w.eorsti().clear_bit()); + + // Enable endpoint. // + + usb.ueconx.modify(|_, w| w.epen().set_bit()); + + // Set markered endpoint parameters to uecfg0x/1x register. // + + usb.uecfg0x.modify(|_, w| { + w.epdir() + .bit(current_endpoint.ep_dir) + .eptype() + .bits(current_endpoint.ep_type) + }); + + usb.uecfg1x.modify(|_, w| { + w.epbk() + .bits(current_endpoint.banks) + .epsize() + .bits(current_endpoint.size) + .alloc() + .bit(current_endpoint.is_allocated) + }); + + if !usb.uesta0x.read().cfgok().bit() { + Err(UsbError::EndpointOverflow) + } else { + Ok(()) + } + } + Err(exception) => Err(exception), + } + } } impl UsbBus for UsbDevice {