Skip to content

Commit 25724cb

Browse files
committed
Spi Instance
1 parent ba3acf1 commit 25724cb

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4141

4242
### Changed
4343

44-
- Add `Spi::new` and deprecate `Spi:spix`, deprecate `Serial::usartx`, remove deprecated `I2c::i2cx`
44+
- Add `Spi::new`, `spi::Instance` and deprecate `Spi:spix`, deprecate `Serial::usartx`, remove deprecated `I2c::i2cx`
4545
- Deprecate `free` in favour of `release`
4646
- Clean features in `serial`, `spi`, `i2c`, `timer`
4747
- Internal implementation of GPIO Pin API changed to use Const Generics

examples/analog-stopwatch-with-spi-ssd1306.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn main() -> ! {
9898
let miso = gpioe.pe5.into_alternate();
9999
let mosi = gpioe.pe6.into_alternate();
100100

101-
let spi = Spi::spi4(
101+
let spi = Spi::new(
102102
dp.SPI4,
103103
(sck, miso, mosi),
104104
Mode {

src/spi.rs

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -374,25 +374,34 @@ pub struct Spi<SPI, PINS> {
374374
pins: PINS,
375375
}
376376

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
378388
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+
380397
impl<PINS> Spi<$SPI, PINS>
381398
where
382399
PINS: Pins<$SPI>,
383400
{
401+
#[deprecated(since = "0.10.0", note = "Please use new instead")]
384402
pub fn $spi(spi: $SPI, pins: PINS, mode: Mode, freq: Hertz, clocks: Clocks) -> Self {
385403
Self::new(spi, pins, mode, freq, clocks)
386404
}
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-
}
396405
}
397406
};
398407
}
@@ -414,7 +423,23 @@ spi! { SPI6: (spi6, pclk2) }
414423

415424
impl<SPI, PINS> Spi<SPI, PINS>
416425
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,
418443
{
419444
pub fn init(self, mode: Mode, freq: Hertz, clock: Hertz) -> Self {
420445
// disable SS output
@@ -527,7 +552,7 @@ where
527552

528553
impl<SPI, PINS> spi::FullDuplex<u8> for Spi<SPI, PINS>
529554
where
530-
SPI: Deref<Target = spi1::RegisterBlock>,
555+
SPI: Instance,
531556
{
532557
type Error = Error;
533558

@@ -578,11 +603,11 @@ where
578603
}
579604

580605
impl<SPI, PINS> embedded_hal::blocking::spi::transfer::Default<u8> for Spi<SPI, PINS> where
581-
SPI: Deref<Target = spi1::RegisterBlock>
606+
SPI: Instance
582607
{
583608
}
584609

585610
impl<SPI, PINS> embedded_hal::blocking::spi::write::Default<u8> for Spi<SPI, PINS> where
586-
SPI: Deref<Target = spi1::RegisterBlock>
611+
SPI: Instance
587612
{
588613
}

0 commit comments

Comments
 (0)