Skip to main content

Mode

Enum Mode 

Source
pub enum Mode {
    Mode0,
    Mode1,
    Mode2,
    Mode3,
}
Expand description

An SPI clock mode: the (CPOL, CPHA) pair a controller and peripheral must share.

The mode number is (CPOL << 1) | CPHA, so the four modes are:

ModeCPOLCPHAClock idlesData sampled on
000lowleading edge (rising)
101lowtrailing edge (falling)
210highleading edge (falling)
311hightrailing edge (rising)

§Examples

use pamoja_gpio::spi::Mode;

// An SD card and most LoRa radios use mode 0.
assert_eq!(Mode::Mode0.number(), 0);
assert_eq!(Mode::Mode0.cpol_cpha(), (false, false));
assert_eq!(Mode::from_number(3), Some(Mode::Mode3));
assert_eq!(Mode::from_cpol_cpha(true, false), Mode::Mode2);

Variants§

§

Mode0

CPOL 0, CPHA 0: clock idles low, data sampled on the rising (leading) edge.

§

Mode1

CPOL 0, CPHA 1: clock idles low, data sampled on the falling (trailing) edge.

§

Mode2

CPOL 1, CPHA 0: clock idles high, data sampled on the falling (leading) edge.

§

Mode3

CPOL 1, CPHA 1: clock idles high, data sampled on the rising (trailing) edge.

Implementations§

Source§

impl Mode

Source

pub fn number(self) -> u8

Returns the mode number, 0..=3, as datasheets quote it.

§Returns

The number (CPOL << 1) | CPHA.

Source

pub fn from_number(number: u8) -> Option<Mode>

Returns the mode a number names, if it is in range.

§Arguments
  • number - a mode number.
§Returns

The matching Mode, or None if number is above 3.

Source

pub fn cpol_cpha(self) -> (bool, bool)

Returns the (CPOL, CPHA) pair for this mode.

§Returns

(clock idles high, data sampled on the trailing edge).

Source

pub fn from_cpol_cpha(cpol: bool, cpha: bool) -> Mode

Returns the mode for a (CPOL, CPHA) pair.

§Arguments
  • cpol - clock polarity: true if the clock idles high.
  • cpha - clock phase: true if data is sampled on the trailing edge.
§Returns

The matching Mode. Every pair maps to a mode, so this never fails.

Source

pub fn clock_idles_high(self) -> bool

Returns true if the clock idles high (CPOL = 1), which is modes 2 and 3.

Source

pub fn samples_on_trailing_edge(self) -> bool

Returns true if data is sampled on the trailing clock edge (CPHA = 1), which is modes 1 and 3.

Trait Implementations§

Source§

impl Clone for Mode

Source§

fn clone(&self) -> Mode

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Mode

Source§

impl Debug for Mode

Source§

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

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

impl Eq for Mode

Source§

impl PartialEq for Mode

Source§

fn eq(&self, other: &Mode) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 StructuralPartialEq for Mode

Auto Trait Implementations§

§

impl Freeze for Mode

§

impl RefUnwindSafe for Mode

§

impl Send for Mode

§

impl Sync for Mode

§

impl Unpin for Mode

§

impl UnsafeUnpin for Mode

§

impl UnwindSafe for Mode

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.