Compare commits

..

11 Commits

Author SHA1 Message Date
a800e28303 feat: make USBEndpoint public + add method get_ep_table 2025-06-30 21:08:09 +04:00
16e254d7dc feat: remove pending_ins field and it usage 2025-06-22 21:37:36 +04:00
c510d908d3 refactor: move constants + remove code duplicate 2025-05-18 23:51:41 +04:00
cfa76a6672 fix(int): USBINT interrupt 2025-05-18 23:20:14 +04:00
dba9637614 revert a269df21fe
revert fix(int): fix bytes for clearing interrupt flags for usbd_serial
2025-05-18 22:15:59 +03:00
dd28dfe61e test 2025-05-18 23:05:30 +04:00
a269df21fe fix(int): fix bytes for clearing interrupt flags for usbd_serial 2025-05-18 23:00:00 +04:00
93ade5bda0 fix: add clearing interrupts 2025-05-15 10:23:43 +04:00
64a6532c94 fix(f_reset): using detach bit in udcon register instead usbcon 2025-05-15 10:03:31 +04:00
6322868796 refactor: remove unwrap() usage, redundant free(||) usage and etc 2025-05-14 21:01:58 +04:00
f7c4b48b85 remove free-interrupt context for endpoint allocator 2025-05-05 23:09:54 +04:00
2 changed files with 138 additions and 138 deletions

View File

@@ -14,6 +14,10 @@ pub use types::*;
use types::{DPRAM_SIZE, ENDPOINTS_ALLOC_LAYOUT, ONE_MS_16_MGHZ}; 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> { impl<const L: usize> UsbBus for UsbDevice<L> {
fn alloc_ep( fn alloc_ep(
&mut self, &mut self,
@@ -24,9 +28,9 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
_interval: u8, _interval: u8,
) -> UsbResult<EndpointAddress> { ) -> UsbResult<EndpointAddress> {
// Handle first endpoint. // // Handle first endpoint. //
free(|_cs| {
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::InvalidState)
} 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. //
@@ -40,8 +44,7 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
.enumerate() .enumerate()
.skip(1) .skip(1)
.find(|(index, ep)| { .find(|(index, ep)| {
!ep.is_allocated !ep.is_allocated && max_packet_size <= ENDPOINTS_ALLOC_LAYOUT[*index]
&& max_packet_size <= ENDPOINTS_ALLOC_LAYOUT[*index]
}) })
.ok_or(UsbError::EndpointOverflow)? .ok_or(UsbError::EndpointOverflow)?
.0; .0;
@@ -77,7 +80,6 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
Ok(address) Ok(address)
} }
} }
})
} }
fn enable(&mut self) { fn enable(&mut self) {
@@ -116,7 +118,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. //
@@ -132,17 +134,15 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
} }
fn force_reset(&self) -> UsbResult<()> { fn force_reset(&self) -> UsbResult<()> {
let set_detach = |bit| {
free(|cs| { free(|cs| {
let usbcon = &self.usb.borrow(cs).usbcon; self.usb.borrow(cs).udcon.modify(|_, w| w.detach().bit(bit));
usbcon.modify(|_, w| w.usbe().set_bit());
}); });
};
set_detach(true);
delay_cycles(ONE_MS_16_MGHZ); 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(()) Ok(())
} }
@@ -162,7 +162,8 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
if usbint.vbusti().bit_is_set() { if usbint.vbusti().bit_is_set() {
usb.usbint usb.usbint
.write(|w| unsafe { w.bits(0x01) }.vbusti().clear_bit()); .write(|w| unsafe { w.bits(USBINT_CLEAR) }.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 {
@@ -184,12 +185,11 @@ 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| unsafe { w.bits(0x7d) }.sofi().clear_bit()); .write(|w| unsafe { w.bits(UDINT_CLEAR) }.sofi().clear_bit());
} }
if usb.usbcon.read().frzclk().bit_is_clear() { if usb.usbcon.read().frzclk().bit_is_clear() {
let (mut ep_out, mut ep_in_complete, mut ep_setup) = (0u16, 0u16, 0u16); let (mut ep_out, mut ep_in_complete, mut ep_setup) = (0u16, 0u16, 0u16);
let pending_ins = self.pending_ins.borrow(cs);
for (ep_index, _ep) in self.allocated_endpoints() { for (ep_index, _ep) in self.allocated_endpoints() {
if self.select_endpoint(cs, ep_index).is_err() { if self.select_endpoint(cs, ep_index).is_err() {
@@ -204,9 +204,8 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
ep_setup |= 1 << ep_index; ep_setup |= 1 << ep_index;
} }
if pending_ins.get() & (1 << ep_index) != 0 && ueintx.txini().bit_is_set() { if ueintx.txini().bit_is_set() {
ep_in_complete |= 1 << ep_index; ep_in_complete |= 1 << ep_index;
pending_ins.set(pending_ins.get() & !(1 << ep_index));
} }
} }
} }
@@ -226,9 +225,8 @@ 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()]; let ep = &self.ep_table[ep_addr.index()];
if ep.ep_type == 0 { if ep.ep_type == 0 {
@@ -239,6 +237,7 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
} }
let buf_size = self.get_size(cs); let buf_size = self.get_size(cs);
if buf.len() < buf_size { if buf.len() < buf_size {
return Err(UsbError::BufferOverflow); return Err(UsbError::BufferOverflow);
} }
@@ -248,7 +247,7 @@ 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()
.rxstpi() .rxstpi()
@@ -260,10 +259,12 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
if usb.ueintx.read().rxouti().bit_is_clear() { if usb.ueintx.read().rxouti().bit_is_clear() {
return Err(UsbError::WouldBlock); return Err(UsbError::WouldBlock);
} }
usb.ueintx usb.ueintx
.write(|w| unsafe { w.bits(0xdf) }.rxouti().clear_bit()); .write(|w| unsafe { w.bits(RESTRICT_RW_FLAG) }.rxouti().clear_bit());
let mut bytes_read = 0; let mut bytes_read = 0;
for slot in buf { for slot in buf {
if usb.ueintx.read().rwal().bit_is_clear() { if usb.ueintx.read().rwal().bit_is_clear() {
break; break;
@@ -277,10 +278,10 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
} }
usb.ueintx usb.ueintx
.write(|w| unsafe { w.bits(0xdf) }.fifocon().clear_bit()); .write(|w| unsafe { w.bits(RESTRICT_RW_FLAG) }.fifocon().clear_bit());
Ok(bytes_read) Ok(bytes_read)
} }
}
}) })
} }
@@ -291,13 +292,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.write(|w| {
unsafe { w.bits(0x7d) } unsafe { w.bits(UDINT_CLEAR) }
.wakeupi() .wakeupi()
.clear_bit() .clear_bit()
.suspi() .suspi()
@@ -325,7 +326,7 @@ 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.write(|w| {
unsafe { w.bits(0x7d) } unsafe { w.bits(UDINT_CLEAR) }
.wakeupi() .wakeupi()
.clear_bit() .clear_bit()
.suspi() .suspi()
@@ -369,7 +370,7 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
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.write(|w| {
unsafe { w.bits(0x7d) } unsafe { w.bits(UDINT_CLEAR) }
.wakeupi() .wakeupi()
.clear_bit() .clear_bit()
.suspi() .suspi()
@@ -395,14 +396,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()]; let ep = &self.ep_table[ep_addr.index()];
// Endpoint type confitions // // Endpoint type confitions //
if ep.ep_type == 0 { 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);
} }
@@ -416,13 +417,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()
@@ -438,19 +441,16 @@ 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);
pending_ins.set(pending_ins.get() | 1 << ep_addr.index());
Ok(buf.len()) Ok(buf.len())
}
}) })
} }
} }

