Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set i2c frequency using Hertz #128

Merged
merged 2 commits into from
Nov 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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