Skip to content

Commit

Permalink
generic: improve bit operations usage
Browse files Browse the repository at this point in the history
  • Loading branch information
LuigiPiucco committed Jan 28, 2024
1 parent 4dbd683 commit 435b15d
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions avr-hal-generic/src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,10 @@ macro_rules! impl_port_traditional {
}

impl Dynamic {
fn new(port: DynamicPort, pin_num: u8) -> Self {
fn new(port: DynamicPort, num: u8) -> Self {
Self {
port,
mask: 1 << pin_num,
mask: 1u8 << num,
}
}
}
Expand Down Expand Up @@ -653,16 +653,18 @@ macro_rules! impl_port_traditional {
#[inline]
unsafe fn out_get(&self) -> bool {
match self.port {
$(DynamicPort::[<PORT $name>] => (*<$port>::ptr()).[<port $name:lower>].read().bits()
& self.mask != 0,)+
$(DynamicPort::[<PORT $name>] => {
(*<$port>::ptr()).[<port $name:lower>].read().bits() & self.mask != 0
})+
}
}

#[inline]
unsafe fn in_get(&self) -> bool {
match self.port {
$(DynamicPort::[<PORT $name>] => (*<$port>::ptr()).[<pin $name:lower>].read().bits()
& self.mask != 0,)+
$(DynamicPort::[<PORT $name>] => {
(*<$port>::ptr()).[<pin $name:lower>].read().bits() & self.mask != 0
})+
}
}

Expand Down Expand Up @@ -707,44 +709,46 @@ macro_rules! impl_port_traditional {

#[inline]
unsafe fn out_set(&mut self) {
(*<$port>::ptr()).[<port $name:lower>].modify(|r, w| {
w.bits(r.bits() | (1 << $pin))
(*<$port>::ptr()).[<port $name:lower>].modify(|_, w| {
w.[<p $name:lower $pin>]().set_bit()
})
}

#[inline]
unsafe fn out_clear(&mut self) {
(*<$port>::ptr()).[<port $name:lower>].modify(|r, w| {
w.bits(r.bits() & !(1 << $pin))
(*<$port>::ptr()).[<port $name:lower>].modify(|_, w| {
w.[<p $name:lower $pin>]().clear_bit()
})
}

#[inline]
unsafe fn out_toggle(&mut self) {
(*<$port>::ptr()).[<pin $name:lower>].write(|w| w.bits(1 << $pin))
(*<$port>::ptr()).[<pin $name:lower>].modify(|_, w| {
w.[<p $name:lower $pin>]().set_bit()
})
}

#[inline]
unsafe fn out_get(&self) -> bool {
(*<$port>::ptr()).[<port $name:lower>].read().bits() & (1 << $pin) != 0
(*<$port>::ptr()).[<port $name:lower>].read().[<p $name:lower $pin>]().bit()
}

#[inline]
unsafe fn in_get(&self) -> bool {
(*<$port>::ptr()).[<pin $name:lower>].read().bits() & (1 << $pin) != 0
(*<$port>::ptr()).[<pin $name:lower>].read().[<p $name:lower $pin>]().bit()
}

#[inline]
unsafe fn make_output(&mut self) {
(*<$port>::ptr()).[<ddr $name:lower>].modify(|r, w| {
w.bits(r.bits() | (1 << $pin))
(*<$port>::ptr()).[<ddr $name:lower>].modify(|_, w| {
w.[<p $name:lower $pin>]().set_bit()
})
}

#[inline]
unsafe fn make_input(&mut self, pull_up: bool) {
(*<$port>::ptr()).[<ddr $name:lower>].modify(|r, w| {
w.bits(r.bits() & !(1 << $pin))
(*<$port>::ptr()).[<ddr $name:lower>].modify(|_, w| {
w.[<p $name:lower $pin>]().clear_bit()
});
if pull_up {
self.out_set()
Expand Down

0 comments on commit 435b15d

Please sign in to comment.