@@ -374,25 +374,34 @@ pub struct Spi<SPI, PINS> {
374
374
pins : PINS ,
375
375
}
376
376
377
- // Implemented by all I2C instances
377
+ mod private {
378
+ pub trait Sealed { }
379
+ }
380
+
381
+ // Implemented by all SPI instances
382
+ pub trait Instance : private:: Sealed + Deref < Target = spi1:: RegisterBlock > + Enable {
383
+ #[ doc( hidden) ]
384
+ fn pclk_freq ( clocks : & Clocks ) -> Hertz ;
385
+ }
386
+
387
+ // Implemented by all SPI instances
378
388
macro_rules! spi {
379
- ( $SPI: ty: ( $spi: ident, $pclk: ident) ) => {
389
+ ( $SPI: ident: ( $spi: ident, $pclk: ident) ) => {
390
+ impl private:: Sealed for $SPI { }
391
+ impl Instance for $SPI {
392
+ fn pclk_freq( clocks: & Clocks ) -> Hertz {
393
+ clocks. $pclk( )
394
+ }
395
+ }
396
+
380
397
impl <PINS > Spi <$SPI, PINS >
381
398
where
382
399
PINS : Pins <$SPI>,
383
400
{
401
+ #[ deprecated( since = "0.10.0" , note = "Please use new instead" ) ]
384
402
pub fn $spi( spi: $SPI, pins: PINS , mode: Mode , freq: Hertz , clocks: Clocks ) -> Self {
385
403
Self :: new( spi, pins, mode, freq, clocks)
386
404
}
387
- pub fn new( spi: $SPI, pins: PINS , mode: Mode , freq: Hertz , clocks: Clocks ) -> Self {
388
- unsafe {
389
- // NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
390
- let rcc = & ( * RCC :: ptr( ) ) ;
391
- <$SPI>:: enable( rcc) ;
392
- }
393
-
394
- Spi { spi, pins } . init( mode, freq, clocks. $pclk( ) )
395
- }
396
405
}
397
406
} ;
398
407
}
@@ -414,7 +423,23 @@ spi! { SPI6: (spi6, pclk2) }
414
423
415
424
impl < SPI , PINS > Spi < SPI , PINS >
416
425
where
417
- SPI : Deref < Target = spi1:: RegisterBlock > ,
426
+ SPI : Instance ,
427
+ PINS : Pins < SPI > ,
428
+ {
429
+ pub fn new ( spi : SPI , pins : PINS , mode : Mode , freq : Hertz , clocks : Clocks ) -> Self {
430
+ unsafe {
431
+ // NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
432
+ let rcc = & ( * RCC :: ptr ( ) ) ;
433
+ SPI :: enable ( rcc) ;
434
+ }
435
+
436
+ Spi { spi, pins } . init ( mode, freq, SPI :: pclk_freq ( & clocks) )
437
+ }
438
+ }
439
+
440
+ impl < SPI , PINS > Spi < SPI , PINS >
441
+ where
442
+ SPI : Instance ,
418
443
{
419
444
pub fn init ( self , mode : Mode , freq : Hertz , clock : Hertz ) -> Self {
420
445
// disable SS output
@@ -527,7 +552,7 @@ where
527
552
528
553
impl < SPI , PINS > spi:: FullDuplex < u8 > for Spi < SPI , PINS >
529
554
where
530
- SPI : Deref < Target = spi1 :: RegisterBlock > ,
555
+ SPI : Instance ,
531
556
{
532
557
type Error = Error ;
533
558
@@ -578,11 +603,11 @@ where
578
603
}
579
604
580
605
impl < SPI , PINS > embedded_hal:: blocking:: spi:: transfer:: Default < u8 > for Spi < SPI , PINS > where
581
- SPI : Deref < Target = spi1 :: RegisterBlock >
606
+ SPI : Instance
582
607
{
583
608
}
584
609
585
610
impl < SPI , PINS > embedded_hal:: blocking:: spi:: write:: Default < u8 > for Spi < SPI , PINS > where
586
- SPI : Deref < Target = spi1 :: RegisterBlock >
611
+ SPI : Instance
587
612
{
588
613
}
0 commit comments