add IoGpio trait

this allows us to stash a reference to the Iox in both std
and no-std reperesentations of the hardware.
This commit is contained in:
bunnie 2024-10-04 13:04:22 +08:00
parent b6532ffd39
commit 437dcb2de5

View file

@ -1,83 +1,6 @@
use utralib::generated::utra::iox;
use utralib::generated::*;
/// Create an immutable-friendly shared CSR object for the I/O ports. The consequence
/// of this is that we don't get the borrow checker to check the global shared state of
/// the I/O pins status, but the truth is, it's global shared state and there's nothing
/// you can do about it. Might as well make the APIs cleaner so we have less work to
/// do maintaining APIs and can pay attention to sharing/allocating the shared state
/// correctly.
#[derive(Debug)]
pub struct SharedCsr<T> {
pub base: *const T,
}
impl<T> SharedCsr<T>
where
T: core::convert::TryFrom<usize> + core::convert::TryInto<usize> + core::default::Default,
{
pub fn new(base: *const T) -> Self { SharedCsr { base: base as *const T } }
pub unsafe fn base(&self) -> *mut T { self.base as *mut T }
pub fn clone(&self) -> Self { SharedCsr { base: self.base.clone() } }
/// Read the contents of this register
pub fn r(&self, reg: Register) -> T {
// prevent re-ordering
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
let usize_base: *mut usize = unsafe { core::mem::transmute(self.base) };
unsafe { usize_base.add(reg.offset()).read_volatile() }.try_into().unwrap_or_default()
}
/// Read a field from this CSR
pub fn rf(&self, field: Field) -> T {
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
let usize_base: *mut usize = unsafe { core::mem::transmute(self.base) };
((unsafe { usize_base.add(field.register().offset()).read_volatile() } >> field.offset())
& field.mask())
.try_into()
.unwrap_or_default()
}
/// Read-modify-write a given field in this CSR
pub fn rmwf(&self, field: Field, value: T) {
let usize_base: *mut usize = unsafe { core::mem::transmute(self.base) };
let value_as_usize: usize = value.try_into().unwrap_or_default() << field.offset();
let previous = unsafe { usize_base.add(field.register().offset()).read_volatile() }
& !(field.mask() << field.offset());
unsafe { usize_base.add(field.register().offset()).write_volatile(previous | value_as_usize) };
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
/// Write a given field without reading it first
pub fn wfo(&self, field: Field, value: T) {
let usize_base: *mut usize = unsafe { core::mem::transmute(self.base) };
let value_as_usize: usize = (value.try_into().unwrap_or_default() & field.mask()) << field.offset();
unsafe { usize_base.add(field.register().offset()).write_volatile(value_as_usize) };
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
/// Write the entire contents of a register without reading it first
pub fn wo(&self, reg: Register, value: T) {
let usize_base: *mut usize = unsafe { core::mem::transmute(self.base) };
let value_as_usize: usize = value.try_into().unwrap_or_default();
unsafe { usize_base.add(reg.offset()).write_volatile(value_as_usize) };
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
/// Zero a field from a provided value
pub fn zf(&self, field: Field, value: T) -> T {
let value_as_usize: usize = value.try_into().unwrap_or_default();
(value_as_usize & !(field.mask() << field.offset())).try_into().unwrap_or_default()
}
/// Shift & mask a value to its final field position
pub fn ms(&self, field: Field, value: T) -> T {
let value_as_usize: usize = value.try_into().unwrap_or_default();
((value_as_usize & field.mask()) << field.offset()).try_into().unwrap_or_default()
}
}
use crate::SharedCsr;
macro_rules! set_pin_in_bank {
($self:expr, $register:expr, $port:expr, $pin:expr, $val:expr) => {{
@ -145,7 +68,7 @@ pub enum IoxEnable {
}
#[cfg_attr(feature = "derive-rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(u32)]
pub enum IoxValue {
Low = 0,
@ -171,6 +94,13 @@ pub trait IoSetup {
);
}
/// Traits for accessing GPIOs after the port has been set up.
pub trait IoGpio {
fn set_gpio_pin_value(&self, port: IoxPort, pin: u8, value: IoxValue);
fn get_gpio_pin_value(&self, port: IoxPort, pin: u8) -> IoxValue;
fn set_gpio_pin_dir(&self, port: IoxPort, pin: u8, dir: IoxDir);
}
pub struct Iox {
pub csr: SharedCsr<u32>,
}
@ -527,3 +457,13 @@ impl IoSetup for Iox {
}
}
}
impl IoGpio for Iox {
fn get_gpio_pin_value(&self, port: IoxPort, pin: u8) -> IoxValue { self.get_gpio_pin(port, pin) }
fn set_gpio_pin_value(&self, port: IoxPort, pin: u8, value: IoxValue) {
self.set_gpio_pin(port, pin, value);
}
fn set_gpio_pin_dir(&self, port: IoxPort, pin: u8, dir: IoxDir) { self.set_gpio_dir(port, pin, dir); }
}