11use embassy_usb_driver:: EndpointAllocError ;
22
3- pub ( crate ) struct EndpointBufferAllocator < ' d , const NR_EP : usize > {
4- ep_buffer : & ' d mut [ EndpointDataBuffer ; NR_EP ] ,
3+ pub ( crate ) struct EndpointBufferAllocator < ' d , const NR_EP : usize , const SIZE : usize > {
4+ ep_buffer : & ' d mut [ EndpointDataBuffer < SIZE > ; NR_EP ] ,
55 ep_next : usize ,
66}
77
8- impl < ' d , const NR_EP : usize > EndpointBufferAllocator < ' d , NR_EP > {
9- pub fn new ( ep_buffer : & ' d mut [ EndpointDataBuffer ; NR_EP ] ) -> Self {
8+ /// Compile-time validation for buffer sizes
9+ const fn validate_buffer_size ( size : usize ) -> bool {
10+ matches ! ( size, 8 | 16 | 32 | 64 | 512 )
11+ }
12+
13+ impl < ' 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" ) } ;
1016 Self { ep_buffer, ep_next : 0 }
1117 }
1218
1319 pub fn alloc_endpoint (
1420 & mut self ,
1521 max_packet_size : u16 ,
16- ) -> Result < EndpointData < ' d > , embassy_usb_driver:: EndpointAllocError > {
22+ ) -> Result < EndpointData < ' d , SIZE > , embassy_usb_driver:: EndpointAllocError > {
1723 if self . ep_next >= NR_EP {
1824 error ! ( "out of endpoint buffers" ) ;
1925 return Err ( EndpointAllocError ) ;
@@ -22,37 +28,39 @@ impl<'d, const NR_EP: usize> EndpointBufferAllocator<'d, NR_EP> {
2228 let ep_buf_idx = self . ep_next ;
2329 self . ep_next += 1 ;
2430
25- // TODO: Fix this, and allow variable buffer sizes
26- assert ! ( max_packet_size as usize <= ENDPOINT_DATA_BUFFER_SIZE ) ;
31+ assert ! (
32+ max_packet_size as usize <= SIZE ,
33+ "max_packet_size {} exceeds buffer size {}" ,
34+ max_packet_size,
35+ SIZE
36+ ) ;
2737
2838 Ok ( EndpointData {
2939 max_packet_size,
30- buffer : unsafe { core:: mem:: transmute ( & self . ep_buffer [ ep_buf_idx] as * const EndpointDataBuffer ) } ,
40+ buffer : unsafe { core:: mem:: transmute ( & self . ep_buffer [ ep_buf_idx] as * const EndpointDataBuffer < SIZE > ) } ,
3141 } )
3242 }
3343}
3444
35- pub struct EndpointData < ' d > {
45+ pub struct EndpointData < ' d , const SIZE : usize > {
3646 pub max_packet_size : u16 ,
37- pub buffer : & ' d mut EndpointDataBuffer ,
47+ pub buffer : & ' d mut EndpointDataBuffer < SIZE > ,
3848}
3949
40- impl < ' d > EndpointData < ' d > {
50+ impl < ' d , const SIZE : usize > EndpointData < ' d , SIZE > {
4151 pub fn addr ( & self ) -> usize {
4252 self . buffer . addr ( )
4353 }
4454}
4555
46- // todo generic
47- const ENDPOINT_DATA_BUFFER_SIZE : usize = 64 ;
48-
4956#[ repr( C , align( 4 ) ) ]
50- pub struct EndpointDataBuffer {
51- data : [ u8 ; ENDPOINT_DATA_BUFFER_SIZE as usize ] ,
57+ pub struct EndpointDataBuffer < const SIZE : usize > {
58+ data : [ u8 ; SIZE ] ,
5259}
5360
54- impl Default for EndpointDataBuffer {
61+ impl < const SIZE : usize > Default for EndpointDataBuffer < SIZE > {
5562 fn default ( ) -> Self {
63+ const { assert ! ( validate_buffer_size( SIZE ) , "Invalid buffer size" ) } ;
5664 unsafe {
5765 EndpointDataBuffer {
5866 data : core:: mem:: zeroed ( ) ,
@@ -61,7 +69,7 @@ impl Default for EndpointDataBuffer {
6169 }
6270}
6371
64- impl EndpointDataBuffer {
72+ impl < const SIZE : usize > EndpointDataBuffer < SIZE > {
6573 pub ( crate ) fn read_volatile ( & self , buf : & mut [ u8 ] ) {
6674 assert ! ( buf. len( ) <= self . data. len( ) ) ;
6775 let len = buf. len ( ) ;
@@ -85,6 +93,25 @@ impl EndpointDataBuffer {
8593 }
8694}
8795
96+ // Type aliases for common buffer sizes
97+ pub type EndpointDataBuffer8 = EndpointDataBuffer < 8 > ;
98+ pub type EndpointDataBuffer16 = EndpointDataBuffer < 16 > ;
99+ pub type EndpointDataBuffer32 = EndpointDataBuffer < 32 > ;
100+ pub type EndpointDataBuffer64 = EndpointDataBuffer < 64 > ;
101+ pub type EndpointDataBuffer512 = EndpointDataBuffer < 512 > ;
102+
103+ pub type EndpointData8 < ' d > = EndpointData < ' d , 8 > ;
104+ pub type EndpointData16 < ' d > = EndpointData < ' d , 16 > ;
105+ pub type EndpointData32 < ' d > = EndpointData < ' d , 32 > ;
106+ pub type EndpointData64 < ' d > = EndpointData < ' d , 64 > ;
107+ pub type EndpointData512 < ' d > = EndpointData < ' d , 512 > ;
108+
109+ pub type EndpointBufferAllocator8 < ' d , const NR_EP : usize > = EndpointBufferAllocator < ' d , NR_EP , 8 > ;
110+ pub type EndpointBufferAllocator16 < ' d , const NR_EP : usize > = EndpointBufferAllocator < ' d , NR_EP , 16 > ;
111+ pub type EndpointBufferAllocator32 < ' d , const NR_EP : usize > = EndpointBufferAllocator < ' d , NR_EP , 32 > ;
112+ pub type EndpointBufferAllocator64 < ' d , const NR_EP : usize > = EndpointBufferAllocator < ' d , NR_EP , 64 > ;
113+ pub type EndpointBufferAllocator512 < ' d , const NR_EP : usize > = EndpointBufferAllocator < ' d , NR_EP , 512 > ;
114+
88115/// USB Direction Trait
89116pub trait Dir { }
90117
0 commit comments