Skip to content

Commit 9a4d5bd

Browse files
authored
Set i2c frequency using Hertz (#128)
* Set i2c frequency using Hertz * Allow creation of mode from Into<Hertz>
1 parent e36195e commit 9a4d5bd

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2424
- Renames `set_seconds` and `seconds` methods on RTC to `set_time` and `current_time`, respectively
2525
- Starting the timer does not generate interrupt requests anymore
2626
- Make MAPR::mapr() private
27+
- i2c mode now takes Hertz instead of a generic u32
2728

2829
### Fixed
2930

src/i2c.rs

Lines changed: 16 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,24 @@ 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 standard<F: Into<Hertz>>(frequency: F) -> Self {
50+
Mode::Standard{frequency: frequency.into()}
51+
}
52+
53+
pub fn fast<F: Into<Hertz>>(frequency: F, duty_cycle: DutyCycle) -> Self {
54+
Mode::Fast{frequency: frequency.into(), duty_cycle}
55+
}
56+
57+
pub fn get_frequency(&self) -> Hertz {
4958
match self {
5059
&Mode::Standard { frequency } => frequency,
5160
&Mode::Fast { frequency, .. } => frequency,
@@ -251,7 +260,7 @@ macro_rules! hal {
251260

252261
let pclk1 = clocks.pclk1().0;
253262

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

256265
let mut i2c = I2c { i2c, pins, mode, pclk1 };
257266
i2c.init();
@@ -273,7 +282,7 @@ macro_rules! hal {
273282
w.trise().bits((pclk1_mhz + 1) as u8)
274283
});
275284
self.i2c.ccr.write(|w| unsafe {
276-
w.ccr().bits(((self.pclk1 / (freq * 2)) as u16).max(4))
285+
w.ccr().bits(((self.pclk1 / (freq.0 * 2)) as u16).max(4))
277286
});
278287
},
279288
Mode::Fast { ref duty_cycle, .. } => {
@@ -283,8 +292,8 @@ macro_rules! hal {
283292

284293
self.i2c.ccr.write(|w| {
285294
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)
295+
&DutyCycle::Ratio2to1 => (((self.pclk1 / (freq.0* 3)) as u16).max(1), false),
296+
&DutyCycle::Ratio16to9 => (((self.pclk1 / (freq.0 * 25)) as u16).max(1), true)
288297
};
289298

290299
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)