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