11use crate :: endpoint:: NUM_ENDPOINTS ;
2- use crate :: UsbPeripheral ;
2+ use crate :: { UsbPeripheral , Word } ;
33use core:: marker:: PhantomData ;
44use core:: slice;
55use usb_device:: { Result , UsbError } ;
66use vcell:: VolatileCell ;
77
8- pub struct EndpointBuffer < USB > {
9- mem : & ' static mut [ VolatileCell < u16 > ] ,
8+ pub struct EndpointBuffer < USB : UsbPeripheral > {
9+ mem : & ' static mut [ VolatileCell < USB :: Word > ] ,
1010 marker : PhantomData < USB > ,
1111}
1212
1313impl < USB : UsbPeripheral > EndpointBuffer < USB > {
1414 pub fn new ( offset_bytes : usize , size_bytes : usize ) -> Self {
15- let ep_mem_ptr = USB :: EP_MEMORY as * mut VolatileCell < u16 > ;
15+ let ep_mem_ptr = USB :: EP_MEMORY as * mut VolatileCell < _ > ;
1616
1717 let offset_words = offset_bytes >> 1 ;
1818 let count_words = size_bytes >> 1 ;
@@ -36,7 +36,7 @@ impl<USB: UsbPeripheral> EndpointBuffer<USB> {
3636 }
3737
3838 #[ inline( always) ]
39- fn read_word ( & self , index : usize ) -> u16 {
39+ fn read_word ( & self , index : usize ) -> USB :: Word {
4040 if USB :: EP_MEMORY_ACCESS_2X16 {
4141 self . mem [ index] . get ( )
4242 } else {
@@ -45,46 +45,32 @@ impl<USB: UsbPeripheral> EndpointBuffer<USB> {
4545 }
4646
4747 #[ inline( always) ]
48- fn write_word ( & self , index : usize , value : u16 ) {
48+ fn write_word ( & self , index : usize , value : USB :: Word ) {
4949 if USB :: EP_MEMORY_ACCESS_2X16 {
5050 self . mem [ index] . set ( value) ;
5151 } else {
5252 self . mem [ index * 2 ] . set ( value) ;
5353 }
5454 }
5555
56- pub fn read ( & self , mut buf : & mut [ u8 ] ) {
56+ pub fn read ( & self , buf : & mut [ u8 ] ) {
5757 let mut index = 0 ;
58+ let mut writer = buf. into_iter ( ) . peekable ( ) ;
5859
59- while buf. len ( ) >= 2 {
60- let word = self . read_word ( index) ;
61-
62- buf[ 0 ] = ( word & 0xff ) as u8 ;
63- buf[ 1 ] = ( word >> 8 ) as u8 ;
64-
60+ while writer. peek ( ) . is_some ( ) {
61+ self . read_word ( index) . write_to_iter_le ( & mut writer) ;
6562 index += 1 ;
66-
67- buf = & mut buf[ 2 ..] ;
68- }
69-
70- if buf. len ( ) > 0 {
71- buf[ 0 ] = ( self . read_word ( index) & 0xff ) as u8 ;
7263 }
7364 }
7465
75- pub fn write ( & self , mut buf : & [ u8 ] ) {
66+ pub fn write ( & self , buf : & [ u8 ] ) {
7667 let mut index = 0 ;
68+ let mut reader = buf. into_iter ( ) . peekable ( ) ;
7769
78- while buf . len ( ) >= 2 {
79- let value: u16 = buf [ 0 ] as u16 | ( ( buf [ 1 ] as u16 ) << 8 ) ;
70+ while reader . peek ( ) . is_some ( ) {
71+ let value = Word :: from_iter_le ( & mut reader ) ;
8072 self . write_word ( index, value) ;
8173 index += 1 ;
82-
83- buf = & buf[ 2 ..] ;
84- }
85-
86- if buf. len ( ) > 0 {
87- self . write_word ( index, buf[ 0 ] as u16 ) ;
8874 }
8975 }
9076
@@ -106,36 +92,28 @@ impl<USB: UsbPeripheral> EndpointBuffer<USB> {
10692}
10793
10894#[ repr( C ) ]
109- pub struct BufferDescriptor < USB > {
110- ptr : * const VolatileCell < u16 > ,
95+ pub struct BufferDescriptor < USB : UsbPeripheral > {
96+ ptr : * const VolatileCell < USB :: Word > ,
11197 marker : PhantomData < USB > ,
11298}
11399
114100impl < USB : UsbPeripheral > BufferDescriptor < USB > {
115101 #[ inline( always) ]
116- fn field ( & self , index : usize ) -> & ' static VolatileCell < u16 > {
102+ fn field ( & self , index : usize ) -> & ' static VolatileCell < USB :: Word > {
117103 let mul = if USB :: EP_MEMORY_ACCESS_2X16 { 1 } else { 2 } ;
118104 unsafe { & * ( self . ptr . add ( index * mul) ) }
119105 }
120106
121107 #[ inline( always) ]
122- pub fn addr_tx ( & self ) -> & ' static VolatileCell < u16 > {
123- self . field ( 0 )
124- }
125-
126- #[ inline( always) ]
127- pub fn count_tx ( & self ) -> & ' static VolatileCell < u16 > {
128- self . field ( 1 )
129- }
130-
131- #[ inline( always) ]
132- pub fn addr_rx ( & self ) -> & ' static VolatileCell < u16 > {
133- self . field ( 2 )
108+ pub fn set_tx ( & self , address : u16 , count : u16 ) {
109+ self . field ( 0 ) // addr
110+ self. field ( 1 ) // count (msb in 32bit)
134111 }
135112
136113 #[ inline( always) ]
137- pub fn count_rx ( & self ) -> & ' static VolatileCell < u16 > {
138- self . field ( 3 )
114+ pub fn get_rx ( & self ) -> ( u16 , u16 ) {
115+ self . field ( 2 ) // addr
116+ self. field ( 3 ) // count(msb in 32bit)
139117 }
140118}
141119
@@ -170,7 +148,7 @@ impl<USB: UsbPeripheral> EndpointMemoryAllocator<USB> {
170148 let mul = if USB :: EP_MEMORY_ACCESS_2X16 { 1 } else { 2 } ;
171149
172150 unsafe {
173- let ptr = ( USB :: EP_MEMORY as * const VolatileCell < u16 > ) . add ( ( index as usize ) * 4 * mul) ;
151+ let ptr = ( USB :: EP_MEMORY as * const VolatileCell < USB :: Word > ) . add ( ( index as usize ) * 4 * mul) ;
174152 BufferDescriptor {
175153 ptr,
176154 marker : Default :: default ( ) ,
0 commit comments