Skip to content

Commit c18eb55

Browse files
authored
Merge pull request #87 from gfroerli/i2c-correct-mappings
Update I2C pin mappings based on STM32L0xx GPIO IP
2 parents 4ed1629 + a2dd816 commit c18eb55

File tree

6 files changed

+123
-51
lines changed

6 files changed

+123
-51
lines changed

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ stm32l082 = ["stm32l0x2"]
6565

6666
# Features based on the GPIO peripheral version.
6767
# This determines the pin function mapping of the MCU.
68+
# Note: The easiest way to pick the proper io-* feature is to apply
69+
# the matching mcu-* feature for your MCU!
6870
io-STM32L021 = []
6971
io-STM32L031 = []
7072
io-STM32L051 = []
@@ -302,9 +304,13 @@ required-features = ["rt"]
302304
name = "flash"
303305
required-features = ["rt","stm32l082"]
304306

307+
[[example]]
308+
name = "i2c"
309+
required-features = ["rt","stm32l0x2","io-STM32L071"]
310+
305311
[[example]]
306312
name = "i2c_dma"
307-
required-features = ["rt","stm32l0x2"]
313+
required-features = ["rt","stm32l0x2","io-STM32L071"]
308314

309315
[[example]]
310316
name = "rng"

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn main() {
2222
}
2323

2424
if feature_count != 1 {
25-
panic!("\n\nMust select exactly one package for linker script generation!\nChoices: 'stm32l0x1' or 'stm32l0x2' or 'stm32l0x3'\n\n");
25+
panic!("\n\nMust select exactly one package for linker script generation!\nChoices: 'stm32l0x1' or 'stm32l0x2' or 'stm32l0x3'\nAlternatively, pick the mcu-feature that matches your MCU, for example 'mcu-STM32L071KBTx'\n\n");
2626
}
2727

2828
if !cfg!(feature = "disable-linker-script") {

src/dma.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@ use as_slice::AsSlice;
2828

2929
use crate::{
3030
adc,
31-
i2c,
3231
pac::{
3332
self,
3433
dma1::ch::cr,
35-
I2C1,
36-
I2C2,
37-
I2C3,
3834
},
3935
rcc::Rcc,
4036
};
@@ -48,15 +44,11 @@ use crate::pac::USART1;
4844
feature = "io-STM32L051",
4945
feature = "io-STM32L071",
5046
))]
51-
use crate::pac::USART2;
52-
53-
#[cfg(any(
54-
feature = "io-STM32L021",
55-
feature = "io-STM32L031",
56-
feature = "io-STM32L051",
57-
feature = "io-STM32L071",
58-
))]
59-
use crate::serial;
47+
use crate::{
48+
i2c,
49+
serial,
50+
pac::{I2C1, I2C2, I2C3, USART2},
51+
};
6052

6153
#[cfg(feature = "stm32l082")]
6254
use crate::aes;
@@ -563,6 +555,12 @@ impl_target!(
563555
);
564556

