diff --git a/src/i2c.rs b/src/i2c.rs index 60c738e..d7e33d4 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -17,134 +17,6 @@ pub struct I2c { pub trait SclPin {} pub trait SdaPin {} -macro_rules! i2c_pins { - ($($I2C:ident => { - scl => [$($scl:ty),+ $(,)*], - sda => [$($sda:ty),+ $(,)*], - })+) => { - $( - $( - impl SclPin for $scl {} - )+ - $( - impl SdaPin for $sda {} - )+ - )+ - } -} - -#[cfg(any( - feature = "stm32f030", - feature = "stm32f031", - feature = "stm32f038", - feature = "stm32f042", - feature = "stm32f048", - feature = "stm32f051", - feature = "stm32f058", - feature = "stm32f070", - feature = "stm32f071", - feature = "stm32f072", - feature = "stm32f078", - feature = "stm32f091", - feature = "stm32f098", -))] -i2c_pins! { - I2C1 => { - scl => [gpiob::PB6>, gpiob::PB8>], - sda => [gpiob::PB7>, gpiob::PB9>], - } -} -#[cfg(any( - feature = "stm32f030x4", - feature = "stm32f030x6", - feature = "stm32f030xc", - feature = "stm32f031", - feature = "stm32f038", - feature = "stm32f042", - feature = "stm32f048", - feature = "stm32f070x6", - feature = "stm32f091", - feature = "stm32f098", -))] -i2c_pins! { - I2C1 => { - scl => [gpioa::PA9>], - sda => [gpioa::PA10>], - } -} -#[cfg(any(feature = "stm32f030", feature = "stm32f042", feature = "stm32f048"))] -i2c_pins! { - I2C1 => { - scl => [gpioa::PA11>], - sda => [gpioa::PA12>], - } -} -#[cfg(any( - feature = "stm32f030x4", - feature = "stm32f030x6", - feature = "stm32f031", - feature = "stm32f038", - feature = "stm32f042", - feature = "stm32f048", -))] -i2c_pins! { - I2C1 => { - scl => [gpiob::PB10>], - sda => [gpiob::PB11>], - } -} -#[cfg(any(feature = "stm32f030xc", feature = "stm32f042", feature = "stm32f048"))] -i2c_pins! { - I2C1 => { - scl => [gpiob::PB13>], - sda => [gpiob::PB14>], - } -} -#[cfg(any( - feature = "stm32f030xc", - feature = "stm32f042", - feature = "stm32f048", - feature = "stm32f070x6", - feature = "stm32f091", - feature = "stm32f098", -))] -i2c_pins! { - I2C1 => { - scl => [gpiof::PF1>], - sda => [gpiof::PF0>], - } -} - -#[cfg(any(feature = "stm32f030x8", feature = "stm32f051", feature = "stm32f058"))] -i2c_pins! { - I2C2 => { - scl => [gpiob::PB10>], - sda => [gpiob::PB11>], - } -} -#[cfg(any( - feature = "stm32f030xc", - feature = "stm32f070xb", - feature = "stm32f071", - feature = "stm32f072", - feature = "stm32f078", - feature = "stm32f091", - feature = "stm32f098", -))] -i2c_pins! { - I2C2 => { - scl => [gpiob::PB10>, gpiob::PB13>], - sda => [gpiob::PB11>, gpiob::PB14>], - } -} -#[cfg(any(feature = "stm32f091", feature = "stm32f098"))] -i2c_pins! { - I2C2 => { - scl => [gpioa::PA11>], - sda => [gpioa::PA12>], - } -} - #[derive(Debug)] pub enum Error { OVERRUN, diff --git a/src/lib.rs b/src/lib.rs index 510e1b4..e616ecc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,6 +35,8 @@ pub mod gpio; #[cfg(feature = "device-selected")] pub mod i2c; #[cfg(feature = "device-selected")] +pub mod pin_mappings; +#[cfg(feature = "device-selected")] pub mod prelude; #[cfg(feature = "device-selected")] pub mod rcc; diff --git a/src/pin_mappings.rs b/src/pin_mappings.rs new file mode 100644 index 0000000..1ea75e3 --- /dev/null +++ b/src/pin_mappings.rs @@ -0,0 +1,187 @@ +#[cfg(feature = "device-selected")] +use crate::gpio::gpioa::*; +#[cfg(feature = "device-selected")] +use crate::gpio::gpiob::*; +#[allow(unused)] +#[cfg(feature = "device-selected")] +use crate::gpio::gpioc::*; +#[cfg(feature = "stm32f030")] +use crate::gpio::gpiod::*; +#[allow(unused)] +#[cfg(feature = "device-selected")] +use crate::gpio::gpiof::*; +#[allow(unused)] +use crate::gpio::{Alternate, AF0, AF1, AF2, AF4, AF5}; +use crate::i2c::*; +use crate::serial::*; +use crate::spi::*; +#[cfg(feature = "device-selected")] +use crate::stm32::*; + +macro_rules! pins { + ($($PIN:ident => { + $($AF:ty: $TRAIT:ty),+ + }),+) => { + $( + $( + impl $TRAIT for $PIN> {} + )+ + )+ + } +} + +#[cfg(feature = "device-selected")] +pins! { + PA5 => {AF0: SckPin}, + PA6 => {AF0: MisoPin}, + PA7 => {AF0: MosiPin}, + PA9 => {AF1: TxPin}, + PA10 => {AF1: RxPin}, + PB3 => {AF0: SckPin}, + PB4 => {AF0: MisoPin}, + PB5 => {AF0: MosiPin}, + PB6 => { + AF0: TxPin, + AF1: SclPin + }, + PB7 => { + AF0: RxPin, + AF1: SdaPin + }, + PB8 => {AF1: SclPin}, + PB9 => {AF1: SdaPin} +} + +#[cfg(feature = "stm32f030")] +pins! { + PA0 => {AF4: TxPin}, + PA1 => {AF4: RxPin}, + PA4 => {AF5: TxPin}, + PA5 => {AF5: RxPin}, + PA11 => {AF5: SclPin}, + PA12 => {AF5: SdaPin}, + PB3 => {AF4: TxPin}, + PB4 => {AF4: RxPin}, + PB10 => { + AF4: TxPin, + AF5: SckPin + }, + PB11 => {AF4: RxPin}, + PC2 => {AF1: MisoPin}, + PC3 => {AF1: MosiPin}, + PC4 => {AF1: TxPin}, + PC5 => {AF1: RxPin}, + PC10 => { + AF0: TxPin, + AF1: TxPin + }, + PC11 => { + AF0: RxPin, + AF1: RxPin + }, + PC0 => {AF2: TxPin}, + PC1 => {AF2: RxPin}, + PC12 => {AF2: RxPin}, + PD2 => {AF2: TxPin} +} + +#[cfg(feature = "stm32f030x6")] +pins! { + PA2 => {AF1: TxPin}, + PA3 => {AF1: RxPin}, + PA9 => {AF4: SclPin}, + PA10 => {AF4: SdaPin}, + PA14 => {AF1: TxPin}, + PA15 => {AF1: RxPin}, + PB10 => {AF1: SclPin}, + PB11 => {AF1: SdaPin}, + PB13 => {AF0: SckPin}, + PB14 => {AF0: MisoPin}, + PB15 => {AF0: MosiPin} +} + +#[cfg(any(feature = "stm32f030x8", feature = "stm32f030xc"))] +pins! { + PA2 => {AF1: TxPin}, + PA3 => {AF1: RxPin}, + PA14 => {AF1: TxPin}, + PA15 => {AF1: RxPin}, + PB10 => {AF1: SclPin}, + PB11 => {AF1: SdaPin}, + PB13 => {AF0: SckPin}, + PB14 => {AF0: MisoPin}, + PB15 => {AF0: MosiPin} +} + +#[cfg(any(feature = "stm32f030xc",))] +pins! { + PA9 => {AF4: SclPin}, + PA10 => {AF4: SdaPin}, + PB13 => {AF5: SclPin}, + PB14 => {AF5: SdaPin}, + PF0 => {AF1: SdaPin}, + PF1 => {AF1: SclPin} +} + +#[cfg(feature = "stm32f042")] +pins! { + PA11 => {AF5: SclPin}, + PA12 => {AF5: SdaPin}, + PA2 => {AF1: TxPin}, + PA3 => {AF1: RxPin}, + PA9 => {AF4: SclPin}, + PA10 => {AF4: SdaPin}, + PA14 => {AF1: TxPin}, + PA15 => {AF1: RxPin}, + PB10 => {AF1: SclPin}, + PB11 => {AF1: SdaPin}, + PB13 => {AF5: SclPin}, + PB14 => {AF5: SdaPin}, + PF0 => {AF1: SdaPin}, + PF1 => {AF1: SclPin} +} + +#[cfg(feature = "stm32f070")] +pins! { + PA0 => {AF4: TxPin}, + PA1 => {AF4: RxPin}, + PA2 => {AF1: TxPin}, + PA3 => {AF1: RxPin}, + PA14 => {AF1: TxPin}, + PA15 => {AF1: RxPin}, + PB10 => { + AF4: TxPin, + AF5: SckPin + }, + PB11 => {AF4: RxPin}, + PC2 => {AF1: MisoPin}, + PC3 => {AF1: MosiPin}, + PC4 => {AF1: TxPin}, + PC5 => {AF1: RxPin}, + PC10 => { + AF0: TxPin, + AF1: TxPin + }, + PC11 => { + AF0: RxPin, + AF1: RxPin + } +} + +#[cfg(feature = "stm32f070xb")] +pins! { + PB10 => {AF1: SclPin}, + PB11 => {AF1: SdaPin}, + PB13 => {AF0: SckPin}, + PB14 => {AF0: MisoPin}, + PB13 => {AF5: SclPin}, + PB14 => {AF5: SdaPin}, + PB15 => {AF0: MosiPin} +} +#[cfg(feature = "stm32f070x6")] +pins! { + PA9 => {AF4: SclPin}, + PA10 => {AF4: SdaPin}, + PF0 => {AF1: SdaPin}, + PF1 => {AF1: SclPin} +} diff --git a/src/serial.rs b/src/serial.rs index 504bf6a..701870a 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -98,165 +98,6 @@ pub enum Event { pub trait TxPin {} pub trait RxPin {} -macro_rules! usart_pins { - ($($USART:ident => { - tx => [$($tx:ty),+ $(,)*], - rx => [$($rx:ty),+ $(,)*], - })+) => { - $( - $( - impl TxPin for $tx {} - )+ - $( - impl RxPin for $rx {} - )+ - )+ - } -} - -#[cfg(any( - feature = "stm32f030", - feature = "stm32f031", - feature = "stm32f038", - feature = "stm32f042", - feature = "stm32f048", - feature = "stm32f051", - feature = "stm32f058", - feature = "stm32f070", - feature = "stm32f071", - feature = "stm32f072", - feature = "stm32f078", - feature = "stm32f091", - feature = "stm32f098", -))] -usart_pins! { - USART1 => { - tx => [gpioa::PA9>, gpiob::PB6>], - rx => [gpioa::PA10>, gpiob::PB7>], - } -} -#[cfg(any( - feature = "stm32f030x4", - feature = "stm32f030x6", - feature = "stm32f031", - feature = "stm32f038", -))] -usart_pins! { - USART1 => { - tx => [gpioa::PA2>, gpioa::PA14>], - rx => [gpioa::PA3>, gpioa::PA15>], - } -} - -#[cfg(any( - feature = "stm32f030x8", - feature = "stm32f030xc", - feature = "stm32f042", - feature = "stm32f048", - feature = "stm32f051", - feature = "stm32f058", - feature = "stm32f070", - feature = "stm32f071", - feature = "stm32f072", - feature = "stm32f078", - feature = "stm32f091", - feature = "stm32f098", -))] -usart_pins! { - USART2 => { - tx => [gpioa::PA2>, gpioa::PA14>], - rx => [gpioa::PA3>, gpioa::PA15>], - } -} -#[cfg(any( - feature = "stm32f071", - feature = "stm32f072", - feature = "stm32f078", - feature = "stm32f091", - feature = "stm32f098", -))] -usart_pins! { - USART2 => { - tx => [gpiod::PD5>], - rx => [gpiod::PD6>], - } -} - -#[cfg(any( - feature = "stm32f030xc", - feature = "stm32f070xb", - feature = "stm32f071", - feature = "stm32f072", - feature = "stm32f078", - feature = "stm32f091", - feature = "stm32f098", -))] -usart_pins! { - USART3 => { - // According to the datasheet PB10 is both tx and rx, but in stm32cubemx it's only tx - tx => [gpiob::PB10>, gpioc::PC4>, gpioc::PC10>], - rx => [gpiob::PB11>, gpioc::PC5>, gpioc::PC11>], - } - USART4 => { - tx => [gpioa::PA0>, gpioc::PC10>], - rx => [gpioa::PA1>, gpioc::PC11>], - } -} -#[cfg(any( - feature = "stm32f071", - feature = "stm32f072", - feature = "stm32f078", - feature = "stm32f091", - feature = "stm32f098", -))] -usart_pins! { - USART3 => { - tx => [gpiod::PD8>], - rx => [gpiod::PD9>], - } -} -// TODO: The ST SVD files are missing the entire PE enable register. -// Re-enable as soon as this gets fixed. -// #[cfg(any(feature = "stm32f091", feature = "stm32f098"))] -// usart_pins! { -// USART4 => { -// tx => [gpioe::PE8>], -// rx => [gpioe::PE9>], -// } -// } - -#[cfg(any(feature = "stm32f030xc", feature = "stm32f091", feature = "stm32f098"))] -usart_pins! { - USART5 => { - tx => [gpioc::PC12>], - rx => [gpiod::PD2>], - } - USART6 => { - tx => [gpioa::PA4>, gpioc::PC0>], - rx => [gpioa::PA5>, gpioc::PC1>], - } -} -#[cfg(any(feature = "stm32f030xc", feature = "stm32f091"))] -usart_pins! { - USART5 => { - tx => [gpiob::PB3>], - rx => [gpiob::PB4>], - } -} -// TODO: The ST SVD files are missing the entire PE enable register. -// Re-enable as soon as this gets fixed. -#[cfg(any(feature = "stm32f091", feature = "stm32f098"))] -usart_pins! { - // USART5 => { - // tx => [gpioe::PE10>], - // rx => [gpioe::PE11>], - // } - USART6 => { - tx => [gpiof::PF9>], - rx => [gpiof::PF10>], - } -} - /// Serial abstraction pub struct Serial { usart: USART, diff --git a/src/spi.rs b/src/spi.rs index 7ed5c1b..96b0963 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -91,116 +91,6 @@ pub trait SckPin {} pub trait MisoPin {} pub trait MosiPin {} -macro_rules! spi_pins { - ($($SPI:ident => { - sck => [$($sck:ty),+ $(,)*], - miso => [$($miso:ty),+ $(,)*], - mosi => [$($mosi:ty),+ $(,)*], - })+) => { - $( - $( - impl SckPin for $sck {} - )+ - $( - impl MisoPin for $miso {} - )+ - $( - impl MosiPin for $mosi {} - )+ - )+ - } -} - -spi_pins! { - SPI1 => { - sck => [gpioa::PA5>, gpiob::PB3>], - miso => [gpioa::PA6>, gpiob::PB4>], - mosi => [gpioa::PA7>, gpiob::PB5>], - } -} -#[cfg(any( - feature = "stm32f030x4", - feature = "stm32f030x6", - feature = "stm32f031", - feature = "stm32f038", -))] -spi_pins! { - SPI1 => { - sck => [gpiob::PB13>], - miso => [gpiob::PB14>], - mosi => [gpiob::PB15>], - } -} -// TODO: The ST SVD files are missing the entire PE enable register. -// So those pins do not exist in the register definitions. -// Re-enable as soon as this gets fixed. -// #[cfg(any( -// feature = "stm32f071", -// feature = "stm32f072", -// feature = "stm32f078", -// feature = "stm32f091", -// feature = "stm32f098", -// ))] -// spi_pins! { -// SPI1 => { -// sck => [gpioe::PE13>], -// miso => [gpioe::PE14>], -// mosi => [gpioe::PE15>], -// } -// } - -#[cfg(any( - feature = "stm32f030x8", - feature = "stm32f030xc", - feature = "stm32f042", - feature = "stm32f048", - feature = "stm32f051", - feature = "stm32f058", - feature = "stm32f070xb", - feature = "stm32f071", - feature = "stm32f072", - feature = "stm32f078", - feature = "stm32f091", - feature = "stm32f098", -))] -spi_pins! { - SPI2 => { - sck => [gpiob::PB13>], - miso => [gpiob::PB14>], - mosi => [gpiob::PB15>], - } -} -#[cfg(any( - feature = "stm32f030xc", - feature = "stm32f070xb", - feature = "stm32f071", - feature = "stm32f072", - feature = "stm32f078", - feature = "stm32f091", - feature = "stm32f098", -))] -spi_pins! { - SPI2 => { - sck => [gpiob::PB10>], - miso => [gpioc::PC2>], - mosi => [gpioc::PC3>], - } -} -#[cfg(any( - feature = "stm32f071", - feature = "stm32f072", - feature = "stm32f078", - feature = "stm32f091", - feature = "stm32f098", -))] -spi_pins! { - SPI2 => { - sck => [gpiod::PD1>], - miso => [gpiod::PD3>], - mosi => [gpiod::PD4>], - } -} - macro_rules! spi { ($($SPI:ident: ($spi:ident, $spiXen:ident, $spiXrst:ident, $apbenr:ident, $apbrstr:ident),)+) => { $(