@@ -824,26 +824,26 @@ pub enum Event {
824824}
825825
826826#[ derive( Debug ) ]
827- pub struct Inner < SPI , W : FrameSize > {
827+ pub struct Inner < SPI , W : FrameSize < SPI > > {
828828 spi : SPI ,
829829 _word : PhantomData < W > ,
830830}
831831
832832/// Spi in Master mode
833833#[ derive( Debug ) ]
834- pub struct Spi < SPI , W : FrameSize = u8 > {
834+ pub struct Spi < SPI , W : FrameSize < SPI > = u8 > {
835835 inner : Inner < SPI , W > ,
836836 _word : PhantomData < W > ,
837837}
838838
839- impl < SPI , W : FrameSize > Deref for Spi < SPI , W > {
839+ impl < SPI , W : FrameSize < SPI > > Deref for Spi < SPI , W > {
840840 type Target = Inner < SPI , W > ;
841841 fn deref ( & self ) -> & Self :: Target {
842842 & self . inner
843843 }
844844}
845845
846- impl < SPI , W : FrameSize > DerefMut for Spi < SPI , W > {
846+ impl < SPI , W : FrameSize < SPI > > DerefMut for Spi < SPI , W > {
847847 fn deref_mut ( & mut self ) -> & mut Self :: Target {
848848 & mut self . inner
849849 }
@@ -903,24 +903,37 @@ instance! { SPI1: Spi1, SPI123 }
903903instance ! { SPI2 : Spi2 , SPI123 }
904904instance ! { SPI3 : Spi3 , SPI123 }
905905
906- pub trait FrameSize : Copy + Default + ' static + crate :: Sealed {
906+ pub trait FrameSize < SPI > : Copy + Default + ' static + crate :: Sealed {
907907 const BITS : u8 ;
908908}
909909
910910macro_rules! framesize {
911911 ( $type: ty) => {
912- impl FrameSize for $type {
912+ impl < SPI > FrameSize < SPI > for $type {
913913 const BITS : u8 = <$type>:: BITS as u8 ;
914914 }
915915 impl crate :: Sealed for $type { }
916916 } ;
917917}
918918
919- framesize ! ( u32 ) ;
919+ macro_rules! framesize_u32 {
920+ ( $SPIx: ty) => {
921+ impl FrameSize <$SPIx> for u32 {
922+ const BITS : u8 = u32 :: BITS as u8 ;
923+ }
924+ } ;
925+ }
926+ impl crate :: Sealed for u32 { }
927+
920928framesize ! ( u16 ) ;
921929framesize ! ( u8 ) ;
922930
923- pub trait SpiExt < SPI : Instance , W : FrameSize = u8 > {
931+ // Only SPI[1,2,3] support 32bit data size
932+ framesize_u32 ! ( SPI1 ) ;
933+ framesize_u32 ! ( SPI2 ) ;
934+ framesize_u32 ! ( SPI3 ) ;
935+
936+ pub trait SpiExt < SPI : Instance , W : FrameSize < SPI > = u8 > {
924937 fn spi < PINS , CONFIG > (
925938 self ,
926939 _pins : PINS ,
@@ -944,7 +957,7 @@ pub trait SpiExt<SPI: Instance, W: FrameSize = u8> {
944957 CONFIG : Into < Config > ;
945958}
946959
947- impl < SPI : Instance , W : FrameSize > SpiExt < SPI , W > for SPI {
960+ impl < SPI : Instance , W : FrameSize < SPI > > SpiExt < SPI , W > for SPI {
948961 fn spi < PINS , CONFIG > (
949962 self ,
950963 _pins : PINS ,
@@ -993,7 +1006,7 @@ fn calc_mbr(spi_ker_ck: u32, spi_freq: u32) -> MBR {
9931006 }
9941007}
9951008
996- impl < SPI : Instance , W : FrameSize > Spi < SPI , W > {
1009+ impl < SPI : Instance , W : FrameSize < SPI > > Spi < SPI , W > {
9971010 fn new (
9981011 spi : SPI ,
9991012 config : impl Into < Config > ,
@@ -1114,7 +1127,7 @@ macro_rules! check_status_error {
11141127 } ;
11151128}
11161129
1117- impl < SPI : Instance , W : FrameSize > Inner < SPI , W > {
1130+ impl < SPI : Instance , W : FrameSize < SPI > > Inner < SPI , W > {
11181131 fn new ( spi : SPI ) -> Self {
11191132 Self {
11201133 spi,
@@ -1387,7 +1400,7 @@ impl<SPI: Instance, W: FrameSize> Inner<SPI, W> {
13871400 }
13881401}
13891402
1390- impl < SPI : Instance , W : FrameSize > Spi < SPI , W > {
1403+ impl < SPI : Instance , W : FrameSize < SPI > > Spi < SPI , W > {
13911404 /// Sets up a frame transaction with the given amount of data words.
13921405 ///
13931406 /// If this is called when a transaction has already started,
@@ -1441,15 +1454,15 @@ impl<SPI: Instance, W: FrameSize> Spi<SPI, W> {
14411454}
14421455
14431456#[ derive( Debug ) ]
1444- pub struct NonBlockingTransfer < ' a , W : FrameSize > {
1457+ pub struct NonBlockingTransfer < ' a , W > {
14451458 write : & ' a [ W ] ,
14461459 read : & ' a mut [ W ] ,
14471460 write_idx : usize ,
14481461 read_idx : usize ,
14491462 len : usize ,
14501463}
14511464
1452- impl < ' a , W : FrameSize > NonBlockingTransfer < ' a , W > {
1465+ impl < ' a , W > NonBlockingTransfer < ' a , W > {
14531466 pub fn new ( write : & ' a [ W ] , read : & ' a mut [ W ] ) -> Self {
14541467 let len = core:: cmp:: max ( read. len ( ) , write. len ( ) ) ;
14551468 NonBlockingTransfer {
@@ -1469,10 +1482,14 @@ impl<'a, W: FrameSize> NonBlockingTransfer<'a, W> {
14691482 self . read_idx >= self . read . len ( ) && self . write_idx >= self . write . len ( )
14701483 }
14711484
1472- fn write_to_spi_nb < SPI : Instance > (
1485+ fn write_to_spi_nb < SPI > (
14731486 & mut self ,
14741487 spi : & mut Inner < SPI , W > ,
1475- ) -> Result < ( ) , Error > {
1488+ ) -> Result < ( ) , Error >
1489+ where
1490+ SPI : Instance ,
1491+ W : FrameSize < SPI > ,
1492+ {
14761493 if self . write_idx < self . write . len ( ) {
14771494 self . write_idx += spi. write_nb ( & self . write [ self . write_idx ..] ) ?;
14781495 }
@@ -1484,10 +1501,14 @@ impl<'a, W: FrameSize> NonBlockingTransfer<'a, W> {
14841501 Ok ( ( ) )
14851502 }
14861503
1487- fn read_from_spi_nb < SPI : Instance > (
1504+ fn read_from_spi_nb < SPI > (
14881505 & mut self ,
14891506 spi : & mut Inner < SPI , W > ,
1490- ) -> Result < ( ) , Error > {
1507+ ) -> Result < ( ) , Error >
1508+ where
1509+ SPI : Instance ,
1510+ W : FrameSize < SPI > ,
1511+ {
14911512 if self . read_idx < self . read . len ( ) {
14921513 self . read_idx += spi. read_nb ( & mut self . read [ self . read_idx ..] ) ?;
14931514 }
@@ -1499,10 +1520,14 @@ impl<'a, W: FrameSize> NonBlockingTransfer<'a, W> {
14991520 Ok ( ( ) )
15001521 }
15011522
1502- fn exchange_nb < SPI : Instance > (
1523+ fn exchange_nb < SPI > (
15031524 & mut self ,
15041525 spi : & mut Inner < SPI , W > ,
1505- ) -> nb:: Result < ( ) , Error > {
1526+ ) -> nb:: Result < ( ) , Error >
1527+ where
1528+ SPI : Instance ,
1529+ W : FrameSize < SPI > ,
1530+ {
15061531 self . write_to_spi_nb ( spi) ?;
15071532 self . read_from_spi_nb ( spi) ?;
15081533 if self . is_complete ( ) {
@@ -1514,7 +1539,7 @@ impl<'a, W: FrameSize> NonBlockingTransfer<'a, W> {
15141539}
15151540
15161541// Non-blocking operations
1517- impl < SPI : Instance , W : FrameSize > Spi < SPI , W > {
1542+ impl < SPI : Instance , W : FrameSize < SPI > > Spi < SPI , W > {
15181543 /// Ends the current transaction. This must always be called when all data has been sent to
15191544 /// properly terminate the transaction and reset the SPI peripheral. Returns
15201545 /// nb::Error::WouldBlock while a transfer is in progress (according to SR:TXC)
@@ -1560,7 +1585,7 @@ impl<SPI: Instance, W: FrameSize> Spi<SPI, W> {
15601585}
15611586
15621587// Implement blocking transaction interface for Spi
1563- impl < SPI : Instance , W : FrameSize > Spi < SPI , W > {
1588+ impl < SPI : Instance , W : FrameSize < SPI > > Spi < SPI , W > {
15641589 /// Write-only transfer
15651590 fn write ( & mut self , words : & [ W ] ) -> Result < ( ) , Error > {
15661591 let communication_mode = self . communication_mode ( ) ;
0 commit comments