565557
#[cfg(feature = "stm32l0x2")]
558+
#[cfg(any(
559+
feature = "io-STM32L021",
560+
feature = "io-STM32L031",
561+
feature = "io-STM32L051",
562+
feature = "io-STM32L071",
563+
))]
566564
impl_target!(
567565
// I2C1
568566
i2c::Tx<I2C1>, Channel2, 6;

src/i2c.rs

Lines changed: 90 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,58 @@ use as_slice::{
1515
AsMutSlice,
1616
};
1717

18-
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
1918

2019
#[cfg(feature = "stm32l0x2")]
2120
use crate::dma::{
2221
self,
2322
Buffer
2423
};
25-
use crate::gpio::gpioa::{PA10, PA9};
26-
use crate::gpio::gpiob::{PB6, PB7};
27-
use crate::gpio::{AltMode, OpenDrain, Output};
2824
use crate::pac::{
2925
i2c1::{
3026
RegisterBlock,
3127
cr2::RD_WRN_A,
3228
}
3329
};
34-
pub use crate::pac::I2C1;
3530
use crate::rcc::Rcc;
3631
use crate::time::Hertz;
3732
use cast::u8;
3833

39-
#[cfg(feature = "stm32l0x1")]
40-
use crate::gpio::gpioa::{PA13, PA4};
34+
// I²C traits
35+
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
4136

42-
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
37+
// I/O Imports
38+
use crate::gpio::{AltMode, OpenDrain, Output};
39+
#[cfg(feature = "io-STM32L021")]
40+
use crate::{
41+
gpio::{
42+
gpioa::{PA4, PA9, PA10, PA13},
43+
gpiob::{PB6, PB7, PB8},
44+
},
45+
pac::I2C1,
46+
};
47+
#[cfg(feature = "io-STM32L031")]
4348
use crate::{
4449
gpio::{
45-
gpioa::PA8,
46-
gpiob::{PB10, PB11, PB13, PB14, PB4, PB8, PB9},
47-
gpioc::{PC0, PC1},
50+
gpioa::{PA9, PA10},
51+
gpiob::{PB6, PB7, PB8, PB9},
4852
},
49-
pac::{I2C2, I2C3},
53+
pac::I2C1,
54+
};
55+
#[cfg(feature = "io-STM32L051")]
56+
use crate::{
57+
gpio::{
58+
gpiob::{PB6, PB7, PB8, PB9, PB10, PB11, PB13, PB14},
59+
},
60+
pac::{I2C1, I2C2},
61+
};
62+
#[cfg(feature = "io-STM32L071")]
63+
use crate::{
64+
gpio::{
65+
gpioa::{PA8, PA9, PA10},
66+
gpiob::{PB4, PB6, PB7, PB8, PB9, PB10, PB11, PB13, PB14},
67+
gpioc::{PC0, PC1, PC9},
68+
},
69+
pac::{I2C1, I2C2, I2C3},
5070
};
5171

5272
/// I2C abstraction
@@ -488,32 +508,71 @@ macro_rules! i2c {
488508
};
489509
}
490510

491-
#[cfg(feature = "stm32l0x1")]
511+
512+
#[cfg(feature = "io-STM32L021")]
492513
i2c!(
493-
I2C1,
494-
i2c1en,
495-
i2c1rst,
514+
I2C1, i2c1en, i2c1rst,
496515
sda: [
497-
(PB7<Output<OpenDrain>>, AltMode::AF1),
498-
(PA10<Output<OpenDrain>>, AltMode::AF6),
516+
(PA10<Output<OpenDrain>>, AltMode::AF1),
499517
(PA13<Output<OpenDrain>>, AltMode::AF3),
518+
(PB7<Output<OpenDrain>>, AltMode::AF1),
500519
],
501520
scl: [
502-
(PB6<Output<OpenDrain>>, AltMode::AF1),
503-
(PA9<Output<OpenDrain>>, AltMode::AF6),
504521
(PA4<Output<OpenDrain>>, AltMode::AF3),
522+
(PA9<Output<OpenDrain>>, AltMode::AF1),
523+
(PB6<Output<OpenDrain>>, AltMode::AF1),
524+
(PB8<Output<OpenDrain>>, AltMode::AF4),
505525
],
506526
);
507527

