Module esp32c2_hal::spi
source · Expand description
Serial Peripheral Interface
There are multiple ways to use SPI, depending on your needs. Regardless of
which way you choose, you must first create an SPI instance with
Spi::new
.
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let sclk = io.pins.gpio12;
let miso = io.pins.gpio11;
let mosi = io.pins.gpio13;
let cs = io.pins.gpio10;
let mut spi = hal::spi::Spi::new(
peripherals.SPI2,
sclk,
mosi,
miso,
cs,
100u32.kHz(),
SpiMode::Mode0,
&mut peripheral_clock_control,
&mut clocks,
);
Exclusive access to the SPI bus
If all you want to do is to communicate to a single device, and you initiate transactions yourself, there are a number of ways to achieve this:
- Use the
FullDuplex
trait to read/write single bytes at a time, - Use the
SpiBus
trait (requires the “eh1” feature) and its associated functions to initiate transactions with simultaneous reads and writes, or - Use the
SpiBusWrite
andSpiBusRead
traits (requires the “eh1” feature) and their associated functions to read or write mutiple bytes at a time.
Shared SPI access
If you have multiple devices on the same SPI bus that each have their own CS
line, you may want to have a look at the [SpiBusController
] and
[SpiBusDevice
] implemented here. These give exclusive access to the
underlying SPI bus by means of a Mutex. This ensures that device
transactions do not interfere with each other.