Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPIO - Use nice new pac methods #158

Draft
wants to merge 2 commits into
base: staged-pac
Choose a base branch
from
Draft
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
167 changes: 77 additions & 90 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,13 @@ macro_rules! gpio {

fn set_high(&mut self) -> Result<(), ()> {
// NOTE(unsafe) atomic write to a stateless register
unsafe { (*$GPIOX::ptr()).bsrr().write(|w| w.bits(1 << self.i)) };
unsafe { (*$GPIOX::ptr()).bsrr().write(|w| w.bs(self.i).set_bit()) };
Ok(())
}

fn set_low(&mut self) -> Result<(), ()> {
// NOTE(unsafe) atomic write to a stateless register
unsafe { (*$GPIOX::ptr()).bsrr().write(|w| w.bits(1 << (self.i + 16))) };
unsafe { (*$GPIOX::ptr()).bsrr().write(|w| w.br(self.i).set_bit()) };
Ok(())
}
}
Expand All @@ -296,7 +296,7 @@ macro_rules! gpio {

fn is_set_low(&self) -> Result<bool, ()> {
// NOTE(unsafe) atomic read with no side effects
let is_set_low = unsafe { (*$GPIOX::ptr()).odr().read().bits() & (1 << self.i) == 0 };
let is_set_low = unsafe { (*$GPIOX::ptr()).odr().read().odr(self.i).is_low() };
Ok(is_set_low)
}
}
Expand All @@ -314,7 +314,7 @@ macro_rules! gpio {

fn is_low(&self) -> Result<bool, ()> {
// NOTE(unsafe) atomic read with no side effects
let is_low = unsafe { (*$GPIOX::ptr()).idr().read().bits() & (1 << self.i) == 0 };
let is_low = unsafe { (*$GPIOX::ptr()).idr().read().idr(self.i).is_low() };
Ok(is_low)
}
}
Expand All @@ -329,7 +329,7 @@ macro_rules! gpio {

fn is_low(&self) -> Result<bool, ()> {
// NOTE(unsafe) atomic read with no side effects
let is_low = unsafe { (*$GPIOX::ptr()).idr().read().bits() & (1 << self.i) == 0 };
let is_low = unsafe { (*$GPIOX::ptr()).idr().read().idr(self.i).is_low() };
Ok(is_low)
}
}
Expand Down Expand Up @@ -382,97 +382,89 @@ macro_rules! gpio {
impl<MODE> $PXi<MODE> {
/// Configures the pin to operate as a floating input pin
pub fn into_floating_input(self) -> $PXi<Input<Floating>> {
let offset = 2 * $i;
unsafe {
cortex_m::interrupt::free(|_| unsafe {
let gpio = &(*$GPIOX::ptr());
gpio.pupdr().modify(|r, w| {
w.bits(r.bits() & !(0b11 << offset))
gpio.pupdr().modify(|_, w| {
w.pupdr($i).floating()
});
gpio.moder().modify(|r, w| {
w.bits(r.bits() & !(0b11 << offset))
gpio.moder().modify(|_, w| {
w.moder($i).input()
})
};
});
$PXi { _mode: PhantomData }
}

/// Configures the pin to operate as a pulled down input pin
pub fn into_pull_down_input(self) -> $PXi<Input<PullDown>> {
let offset = 2 * $i;
unsafe {
cortex_m::interrupt::free(|_| unsafe {
let gpio = &(*$GPIOX::ptr());
gpio.pupdr().modify(|r, w| {
w.bits((r.bits() & !(0b11 << offset)) | (0b10 << offset))
gpio.pupdr().modify(|_, w| {
w.pupdr($i).pull_down()
});
gpio.moder().modify(|r, w| {
w.bits(r.bits() & !(0b11 << offset))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should not have removed this

})
};

});
$PXi { _mode: PhantomData }
}

/// Configures the pin to operate as a pulled up input pin
pub fn into_pull_up_input(self) -> $PXi<Input<PullUp>> {
let offset = 2 * $i;
unsafe {
cortex_m::interrupt::free(|_| unsafe {
let gpio = &(*$GPIOX::ptr());
gpio.pupdr().modify(|r, w| {
w.bits((r.bits() & !(0b11 << offset)) | (0b01 << offset))
gpio.pupdr().modify(|_, w| {
w.pupdr($i).pull_up()
});
gpio.moder().modify(|r, w| {
w.bits(r.bits() & !(0b11 << offset))
gpio.moder().modify(|_, w| {
w.moder($i).input()
})
};
});
$PXi { _mode: PhantomData }
}

/// Configures the pin to operate as an analog pin
pub fn into_analog(self) -> $PXi<Analog> {
let offset = 2 * $i;
unsafe {
cortex_m::interrupt::free(|_| unsafe {
let gpio = &(*$GPIOX::ptr());
gpio.pupdr().modify(|r, w| {
w.bits(r.bits() & !(0b11 << offset))
gpio.pupdr().modify(|_, w| {
w.pupdr($i).floating()
});
gpio.moder().modify(|r, w| {
w.bits((r.bits() & !(0b11 << offset)) | (0b11 << offset))
});
}
gpio.moder().modify(|_, w| {
w.moder($i).analog()
})
});
$PXi { _mode: PhantomData }
}

/// Configures the pin to operate as an open drain output pin
pub fn into_open_drain_output(self) -> $PXi<Output<OpenDrain>> {
let offset = 2 * $i;
unsafe {
cortex_m::interrupt::free(|_| unsafe {
let gpio = &(*$GPIOX::ptr());
gpio.pupdr().modify(|r, w| {
w.bits(r.bits() & !(0b11 << offset))
gpio.pupdr().modify(|_, w| {
w.pupdr($i).floating()
});
gpio.otyper().modify(|r, w| {
w.bits(r.bits() | (0b1 << $i))
gpio.otyper().modify(|_, w| {
w.ot($i).open_drain()
});
gpio.moder().modify(|r, w| {
w.bits((r.bits() & !(0b11 << offset)) | (0b01 << offset))
})
};
gpio.moder().modify(|_, w| {
w.moder($i).output()
});
});
$PXi { _mode: PhantomData }
}

/// Configures the pin to operate as an push pull output pin
pub fn into_push_pull_output(self) -> $PXi<Output<PushPull>> {
let offset = 2 * $i;
unsafe {
cortex_m::interrupt::free(|_| unsafe {
let gpio = &(*$GPIOX::ptr());
gpio.pupdr().modify(|r, w| {
w.bits(r.bits() & !(0b11 << offset))
gpio.pupdr().modify(|_, w| {
w.pupdr($i).floating()
});
gpio.otyper().modify(|r, w| {
w.bits(r.bits() & !(0b1 << $i))
gpio.otyper().modify(|_, w| {
w.ot($i).push_pull()
});
gpio.moder().modify(|r, w| {
w.bits((r.bits() & !(0b11 << offset)) | (0b01 << offset))
})
};
gpio.moder().modify(|_, w| {
w.moder($i).output()
});
});
$PXi { _mode: PhantomData }
}

Expand All @@ -484,64 +476,59 @@ macro_rules! gpio {

/// Set pin speed
pub fn set_speed(self, speed: Speed) -> Self {
let offset = 2 * $i;
unsafe {
(*$GPIOX::ptr()).ospeedr().modify(|r, w| {
w.bits((r.bits() & !(0b11 << offset)) | ((speed as u32) << offset))
cortex_m::interrupt::free(|_| unsafe {
(*$GPIOX::ptr()).ospeedr().modify(|_, w| {
w.ospeedr($i).set(speed as u8)
});
}
});
self
}

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 {
let mode = A as u8;
cortex_m::interrupt::free(|_| unsafe {
let gpio = &(*$GPIOX::ptr());
if offset2 < 32 {
gpio.afrl().modify(|r, w| {
w.bits((r.bits() & !(0b1111 << offset2)) | (mode << offset2))
if $i < 8 {
gpio.afrl().modify(|_, w| {
w.afr($i).set(mode)
});
} else {
let offset2 = offset2 - 32;
gpio.afrh().modify(|r, w| {
w.bits((r.bits() & !(0b1111 << offset2)) | (mode << offset2))
let i = $i - 8;
gpio.afrh().modify(|_, w| {
w.afr(i).set(mode)
});
}
gpio.moder().modify(|r, w| {
w.bits((r.bits() & !(0b11 << offset)) | (0b10 << offset))
gpio.moder().modify(|_, w| {
w.moder($i).alternate()
});
gpio.otyper().modify(|r, w| {
w.bits(r.bits() & !(0b1 << $i))
gpio.otyper().modify(|_, w| {
w.ot($i).push_pull()
});
}
});
$PXi { _mode: PhantomData }
}

pub fn into_alternate_open_drain<const A: u8>(self) -> $PXi<AlternateOD<A>> {
let mode = A as u32;
let offset = 2 * $i;
let offset2 = 4 * $i;
unsafe {
let mode = A as u8;
cortex_m::interrupt::free(|_| unsafe {
let gpio = &(*$GPIOX::ptr());
if offset2 < 32 {
gpio.afrl().modify(|r, w| {
w.bits((r.bits() & !(0b1111 << offset2)) | (mode << offset2))
if $i < 8 {
gpio.afrl().modify(|_, w| {
w.afr($i).set(mode)
});
} else {
let offset2 = offset2 - 32;
gpio.afrh().modify(|r, w| {
w.bits((r.bits() & !(0b1111 << offset2)) | (mode << offset2))
let i = $i - 8;
gpio.afrh().modify(|_, w| {
w.afr(i).set(mode)
});
}
gpio.otyper().modify(|r, w| {
w.bits(r.bits() | (0b1 << $i))
gpio.moder().modify(|_, w| {
w.moder($i).alternate()
});
gpio.moder().modify(|r, w| {
w.bits((r.bits() & !(0b11 << offset)) | (0b10 << offset))
gpio.otyper().modify(|_, w| {
w.ot($i).open_drain()
});
}
});
$PXi { _mode: PhantomData }
}
}
Expand Down
Loading