Struct rppal::spi::Spi

source ·
pub struct Spi { /* private fields */ }
Expand description

Provides access to the Raspberry Pi’s SPI peripherals.

Before using Spi, make sure your Raspberry Pi has the necessary SPI buses and Slave Select pins enabled. More information can be found here.

The embedded-hal blocking::spi::Transfer<u8>, blocking::spi::Write<u8> and spi::FullDuplex<u8> trait implementations for Spi can be enabled by specifying the optional hal feature in the dependency declaration for the rppal crate.

Implementations§

source§

impl Spi

source

pub fn new( bus: Bus, slave_select: SlaveSelect, clock_speed: u32, mode: Mode ) -> Result<Spi>

Constructs a new Spi.

bus and slave_select specify the selected SPI bus and one of its associated Slave Select pins.

clock_speed defines the maximum clock frequency in hertz (Hz). The SPI driver will automatically round down to the closest valid frequency.

mode selects the clock polarity and phase.

source

pub fn bit_order(&self) -> Result<BitOrder>

Gets the bit order.

source

pub fn set_bit_order(&self, bit_order: BitOrder) -> Result<()>

Sets the order in which bits are shifted out and in.

The Raspberry Pi currently only supports the MsbFirst bit order. If you need the LsbFirst bit order, you can use the reverse_bits function instead to reverse the bit order in software by converting your write buffer before sending it to the slave device, and your read buffer after reading any incoming data.

By default, bit_order is set to MsbFirst.

source

pub fn bits_per_word(&self) -> Result<u8>

Gets the number of bits per word.

source

pub fn set_bits_per_word(&self, bits_per_word: u8) -> Result<()>

Sets the number of bits per word.

The Raspberry Pi currently only supports 8 bit words.

By default, bits_per_word is set to 8.

source

pub fn clock_speed(&self) -> Result<u32>

Gets the clock frequency in hertz (Hz).

source

pub fn set_clock_speed(&self, clock_speed: u32) -> Result<()>

Sets the clock frequency in hertz (Hz).

The SPI driver will automatically round down to the closest valid frequency.

source

pub fn mode(&self) -> Result<Mode>

Gets the SPI mode.

source

pub fn set_mode(&self, mode: Mode) -> Result<()>

Sets the SPI mode.

The SPI mode indicates the serial clock polarity and phase. Some modes may not be available depending on the SPI bus that’s used.

source

pub fn ss_polarity(&self) -> Result<Polarity>

Gets the Slave Select polarity.

source

pub fn set_ss_polarity(&self, polarity: Polarity) -> Result<()>

Sets Slave Select polarity.

By default, the Slave Select polarity is set to ActiveLow.

source

pub fn read(&mut self, buffer: &mut [u8]) -> Result<usize>

Receives incoming data from the slave device and writes it to buffer.

The SPI protocol doesn’t indicate how much incoming data is waiting, so the total number of bytes read depends on the length of buffer.

During the read, the MOSI line is kept in a state that results in a zero value byte shifted out for every byte read receives on the MISO line.

Slave Select is set to active at the start of the read, and inactive when the read completes.

Returns how many bytes were read.

source

pub fn write(&mut self, buffer: &[u8]) -> Result<usize>

Sends the outgoing data contained in buffer to the slave device.

Any data received on the MISO line from the slave is ignored.

Slave Select is set to active at the start of the write, and inactive when the write completes.

Returns how many bytes were written.

source

pub fn transfer( &self, read_buffer: &mut [u8], write_buffer: &[u8] ) -> Result<usize>

Sends and receives data at the same time.

SPI is a full-duplex protocol that shifts out bits to the slave device on the MOSI line while simultaneously shifting in bits it receives on the MISO line. transfer stores the incoming data in read_buffer, and sends the outgoing data contained in write_buffer.

Because data is sent and received simultaneously, transfer will only transfer as many bytes as the shortest of the two buffers contains.

Slave Select is set to active at the start of the transfer, and inactive when the transfer completes.

Returns how many bytes were transferred.

source

pub fn transfer_segments(&self, segments: &[Segment<'_, '_>]) -> Result<()>

Transfers multiple half-duplex or full-duplex segments.

transfer_segments transfers multiple segments in a single call. Each Segment contains a reference to either a read buffer or a write buffer, or both. Optional settings can be configured that override the SPI bus settings for that specific segment.

By default, Slave Select stays active until all segments have been transferred. You can change this behavior using Segment::set_ss_change.

Trait Implementations§

source§

impl Debug for Spi

source§

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

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

impl Send for Spi

Auto Trait Implementations§

§

impl RefUnwindSafe for Spi

§

impl !Sync for Spi

§

impl Unpin for Spi

§

impl UnwindSafe for Spi

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.