Skip to content

Commit

Permalink
internal_resistor
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Oct 11, 2024
1 parent 7453ea4 commit 91b3248
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
4 changes: 2 additions & 2 deletions examples/mfrc522.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ fn main() -> ! {

let clocks = rcc.cfgr.freeze(&mut flash.acr);

let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
let sck = gpioa.pa5;
let miso = gpioa.pa6;
let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
let mosi = gpioa.pa7;
let spi = Spi::new(
dp.SPI1,
(sck, miso, mosi, &mut afio.mapr),
Expand Down
56 changes: 43 additions & 13 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,40 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
self
}
}
impl<const P: char, const N: u8> Pin<P, N, Input> {
/// Set the internal pull-up and pull-down resistor
pub fn set_internal_resistor(&mut self, pull: Pull) {
match pull {
Pull::None => {
let gpio = unsafe { &(*gpiox::<P>()) };
match N {
0..=7 => {
gpio.crl().modify(|_, w| w.cnf(N).variant(Cnf::OpenDrain));
}
8..=15 => {
gpio.crh()
.modify(|_, w| unsafe { w.cnf(N - 16).bits(Cnf::OpenDrain as u8) });
}
_ => unreachable!(),
}
}
Pull::Up => {
self.mode::<PulledInput>();
self._set_pull_up()
}
Pull::Down => {
self.mode::<PulledInput>();
self._set_pull_down()
}
}
}

/// Set the internal pull-up and pull-down resistor
pub fn internal_resistor(mut self, pull: Pull) -> Self {
self.set_internal_resistor(pull);
self
}
}

impl<const P: char, const N: u8> Pin<P, N, Output<OpenDrain>> {
#[inline]
Expand Down Expand Up @@ -787,9 +821,7 @@ macro_rules! impl_temp_output {
&mut self,
_cr: &mut <Self as HL>::Cr,
mut f: impl FnMut(&mut Pin<P, N, $mode>),
) where
Pin<P, N, $mode>: HL,
{
) {
self.mode::<$mode>();
let mut temp = Pin::<P, N, $mode>::new();
f(&mut temp);
Expand All @@ -807,9 +839,7 @@ macro_rules! impl_temp_output {
_cr: &mut <Self as HL>::Cr,
state: PinState,
mut f: impl FnMut(&mut Pin<P, N, $mode>),
) where
Pin<P, N, $mode>: HL,
{
) {
self._set_state(state);
self.mode::<$mode>();
let mut temp = Pin::<P, N, $mode>::new();
Expand All @@ -826,9 +856,7 @@ macro_rules! impl_temp_input {
&mut self,
_cr: &mut <Self as HL>::Cr,
mut f: impl FnMut(&mut Pin<P, N, Input>),
)
where Pin::<P, N, Input>: HL
{
) {
self.mode::<$mode>();
let mut temp = Pin::<P, N, Input>::new() $(.$pull())?;
f(&mut temp);
Expand Down Expand Up @@ -975,10 +1003,7 @@ impl PinMode for Alternate<OpenDrain> {
const CNF: Cnf = Cnf::AltOpenDrain;
}

impl<const P: char, const N: u8, M> Pin<P, N, M>
where
Self: HL,
{
impl<const P: char, const N: u8, M> Pin<P, N, M> {
fn mode<MODE: PinMode>(&mut self) {
let gpio = unsafe { &(*gpiox::<P>()) };

Expand All @@ -996,7 +1021,12 @@ where
_ => unreachable!(),
}
}
}

impl<const P: char, const N: u8, M> Pin<P, N, M>
where
Self: HL,
{
#[inline]
pub(crate) fn into_mode<MODE: PinMode>(
mut self,
Expand Down

0 comments on commit 91b3248

Please sign in to comment.