6#include "driver/gpio.h"
8#include "esp_intr_alloc.h"
10#include "hal/spi_ll.h"
11#include "hal/spi_types.h"
12#include "soc/soc_caps.h"
21 ESP32SPI(spi_host_device_t host,
int sclk_pin,
int miso_pin,
int mosi_pin,
30 uint32_t dma_enable_min_size = 3U,
bool enable_dma =
true);
33 bool in_isr =
false)
override;
38 bool in_isr =
false)
override;
41 bool in_isr =
false)
override;
54 static constexpr size_t MAX_POLLING_TRANSFER_BYTES = SOC_SPI_MAXIMUM_BUFFER_SIZE;
55 static constexpr size_t MAX_DMA_TRANSFER_BYTES = SPI_LL_DMA_MAX_BIT_LEN / 8U;
65 ErrorCode ResolveClockSource(uint32_t& source_hz);
71 static void SpiIsrEntry(
void* arg);
73 void HandleInterrupt();
75 void FinishAsync(
bool in_isr,
ErrorCode ec);
77 bool CanUseDma(
size_t size)
const;
87 ErrorCode StartAsyncTransfer(
const uint8_t* tx, uint8_t* rx,
size_t size,
88 bool enable_rx,
RawData read_back,
bool mem_read,
91 void ConfigureTransferRegisters(
size_t size);
93 ErrorCode ExecuteChunk(
const uint8_t* tx, uint8_t* rx,
size_t size,
bool enable_rx);
95 ErrorCode ExecuteTransfer(
const uint8_t* tx, uint8_t* rx,
size_t size,
bool enable_rx);
97 bool UseLocalDoubleBuffer()
const;
99 void SwitchBufferLocal();
101 spi_host_device_t host_;
102 spi_dev_t* hw_ =
nullptr;
106 uint32_t source_clock_hz_ = 0;
108 bool initialized_ =
false;
109 uint32_t dma_enable_min_size_ = 3U;
110 bool dma_requested_ =
true;
111 RawData dma_rx_raw_ = {
nullptr, 0};
112 RawData dma_tx_raw_ = {
nullptr, 0};
113 size_t dbuf_rx_block_size_ = 0;
114 size_t dbuf_tx_block_size_ = 0;
115 uint8_t dbuf_active_block_ = 0;
117 bool dbuf_enabled_ =
false;
118 intr_handle_t intr_handle_ =
nullptr;
119 bool intr_installed_ =
false;
120 bool dma_enabled_ =
false;
121 void* dma_ctx_ =
nullptr;
122 size_t dma_max_transfer_bytes_ = 0;
123 bool async_running_ =
false;
124 size_t async_dma_size_ = 0;
125 bool async_dma_rx_enabled_ =
false;
126 bool mem_read_ =
false;
127 RawData read_back_ = {
nullptr, 0};
Shared BLOCK waiter handoff for synchronous driver operations.
只读原始数据视图 / Immutable raw data view
Prescaler GetMaxPrescaler() const override
获取 SPI 设备的最大分频系数。Gets the maximum prescaler of the SPI device.
ErrorCode Transfer(size_t size, OperationRW &op, bool in_isr=false) override
进行一次SPI传输(使用当前缓冲区数据,零拷贝,支持双缓冲)。 Performs a SPI transfer (zero-copy, supports double buffering).
ErrorCode MemWrite(uint16_t reg, ConstRawData write_data, OperationRW &op, bool in_isr=false) override
向 SPI 设备的寄存器写入数据。 Writes data to a specific register of the SPI device.
ErrorCode MemRead(uint16_t reg, RawData read_data, OperationRW &op, bool in_isr=false) override
从 SPI 设备的寄存器读取数据。 Reads data from a specific register of the SPI device.
uint32_t GetMaxBusSpeed() const override
获取 SPI 设备的最大时钟速度。Gets the maximum clock speed of the SPI device.
ErrorCode SetConfig(SPI::Configuration config) override
设置 SPI 配置参数。Sets SPI configuration parameters.
ErrorCode ReadAndWrite(RawData read_data, ConstRawData write_data, OperationRW &op, bool in_isr=false) override
进行 SPI 读写操作。Performs SPI read and write operations.
可写原始数据视图 / Mutable raw data view
串行外设接口(SPI)抽象类。Abstract class for Serial Peripheral Interface (SPI).
@ EDGE_1
在第一个时钟边沿采样数据。Data sampled on the first clock edge.
@ DIV_8
分频系数为 8。Division factor is 8.
@ LOW
时钟空闲时为低电平。Clock idle low.
存储 SPI 配置参数的结构体。Structure for storing SPI configuration parameters.