Skip to content

Commit

Permalink
PinMode u32
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Jul 24, 2021
1 parent 22c9b68 commit 6b2b0c8
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions src/gpio/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,22 +600,23 @@ impl<MODE, const P: char, const N: u8> Pin<MODE, P, N> {
///
/// This violates the type state constraints from `MODE`, so callers must
/// ensure they use this properly.
#[inline(always)]
fn mode<M: PinMode>(&mut self) {
let offset = 2 * N;
unsafe {
(*Gpio::<P>::ptr()).pupdr.modify(|r, w| {
w.bits((r.bits() & !(0b11 << offset)) | (u32::from(M::PUPDR) << offset))
});
(*Gpio::<P>::ptr())
.pupdr
.modify(|r, w| w.bits((r.bits() & !(0b11 << offset)) | (M::PUPDR << offset)));

if let Some(otyper) = M::OTYPER {
(*Gpio::<P>::ptr())
.otyper
.modify(|r, w| w.bits(r.bits() & !(0b1 << N) | (u32::from(otyper) << N)));
.modify(|r, w| w.bits(r.bits() & !(0b1 << N) | (otyper << N)));
}

(*Gpio::<P>::ptr()).moder.modify(|r, w| {
w.bits((r.bits() & !(0b11 << offset)) | (u32::from(M::MODER) << offset))
});
(*Gpio::<P>::ptr())
.moder
.modify(|r, w| w.bits((r.bits() & !(0b11 << offset)) | (M::MODER << offset)));
}
}
}
Expand Down Expand Up @@ -722,47 +723,47 @@ pub trait PinMode: crate::Sealed {
// They are not part of public API.

#[doc(hidden)]
const PUPDR: u8;
const PUPDR: u32;
#[doc(hidden)]
const MODER: u8;
const MODER: u32;
#[doc(hidden)]
const OTYPER: Option<u8> = None;
const OTYPER: Option<u32> = None;
}

impl crate::Sealed for Input<Floating> {}
impl PinMode for Input<Floating> {
const PUPDR: u8 = 0b00;
const MODER: u8 = 0b00;
const PUPDR: u32 = 0b00;
const MODER: u32 = 0b00;
}

impl crate::Sealed for Input<PullDown> {}
impl PinMode for Input<PullDown> {
const PUPDR: u8 = 0b10;
const MODER: u8 = 0b00;
const PUPDR: u32 = 0b10;
const MODER: u32 = 0b00;
}

impl crate::Sealed for Input<PullUp> {}
impl PinMode for Input<PullUp> {
const PUPDR: u8 = 0b01;
const MODER: u8 = 0b00;
const PUPDR: u32 = 0b01;
const MODER: u32 = 0b00;
}

impl crate::Sealed for Analog {}
impl PinMode for Analog {
const PUPDR: u8 = 0b00;
const MODER: u8 = 0b11;
const PUPDR: u32 = 0b00;
const MODER: u32 = 0b11;
}

impl crate::Sealed for Output<OpenDrain> {}
impl PinMode for Output<OpenDrain> {
const PUPDR: u8 = 0b00;
const MODER: u8 = 0b01;
const OTYPER: Option<u8> = Some(0b1);
const PUPDR: u32 = 0b00;
const MODER: u32 = 0b01;
const OTYPER: Option<u32> = Some(0b1);
}

impl crate::Sealed for Output<PushPull> {}
impl PinMode for Output<PushPull> {
const PUPDR: u8 = 0b00;
const MODER: u8 = 0b01;
const OTYPER: Option<u8> = Some(0b0);
const PUPDR: u32 = 0b00;
const MODER: u32 = 0b01;
const OTYPER: Option<u32> = Some(0b0);
}

0 comments on commit 6b2b0c8

Please sign in to comment.