Skip to content

Commit

Permalink
Move definitions to sub-module, only define for H503 for now
Browse files Browse the repository at this point in the history
  • Loading branch information
astapleton committed Nov 18, 2024
1 parent 53372b2 commit fc92300
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 89 deletions.
93 changes: 4 additions & 89 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISRrs>;

use crate::time::Hertz;

mod hal;
mod i2c_def;

/// I2C Events
///
Expand Down Expand Up @@ -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<Alternate<4, OpenDrain>>,
gpio::PB8<Alternate<4, OpenDrain>>,
gpio::PC8<Alternate<4, OpenDrain>>
]

SDA: [
gpio::PB5<Alternate<11, OpenDrain>>,
gpio::PB7<Alternate<4, OpenDrain>>,
gpio::PB10<Alternate<11, OpenDrain>>,
gpio::PC9<Alternate<4, OpenDrain>>
]

I2C2:
SCL: [
gpio::PB3<Alternate<8, OpenDrain>>,
gpio::PB5<Alternate<4, OpenDrain>>,
gpio::PB10<Alternate<4, OpenDrain>>,
gpio::PC6<Alternate<8, OpenDrain>>,
gpio::PC10<Alternate<8, OpenDrain>>
]

SDA: [
gpio::PB3<Alternate<4, OpenDrain>>,
gpio::PB4<Alternate<8, OpenDrain>>,
gpio::PB8<Alternate<8, OpenDrain>>,
gpio::PB13<Alternate<4, OpenDrain>>,
gpio::PC7<Alternate<8, OpenDrain>>,
gpio::PC11<Alternate<8, OpenDrain>>
]
}

pub trait Instance:
crate::Sealed + Deref<Target = i2c1::RegisterBlock>
{
Expand All @@ -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<I2C> {
Expand Down
96 changes: 96 additions & 0 deletions src/i2c/i2c_def.rs
Original file line number Diff line number Diff line change
@@ -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<Alternate<4, OpenDrain>>,
gpio::PB8<Alternate<4, OpenDrain>>,
gpio::PC8<Alternate<4, OpenDrain>>
]

SDA: [
gpio::PB5<Alternate<11, OpenDrain>>,
gpio::PB7<Alternate<4, OpenDrain>>,
gpio::PB10<Alternate<11, OpenDrain>>,
gpio::PC9<Alternate<4, OpenDrain>>
]

I2C2:
SCL: [
gpio::PB3<Alternate<8, OpenDrain>>,
gpio::PB5<Alternate<4, OpenDrain>>,
gpio::PB10<Alternate<4, OpenDrain>>,
gpio::PC6<Alternate<8, OpenDrain>>,
gpio::PC10<Alternate<8, OpenDrain>>
]

SDA: [
gpio::PB3<Alternate<4, OpenDrain>>,
gpio::PB4<Alternate<8, OpenDrain>>,
gpio::PB8<Alternate<8, OpenDrain>>,
gpio::PB13<Alternate<4, OpenDrain>>,
gpio::PC7<Alternate<8, OpenDrain>>,
gpio::PC11<Alternate<8, OpenDrain>>
]
}

i2c! { I2C1: I2c1, RccPclk1, pclk1 }
i2c! { I2C2: I2c2, RccPclk1, pclk1 }
}

0 comments on commit fc92300

Please sign in to comment.