18
18
19
19
use core:: fmt;
20
20
use core:: marker:: PhantomData ;
21
- use core:: ops:: { Deref , DerefMut } ;
22
21
23
22
use crate :: rcc;
24
23
use nb:: block;
@@ -187,32 +186,11 @@ pub type NoRx = NoPin;
187
186
188
187
/// Serial abstraction
189
188
pub struct Serial < USART : Instance , WORD = u8 > {
190
- usart : USART ,
191
- pins : ( USART :: TxPin , USART :: RxPin ) ,
192
189
tx : Tx < USART , WORD > ,
193
190
rx : Rx < USART , WORD > ,
194
191
}
195
192
196
- /// Serial receiver
197
- pub struct Rx < USART , WORD = u8 > {
198
- _usart : PhantomData < USART > ,
199
- _word : PhantomData < WORD > ,
200
- }
201
-
202
- /// Serial transmitter
203
- pub struct Tx < USART , WORD = u8 > {
204
- _usart : PhantomData < USART > ,
205
- _word : PhantomData < WORD > ,
206
- }
207
-
208
193
impl < USART : Instance , WORD > Rx < USART , WORD > {
209
- fn new ( ) -> Self {
210
- Self {
211
- _usart : PhantomData ,
212
- _word : PhantomData ,
213
- }
214
- }
215
-
216
194
/// Start listening for an rx not empty interrupt event
217
195
///
218
196
/// Note, you will also have to enable the corresponding interrupt
@@ -240,6 +218,18 @@ impl<USART: Instance, WORD> Rx<USART, WORD> {
240
218
}
241
219
}
242
220
221
+ impl < USART : Instance > Rx < USART , u8 > {
222
+ fn with_u16_data ( self ) -> Rx < USART , u16 > {
223
+ Rx :: new ( self . pin )
224
+ }
225
+ }
226
+
227
+ impl < USART : Instance > Rx < USART , u16 > {
228
+ fn with_u8_data ( self ) -> Rx < USART , u8 > {
229
+ Rx :: new ( self . pin )
230
+ }
231
+ }
232
+
243
233
/// Trait for [`Rx`] interrupt handling.
244
234
pub trait RxISR {
245
235
/// Return true if the line idle status is set
@@ -273,13 +263,6 @@ impl<USART: Instance, WORD> RxISR for Rx<USART, WORD> {
273
263
}
274
264
275
265
impl < USART : Instance , WORD > Tx < USART , WORD > {
276
- fn new ( ) -> Self {
277
- Self {
278
- _usart : PhantomData ,
279
- _word : PhantomData ,
280
- }
281
- }
282
-
283
266
/// Start listening for a tx empty interrupt event
284
267
///
285
268
/// Note, you will also have to enable the corresponding interrupt
@@ -294,6 +277,18 @@ impl<USART: Instance, WORD> Tx<USART, WORD> {
294
277
}
295
278
}
296
279
280
+ impl < USART : Instance > Tx < USART , u8 > {
281
+ fn with_u16_data ( self ) -> Tx < USART , u16 > {
282
+ Tx :: new ( self . usart , self . pin )
283
+ }
284
+ }
285
+
286
+ impl < USART : Instance > Tx < USART , u16 > {
287
+ fn with_u8_data ( self ) -> Tx < USART , u8 > {
288
+ Tx :: new ( self . usart , self . pin )
289
+ }
290
+ }
291
+
297
292
/// Trait for [`Tx`] interrupt handling.
298
293
pub trait TxISR {
299
294
/// Return true if the tx register is empty (and can accept data)
@@ -336,118 +331,42 @@ impl<USART: Instance, WORD> AsMut<Rx<USART, WORD>> for Serial<USART, WORD> {
336
331
}
337
332
338
333
/// Serial receiver containing RX pin
339
- pub struct URx < USART : Instance , WORD = u8 > {
340
- inner : Rx < USART , WORD > ,
334
+ pub struct Rx < USART : Instance , WORD = u8 > {
335
+ _word : PhantomData < ( USART , WORD ) > ,
341
336
pin : USART :: RxPin ,
342
337
}
343
338
344
339
/// Serial transmitter containing TX pin
345
- pub struct UTx < USART : Instance , WORD = u8 > {
346
- inner : Tx < USART , WORD > ,
340
+ pub struct Tx < USART : Instance , WORD = u8 > {
341
+ _word : PhantomData < WORD > ,
347
342
usart : USART ,
348
343
pin : USART :: TxPin ,
349
344
}
350
345
351
- impl < USART : Instance , WORD > URx < USART , WORD > {
346
+ impl < USART : Instance , WORD > Rx < USART , WORD > {
352
347
fn new ( pin : USART :: RxPin ) -> Self {
353
348
Self {
354
- inner : Rx :: new ( ) ,
349
+ _word : PhantomData ,
355
350
pin,
356
351
}
357
352
}
358
353
359
- pub fn erase ( self ) -> Rx < USART , WORD > {
360
- Rx :: new ( )
361
- }
362
-
363
- pub fn join < TX > ( self , tx : UTx < USART , WORD > ) -> Serial < USART , WORD > {
364
- Serial {
365
- usart : tx. usart ,
366
- pins : ( tx. pin , self . pin ) ,
367
- tx : tx. inner ,
368
- rx : self . inner ,
369
- }
354
+ pub fn join < TX > ( self , tx : Tx < USART , WORD > ) -> Serial < USART , WORD > {
355
+ Serial { tx, rx : self }
370
356
}
371
357
}
372
358
373
- impl < USART : Instance , WORD > UTx < USART , WORD > {
359
+ impl < USART : Instance , WORD > Tx < USART , WORD > {
374
360
fn new ( usart : USART , pin : USART :: TxPin ) -> Self {
375
361
Self {
376
- inner : Tx :: new ( ) ,
362
+ _word : PhantomData ,
377
363
usart,
378
364
pin,
379
365
}
380
366
}
381
367
382
- pub fn erase ( self ) -> Tx < USART , WORD > {
383
- Tx :: new ( )
384
- }
385
-
386
- pub fn join ( self , rx : URx < USART , WORD > ) -> Serial < USART , WORD > {
387
- Serial {
388
- usart : self . usart ,
389
- pins : ( self . pin , rx. pin ) ,
390
- tx : self . inner ,
391
- rx : rx. inner ,
392
- }
393
- }
394
- }
395
-
396
- impl < USART : Instance , WORD > AsRef < Tx < USART , WORD > > for UTx < USART , WORD > {
397
- #[ inline( always) ]
398
- fn as_ref ( & self ) -> & Tx < USART , WORD > {
399
- & self . inner
400
- }
401
- }
402
-
403
- impl < USART : Instance , WORD > Deref for UTx < USART , WORD > {
404
- type Target = Tx < USART , WORD > ;
405
- #[ inline( always) ]
406
- fn deref ( & self ) -> & Self :: Target {
407
- & self . inner
408
- }
409
- }
410
-
411
- impl < USART : Instance , WORD > AsRef < Rx < USART , WORD > > for URx < USART , WORD > {
412
- #[ inline( always) ]
413
- fn as_ref ( & self ) -> & Rx < USART , WORD > {
414
- & self . inner
415
- }
416
- }
417
-
418
- impl < USART : Instance , WORD > Deref for URx < USART , WORD > {
419
- type Target = Rx < USART , WORD > ;
420
- #[ inline( always) ]
421
- fn deref ( & self ) -> & Self :: Target {
422
- & self . inner
423
- }
424
- }
425
-
426
- impl < USART : Instance , WORD > AsMut < Tx < USART , WORD > > for UTx < USART , WORD > {
427
- #[ inline( always) ]
428
- fn as_mut ( & mut self ) -> & mut Tx < USART , WORD > {
429
- & mut self . inner
430
- }
431
- }
432
-
433
- impl < USART : Instance , WORD > DerefMut for UTx < USART , WORD > {
434
- #[ inline( always) ]
435
- fn deref_mut ( & mut self ) -> & mut Self :: Target {
436
- & mut self . inner
437
- }
438
- }
439
-
440
- impl < USART : Instance , WORD > AsMut < Rx < USART , WORD > > for URx < USART , WORD > {
441
- #[ inline( always) ]
442
- fn as_mut ( & mut self ) -> & mut Rx < USART , WORD > {
443
- & mut self . inner
444
- }
445
- }
446
-
447
- impl < USART : Instance , WORD > DerefMut for URx < USART , WORD > {
448
- #[ inline( always) ]
449
- fn deref_mut ( & mut self ) -> & mut Self :: Target {
450
- & mut self . inner
368
+ pub fn join ( self , rx : Rx < USART , WORD > ) -> Serial < USART , WORD > {
369
+ Serial { tx : self , rx }
451
370
}
452
371
}
453
372
@@ -628,13 +547,9 @@ impl<USART: Instance, WORD> Serial<USART, WORD> {
628
547
DmaConfig :: None => { }
629
548
}
630
549
631
- let pins = ( pins. 0 . into ( ) , pins. 1 . into ( ) ) ;
632
-
633
550
Ok ( Serial {
634
- usart,
635
- pins,
636
- tx : Tx :: new ( ) ,
637
- rx : Rx :: new ( ) ,
551
+ tx : Tx :: new ( usart, pins. 0 . into ( ) ) ,
552
+ rx : Rx :: new ( pins. 1 . into ( ) ) ,
638
553
}
639
554
. config_stop ( config) )
640
555
}
@@ -645,8 +560,8 @@ impl<USART: Instance, WORD> Serial<USART, WORD> {
645
560
RX : TryFrom < USART :: RxPin , Error = E > ,
646
561
{
647
562
Ok ( (
648
- self . usart ,
649
- ( self . pins . 0 . try_into ( ) ?, self . pins . 1 . try_into ( ) ?) ,
563
+ self . tx . usart ,
564
+ ( self . tx . pin . try_into ( ) ?, self . rx . pin . try_into ( ) ?) ,
650
565
) )
651
566
}
652
567
}
@@ -730,22 +645,14 @@ impl<USART: Instance, WORD> TxISR for Serial<USART, WORD> {
730
645
}
731
646
}
732
647
733
- impl < USART : Instance , WORD > Serial < USART , WORD > {
734
- pub fn split_nondestructive ( self ) -> ( UTx < USART , WORD > , URx < USART , WORD > ) {
735
- ( UTx :: new ( self . usart , self . pins . 0 ) , URx :: new ( self . pins . 1 ) )
736
- }
737
- }
738
-
739
648
impl < USART : Instance > Serial < USART , u8 > {
740
649
/// Converts this Serial into a version that can read and write `u16` values instead of `u8`s
741
650
///
742
651
/// This can be used with a word length of 9 bits.
743
652
pub fn with_u16_data ( self ) -> Serial < USART , u16 > {
744
653
Serial {
745
- usart : self . usart ,
746
- pins : self . pins ,
747
- tx : Tx :: new ( ) ,
748
- rx : Rx :: new ( ) ,
654
+ tx : self . tx . with_u16_data ( ) ,
655
+ rx : self . rx . with_u16_data ( ) ,
749
656
}
750
657
}
751
658
}
@@ -756,10 +663,8 @@ impl<USART: Instance> Serial<USART, u16> {
756
663
/// This can be used with a word length of 8 bits.
757
664
pub fn with_u8_data ( self ) -> Serial < USART , u8 > {
758
665
Serial {
759
- usart : self . usart ,
760
- pins : self . pins ,
761
- tx : Tx :: new ( ) ,
762
- rx : Rx :: new ( ) ,
666
+ tx : self . tx . with_u8_data ( ) ,
667
+ rx : self . rx . with_u8_data ( ) ,
763
668
}
764
669
}
765
670
}
@@ -784,7 +689,7 @@ unsafe impl<USART: Instance> PeriAddress for Tx<USART, u8> {
784
689
785
690
impl < USART : Instance , WORD > Serial < USART , WORD > {
786
691
fn config_stop ( self , config : config:: Config ) -> Self {
787
- self . usart . set_stopbits ( config. stopbits ) ;
692
+ self . tx . usart . set_stopbits ( config. stopbits ) ;
788
693
self
789
694
}
790
695
}
@@ -831,7 +736,7 @@ pub trait Instance: crate::Sealed + rcc::Enable + rcc::Reset + rcc::BusClock {
831
736
}
832
737
833
738
macro_rules! halUsart {
834
- ( $USART: ty, $usart: ident , $Serial: ident, $Tx: ident, $Rx: ident) => {
739
+ ( $USART: ty, $usart: ident, $Serial: ident, $Tx: ident, $Rx: ident) => {
835
740
pub type $Serial<WORD = u8 > = Serial <$USART, WORD >;
836
741
pub type $Tx<WORD = u8 > = Tx <$USART, WORD >;
837
742
pub type $Rx<WORD = u8 > = Rx <$USART, WORD >;
@@ -949,7 +854,11 @@ impl<USART: Instance> fmt::Write for Tx<USART> {
949
854
impl < USART : Instance > Rx < USART , u8 > {
950
855
fn read ( & mut self ) -> nb:: Result < u8 , Error > {
951
856
// Delegate to the Read<u16> implementation, then truncate to 8 bits
952
- Rx :: < USART , u16 > :: new ( ) . read ( ) . map ( |word16| word16 as u8 )
857
+ unsafe {
858
+ ( & mut * ( self as * mut Self as * mut Rx < USART , u16 > ) )
859
+ . read ( )
860
+ . map ( |word16| word16 as u8 )
861
+ }
953
862
}
954
863
}
955
864
@@ -987,12 +896,12 @@ impl<USART: Instance> Rx<USART, u16> {
987
896
impl < USART : Instance > Tx < USART , u8 > {
988
897
fn write ( & mut self , word : u8 ) -> nb:: Result < ( ) , Error > {
989
898
// Delegate to u16 version
990
- Tx :: < USART , u16 > :: new ( ) . write ( u16:: from ( word) )
899
+ unsafe { ( & mut * ( self as * mut Self as * mut Tx < USART , u16 > ) ) . write ( u16:: from ( word) ) }
991
900
}
992
901
993
902
fn flush ( & mut self ) -> nb:: Result < ( ) , Error > {
994
903
// Delegate to u16 version
995
- Tx :: < USART , u16 > :: new ( ) . flush ( )
904
+ unsafe { ( & mut * ( self as * mut Self as * mut Tx < USART , u16 > ) ) . flush ( ) }
996
905
}
997
906
998
907
fn bwrite_all ( & mut self , bytes : & [ u8 ] ) -> Result < ( ) , Error > {
0 commit comments