Struct Value

Source
pub struct Value<R: RegisterValue> { /* private fields */ }
Expand description

A value read from or to be written to a register

§Created by:

  • Reg::read: read the value from the register.
  • Default or the Value::reset: the reset value of the register.

§Usable with:

Values can be modified with the | and ^ operators, and well as with |= and ^=.

§Example

use peripherals::{register, Value};

register! {
    Register: u8 = 0b1001 {
        BIT1: 0 = struct Bit1(bool);
        BIT2: 1 = struct Bit2(bool);
        TWO_BITS: 2..3 = struct TwoBits(u8);
    }
}

// Obtain it with the `reset` method or by reading the register
let mut value = Value::<Register>::reset();
assert_eq!(value.value(), 0b1001);

/// Extract single field
assert_eq!(value.field(Register::BIT1), Bit1(true));
assert_eq!(value.field(Register::BIT2), Bit2(false));
assert_eq!(value.field(Register::TWO_BITS), TwoBits(0b10));

// Extract multiple fields
let fields_12 = value & (Register::BIT1 | Register::BIT2);
assert_eq!(fields_12.bits(), 0b001);
assert_eq!((value & Register::TWO_BITS).bits(), 0b1000);

// Toggle single-bit fields
value ^= Register::BIT1;
assert_eq!(value.value(), 0b1000);
value ^= Register::BIT1 | Register::BIT2;
assert_eq!(value.value(), 0b1011);

// Write back fields previously read
value |= fields_12;
assert_eq!(value.value(), 0b1001);

// Modify fields
value |= Bit1(false);
assert_eq!(value.value(), 0b1000);
value |= TwoBits(0b01);
assert_eq!(value.value(), 0b0100);

Implementations§

Source§

impl<R: RegisterValue> Value<R>

Source

pub fn value(self) -> R::Int

Get the raw value

Source

pub unsafe fn from_raw(value: R::Int) -> Value<R>

Build from a raw value

§Safety

You must ensure the value is valid for the associated register.

Source

pub fn field<T>(self, field: Field<R, T, R::Int>) -> T
where R::Int: TryInto<T>, <R::Int as TryInto<T>>::Error: Debug,

Read the given field

This returns the value of a field defined with the [periph!] or [register!] macro.

Source

pub fn test<B: Into<FieldValues<R>>>(self, bits: B) -> bool

Test the given fields

This returns true if the field has the value given in parameter. It can also be used with more values, combined with |, in which case all fields must match.

Source

pub fn reset() -> Value<R>

Get the default / reset value

This returns to the value that the register has right right after a reset or a boot.

Trait Implementations§

Source§

impl<R: RegisterValue, T: Into<Fields<R>> + MayToggle> BitAnd<T> for Value<R>

Source§

type Output = FieldValues<R, <T as MayToggle>::Toggle>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: T) -> Self::Output

Performs the & operation. Read more
Source§

impl<R: RegisterValue, T: Into<FieldValues<R>>> BitOr<T> for Value<R>

Source§

type Output = Value<R>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: T) -> Self::Output

Performs the | operation. Read more
Source§

impl<R: RegisterValue, T: Into<FieldValues<R>>> BitOrAssign<T> for Value<R>

Source§

fn bitor_assign(&mut self, other: T)

Performs the |= operation. Read more
Source§

impl<R: RegisterValue, T: Into<Fields<R, Toggle>>> BitXor<T> for Value<R>

Source§

type Output = Value<R>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: T) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<R: RegisterValue, T: Into<Fields<R, Toggle>>> BitXorAssign<T> for Value<R>

Source§

fn bitxor_assign(&mut self, other: T)

Performs the ^= operation. Read more
Source§

impl<R: RegisterValue> Clone for Value<R>

Source§

fn clone(&self) -> Value<R>

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<R: RegisterValue> Debug for Value<R>

Source§

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

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

impl<R: RegisterValue> Default for Value<R>

Source§

fn default() -> Value<R>

Returns the “default value” for a type. Read more
Source§

impl<R: RegisterValue, T: Into<Value<R>> + Copy> PartialEq<T> for Value<R>

Source§

fn eq(&self, other: &T) -> 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: RegisterValue> Copy for Value<R>

Source§

impl<R: RegisterValue> Eq for Value<R>

Auto Trait Implementations§

§

impl<R> Freeze for Value<R>
where <R as RegisterValue>::Int: Freeze,

§

impl<R> RefUnwindSafe for Value<R>

§

impl<R> Send for Value<R>
where <R as RegisterValue>::Int: Send, R: Send,

§

impl<R> Sync for Value<R>
where <R as RegisterValue>::Int: Sync, R: Sync,

§

impl<R> Unpin for Value<R>
where <R as RegisterValue>::Int: Unpin, R: Unpin,

§

impl<R> UnwindSafe for Value<R>

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.