Skip to content

Commit d150398

Browse files
committed
Set i2c frequency using Hertz
1 parent 26ab9e2 commit d150398

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/i2c.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Inter-Integrated Circuit (I2C) bus
22
33
use crate::afio::MAPR;
4+
use crate::time::Hertz;
45
use crate::gpio::gpiob::{PB10, PB11, PB6, PB7, PB8, PB9};
56
use crate::gpio::{Alternate, OpenDrain};
67
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
@@ -36,16 +37,16 @@ pub enum DutyCycle {
3637
#[derive(Debug, PartialEq)]
3738
pub enum Mode {
3839
Standard {
39-
frequency: u32,
40+
frequency: Hertz,
4041
},
4142
Fast {
42-
frequency: u32,
43+
frequency: Hertz,
4344
duty_cycle: DutyCycle,
4445
},
4546
}
4647

4748
impl Mode {
48-
pub fn get_frequency(&self) -> u32 {
49+
pub fn get_frequency(&self) -> Hertz {
4950
match self {
5051
&Mode::Standard { frequency } => frequency,
5152
&Mode::Fast { frequency, .. } => frequency,
@@ -251,7 +252,7 @@ macro_rules! hal {
251252

252253
let pclk1 = clocks.pclk1().0;
253254

254-
assert!(mode.get_frequency() <= 400_000);
255+
assert!(mode.get_frequency().0 <= 400_000);
255256

256257
let mut i2c = I2c { i2c, pins, mode, pclk1 };
257258
i2c.init();
@@ -273,7 +274,7 @@ macro_rules! hal {
273274
w.trise().bits((pclk1_mhz + 1) as u8)
274275
});
275276
self.i2c.ccr.write(|w| unsafe {
276-
w.ccr().bits(((self.pclk1 / (freq * 2)) as u16).max(4))
277+
w.ccr().bits(((self.pclk1 / (freq.0 * 2)) as u16).max(4))
277278
});
278279
},
279280
Mode::Fast { ref duty_cycle, .. } => {
@@ -283,8 +284,8 @@ macro_rules! hal {
283284

284285
self.i2c.ccr.write(|w| {
285286
let (freq, duty) = match duty_cycle {
286-
&DutyCycle::Ratio2to1 => (((self.pclk1 / (freq * 3)) as u16).max(1), false),
287-
&DutyCycle::Ratio16to9 => (((self.pclk1 / (freq * 25)) as u16).max(1), true)
287+
&DutyCycle::Ratio2to1 => (((self.pclk1 / (freq.0* 3)) as u16).max(1), false),
288+
&DutyCycle::Ratio16to9 => (((self.pclk1 / (freq.0 * 25)) as u16).max(1), true)
288289
};
289290

290291
unsafe {

src/time.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ use cortex_m::peripheral::DWT;
55
use crate::rcc::Clocks;
66

77
/// Bits per second
8-
#[derive(Clone, Copy)]
8+
#[derive(Clone, Copy, PartialEq, Debug)]
99
pub struct Bps(pub u32);
1010

1111
/// Hertz
12-
#[derive(Clone, Copy)]
12+
#[derive(Clone, Copy, PartialEq, Debug)]
1313
pub struct Hertz(pub u32);
1414

1515
/// KiloHertz
16-
#[derive(Clone, Copy)]
16+
#[derive(Clone, Copy, PartialEq, Debug)]
1717
pub struct KiloHertz(pub u32);
1818

1919
/// MegaHertz
20-
#[derive(Clone, Copy)]
20+
#[derive(Clone, Copy, PartialEq, Debug)]
2121
pub struct MegaHertz(pub u32);
2222

2323
/// Time unit

0 commit comments

Comments
 (0)