Compare commits

...

24 Commits

Author SHA1 Message Date
ae466a8aad fix: remove redundant length check 2025-05-27 21:48:37 +04:00
21fcb661f1 fix: use .skip() instead slicing [1..] 2025-05-27 21:47:53 +04:00
29e5edde8a feat(check): add check for endpoint table length 2025-05-26 10:24:51 +04:00
137fcc78e5 refactor: reduce Iterator methods usage 2025-05-26 10:24:19 +04:00
5ac1a07c6b Merge pull request 'Merge first (maybe) stable version usb-avr for atmega32u4/atmega16u4' (#1) from test into main
Reviewed-on: https://gitea.doryan04.ru/TheEmbeddedRust/usb-avr-lib/pulls/1
2025-05-18 22:52:52 +03: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
4a90b9a6c8 feat(usb_device): add getters 2025-04-28 20:23:02 +04:00
ce10b959e4 Update src/types/usb_device.rs 2025-04-28 18:51:49 +03:00
e1711038c5 revert f399278d2c
revert feat: rename method
2025-04-28 18:51:16 +03:00
29b3bff247 revert 995bfd0169
revert feat: separate constructor for UsbDevice and UsbBusAllocator
2025-04-28 18:50:45 +03:00
79d01f8571 revert c4686aafc4
revert feat(api): add plug_in_detection
2025-04-28 18:49:26 +03:00
fb6f180ce2 feat: rename crate 2025-04-25 16:25:03 +04:00
995bfd0169 feat: separate constructor for UsbDevice and UsbBusAllocator 2025-04-25 15:40:55 +04:00
f399278d2c feat: rename method 2025-04-25 15:28:29 +04:00
c4686aafc4 feat(api): add plug_in_detection 2025-04-25 14:35:30 +04:00
ba319eecff refactor: move function down + set #[inline(always)] for small functions 2025-04-25 14:35:11 +04:00
3 changed files with 153 additions and 138 deletions

View File

@@ -1,5 +1,5 @@
[package] [package]
name = "usb_avr" name = "usb-avr"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

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,60 +28,58 @@ 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)) {
Ok(ep_addr.unwrap())
} else {
let address = match ep_addr {
// If current endpoint doesn't allocated, assign ep_addr to variable. //
Some(addr) if !self.ep_table[addr.index()].is_allocated => addr,
// If ep_aadr not provided, or current endpoint is allocated, try to find next free endpoint, otherwise return UsbError. // if ep_addr == Some(EndpointAddress::from_parts(0, UsbDirection::In)) {
_ => { ep_addr.ok_or(UsbError::InvalidState)
let index = self } else {
.ep_table let address = match ep_addr {
.iter() // If current endpoint doesn't allocated, assign ep_addr to variable. //
.enumerate() Some(addr) if !self.ep_table[addr.index()].is_allocated => addr,
.skip(1)
.find(|(index, ep)| {
!ep.is_allocated
&& max_packet_size <= ENDPOINTS_ALLOC_LAYOUT[*index]
})
.ok_or(UsbError::EndpointOverflow)?
.0;
EndpointAddress::from_parts(index, ep_dir) // If ep_aadr not provided, or current endpoint is allocated, try to find next free endpoint, otherwise return UsbError. //
} _ => {
}; let index = self
.ep_table
.iter()
.enumerate()
.skip(1)
.find(|(index, ep)| {
!ep.is_allocated && max_packet_size <= ENDPOINTS_ALLOC_LAYOUT[*index]
})
.map(|(index, _)| index)
.ok_or(UsbError::EndpointOverflow)?;
// Select endpoint info by address index. // EndpointAddress::from_parts(index, ep_dir)
let target_endpoint = &mut self.ep_table[address.index()];
// Get power of two number of endpoint size. //
let ep_size = max(8, max_packet_size.next_power_of_two());
// Endpoint allocation marker. //
if DPRAM_SIZE - self.dpram_already_used < ep_size {
Err(UsbError::EndpointMemoryOverflow)
} else {
// Set endpoint parameters. //
target_endpoint.set_dir(ep_dir);
target_endpoint.set_type(ep_type);
target_endpoint.set_size(ep_size)?;
// Add used dpram memory. //
target_endpoint.is_allocated = true;
self.dpram_already_used += ep_size;
Ok(address)
} }
};
// Select endpoint info by address index. //
let target_endpoint = &mut self.ep_table[address.index()];
// Get power of two number of endpoint size. //
let ep_size = max(8, max_packet_size.next_power_of_two());
// Endpoint allocation marker. //
if DPRAM_SIZE - self.dpram_already_used < ep_size {
Err(UsbError::EndpointMemoryOverflow)
} else {
// Set endpoint parameters. //
target_endpoint.set_dir(ep_dir);
target_endpoint.set_type(ep_type);
target_endpoint.set_size(ep_size)?;
// Add used dpram memory. //
target_endpoint.is_allocated = true;
self.dpram_already_used += ep_size;
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<()> {
free(|cs| { let set_detach = |bit| {
let usbcon = &self.usb.borrow(cs).usbcon; free(|cs| {
usbcon.modify(|_, w| w.usbe().set_bit()); self.usb.borrow(cs).udcon.modify(|_, w| w.detach().bit(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,7 +185,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| 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() {
@@ -226,60 +227,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)
} }
}) })
} }
@@ -291,13 +294,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 +328,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 +372,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 +398,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);
} }
@@ -416,13 +419,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 +443,19 @@ 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())
}
}) })
} }
} }

