Skip to content

Commit 9876d19

Browse files
bors[bot]burrbull
andauthored
Merge #391
391: replace GetBusFreq with BusClock r=therealprof a=burrbull Same as in f3xx-hal, f4xx-hal Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents ce6648f + d0416c0 commit 9876d19

File tree

7 files changed

+56
-34
lines changed

7 files changed

+56
-34
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212
- `Instance` for Timer's, rtic-monotonic fugit impl
1313
- Serial can now be reconfigured, allowing to change e.g. the baud rate after initialisation.
1414

15+
### Changed
16+
17+
- replace `GetBusFreq` with `BusClock` and `BusTimerClock`
18+
1519
## [v0.8.0] - 2021-12-29
1620

1721
### Breaking changes

src/i2c.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::gpio::gpiob::{PB10, PB11, PB6, PB7, PB8, PB9};
99
use crate::gpio::{Alternate, OpenDrain};
1010
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
1111
use crate::pac::{DWT, I2C1, I2C2, RCC};
12-
use crate::rcc::{Clocks, Enable, GetBusFreq, Reset};
12+
use crate::rcc::{BusClock, Clocks, Enable, Reset};
1313
use crate::time::Hertz;
1414
use core::ops::Deref;
1515
use nb::Error::{Other, WouldBlock};
@@ -117,7 +117,7 @@ pub struct I2c<I2C, PINS> {
117117
}
118118

119119
pub trait Instance:
120-
crate::Sealed + Deref<Target = crate::pac::i2c1::RegisterBlock> + Enable + Reset + GetBusFreq
120+
crate::Sealed + Deref<Target = crate::pac::i2c1::RegisterBlock> + Enable + Reset + BusClock
121121
{
122122
}
123123

@@ -162,7 +162,7 @@ where
162162
I2C::enable(rcc);
163163
I2C::reset(rcc);
164164

165-
let pclk1 = I2C::get_frequency(&clocks).0;
165+
let pclk1 = I2C::clock(&clocks).0;
166166

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

src/pwm_input.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::pac::{TIM2, TIM3};
1313

1414
use crate::afio::MAPR;
1515
use crate::gpio::{self, Input};
16-
use crate::rcc::{Clocks, GetBusFreq, RccBus};
16+
use crate::rcc::{BusTimerClock, Clocks};
1717
use crate::time::Hertz;
1818
use crate::timer::Timer;
1919

@@ -273,7 +273,7 @@ macro_rules! hal {
273273
if ccr1 == 0 {
274274
Err(Error::FrequencyTooLow)
275275
} else {
276-
let clk : u32 = <$TIMX as RccBus>::Bus::get_timer_frequency(&clocks).0;
276+
let clk : u32 = <$TIMX>::timer_clock(&clocks).0;
277277
Ok(Hertz(clk/((presc+1) as u32*(ccr1 + 1)as u32)))
278278
}
279279
}

src/rcc.rs

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -416,46 +416,64 @@ impl Clocks {
416416
}
417417
}
418418

