25#include <idfxx/future>
27#include <idfxx/intr_alloc>
32#include <driver/spi_master.h>
33#include <esp_idf_version.h>
34#include <frequency/frequency>
54#if SOC_SPI_PERIPH_NUM > 2
65#if CONFIG_IDF_TARGET_ESP32
141#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
256#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
310 bool _initialized =
true;
362#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
445#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
548#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
573#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
695#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
733 template<
typename Rep,
typename Period>
769 template<
typename Rep,
typename Period>
772 return _try_queue_trans(
trans, std::chrono::ceil<std::chrono::milliseconds>(
timeout));
852#if SOC_SPI_PERIPH_NUM > 2
857 return "unknown(" + std::to_string(
static_cast<int>(
h)) +
")";
863#include "sdkconfig.h"
864#ifdef CONFIG_IDFXX_STD_FORMAT
870struct formatter<
idfxx::spi::host_device> {
871 constexpr auto parse(format_parse_context& ctx) {
return ctx.begin(); }
873 template<
typename FormatContext>
876 return std::copy(s.begin(), s.end(), ctx.out());
Type-safe set of flags from a scoped enum.
static constexpr gpio nc()
Returns a GPIO representing "not connected".
level
GPIO output/input level.
master_bus(master_bus &&other) noexcept
Move constructor.
master_bus(const master_bus &)=delete
static result< master_bus > make(enum host_device host, enum dma_chan dma_chan, struct bus_config config)
Creates a new SPI master bus.
master_bus(enum host_device host, enum dma_chan dma_chan, struct bus_config config)
Constructs a new SPI master bus.
enum host_device host() const
Returns the host device ID the bus is using.
size_t max_transaction_length() const
Returns the maximum transaction length for this bus.
master_bus & operator=(master_bus &&other) noexcept
Move assignment.
master_bus & operator=(const master_bus &)=delete
A device on a SPI master bus.
bool try_lock() const noexcept
Tries to acquire exclusive bus access without blocking.
master_bus & bus() const
Returns the parent bus.
void transmit(const transaction &trans)
Executes a full-control transaction.
std::vector< uint8_t > receive(size_t size)
Receives data from the device.
void transfer(std::span< const uint8_t > tx_data, std::span< uint8_t > rx_data)
Performs a full-duplex transfer.
void transmit(std::span< const uint8_t > tx_data)
Transmits data to the device.
result< std::vector< uint8_t > > try_receive(size_t size)
Receives data from the device.
result< void > try_polling_receive(std::span< uint8_t > rx_data)
Receives data using polling (busy-wait).
std::vector< uint8_t > polling_receive(size_t size)
Receives data using polling (busy-wait).
idfxx::future< void > queue_trans(const transaction &trans, const std::chrono::duration< Rep, Period > &timeout)
Queues a transaction for asynchronous execution with timeout.
void polling_transfer(std::span< const uint8_t > tx_data, std::span< uint8_t > rx_data)
Performs a full-duplex transfer using polling (busy-wait).
result< idfxx::future< void > > try_queue_trans(const transaction &trans, const std::chrono::duration< Rep, Period > &timeout)
Queues a transaction for asynchronous execution with timeout.
result< void > try_polling_transmit(std::span< const uint8_t > tx_data)
Transmits data using polling (busy-wait).
result< void > try_transfer(std::span< const uint8_t > tx_data, std::span< uint8_t > rx_data)
Performs a full-duplex transfer.
master_device & operator=(master_device &&other) noexcept
master_device(master_bus &bus, const struct config &config)
Creates a new SPI device on the specified bus.
master_device(master_device &&other) noexcept
void unlock() const
Releases exclusive bus access.
master_device(const master_device &)=delete
result< idfxx::future< void > > try_queue_trans(const transaction &trans)
Queues a transaction for asynchronous execution.
spi_device_handle_t idf_handle() const
Returns the underlying ESP-IDF device handle.
master_device & operator=(const master_device &)=delete
void lock() const
Acquires exclusive access to the SPI bus for this device.
static result< master_device > make(master_bus &bus, const struct config &config)
Creates a new SPI device on the specified bus.
void polling_transmit(std::span< const uint8_t > tx_data)
Transmits data using polling (busy-wait).
void receive(std::span< uint8_t > rx_data)
Receives data from the device into the provided buffer.
result< void > try_transmit(const transaction &trans)
Executes a full-control transaction.
result< void > try_transmit(std::span< const uint8_t > tx_data)
Transmits data to the device.
result< void > try_polling_transmit(const transaction &trans)
Executes a full-control transaction using polling (busy-wait).
result< std::vector< uint8_t > > try_polling_receive(size_t size)
Receives data using polling (busy-wait).
idfxx::future< void > queue_trans(const transaction &trans)
Queues a transaction for asynchronous execution.
result< void > try_receive(std::span< uint8_t > rx_data)
Receives data from the device into the provided buffer.
void polling_transmit(const transaction &trans)
Executes a full-control transaction using polling (busy-wait).
freq::hertz frequency() const
Returns the actual clock frequency in use.
void polling_receive(std::span< uint8_t > rx_data)
Receives data using polling (busy-wait).
result< void > try_polling_transfer(std::span< const uint8_t > tx_data, std::span< uint8_t > rx_data)
Performs a full-duplex transfer using polling (busy-wait).
std::string to_string(core_id c)
Returns a string representation of a CPU core identifier.
trans_flags
SPI transaction flags.
dma_chan
SPI DMA channel selection.
bus_flags
SPI bus capability and configuration flags.
device_flags
SPI device capability and configuration flags.
host_device
General purpose SPI Host Controller ID.
@ multiline_cmd
Use multi-line mode for command phase.
@ dma_use_psram
Use PSRAM for DMA buffer directly. Requires ESP-IDF 6.0+.
@ mode_qio
Transmit/receive data in 4-bit mode.
@ variable_cmd
Use per-transaction command_bits length.
@ use_rxdata
Receive into inline rx_data instead of rx_buffer.
@ use_txdata
Transmit inline tx_data instead of tx_buffer.
@ cs_keep_active
Keep CS active after data transfer.
@ mode_oct
Transmit/receive data in 8-bit mode.
@ variable_addr
Use per-transaction address_bits length.
@ variable_dummy
Use per-transaction dummy_bits length.
@ dma_buffer_align_manual
Disable automatic DMA buffer re-alignment.
@ multiline_addr
Use multi-line mode for address phase.
@ mode_dio
Transmit/receive data in 2-bit mode.
@ ch_auto
Auto select DMA channel.
@ slp_allow_pd
Allow to power down the peripheral during light sleep, and auto recover then.
@ slave
Bus supports slave mode.
@ iomux_pins
Bus uses IOMUX pins.
@ octal
Check existing of MOSI/MISO/WP/HD/SPIIO4/SPIIO5/SPIIO6/SPIIO7 pins as output.
@ dual
Check MOSI and MISO pins can output. Or indicates bus able to work under DIO mode.
@ quad
Check existing of MOSI/MISO/WP/HD pins as output.
@ io4_io7
Check existing of IO4~IO7 pins. Or indicates IO4~IO7 pins initialized.
@ wphd
Check existing of WP and HD pins. Or indicates WP & HD pins initialized.
@ native_pins
Bus uses native pins.
@ mosi
Check existing of MOSI pin. Or indicates MOSI line initialized.
@ miso
Check existing of MISO pin. Or indicates MISO line initialized.
@ master
Bus supports master mode.
@ sclk
Check existing of SCLK pin. Or indicates CLK line initialized.
@ no_dummy
Disable automatic dummy bit insertion.
@ halfduplex
Transmit data before receiving, not simultaneously.
@ three_wire
Use MOSI for both sending and receiving.
@ ddrclk
Use double data rate clocking.
@ clk_as_cs
Output clock on CS line while CS is active.
@ txbit_lsbfirst
Transmit command/address/data LSB first.
@ bit_lsbfirst
Transmit and receive LSB first.
@ rxbit_lsbfirst
Receive data LSB first.
@ no_return_result
Don't return descriptor on completion (use post_cb).
@ positive_cs
CS is active high during a transaction.
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
@ timeout
Operation timed out.
std::expected< T, std::error_code > result
result type wrapping a value or error code.
gpio::level data_idle_level
Data pin output level while no transaction is active.
idfxx::flags< idfxx::intr_flag > intr_flags
Behavioral interrupt flags.
idfxx::gpio data7
GPIO pin for spi data7 signal in octal mode, or gpio::nc() if not used.
idfxx::gpio mosi
GPIO pin for Master Out Slave In (=spi_d) signal.
idfxx::gpio data1
GPIO pin for spi data1 signal in quad/octal mode.
idfxx::gpio miso
GPIO pin for Master In Slave Out (=spi_q) signal.
idfxx::gpio data2
GPIO pin for spi data2 signal in quad/octal mode, or gpio::nc() if not used.
idfxx::gpio data4
GPIO pin for spi data4 signal in octal mode, or gpio::nc() if not used.
idfxx::gpio quadwp
GPIO pin for WP (Write Protect) signal, or gpio::nc() if not used.
size_t max_transfer_sz
Maximum transfer size, in bytes (DMA only).
idfxx::gpio data0
GPIO pin for spi data0 signal in quad/octal mode.
idfxx::gpio data5
GPIO pin for spi data5 signal in octal mode, or gpio::nc() if not used.
idfxx::gpio sclk
GPIO pin for SPI Clock signal.
idfxx::gpio data3
GPIO pin for spi data3 signal in quad/octal mode, or gpio::nc() if not used.
idfxx::gpio quadhd
GPIO pin for HD (Hold) signal, or gpio::nc() if not used.
idfxx::gpio data6
GPIO pin for spi data6 signal in octal mode, or gpio::nc() if not used.
std::optional< idfxx::core_id > isr_cpu_id
Select cpu core to register SPI ISR.
SPI device configuration.
std::chrono::nanoseconds input_delay
Maximum data valid time of slave.
uint8_t mode
SPI mode (0-3): (CPOL, CPHA) pair.
uint16_t cs_ena_pretrans
SPI bit-cycles CS is active before transmission (half-duplex only).
uint8_t cs_ena_posttrans
SPI bit-cycles CS stays active after transmission.
freq::hertz clock_speed
SPI clock speed in Hz.
uint8_t dummy_bits
Dummy bits between address and data phase.
uint8_t address_bits
Default address phase length in bits (0-64).
int queue_size
Transaction queue depth for async API.
uint8_t command_bits
Default command phase length in bits (0-16).
uint16_t duty_cycle_pos
Duty cycle of positive clock in 1/256 increments (128 = 50%, 0 = default).
idfxx::gpio cs
GPIO pin for chip select, or gpio::nc() if not used.
SPI transaction descriptor for full-control transactions.
size_t length
Total data length in bits.
uint8_t dummy_bits
Dummy length for variable_dummy flag.
uint64_t addr
Address data (length set by device config or variable_addr).
std::span< uint8_t > rx_buffer
Receive buffer, or empty for no MISO phase.
freq::hertz override_freq
Override clock speed for this transaction (0 = no override).
uint16_t cmd
Command data (length set by device config or variable_cmd).
std::span< const uint8_t > tx_buffer
Transmit buffer, or empty for no MOSI phase.
size_t rx_length
Receive length in bits (0 = same as length).
uint8_t command_bits
Command length for variable_cmd flag.
uint8_t address_bits
Address length for variable_addr flag.