Skip to content

Commit e1bced0

Browse files
Implement single pin trait based AF for I2C
1 parent 27d0137 commit e1bced0

File tree

1 file changed

+53
-20
lines changed

1 file changed

+53
-20
lines changed

src/i2c.rs

+53-20
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,70 @@
11
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
22
use crate::stm32::{I2C1, RCC};
33

4+
use crate::stm32;
45
use embedded_hal::blocking::i2c::{Write, WriteRead};
56

67
use crate::gpio::*;
78
use crate::time::{KiloHertz, U32Ext};
89
use core::cmp;
910

1011
/// I2C abstraction
11-
pub struct I2c<I2C, PINS> {
12+
pub struct I2c<I2C, SCLPIN, SDAPIN> {
1213
i2c: I2C,
13-
pins: PINS,
14+
pins: (SCLPIN, SDAPIN),
1415
}
1516

16-
pub trait Pins<I2c> {}
17+
pub trait SclPin<I2C> {}
18+
pub trait SdaPin<I2C> {}
19+
20+
macro_rules! i2c_pins {
21+
($($I2C:ident => {
22+
scl => [$($scl:ty),+ $(,)*],
23+
sda => [$($sda:ty),+ $(,)*],
24+
})+) => {
25+
$(
26+
$(
27+
impl SclPin<stm32::$I2C> for $scl {}
28+
)+
29+
$(
30+
impl SdaPin<stm32::$I2C> for $sda {}
31+
)+
32+
)+
33+
}
34+
}
1735

36+
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
37+
i2c_pins! {
38+
I2C1 => {
39+
scl => [gpioa::PA11<Alternate<AF5>>, gpiob::PB6<Alternate<AF1>>, gpiob::PB8<Alternate<AF1>>],
40+
sda => [gpioa::PA12<Alternate<AF5>>, gpiob::PB7<Alternate<AF1>>, gpiob::PB9<Alternate<AF1>>],
41+
}
42+
}
1843
#[cfg(any(
1944
feature = "stm32f042",
2045
feature = "stm32f030x6",
2146
feature = "stm32f030xc"
2247
))]
23-
impl Pins<I2C1> for (gpioa::PA9<Alternate<AF4>>, gpioa::PA10<Alternate<AF4>>) {}
24-
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
25-
impl Pins<I2C1> for (gpioa::PA11<Alternate<AF5>>, gpioa::PA12<Alternate<AF5>>) {}
26-
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
27-
impl Pins<I2C1> for (gpiob::PB6<Alternate<AF1>>, gpiob::PB7<Alternate<AF1>>) {}
28-
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
29-
impl Pins<I2C1> for (gpiob::PB8<Alternate<AF1>>, gpiob::PB9<Alternate<AF1>>) {}
48+
i2c_pins! {
49+
I2C1 => {
50+
scl => [gpioa::PA9<Alternate<AF4>>],
51+
sda => [gpioa::PA10<Alternate<AF4>>],
52+
}
53+
}
3054
#[cfg(any(feature = "stm32f042", feature = "stm32f030x6"))]
31-
impl Pins<I2C1> for (gpiob::PB10<Alternate<AF1>>, gpiob::PB11<Alternate<AF1>>) {}
32-
#[cfg(any(feature = "stm32f042", feature = "stm32f030xc"))]
33-
impl Pins<I2C1> for (gpiob::PB13<Alternate<AF5>>, gpiob::PB14<Alternate<AF5>>) {}
55+
i2c_pins! {
56+
I2C1 => {
57+
scl => [gpiob::PB10<Alternate<AF1>>],
58+
sda => [gpiob::PB11<Alternate<AF1>>],
59+
}
60+
}
3461
#[cfg(any(feature = "stm32f042", feature = "stm32f030xc"))]
35-
impl Pins<I2C1> for (gpiof::PF1<Alternate<AF1>>, gpiof::PF0<Alternate<AF1>>) {}
62+
i2c_pins! {
63+
I2C1 => {
64+
scl => [gpiob::PB13<Alternate<AF5>>, gpiof::PF1<Alternate<AF1>>],
65+
sda => [gpiob::PB14<Alternate<AF5>>, gpiof::PF0<Alternate<AF1>>],
66+
}
67+
}
3668

3769
#[derive(Debug)]
3870
pub enum Error {
@@ -41,10 +73,11 @@ pub enum Error {
4173
}
4274

4375
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
44-
impl<PINS> I2c<I2C1, PINS> {
45-
pub fn i2c1(i2c: I2C1, pins: PINS, speed: KiloHertz) -> Self
76+
impl<SCLPIN, SDAPIN> I2c<I2C1, SCLPIN, SDAPIN> {
77+
pub fn i2c1(i2c: I2C1, pins: (SCLPIN, SDAPIN), speed: KiloHertz) -> Self
4678
where
47-
PINS: Pins<I2C1>,
79+
SCLPIN: SclPin<I2C1>,
80+
SDAPIN: SdaPin<I2C1>,
4881
{
4982
// NOTE(unsafe) This executes only during initialisation
5083
let rcc = unsafe { &(*RCC::ptr()) };
@@ -104,7 +137,7 @@ impl<PINS> I2c<I2C1, PINS> {
104137
I2c { i2c, pins }
105138
}
106139

107-
pub fn release(self) -> (I2C1, PINS) {
140+
pub fn release(self) -> (I2C1, (SCLPIN, SDAPIN)) {
108141
(self.i2c, self.pins)
109142
}
110143

@@ -134,7 +167,7 @@ impl<PINS> I2c<I2C1, PINS> {
134167
}
135168

136169
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
137-
impl<PINS> WriteRead for I2c<I2C1, PINS> {
170+
impl<SCLPIN, SDAPIN> WriteRead for I2c<I2C1, SCLPIN, SDAPIN> {
138171
type Error = Error;
139172

140173
fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> {
@@ -212,7 +245,7 @@ impl<PINS> WriteRead for I2c<I2C1, PINS> {
212245
}
213246

214247
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
215-
impl<PINS> Write for I2c<I2C1, PINS> {
248+
impl<SCLPIN, SDAPIN> Write for I2c<I2C1, SCLPIN, SDAPIN> {
216249
type Error = Error;
217250

218251
fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> {

0 commit comments

Comments
 (0)