Skip to content

Commit 8f05e23

Browse files
Piroro-hsSh3Rm4n
authored andcommitted
Support I2C3
1 parent 2bcebfe commit 8f05e23

File tree

1 file changed

+87
-20
lines changed

1 file changed

+87
-20
lines changed

src/i2c.rs

Lines changed: 87 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,38 @@
33
use core::convert::TryFrom;
44

55
use crate::{
6-
gpio::{gpioa, gpiob, gpiof, AF4},
6+
gpio::{gpioa, gpiob, AF4},
77
hal::blocking::i2c::{Read, Write, WriteRead},
8-
pac::{I2C1, I2C2, RCC, rcc::cfgr3::I2C1SW_A},
8+
pac::{rcc::cfgr3::I2C1SW_A, I2C1, RCC},
99
rcc::{Clocks, APB1},
1010
time::Hertz,
1111
};
1212

13+
#[cfg(not(any(
14+
feature = "stm32f303x6",
15+
feature = "stm32f303x8",
16+
feature = "stm32f334",
17+
feature = "stm32f328",
18+
)))]
19+
use crate::{gpio::gpiof, pac::I2C2};
20+
#[cfg(any(
21+
feature = "stm32f301",
22+
feature = "stm32f302x6",
23+
feature = "stm32f302x8",
24+
feature = "stm32f302xd",
25+
feature = "stm32f302xe",
26+
feature = "stm32f303xd",
27+
feature = "stm32f303xe",
28+
feature = "stm32f318",
29+
feature = "stm32f398",
30+
))]
31+
use crate::{
32+
gpio::{gpioc, AF3, AF8},
33+
pac::I2C3,
34+
};
35+
36+
use cfg_if::cfg_if;
37+
1338
/// I2C error
1439
#[derive(Debug)]
1540
#[non_exhaustive]
@@ -35,22 +60,48 @@ pub unsafe trait SclPin<I2C> {}
3560
/// SDA pin -- DO NOT IMPLEMENT THIS TRAIT
3661
pub unsafe trait SdaPin<I2C> {}
3762

38-
// unsafe impl SclPin<I2C1> for PA15<AF4> {}
63+
unsafe impl SclPin<I2C1> for gpioa::PA15<AF4> {}
3964
unsafe impl SclPin<I2C1> for gpiob::PB6<AF4> {}
4065
unsafe impl SclPin<I2C1> for gpiob::PB8<AF4> {}
41-
42-
unsafe impl SclPin<I2C2> for gpioa::PA9<AF4> {}
43-
unsafe impl SclPin<I2C2> for gpiof::PF1<AF4> {}
44-
45-
#[cfg(any(feature = "gpio-f303", feature = "gpio-f303e", feature = "gpio-f373"))]
46-
unsafe impl SclPin<I2C2> for gpiof::PF6<AF4> {}
47-
48-
// unsafe impl SdaPin<I2C1> for PA14<AF4> {}
66+
unsafe impl SdaPin<I2C1> for gpioa::PA14<AF4> {}
4967
unsafe impl SdaPin<I2C1> for gpiob::PB7<AF4> {}
5068
unsafe impl SdaPin<I2C1> for gpiob::PB9<AF4> {}
5169

52-
unsafe impl SdaPin<I2C2> for gpioa::PA10<AF4> {}
53-
unsafe impl SdaPin<I2C2> for gpiof::PF0<AF4> {}
70+
cfg_if! {
71+
if #[cfg(not(any(
72+
feature = "stm32f303x6",
73+
feature = "stm32f303x8",
74+
feature = "stm32f334",
75+
feature = "stm32f328",
76+
)))] {
77+
unsafe impl SclPin<I2C2> for gpioa::PA9<AF4> {}
78+
unsafe impl SclPin<I2C2> for gpiof::PF1<AF4> {}
79+
#[cfg(any(feature = "gpio-f303", feature = "gpio-f303e", feature = "gpio-f373"))]
80+
unsafe impl SclPin<I2C2> for gpiof::PF6<AF4> {}
81+
unsafe impl SdaPin<I2C2> for gpioa::PA10<AF4> {}
82+
unsafe impl SdaPin<I2C2> for gpiof::PF0<AF4> {}
83+
#[cfg(feature = "gpio-f373")]
84+
unsafe impl SdaPin<I2C2> for gpiof::PF7<AF4> {}
85+
}
86+
}
87+
88+
cfg_if! {
89+
if #[cfg(any(
90+
feature = "stm32f301",
91+
feature = "stm32f302x6",
92+
feature = "stm32f302x8",
93+
feature = "stm32f302xd",
94+
feature = "stm32f302xe",
95+
feature = "stm32f303xd",
96+
feature = "stm32f303xe",
97+
feature = "stm32f318",
98+
feature = "stm32f398",
99+
))] {
100+
unsafe impl SclPin<I2C3> for gpioa::PA8<AF3> {}
101+
unsafe impl SdaPin<I2C3> for gpiob::PB5<AF8> {}
102+
unsafe impl SdaPin<I2C3> for gpioc::PC9<AF3> {}
103+
}
104+
}
54105

55106
/// I2C peripheral operating in master mode
56107
pub struct I2c<I2C, PINS> {
@@ -429,17 +480,33 @@ macro_rules! i2c {
429480
}
430481

431482
#[cfg(any(
432-
feature = "stm32f301",
433-
feature = "stm32f302",
434-
feature = "stm32f303",
435-
feature = "stm32f318",
483+
feature = "stm32f303x6",
484+
feature = "stm32f303x8",
485+
feature = "stm32f334",
436486
feature = "stm32f328",
487+
))]
488+
i2c!([1]);
489+
490+
#[cfg(any(
491+
feature = "stm32f302xb",
492+
feature = "stm32f302xc",
493+
feature = "stm32f303xb",
494+
feature = "stm32f303xc",
437495
feature = "stm32f358",
438496
feature = "stm32f373",
439497
feature = "stm32f378",
440-
feature = "stm32f398",
441498
))]
442499
i2c!([1, 2]);
443500

444-
#[cfg(feature = "stm32f334")]
445-
i2c!([1]);
501+
#[cfg(any(
502+
feature = "stm32f301",
503+
feature = "stm32f302x6",
504+
feature = "stm32f302x8",
505+
feature = "stm32f302xd",
506+
feature = "stm32f302xe",
507+
feature = "stm32f303xd",
508+
feature = "stm32f303xe",
509+
feature = "stm32f318",
510+
feature = "stm32f398",
511+
))]
512+
i2c!([1, 2, 3]);

0 commit comments

Comments
 (0)