1use embassy_usb_driver::EndpointAllocError;
2
3#[allow(dead_code)]
4pub(crate) struct EndpointBufferAllocator<'d, const NR_EP: usize, const SIZE: usize> {
5 free: &'d mut [EndpointDataBuffer<SIZE>],
8}
9
10const fn validate_buffer_size(size: usize) -> bool {
12 matches!(size, 8 | 16 | 32 | 64 | 512)
13}
14
15#[allow(dead_code)]
16impl<'d, const NR_EP: usize, const SIZE: usize> EndpointBufferAllocator<'d, NR_EP, SIZE> {
17 pub fn new(ep_buffer: &'d mut [EndpointDataBuffer<SIZE>; NR_EP]) -> Self {
18 const { assert!(validate_buffer_size(SIZE), "Invalid buffer size") };
19 Self { free: ep_buffer }
20 }
21
22 pub fn alloc_endpoint(
23 &mut self,
24 max_packet_size: u16,
25 ) -> Result<EndpointData<'d, SIZE>, embassy_usb_driver::EndpointAllocError> {
26 let Some((buffer, rest)) = core::mem::take(&mut self.free).split_first_mut() else {
27 error!("out of endpoint buffers");
28 return Err(EndpointAllocError);
29 };
30 self.free = rest;
31
32 assert!(
33 max_packet_size as usize <= SIZE,
34 "max_packet_size {} exceeds buffer size {}",
35 max_packet_size,
36 SIZE
37 );
38
39 Ok(EndpointData {
40 max_packet_size,
41 buffer,
42 })
43 }
44}
45
46pub struct EndpointData<'d, const SIZE: usize> {
47 pub max_packet_size: u16,
48 pub buffer: &'d mut EndpointDataBuffer<SIZE>,
49}
50
51impl<'d, const SIZE: usize> EndpointData<'d, SIZE> {
52 pub fn addr(&self) -> usize {
53 self.buffer.addr()
54 }
55}
56
57#[repr(C, align(4))]
58pub struct EndpointDataBuffer<const SIZE: usize> {
59 data: [u8; SIZE],
60}
61
62impl<const SIZE: usize> Default for EndpointDataBuffer<SIZE> {
63 fn default() -> Self {
64 const { assert!(validate_buffer_size(SIZE), "Invalid buffer size") };
65 unsafe {
66 EndpointDataBuffer {
67 data: core::mem::zeroed(),
68 }
69 }
70 }
71}
72
73#[allow(dead_code)]
74impl<const SIZE: usize> EndpointDataBuffer<SIZE> {
75 pub(crate) fn read_volatile(&self, buf: &mut [u8]) {
76 assert!(buf.len() <= self.data.len());
77 let len = buf.len();
78
79 for i in 0..len {
80 buf[i] = unsafe { core::ptr::read_volatile(&self.data[i]) };
81 }
82 }
83
84 pub(crate) fn write_volatile(&mut self, buf: &[u8]) {
85 assert!(buf.len() <= self.data.len());
86 let len = buf.len();
87
88 for i in 0..len {
89 unsafe { core::ptr::write_volatile(&mut self.data[i], buf[i]) };
90 }
91 }
92
93 pub(crate) fn addr(&self) -> usize {
94 self.data.as_ptr() as usize
95 }
96
97 pub(crate) fn slice_as_mut_ptr<const NR_EP: usize>(bufs: &mut [Self; NR_EP]) -> (*mut u8, usize) {
99 const {
100 assert!(validate_buffer_size(SIZE), "Invalid buffer size");
101 assert!(
102 core::mem::size_of::<[Self; NR_EP]>() == NR_EP * SIZE,
103 "EndpointDataBuffer array must have no padding"
104 );
105 };
106 (bufs.as_mut_ptr().cast::<u8>(), NR_EP * SIZE)
107 }
108}
109
110pub type EndpointDataBuffer8 = EndpointDataBuffer<8>;
112pub type EndpointDataBuffer16 = EndpointDataBuffer<16>;
113pub type EndpointDataBuffer32 = EndpointDataBuffer<32>;
114pub type EndpointDataBuffer64 = EndpointDataBuffer<64>;
115pub type EndpointDataBuffer512 = EndpointDataBuffer<512>;
116
117pub type EndpointData8<'d> = EndpointData<'d, 8>;
118pub type EndpointData16<'d> = EndpointData<'d, 16>;
119pub type EndpointData32<'d> = EndpointData<'d, 32>;
120pub type EndpointData64<'d> = EndpointData<'d, 64>;
121pub type EndpointData512<'d> = EndpointData<'d, 512>;
122
123pub type EndpointBufferAllocator8<'d, const NR_EP: usize> = EndpointBufferAllocator<'d, NR_EP, 8>;
124pub type EndpointBufferAllocator16<'d, const NR_EP: usize> = EndpointBufferAllocator<'d, NR_EP, 16>;
125pub type EndpointBufferAllocator32<'d, const NR_EP: usize> = EndpointBufferAllocator<'d, NR_EP, 32>;
126pub type EndpointBufferAllocator64<'d, const NR_EP: usize> = EndpointBufferAllocator<'d, NR_EP, 64>;
127pub type EndpointBufferAllocator512<'d, const NR_EP: usize> = EndpointBufferAllocator<'d, NR_EP, 512>;
128
129pub trait Dir {}
131
132pub struct In;
134impl Dir for In {}
135
136pub struct Out;
138impl Dir for Out {}
139
140pub struct InOut;
142impl Dir for InOut {}