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 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 ErrorType for Spi

§

type Error = Error

Error type.
source§

impl FullDuplex for Spi

source§

fn read(&mut self) -> Result<u8, Self::Error>

Reads the word stored in the shift register Read more
source§

fn write(&mut self, byte: u8) -> Result<(), Self::Error>

Writes a word to the slave
source§

impl SpiBus for Spi

source§

fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error>

Read words from the slave. Read more
source§

fn write(&mut self, words: &[u8]) -> Result<(), Self::Error>

Write words to the slave, ignoring all the incoming words. Read more
source§

fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error>

Write and read simultaneously. write is written to the slave on MOSI and words received on MISO are stored in read. Read more
source§

fn transfer_in_place(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error>

Write and read simultaneously. The contents of words are written to the slave, and the received words are stored into the same words buffer, overwriting it. Read more
source§

fn flush(&mut self) -> Result<(), Self::Error>

Wait until all operations have completed and the bus is idle. Read more
source§

impl Transfer<u8> for Spi

§

type Error = Error

Error type
source§

fn transfer<'a>( &mut self, buffer: &'a mut [u8] ) -> Result<&'a [u8], Self::Error>

Sends words to the slave. Returns the words received from the slave
source§

impl Write<u8> for Spi

§

type Error = Error

Error type
source§

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

Sends words to the slave, ignoring all the incoming words
source§

impl FullDuplex<u8> for Spi

§

type Error = Error

An enumeration of SPI errors
source§

fn read(&mut self) -> Result<u8, Self::Error>

Reads the word stored in the shift register Read more
source§

fn send(&mut self, byte: u8) -> Result<(), Self::Error>

Sends a word to the slave
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.