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
impl Spi
sourcepub fn new(
bus: Bus,
slave_select: SlaveSelect,
clock_speed: u32,
mode: Mode
) -> Result<Spi>
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.
sourcepub fn set_bit_order(&self, bit_order: BitOrder) -> Result<()>
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.
sourcepub fn bits_per_word(&self) -> Result<u8>
pub fn bits_per_word(&self) -> Result<u8>
Gets the number of bits per word.
sourcepub fn set_bits_per_word(&self, bits_per_word: u8) -> Result<()>
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.
sourcepub fn clock_speed(&self) -> Result<u32>
pub fn clock_speed(&self) -> Result<u32>
Gets the clock frequency in hertz (Hz).
sourcepub fn set_clock_speed(&self, clock_speed: u32) -> Result<()>
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.
sourcepub fn set_mode(&self, mode: Mode) -> Result<()>
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.
sourcepub fn ss_polarity(&self) -> Result<Polarity>
pub fn ss_polarity(&self) -> Result<Polarity>
Gets the Slave Select polarity.
sourcepub fn set_ss_polarity(&self, polarity: Polarity) -> Result<()>
pub fn set_ss_polarity(&self, polarity: Polarity) -> Result<()>
Sets Slave Select polarity.
By default, the Slave Select polarity is set to ActiveLow.
sourcepub fn read(&mut self, buffer: &mut [u8]) -> Result<usize>
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.
sourcepub fn write(&mut self, buffer: &[u8]) -> Result<usize>
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.
sourcepub fn transfer(
&self,
read_buffer: &mut [u8],
write_buffer: &[u8]
) -> Result<usize>
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.
sourcepub fn transfer_segments(&self, segments: &[Segment<'_, '_>]) -> Result<()>
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 FullDuplex<u8> for Spi
impl FullDuplex<u8> for Spi
FullDuplex<u8> trait implementation for embedded-hal v1.0.0
source§impl FullDuplex<u8> for Spi
impl FullDuplex<u8> for Spi
FullDuplex<u8> trait implementation for embedded-hal v0.2.7.
source§impl SpiBus<u8> for Spi
impl SpiBus<u8> for Spi
SpiBus<u8> trait implementation for embedded-hal v1.0.0.
source§fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error>
fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error>
words from the slave. Read moresource§fn write(&mut self, words: &[u8]) -> Result<(), Self::Error>
fn write(&mut self, words: &[u8]) -> Result<(), Self::Error>
words to the slave, ignoring all the incoming words. Read moresource§fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error>
fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error>
write is written to the slave on MOSI and
words received on MISO are stored in read. Read more