Skip to content

Commit

Permalink
Update I2C pin mappings based on STM32L0xx GPIO IP
Browse files Browse the repository at this point in the history
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:

- stm32-rs#76
- stm32-rs#77
- stm32-rs#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`.
  • Loading branch information
dbrgn committed Mar 28, 2020
1 parent 6700dbc commit c4183cd
Showing 1 changed file with 88 additions and 35 deletions.
123 changes: 88 additions & 35 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,49 @@ use crate::dma::{
self,
Buffer
};
use crate::gpio::gpioa::{PA10, PA9};
use crate::gpio::gpiob::{PB6, PB7};
use crate::gpio::{AltMode, OpenDrain, Output};
use crate::pac::{
i2c1::{
RegisterBlock,
cr2::RD_WRN_A,
}
};
pub use crate::pac::I2C1;
use crate::rcc::Rcc;
use crate::time::Hertz;
use cast::u8;

#[cfg(feature = "stm32l0x1")]
use crate::gpio::gpioa::{PA13, PA4};

#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
// I/O Imports
use crate::gpio::{AltMode, OpenDrain, Output};
#[cfg(feature = "io-STM32L021")]
use crate::{
gpio::{
gpioa::{PA4, PA9, PA10, PA13},
gpiob::{PB6, PB7, PB8},
},
pac::I2C1,
};
#[cfg(feature = "io-STM32L031")]
use crate::{
gpio::{
gpioa::{PA9, PA10},
gpiob::{PB6, PB7, PB8, PB9},
},
pac::I2C1,
};
#[cfg(feature = "io-STM32L051")]
use crate::{
gpio::{
gpiob::{PB6, PB7, PB8, PB9, PB10, PB11, PB13, PB14},
},
pac::{I2C1, I2C2},
};
#[cfg(feature = "io-STM32L071")]
use crate::{
gpio::{
gpioa::PA8,
gpiob::{PB10, PB11, PB13, PB14, PB4, PB8, PB9},
gpioc::{PC0, PC1},
gpioa::{PA8, PA9, PA10},
gpiob::{PB4, PB6, PB7, PB8, PB9, PB10, PB11, PB13, PB14},
gpioc::{PC0, PC1, PC9},
},
pac::{I2C2, I2C3},
pac::{I2C1, I2C2, I2C3},
};

/// I2C abstraction
Expand Down Expand Up @@ -488,32 +506,71 @@ macro_rules! i2c {
};
}

#[cfg(feature = "stm32l0x1")]

#[cfg(feature = "io-STM32L021")]
i2c!(
I2C1,
i2c1en,
i2c1rst,
I2C1, i2c1en, i2c1rst,
sda: [
(PB7<Output<OpenDrain>>, AltMode::AF1),
(PA10<Output<OpenDrain>>, AltMode::AF6),
(PA10<Output<OpenDrain>>, AltMode::AF1),
(PA13<Output<OpenDrain>>, AltMode::AF3),
(PB7<Output<OpenDrain>>, AltMode::AF1),
],
scl: [
(PB6<Output<OpenDrain>>, AltMode::AF1),
(PA9<Output<OpenDrain>>, AltMode::AF6),
(PA4<Output<OpenDrain>>, AltMode::AF3),
(PA9<Output<OpenDrain>>, AltMode::AF1),
(PB6<Output<OpenDrain>>, AltMode::AF1),
(PB8<Output<OpenDrain>>, AltMode::AF4),
],
);

#[cfg(feature = "io-STM32L031")]
i2c!(
I2C1, i2c1en, i2c1rst,
sda: [
(PA10<Output<OpenDrain>>, AltMode::AF1),
(PB7<Output<OpenDrain>>, AltMode::AF1),
(PB9<Output<OpenDrain>>, AltMode::AF4),
],
scl: [
(PA9<Output<OpenDrain>>, AltMode::AF1),
(PB6<Output<OpenDrain>>, AltMode::AF1),
(PB8<Output<OpenDrain>>, AltMode::AF4),
],
);

#[cfg(feature = "io-STM32L051")]
i2c!(
I2C1, i2c1en, i2c1rst,
sda: [
(PB7<Output<OpenDrain>>, AltMode::AF1),
(PB9<Output<OpenDrain>>, AltMode::AF4),
],
scl: [
(PB6<Output<OpenDrain>>, AltMode::AF1),
(PB8<Output<OpenDrain>>, AltMode::AF4),
],
);

#[cfg(feature = "io-STM32L051")]
i2c!(
I2C2, i2c2en, i2c2rst,
sda: [
(PB11<Output<OpenDrain>>, AltMode::AF6),
(PB14<Output<OpenDrain>>, AltMode::AF5),
],
scl: [
(PB10<Output<OpenDrain>>, AltMode::AF6),
(PB13<Output<OpenDrain>>, AltMode::AF5),
],
);

#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
#[cfg(feature = "io-STM32L071")]
i2c!(
I2C1,
i2c1en,
i2c1rst,
I2C1, i2c1en, i2c1rst,
sda: [
(PA10<Output<OpenDrain>>, AltMode::AF6),
(PB7<Output<OpenDrain>>, AltMode::AF1),
(PB9<Output<OpenDrain>>, AltMode::AF4),
(PB7<Output<OpenDrain>>, AltMode::AF1),
(PB9<Output<OpenDrain>>, AltMode::AF4),
],
scl: [
(PA9<Output<OpenDrain>>, AltMode::AF6),
Expand All @@ -522,11 +579,9 @@ i2c!(
],
);

#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
#[cfg(feature = "io-STM32L071")]
i2c!(
I2C2,
i2c2en,
i2c2rst,
I2C2, i2c2en, i2c2rst,
sda: [
(PB11<Output<OpenDrain>>, AltMode::AF6),
(PB14<Output<OpenDrain>>, AltMode::AF5),
Expand All @@ -537,22 +592,20 @@ i2c!(
],
);

#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
#[cfg(feature = "io-STM32L071")]
i2c!(
I2C3,
i2c3en,
i2c3rst,
I2C3, i2c3en, i2c3rst,
sda: [
(PB4<Output<OpenDrain>>, AltMode::AF7),
(PC1<Output<OpenDrain>>, AltMode::AF7),
(PC9<Output<OpenDrain>>, AltMode::AF7),
],
scl: [
(PA8<Output<OpenDrain>>, AltMode::AF7),
(PC0<Output<OpenDrain>>, AltMode::AF7),
],
);


/// Token used for DMA transfers
///
/// This is an implementation detail. The user doesn't have to deal with this
Expand Down

0 comments on commit c4183cd

Please sign in to comment.