pub struct Spi<'d, Dm: DriverMode> { /* private fields */ }
Expand description
SPI peripheral driver
§SPI Initialization
let mut spi = Spi::new(
peripherals.SPI2,
Config::default().with_frequency(Rate::from_khz(100)).
with_mode(Mode::_0) )?
.with_sck(peripherals.GPIO0)
.with_mosi(peripherals.GPIO1)
.with_miso(peripherals.GPIO2);
Implementations§
Source§impl<Dm> Spi<'_, Dm>where
Dm: DriverMode,
impl<Dm> Spi<'_, Dm>where
Dm: DriverMode,
Sourcepub fn write(&mut self, words: &[u8]) -> Result<(), Error>
pub fn write(&mut self, words: &[u8]) -> Result<(), Error>
Write bytes to SPI. After writing, flush is called to ensure all data has been transmitted.
Source§impl<'d> Spi<'d, Blocking>
impl<'d> Spi<'d, Blocking>
Sourcepub fn new(
spi: impl Peripheral<P = impl PeripheralInstance> + 'd,
config: Config,
) -> Result<Self, ConfigError>
pub fn new( spi: impl Peripheral<P = impl PeripheralInstance> + 'd, config: Config, ) -> Result<Self, ConfigError>
Sourcepub fn into_async(self) -> Spi<'d, Async>
pub fn into_async(self) -> Spi<'d, Async>
Converts the SPI instance into async mode.
Sourcepub fn with_dma<CH>(
self,
channel: impl Peripheral<P = CH> + 'd,
) -> SpiDma<'d, Blocking>where
CH: DmaChannelFor<AnySpi>,
Available on crate feature unstable
only.
pub fn with_dma<CH>(
self,
channel: impl Peripheral<P = CH> + 'd,
) -> SpiDma<'d, Blocking>where
CH: DmaChannelFor<AnySpi>,
unstable
only.Configures the SPI instance to use DMA with the specified channel.
This method prepares the SPI instance for DMA transfers using SPI
and returns an instance of SpiDma
that supports DMA
operations.
let dma_channel = peripherals.DMA_CH0;
let (rx_buffer, rx_descriptors, tx_buffer, tx_descriptors) =
dma_buffers!(32000);
let dma_rx_buf = DmaRxBuf::new(
rx_descriptors,
rx_buffer
)?;
let dma_tx_buf = DmaTxBuf::new(
tx_descriptors,
tx_buffer
)?;
let mut spi = Spi::new(
peripherals.SPI2,
Config::default().with_frequency(Rate::from_khz(100)).
with_mode(Mode::_0) )?
.with_dma(dma_channel)
.with_buffers(dma_rx_buf, dma_tx_buf);
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Sourcepub fn set_interrupt_handler(&mut self, handler: InterruptHandler)
Available on crate feature unstable
only.
pub fn set_interrupt_handler(&mut self, handler: InterruptHandler)
unstable
only.Registers an interrupt handler for the peripheral.
Note that this will replace any previously registered interrupt handlers.
You can restore the default/unhandled interrupt handler by using crate::interrupt::DEFAULT_INTERRUPT_HANDLER
§Panics
Panics if passed interrupt handler is invalid (e.g. has priority
None
)
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Source§impl<'d> Spi<'d, Async>
impl<'d> Spi<'d, Async>
Sourcepub fn into_blocking(self) -> Spi<'d, Blocking>
pub fn into_blocking(self) -> Spi<'d, Blocking>
Converts the SPI instance into blocking mode.
Sourcepub async fn flush_async(&mut self) -> Result<(), Error>
pub async fn flush_async(&mut self) -> Result<(), Error>
Waits for the completion of previous operations.
Sourcepub async fn transfer_in_place_async(
&mut self,
words: &mut [u8],
) -> Result<(), Error>
pub async fn transfer_in_place_async( &mut self, words: &mut [u8], ) -> Result<(), Error>
Sends words
to the slave. Returns the words
received from the slave
Source§impl<'d, Dm> Spi<'d, Dm>where
Dm: DriverMode,
impl<'d, Dm> Spi<'d, Dm>where
Dm: DriverMode,
Sourcepub fn with_mosi<MOSI: PeripheralOutput>(
self,
mosi: impl Peripheral<P = MOSI> + 'd,
) -> Self
pub fn with_mosi<MOSI: PeripheralOutput>( self, mosi: impl Peripheral<P = MOSI> + 'd, ) -> Self
Assign the MOSI (Master Out Slave In) pin for the SPI instance.
Enables output functionality for the pin, and connects it as the MOSI signal. You want to use this for full-duplex SPI or if you intend to use DataMode::SingleTwoDataLines.
Disconnects the previous pin that was assigned with with_mosi
or
with_sio0
.
Sourcepub fn with_sio0<MOSI: PeripheralOutput>(
self,
mosi: impl Peripheral<P = MOSI> + 'd,
) -> Self
Available on crate feature unstable
only.
pub fn with_sio0<MOSI: PeripheralOutput>( self, mosi: impl Peripheral<P = MOSI> + 'd, ) -> Self
unstable
only.Assign the SIO0 pin for the SPI instance.
Enables both input and output functionality for the pin, and connects it to the MOSI signal and SIO0 input signal.
Disconnects the previous pin that was assigned with with_sio0
or
with_mosi
.
Use this if any of the devices on the bus use half-duplex SPI.
The pin is configured to open-drain mode.
Note: You do not need to call Self::with_mosi when this is used.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Sourcepub fn with_miso<MISO: PeripheralInput>(
self,
miso: impl Peripheral<P = MISO> + 'd,
) -> Self
pub fn with_miso<MISO: PeripheralInput>( self, miso: impl Peripheral<P = MISO> + 'd, ) -> Self
Assign the MISO (Master In Slave Out) pin for the SPI instance.
Enables input functionality for the pin, and connects it to the MISO signal.
You want to use this for full-duplex SPI or DataMode::SingleTwoDataLines
Sourcepub fn with_sio1<SIO1: PeripheralOutput>(
self,
miso: impl Peripheral<P = SIO1> + 'd,
) -> Self
Available on crate feature unstable
only.
pub fn with_sio1<SIO1: PeripheralOutput>( self, miso: impl Peripheral<P = SIO1> + 'd, ) -> Self
unstable
only.Assign the SIO1/MISO pin for the SPI instance.
Enables both input and output functionality for the pin, and connects it to the MISO signal and SIO1 input signal.
Disconnects the previous pin that was assigned with with_sio1
.
Use this if any of the devices on the bus use half-duplex SPI.
The pin is configured to open-drain mode.
Note: You do not need to call Self::with_miso when this is used.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Sourcepub fn with_sck<SCK: PeripheralOutput>(
self,
sclk: impl Peripheral<P = SCK> + 'd,
) -> Self
pub fn with_sck<SCK: PeripheralOutput>( self, sclk: impl Peripheral<P = SCK> + 'd, ) -> Self
Assign the SCK (Serial Clock) pin for the SPI instance.
Configures the specified pin to push-pull output and connects it to the SPI clock signal.
Disconnects the previous pin that was assigned with with_sck
.
Sourcepub fn with_cs<CS: PeripheralOutput>(
self,
cs: impl Peripheral<P = CS> + 'd,
) -> Self
Available on crate feature unstable
only.
pub fn with_cs<CS: PeripheralOutput>( self, cs: impl Peripheral<P = CS> + 'd, ) -> Self
unstable
only.Assign the CS (Chip Select) pin for the SPI instance.
Configures the specified pin to push-pull output and connects it to the SPI CS signal.
Disconnects the previous pin that was assigned with with_cs
.
§Current Stability Limitations
The hardware chip select functionality is limited; only one CS line can be set, regardless of the total number available. There is no mechanism to select which CS line to use.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Sourcepub fn apply_config(&mut self, config: &Config) -> Result<(), ConfigError>
pub fn apply_config(&mut self, config: &Config) -> Result<(), ConfigError>
Change the bus configuration.
§Errors
If frequency passed in config exceeds
80MHz
or is below 70kHz,
ConfigError::UnsupportedFrequency
error will be returned.
Source§impl<'d, Dm> Spi<'d, Dm>where
Dm: DriverMode,
impl<'d, Dm> Spi<'d, Dm>where
Dm: DriverMode,
Sourcepub fn with_sio2<SIO2: PeripheralOutput>(
self,
sio2: impl Peripheral<P = SIO2> + 'd,
) -> Self
Available on crate feature unstable
only.
pub fn with_sio2<SIO2: PeripheralOutput>( self, sio2: impl Peripheral<P = SIO2> + 'd, ) -> Self
unstable
only.Assign the SIO2 pin for the SPI instance.
Enables both input and output functionality for the pin, and connects it to the SIO2 output and input signals.
§Current Stability Limitations
QSPI operations are unstable, associated pins configuration is inefficient.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Sourcepub fn with_sio3<SIO3: PeripheralOutput>(
self,
sio3: impl Peripheral<P = SIO3> + 'd,
) -> Self
Available on crate feature unstable
only.
pub fn with_sio3<SIO3: PeripheralOutput>( self, sio3: impl Peripheral<P = SIO3> + 'd, ) -> Self
unstable
only.Assign the SIO3 pin for the SPI instance.
Enables both input and output functionality for the pin, and connects it to the SIO3 output and input signals.
§Current Stability Limitations
QSPI operations are unstable, associated pins configuration is inefficient.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Source§impl<Dm> Spi<'_, Dm>where
Dm: DriverMode,
impl<Dm> Spi<'_, Dm>where
Dm: DriverMode,
Sourcepub fn half_duplex_read(
&mut self,
data_mode: DataMode,
cmd: Command,
address: Address,
dummy: u8,
buffer: &mut [u8],
) -> Result<(), Error>
Available on crate feature unstable
only.
pub fn half_duplex_read( &mut self, data_mode: DataMode, cmd: Command, address: Address, dummy: u8, buffer: &mut [u8], ) -> Result<(), Error>
unstable
only.Half-duplex read.
§Errors
Error::FifoSizeExeeded
or Error::Unsupported
will be returned if
passed buffer is bigger than FIFO size or if buffer is empty (currently
unsupported). DataMode::Single
cannot be combined with any other
DataMode
, otherwise Error::Unsupported
will be returned.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Sourcepub fn half_duplex_write(
&mut self,
data_mode: DataMode,
cmd: Command,
address: Address,
dummy: u8,
buffer: &[u8],
) -> Result<(), Error>
Available on crate feature unstable
only.
pub fn half_duplex_write( &mut self, data_mode: DataMode, cmd: Command, address: Address, dummy: u8, buffer: &[u8], ) -> Result<(), Error>
unstable
only.Half-duplex write.
§Errors
Error::FifoSizeExeeded
will be returned if
passed buffer is bigger than FIFO size.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Trait Implementations§
Source§impl<'d, Dm: Debug + DriverMode> Debug for Spi<'d, Dm>
impl<'d, Dm: Debug + DriverMode> Debug for Spi<'d, Dm>
Source§impl<'d, Dm: DriverMode> Format for Spi<'d, Dm>
impl<'d, Dm: DriverMode> Format for Spi<'d, Dm>
Source§impl InterruptConfigurable for Spi<'_, Blocking>
Available on crate feature unstable
only.§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
impl InterruptConfigurable for Spi<'_, Blocking>
unstable
only.§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Source§fn set_interrupt_handler(&mut self, handler: InterruptHandler)
fn set_interrupt_handler(&mut self, handler: InterruptHandler)
Sets the interrupt handler
Interrupts are not enabled at the peripheral level here.
Source§impl<Dm> SetConfig for Spi<'_, Dm>where
Dm: DriverMode,
Available on crate feature unstable
only.§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
impl<Dm> SetConfig for Spi<'_, Dm>where
Dm: DriverMode,
unstable
only.§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Source§type ConfigError = ConfigError
type ConfigError = ConfigError
set_config
fails.Source§fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError>
fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError>
Source§impl SpiBus for Spi<'_, Async>
impl SpiBus for Spi<'_, Async>
Source§async fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error>
async fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error>
words
from the slave. Read moreSource§async fn write(&mut self, words: &[u8]) -> Result<(), Self::Error>
async fn write(&mut self, words: &[u8]) -> Result<(), Self::Error>
words
to the slave, ignoring all the incoming words. Read moreSource§async fn transfer(
&mut self,
read: &mut [u8],
write: &[u8],
) -> Result<(), Self::Error>
async 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§async fn transfer_in_place(
&mut self,
words: &mut [u8],
) -> Result<(), Self::Error>
async 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§impl<Dm> SpiBus for Spi<'_, Dm>where
Dm: DriverMode,
impl<Dm> SpiBus for Spi<'_, Dm>where
Dm: DriverMode,
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 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 more