View File

@@ -1,5 +1,3 @@
use core::cell::Cell;
use avr_device::{ use avr_device::{
atmega32u4::{PLL, USB_DEVICE}, atmega32u4::{PLL, USB_DEVICE},
interrupt::{CriticalSection, Mutex}, interrupt::{CriticalSection, Mutex},
@@ -8,7 +6,7 @@ use usb_device::{bus::UsbBusAllocator, endpoint::EndpointType, UsbDirection, Usb
#[allow(unused)] #[allow(unused)]
#[derive(Default, Copy, Clone)] #[derive(Default, Copy, Clone)]
pub(crate) struct USBEndpoint { pub struct USBEndpoint {
pub(crate) is_allocated: bool, pub(crate) is_allocated: bool,
pub(crate) size: u8, pub(crate) size: u8,
pub(crate) ep_type: u8, pub(crate) ep_type: u8,
@@ -72,7 +70,6 @@ pub struct UsbDevice<const L: usize> {
pub(crate) pll: Mutex<PLL>, pub(crate) pll: Mutex<PLL>,
pub(crate) usb: Mutex<USB_DEVICE>, pub(crate) usb: Mutex<USB_DEVICE>,
pub(crate) ep_table: [USBEndpoint; L], pub(crate) ep_table: [USBEndpoint; L],
pub(crate) pending_ins: Mutex<Cell<u8>>,
pub(crate) dpram_already_used: u16, pub(crate) dpram_already_used: u16,
} }
@@ -88,7 +85,6 @@ impl<const L: usize> UsbDevice<L> {
pll: Mutex::new(pll), pll: Mutex::new(pll),
usb: Mutex::new(usb), usb: Mutex::new(usb),
ep_table: [USBEndpoint::default(); L], ep_table: [USBEndpoint::default(); L],
pending_ins: Mutex::new(Cell::new(0u8)),
dpram_already_used: 0, dpram_already_used: 0,
}) })
} }
@@ -136,6 +132,10 @@ impl<const L: usize> UsbDevice<L> {
Ok(()) Ok(())
} }
pub fn get_ep_table(&self) -> &[USBEndpoint] {
&self.ep_table
}
pub(crate) fn configure_endpoint( pub(crate) fn configure_endpoint(
&self, &self,
cs: CriticalSection<'_>, cs: CriticalSection<'_>,