Skip to content

Commit 4084456

Browse files
committed
Make HSI public and Hertz
1 parent 74fd875 commit 4084456

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/i2c.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ impl<I2C, SCL, SDA> I2c<I2C, (SCL, SDA)> {
132132
// t_SCL ~= t_SYNC1 + t_SYNC2 + t_SCLL + t_SCLH
133133
let i2cclk = I2C::clock(&clocks).0;
134134
let ratio = i2cclk / freq.integer() - 4;
135-
let (presc, scll, sclh, sdadel, scldel) = if freq.integer() >= 100_000 {
135+
let (presc, scll, sclh, sdadel, scldel) = if freq >= 100.kHz() {
136136
// fast-mode or fast-mode plus
137137
// here we pick SCLL + 1 = 2 * (SCLH + 1)
138138
let presc = ratio / 387;
139139

140140
let sclh = ((ratio / (presc + 1)) - 3) / 3;
141141
let scll = 2 * (sclh + 1) - 1;
142142

143-
let (sdadel, scldel) = if freq.integer() > 400_000 {
143+
let (sdadel, scldel) = if freq > 400.kHz() {
144144
// fast-mode plus
145145
let sdadel = 0;
146146
let scldel = i2cclk / 4_000_000 / (presc + 1) - 1;
@@ -449,7 +449,7 @@ macro_rules! i2c {
449449
fn clock(clocks: &Clocks) -> Hertz {
450450
// NOTE(unsafe) atomic read with no side effects
451451
match unsafe { (*RCC::ptr()).cfgr3.read().$i2cXsw().variant() } {
452-
I2C1SW_A::HSI => Hertz(8_000_000),
452+
I2C1SW_A::HSI => crate::rcc::HSI,
453453
I2C1SW_A::SYSCLK => clocks.sysclk(),
454454
}
455455
}

src/rcc.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ impl APB2 {
182182
}
183183
}
184184

185-
const HSI: u32 = 8_000_000; // Hz
185+
/// Frequency of interal hardware RC oscillator (HSI OSC)
186+
pub const HSI: Hertz = Hertz(8_000_000);
187+
/// Frequency of external 32.768 kHz oscillator (LSE OSC)
188+
pub const LSE: Hertz = Hertz(32_768);
186189

187190
// some microcontrollers do not have USB
188191
#[cfg(any(feature = "stm32f301", feature = "stm32f318", feature = "stm32f334",))]
@@ -469,7 +472,7 @@ impl CFGR {
469472
feature = "stm32f398"
470473
)))]
471474
fn calc_pll(&self, sysclk: u32) -> (u32, PllConfig) {
472-
let pllsrcclk = self.hse.unwrap_or(HSI / 2);
475+
let pllsrcclk = self.hse.unwrap_or(HSI.integer() / 2);
473476
// Get the optimal value for the pll divisor (PLL_DIV) and multiplier (PLL_MUL)
474477
// Only for HSE PLL_DIV can be changed
475478
let (pll_mul, pll_div): (u32, Option<u32>) = if self.hse.is_some() {
@@ -545,7 +548,7 @@ impl CFGR {
545548
feature = "stm32f398",
546549
))]
547550
fn calc_pll(&self, sysclk: u32) -> (u32, PllConfig) {
548-
let pllsrcclk = self.hse.unwrap_or(HSI);
551+
let pllsrcclk = self.hse.unwrap_or(HSI.integer());
549552

550553
let (pll_mul, pll_div) = {
551554
// Get the optimal value for the pll divisor (PLL_DIV) and multiplcator (PLL_MUL)
@@ -613,15 +616,17 @@ impl CFGR {
613616
// Oscillator (max 32 Mhz), without using the PLL.
614617
(Some(sysclk), Some(hse)) if sysclk == hse => (hse, cfgr::SW_A::HSE, None),
615618
// No need to use the PLL
616-
(Some(sysclk), None) if sysclk == HSI => (HSI, cfgr::SW_A::HSI, None),
619+
(Some(sysclk), None) if sysclk == HSI.integer() => {
620+
(HSI.integer(), cfgr::SW_A::HSI, None)
621+
}
617622
(Some(sysclk), _) => {
618623
let (sysclk, pll_config) = self.calc_pll(sysclk);
619624
(sysclk, cfgr::SW_A::PLL, Some(pll_config))
620625
}
621626
// Use HSE as system clock
622627
(None, Some(hse)) => (hse, cfgr::SW_A::HSE, None),
623628
// Use HSI as system clock
624-
(None, None) => (HSI, cfgr::SW_A::HSI, None),
629+
(None, None) => (HSI.integer(), cfgr::SW_A::HSI, None),
625630
}
626631
}
627632

0 commit comments

Comments
 (0)