mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
refactor iox to have a setup_pin() trait
this lays the foundation to make code portable between std and no-std environments for setting up pin configurations.
This commit is contained in:
parent
6f0ab9ae3d
commit
c7bdc1aec5
1 changed files with 139 additions and 13 deletions
|
|
@ -1,6 +1,84 @@
|
|||
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()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! set_pin_in_bank {
|
||||
($self:expr, $register:expr, $port:expr, $pin:expr, $val:expr) => {{
|
||||
assert!($pin < 16, "pin must be in range of 0-15");
|
||||
|
|
@ -78,26 +156,41 @@ impl From<u32> for IoxValue {
|
|||
fn from(value: u32) -> Self { if value == 0 { IoxValue::Low } else { IoxValue::High } }
|
||||
}
|
||||
|
||||
/// Use a trait that will allow us to share code between both `std` and `no-std` implementations
|
||||
pub trait IoSetup {
|
||||
fn setup_pin(
|
||||
&self,
|
||||
port: IoxPort,
|
||||
pin: u8,
|
||||
direction: Option<IoxDir>,
|
||||
function: Option<IoxFunction>,
|
||||
schmitt_trigger: Option<IoxEnable>,
|
||||
pullup: Option<IoxEnable>,
|
||||
slow_slew: Option<IoxEnable>,
|
||||
strength: Option<IoxDriveStrength>,
|
||||
);
|
||||
}
|
||||
|
||||
pub struct Iox {
|
||||
pub csr: CSR<u32>,
|
||||
pub csr: SharedCsr<u32>,
|
||||
}
|
||||
|
||||
impl Iox {
|
||||
pub fn new(base_address: *mut u32) -> Self { Iox { csr: CSR::new(base_address) } }
|
||||
pub fn new(base_address: *mut u32) -> Self { Iox { csr: SharedCsr::new(base_address) } }
|
||||
|
||||
pub fn set_gpio_dir(&mut self, port: IoxPort, pin: u8, direction: IoxDir) {
|
||||
pub fn set_gpio_dir(&self, port: IoxPort, pin: u8, direction: IoxDir) {
|
||||
set_pin_in_bank!(self, iox::SFR_GPIOOE_CRGOE0, port, pin, direction)
|
||||
}
|
||||
|
||||
pub fn set_gpio_pullup(&mut self, port: IoxPort, pin: u8, enable: IoxEnable) {
|
||||
pub fn set_gpio_pullup(&self, port: IoxPort, pin: u8, enable: IoxEnable) {
|
||||
set_pin_in_bank!(self, iox::SFR_GPIOPU_CRGPU0, port, pin, enable)
|
||||
}
|
||||
|
||||
pub fn set_gpio_pin(&mut self, port: IoxPort, pin: u8, value: IoxValue) {
|
||||
pub fn set_gpio_pin(&self, port: IoxPort, pin: u8, value: IoxValue) {
|
||||
set_pin_in_bank!(self, iox::SFR_GPIOOUT_CRGO0, port, pin, value)
|
||||
}
|
||||
|
||||
pub fn set_gpio_bank(&mut self, port: IoxPort, value: u16, mask: u16) {
|
||||
pub fn set_gpio_bank(&self, port: IoxPort, value: u16, mask: u16) {
|
||||
// safety: it is safe to manipulate a raw pointer because IoxPort constrains
|
||||
// the offset to be within range.
|
||||
unsafe {
|
||||
|
|
@ -109,11 +202,11 @@ impl Iox {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn set_gpio_schmitt_trigger(&mut self, port: IoxPort, pin: u8, enable: IoxEnable) {
|
||||
pub fn set_gpio_schmitt_trigger(&self, port: IoxPort, pin: u8, enable: IoxEnable) {
|
||||
set_pin_in_bank!(self, iox::SFR_CFG_SCHM_CR_CFG_SCHMSEL0, port, pin, enable)
|
||||
}
|
||||
|
||||
pub fn set_slow_slew_rate(&mut self, port: IoxPort, pin: u8, enable: IoxEnable) {
|
||||
pub fn set_slow_slew_rate(&self, port: IoxPort, pin: u8, enable: IoxEnable) {
|
||||
set_pin_in_bank!(self, iox::SFR_CFG_SLEW_CR_CFG_SLEWSLOW0, port, pin, enable)
|
||||
}
|
||||
|
||||
|
|
@ -141,7 +234,7 @@ impl Iox {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn set_alternate_function(&mut self, port: IoxPort, pin: u8, function: IoxFunction) {
|
||||
pub fn set_alternate_function(&self, port: IoxPort, pin: u8, function: IoxFunction) {
|
||||
assert!(pin < 16, "pin must be in range of 0-15");
|
||||
match port {
|
||||
IoxPort::PA => {
|
||||
|
|
@ -248,7 +341,7 @@ impl Iox {
|
|||
/// by the mapping request. The index of the array corresponds to the bit position in
|
||||
/// the bitmask. You may use this to pass as arguments to further functions
|
||||
/// that do things like control slew rate or apply pull-ups.
|
||||
pub fn set_ports_from_pio_bitmask(&mut self, enable_bitmask: u32) -> [Option<(IoxPort, u8)>; 32] {
|
||||
pub fn set_ports_from_pio_bitmask(&self, enable_bitmask: u32) -> [Option<(IoxPort, u8)>; 32] {
|
||||
let mut mapping: [Option<(IoxPort, u8)>; 32] = [None; 32];
|
||||
|
||||
for i in 0..32 {
|
||||
|
|
@ -310,7 +403,7 @@ impl Iox {
|
|||
|
||||
/// Returns the PIO bit that was enabled based on the port and pin specifier given;
|
||||
/// returns `None` if the proposed mapping is invalid.
|
||||
pub fn set_pio_bit_from_port_and_pin(&mut self, port: IoxPort, pin: u8) -> Option<u8> {
|
||||
pub fn set_pio_bit_from_port_and_pin(&self, port: IoxPort, pin: u8) -> Option<u8> {
|
||||
match port {
|
||||
IoxPort::PA => None,
|
||||
IoxPort::PB => {
|
||||
|
|
@ -340,7 +433,7 @@ impl Iox {
|
|||
/// Returns the PIO bit that was disabled based on the port and pin specifier given;
|
||||
/// returns `None` if the proposed mapping is invalid. Does not change the AF mapping,
|
||||
/// simply disables the bit in the PIO mux register.
|
||||
pub fn unset_pio_bit_from_port_and_pin(&mut self, port: IoxPort, pin: u8) -> Option<u8> {
|
||||
pub fn unset_pio_bit_from_port_and_pin(&self, port: IoxPort, pin: u8) -> Option<u8> {
|
||||
match port {
|
||||
IoxPort::PA => None,
|
||||
IoxPort::PB => {
|
||||
|
|
@ -365,7 +458,7 @@ impl Iox {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn set_drive_strength(&mut self, port: IoxPort, pin: u8, strength: IoxDriveStrength) {
|
||||
pub fn set_drive_strength(&self, port: IoxPort, pin: u8, strength: IoxDriveStrength) {
|
||||
assert!(pin < 16, "pin must be in range of 0-15");
|
||||
match port {
|
||||
IoxPort::PA => self.csr.wo(
|
||||
|
|
@ -401,3 +494,36 @@ impl Iox {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IoSetup for Iox {
|
||||
fn setup_pin(
|
||||
&self,
|
||||
port: IoxPort,
|
||||
pin: u8,
|
||||
direction: Option<IoxDir>,
|
||||
function: Option<IoxFunction>,
|
||||
schmitt_trigger: Option<IoxEnable>,
|
||||
pullup: Option<IoxEnable>,
|
||||
slow_slew: Option<IoxEnable>,
|
||||
strength: Option<IoxDriveStrength>,
|
||||
) {
|
||||
if let Some(f) = function {
|
||||
self.set_alternate_function(port, pin, f);
|
||||
}
|
||||
if let Some(d) = direction {
|
||||
self.set_gpio_dir(port, pin, d);
|
||||
}
|
||||
if let Some(t) = schmitt_trigger {
|
||||
self.set_gpio_schmitt_trigger(port, pin, t);
|
||||
}
|
||||
if let Some(p) = pullup {
|
||||
self.set_gpio_pullup(port, pin, p);
|
||||
}
|
||||
if let Some(s) = slow_slew {
|
||||
self.set_slow_slew_rate(port, pin, s);
|
||||
}
|
||||
if let Some(s) = strength {
|
||||
self.set_drive_strength(port, pin, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue