15
15
16
16
use core:: ops:: Deref ;
17
17
use e310x:: { uart0, Uart0 , Uart1 } ;
18
- use embedded_hal_nb:: serial:: { ErrorKind , ErrorType , Read , Write } ;
19
- use nb;
18
+ use embedded_hal_nb:: serial;
20
19
21
20
use crate :: { clock:: Clocks , time:: Bps } ;
22
21
@@ -73,12 +72,17 @@ impl<UART, PIN> Rx<UART, PIN> {
73
72
}
74
73
}
75
74
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 ;
78
77
}
79
78
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 > {
82
86
let rxdata = self . uart . rxdata ( ) . read ( ) ;
83
87
84
88
if rxdata. empty ( ) . bit_is_set ( ) {
@@ -89,6 +93,28 @@ impl<UART: UartX, PIN: RxPin<UART>> Read for Rx<UART, PIN> {
89
93
}
90
94
}
91
95
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
+
92
118
/// Serial transmitter half
93
119
pub struct Tx < UART , PIN > {
94
120
uart : UART ,
@@ -102,23 +128,34 @@ impl<UART, PIN> Tx<UART, PIN> {
102
128
}
103
129
}
104
130
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
+ }
107
136
}
108
137
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
+ }
112
141
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 )
115
151
} else {
116
152
self . uart . txdata ( ) . write ( |w| unsafe { w. data ( ) . bits ( byte) } ) ;
117
153
Ok ( ( ) )
118
154
}
119
155
}
120
156
121
- fn flush ( & mut self ) -> nb:: Result < ( ) , ErrorKind > {
157
+ #[ inline]
158
+ fn flush ( & mut self ) -> nb:: Result < ( ) , Self :: Error > {
122
159
if self . uart . ip ( ) . read ( ) . txwm ( ) . bit_is_set ( ) {
123
160
// FIFO count is below the receive watermark (1)
124
161
Ok ( ( ) )
@@ -128,6 +165,38 @@ impl<UART: UartX, PIN: TxPin<UART>> Write for Tx<UART, PIN> {
128
165
}
129
166
}
130
167
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
+
131
200
/// Serial abstraction
132
201
pub struct Serial < UART , TX , RX > {
133
202
uart : UART ,
@@ -186,22 +255,48 @@ impl<UART: UartX, TX: TxPin<UART>, RX: RxPin<UART>> Serial<UART, TX, RX> {
186
255
}
187
256
}
188
257
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 ;
191
260
}
192
261
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 > {
195
264
self . rx . read ( )
196
265
}
197
266
}
198
267
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 > {
201
270
self . tx . write ( byte)
202
271
}
203
272
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 > {
205
300
self . tx . flush ( )
206
301
}
207
302
}
0 commit comments