Compare commits
1 Commits
test
...
12104cdc0b
| Author | SHA1 | Date | |
|---|---|---|---|
| 12104cdc0b |
@@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "usb-avr"
|
||||
name = "usb_avr"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
@@ -17,5 +17,3 @@ features = ["atmega32u4"]
|
||||
# The latest releases of `proc-macro2` do not support the rust toolchain that
|
||||
# we use. Thus, we must fix this dependency to an older version where our
|
||||
# toolchain is still supported. See https://github.com/Rahix/avr-hal/issues/537
|
||||
# [build-dependencies.proc-macro2]
|
||||
# version = "=1.0.79"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
[toolchain]
|
||||
channel = "nightly-2024-06-13"
|
||||
components = [ "rust-src" ]
|
||||
profile = "default"
|
||||
profile = "complete"
|
||||
|
||||
262
src/lib.rs
262
src/lib.rs
@@ -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,58 +24,60 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||
_interval: u8,
|
||||
) -> UsbResult<EndpointAddress> {
|
||||
// Handle first endpoint. //
|
||||
|
||||
if ep_addr == Some(EndpointAddress::from_parts(0, UsbDirection::In)) {
|
||||
ep_addr.ok_or(UsbError::InvalidState)
|
||||
} 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. //
|
||||
_ => {
|
||||
let index = self
|
||||
.ep_table
|
||||
.iter()
|
||||
.enumerate()
|
||||
.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)
|
||||
}
|
||||
};
|
||||
|
||||
// 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)
|
||||
free(|_cs| {
|
||||
if ep_addr == Some(EndpointAddress::from_parts(0, UsbDirection::In)) {
|
||||
Ok(ep_addr.unwrap())
|
||||
} else {
|
||||
// Set endpoint parameters. //
|
||||
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,
|
||||
|
||||
target_endpoint.set_dir(ep_dir);
|
||||
target_endpoint.set_type(ep_type);
|
||||
target_endpoint.set_size(ep_size)?;
|
||||
// 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]
|
||||
})
|
||||
.ok_or(UsbError::EndpointOverflow)?
|
||||
.0;
|
||||
|
||||
// Add used dpram memory. //
|
||||
EndpointAddress::from_parts(index, ep_dir)
|
||||
}
|
||||
};
|
||||
|
||||
target_endpoint.is_allocated = true;
|
||||
self.dpram_already_used += ep_size;
|
||||
// Select endpoint info by address index. //
|
||||
|
||||
Ok(address)
|
||||
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) {
|
||||
@@ -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));
|
||||
});
|
||||
};
|
||||
free(|cs| {
|
||||
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,11 +184,12 @@ 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() {
|
||||
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() {
|
||||
if self.select_endpoint(cs, ep_index).is_err() {
|
||||
@@ -204,8 +204,9 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||
ep_setup |= 1 << ep_index;
|
||||
}
|
||||
|
||||
if ueintx.txini().bit_is_set() {
|
||||
if pending_ins.get() & (1 << ep_index) != 0 && ueintx.txini().bit_is_set() {
|
||||
ep_in_complete |= 1 << ep_index;
|
||||
pending_ins.set(pending_ins.get() & !(1 << ep_index));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -225,62 +226,60 @@ impl<const L: usize> UsbBus for UsbDevice<L> {
|
||||
free(|cs| {
|
||||
let usb = self.usb.borrow(cs);
|
||||
|
||||
self.select_endpoint(cs, ep_addr.index())?;
|
||||
|
||||
let ep = &self.ep_table[ep_addr.index()];
|
||||
|
||||
if ep.ep_type == 0 {
|
||||
let ueintx = usb.ueintx.read();
|
||||
|
||||
if ueintx.rxouti().bit_is_clear() && ueintx.rxstpi().bit_is_clear() {
|
||||
return Err(UsbError::WouldBlock);
|
||||
}
|
||||
|
||||
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)
|
||||
if let Err(error) = self.select_endpoint(cs, ep_addr.index()) {
|
||||
Err(error)
|
||||
} else {
|
||||
if usb.ueintx.read().rxouti().bit_is_clear() {
|
||||
return Err(UsbError::WouldBlock);
|
||||
}
|
||||
let ep = &self.ep_table[ep_addr.index()];
|
||||
|
||||
usb.ueintx
|
||||
.write(|w| unsafe { w.bits(RESTRICT_RW_FLAG) }.rxouti().clear_bit());
|
||||
if ep.ep_type == 0 {
|
||||
let ueintx = usb.ueintx.read();
|
||||
|
||||
let mut bytes_read = 0;
|
||||
|
||||
for slot in buf {
|
||||
if usb.ueintx.read().rwal().bit_is_clear() {
|
||||
break;
|
||||
if ueintx.rxouti().bit_is_clear() && ueintx.rxstpi().bit_is_clear() {
|
||||
return Err(UsbError::WouldBlock);
|
||||
}
|
||||
*slot = usb.uedatx.read().bits();
|
||||
bytes_read += 1;
|
||||
|
||||
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(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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -292,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()
|
||||
@@ -326,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()
|
||||
@@ -370,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()
|
||||
@@ -396,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()];
|
||||
|
||||
let ep = &self.ep_table[ep_addr.index()];
|
||||
// Endpoint type confitions //
|
||||
|
||||
// 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);
|
||||
}
|
||||
@@ -417,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()
|
||||
@@ -441,16 +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()
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Ok(buf.len())
|
||||
let pending_ins = self.pending_ins.borrow(cs);
|
||||
pending_ins.set(pending_ins.get() | 1 << ep_addr.index());
|
||||
|
||||
Ok(buf.len())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use core::cell::Cell;
|
||||
|
||||
use avr_device::{
|
||||
atmega32u4::{PLL, USB_DEVICE},
|
||||
interrupt::{CriticalSection, Mutex},
|
||||
@@ -6,7 +8,7 @@ use usb_device::{bus::UsbBusAllocator, endpoint::EndpointType, UsbDirection, Usb
|
||||
|
||||
#[allow(unused)]
|
||||
#[derive(Default, Copy, Clone)]
|
||||
pub struct USBEndpoint {
|
||||
pub(crate) struct USBEndpoint {
|
||||
pub(crate) is_allocated: bool,
|
||||
pub(crate) size: u8,
|
||||
pub(crate) ep_type: u8,
|
||||
@@ -70,6 +72,7 @@ pub struct UsbDevice<const L: usize> {
|
||||
pub(crate) pll: Mutex<PLL>,
|
||||
pub(crate) usb: Mutex<USB_DEVICE>,
|
||||
pub(crate) ep_table: [USBEndpoint; L],
|
||||
pub(crate) pending_ins: Mutex<Cell<u8>>,
|
||||
pub(crate) dpram_already_used: u16,
|
||||
}
|
||||
|
||||
@@ -79,27 +82,25 @@ pub(crate) const ENDPOINTS_ALLOC_LAYOUT: [u16; MAX_ENDPOINTS] = [64, 256, 64, 64
|
||||
pub(crate) const ONE_MS_16_MGHZ: u32 = 16000;
|
||||
|
||||
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]
|
||||
pub fn new(pll: PLL, usb: USB_DEVICE) -> UsbBusAllocator<Self> {
|
||||
UsbBusAllocator::new(Self {
|
||||
pll: Mutex::new(pll),
|
||||
usb: Mutex::new(usb),
|
||||
ep_table: [USBEndpoint::default(); L],
|
||||
pending_ins: Mutex::new(Cell::new(0u8)),
|
||||
dpram_already_used: 0,
|
||||
})
|
||||
}
|
||||
|
||||
#[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)]
|
||||
#[inline]
|
||||
pub(crate) fn allocated_endpoints(&self) -> impl Iterator<Item = (usize, &USBEndpoint)> {
|
||||
self.ep_table
|
||||
.iter()
|
||||
@@ -132,10 +133,6 @@ impl<const L: usize> UsbDevice<L> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_ep_table(&self) -> &[USBEndpoint] {
|
||||
&self.ep_table
|
||||
}
|
||||
|
||||
pub(crate) fn configure_endpoint(
|
||||
&self,
|
||||
cs: CriticalSection<'_>,
|
||||
@@ -181,11 +178,4 @@ impl<const L: usize> UsbDevice<L> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user