Skip to content

Commit

Permalink
Rename GPIO "offset" to "mask"
Browse files Browse the repository at this point in the history
The usage resembles a bitmask with a single set bit. The name is easier
to reason about.
  • Loading branch information
mciantyre committed Sep 18, 2021
1 parent cd6e3f5 commit b79b4de
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions imxrt-hal/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ where
REGISTER_BLOCKS[self.module().wrapping_sub(1)]
}

/// Returns the bitmask for this GPIO
#[inline(always)]
fn offset(&self) -> u32 {
fn mask(&self) -> u32 {
1u32 << <P as Pin>::Offset::USIZE
}

Expand Down Expand Up @@ -96,7 +97,7 @@ where
// Safety: MMIO valid per gpr() function.
// Read is atomic
self.gpr()
.map(|gpr| unsafe { core::ptr::read_volatile(gpr) & self.offset() != 0 })
.map(|gpr| unsafe { core::ptr::read_volatile(gpr) & self.mask() != 0 })
.unwrap_or(false)
}

Expand All @@ -117,9 +118,9 @@ where
let was_output = self.is_output();

if fast {
core::ptr::write_volatile(gpr, v | self.offset());
core::ptr::write_volatile(gpr, v | self.mask());
} else {
core::ptr::write_volatile(gpr, v & !self.offset());
core::ptr::write_volatile(gpr, v & !self.mask());
}

// At this point, calls to set_output / set_input will refer to the 'fast'
Expand All @@ -139,15 +140,15 @@ where
/// Returns `true` if the pin is configured as an output pin
fn is_output(&self) -> bool {
// Safety: atomic read
unsafe { ral::read_reg!(ral::gpio, self.register_block(), GDIR) & self.offset() != 0 }
unsafe { ral::read_reg!(ral::gpio, self.register_block(), GDIR) & self.mask() != 0 }
}

/// Configure the GPIO as an output
fn set_output(&self, _: &cortex_m::interrupt::CriticalSection) {
// Safety: critical section, enforced by API, ensures consistency
unsafe {
ral::modify_reg!(ral::gpio, self.register_block(), GDIR, |gdir| gdir
| self.offset());
| self.mask());
}
}

Expand All @@ -156,7 +157,7 @@ where
// Safety: critical section, enforced by API, ensures consistency
unsafe {
ral::modify_reg!(ral::gpio, self.register_block(), GDIR, |gdir| gdir
& !self.offset());
& !self.mask());
}
}
}
Expand Down Expand Up @@ -188,7 +189,7 @@ where
/// Returns `true` if this input pin is high
pub fn is_set(&self) -> bool {
// Safety: read is atomic
unsafe { ral::read_reg!(ral::gpio, self.register_block(), PSR) & self.offset() != 0 }
unsafe { ral::read_reg!(ral::gpio, self.register_block(), PSR) & self.mask() != 0 }
}
}

Expand All @@ -208,25 +209,25 @@ where
/// Set the GPIO high
pub fn set(&mut self) {
// Safety: atomic write
unsafe { ral::write_reg!(ral::gpio, self.register_block(), DR_SET, self.offset()) };
unsafe { ral::write_reg!(ral::gpio, self.register_block(), DR_SET, self.mask()) };
}

/// Set the GPIO low
pub fn clear(&mut self) {
// Safety: atomic write
unsafe { ral::write_reg!(ral::gpio, self.register_block(), DR_CLEAR, self.offset()) };
unsafe { ral::write_reg!(ral::gpio, self.register_block(), DR_CLEAR, self.mask()) };
}

/// Returns `true` if the pin is high
pub fn is_set(&self) -> bool {
// Safety: atomic read
unsafe { ral::read_reg!(ral::gpio, self.register_block(), DR) & self.offset() != 0u32 }
unsafe { ral::read_reg!(ral::gpio, self.register_block(), DR) & self.mask() != 0u32 }
}

/// Alternate the state of the pin
pub fn toggle(&mut self) {
// Safety: atomic write
unsafe { ral::write_reg!(ral::gpio, self.register_block(), DR_TOGGLE, self.offset()) }
unsafe { ral::write_reg!(ral::gpio, self.register_block(), DR_TOGGLE, self.mask()) }
}
}

Expand Down

0 comments on commit b79b4de

Please sign in to comment.