@@ -190,7 +190,6 @@ pub struct Serial<USART, PINS> {
190
190
}
191
191
192
192
pub struct ErasedSerial < USART > {
193
- usart : USART ,
194
193
tx : Tx < USART > ,
195
194
rx : Rx < USART > ,
196
195
}
@@ -209,7 +208,7 @@ pub struct Rx<USART> {
209
208
210
209
/// Serial transmitter
211
210
pub struct Tx < USART > {
212
- _usart : PhantomData < USART > ,
211
+ usart : USART ,
213
212
}
214
213
215
214
impl < USART > Rx < USART > {
@@ -218,13 +217,21 @@ impl<USART> Rx<USART> {
218
217
_usart : PhantomData ,
219
218
}
220
219
}
220
+
221
+ /// Reunite the two halves of a split serial
222
+ pub fn reunite ( self , tx : Tx < USART > ) -> ErasedSerial < USART > {
223
+ ErasedSerial { rx : self , tx }
224
+ }
221
225
}
222
226
223
227
impl < USART > Tx < USART > {
224
- fn new ( ) -> Self {
225
- Self {
226
- _usart : PhantomData ,
227
- }
228
+ fn new ( usart : USART ) -> Self {
229
+ Self { usart }
230
+ }
231
+
232
+ /// Reunite the two halves of a split serial
233
+ pub fn reunite ( self , rx : Rx < USART > ) -> ErasedSerial < USART > {
234
+ ErasedSerial { tx : self , rx }
228
235
}
229
236
}
230
237
@@ -244,7 +251,8 @@ where
244
251
// UE: enable USART
245
252
// RE: enable receiver
246
253
// TE: enable transceiver
247
- self . usart
254
+ self . tx
255
+ . usart
248
256
. cr1
249
257
. modify ( |_r, w| w. ue ( ) . set_bit ( ) . re ( ) . set_bit ( ) . te ( ) . set_bit ( ) ) ;
250
258
@@ -255,7 +263,7 @@ where
255
263
// Configure baud rate
256
264
let brr = USART :: get_frequency ( & clocks) . 0 / config. baudrate . 0 ;
257
265
assert ! ( brr >= 16 , "impossible baud rate" ) ;
258
- self . usart . brr . write ( |w| unsafe { w. bits ( brr) } ) ;
266
+ self . tx . usart . brr . write ( |w| unsafe { w. bits ( brr) } ) ;
259
267
260
268
// Configure parity and word length
261
269
// Unlike most uart devices, the "word length" of this usart device refers to
@@ -267,7 +275,7 @@ where
267
275
Parity :: ParityEven => ( true , true , false ) ,
268
276
Parity :: ParityOdd => ( true , true , true ) ,
269
277
} ;
270
- self . usart . cr1 . modify ( |_r, w| {
278
+ self . tx . usart . cr1 . modify ( |_r, w| {
271
279
w. m ( )
272
280
. bit ( word_length)
273
281
. ps ( )
@@ -283,7 +291,7 @@ where
283
291
StopBits :: STOP2 => 0b10 ,
284
292
StopBits :: STOP1P5 => 0b11 ,
285
293
} ;
286
- self . usart . cr2 . modify ( |_r, w| w. stop ( ) . bits ( stop_bits) ) ;
294
+ self . tx . usart . cr2 . modify ( |_r, w| w. stop ( ) . bits ( stop_bits) ) ;
287
295
}
288
296
289
297
/// Reconfigure the USART instance.
@@ -295,7 +303,7 @@ where
295
303
config : impl Into < Config > ,
296
304
clocks : Clocks ,
297
305
) -> nb:: Result < ( ) , Infallible > {
298
- let sr = self . usart . sr . read ( ) ;
306
+ let sr = self . tx . usart . sr . read ( ) ;
299
307
// if we're currently busy transmitting, we have to wait until that is
300
308
// over -- regarding reception, we assume that the caller -- with
301
309
// exclusive access to the Serial instance due to &mut self -- knows
@@ -312,9 +320,9 @@ where
312
320
/// register empty (TXE)_ interrupt
313
321
pub fn listen ( & mut self , event : Event ) {
314
322
match event {
315
- Event :: Rxne => self . usart . cr1 . modify ( |_, w| w. rxneie ( ) . set_bit ( ) ) ,
316
- Event :: Txe => self . usart . cr1 . modify ( |_, w| w. txeie ( ) . set_bit ( ) ) ,
317
- Event :: Idle => self . usart . cr1 . modify ( |_, w| w. idleie ( ) . set_bit ( ) ) ,
323
+ Event :: Rxne => self . tx . usart . cr1 . modify ( |_, w| w. rxneie ( ) . set_bit ( ) ) ,
324
+ Event :: Txe => self . tx . usart . cr1 . modify ( |_, w| w. txeie ( ) . set_bit ( ) ) ,
325
+ Event :: Idle => self . tx . usart . cr1 . modify ( |_, w| w. idleie ( ) . set_bit ( ) ) ,
318
326
}
319
327
}
320
328
@@ -323,25 +331,25 @@ where
323
331
/// register empty (TXE)_ interrupt
324
332
pub fn unlisten ( & mut self , event : Event ) {
325
333
match event {
326
- Event :: Rxne => self . usart . cr1 . modify ( |_, w| w. rxneie ( ) . clear_bit ( ) ) ,
327
- Event :: Txe => self . usart . cr1 . modify ( |_, w| w. txeie ( ) . clear_bit ( ) ) ,
328
- Event :: Idle => self . usart . cr1 . modify ( |_, w| w. idleie ( ) . clear_bit ( ) ) ,
334
+ Event :: Rxne => self . tx . usart . cr1 . modify ( |_, w| w. rxneie ( ) . clear_bit ( ) ) ,
335
+ Event :: Txe => self . tx . usart . cr1 . modify ( |_, w| w. txeie ( ) . clear_bit ( ) ) ,
336
+ Event :: Idle => self . tx . usart . cr1 . modify ( |_, w| w. idleie ( ) . clear_bit ( ) ) ,
329
337
}
330
338
}
331
339
332
340
/// Returns true if the line idle status is set
333
341
pub fn is_idle ( & self ) -> bool {
334
- self . usart . sr . read ( ) . idle ( ) . bit_is_set ( )
342
+ self . tx . usart . sr . read ( ) . idle ( ) . bit_is_set ( )
335
343
}
336
344
337
345
/// Returns true if the tx register is empty (and can accept data)
338
346
pub fn is_tx_empty ( & self ) -> bool {
339
- self . usart . sr . read ( ) . txe ( ) . bit_is_set ( )
347
+ self . tx . usart . sr . read ( ) . txe ( ) . bit_is_set ( )
340
348
}
341
349
342
350
/// Returns true if the rx register is not empty (and can be read)
343
351
pub fn is_rx_not_empty ( & self ) -> bool {
344
- self . usart . sr . read ( ) . rxne ( ) . bit_is_set ( )
352
+ self . tx . usart . sr . read ( ) . rxne ( ) . bit_is_set ( )
345
353
}
346
354
347
355
/// Clear idle line interrupt flag
@@ -365,7 +373,7 @@ where
365
373
{
366
374
/// Returns ownership of the borrowed register handles
367
375
pub fn release ( self ) -> ( USART , PINS ) {
368
- ( self . inner . usart , self . pins )
376
+ ( self . inner . tx . usart , self . pins )
369
377
}
370
378
371
379
/// Erase the pins used for the Serial from the type
@@ -385,7 +393,11 @@ where
385
393
/// If a transmission is currently in progress, this returns
386
394
/// [`nb::Error::WouldBlock`].
387
395
#[ inline( always) ]
388
- pub fn reconfigure ( & mut self , config : impl Into < Config > , clocks : Clocks ) -> nb:: Result < ( ) , ( ) > {
396
+ pub fn reconfigure (
397
+ & mut self ,
398
+ config : impl Into < Config > ,
399
+ clocks : Clocks ,
400
+ ) -> nb:: Result < ( ) , Infallible > {
389
401
self . inner . reconfigure ( config, clocks)
390
402
}
391
403
@@ -476,7 +488,7 @@ macro_rules! hal {
476
488
PINS : Pins <$USARTX>,
477
489
{
478
490
#[ allow( unused_unsafe) ]
479
- Serial { pins, inner: ErasedSerial { usart , tx: Tx :: new( ) , rx: Rx :: new( ) } . init( config. into( ) , clocks, || {
491
+ Serial { pins, inner: ErasedSerial { tx: Tx :: new( usart ) , rx: Rx :: new( ) } . init( config. into( ) , clocks, || {
480
492
mapr. modify_mapr( |_, w| unsafe {
481
493
#[ allow( clippy:: redundant_closure_call) ]
482
494
w. $usartX_remap( ) . $bit( ( $closure) ( PINS :: REMAP ) )
0 commit comments