Skip to content

Update I2C pin mappings based on STM32L0xx GPIO IP #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ stm32l082 = ["stm32l0x2"]

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

[[example]]
name = "i2c"
required-features = ["rt","stm32l0x2","io-STM32L071"]

[[example]]
name = "i2c_dma"
required-features = ["rt","stm32l0x2"]
required-features = ["rt","stm32l0x2","io-STM32L071"]

[[example]]
name = "rng"
Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
}

if feature_count != 1 {
panic!("\n\nMust select exactly one package for linker script generation!\nChoices: 'stm32l0x1' or 'stm32l0x2' or 'stm32l0x3'\n\n");
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");
}

if !cfg!(feature = "disable-linker-script") {
Expand Down
24 changes: 11 additions & 13 deletions src/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ use as_slice::AsSlice;

use crate::{
adc,
i2c,
pac::{
self,
dma1::ch::cr,
I2C1,
I2C2,
I2C3,
},
rcc::Rcc,
};
Expand All @@ -48,15 +44,11 @@ use crate::pac::USART1;
feature = "io-STM32L051",
feature = "io-STM32L071",
))]
use crate::pac::USART2;

#[cfg(any(
feature = "io-STM32L021",
feature = "io-STM32L031",
feature = "io-STM32L051",
feature = "io-STM32L071",
))]
use crate::serial;
use crate::{
i2c,
serial,
pac::{I2C1, I2C2, I2C3, USART2},
};

#[cfg(feature = "stm32l082")]
use crate::aes;
Expand Down Expand Up @@ -563,6 +555,12 @@ impl_target!(
);

#[cfg(feature = "stm32l0x2")]
#[cfg(any(
feature = "io-STM32L021",
feature = "io-STM32L031",
feature = "io-STM32L051",
feature = "io-STM32L071",
))]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple cfg attributes are additive (so both need to match), right?

I'm not sure whether stm32l0x2 is still required, but on the other hand I don't have much experience with DMA yet.

impl_target!(
// I2C1
i2c::Tx<I2C1>, Channel2, 6;
Expand Down
125 changes: 90 additions & 35 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,58 @@ use as_slice::{
AsMutSlice,
};

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

#[cfg(feature = "stm32l0x2")]
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};
// I²C traits
use crate::hal::blocking::i2c::{Read, Write, WriteRead};

#[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::PA8,
gpiob::{PB10, PB11, PB13, PB14, PB4, PB8, PB9},
gpioc::{PC0, PC1},
gpioa::{PA9, PA10},
gpiob::{PB6, PB7, PB8, PB9},
},
pac::{I2C2, I2C3},
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, PA9, PA10},
gpiob::{PB4, PB6, PB7, PB8, PB9, PB10, PB11, PB13, PB14},
gpioc::{PC0, PC1, PC9},
},
pac::{I2C1, I2C2, I2C3},
};

/// I2C abstraction
Expand Down Expand Up @@ -488,32 +508,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(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
#[cfg(feature = "io-STM32L031")]
i2c!(
I2C1,
i2c1en,
i2c1rst,
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(feature = "io-STM32L071")]
i2c!(
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 +581,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 +594,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
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ pub mod exti;
#[cfg(feature = "stm32l0x2")]
pub mod flash;
pub mod gpio;
#[cfg(any(
feature = "io-STM32L021",
feature = "io-STM32L031",
feature = "io-STM32L051",
feature = "io-STM32L071",
))]
pub mod i2c;
pub mod lptim;
pub mod prelude;
Expand Down
9 changes: 8 additions & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub use crate::{
adc::AdcExt as _,
delay::DelayExt as _,
gpio::GpioExt as _,
i2c::I2cExt as _,
pwr::PowerMode as _,
rcc::RccExt as _,
spi::SpiExt as _,
Expand All @@ -25,6 +24,14 @@ pub use crate::{
},
};

#[cfg(any(
feature = "io-STM32L021",
feature = "io-STM32L031",
feature = "io-STM32L051",
feature = "io-STM32L071",
))]
pub use crate::i2c::I2cExt as _;

#[cfg(any(
feature = "io-STM32L021",
feature = "io-STM32L031",
Expand Down