Skip to content

Commit c280757

Browse files
committed
Into ClockSource
1 parent fcd2729 commit c280757

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/fmpi2c.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ pub enum ClockSource<'a> {
123123
Hsi,
124124
}
125125

126+
impl<'a> From<&'a Clocks> for ClockSource<'a> {
127+
fn from(value: &'a Clocks) -> Self {
128+
Self::Apb(value)
129+
}
130+
}
131+
126132
// hddat and vddat are removed because SDADEL is always going to be 0 in this implementation so
127133
// condition is always met
128134
struct I2cSpec {
@@ -247,12 +253,32 @@ fn calculate_timing(
247253
}
248254
}
249255

256+
pub trait I2cExt: Sized + Instance {
257+
fn i2c<'a>(
258+
self,
259+
pins: (impl Into<Self::Scl>, impl Into<Self::Sda>),
260+
mode: impl Into<Mode>,
261+
clocks: impl Into<ClockSource<'a>>,
262+
) -> I2c<Self>;
263+
}
264+
265+
impl<I2C: Instance> I2cExt for I2C {
266+
fn i2c<'a>(
267+
self,
268+
pins: (impl Into<Self::Scl>, impl Into<Self::Sda>),
269+
mode: impl Into<Mode>,
270+
clocks: impl Into<ClockSource<'a>>,
271+
) -> I2c<Self> {
272+
I2c::new(self, pins, mode, clocks)
273+
}
274+
}
275+
250276
impl<I2C: Instance> I2c<I2C> {
251-
pub fn new(
277+
pub fn new<'a>(
252278
i2c: I2C,
253279
pins: (impl Into<I2C::Scl>, impl Into<I2C::Sda>),
254280
mode: impl Into<Mode>,
255-
clocks: ClockSource<'_>,
281+
clocks: impl Into<ClockSource<'a>>,
256282
) -> Self {
257283
unsafe {
258284
// Enable and reset clock.
@@ -263,7 +289,7 @@ impl<I2C: Instance> I2c<I2C> {
263289
let pins = (pins.0.into(), pins.1.into());
264290

265291
let i2c = I2c { i2c, pins };
266-
i2c.i2c_init(mode, clocks);
292+
i2c.i2c_init(mode, clocks.into());
267293
i2c
268294
}
269295

0 commit comments

Comments
 (0)