1515
1616use core:: ops:: Deref ;
1717use e310x:: { uart0, Uart0 , Uart1 } ;
18- use embedded_hal_nb:: serial:: { ErrorKind , ErrorType , Read , Write } ;
19- use nb;
18+ use embedded_hal_nb:: serial;
2019
2120use crate :: { clock:: Clocks , time:: Bps } ;
2221
@@ -73,12 +72,17 @@ impl<UART, PIN> Rx<UART, PIN> {
7372 }
7473}
7574
76- impl < UART : UartX , PIN : RxPin < UART > > ErrorType for Rx < UART , PIN > {
77- type Error = ErrorKind ;
75+ impl < UART : UartX , PIN : RxPin < UART > > serial :: ErrorType for Rx < UART , PIN > {
76+ type Error = serial :: ErrorKind ;
7877}
7978
80- impl < UART : UartX , PIN : RxPin < UART > > Read for Rx < UART , PIN > {
81- fn read ( & mut self ) -> nb:: Result < u8 , ErrorKind > {
79+ impl < UART : UartX , PIN : RxPin < UART > > embedded_io:: ErrorType for Rx < UART , PIN > {
80+ type Error = embedded_io:: ErrorKind ;
81+ }
82+
83+ impl < UART : UartX , PIN : RxPin < UART > > serial:: Read for Rx < UART , PIN > {
84+ #[ inline]
85+ fn read ( & mut self ) -> nb:: Result < u8 , Self :: Error > {
8286 let rxdata = self . uart . rxdata ( ) . read ( ) ;
8387
8488 if rxdata. empty ( ) . bit_is_set ( ) {
@@ -89,6 +93,28 @@ impl<UART: UartX, PIN: RxPin<UART>> Read for Rx<UART, PIN> {
8993 }
9094}
9195
96+ impl < UART : UartX , PIN : RxPin < UART > > embedded_io:: Read for Rx < UART , PIN > {
97+ #[ inline]
98+ fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
99+ if buf. is_empty ( ) {
100+ return Ok ( 0 ) ;
101+ }
102+ buf[ 0 ] = nb:: block!( serial:: Read :: read( self ) ) . unwrap ( ) ; // first byte may block
103+ let mut count = 1 ;
104+ for byte in buf. iter_mut ( ) . skip ( 1 ) {
105+ match serial:: Read :: read ( self ) {
106+ Ok ( b) => {
107+ * byte = b;
108+ count += 1
109+ }
110+ Err ( nb:: Error :: WouldBlock ) => break ,
111+ _ => unreachable ! ( ) ,
112+ }
113+ }
114+ Ok ( count)
115+ }
116+ }
117+
92118/// Serial transmitter half
93119pub struct Tx < UART , PIN > {
94120 uart : UART ,
@@ -102,23 +128,34 @@ impl<UART, PIN> Tx<UART, PIN> {
102128 }
103129}
104130
105- impl < UART : UartX , PIN : TxPin < UART > > ErrorType for Tx < UART , PIN > {
106- type Error = ErrorKind ;
131+ impl < UART : UartX , PIN : TxPin < UART > > Tx < UART , PIN > {
132+ /// Returns true if the transmit buffer is full
133+ fn is_buffer_full ( & self ) -> bool {
134+ self . uart . txdata ( ) . read ( ) . full ( ) . bit_is_set ( )
135+ }
107136}
108137
109- impl < UART : UartX , PIN : TxPin < UART > > Write for Tx < UART , PIN > {
110- fn write ( & mut self , byte : u8 ) -> nb :: Result < ( ) , ErrorKind > {
111- let txdata = self . uart . txdata ( ) . read ( ) ;
138+ impl < UART : UartX , PIN : TxPin < UART > > serial :: ErrorType for Tx < UART , PIN > {
139+ type Error = serial :: ErrorKind ;
140+ }
112141
113- if txdata. full ( ) . bit_is_set ( ) {
114- Err ( :: nb:: Error :: WouldBlock )
142+ impl < UART : UartX , PIN : TxPin < UART > > embedded_io:: ErrorType for Tx < UART , PIN > {
143+ type Error = embedded_io:: ErrorKind ;
144+ }
145+
146+ impl < UART : UartX , PIN : TxPin < UART > > serial:: Write for Tx < UART , PIN > {
147+ #[ inline]
148+ fn write ( & mut self , byte : u8 ) -> nb:: Result < ( ) , Self :: Error > {
149+ if self . is_buffer_full ( ) {
150+ Err ( nb:: Error :: WouldBlock )
115151 } else {
116152 self . uart . txdata ( ) . write ( |w| unsafe { w. data ( ) . bits ( byte) } ) ;
117153 Ok ( ( ) )
118154 }
119155 }
120156
121- fn flush ( & mut self ) -> nb:: Result < ( ) , ErrorKind > {
157+ #[ inline]
158+ fn flush ( & mut self ) -> nb:: Result < ( ) , Self :: Error > {
122159 if self . uart . ip ( ) . read ( ) . txwm ( ) . bit_is_set ( ) {
123160 // FIFO count is below the receive watermark (1)
124161 Ok ( ( ) )
@@ -128,6 +165,38 @@ impl<UART: UartX, PIN: TxPin<UART>> Write for Tx<UART, PIN> {
128165 }
129166}
130167
168+ impl < UART : UartX , PIN : TxPin < UART > > embedded_io:: WriteReady for Tx < UART , PIN > {
169+ #[ inline]
170+ fn write_ready ( & mut self ) -> Result < bool , Self :: Error > {
171+ Ok ( !self . is_buffer_full ( ) )
172+ }
173+ }
174+
175+ impl < UART : UartX , PIN : TxPin < UART > > embedded_io:: Write for Tx < UART , PIN > {
176+ #[ inline]
177+ fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize , Self :: Error > {
178+ if buf. is_empty ( ) {
179+ return Ok ( 0 ) ;
180+ }
181+ nb:: block!( serial:: Write :: write( self , buf[ 0 ] ) ) . unwrap ( ) ; // first byte may block
182+ let mut count = 1 ;
183+ for byte in buf. iter ( ) . skip ( 1 ) {
184+ match serial:: Write :: write ( self , * byte) {
185+ Ok ( ( ) ) => count += 1 ,
186+ Err ( nb:: Error :: WouldBlock ) => break ,
187+ _ => unreachable ! ( ) ,
188+ }
189+ }
190+ Ok ( count)
191+ }
192+
193+ #[ inline]
194+ fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
195+ nb:: block!( serial:: Write :: flush( self ) ) . unwrap ( ) ;
196+ Ok ( ( ) )
197+ }
198+ }
199+
131200/// Serial abstraction
132201pub struct Serial < UART , TX , RX > {
133202 uart : UART ,
@@ -186,22 +255,48 @@ impl<UART: UartX, TX: TxPin<UART>, RX: RxPin<UART>> Serial<UART, TX, RX> {
186255 }
187256}
188257
189- impl < UART , TX , RX > ErrorType for Serial < UART , TX , RX > {
190- type Error = ErrorKind ;
258+ impl < UART : UartX , TX , RX > serial :: ErrorType for Serial < UART , TX , RX > {
259+ type Error = serial :: ErrorKind ;
191260}
192261
193- impl < UART : UartX , TX , RX : RxPin < UART > > Read for Serial < UART , TX , RX > {
194- fn read ( & mut self ) -> nb:: Result < u8 , ErrorKind > {
262+ impl < UART : UartX , TX , RX : RxPin < UART > > serial :: Read for Serial < UART , TX , RX > {
263+ fn read ( & mut self ) -> nb:: Result < u8 , Self :: Error > {
195264 self . rx . read ( )
196265 }
197266}
198267
199- impl < UART : UartX , TX : TxPin < UART > , RX > Write for Serial < UART , TX , RX > {
200- fn write ( & mut self , byte : u8 ) -> nb:: Result < ( ) , ErrorKind > {
268+ impl < UART : UartX , TX : TxPin < UART > , RX > serial :: Write for Serial < UART , TX , RX > {
269+ fn write ( & mut self , byte : u8 ) -> nb:: Result < ( ) , Self :: Error > {
201270 self . tx . write ( byte)
202271 }
203272
204- fn flush ( & mut self ) -> nb:: Result < ( ) , ErrorKind > {
273+ fn flush ( & mut self ) -> nb:: Result < ( ) , Self :: Error > {
274+ self . tx . flush ( )
275+ }
276+ }
277+
278+ impl < UART , TX , RX > embedded_io:: ErrorType for Serial < UART , TX , RX > {
279+ type Error = embedded_io:: ErrorKind ;
280+ }
281+
282+ impl < UART : UartX , TX , RX : RxPin < UART > > embedded_io:: Read for Serial < UART , TX , RX > {
283+ fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
284+ self . rx . read ( buf)
285+ }
286+ }
287+
288+ impl < UART : UartX , TX : TxPin < UART > , RX > embedded_io:: WriteReady for Serial < UART , TX , RX > {
289+ fn write_ready ( & mut self ) -> Result < bool , Self :: Error > {
290+ self . tx . write_ready ( )
291+ }
292+ }
293+
294+ impl < UART : UartX , TX : TxPin < UART > , RX > embedded_io:: Write for Serial < UART , TX , RX > {
295+ fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize , Self :: Error > {
296+ self . tx . write ( buf)
297+ }
298+
299+ fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
205300 self . tx . flush ( )
206301 }
207302}
0 commit comments