pub struct Spi<SPIX, SCK, MISO, MOSI, WORD = Byte> { /* private fields */ }Expand description
A configured SPI master, owning the peripheral and its three pins.
WORD is the frame width: transfer_byte and
SpiBus<u8> live on Spi<.., Byte>, transfer_word
and SpiBus<u16> on Spi<.., Word>. Defaults to Byte.
Chip select is not handled here — NSS is software-managed, so drive the slave’s CS with an ordinary output pin around each transaction.
Implementations§
Source§impl<SPIX, SCK, MISO, MOSI, WORD> Spi<SPIX, SCK, MISO, MOSI, WORD>where
SPIX: Instance,
impl<SPIX, SCK, MISO, MOSI, WORD> Spi<SPIX, SCK, MISO, MOSI, WORD>where
SPIX: Instance,
Sourcepub fn read_ready(&self) -> bool
pub fn read_ready(&self) -> bool
Returns whether a received word is waiting in DATA (RBNE).
Only guarantees that the next single read will not block.
Sourcepub fn write_ready(&self) -> bool
pub fn write_ready(&self) -> bool
Returns whether DATA can accept the next word (TBE).
True at idle, the bus having nothing queued; it says nothing about a word still on its way back.
Sourcepub fn take_error(&mut self) -> Option<Error>
pub fn take_error(&mut self) -> Option<Error>
Returns the pending error, if any, and clears it.
The acknowledge for Event::Error: those flags are what hold the
interrupt request up, so a handler that skips this re-enters forever.
One error per call — with two pending, the second survives for the next.
Overrun also needs DATA drained, which the read a handler makes to do
its work already does.
Sourcepub fn listen(&mut self, event: Event)
pub fn listen(&mut self, event: Event)
Raises an interrupt on event, which still has to be unmasked in the
NVIC.
Event::Tbe is true whenever nothing is queued, so a handler that
listens for it must call unlisten once it has nothing left to send.
Sourcepub fn is_listening(&self, event: Event) -> bool
pub fn is_listening(&self, event: Event) -> bool
Whether event is being listened for.
Sourcepub fn release(self, rcu: &mut Rcu) -> (SPIX, SCK, MISO, MOSI)
pub fn release(self, rcu: &mut Rcu) -> (SPIX, SCK, MISO, MOSI)
Waits for the last word to leave the wire, then returns the peripheral and the three pins, disabled, with its interrupts off and its clock stopped. A received word not yet read is discarded.
Source§impl<SPIX, SCK, MISO, MOSI> Spi<SPIX, SCK, MISO, MOSI, Byte>where
SPIX: Instance,
impl<SPIX, SCK, MISO, MOSI> Spi<SPIX, SCK, MISO, MOSI, Byte>where
SPIX: Instance,
Sourcepub fn write_byte(&mut self, byte: u8)
pub fn write_byte(&mut self, byte: u8)
Hands one byte to DATA and returns at once, the clock now running.
Half an exchange: the answer arrives eight clocks later, on
Event::Rbne. Check write_ready first — writing
over a full transmit buffer loses the byte.
Sourcepub fn read_byte(&mut self) -> u8
pub fn read_byte(&mut self) -> u8
Takes the byte that arrived on MISO, clearing RBNE.
The other half: meaningful only once read_ready
holds, and it reports no error — take_error drains
those.
Sourcepub fn transfer_byte(&mut self, byte: u8) -> Result<u8, Error>
pub fn transfer_byte(&mut self, byte: u8) -> Result<u8, Error>
Exchanges one byte: sends byte on MOSI and returns what arrived on MISO.
Blocks until the exchange has completed, so nothing is left pending on the bus when it returns.
§Errors
The error take_error finds once the exchange is
over; the received byte is then discarded.
Sourcepub fn transfer_bytes(
&mut self,
read: &mut [u8],
write: &[u8],
) -> Result<(), Error>
pub fn transfer_bytes( &mut self, read: &mut [u8], write: &[u8], ) -> Result<(), Error>
Exchanges two buffers: byte i of write goes out and what comes back
lands at byte i of read.
§Errors
The first failed exchange, as in transfer_byte;
the bytes after it are neither sent nor received.
§Panics
If the lengths differ. To read more than is written, pad write —
fill names the usual levels.
Sourcepub fn transfer_bytes_in_place(&mut self, words: &mut [u8]) -> Result<(), Error>
pub fn transfer_bytes_in_place(&mut self, words: &mut [u8]) -> Result<(), Error>
Exchanges words against itself: each byte is replaced by what came back.
§Errors
The first failed exchange, as in transfer_byte;
the bytes after it are neither sent nor received.
Source§impl<SPIX, SCK, MISO, MOSI> Spi<SPIX, SCK, MISO, MOSI, Word>where
SPIX: Instance,
impl<SPIX, SCK, MISO, MOSI> Spi<SPIX, SCK, MISO, MOSI, Word>where
SPIX: Instance,
Sourcepub fn write_word(&mut self, word: u16)
pub fn write_word(&mut self, word: u16)
Hands one 16-bit word to DATA and returns at once, the clock now running.
Half an exchange: the answer arrives sixteen clocks later, on
Event::Rbne. Check write_ready first — writing
over a full transmit buffer loses the word.
Sourcepub fn read_word(&mut self) -> u16
pub fn read_word(&mut self) -> u16
Takes the word that arrived on MISO, clearing RBNE.
The other half: meaningful only once read_ready
holds, and it reports no error — take_error drains
those.
Sourcepub fn transfer_word(&mut self, word: u16) -> Result<u16, Error>
pub fn transfer_word(&mut self, word: u16) -> Result<u16, Error>
Exchanges one 16-bit word: sends word on MOSI, returns what arrived on MISO.
Blocks until the exchange has completed, so nothing is left pending on the bus when it returns.
§Errors
The error take_error finds once the exchange is
over; the received word is then discarded.
Sourcepub fn transfer_words(
&mut self,
read: &mut [u16],
write: &[u16],
) -> Result<(), Error>
pub fn transfer_words( &mut self, read: &mut [u16], write: &[u16], ) -> Result<(), Error>
Exchanges two buffers: word i of write goes out and what comes back
lands at word i of read.
§Errors
The first failed exchange, as in transfer_word;
the words after it are neither sent nor received.
§Panics
If the lengths differ. To read more than is written, pad write —
fill names the usual levels.
Sourcepub fn transfer_words_in_place(
&mut self,
words: &mut [u16],
) -> Result<(), Error>
pub fn transfer_words_in_place( &mut self, words: &mut [u16], ) -> Result<(), Error>
Exchanges words against itself: each word is replaced by what came back.
§Errors
The first failed exchange, as in transfer_word;
the words after it are neither sent nor received.
Trait Implementations§
impl<SCK, MISO, MOSI> DmaDst<2> for Spi<Spi0, SCK, MISO, MOSI, Byte>
impl<SCK, MISO, MOSI> DmaDst<2> for Spi<Spi0, SCK, MISO, MOSI, Word>
impl<SCK, MISO, MOSI> DmaDst<4> for Spi<Spi1, SCK, MISO, MOSI, Byte>
impl<SCK, MISO, MOSI> DmaDst<4> for Spi<Spi1, SCK, MISO, MOSI, Word>
Source§impl<SCK, MISO, MOSI> DmaPeriph<1> for Spi<Spi0, SCK, MISO, MOSI, Byte>
impl<SCK, MISO, MOSI> DmaPeriph<1> for Spi<Spi0, SCK, MISO, MOSI, Byte>
Source§fn enable_dma(&mut self)
fn enable_dma(&mut self)
Source§fn disable_dma(&mut self)
fn disable_dma(&mut self)
Source§impl<SCK, MISO, MOSI> DmaPeriph<1> for Spi<Spi0, SCK, MISO, MOSI, Word>
impl<SCK, MISO, MOSI> DmaPeriph<1> for Spi<Spi0, SCK, MISO, MOSI, Word>
Source§fn enable_dma(&mut self)
fn enable_dma(&mut self)
Source§fn disable_dma(&mut self)
fn disable_dma(&mut self)
Source§impl<SCK, MISO, MOSI> DmaPeriph<2> for Spi<Spi0, SCK, MISO, MOSI, Byte>
impl<SCK, MISO, MOSI> DmaPeriph<2> for Spi<Spi0, SCK, MISO, MOSI, Byte>
Source§fn enable_dma(&mut self)
fn enable_dma(&mut self)
Source§fn disable_dma(&mut self)
fn disable_dma(&mut self)
Source§impl<SCK, MISO, MOSI> DmaPeriph<2> for Spi<Spi0, SCK, MISO, MOSI, Word>
impl<SCK, MISO, MOSI> DmaPeriph<2> for Spi<Spi0, SCK, MISO, MOSI, Word>
Source§fn enable_dma(&mut self)
fn enable_dma(&mut self)
Source§fn disable_dma(&mut self)
fn disable_dma(&mut self)
Source§impl<SCK, MISO, MOSI> DmaPeriph<3> for Spi<Spi1, SCK, MISO, MOSI, Byte>
impl<SCK, MISO, MOSI> DmaPeriph<3> for Spi<Spi1, SCK, MISO, MOSI, Byte>
Source§fn enable_dma(&mut self)
fn enable_dma(&mut self)
Source§fn disable_dma(&mut self)
fn disable_dma(&mut self)
Source§impl<SCK, MISO, MOSI> DmaPeriph<3> for Spi<Spi1, SCK, MISO, MOSI, Word>
impl<SCK, MISO, MOSI> DmaPeriph<3> for Spi<Spi1, SCK, MISO, MOSI, Word>
Source§fn enable_dma(&mut self)
fn enable_dma(&mut self)
Source§fn disable_dma(&mut self)
fn disable_dma(&mut self)
Source§impl<SCK, MISO, MOSI> DmaPeriph<4> for Spi<Spi1, SCK, MISO, MOSI, Byte>
impl<SCK, MISO, MOSI> DmaPeriph<4> for Spi<Spi1, SCK, MISO, MOSI, Byte>
Source§fn enable_dma(&mut self)
fn enable_dma(&mut self)
Source§fn disable_dma(&mut self)
fn disable_dma(&mut self)
Source§impl<SCK, MISO, MOSI> DmaPeriph<4> for Spi<Spi1, SCK, MISO, MOSI, Word>
impl<SCK, MISO, MOSI> DmaPeriph<4> for Spi<Spi1, SCK, MISO, MOSI, Word>
Source§fn enable_dma(&mut self)
fn enable_dma(&mut self)
Source§fn disable_dma(&mut self)
fn disable_dma(&mut self)
impl<SCK, MISO, MOSI> DmaSrc<1> for Spi<Spi0, SCK, MISO, MOSI, Byte>
impl<SCK, MISO, MOSI> DmaSrc<1> for Spi<Spi0, SCK, MISO, MOSI, Word>
impl<SCK, MISO, MOSI> DmaSrc<3> for Spi<Spi1, SCK, MISO, MOSI, Byte>
impl<SCK, MISO, MOSI> DmaSrc<3> for Spi<Spi1, SCK, MISO, MOSI, Word>
Source§impl<SPIX, SCK, MISO, MOSI, WORD> ErrorType for Spi<SPIX, SCK, MISO, MOSI, WORD>where
SPIX: Instance,
impl<SPIX, SCK, MISO, MOSI, WORD> ErrorType for Spi<SPIX, SCK, MISO, MOSI, WORD>where
SPIX: Instance,
Source§impl<SPIX, SCK, MISO, MOSI> SpiBus for Spi<SPIX, SCK, MISO, MOSI, Byte>where
SPIX: Instance,
impl<SPIX, SCK, MISO, MOSI> SpiBus for Spi<SPIX, SCK, MISO, MOSI, Byte>where
SPIX: Instance,
Source§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 moreSource§fn transfer_in_place(&mut self, words: &mut [u8]) -> Result<(), Self::Error>
fn transfer_in_place(&mut self, words: &mut [u8]) -> Result<(), Self::Error>
words are
written to the slave, and the received words are stored into the same
words buffer, overwriting it. Read moreSource§fn flush(&mut self) -> Result<(), Self::Error>
fn flush(&mut self) -> Result<(), Self::Error>
Source§impl<SPIX, SCK, MISO, MOSI> SpiBus<u16> for Spi<SPIX, SCK, MISO, MOSI, Word>where
SPIX: Instance,
impl<SPIX, SCK, MISO, MOSI> SpiBus<u16> for Spi<SPIX, SCK, MISO, MOSI, Word>where
SPIX: Instance,
Source§fn transfer(
&mut self,
read: &mut [u16],
write: &[u16],
) -> Result<(), Self::Error>
fn transfer( &mut self, read: &mut [u16], write: &[u16], ) -> Result<(), Self::Error>
write is written to the slave on MOSI and
words received on MISO are stored in read. Read moreSource§fn transfer_in_place(&mut self, words: &mut [u16]) -> Result<(), Self::Error>
fn transfer_in_place(&mut self, words: &mut [u16]) -> Result<(), Self::Error>
words are
written to the slave, and the received words are stored into the same
words buffer, overwriting it. Read more