Sensors and I/O
SPI Master
Bus for displays, external flash and fast ADCs.
Sensors and I/O
SPI display / flash
ESP32 family
Official path: examples/peripherals/spi_master
01 Overview
SPI master shifts clock and data to a slave. The ESP32's SPI2 and SPI3 peripherals can act as masters, supporting 4-wire (MOSI/MISO/SCLK/CS) and 3-wire (no MISO) modes.
Typical uses: driving TFT panels (e.g., ST7789), external flash, SD cards, and high-speed ADCs.
Compared to I2C, SPI is faster and more flexible but requires more pins and has no built-in acknowledgment.
If you only need two wires and low speed, consider I2C first; SPI suits high-throughput and high-refresh-rate scenarios.
This example demonstrates how to initialize the SPI bus, add devices, queue transactions, and use DMA for large payloads.
02 Hardware
Peripheral: ESP32 has SPI2 and SPI3, both configurable as master.Pins: Typical connections: MOSI, MISO, SCLK, CS (one per slave). See schematic for exact pins.CS polarity: Most slaves are active-low, but check the datasheet.Clock polarity/phase: Modes 0–3, must match the slave.Signal integrity: Trace length, impedance, and return path affect edges; pay attention at high speeds.Extra control pins: Some panels need DC (data/command) and RST (reset) pins, requiring additional GPIOs.Level matching: If the slave is 3.3V, ESP32 can drive directly; if 5V, level shifting is needed.
03 Software flow
1. Call spi_bus_initialize() to set up the bus, pins, clock source, and DMA channel.
2. Call spi_bus_add_device() to add a slave device, configuring mode, clock speed, CS polarity, etc.
3. Prepare transmit data, optionally allocating DMA buffers.
4. Call spi_device_transmit() to send a transaction and wait for completion.
5. For large payloads, use DMA to avoid CPU spinning.
6. Error handling: check return values; retry or reset on timeout or failure.
7. Low power: disable the SPI peripheral clock after transfers complete.
04 Core points
Wrong mode or CS polarity looks like “waveforms but no data”. Custom work: panel driver, extra flash, layout.Note: Different slaves have different timing requirements; consult the datasheet.Do not copy SPI init code from other chips directly; pins and registers differ. At high speeds, watch signal integrity; reduce clock or add termination if needed.
Want this as your product?
Keep this ESP32 architecture. WhatsApp Leo with the sensors, I/O and product logic — we can design the board and firmware, or just walk the risks with you.