View File

@@ -82,13 +82,6 @@ pub(crate) const ENDPOINTS_ALLOC_LAYOUT: [u16; MAX_ENDPOINTS] = [64, 256, 64, 64
pub(crate) const ONE_MS_16_MGHZ: u32 = 16000; pub(crate) const ONE_MS_16_MGHZ: u32 = 16000;
impl<const L: usize> UsbDevice<L> { impl<const L: usize> UsbDevice<L> {
#[inline]
pub(crate) fn get_size(&self, cs: CriticalSection<'_>) -> usize {
let usb = self.usb.borrow(cs);
(((usb.uebchx.read().bits() as u16) << 8) | (usb.uebclx.read().bits() as u16)).into()
}
#[inline] #[inline]
pub fn new(pll: PLL, usb: USB_DEVICE) -> UsbBusAllocator<Self> { pub fn new(pll: PLL, usb: USB_DEVICE) -> UsbBusAllocator<Self> {
UsbBusAllocator::new(Self { UsbBusAllocator::new(Self {
@@ -100,7 +93,17 @@ impl<const L: usize> UsbDevice<L> {
}) })
} }
#[inline] #[inline(always)]
pub fn get_usb_device<'u>(&'u self, cs: CriticalSection<'u>) -> &'u USB_DEVICE {
self.usb.borrow(cs)
}
#[inline(always)]
pub fn get_pll<'u>(&'u self, cs: CriticalSection<'u>) -> &'u PLL {
self.pll.borrow(cs)
}
#[inline(always)]
pub(crate) fn allocated_endpoints(&self) -> impl Iterator<Item = (usize, &USBEndpoint)> { pub(crate) fn allocated_endpoints(&self) -> impl Iterator<Item = (usize, &USBEndpoint)> {
self.ep_table self.ep_table
.iter() .iter()
@@ -178,4 +181,11 @@ impl<const L: usize> UsbDevice<L> {
Err(exception) => Err(exception), Err(exception) => Err(exception),
} }
} }
#[inline(always)]
pub(crate) fn get_size(&self, cs: CriticalSection<'_>) -> usize {
let usb = self.usb.borrow(cs);
(((usb.uebchx.read().bits() as u16) << 8) | (usb.uebclx.read().bits() as u16)).into()
}
} }