Skip to content

Commit

Permalink
Set i2c frequency using Hertz (#128)
Browse files Browse the repository at this point in the history
* Set i2c frequency using Hertz

* Allow creation of mode from Into<Hertz>
  • Loading branch information
TheZoq2 authored Nov 16, 2019
1 parent e36195e commit 9a4d5bd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Renames `set_seconds` and `seconds` methods on RTC to `set_time` and `current_time`, respectively
- Starting the timer does not generate interrupt requests anymore
- Make MAPR::mapr() private
- i2c mode now takes Hertz instead of a generic u32

### Fixed

Expand Down
23 changes: 16 additions & 7 deletions src/i2c.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Inter-Integrated Circuit (I2C) bus
use crate::afio::MAPR;
use crate::time::Hertz;
use crate::gpio::gpiob::{PB10, PB11, PB6, PB7, PB8, PB9};
use crate::gpio::{Alternate, OpenDrain};
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
Expand Down Expand Up @@ -36,16 +37,24 @@ pub enum DutyCycle {
#[derive(Debug, PartialEq)]
pub enum Mode {
Standard {
frequency: u32,
frequency: Hertz,
},
Fast {
frequency: u32,
frequency: Hertz,
duty_cycle: DutyCycle,
},
}

impl Mode {
pub fn get_frequency(&self) -> u32 {
pub fn standard<F: Into<Hertz>>(frequency: F) -> Self {
Mode::Standard{frequency: frequency.into()}
}

pub fn fast<F: Into<Hertz>>(frequency: F, duty_cycle: DutyCycle) -> Self {
Mode::Fast{frequency: frequency.into(), duty_cycle}
}

pub fn get_frequency(&self) -> Hertz {
match self {
&Mode::Standard { frequency } => frequency,
&Mode::Fast { frequency, .. } => frequency,
Expand Down Expand Up @@ -251,7 +260,7 @@ macro_rules! hal {

let pclk1 = clocks.pclk1().0;

assert!(mode.get_frequency() <= 400_000);
assert!(mode.get_frequency().0 <= 400_000);

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

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

unsafe {
Expand Down
8 changes: 4 additions & 4 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ use cortex_m::peripheral::DWT;
use crate::rcc::Clocks;

/// Bits per second
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Bps(pub u32);

/// Hertz
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Hertz(pub u32);

/// KiloHertz
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct KiloHertz(pub u32);

/// MegaHertz
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct MegaHertz(pub u32);

/// Time unit
Expand Down

0 comments on commit 9a4d5bd

Please sign in to comment.