Skip to content

Commit 527f182

Browse files
Implement single pin trait based AF for SPI
1 parent f1f2b32 commit 527f182

File tree

1 file changed

+62
-33
lines changed

1 file changed

+62
-33
lines changed

src/spi.rs

+62-33
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use nb;
44

55
pub use embedded_hal::spi::{Mode, Phase, Polarity};
66

7+
use crate::stm32;
78
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
89
use crate::stm32::{RCC, SPI1};
910

@@ -25,45 +26,65 @@ pub enum Error {
2526
}
2627

2728
/// SPI abstraction
28-
pub struct Spi<SPI, PINS> {
29+
pub struct Spi<SPI, SCKPIN, MISOPIN, MOSIPIN> {
2930
spi: SPI,
30-
pins: PINS,
31+
pins: (SCKPIN, MISOPIN, MOSIPIN),
3132
}
3233

33-
pub trait Pins<Spi> {}
34+
pub trait SckPin<SPI> {}
35+
pub trait MisoPin<SPI> {}
36+
pub trait MosiPin<SPI> {}
37+
38+
macro_rules! spi_pins {
39+
($($SPI:ident => {
40+
sck => [$($sck:ty),+ $(,)*],
41+
miso => [$($miso:ty),+ $(,)*],
42+
mosi => [$($mosi:ty),+ $(,)*],
43+
})+) => {
44+
$(
45+
$(
46+
impl SckPin<stm32::$SPI> for $sck {}
47+
)+
48+
$(
49+
impl MisoPin<stm32::$SPI> for $miso {}
50+
)+
51+
$(
52+
impl MosiPin<stm32::$SPI> for $mosi {}
53+
)+
54+
)+
55+
}
56+
}
3457

3558
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
36-
impl Pins<SPI1>
37-
for (
38-
gpioa::PA5<Alternate<AF0>>,
39-
gpioa::PA6<Alternate<AF0>>,
40-
gpioa::PA7<Alternate<AF0>>,
41-
)
42-
{}
43-
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
44-
impl Pins<SPI1>
45-
for (
46-
gpiob::PB3<Alternate<AF0>>,
47-
gpiob::PB4<Alternate<AF0>>,
48-
gpiob::PB5<Alternate<AF0>>,
49-
)
50-
{}
51-
59+
spi_pins! {
60+
SPI1 => {
61+
sck => [gpioa::PA5<Alternate<AF0>>, gpiob::PB3<Alternate<AF0>>],
62+
miso => [gpioa::PA6<Alternate<AF0>>, gpiob::PB4<Alternate<AF0>>],
63+
mosi => [gpioa::PA7<Alternate<AF0>>, gpiob::PB5<Alternate<AF0>>],
64+
}
65+
}
5266
#[cfg(feature = "stm32f030x6")]
53-
impl Pins<SPI1>
54-
for (
55-
gpiob::PB13<Alternate<AF0>>,
56-
gpiob::PB14<Alternate<AF0>>,
57-
gpiob::PB15<Alternate<AF0>>,
58-
)
59-
{
67+
spi_pins! {
68+
SPI1 => {
69+
sck => [gpiob::PB13<Alternate<AF0>>],
70+
miso => [gpiob::PB14<Alternate<AF0>>],
71+
mosi => [gpiob::PB15<Alternate<AF0>>],
72+
}
6073
}
6174

6275
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
63-
impl<PINS> Spi<SPI1, PINS> {
64-
pub fn spi1<F>(spi: SPI1, pins: PINS, mode: Mode, speed: F, clocks: Clocks) -> Self
76+
impl<SCKPIN, MISOPIN, MOSIPIN> Spi<SPI1, SCKPIN, MISOPIN, MOSIPIN> {
77+
pub fn spi1<F>(
78+
spi: SPI1,
79+
pins: (SCKPIN, MISOPIN, MOSIPIN),
80+
mode: Mode,
81+
speed: F,
82+
clocks: Clocks,
83+
) -> Self
6584
where
66-
PINS: Pins<SPI1>,
85+
SCKPIN: SckPin<SPI1>,
86+
MISOPIN: MisoPin<SPI1>,
87+
MOSIPIN: MosiPin<SPI1>,
6788
F: Into<Hertz>,
6889
{
6990
// NOTE(unsafe) This executes only during initialisation
@@ -133,13 +154,15 @@ impl<PINS> Spi<SPI1, PINS> {
133154
Spi { spi, pins }
134155
}
135156

136-
pub fn release(self) -> (SPI1, PINS) {
157+
pub fn release(self) -> (SPI1, (SCKPIN, MISOPIN, MOSIPIN)) {
137158
(self.spi, self.pins)
138159
}
139160
}
140161

141162
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
142-
impl<PINS> ::embedded_hal::spi::FullDuplex<u8> for Spi<SPI1, PINS> {
163+
impl<SCKPIN, MISOPIN, MOSIPIN> ::embedded_hal::spi::FullDuplex<u8>
164+
for Spi<SPI1, SCKPIN, MISOPIN, MOSIPIN>
165+
{
143166
type Error = Error;
144167

145168
fn read(&mut self) -> nb::Result<u8, Error> {
@@ -180,6 +203,12 @@ impl<PINS> ::embedded_hal::spi::FullDuplex<u8> for Spi<SPI1, PINS> {
180203
}
181204

182205
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
183-
impl<PINS> ::embedded_hal::blocking::spi::transfer::Default<u8> for Spi<SPI1, PINS> {}
206+
impl<SCKPIN, MISOPIN, MOSIPIN> ::embedded_hal::blocking::spi::transfer::Default<u8>
207+
for Spi<SPI1, SCKPIN, MISOPIN, MOSIPIN>
208+
{
209+
}
184210
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
185-
impl<PINS> ::embedded_hal::blocking::spi::write::Default<u8> for Spi<SPI1, PINS> {}
211+
impl<SCKPIN, MISOPIN, MOSIPIN> ::embedded_hal::blocking::spi::write::Default<u8>
212+
for Spi<SPI1, SCKPIN, MISOPIN, MOSIPIN>
213+
{
214+
}

0 commit comments

Comments
 (0)