@@ -132,10 +132,10 @@ impl Ms for Master {
132
132
}
133
133
134
134
#[ derive( Debug ) ]
135
- pub struct Spi < SPI , PINS , const BIDI : bool = false, OPERATION = Master > {
135
+ pub struct Spi < SPI , PINS , const BIDI : bool = false, FrameSize = u8 , OPERATION = Master > {
136
136
spi : SPI ,
137
137
pins : PINS ,
138
- _operation : PhantomData < OPERATION > ,
138
+ _operation : PhantomData < ( FrameSize , OPERATION ) > ,
139
139
}
140
140
141
141
// Implemented by all SPI instances
@@ -149,8 +149,8 @@ pub trait Instance:
149
149
// Implemented by all SPI instances
150
150
macro_rules! spi {
151
151
( $SPI: ty: $Spi: ident) => {
152
- pub type $Spi<PINS , const BIDI : bool = false , OPERATION = Master > =
153
- Spi <$SPI, PINS , BIDI , OPERATION >;
152
+ pub type $Spi<PINS , const BIDI : bool = false , FrameSize = u8 , OPERATION = Master > =
153
+ Spi <$SPI, PINS , BIDI , FrameSize , OPERATION >;
154
154
155
155
impl Instance for $SPI {
156
156
fn ptr( ) -> * const spi1:: RegisterBlock {
@@ -281,30 +281,58 @@ impl<SPI: Instance, PINS, const BIDI: bool, OPERATION: Ms> Spi<SPI, PINS, BIDI,
281
281
}
282
282
}
283
283
284
- impl < SPI : Instance , PINS , OPERATION : Ms > Spi < SPI , PINS , false , OPERATION > {
284
+ impl < SPI : Instance , PINS , FrameSize , OPERATION : Ms > Spi < SPI , PINS , false , OPERATION > {
285
285
pub fn to_bidi_transfer_mode ( self ) -> Spi < SPI , PINS , true , OPERATION > {
286
286
self . into_mode ( )
287
287
}
288
288
}
289
289
290
- impl < SPI : Instance , PINS , OPERATION : Ms > Spi < SPI , PINS , true , OPERATION > {
290
+ impl < SPI : Instance , PINS , FrameSize , OPERATION : Ms > Spi < SPI , PINS , true , OPERATION > {
291
291
pub fn to_normal_transfer_mode ( self ) -> Spi < SPI , PINS , false , OPERATION > {
292
292
self . into_mode ( )
293
293
}
294
294
}
295
295
296
- impl < SPI : Instance , PINS , const BIDI : bool > Spi < SPI , PINS , BIDI , Master > {
296
+ impl < SPI : Instance , PINS , const BIDI : bool , FrameSize > Spi < SPI , PINS , BIDI , FrameSize , Master > {
297
297
pub fn to_slave_operation ( self ) -> Spi < SPI , PINS , BIDI , Slave > {
298
298
self . into_mode ( )
299
299
}
300
300
}
301
301
302
- impl < SPI : Instance , PINS , const BIDI : bool > Spi < SPI , PINS , BIDI , Slave > {
302
+ impl < SPI : Instance , PINS , const BIDI : bool , FrameSize > Spi < SPI , PINS , BIDI , FrameSize , Slave > {
303
303
pub fn to_master_operation ( self ) -> Spi < SPI , PINS , BIDI , Master > {
304
304
self . into_mode ( )
305
305
}
306
306
}
307
307
308
+ impl < SPI , REMAP , PINS , const BIDI : bool , OPERATION > Spi < SPI , REMAP , PINS , u8 , OPERATION >
309
+ where
310
+ SPI : Instance ,
311
+ {
312
+ /// Converts from 8bit dataframe to 16bit.
313
+ pub fn frame_size_16bit ( self ) -> Spi < SPI , REMAP , PINS , u16 > {
314
+ self . spi . cr1 . modify ( |_, w| w. spe ( ) . clear_bit ( ) ) ;
315
+ self . spi
316
+ . cr1
317
+ . modify ( |_, w| w. dff ( ) . set_bit ( ) . spe ( ) . set_bit ( ) ) ;
318
+ Spi :: _new ( spi, pins)
319
+ }
320
+ }
321
+
322
+ impl < SPI , REMAP , PINS , const BIDI : bool , OPERATION > Spi < SPI , REMAP , PINS , u16 , OPERATION >
323
+ where
324
+ SPI : Instance ,
325
+ {
326
+ /// Converts from 16bit dataframe to 8bit.
327
+ pub fn frame_size_8bit ( self ) -> Spi < SPI , REMAP , PINS , u8 > {
328
+ self . spi . cr1 . modify ( |_, w| w. spe ( ) . clear_bit ( ) ) ;
329
+ self . spi
330
+ . cr1
331
+ . modify ( |_, w| w. dff ( ) . clear_bit ( ) . spe ( ) . set_bit ( ) ) ;
332
+ Spi :: _new ( spi, pins)
333
+ }
334
+ }
335
+
308
336
impl < SPI : Instance , SCK , MISO , MOSI > Spi < SPI , ( SCK , MISO , MOSI ) , false , Master > {
309
337
pub fn new (
310
338
spi : SPI ,
@@ -556,7 +584,7 @@ impl<SPI: Instance, PINS, const BIDI: bool, OPERATION> Spi<SPI, PINS, BIDI, OPER
556
584
}
557
585
558
586
#[ inline( always) ]
559
- fn check_read ( & mut self ) -> nb:: Result < u8 , Error > {
587
+ fn check_read ( & mut self ) -> nb:: Result < FrameSize , Error > {
560
588
let sr = self . spi . sr . read ( ) ;
561
589
562
590
Err ( if sr. ovr ( ) . bit_is_set ( ) {
@@ -566,14 +594,14 @@ impl<SPI: Instance, PINS, const BIDI: bool, OPERATION> Spi<SPI, PINS, BIDI, OPER
566
594
} else if sr. crcerr ( ) . bit_is_set ( ) {
567
595
Error :: Crc . into ( )
568
596
} else if sr. rxne ( ) . bit_is_set ( ) {
569
- return Ok ( self . read_u8 ( ) ) ;
597
+ return Ok ( self . read_data_reg ( ) ) ;
570
598
} else {
571
599
nb:: Error :: WouldBlock
572
600
} )
573
601
}
574
602
575
603
#[ inline( always) ]
576
- fn check_send ( & mut self , byte : u8 ) -> nb:: Result < ( ) , Error > {
604
+ fn check_send ( & mut self , byte : FrameSize ) -> nb:: Result < ( ) , Error > {
577
605
let sr = self . spi . sr . read ( ) ;
578
606
579
607
Err ( if sr. ovr ( ) . bit_is_set ( ) {
@@ -592,23 +620,34 @@ impl<SPI: Instance, PINS, const BIDI: bool, OPERATION> Spi<SPI, PINS, BIDI, OPER
592
620
} ) ;
593
621
Error :: Crc . into ( )
594
622
} else if sr. txe ( ) . bit_is_set ( ) {
595
- self . send_u8 ( byte) ;
623
+ self . write_data_reg ( byte) ;
596
624
return Ok ( ( ) ) ;
597
625
} else {
598
626
nb:: Error :: WouldBlock
599
627
} )
600
628
}
629
+ }
601
630
602
- #[ inline( always) ]
603
- fn read_u8 ( & mut self ) -> u8 {
604
- // NOTE(read_volatile) read only 1 byte (the svd2rust API only allows reading a half-word)
605
- unsafe { ptr:: read_volatile ( & self . spi . dr as * const _ as * const u8 ) }
631
+ pub trait ReadWriteReg < T > {
632
+ fn read_data_reg ( & mut self ) -> T ;
633
+ fn write_data_reg ( & mut self , data : T ) ;
634
+ }
635
+
636
+ impl < SPI , REMAP , PINS , const BIDI : bool , OPERATION , FrameSize > SpiReadWrite < FrameSize >
637
+ for Spi < SPI , REMAP , PINS , FrameSize , OPERATION >
638
+ where
639
+ SPI : Instance ,
640
+ FrameSize : Copy ,
641
+ {
642
+ fn read_data_reg ( & mut self ) -> FrameSize {
643
+ // NOTE(read_volatile) read only 1 byte (the svd2rust API only allows
644
+ // reading a half-word)
645
+ unsafe { ptr:: read_volatile ( & self . spi . dr as * const _ as * const FrameSize ) }
606
646
}
607
647
608
- #[ inline( always) ]
609
- fn send_u8 ( & mut self , byte : u8 ) {
648
+ fn write_data_reg ( & mut self , data : FrameSize ) {
610
649
// NOTE(write_volatile) see note above
611
- unsafe { ptr:: write_volatile ( & self . spi . dr as * const _ as * mut u8 , byte ) }
650
+ unsafe { ptr:: write_volatile ( & self . spi . dr as * const _ as * mut FrameSize , data ) }
612
651
}
613
652
}
614
653
0 commit comments