18
18
19
19
use core:: fmt;
20
20
use core:: marker:: PhantomData ;
21
+ use core:: ops:: { Deref , DerefMut } ;
21
22
22
23
use crate :: rcc;
23
24
use nb:: block;
@@ -309,107 +310,77 @@ impl<USART: Instance, WORD> Tx<USART, WORD> {
309
310
}
310
311
311
312
impl < USART , PINS , WORD > AsRef < Tx < USART , WORD > > for Serial < USART , PINS , WORD > {
313
+ #[ inline( always) ]
312
314
fn as_ref ( & self ) -> & Tx < USART , WORD > {
313
315
& self . tx
314
316
}
315
317
}
316
318
317
319
impl < USART , PINS , WORD > AsRef < Rx < USART , WORD > > for Serial < USART , PINS , WORD > {
320
+ #[ inline( always) ]
318
321
fn as_ref ( & self ) -> & Rx < USART , WORD > {
319
322
& self . rx
320
323
}
321
324
}
322
325
323
326
impl < USART , PINS , WORD > AsMut < Tx < USART , WORD > > for Serial < USART , PINS , WORD > {
327
+ #[ inline( always) ]
324
328
fn as_mut ( & mut self ) -> & mut Tx < USART , WORD > {
325
329
& mut self . tx
326
330
}
327
331
}
328
332
329
333
impl < USART , PINS , WORD > AsMut < Rx < USART , WORD > > for Serial < USART , PINS , WORD > {
334
+ #[ inline( always) ]
330
335
fn as_mut ( & mut self ) -> & mut Rx < USART , WORD > {
331
336
& mut self . rx
332
337
}
333
338
}
334
339
335
340
/// Serial receiver containing RX pin
336
341
pub struct URx < USART , RX , WORD = u8 > {
337
- _usart : PhantomData < USART > ,
342
+ inner : Rx < USART , WORD > ,
338
343
pin : RX ,
339
- _word : PhantomData < WORD > ,
340
344
}
341
345
342
346
/// Serial transmitter containing TX pin
343
347
pub struct UTx < USART , TX , WORD = u8 > {
348
+ inner : Tx < USART , WORD > ,
344
349
usart : USART ,
345
350
pin : TX ,
346
- _word : PhantomData < WORD > ,
347
351
}
348
352
349
353
impl < USART : Instance , RX , WORD > URx < USART , RX , WORD > {
350
354
fn new ( pin : RX ) -> Self {
351
355
Self {
352
- _usart : PhantomData ,
356
+ inner : Rx :: new ( ) ,
353
357
pin,
354
- _word : PhantomData ,
355
358
}
356
359
}
357
360
358
361
pub fn erase ( self ) -> Rx < USART , WORD > {
359
362
Rx :: new ( )
360
363
}
361
364
362
- /// Start listening for an rx not empty interrupt event
363
- ///
364
- /// Note, you will also have to enable the corresponding interrupt
365
- /// in the NVIC to start receiving events.
366
- pub fn listen ( & mut self ) {
367
- unsafe { ( * USART :: ptr ( ) ) . cr1 . modify ( |_, w| w. rxneie ( ) . set_bit ( ) ) }
368
- }
369
-
370
- /// Stop listening for the rx not empty interrupt event
371
- pub fn unlisten ( & mut self ) {
372
- unsafe { ( * USART :: ptr ( ) ) . cr1 . modify ( |_, w| w. rxneie ( ) . clear_bit ( ) ) }
373
- }
374
-
375
- /// Start listening for a line idle interrupt event
376
- ///
377
- /// Note, you will also have to enable the corresponding interrupt
378
- /// in the NVIC to start receiving events.
379
- pub fn listen_idle ( & mut self ) {
380
- unsafe { ( * USART :: ptr ( ) ) . cr1 . modify ( |_, w| w. idleie ( ) . set_bit ( ) ) }
381
- }
382
-
383
- /// Stop listening for the line idle interrupt event
384
- pub fn unlisten_idle ( & mut self ) {
385
- unsafe { ( * USART :: ptr ( ) ) . cr1 . modify ( |_, w| w. idleie ( ) . clear_bit ( ) ) }
386
- }
387
-
388
- /// Return true if the line idle status is set
389
- pub fn is_idle ( & self ) -> bool {
390
- unsafe { ( * USART :: ptr ( ) ) . sr . read ( ) . idle ( ) . bit_is_set ( ) }
391
- }
392
-
393
- /// Return true if the rx register is not empty (and can be read)
394
- pub fn is_rx_not_empty ( & self ) -> bool {
395
- unsafe { ( * USART :: ptr ( ) ) . sr . read ( ) . rxne ( ) . bit_is_set ( ) }
396
- }
397
-
398
- /// Clear idle line interrupt flag
399
- pub fn clear_idle_interrupt ( & self ) {
400
- unsafe {
401
- let _ = ( * USART :: ptr ( ) ) . sr . read ( ) ;
402
- let _ = ( * USART :: ptr ( ) ) . dr . read ( ) ;
365
+ pub fn join < TX > ( self , tx : UTx < USART , TX , WORD > ) -> Serial < USART , ( TX , RX ) , WORD >
366
+ where
367
+ ( TX , RX ) : Pins < USART > ,
368
+ {
369
+ Serial {
370
+ usart : tx. usart ,
371
+ pins : ( tx. pin , self . pin ) ,
372
+ tx : tx. inner ,
373
+ rx : self . inner ,
403
374
}
404
375
}
405
376
}
406
377
407
378
impl < USART : Instance , TX , WORD > UTx < USART , TX , WORD > {
408
379
fn new ( usart : USART , pin : TX ) -> Self {
409
380
Self {
381
+ inner : Tx :: new ( ) ,
410
382
usart,
411
383
pin,
412
- _word : PhantomData ,
413
384
}
414
385
}
415
386
@@ -424,28 +395,67 @@ impl<USART: Instance, TX, WORD> UTx<USART, TX, WORD> {
424
395
Serial {
425
396
usart : self . usart ,
426
397
pins : ( self . pin , rx. pin ) ,
427
- tx : Tx :: new ( ) ,
428
- rx : Rx :: new ( ) ,
398
+ tx : self . inner ,
399
+ rx : rx . inner ,
429
400
}
430
401
}
402
+ }
431
403
432
- /// Start listening for a tx empty interrupt event
433
- ///
434
- /// Note, you will also have to enable the corresponding interrupt
435
- /// in the NVIC to start receiving events.
436
- // TODO: replace with "self.usart"
437
- pub fn listen ( & mut self ) {
438
- unsafe { ( * USART :: ptr ( ) ) . cr1 . modify ( |_, w| w. txeie ( ) . set_bit ( ) ) }
404
+ impl < USART , TX , WORD > AsRef < Tx < USART , WORD > > for UTx < USART , TX , WORD > {
405
+ #[ inline( always) ]
406
+ fn as_ref ( & self ) -> & Tx < USART , WORD > {
407
+ & self . inner
439
408
}
409
+ }
440
410
441
- /// Stop listening for the tx empty interrupt event
442
- pub fn unlisten ( & mut self ) {
443
- unsafe { ( * USART :: ptr ( ) ) . cr1 . modify ( |_, w| w. txeie ( ) . clear_bit ( ) ) }
411
+ impl < USART , TX , WORD > Deref for UTx < USART , TX , WORD > {
412
+ type Target = Tx < USART , WORD > ;
413
+ #[ inline( always) ]
414
+ fn deref ( & self ) -> & Self :: Target {
415
+ & self . inner
444
416
}
417
+ }
445
418
446
- /// Return true if the tx register is empty (and can accept data)
447
- pub fn is_tx_empty ( & self ) -> bool {
448
- unsafe { ( * USART :: ptr ( ) ) . sr . read ( ) . txe ( ) . bit_is_set ( ) }
419
+ impl < USART , RX , WORD > AsRef < Rx < USART , WORD > > for URx < USART , RX , WORD > {
420
+ #[ inline( always) ]
421
+ fn as_ref ( & self ) -> & Rx < USART , WORD > {
422
+ & self . inner
423
+ }
424
+ }
425
+
426
+ impl < USART , RX , WORD > Deref for URx < USART , RX , WORD > {
427
+ type Target = Rx < USART , WORD > ;
428
+ #[ inline( always) ]
429
+ fn deref ( & self ) -> & Self :: Target {
430
+ & self . inner
431
+ }
432
+ }
433
+
434
+ impl < USART , TX , WORD > AsMut < Tx < USART , WORD > > for UTx < USART , TX , WORD > {
435
+ #[ inline( always) ]
436
+ fn as_mut ( & mut self ) -> & mut Tx < USART , WORD > {
437
+ & mut self . inner
438
+ }
439
+ }
440
+
441
+ impl < USART , TX , WORD > DerefMut for UTx < USART , TX , WORD > {
442
+ #[ inline( always) ]
443
+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
444
+ & mut self . inner
445
+ }
446
+ }
447
+
448
+ impl < USART , RX , WORD > AsMut < Rx < USART , WORD > > for URx < USART , RX , WORD > {
449
+ #[ inline( always) ]
450
+ fn as_mut ( & mut self ) -> & mut Rx < USART , WORD > {
451
+ & mut self . inner
452
+ }
453
+ }
454
+
455
+ impl < USART , RX , WORD > DerefMut for URx < USART , RX , WORD > {
456
+ #[ inline( always) ]
457
+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
458
+ & mut self . inner
449
459
}
450
460
}
451
461
@@ -1035,109 +1045,3 @@ impl<USART: Instance> Tx<USART, u16> {
1035
1045
nb:: block!( self . flush( ) )
1036
1046
}
1037
1047
}
1038
-
1039
- impl < USART : Instance , TX > fmt:: Write for UTx < USART , TX > {
1040
- fn write_str ( & mut self , s : & str ) -> fmt:: Result {
1041
- s. bytes ( )
1042
- . try_for_each ( |c| block ! ( self . write( c) ) )
1043
- . map_err ( |_| fmt:: Error )
1044
- }
1045
- }
1046
-
1047
- impl < USART : Instance , RX > URx < USART , RX , u8 > {
1048
- fn read ( & mut self ) -> nb:: Result < u8 , Error > {
1049
- // Delegate to the Read<u16> implementation, then truncate to 8 bits
1050
- Rx :: < USART , u16 > :: new ( ) . read ( ) . map ( |word16| word16 as u8 )
1051
- }
1052
- }
1053
-
1054
- impl < USART : Instance , RX > URx < USART , RX , u16 > {
1055
- fn read ( & mut self ) -> nb:: Result < u16 , Error > {
1056
- // NOTE(unsafe) atomic read with no side effects
1057
- let sr = unsafe { ( * USART :: ptr ( ) ) . sr . read ( ) } ;
1058
-
1059
- // Any error requires the dr to be read to clear
1060
- if sr. pe ( ) . bit_is_set ( )
1061
- || sr. fe ( ) . bit_is_set ( )
1062
- || sr. nf ( ) . bit_is_set ( )
1063
- || sr. ore ( ) . bit_is_set ( )
1064
- {
1065
- unsafe { ( * USART :: ptr ( ) ) . dr . read ( ) } ;
1066
- }
1067
-
1068
- Err ( if sr. pe ( ) . bit_is_set ( ) {
1069
- Error :: Parity . into ( )
1070
- } else if sr. fe ( ) . bit_is_set ( ) {
1071
- Error :: FrameFormat . into ( )
1072
- } else if sr. nf ( ) . bit_is_set ( ) {
1073
- Error :: Noise . into ( )
1074
- } else if sr. ore ( ) . bit_is_set ( ) {
1075
- Error :: Overrun . into ( )
1076
- } else if sr. rxne ( ) . bit_is_set ( ) {
1077
- // NOTE(unsafe) atomic read from stateless register
1078
- return Ok ( unsafe { & * USART :: ptr ( ) } . dr . read ( ) . dr ( ) . bits ( ) ) ;
1079
- } else {
1080
- nb:: Error :: WouldBlock
1081
- } )
1082
- }
1083
- }
1084
-
1085
- impl < USART : Instance , TX > UTx < USART , TX , u8 > {
1086
- fn write ( & mut self , word : u8 ) -> nb:: Result < ( ) , Error > {
1087
- // Delegate to u16 version
1088
- Tx :: < USART , u16 > :: new ( ) . write ( u16:: from ( word) )
1089
- }
1090
-
1091
- fn flush ( & mut self ) -> nb:: Result < ( ) , Error > {
1092
- // Delegate to u16 version
1093
- Tx :: < USART , u16 > :: new ( ) . flush ( )
1094
- }
1095
-
1096
- fn bwrite_all ( & mut self , bytes : & [ u8 ] ) -> Result < ( ) , Error > {
1097
- for & b in bytes {
1098
- nb:: block!( self . write( b) ) ?;
1099
- }
1100
- Ok ( ( ) )
1101
- }
1102
-
1103
- fn bflush ( & mut self ) -> Result < ( ) , Error > {
1104
- nb:: block!( self . flush( ) )
1105
- }
1106
- }
1107
-
1108
- impl < USART : Instance , TX > UTx < USART , TX , u16 > {
1109
- fn write ( & mut self , word : u16 ) -> nb:: Result < ( ) , Error > {
1110
- // NOTE(unsafe) atomic read with no side effects
1111
- let sr = unsafe { ( * USART :: ptr ( ) ) . sr . read ( ) } ;
1112
-
1113
- if sr. txe ( ) . bit_is_set ( ) {
1114
- // NOTE(unsafe) atomic write to stateless register
1115
- unsafe { & * USART :: ptr ( ) } . dr . write ( |w| w. dr ( ) . bits ( word) ) ;
1116
- Ok ( ( ) )
1117
- } else {
1118
- Err ( nb:: Error :: WouldBlock )
1119
- }
1120
- }
1121
-
1122
- fn flush ( & mut self ) -> nb:: Result < ( ) , Error > {
1123
- // NOTE(unsafe) atomic read with no side effects
1124
- let sr = unsafe { ( * USART :: ptr ( ) ) . sr . read ( ) } ;
1125
-
1126
- if sr. tc ( ) . bit_is_set ( ) {
1127
- Ok ( ( ) )
1128
- } else {
1129
- Err ( nb:: Error :: WouldBlock )
1130
- }
1131
- }
1132
-
1133
- fn bwrite_all ( & mut self , buffer : & [ u16 ] ) -> Result < ( ) , Error > {
1134
- for & b in buffer {
1135
- nb:: block!( self . write( b) ) ?;
1136
- }
1137
- Ok ( ( ) )
1138
- }
1139
-
1140
- fn bflush ( & mut self ) -> Result < ( ) , Error > {
1141
- nb:: block!( self . flush( ) )
1142
- }
1143
- }
0 commit comments