From fc9230052d524bd007263e864e256b99db12150d Mon Sep 17 00:00:00 2001 From: astapleton Date: Mon, 18 Nov 2024 10:07:19 -0800 Subject: [PATCH] Move definitions to sub-module, only define for H503 for now --- src/i2c.rs | 93 ++------------------------------------------ src/i2c/i2c_def.rs | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 89 deletions(-) create mode 100644 src/i2c/i2c_def.rs diff --git a/src/i2c.rs b/src/i2c.rs index b55dcff..8e024e3 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -90,20 +90,18 @@ //! //! - [I2C controller simple example](https://github.com/stm32-rs/stm32h5xx-hal/blob/master/examples/i2c.rs) -use core::marker::PhantomData; use core::ops::{Deref, DerefMut}; -use crate::gpio::{self, Alternate, OpenDrain}; -use crate::rcc::{rec, CoreClocks, ResetEnable}; -use crate::stm32; -use crate::stm32::rcc::ccipr4; -use crate::stm32::{i2c1, i2c1::isr::ISRrs, I2C1, I2C2}; +use crate::rcc::{CoreClocks, ResetEnable}; + +use crate::stm32::{i2c1, i2c1::isr::ISRrs}; type Isr = stm32h5::R; use crate::time::Hertz; mod hal; +mod i2c_def; /// I2C Events /// @@ -198,53 +196,6 @@ where { } -macro_rules! pins { - ($($I2CX:ty: SCL: [$($SCL:ty),*] SDA: [$($SDA:ty),*])+) => { - $( - $( - impl PinScl<$I2CX> for $SCL { } - )* - $( - impl PinSda<$I2CX> for $SDA { } - )* - )+ - } -} - -pins! { - I2C1: - SCL: [ - gpio::PB6>, - gpio::PB8>, - gpio::PC8> - ] - - SDA: [ - gpio::PB5>, - gpio::PB7>, - gpio::PB10>, - gpio::PC9> - ] - - I2C2: - SCL: [ - gpio::PB3>, - gpio::PB5>, - gpio::PB10>, - gpio::PC6>, - gpio::PC10> - ] - - SDA: [ - gpio::PB3>, - gpio::PB4>, - gpio::PB8>, - gpio::PB13>, - gpio::PC7>, - gpio::PC11> - ] -} - pub trait Instance: crate::Sealed + Deref { @@ -260,42 +211,6 @@ pub trait Instance: fn rec() -> Self::Rec; } -// Implemented by all I2C instances -macro_rules! i2c { - ($I2CX:ty: $I2cX:ident, $Pclk:ident, $pclk:ident) => { - paste::item! { - - impl Instance for $I2CX { - type Rec = rec::$I2cX; - - fn ptr() -> *const i2c1::RegisterBlock { - <$I2CX>::ptr() as *const _ - } - - fn clock(clocks: &CoreClocks) -> Hertz { - let ccipr4 = unsafe { (*stm32::RCC::ptr()).ccipr4().read() }; - - match ccipr4.[<$I2CX:lower sel>]().variant() { - ccipr4::[<$I2CX SEL>]::$Pclk => Some(clocks.$pclk()), - ccipr4::[<$I2CX SEL>]::Pll2R => clocks.pll2_r_ck(), - ccipr4::[<$I2CX SEL>]::HsiKer => clocks.hsi_ck(), - ccipr4::[<$I2CX SEL>]::CsiKer => clocks.csi_ck(), - }.expect("Source clock not enabled") - } - - fn rec() -> Self::Rec { - rec::$I2cX { _marker: PhantomData } - } - } - - impl crate::Sealed for $I2CX {} - } - }; -} - -i2c! { I2C1: I2c1, RccPclk1, pclk1 } -i2c! { I2C2: I2c2, RccPclk1, pclk1 } - #[derive(Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct Inner { diff --git a/src/i2c/i2c_def.rs b/src/i2c/i2c_def.rs new file mode 100644 index 0000000..597fe36 --- /dev/null +++ b/src/i2c/i2c_def.rs @@ -0,0 +1,96 @@ +use core::marker::PhantomData; + +use crate::gpio::{self, Alternate, OpenDrain}; +use crate::rcc::{rec, CoreClocks}; +use crate::stm32::{self, i2c1, rcc::ccipr4, I2C1, I2C2}; +use crate::time::Hertz; + +use super::{Instance, PinScl, PinSda}; + +macro_rules! pins { + ($($I2CX:ty: SCL: [$($SCL:ty),*] SDA: [$($SDA:ty),*])+) => { + $( + $( + impl PinScl<$I2CX> for $SCL { } + )* + $( + impl PinSda<$I2CX> for $SDA { } + )* + )+ + } +} + +// Implemented by all I2C instances +macro_rules! i2c { + ($I2CX:ty: $I2cX:ident, $Pclk:ident, $pclk:ident) => { + + paste::item! { + impl Instance for $I2CX { + type Rec = rec::$I2cX; + + fn ptr() -> *const i2c1::RegisterBlock { + <$I2CX>::ptr() as *const _ + } + + fn clock(clocks: &CoreClocks) -> Hertz { + let ccipr4 = unsafe { (*stm32::RCC::ptr()).ccipr4().read() }; + + match ccipr4.[<$I2CX:lower sel>]().variant() { + ccipr4::[<$I2CX SEL>]::$Pclk => Some(clocks.$pclk()), + ccipr4::[<$I2CX SEL>]::Pll2R => clocks.pll2_r_ck(), + ccipr4::[<$I2CX SEL>]::HsiKer => clocks.hsi_ck(), + ccipr4::[<$I2CX SEL>]::CsiKer => clocks.csi_ck(), + }.expect("Source clock not enabled") + } + + fn rec() -> Self::Rec { + rec::$I2cX { _marker: PhantomData } + } + } + + impl crate::Sealed for $I2CX {} + } + }; +} + +#[cfg(feature = "rm0492")] +mod rm492 { + use super::*; + + pins! { + I2C1: + SCL: [ + gpio::PB6>, + gpio::PB8>, + gpio::PC8> + ] + + SDA: [ + gpio::PB5>, + gpio::PB7>, + gpio::PB10>, + gpio::PC9> + ] + + I2C2: + SCL: [ + gpio::PB3>, + gpio::PB5>, + gpio::PB10>, + gpio::PC6>, + gpio::PC10> + ] + + SDA: [ + gpio::PB3>, + gpio::PB4>, + gpio::PB8>, + gpio::PB13>, + gpio::PC7>, + gpio::PC11> + ] + } + + i2c! { I2C1: I2c1, RccPclk1, pclk1 } + i2c! { I2C2: I2c2, RccPclk1, pclk1 } +}