Compare commits

..

No commits in common. "5ac1a07c6bc821eb574de533b2373bcefdcbfc8b" and "4a90b9a6c87eef24c249908196f72ba2cde2b2b0" have entirely different histories.

View File

@ -14,10 +14,6 @@ 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<const L: usize> UsbBus for UsbDevice<L> {
fn alloc_ep(
&mut self,
@ -28,9 +24,9 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
_interval: u8,
) -> UsbResult<EndpointAddress> {
// Handle first endpoint. //
free(|_cs| {
if ep_addr == Some(EndpointAddress::from_parts(0, UsbDirection::In)) {
ep_addr.ok_or(UsbError::InvalidState)
Ok(ep_addr.unwrap())
} else {
let address = match ep_addr {
// If current endpoint doesn't allocated, assign ep_addr to variable. //
@ -44,7 +40,8 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
.enumerate()
.skip(1)
.find(|(index, ep)| {
!ep.is_allocated && max_packet_size <= ENDPOINTS_ALLOC_LAYOUT[*index]
!ep.is_allocated
&& max_packet_size <= ENDPOINTS_ALLOC_LAYOUT[*index]
})
.ok_or(UsbError::EndpointOverflow)?
.0;
@ -80,6 +77,7 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
Ok(address)
}
}
})
}
fn enable(&mut self) {
@ -118,7 +116,7 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
// Endpoint configuration //
self.allocated_endpoints().for_each(|(i, _ep)| {
let _ = self.configure_endpoint(cs, i);
self.configure_endpoint(cs, i).unwrap();
});
// Set high speed and attach the USB. //
@ -134,15 +132,17 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
}
fn force_reset(&self) -> UsbResult<()> {
let set_detach = |bit| {
free(|cs| {
self.usb.borrow(cs).udcon.modify(|_, w| w.detach().bit(bit));
let usbcon = &self.usb.borrow(cs).usbcon;
usbcon.modify(|_, w| w.usbe().set_bit());
});
};
set_detach(true);
delay_cycles(ONE_MS_16_MGHZ);
set_detach(false);
free(|cs| {
let usbcon = &self.usb.borrow(cs).usbcon;
usbcon.modify(|_, w| w.usbe().set_bit());
});
Ok(())
}
@ -162,8 +162,7 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
if usbint.vbusti().bit_is_set() {
usb.usbint
.write(|w| unsafe { w.bits(USBINT_CLEAR) }.vbusti().clear_bit());
.write(|w| unsafe { w.bits(0x01) }.vbusti().clear_bit());
if usb.usbsta.read().vbus().bit_is_set() {
return PollResult::Resume;
} else {
@ -185,7 +184,7 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
if udint.sofi().bit_is_set() {
usb.udint
.write(|w| unsafe { w.bits(UDINT_CLEAR) }.sofi().clear_bit());
.write(|w| unsafe { w.bits(0x7d) }.sofi().clear_bit());
}
if usb.usbcon.read().frzclk().bit_is_clear() {
@ -227,8 +226,9 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
free(|cs| {
let usb = self.usb.borrow(cs);
self.select_endpoint(cs, ep_addr.index())?;
if let Err(error) = self.select_endpoint(cs, ep_addr.index()) {
Err(error)
} else {
let ep = &self.ep_table[ep_addr.index()];
if ep.ep_type == 0 {
@ -239,7 +239,6 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
}
let buf_size = self.get_size(cs);
if buf.len() < buf_size {
return Err(UsbError::BufferOverflow);
}
@ -249,7 +248,7 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
}
usb.ueintx.write(|w| {
unsafe { w.bits(RESTRICT_RW_FLAG) }
unsafe { w.bits(0xdf) }
.rxouti()
.clear_bit()
.rxstpi()
@ -261,12 +260,10 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
if usb.ueintx.read().rxouti().bit_is_clear() {
return Err(UsbError::WouldBlock);
}
usb.ueintx
.write(|w| unsafe { w.bits(RESTRICT_RW_FLAG) }.rxouti().clear_bit());
.write(|w| unsafe { w.bits(0xdf) }.rxouti().clear_bit());
let mut bytes_read = 0;
for slot in buf {
if usb.ueintx.read().rwal().bit_is_clear() {
break;
@ -280,10 +277,10 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
}
usb.ueintx
.write(|w| unsafe { w.bits(RESTRICT_RW_FLAG) }.fifocon().clear_bit());
.write(|w| unsafe { w.bits(0xdf) }.fifocon().clear_bit());
Ok(bytes_read)
}
}
})
}
@ -294,13 +291,13 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
usb.udint.modify(|_, w| w.eorsti().clear_bit());
self.allocated_endpoints().for_each(|(i, _)| {
let _ = self.configure_endpoint(cs, i);
self.configure_endpoint(cs, i).unwrap();
});
// Clear resume informations. //
usb.udint.write(|w| {
unsafe { w.bits(UDINT_CLEAR) }
unsafe { w.bits(0x7d) }
.wakeupi()
.clear_bit()
.suspi()
@ -328,7 +325,7 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
usb.usbcon.modify(|_, w| w.frzclk().clear_bit());
usb.udint.write(|w| {
unsafe { w.bits(UDINT_CLEAR) }
unsafe { w.bits(0x7d) }
.wakeupi()
.clear_bit()
.suspi()
@ -372,7 +369,7 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
let (usb, pll) = (self.usb.borrow(cs), self.pll.borrow(cs));
usb.udint.write(|w| {
unsafe { w.bits(UDINT_CLEAR) }
unsafe { w.bits(0x7d) }
.wakeupi()
.clear_bit()
.suspi()
@ -398,14 +395,14 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
free(|cs| {
let usb = self.usb.borrow(cs);
self.select_endpoint(cs, ep_addr.index())?;
if let Err(error) = self.select_endpoint(cs, ep_addr.index()) {
Err(error)
} else {
let ep = &self.ep_table[ep_addr.index()];
// Endpoint type confitions //
match ep.ep_type {
0 => {
if ep.ep_type == 0 {
if usb.ueintx.read().txini().bit_is_clear() {
return Err(UsbError::WouldBlock);
}
@ -419,15 +416,13 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
}
usb.ueintx
.write(|w| unsafe { w.bits(RESTRICT_RW_FLAG) }.txini().clear_bit());
}
_ => {
.write(|w| unsafe { w.bits(0xdf) }.txini().clear_bit());
} else {
if usb.ueintx.read().txini().bit_is_clear() {
return Err(UsbError::WouldBlock);
}
usb.ueintx.write(|w| {
unsafe { w.bits(RESTRICT_RW_FLAG) }
unsafe { w.bits(0xdf) }
.txini()
.clear_bit()
.rxouti()
@ -443,19 +438,19 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
}
usb.ueintx.write(|w| {
unsafe { w.bits(RESTRICT_RW_FLAG) }
unsafe { w.bits(0xdf) }
.rxouti()
.clear_bit()
.fifocon()
.clear_bit()
});
}
};
let pending_ins = self.pending_ins.borrow(cs);
pending_ins.set(pending_ins.get() | 1 << ep_addr.index());
Ok(buf.len())
}
})
}
}