Reg

Struct Reg 

Source
pub struct Reg<'io, R: Register, IO: RegisterIO = PtrIO> { /* private fields */ }
Expand description

Register abstraction used to read, write, and modify register values.

This is generic over both the Register to access and the RegisterIO type used to access the register.

The Register trait has associated types defining the register width, access controls, and endianness which are used to customize the read/write implementation for each register.

RegisterIO defaults to regular volatile pointer I/O and is only needed for advanced use cases like tunneled registers.

Implementations§

Source§

impl<R: Register> Reg<'static, R, PtrIO>

Source

pub const unsafe fn from_ptr(ptr: *mut R::Regwidth) -> Self

§Safety

The caller must guarantee that the provided address points to a hardware register of type R.

Source§

impl<'io, R: Register, IO: RegisterIO> Reg<'io, R, IO>

Source

pub const unsafe fn from_ptr_with(ptr: *mut R::Regwidth, io: &'io IO) -> Self

§Safety

The caller must guarantee that the provided address points to a hardware register of type R.

Source

pub const fn as_ptr(&self) -> *mut R

Source§

impl<R: Register, IO: RegisterIO> Reg<'_, R, IO>
where R::Access: Read,

Source

pub fn try_read(&self) -> Result<R, IO::Error>

Try to read a register value.

If the register is to be modified (i.e., a read-modify-write), use the Reg::modify method instead.

§Example
let reg1_val = registers.regfile().register1().try_read().unwrap();
let field1_val = reg1_val.field1();
let field2_val = reg1_val.field2();
Source§

impl<R: Register, IO: RegisterIO<Error = Infallible>> Reg<'_, R, IO>
where R::Access: Read,

Source

pub fn read(&self) -> R

Read a register value.

If the register is to be modified (i.e., a read-modify-write), use the Reg::modify method instead.

§Example
let reg1_val = registers.regfile().register1().read();
let field1_val = reg1_val.field1();
let field2_val = reg1_val.field2();
Source§

impl<R: Register, IO: RegisterIO> Reg<'_, R, IO>
where R::Access: Write,

Source

pub fn try_write_value(&self, val: R) -> Result<(), IO::Error>

Try to write a register value.

Typically one would use Reg::try_write or Reg::try_modify to update a register’s contents, but this method has a few different use cases such as updating a register with a stored value, or updating one register with the contents of another.

§Example
let reg0 = registers.regfile().reg_array()[0].try_read().unwrap();
registers.regfile().reg_array()[1].try_write_value(reg0).unwrap();
Source§

impl<R: Register, IO: RegisterIO<Error = Infallible>> Reg<'_, R, IO>
where R::Access: Write,

Source

pub fn write_value(&self, val: R)

Write a register value.

Typically one would use Reg::write or Reg::modify to update a register’s contents, but this method has a few different use cases such as updating a register with a stored value, or updating one register with the contents of another.

§Example
let reg0 = registers.regfile().reg_array()[0].read();
registers.regfile().reg_array()[1].write_value(reg0);
Source§

impl<R: Default + Register, IO: RegisterIO> Reg<'_, R, IO>
where R::Access: Write,

Source

pub fn try_write<T>(&self, f: impl FnOnce(&mut R) -> T) -> Result<T, IO::Error>

Try to write a register.

This method takes a closure. The input to the closure is a mutable reference to the default value of the register. It can be updated in the closure. The updated value is then written to the hardware register.

§Example
registers.regfile().register1().try_write(|r| {
    // r contains the default (reset) value of the register
    r.set_field1(0x1);
    r.set_field2(0x0);
}).unwrap();
Source§

impl<R: Default + Register, IO: RegisterIO<Error = Infallible>> Reg<'_, R, IO>
where R::Access: Write,

Source

pub fn write<T>(&self, f: impl FnOnce(&mut R) -> T) -> T

Write a register.

This method takes a closure. The input to the closure is a mutable reference to the default value of the register. It can be updated in the closure. The updated value is then written to the hardware register.

§Example
registers.regfile().register1().write(|r| {
    // r contains the default (reset) value of the register
    r.set_field1(0x1);
    r.set_field2(0x0);
});
Source§

impl<R: Register, IO: RegisterIO> Reg<'_, R, IO>
where R::Access: Read + Write,

Source

pub fn try_modify<T>(&self, f: impl FnOnce(&mut R) -> T) -> Result<T, IO::Error>

Try to modify a register.

This method takes a closure. The input to the closure is a mutable reference to the current value of the register. It can be updated in the closure. The updated value is then written back to the hardware register.

§Example
let orig_r = registers.regfile().register1().try_modify(|r| {
    // r contains the current value of the register
    orig_r = r.clone()
    r.set_field1(r.field1());
    r.set_field2(0x0);
    // whatever value the closure returns is returned by the .try_modify() method
    orig_r
}).unwrap();
Source§

impl<R: Register, IO: RegisterIO<Error = Infallible>> Reg<'_, R, IO>
where R::Access: Read + Write,

Source

pub fn modify<T>(&self, f: impl FnOnce(&mut R) -> T) -> T

Modify a register.

This method takes a closure. The input to the closure is a mutable reference to the current value of the register. It can be updated in the closure. The updated value is then written back to the hardware register.

§Example
let orig_r = registers.regfile().register1().modify(|r| {
    // r contains the current value of the register
    orig_r = r.clone()
    r.set_field1(r.field1());
    r.set_field2(0x0);
    // whatever value the closure returns is returned by the .modify() method
    orig_r
});

Trait Implementations§

Source§

impl<R: Register, IO: RegisterIO> Clone for Reg<'_, R, IO>
where R::Regwidth: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'io, R: Debug + Register, IO: Debug + RegisterIO> Debug for Reg<'io, R, IO>
where R::Regwidth: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'io, R: PartialEq + Register, IO: PartialEq + RegisterIO> PartialEq for Reg<'io, R, IO>
where R::Regwidth: PartialEq,

Source§

fn eq(&self, other: &Reg<'io, R, IO>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<R: Register, IO: RegisterIO> Copy for Reg<'_, R, IO>
where R::Regwidth: Copy,

Source§

impl<'io, R: Eq + Register, IO: Eq + RegisterIO> Eq for Reg<'io, R, IO>
where R::Regwidth: Eq,

Source§

impl<R: Register, IO: RegisterIO + Sync> Send for Reg<'_, R, IO>

Source§

impl<'io, R: Register, IO: RegisterIO> StructuralPartialEq for Reg<'io, R, IO>

Source§

impl<R: Register, IO: RegisterIO + Sync> Sync for Reg<'_, R, IO>

Auto Trait Implementations§

§

impl<'io, R, IO> Freeze for Reg<'io, R, IO>

§

impl<'io, R, IO> RefUnwindSafe for Reg<'io, R, IO>

§

impl<'io, R, IO> Unpin for Reg<'io, R, IO>

§

impl<'io, R, IO> UnwindSafe for Reg<'io, R, IO>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.