Skip to content

Commit

Permalink
lib: hint #[inline] in most of the HAL functions
Browse files Browse the repository at this point in the history
Signed-off-by: Zhouqi Jiang <[email protected]>
  • Loading branch information
luojia65 committed Jan 10, 2024
1 parent 7eb0c08 commit 7f59ce8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/fpioa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ $(
/// [functions]: ../functions/index.html
pub fn into_function<F: Function>(self, func: F) -> $IoX<F> {
let _ = func; // note(discard): Zero-sized typestate value
unsafe { &(*FPIOA::ptr()).io[$id].write(|w|
unsafe { (*FPIOA::ptr()).io[$id].write(|w|
w.bits(FUNCTION_DEFAULTS[F::INDEX as usize])
) };
$IoX { _function: PhantomData }
Expand Down
13 changes: 13 additions & 0 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ macro_rules! def_gpio_pins {
($($GPIOX: ident: ($num: expr, $gpiox: ident, $func: ident);)+) => {

impl GpioExt for pac::GPIO {
#[inline]
fn split(self, apb0: &mut APB0) -> Parts {
// enable APB0 bus
apb0.enable();
Expand Down Expand Up @@ -113,6 +114,7 @@ pub struct Gpio<GPIO, PIN, MODE> {
}

impl<GPIO: GpioIndex, PIN: Mode<GPIO::FUNC>> Gpio<GPIO, PIN, Unknown> {
#[inline]
pub fn new(gpio: GPIO, pin: PIN) -> Gpio<GPIO, PIN, Unknown> {
Gpio {
gpio,
Expand All @@ -123,12 +125,14 @@ impl<GPIO: GpioIndex, PIN: Mode<GPIO::FUNC>> Gpio<GPIO, PIN, Unknown> {
}

impl<GPIO, PIN, MODE> Gpio<GPIO, PIN, MODE> {
#[inline]
pub fn free(self) -> (GPIO, PIN) {
(self.gpio, self.pin)
}
}

impl<GPIO: GpioIndex, PIN: IoPin, MODE: Active> Gpio<GPIO, PIN, MODE> {
#[inline]
pub fn into_floating_input(mut self) -> Gpio<GPIO, PIN, Input<Floating>> {
self.pin.set_io_pull(Pull::None);
self.direction_in();
Expand All @@ -139,6 +143,7 @@ impl<GPIO: GpioIndex, PIN: IoPin, MODE: Active> Gpio<GPIO, PIN, MODE> {
}
}

#[inline]
pub fn into_pull_up_input(mut self) -> Gpio<GPIO, PIN, Input<PullUp>> {
self.pin.set_io_pull(Pull::Up);
self.direction_in();
Expand All @@ -149,6 +154,7 @@ impl<GPIO: GpioIndex, PIN: IoPin, MODE: Active> Gpio<GPIO, PIN, MODE> {
}
}

#[inline]
pub fn into_pull_down_input(mut self) -> Gpio<GPIO, PIN, Input<PullDown>> {
self.pin.set_io_pull(Pull::Down);
self.direction_in();
Expand All @@ -159,6 +165,7 @@ impl<GPIO: GpioIndex, PIN: IoPin, MODE: Active> Gpio<GPIO, PIN, MODE> {
}
}

#[inline]
pub fn into_push_pull_output(mut self) -> Gpio<GPIO, PIN, Output> {
self.pin.set_io_pull(Pull::Down);
self.direction_out();
Expand Down Expand Up @@ -192,13 +199,15 @@ impl<GPIO: GpioIndex, PIN, MODE> ErrorType for Gpio<GPIO, PIN, MODE> {
}

impl<GPIO: GpioIndex, PIN, MODE> InputPin for Gpio<GPIO, PIN, Input<MODE>> {
#[inline]
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(unsafe {
let p = &(*pac::GPIO::ptr()).data_input as *const _ as *const _;
u32_bit_is_set(p, GPIO::INDEX as usize)
})
}

#[inline]
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(unsafe {
let p = &(*pac::GPIO::ptr()).data_input as *const _ as *const _;
Expand All @@ -208,6 +217,7 @@ impl<GPIO: GpioIndex, PIN, MODE> InputPin for Gpio<GPIO, PIN, Input<MODE>> {
}

impl<GPIO: GpioIndex, PIN> OutputPin for Gpio<GPIO, PIN, Output> {
#[inline]
fn set_high(&mut self) -> Result<(), Self::Error> {
unsafe {
let p = &(*pac::GPIO::ptr()).data_output as *const _ as *mut _;
Expand All @@ -216,6 +226,7 @@ impl<GPIO: GpioIndex, PIN> OutputPin for Gpio<GPIO, PIN, Output> {
Ok(())
}

#[inline]
fn set_low(&mut self) -> Result<(), Self::Error> {
unsafe {
let p = &(*pac::GPIO::ptr()).data_output as *const _ as *mut _;
Expand All @@ -226,13 +237,15 @@ impl<GPIO: GpioIndex, PIN> OutputPin for Gpio<GPIO, PIN, Output> {
}

impl<GPIO: GpioIndex, PIN> StatefulOutputPin for Gpio<GPIO, PIN, Output> {
#[inline]
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok(unsafe {
let p = &(*pac::GPIO::ptr()).data_output as *const _ as *const _;
u32_bit_is_set(p, GPIO::INDEX as usize)
})
}

#[inline]
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok(unsafe {
let p = &(*pac::GPIO::ptr()).data_output as *const _ as *const _;
Expand Down
34 changes: 34 additions & 0 deletions src/gpiohs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct Gpiohs0<MODE> {
}

impl<MODE> Gpiohs0<MODE> {
#[inline]
pub fn into_pull_up_input(self) -> Gpiohs0<Input<PullUp>> {
GPIOHS::set_output_en(0, false);
GPIOHS::set_input_en(0, true);
Expand All @@ -60,6 +61,7 @@ bitflags::bitflags! {
}

impl<MODE> Gpiohs0<MODE> {
#[inline]
pub fn trigger_on_edge(&mut self, edge: Edge) {
// clear all pending bits
GPIOHS::clear_rise_ip(0);
Expand All @@ -73,6 +75,7 @@ impl<MODE> Gpiohs0<MODE> {
GPIOHS::set_low_ie(0, edge.contains(Edge::LOW));
}

#[inline]
pub fn check_edges(&self) -> Edge {
let mut ans = Edge::empty();
if GPIOHS::has_rise_ip(0) {
Expand All @@ -90,6 +93,7 @@ impl<MODE> Gpiohs0<MODE> {
ans
}

#[inline]
pub fn clear_interrupt_pending_bits(&mut self) {
if GPIOHS::has_rise_ie(0) {
GPIOHS::set_rise_ie(0, false);
Expand Down Expand Up @@ -120,13 +124,15 @@ impl<MODE> ErrorType for Gpiohs0<MODE> {
}

impl<MODE> InputPin for Gpiohs0<Input<MODE>> {
#[inline]
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(unsafe {
let p = &(*GPIOHS::ptr()).input_val as *const _ as *const _;
u32_bit_is_set(p, 0)
})
}

#[inline]
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(unsafe {
let p = &(*GPIOHS::ptr()).input_val as *const _ as *const _;
Expand All @@ -136,6 +142,7 @@ impl<MODE> InputPin for Gpiohs0<Input<MODE>> {
}

impl<MODE> OutputPin for Gpiohs0<Output<MODE>> {
#[inline]
fn set_high(&mut self) -> Result<(), Self::Error> {
unsafe {
let p = &(*GPIOHS::ptr()).output_val as *const _ as *mut _;
Expand All @@ -144,6 +151,7 @@ impl<MODE> OutputPin for Gpiohs0<Output<MODE>> {
Ok(())
}

#[inline]
fn set_low(&mut self) -> Result<(), Self::Error> {
unsafe {
let p = &(*GPIOHS::ptr()).output_val as *const _ as *mut _;
Expand All @@ -156,174 +164,199 @@ impl<MODE> OutputPin for Gpiohs0<Output<MODE>> {
trait GpiohsAccess {
fn peripheral() -> &'static mut crate::pac::gpiohs::RegisterBlock;

#[inline]
fn set_drive(index: usize, bit: bool) {
unsafe {
let p = &mut Self::peripheral().drive as *mut _ as *mut _;
u32_set_bit(p, bit, index);
}
}

#[inline]
fn input_value(index: usize) -> bool {
unsafe {
let p = &mut Self::peripheral().input_val as *mut _ as *mut _;
u32_bit_is_set(p, index)
}
}

#[inline]
fn set_input_en(index: usize, bit: bool) {
unsafe {
let p = &mut Self::peripheral().input_en as *mut _ as *mut _;
u32_set_bit(p, bit, index);
}
}

#[inline]
fn set_iof_en(index: usize, bit: bool) {
unsafe {
let p = &mut Self::peripheral().iof_en as *mut _ as *mut _;
u32_set_bit(p, bit, index);
}
}

#[inline]
fn set_iof_sel(index: usize, bit: bool) {
unsafe {
let p = &mut Self::peripheral().iof_sel as *mut _ as *mut _;
u32_set_bit(p, bit, index);
}
}

#[inline]
fn set_output_en(index: usize, bit: bool) {
unsafe {
let p = &mut Self::peripheral().output_en as *mut _ as *mut _;
u32_set_bit(p, bit, index);
}
}

#[inline]
fn set_output_value(index: usize, bit: bool) {
unsafe {
let p = &mut Self::peripheral().output_val as *mut _ as *mut _;
u32_set_bit(p, bit, index);
}
}

#[inline]
fn set_output_xor(index: usize, bit: bool) {
unsafe {
let p = &mut Self::peripheral().output_xor as *mut _ as *mut _;
u32_set_bit(p, bit, index);
}
}

#[inline]
fn set_pullup_en(index: usize, bit: bool) {
unsafe {
let p = &mut Self::peripheral().pullup_en as *mut _ as *mut _;
u32_set_bit(p, bit, index);
}
}

#[inline]
fn set_rise_ie(index: usize, bit: bool) {
unsafe {
let p = &mut Self::peripheral().rise_ie as *mut _ as *mut _;
u32_set_bit(p, bit, index);
}
}

#[inline]
fn clear_rise_ip(index: usize) {
unsafe {
let p = &mut Self::peripheral().rise_ip as *mut _ as *mut _;
u32_set_bit(p, true, index);
}
}

#[inline]
fn set_fall_ie(index: usize, bit: bool) {
unsafe {
let p = &mut Self::peripheral().fall_ie as *mut _ as *mut _;
u32_set_bit(p, bit, index);
}
}

#[inline]
fn clear_fall_ip(index: usize) {
unsafe {
let p = &mut Self::peripheral().fall_ip as *mut _ as *mut _;
u32_set_bit(p, true, index);
}
}

#[inline]
fn set_high_ie(index: usize, bit: bool) {
unsafe {
let p = &mut Self::peripheral().high_ie as *mut _ as *mut _;
u32_set_bit(p, bit, index);
}
}

#[inline]
fn clear_high_ip(index: usize) {
unsafe {
let p = &mut Self::peripheral().high_ip as *mut _ as *mut _;
u32_set_bit(p, true, index);
}
}

#[inline]
fn set_low_ie(index: usize, bit: bool) {
unsafe {
let p = &mut Self::peripheral().low_ie as *mut _ as *mut _;
u32_set_bit(p, bit, index);
}
}

#[inline]
fn clear_low_ip(index: usize) {
unsafe {
let p = &mut Self::peripheral().low_ip as *mut _ as *mut _;
u32_set_bit(p, true, index);
}
}

#[inline]
fn has_rise_ie(index: usize) -> bool {
unsafe {
let p = &mut Self::peripheral().rise_ie as *mut _ as *mut _;
u32_bit_is_set(p, index)
}
}

#[inline]
fn has_fall_ie(index: usize) -> bool {
unsafe {
let p = &mut Self::peripheral().fall_ie as *mut _ as *mut _;
u32_bit_is_set(p, index)
}
}

#[inline]
fn has_high_ie(index: usize) -> bool {
unsafe {
let p = &mut Self::peripheral().high_ie as *mut _ as *mut _;
u32_bit_is_set(p, index)
}
}

#[inline]
fn has_low_ie(index: usize) -> bool {
unsafe {
let p = &mut Self::peripheral().low_ie as *mut _ as *mut _;
u32_bit_is_set(p, index)
}
}

#[inline]
fn has_rise_ip(index: usize) -> bool {
unsafe {
let p = &mut Self::peripheral().rise_ip as *mut _ as *mut _;
u32_bit_is_set(p, index)
}
}

#[inline]
fn has_fall_ip(index: usize) -> bool {
unsafe {
let p = &mut Self::peripheral().fall_ip as *mut _ as *mut _;
u32_bit_is_set(p, index)
}
}

#[inline]
fn has_high_ip(index: usize) -> bool {
unsafe {
let p = &mut Self::peripheral().high_ip as *mut _ as *mut _;
u32_bit_is_set(p, index)
}
}

#[inline]
fn has_low_ip(index: usize) -> bool {
unsafe {
let p = &mut Self::peripheral().low_ip as *mut _ as *mut _;
Expand All @@ -333,6 +366,7 @@ trait GpiohsAccess {
}

impl GpiohsAccess for GPIOHS {
#[inline]
fn peripheral() -> &'static mut crate::pac::gpiohs::RegisterBlock {
unsafe { &mut *(GPIOHS::ptr() as *mut _) }
}
Expand Down
Loading

0 comments on commit 7f59ce8

Please sign in to comment.