Skip to content

Commit 018642f

Browse files
committed
Remove backing buffers
1 parent 056a0b0 commit 018642f

File tree

1 file changed

+5
-53
lines changed

1 file changed

+5
-53
lines changed

mod.rs

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
pac::USBD,
1414
};
1515
use core::sync::atomic::{compiler_fence, Ordering};
16-
use core::{cell::Cell, mem, ptr};
16+
use core::cell::Cell;
1717
use core::mem::MaybeUninit;
1818
use cortex_m::interrupt::{self, Mutex};
1919
use usb_device::{
@@ -31,10 +31,6 @@ fn dma_end() {
3131
}
3232

3333
struct Buffers {
34-
// Ptr and size is stored separately to save space
35-
in_bufs: [*mut u8; 9],
36-
out_bufs: [*mut u8; 9],
37-
3834
// Buffers can be up to 64 Bytes since this is a Full-Speed implementation.
3935
in_lens: [u8; 9],
4036
out_lens: [u8; 9],
@@ -43,8 +39,6 @@ struct Buffers {
4339
impl Buffers {
4440
fn new() -> Self {
4541
Self {
46-
in_bufs: [ptr::null_mut(); 9],
47-
out_bufs: [ptr::null_mut(); 9],
4842
in_lens: [0; 9],
4943
out_lens: [0; 9],
5044
}
@@ -72,7 +66,6 @@ pub struct Usbd<'c> {
7266
periph: Mutex<USBD>,
7367
// argument passed to `UsbDeviceBuilder.max_packet_size_0`
7468
max_packet_size_0: u16,
75-
unalloc_buffers: &'static mut [u8],
7669
bufs: Buffers,
7770
used_in: u8,
7871
used_out: u8,
@@ -90,19 +83,14 @@ impl<'c> Usbd<'c> {
9083
/// # Parameters
9184
///
9285
/// * `periph`: The raw USBD peripheral.
93-
/// * `endpoint_buffers`: Backing storage for the endpoint buffers. This
94-
/// needs to be big enough to accomodate all buffers of all endpoints, or
95-
/// `alloc_ep` will fail.
9686
#[inline]
97-
pub fn new_alloc<L, LSTAT>(
87+
pub fn new<L, LSTAT>(
9888
periph: USBD,
99-
endpoint_buffers: &'static mut [u8],
10089
_clocks: &'c Clocks<ExternalOscillator, L, LSTAT>,
10190
) -> UsbBusAllocator<Self> {
10291
UsbBusAllocator::new(Self {
10392
periph: Mutex::new(periph),
10493
max_packet_size_0: 0,
105-
unalloc_buffers: endpoint_buffers,
10694
bufs: Buffers::new(),
10795
used_in: 0,
10896
used_out: 0,
@@ -123,36 +111,6 @@ impl<'c> Usbd<'c> {
123111
unsafe { &*USBD::ptr() }.usbaddr.read().addr().bits()
124112
}
125113

126-
fn alloc_ep_buf(
127-
&mut self,
128-
ep_type: EndpointType,
129-
mut size: u16,
130-
) -> usb_device::Result<&'static mut [u8]> {
131-
if self.unalloc_buffers.len() < usize::from(size) {
132-
Err(UsbError::EndpointMemoryOverflow)
133-
} else {
134-
if ep_type == EndpointType::Bulk || ep_type == EndpointType::Interrupt {
135-
// datasheet: buffer must be 4-byte aligned and its size must be a multiple of 4
136-
let rem = self.unalloc_buffers.as_mut_ptr() as usize % 4;
137-
if rem != 0 {
138-
let (_padding, remaining) =
139-
mem::replace(&mut self.unalloc_buffers, &mut []).split_at_mut(4 - rem);
140-
self.unalloc_buffers = remaining;
141-
}
142-
143-
let rem = size % 4;
144-
if rem != 0 {
145-
size = size + 4 - rem;
146-
}
147-
}
148-
assert!(size <= 64);
149-
let (alloc, remaining) =
150-
mem::replace(&mut self.unalloc_buffers, &mut []).split_at_mut(size.into());
151-
self.unalloc_buffers = remaining;
152-
Ok(alloc)
153-
}
154-
}
155-
156114
fn is_used(&self, ep: EndpointAddress) -> bool {
157115
if ep.index() == 8 {
158116
// ISO
@@ -234,8 +192,6 @@ impl UsbBus for Usbd<'_> {
234192
self.max_packet_size_0 = max_packet_size;
235193
}
236194

237-
let buf = self.alloc_ep_buf(ep_type, max_packet_size)?;
238-
239195
if false {
240196
unimplemented!(
241197
"alloc_ep({:?}, {:?}, {:?}, {}, {})",
@@ -247,15 +203,13 @@ impl UsbBus for Usbd<'_> {
247203
);
248204
}
249205

250-
let (used, bufs, lens) = match ep_dir {
206+
let (used, lens) = match ep_dir {
251207
UsbDirection::In => (
252208
&mut self.used_in,
253-
&mut self.bufs.in_bufs,
254209
&mut self.bufs.in_lens,
255210
),
256211
UsbDirection::Out => (
257212
&mut self.used_out,
258-
&mut self.bufs.out_bufs,
259213
&mut self.bufs.out_lens,
260214
),
261215
};
@@ -271,8 +225,7 @@ impl UsbBus for Usbd<'_> {
271225
return Err(UsbError::EndpointOverflow);
272226
} else {
273227
*flag = true;
274-
bufs[8] = buf.as_mut_ptr();
275-
lens[8] = buf.len() as u8;
228+
lens[8] = max_packet_size as u8;
276229
return Ok(EndpointAddress::from_parts(0x08, ep_dir));
277230
}
278231
}
@@ -297,8 +250,7 @@ impl UsbBus for Usbd<'_> {
297250
}
298251

299252
*used |= 1 << alloc_index;
300-
bufs[alloc_index as usize] = buf.as_mut_ptr();
301-
lens[alloc_index as usize] = buf.len() as u8;
253+
lens[alloc_index as usize] = max_packet_size as u8;
302254

303255
let addr = EndpointAddress::from_parts(alloc_index as usize, ep_dir);
304256
Ok(addr)

0 commit comments

Comments
 (0)