419-
pub trait GetBusFreq {
420-
fn get_frequency(clocks: &Clocks) -> Hertz;
421-
fn get_timer_frequency(clocks: &Clocks) -> Hertz {
422-
Self::get_frequency(clocks)
423-
}
419+
/// Frequency on bus that peripheral is connected in
420+
pub trait BusClock {
421+
/// Calculates frequency depending on `Clock` state
422+
fn clock(clocks: &Clocks) -> Hertz;
423+
}
424+
425+
/// Frequency on bus that timer is connected in
426+
pub trait BusTimerClock {
427+
/// Calculates base frequency of timer depending on `Clock` state
428+
fn timer_clock(clocks: &Clocks) -> Hertz;
424429
}
425430

426-
impl<T> GetBusFreq for T
431+
impl<T> BusClock for T
427432
where
428433
T: RccBus,
429-
T::Bus: GetBusFreq,
434+
T::Bus: BusClock,
430435
{
431-
fn get_frequency(clocks: &Clocks) -> Hertz {
432-
T::Bus::get_frequency(clocks)
436+
fn clock(clocks: &Clocks) -> Hertz {
437+
T::Bus::clock(clocks)
433438
}
434-
fn get_timer_frequency(clocks: &Clocks) -> Hertz {
435-
T::Bus::get_timer_frequency(clocks)
439+
}
440+
441+
impl<T> BusTimerClock for T
442+
where
443+
T: RccBus,
444+
T::Bus: BusTimerClock,
445+
{
446+
fn timer_clock(clocks: &Clocks) -> Hertz {
447+
T::Bus::timer_clock(clocks)
436448
}
437449
}
438450

439-
impl GetBusFreq for AHB {
440-
fn get_frequency(clocks: &Clocks) -> Hertz {
451+
impl BusClock for AHB {
452+
fn clock(clocks: &Clocks) -> Hertz {
441453
clocks.hclk
442454
}
443455
}
444456

445-
impl GetBusFreq for APB1 {
446-
fn get_frequency(clocks: &Clocks) -> Hertz {
457+
impl BusClock for APB1 {
458+
fn clock(clocks: &Clocks) -> Hertz {
447459
clocks.pclk1
448460
}
449-
fn get_timer_frequency(clocks: &Clocks) -> Hertz {
450-
clocks.pclk1_tim()
451-
}
452461
}
453462

454-
impl GetBusFreq for APB2 {
455-
fn get_frequency(clocks: &Clocks) -> Hertz {
463+
impl BusClock for APB2 {
464+
fn clock(clocks: &Clocks) -> Hertz {
456465
clocks.pclk2
457466
}
458-
fn get_timer_frequency(clocks: &Clocks) -> Hertz {
467+
}
468+
469+
impl BusTimerClock for APB1 {
470+
fn timer_clock(clocks: &Clocks) -> Hertz {
471+
clocks.pclk1_tim()
472+
}
473+
}
474+
475+
impl BusTimerClock for APB2 {
476+
fn timer_clock(clocks: &Clocks) -> Hertz {
459477
clocks.pclk2_tim()
460478
}
461479
}

src/serial.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::gpio::gpiob::{PB10, PB11, PB6, PB7};
5353
use crate::gpio::gpioc::{PC10, PC11};
5454
use crate::gpio::gpiod::{PD5, PD6, PD8, PD9};
5555
use crate::gpio::{Alternate, Input};
56-
use crate::rcc::{Clocks, Enable, GetBusFreq, Reset};
56+
use crate::rcc::{BusClock, Clocks, Enable, Reset};
5757
use crate::time::{Bps, U32Ext};
5858

5959
/// Interrupt event
@@ -192,7 +192,7 @@ pub struct Serial<USART, PINS> {
192192
}
193193

194194
pub trait Instance:
195-
crate::Sealed + Deref<Target = uart_base::RegisterBlock> + Enable + Reset + GetBusFreq
195+
crate::Sealed + Deref<Target = uart_base::RegisterBlock> + Enable + Reset + BusClock
196196
{
197197
#[doc(hidden)]
198198
fn ptr() -> *const uart_base::RegisterBlock;
@@ -249,7 +249,7 @@ where
249249

250250
fn apply_config(&self, config: Config, clocks: Clocks) {
251251
// Configure baud rate
252-
let brr = USART::get_frequency(&clocks).0 / config.baudrate.0;
252+
let brr = USART::clock(&clocks).0 / config.baudrate.0;
253253
assert!(brr >= 16, "impossible baud rate");
254254
self.usart.brr.write(|w| unsafe { w.bits(brr) });
255255

src/spi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use crate::gpio::gpiob::{PB13, PB14, PB15, PB3, PB4, PB5};
5151
#[cfg(feature = "connectivity")]
5252
use crate::gpio::gpioc::{PC10, PC11, PC12};
5353
use crate::gpio::{Alternate, Input};
54-
use crate::rcc::{Clocks, Enable, GetBusFreq, Reset};
54+
use crate::rcc::{BusClock, Clocks, Enable, Reset};
5555
use crate::time::Hertz;
5656

5757
use core::sync::atomic::{self, Ordering};
@@ -142,7 +142,7 @@ remap!(Spi3NoRemap, SPI3, false, PB3, PB4, PB5);
142142
remap!(Spi3Remap, SPI3, true, PC10, PC11, PC12);
143143

144144
pub trait Instance:
145-
crate::Sealed + Deref<Target = crate::pac::spi1::RegisterBlock> + Enable + Reset + GetBusFreq
145+
crate::Sealed + Deref<Target = crate::pac::spi1::RegisterBlock> + Enable + Reset + BusClock
146146
{
147147
}
148148

@@ -342,7 +342,7 @@ where
342342
// disable SS output
343343
spi.cr2.write(|w| w.ssoe().clear_bit());
344344

345-
let br = match SPI::get_frequency(&clocks).0 / freq.0 {
345+
let br = match SPI::clock(&clocks).0 / freq.0 {
346346
0 => unreachable!(),
347347
1..=2 => 0b000,
348348
3..=5 => 0b001,

src/timer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl Cancel for CountDownTimer<SYST> {
278278

279279
impl Periodic for CountDownTimer<SYST> {}
280280

281-
pub trait Instance: crate::Sealed + rcc::Enable + rcc::Reset + rcc::GetBusFreq {}
281+
pub trait Instance: crate::Sealed + rcc::Enable + rcc::Reset + rcc::BusTimerClock {}
282282

283283
impl<TIM> Timer<TIM>
284284
where
@@ -295,7 +295,7 @@ where
295295
}
296296

297297
Self {
298-
clk: TIM::get_timer_frequency(&clocks),
298+
clk: TIM::timer_clock(clocks),
299299
tim,
300300
}
301301
}

0 commit comments

Comments
 (0)