[−][src]Struct rppal::spi::Spi
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
Transfer<u8>
, Write<u8>
and FullDuplex<u8>
trait
implementations for Spi
can be enabled by specifying the optional hal
feature in the dependency declaration for the rppal
crate.
Methods
impl Spi
[src]
pub fn new(
bus: Bus,
slave_select: SlaveSelect,
clock_speed: u32,
mode: Mode
) -> Result<Spi>
[src]
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.
pub fn bit_order(&self) -> Result<BitOrder>
[src]
Gets the bit order.
pub fn set_bit_order(&self, bit_order: BitOrder) -> Result<()>
[src]
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
.
pub fn bits_per_word(&self) -> Result<u8>
[src]
Gets the number of bits per word.
pub fn set_bits_per_word(&self, bits_per_word: u8) -> Result<()>
[src]
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.
pub fn clock_speed(&self) -> Result<u32>
[src]
Gets the clock frequency in hertz (Hz).
pub fn set_clock_speed(&self, clock_speed: u32) -> Result<()>
[src]
Sets the clock frequency in hertz (Hz).
The SPI driver will automatically round down to the closest valid frequency.
pub fn mode(&self) -> Result<Mode>
[src]
Gets the SPI mode.
pub fn set_mode(&self, mode: Mode) -> Result<()>
[src]
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.
pub fn ss_polarity(&self) -> Result<Polarity>
[src]
Gets the Slave Select polarity.
pub fn set_ss_polarity(&self, polarity: Polarity) -> Result<()>
[src]
Sets Slave Select polarity.
By default, the Slave Select polarity is set to ActiveLow
.
pub fn read(&mut self, buffer: &mut [u8]) -> Result<usize>
[src]
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.
pub fn write(&mut self, buffer: &[u8]) -> Result<usize>
[src]
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.
pub fn transfer(
&self,
read_buffer: &mut [u8],
write_buffer: &[u8]
) -> Result<usize>
[src]
&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.
pub fn transfer_segments(&self, segments: &[Segment]) -> Result<()>
[src]
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
Auto Trait Implementations
Blanket Implementations
impl<T> From for T
[src]
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,