Skip to content
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
28 changes: 14 additions & 14 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ pub enum SignalEdge {
RisingFalling,
}

#[allow(dead_code)]
pub(crate) enum AltFunction {
AF0 = 0,
AF1 = 1,
AF2 = 2,
AF3 = 3,
AF4 = 4,
AF5 = 5,
AF6 = 6,
AF7 = 7,
}
/// Altername Mode (type state)
pub struct Alternate<const A: u8>;

pub const AF0: u8 = 0;
pub const AF1: u8 = 1;
pub const AF2: u8 = 2;
pub const AF3: u8 = 3;
pub const AF4: u8 = 4;
pub const AF5: u8 = 5;
pub const AF6: u8 = 6;
pub const AF7: u8 = 7;

/// External Interrupt Pin
pub trait ExtiPin {
Expand Down Expand Up @@ -487,9 +487,8 @@ macro_rules! gpio {
self
}

#[allow(dead_code)]
pub(crate) fn set_alt_mode(&self, mode: AltFunction) {
let mode = mode as u32;
pub fn into_alternate<const A: u8>(self) -> $PXi<Alternate<A>> {
let mode = A as u32;
let offset = 2 * $i;
let offset2 = 4 * $i;
unsafe {
Expand All @@ -508,6 +507,7 @@ macro_rules! gpio {
w.bits((r.bits() & !(0b11 << offset)) | (0b10 << offset))
});
}
$PXi { _mode: PhantomData }
}
}

Expand Down
26 changes: 14 additions & 12 deletions src/rcc/clockout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::stm32::RCC;
pub type LscoPin = gpioa::PA2<DefaultMode>;

pub struct Lsco {
pin: LscoPin,
pin: gpioa::PA2<Alternate<AF0>>,
}

impl Lsco {
Expand All @@ -20,7 +20,7 @@ impl Lsco {
}

pub fn release(self) -> LscoPin {
self.pin
self.pin.into_floating_input()
}
}

Expand All @@ -30,7 +30,6 @@ pub trait LSCOExt {

impl LSCOExt for LscoPin {
fn lsco(self, src: LSCOSrc, rcc: &mut Rcc) -> Lsco {
self.set_alt_mode(AltFunction::AF0);
let src_select_bit = match src {
LSCOSrc::LSE => {
rcc.enable_lse(false);
Expand All @@ -43,7 +42,9 @@ impl LSCOExt for LscoPin {
};
rcc.unlock_rtc();
rcc.rb.bdcr.modify(|_, w| w.lscosel().bit(src_select_bit));
Lsco { pin: self }
Lsco {
pin: self.into_alternate(),
}
}
}

Expand All @@ -65,7 +66,7 @@ impl<PIN> Mco<PIN> {
}

pub fn release(self) -> PIN {
self.pin
self.pin //TODO reverse pin to input<floating>
}
}

Expand All @@ -74,12 +75,10 @@ pub trait MCOExt<PIN> {
}

macro_rules! mco {
($($PIN:ty),+) => {
($($PIN:ident),+) => {
$(
impl MCOExt<$PIN> for $PIN {
fn mco(self, src: MCOSrc, psc: Prescaler, rcc: &mut Rcc) -> Mco<$PIN> {
self.set_alt_mode(AltFunction::AF0);

impl MCOExt<$PIN<Alternate<AF0>>> for $PIN<DefaultMode> {
fn mco(self, src: MCOSrc, psc: Prescaler, rcc: &mut Rcc) -> Mco<$PIN<Alternate<AF0>>> {
let psc_bits = match psc {
Prescaler::NotDivided => 0b000,
Prescaler::Div2 => 0b001,
Expand Down Expand Up @@ -114,11 +113,14 @@ macro_rules! mco {
0b111
},
};
Mco { src_bits, pin: self }
Mco { src_bits, pin: self.into_alternate() }
}
}
)+
};
}

mco!(gpioa::PA8<DefaultMode>, gpiog::PG10<DefaultMode>);
use crate::gpio::gpioa::PA8;
use crate::gpio::gpiog::PG10;

mco!(PA8, PG10);