refactor: remove unwrap() usage, redundant free(||) usage and etc
This commit is contained in:
parent
f7c4b48b85
commit
6322868796
183
src/lib.rs
183
src/lib.rs
|
@ -24,8 +24,9 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||||
_interval: u8,
|
_interval: u8,
|
||||||
) -> UsbResult<EndpointAddress> {
|
) -> UsbResult<EndpointAddress> {
|
||||||
// Handle first endpoint. //
|
// Handle first endpoint. //
|
||||||
|
|
||||||
if ep_addr == Some(EndpointAddress::from_parts(0, UsbDirection::In)) {
|
if ep_addr == Some(EndpointAddress::from_parts(0, UsbDirection::In)) {
|
||||||
Ok(ep_addr.unwrap())
|
ep_addr.ok_or(UsbError::WouldBlock)
|
||||||
} else {
|
} else {
|
||||||
let address = match ep_addr {
|
let address = match ep_addr {
|
||||||
// If current endpoint doesn't allocated, assign ep_addr to variable. //
|
// If current endpoint doesn't allocated, assign ep_addr to variable. //
|
||||||
|
@ -113,7 +114,7 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||||
// Endpoint configuration //
|
// Endpoint configuration //
|
||||||
|
|
||||||
self.allocated_endpoints().for_each(|(i, _ep)| {
|
self.allocated_endpoints().for_each(|(i, _ep)| {
|
||||||
self.configure_endpoint(cs, i).unwrap();
|
let _ = self.configure_endpoint(cs, i);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set high speed and attach the USB. //
|
// Set high speed and attach the USB. //
|
||||||
|
@ -131,13 +132,11 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||||
fn force_reset(&self) -> UsbResult<()> {
|
fn force_reset(&self) -> UsbResult<()> {
|
||||||
free(|cs| {
|
free(|cs| {
|
||||||
let usbcon = &self.usb.borrow(cs).usbcon;
|
let usbcon = &self.usb.borrow(cs).usbcon;
|
||||||
usbcon.modify(|_, w| w.usbe().set_bit());
|
|
||||||
});
|
|
||||||
|
|
||||||
delay_cycles(ONE_MS_16_MGHZ);
|
usbcon.modify(|_, w| w.usbe().clear_bit());
|
||||||
|
|
||||||
|
delay_cycles(ONE_MS_16_MGHZ);
|
||||||
|
|
||||||
free(|cs| {
|
|
||||||
let usbcon = &self.usb.borrow(cs).usbcon;
|
|
||||||
usbcon.modify(|_, w| w.usbe().set_bit());
|
usbcon.modify(|_, w| w.usbe().set_bit());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -158,8 +157,8 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||||
let (udint, udien, usbint) = (usb.udint.read(), usb.udien.read(), usb.usbint.read());
|
let (udint, udien, usbint) = (usb.udint.read(), usb.udien.read(), usb.usbint.read());
|
||||||
|
|
||||||
if usbint.vbusti().bit_is_set() {
|
if usbint.vbusti().bit_is_set() {
|
||||||
usb.usbint
|
usb.usbint.write(|w| w.vbusti().clear_bit());
|
||||||
.write(|w| unsafe { w.bits(0x01) }.vbusti().clear_bit());
|
|
||||||
if usb.usbsta.read().vbus().bit_is_set() {
|
if usb.usbsta.read().vbus().bit_is_set() {
|
||||||
return PollResult::Resume;
|
return PollResult::Resume;
|
||||||
} else {
|
} else {
|
||||||
|
@ -180,8 +179,7 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if udint.sofi().bit_is_set() {
|
if udint.sofi().bit_is_set() {
|
||||||
usb.udint
|
usb.udint.write(|w| w.sofi().clear_bit());
|
||||||
.write(|w| unsafe { w.bits(0x7d) }.sofi().clear_bit());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if usb.usbcon.read().frzclk().bit_is_clear() {
|
if usb.usbcon.read().frzclk().bit_is_clear() {
|
||||||
|
@ -223,60 +221,62 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||||
free(|cs| {
|
free(|cs| {
|
||||||
let usb = self.usb.borrow(cs);
|
let usb = self.usb.borrow(cs);
|
||||||
|
|
||||||
if let Err(error) = self.select_endpoint(cs, ep_addr.index()) {
|
self.select_endpoint(cs, ep_addr.index())?;
|
||||||
Err(error)
|
|
||||||
} else {
|
|
||||||
let ep = &self.ep_table[ep_addr.index()];
|
|
||||||
|
|
||||||
if ep.ep_type == 0 {
|
let ep = &self.ep_table[ep_addr.index()];
|
||||||
let ueintx = usb.ueintx.read();
|
|
||||||
|
|
||||||
if ueintx.rxouti().bit_is_clear() && ueintx.rxstpi().bit_is_clear() {
|
if ep.ep_type == 0 {
|
||||||
return Err(UsbError::WouldBlock);
|
let ueintx = usb.ueintx.read();
|
||||||
}
|
|
||||||
|
|
||||||
let buf_size = self.get_size(cs);
|
if ueintx.rxouti().bit_is_clear() && ueintx.rxstpi().bit_is_clear() {
|
||||||
if buf.len() < buf_size {
|
return Err(UsbError::WouldBlock);
|
||||||
return Err(UsbError::BufferOverflow);
|
|
||||||
}
|
|
||||||
|
|
||||||
for byte in &mut buf[..buf_size] {
|
|
||||||
*byte = usb.uedatx.read().bits();
|
|
||||||
}
|
|
||||||
|
|
||||||
usb.ueintx.write(|w| {
|
|
||||||
unsafe { w.bits(0xdf) }
|
|
||||||
.rxouti()
|
|
||||||
.clear_bit()
|
|
||||||
.rxstpi()
|
|
||||||
.clear_bit()
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(buf_size)
|
|
||||||
} else {
|
|
||||||
if usb.ueintx.read().rxouti().bit_is_clear() {
|
|
||||||
return Err(UsbError::WouldBlock);
|
|
||||||
}
|
|
||||||
usb.ueintx
|
|
||||||
.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;
|
|
||||||
}
|
|
||||||
*slot = usb.uedatx.read().bits();
|
|
||||||
bytes_read += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if usb.ueintx.read().rwal().bit_is_set() {
|
|
||||||
return Err(UsbError::BufferOverflow);
|
|
||||||
}
|
|
||||||
|
|
||||||
usb.ueintx
|
|
||||||
.write(|w| unsafe { w.bits(0xdf) }.fifocon().clear_bit());
|
|
||||||
Ok(bytes_read)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let buf_size = self.get_size(cs);
|
||||||
|
|
||||||
|
if buf.len() < buf_size {
|
||||||
|
return Err(UsbError::BufferOverflow);
|
||||||
|
}
|
||||||
|
|
||||||
|
for byte in &mut buf[..buf_size] {
|
||||||
|
*byte = usb.uedatx.read().bits();
|
||||||
|
}
|
||||||
|
|
||||||
|
usb.ueintx.write(|w| {
|
||||||
|
unsafe { w.bits(RESTRICT_RW_FLAG) }
|
||||||
|
.rxouti()
|
||||||
|
.clear_bit()
|
||||||
|
.rxstpi()
|
||||||
|
.clear_bit()
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(buf_size)
|
||||||
|
} else {
|
||||||
|
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());
|
||||||
|
|
||||||
|
let mut bytes_read = 0;
|
||||||
|
|
||||||
|
for slot in buf {
|
||||||
|
if usb.ueintx.read().rwal().bit_is_clear() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
*slot = usb.uedatx.read().bits();
|
||||||
|
bytes_read += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if usb.ueintx.read().rwal().bit_is_set() {
|
||||||
|
return Err(UsbError::BufferOverflow);
|
||||||
|
}
|
||||||
|
|
||||||
|
usb.ueintx
|
||||||
|
.write(|w| unsafe { w.bits(RESTRICT_RW_FLAG) }.fifocon().clear_bit());
|
||||||
|
|
||||||
|
Ok(bytes_read)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -288,18 +288,13 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||||
usb.udint.modify(|_, w| w.eorsti().clear_bit());
|
usb.udint.modify(|_, w| w.eorsti().clear_bit());
|
||||||
|
|
||||||
self.allocated_endpoints().for_each(|(i, _)| {
|
self.allocated_endpoints().for_each(|(i, _)| {
|
||||||
self.configure_endpoint(cs, i).unwrap();
|
let _ = self.configure_endpoint(cs, i);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Clear resume informations. //
|
// Clear resume informations. //
|
||||||
|
|
||||||
usb.udint.write(|w| {
|
usb.udint
|
||||||
unsafe { w.bits(0x7d) }
|
.write(|w| w.wakeupi().clear_bit().suspi().clear_bit());
|
||||||
.wakeupi()
|
|
||||||
.clear_bit()
|
|
||||||
.suspi()
|
|
||||||
.clear_bit()
|
|
||||||
});
|
|
||||||
|
|
||||||
usb.udien
|
usb.udien
|
||||||
.modify(|_, w| w.wakeupe().clear_bit().suspe().set_bit());
|
.modify(|_, w| w.wakeupe().clear_bit().suspe().set_bit());
|
||||||
|
@ -321,13 +316,8 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||||
|
|
||||||
usb.usbcon.modify(|_, w| w.frzclk().clear_bit());
|
usb.usbcon.modify(|_, w| w.frzclk().clear_bit());
|
||||||
|
|
||||||
usb.udint.write(|w| {
|
usb.udint
|
||||||
unsafe { w.bits(0x7d) }
|
.write(|w| w.wakeupi().clear_bit().suspi().clear_bit());
|
||||||
.wakeupi()
|
|
||||||
.clear_bit()
|
|
||||||
.suspi()
|
|
||||||
.clear_bit()
|
|
||||||
});
|
|
||||||
|
|
||||||
usb.udien
|
usb.udien
|
||||||
.modify(|_, w| w.wakeupe().clear_bit().suspe().set_bit());
|
.modify(|_, w| w.wakeupe().clear_bit().suspe().set_bit());
|
||||||
|
@ -365,13 +355,8 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||||
free(|cs| {
|
free(|cs| {
|
||||||
let (usb, pll) = (self.usb.borrow(cs), self.pll.borrow(cs));
|
let (usb, pll) = (self.usb.borrow(cs), self.pll.borrow(cs));
|
||||||
|
|
||||||
usb.udint.write(|w| {
|
usb.udint
|
||||||
unsafe { w.bits(0x7d) }
|
.write(|w| w.wakeupi().clear_bit().suspi().clear_bit());
|
||||||
.wakeupi()
|
|
||||||
.clear_bit()
|
|
||||||
.suspi()
|
|
||||||
.clear_bit()
|
|
||||||
});
|
|
||||||
|
|
||||||
// Suspend. //
|
// Suspend. //
|
||||||
|
|
||||||
|
@ -392,14 +377,14 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||||
free(|cs| {
|
free(|cs| {
|
||||||
let usb = self.usb.borrow(cs);
|
let usb = self.usb.borrow(cs);
|
||||||
|
|
||||||
if let Err(error) = self.select_endpoint(cs, ep_addr.index()) {
|
self.select_endpoint(cs, ep_addr.index())?;
|
||||||
Err(error)
|
|
||||||
} else {
|
|
||||||
let ep = &self.ep_table[ep_addr.index()];
|
|
||||||
|
|
||||||
// Endpoint type confitions //
|
let ep = &self.ep_table[ep_addr.index()];
|
||||||
|
|
||||||
if ep.ep_type == 0 {
|
// Endpoint type confitions //
|
||||||
|
|
||||||
|
match ep.ep_type {
|
||||||
|
0 => {
|
||||||
if usb.ueintx.read().txini().bit_is_clear() {
|
if usb.ueintx.read().txini().bit_is_clear() {
|
||||||
return Err(UsbError::WouldBlock);
|
return Err(UsbError::WouldBlock);
|
||||||
}
|
}
|
||||||
|
@ -413,13 +398,15 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||||
}
|
}
|
||||||
|
|
||||||
usb.ueintx
|
usb.ueintx
|
||||||
.write(|w| unsafe { w.bits(0xdf) }.txini().clear_bit());
|
.write(|w| unsafe { w.bits(RESTRICT_RW_FLAG) }.txini().clear_bit());
|
||||||
} else {
|
}
|
||||||
|
_ => {
|
||||||
if usb.ueintx.read().txini().bit_is_clear() {
|
if usb.ueintx.read().txini().bit_is_clear() {
|
||||||
return Err(UsbError::WouldBlock);
|
return Err(UsbError::WouldBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
usb.ueintx.write(|w| {
|
usb.ueintx.write(|w| {
|
||||||
unsafe { w.bits(0xdf) }
|
unsafe { w.bits(RESTRICT_RW_FLAG) }
|
||||||
.txini()
|
.txini()
|
||||||
.clear_bit()
|
.clear_bit()
|
||||||
.rxouti()
|
.rxouti()
|
||||||
|
@ -435,19 +422,21 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||||
}
|
}
|
||||||
|
|
||||||
usb.ueintx.write(|w| {
|
usb.ueintx.write(|w| {
|
||||||
unsafe { w.bits(0xdf) }
|
unsafe { w.bits(RESTRICT_RW_FLAG) }
|
||||||
.rxouti()
|
.rxouti()
|
||||||
.clear_bit()
|
.clear_bit()
|
||||||
.fifocon()
|
.fifocon()
|
||||||
.clear_bit()
|
.clear_bit()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let pending_ins = self.pending_ins.borrow(cs);
|
let pending_ins = self.pending_ins.borrow(cs);
|
||||||
pending_ins.set(pending_ins.get() | 1 << ep_addr.index());
|
pending_ins.set(pending_ins.get() | 1 << ep_addr.index());
|
||||||
|
|
||||||
Ok(buf.len())
|
Ok(buf.len())
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const RESTRICT_RW_FLAG: u8 = !(1 << 5);
|
||||||
|
|
Loading…
Reference in New Issue