508-
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
528+
#[cfg(feature = "io-STM32L031")]
509529
i2c!(
510-
I2C1,
511-
i2c1en,
512-
i2c1rst,
530+
I2C1, i2c1en, i2c1rst,
531+
sda: [
532+
(PA10<Output<OpenDrain>>, AltMode::AF1),
533+
(PB7<Output<OpenDrain>>, AltMode::AF1),
534+
(PB9<Output<OpenDrain>>, AltMode::AF4),
535+
],
536+
scl: [
537+
(PA9<Output<OpenDrain>>, AltMode::AF1),
538+
(PB6<Output<OpenDrain>>, AltMode::AF1),
539+
(PB8<Output<OpenDrain>>, AltMode::AF4),
540+
],
541+
);
542+
543+
#[cfg(feature = "io-STM32L051")]
544+
i2c!(
545+
I2C1, i2c1en, i2c1rst,
546+
sda: [
547+
(PB7<Output<OpenDrain>>, AltMode::AF1),
548+
(PB9<Output<OpenDrain>>, AltMode::AF4),
549+
],
550+
scl: [
551+
(PB6<Output<OpenDrain>>, AltMode::AF1),
552+
(PB8<Output<OpenDrain>>, AltMode::AF4),
553+
],
554+
);
555+
556+
#[cfg(feature = "io-STM32L051")]
557+
i2c!(
558+
I2C2, i2c2en, i2c2rst,
559+
sda: [
560+
(PB11<Output<OpenDrain>>, AltMode::AF6),
561+
(PB14<Output<OpenDrain>>, AltMode::AF5),
562+
],
563+
scl: [
564+
(PB10<Output<OpenDrain>>, AltMode::AF6),
565+
(PB13<Output<OpenDrain>>, AltMode::AF5),
566+
],
567+
);
568+
569+
#[cfg(feature = "io-STM32L071")]
570+
i2c!(
571+
I2C1, i2c1en, i2c1rst,
513572
sda: [
514573
(PA10<Output<OpenDrain>>, AltMode::AF6),
515-
(PB7<Output<OpenDrain>>, AltMode::AF1),
516-
(PB9<Output<OpenDrain>>, AltMode::AF4),
574+
(PB7<Output<OpenDrain>>, AltMode::AF1),
575+
(PB9<Output<OpenDrain>>, AltMode::AF4),
517576
],
518577
scl: [
519578
(PA9<Output<OpenDrain>>, AltMode::AF6),
@@ -522,11 +581,9 @@ i2c!(
522581
],
523582
);
524583

525-
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
584+
#[cfg(feature = "io-STM32L071")]
526585
i2c!(
527-
I2C2,
528-
i2c2en,
529-
i2c2rst,
586+
I2C2, i2c2en, i2c2rst,
530587
sda: [
531588
(PB11<Output<OpenDrain>>, AltMode::AF6),
532589
(PB14<Output<OpenDrain>>, AltMode::AF5),
@@ -537,22 +594,20 @@ i2c!(
537594
],
538595
);
539596

540-
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
597+
#[cfg(feature = "io-STM32L071")]
541598
i2c!(
542-
I2C3,
543-
i2c3en,
544-
i2c3rst,
599+
I2C3, i2c3en, i2c3rst,
545600
sda: [
546601
(PB4<Output<OpenDrain>>, AltMode::AF7),
547602
(PC1<Output<OpenDrain>>, AltMode::AF7),
603+
(PC9<Output<OpenDrain>>, AltMode::AF7),
548604
],
549605
scl: [
550606
(PA8<Output<OpenDrain>>, AltMode::AF7),
551607
(PC0<Output<OpenDrain>>, AltMode::AF7),
552608
],
553609
);
554610

555-
556611
/// Token used for DMA transfers
557612
///
558613
/// This is an implementation detail. The user doesn't have to deal with this

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ pub mod exti;
2323
#[cfg(feature = "stm32l0x2")]
2424
pub mod flash;
2525
pub mod gpio;
26+
#[cfg(any(
27+
feature = "io-STM32L021",
28+
feature = "io-STM32L031",
29+
feature = "io-STM32L051",
30+
feature = "io-STM32L071",
31+
))]
2632
pub mod i2c;
2733
pub mod lptim;
2834
pub mod prelude;

src/prelude.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub use crate::{
1313
adc::AdcExt as _,
1414
delay::DelayExt as _,
1515
gpio::GpioExt as _,
16-
i2c::I2cExt as _,
1716
pwr::PowerMode as _,
1817
rcc::RccExt as _,
1918
spi::SpiExt as _,
@@ -25,6 +24,14 @@ pub use crate::{
2524
},
2625
};
2726

27+
#[cfg(any(
28+
feature = "io-STM32L021",
29+
feature = "io-STM32L031",
30+
feature = "io-STM32L051",
31+
feature = "io-STM32L071",
32+
))]
33+
pub use crate::i2c::I2cExt as _;
34+
2835
#[cfg(any(
2936
feature = "io-STM32L021",
3037
feature = "io-STM32L031",

0 commit comments

Comments
 (0)