diff --git a/src/lib.rs b/src/lib.rs index 79ba68e..298e76b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,10 @@ pub use types::*; use types::{DPRAM_SIZE, ENDPOINTS_ALLOC_LAYOUT, ONE_MS_16_MGHZ}; +const RESTRICT_RW_FLAG: u8 = !(1 << 5); +const USBINT_CLEAR: u8 = 1 << 0; +const UDINT_CLEAR: u8 = !(1 << 7 | 1 << 1); + impl UsbBus for UsbDevice { fn alloc_ep( &mut self, @@ -26,7 +30,7 @@ impl UsbBus for UsbDevice { // Handle first endpoint. // if ep_addr == Some(EndpointAddress::from_parts(0, UsbDirection::In)) { - ep_addr.ok_or(UsbError::WouldBlock) + ep_addr.ok_or(UsbError::InvalidState) } else { let address = match ep_addr { // If current endpoint doesn't allocated, assign ep_addr to variable. // @@ -130,19 +134,15 @@ impl UsbBus for UsbDevice { } fn force_reset(&self) -> UsbResult<()> { - free(|cs| { - let udcon = &self.usb.borrow(cs).udcon; - - udcon.modify(|_, w| w.detach().set_bit()); - }); + let set_detach = |bit| { + free(|cs| { + self.usb.borrow(cs).udcon.modify(|_, w| w.detach().bit(bit)); + }); + }; + set_detach(true); delay_cycles(ONE_MS_16_MGHZ); - - free(|cs| { - let udcon = &self.usb.borrow(cs).udcon; - - udcon.modify(|_, w| w.detach().clear_bit()); - }); + set_detach(false); Ok(()) } @@ -459,7 +459,3 @@ impl UsbBus for UsbDevice { }) } } - -const RESTRICT_RW_FLAG: u8 = !(1 << 5); -const USBINT_CLEAR: u8 = 1 << 0; -const UDINT_CLEAR: u8 = !(1 << 7 | 1 << 1);