Skip to content

Commit c4183cd

Browse files
committed
Update I2C pin mappings based on STM32L0xx GPIO IP
The GPIO internal peripheral (IP) does not match the stm32l0x/stm32l0x2/stm32l0x3 grouping. Instead, there are 4 different GPIO IPs in the STM32L0 family that define how the pin functions are mapped to the actual pins. This means that the current I2C mappings are broken for a lot of MCUs. For reference, these issues have already been opened because the mappings were broken: - #76 - #77 - #85 We can use the `io-*` Cargo features to differentiate between these mappings. This requires that the proper `io-*` feature is set when compiling (and is thus a breaking change). The easiest way to apply the feature without looking at the STM32CubeMX database XML files is to simply use the proper `mcu-*` feature, for example `mcu-STM32L071KBTx`.
1 parent 6700dbc commit c4183cd

File tree

1 file changed

+88
-35
lines changed

1 file changed

+88
-35
lines changed

src/i2c.rs

Lines changed: 88 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,49 @@ use crate::dma::{
2222
self,
2323
Buffer
2424
};
25-
use crate::gpio::gpioa::{PA10, PA9};
26-
use crate::gpio::gpiob::{PB6, PB7};
27-
use crate::gpio::{AltMode, OpenDrain, Output};
2825
use crate::pac::{
2926
i2c1::{
3027
RegisterBlock,
3128
cr2::RD_WRN_A,
3229
}
3330
};
34-
pub use crate::pac::I2C1;
3531
use crate::rcc::Rcc;
3632
use crate::time::Hertz;
3733
use cast::u8;
3834

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

5270
/// I2C abstraction
@@ -488,32 +506,71 @@ macro_rules! i2c {
488506
};
489507
}
490508

491-
#[cfg(feature = "stm32l0x1")]
509+
510+
#[cfg(feature = "io-STM32L021")]
492511
i2c!(
493-
I2C1,
494-
i2c1en,
495-
i2c1rst,
512+
I2C1, i2c1en, i2c1rst,
496513
sda: [
497-
(PB7<Output<OpenDrain>>, AltMode::AF1),
498-
(PA10<Output<OpenDrain>>, AltMode::AF6),
514+
(PA10<Output<OpenDrain>>, AltMode::AF1),
499515
(PA13<Output<OpenDrain>>, AltMode::AF3),
516+
(PB7<Output<OpenDrain>>, AltMode::AF1),
500517
],
501518
scl: [
502-
(PB6<Output<OpenDrain>>, AltMode::AF1),
503-
(PA9<Output<OpenDrain>>, AltMode::AF6),
504519
(PA4<Output<OpenDrain>>, AltMode::AF3),
520+
(PA9<Output<OpenDrain>>, AltMode::AF1),
521+
(PB6<Output<OpenDrain>>, AltMode::AF1),
522+
(PB8<Output<OpenDrain>>, AltMode::AF4),
523+
],
524+
);
525+
526+
#[cfg(feature = "io-STM32L031")]
527+
i2c!(
528+
I2C1, i2c1en, i2c1rst,
529+
sda: [
530+
(PA10<Output<OpenDrain>>, AltMode::AF1),
531+
(PB7<Output<OpenDrain>>, AltMode::AF1),
532+
(PB9<Output<OpenDrain>>, AltMode::AF4),
533+
],
534+
scl: [
535+
(PA9<Output<OpenDrain>>, AltMode::AF1),
536+
(PB6<Output<OpenDrain>>, AltMode::AF1),
537+
(PB8<Output<OpenDrain>>, AltMode::AF4),
538+
],
539+
);
540+
541+
#[cfg(feature = "io-STM32L051")]
542+
i2c!(
543+
I2C1, i2c1en, i2c1rst,
544+
sda: [
545+
(PB7<Output<OpenDrain>>, AltMode::AF1),
546+
(PB9<Output<OpenDrain>>, AltMode::AF4),
547+
],
548+
scl: [
549+
(PB6<Output<OpenDrain>>, AltMode::AF1),
550+
(PB8<Output<OpenDrain>>, AltMode::AF4),
551+
],
552+
);
553+
554+
#[cfg(feature = "io-STM32L051")]
555+
i2c!(
556+
I2C2, i2c2en, i2c2rst,
557+
sda: [
558+
(PB11<Output<OpenDrain>>, AltMode::AF6),
559+
(PB14<Output<OpenDrain>>, AltMode::AF5),
560+
],
561+
scl: [
562+
(PB10<Output<OpenDrain>>, AltMode::AF6),
563+
(PB13<Output<OpenDrain>>, AltMode::AF5),
505564
],
506565
);
507566

508-
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
567+
#[cfg(feature = "io-STM32L071")]
509568
i2c!(
510-
I2C1,
511-
i2c1en,
512-
i2c1rst,
569+
I2C1, i2c1en, i2c1rst,
513570
sda: [
514571
(PA10<Output<OpenDrain>>, AltMode::AF6),
515-
(PB7<Output<OpenDrain>>, AltMode::AF1),
516-
(PB9<Output<OpenDrain>>, AltMode::AF4),
572+
(PB7<Output<OpenDrain>>, AltMode::AF1),
573+
(PB9<Output<OpenDrain>>, AltMode::AF4),
517574
],
518575
scl: [
519576
(PA9<Output<OpenDrain>>, AltMode::AF6),
@@ -522,11 +579,9 @@ i2c!(
522579
],
523580
);
524581

525-
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
582+
#[cfg(feature = "io-STM32L071")]
526583
i2c!(
527-
I2C2,
528-
i2c2en,
529-
i2c2rst,
584+
I2C2, i2c2en, i2c2rst,
530585
sda: [
531586
(PB11<Output<OpenDrain>>, AltMode::AF6),
532587
(PB14<Output<OpenDrain>>, AltMode::AF5),
@@ -537,22 +592,20 @@ i2c!(
537592
],
538593
);
539594

540-
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
595+
#[cfg(feature = "io-STM32L071")]
541596
i2c!(
542-
I2C3,
543-
i2c3en,
544-
i2c3rst,
597+
I2C3, i2c3en, i2c3rst,
545598
sda: [
546599
(PB4<Output<OpenDrain>>, AltMode::AF7),
547600
(PC1<Output<OpenDrain>>, AltMode::AF7),
601+
(PC9<Output<OpenDrain>>, AltMode::AF7),
548602
],
549603
scl: [
550604
(PA8<Output<OpenDrain>>, AltMode::AF7),
551605
(PC0<Output<OpenDrain>>, AltMode::AF7),
552606
],
553607
);
554608

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

0 commit comments

Comments
 (0)