Skip to content

Commit

Permalink
Merge pull request #239 from braun-embedded/clock-source
Browse files Browse the repository at this point in the history
Finish my current round of clock source clean-up
  • Loading branch information
david-sawatzke committed May 26, 2020
2 parents 0abc88b + 78d9bf1 commit 71e4aac
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 138 deletions.
2 changes: 1 addition & 1 deletion examples/spi_apa102.rs
Expand Up @@ -39,7 +39,7 @@ fn main() -> ! {
.assign(miso_pin, &mut handle);

#[cfg(feature = "82x")]
let spi_clock = spi::Clock::new(0);
let spi_clock = spi::Clock::new(&(), 0);
#[cfg(feature = "845")]
let spi_clock = spi::Clock::new(&syscon.iosc, 0);

Expand Down
56 changes: 54 additions & 2 deletions src/i2c/clock.rs
Expand Up @@ -10,6 +10,25 @@ pub struct Clock<Clock> {
pub(crate) _clock: PhantomData<Clock>,
}

impl<C> Clock<C>
where
C: ClockSource,
{
/// Create the clock config for the I2C peripheral
///
/// `mstclhigh` and `mstcllow` have to be between 2-9.
pub fn new(_: &C, divval: u16, mstsclhigh: u8, mstscllow: u8) -> Self {
assert!(mstsclhigh > 1 && mstsclhigh < 10);
assert!(mstscllow > 1 && mstscllow < 10);
Self {
divval,
mstsclhigh: mstsclhigh - 2,
mstscllow: mstscllow - 2,
_clock: PhantomData,
}
}
}

/// Implemented for I2C clock sources
pub trait ClockSource: private::Sealed {
/// Select the clock source
Expand All @@ -27,9 +46,11 @@ pub trait ClockSource: private::Sealed {

#[cfg(feature = "82x")]
mod target {
use core::marker::PhantomData;

use crate::syscon;

use super::ClockSource;
use super::{Clock, ClockSource};

impl super::private::Sealed for () {}

Expand All @@ -39,16 +60,33 @@ mod target {
// default
}
}

impl Clock<()> {
/// Create a new i2c clock config for 400 kHz
///
/// Assumes the internal oscillator runs at 12 MHz
pub fn new_400khz() -> Self {
Self {
divval: 5,
mstsclhigh: 0,
mstscllow: 1,
_clock: PhantomData,
}
}
}
}

#[cfg(feature = "845")]
mod target {
use core::marker::PhantomData;

use crate::syscon::{
self,
clock_source::{PeripheralClock, PeripheralClockSelector},
IOSC,
};

use super::ClockSource;
use super::{Clock, ClockSource};

impl<T> super::private::Sealed for T where T: PeripheralClock {}
impl<T> ClockSource for T
Expand All @@ -62,6 +100,20 @@ mod target {
T::select(selector, handle);
}
}

impl Clock<IOSC> {
/// Create a new i2c clock config for 400 kHz
///
/// Assumes the internal oscillator runs at 12 MHz
pub fn new_400khz() -> Self {
Self {
divval: 5,
mstsclhigh: 0,
mstscllow: 1,
_clock: PhantomData,
}
}
}
}

mod private {
Expand Down
2 changes: 1 addition & 1 deletion src/spi.rs
Expand Up @@ -32,7 +32,7 @@
//! .assign(p.pins.pio0_15.into_swm_pin(), &mut swm_handle);
//!
//! #[cfg(feature = "82x")]
//! let spi_clock = spi::Clock::new(0);
//! let spi_clock = spi::Clock::new(&(), 0);
//! #[cfg(feature = "845")]
//! let spi_clock = spi::Clock::new(&syscon.iosc, 0);
//!
Expand Down
13 changes: 13 additions & 0 deletions src/spi/clock.rs
Expand Up @@ -9,6 +9,19 @@ pub struct Clock<Clock> {
pub(crate) _clock: PhantomData<Clock>,
}

impl<C> Clock<C>
where
C: ClockSource,
{
/// Create the clock config for the SPI peripheral
pub fn new(_: &C, divval: u16) -> Self {
Self {
divval,
_clock: PhantomData,
}
}
}

/// Implemented for SPI clock sources
pub trait ClockSource: private::Sealed {
/// Select the clock source
Expand Down
53 changes: 43 additions & 10 deletions src/syscon/clock_source.rs
@@ -1,15 +1,5 @@
//! Clock configuration for the peripherals

#[cfg(feature = "82x")]
mod clocksource_82x;
#[cfg(feature = "845")]
mod clocksource_845;

#[cfg(feature = "82x")]
pub use clocksource_82x::*;
#[cfg(feature = "845")]
pub use clocksource_845::*;

use crate::syscon;

/// Internal trait used configure peripheral clock sources
Expand Down Expand Up @@ -69,3 +59,46 @@ impl AdcClock {
Self { caldiv: 24, div: 0 }
}
}

#[cfg(feature = "845")]
mod target {
use crate::{
pac::syscon::fclksel::SEL_A,
syscon::{
self,
frg::{FRG, FRG0, FRG1},
IOSC,
},
};

use super::{PeripheralClock, PeripheralClockSelector};

macro_rules! peripheral_clocks {
(
$(
$clock:ty,
$sel:ident;
)*
) => {
$(
impl PeripheralClock for $clock {
const CLOCK: SEL_A = SEL_A::$sel;

fn select<S>(_: &S, syscon: &mut syscon::Handle)
where
S: PeripheralClockSelector,
{
syscon.fclksel[S::REGISTER_NUM]
.write(|w| w.sel().variant(Self::CLOCK));
}
}
)*
};
}

peripheral_clocks!(
FRG<FRG0>, FRG0CLK;
FRG<FRG1>, FRG1CLK;
IOSC, FRO;
);
}
41 changes: 0 additions & 41 deletions src/syscon/clock_source/clocksource_82x.rs

This file was deleted.

83 changes: 0 additions & 83 deletions src/syscon/clock_source/clocksource_845.rs

This file was deleted.

0 comments on commit 71e4aac

Please sign in to comment.