ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
spi_esp_idf.cpp
Go to the documentation of this file.
1#include "spi.h"
2#include <vector>
3
4namespace esphome {
5namespace spi {
6
7#ifdef USE_ESP_IDF
8static const char *const TAG = "spi-esp-idf";
9static const size_t MAX_TRANSFER_SIZE = 4092; // dictated by ESP-IDF API.
10
11class SPIDelegateHw : public SPIDelegate {
12 public:
13 SPIDelegateHw(SPIInterface channel, uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin,
14 bool release_device, bool write_only)
15 : SPIDelegate(data_rate, bit_order, mode, cs_pin),
16 channel_(channel),
17 release_device_(release_device),
18 write_only_(write_only) {
19 if (!this->release_device_)
20 add_device_();
21 }
22
23 bool is_ready() override { return this->handle_ != nullptr; }
24
25 void begin_transaction() override {
26 if (this->release_device_)
27 this->add_device_();
28 if (this->is_ready()) {
29 if (spi_device_acquire_bus(this->handle_, portMAX_DELAY) != ESP_OK)
30 ESP_LOGE(TAG, "Failed to acquire SPI bus");
32 } else {
33 ESP_LOGW(TAG, "SPI device not ready, cannot begin transaction");
34 }
35 }
36
37 void end_transaction() override {
38 if (this->is_ready()) {
40 spi_device_release_bus(this->handle_);
41 if (this->release_device_) {
42 spi_bus_remove_device(this->handle_);
43 this->handle_ = nullptr; // reset handle to indicate no device is registered
44 }
45 }
46 }
47
48 ~SPIDelegateHw() override {
49 esp_err_t const err = spi_bus_remove_device(this->handle_);
50 if (err != ESP_OK)
51 ESP_LOGE(TAG, "Remove device failed - err %X", err);
52 }
53
54 // do a transfer. either txbuf or rxbuf (but not both) may be null.
55 // transfers above the maximum size will be split.
56 // TODO - make use of the queue for interrupt transfers to provide a (short) pipeline of blocks
57 // when splitting is required.
58 void transfer(const uint8_t *txbuf, uint8_t *rxbuf, size_t length) override {
59 if (rxbuf != nullptr && this->write_only_) {
60 ESP_LOGE(TAG, "Attempted read from write-only channel");
61 return;
62 }
63 spi_transaction_t desc = {};
64 desc.flags = 0;
65 while (length != 0) {
66 size_t const partial = std::min(length, MAX_TRANSFER_SIZE);
67 desc.length = partial * 8;
68 desc.rxlength = this->write_only_ ? 0 : partial * 8;
69 desc.tx_buffer = txbuf;
70 desc.rx_buffer = rxbuf;
71 // polling is used as it has about 10% less overhead than queuing an interrupt transfer
72 esp_err_t err = spi_device_polling_start(this->handle_, &desc, portMAX_DELAY);
73 if (err == ESP_OK) {
74 err = spi_device_polling_end(this->handle_, portMAX_DELAY);
75 }
76 if (err != ESP_OK) {
77 ESP_LOGE(TAG, "Transmit failed - err %X", err);
78 break;
79 }
80 length -= partial;
81 if (txbuf != nullptr)
82 txbuf += partial;
83 if (rxbuf != nullptr)
84 rxbuf += partial;
85 }
86 }
87
88 void write(uint16_t data, size_t num_bits) override {
89 spi_transaction_ext_t desc = {};
90 desc.command_bits = num_bits;
91 desc.base.flags = SPI_TRANS_VARIABLE_CMD;
92 desc.base.cmd = data;
93 esp_err_t err = spi_device_polling_start(this->handle_, (spi_transaction_t *) &desc, portMAX_DELAY);
94 if (err == ESP_OK) {
95 err = spi_device_polling_end(this->handle_, portMAX_DELAY);
96 }
97
98 if (err != ESP_OK) {
99 ESP_LOGE(TAG, "Transmit failed - err %X", err);
100 }
101 }
102
113 void write_cmd_addr_data(size_t cmd_bits, uint32_t cmd, size_t addr_bits, uint32_t address, const uint8_t *data,
114 size_t length, uint8_t bus_width) override {
115 spi_transaction_ext_t desc = {};
116 if (length == 0 && cmd_bits == 0 && addr_bits == 0) {
117 esph_log_w(TAG, "Nothing to transfer");
118 return;
119 }
120 desc.base.flags = SPI_TRANS_VARIABLE_ADDR | SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_DUMMY;
121 if (bus_width == 4) {
122 desc.base.flags |= SPI_TRANS_MODE_QIO;
123 } else if (bus_width == 8) {
124 desc.base.flags |= SPI_TRANS_MODE_OCT;
125 }
126 desc.command_bits = cmd_bits;
127 desc.address_bits = addr_bits;
128 desc.dummy_bits = 0;
129 desc.base.rxlength = 0;
130 desc.base.cmd = cmd;
131 desc.base.addr = address;
132 do {
133 size_t chunk_size = std::min(length, MAX_TRANSFER_SIZE);
134 if (data != nullptr && chunk_size != 0) {
135 desc.base.length = chunk_size * 8;
136 desc.base.tx_buffer = data;
137 length -= chunk_size;
138 data += chunk_size;
139 } else {
140 length = 0;
141 desc.base.length = 0;
142 }
143 esp_err_t err = spi_device_polling_start(this->handle_, (spi_transaction_t *) &desc, portMAX_DELAY);
144 if (err == ESP_OK) {
145 err = spi_device_polling_end(this->handle_, portMAX_DELAY);
146 }
147 if (err != ESP_OK) {
148 ESP_LOGE(TAG, "Transmit failed - err %X", err);
149 return;
150 }
151 // if more data is to be sent, skip the command and address phases.
152 desc.command_bits = 0;
153 desc.address_bits = 0;
154 } while (length != 0);
155 }
156
157 void transfer(uint8_t *ptr, size_t length) override { this->transfer(ptr, ptr, length); }
158
159 uint8_t transfer(uint8_t data) override {
160 uint8_t rxbuf;
161 this->transfer(&data, &rxbuf, 1);
162 return rxbuf;
163 }
164
165 void write16(uint16_t data) override { this->write(data, 16); }
166
167 void write_array(const uint8_t *ptr, size_t length) override { this->transfer(ptr, nullptr, length); }
168
169 void write_array16(const uint16_t *data, size_t length) override {
170 if (this->bit_order_ == BIT_ORDER_LSB_FIRST) {
171 this->write_array((uint8_t *) data, length * 2);
172 } else {
173 uint16_t buffer[MAX_TRANSFER_SIZE / 2];
174 while (length != 0) {
175 size_t const partial = std::min(length, MAX_TRANSFER_SIZE / 2);
176 for (size_t i = 0; i != partial; i++) {
177 buffer[i] = SPI_SWAP_DATA_TX(*data++, 16);
178 }
179 this->write_array((const uint8_t *) buffer, partial * 2);
180 length -= partial;
181 }
182 }
183 }
184
185 void read_array(uint8_t *ptr, size_t length) override { this->transfer(nullptr, ptr, length); }
186
187 protected:
188 bool add_device_() {
189 spi_device_interface_config_t config = {};
190 config.mode = static_cast<uint8_t>(this->mode_);
191 config.clock_speed_hz = static_cast<int>(this->data_rate_);
192 config.spics_io_num = -1;
193 config.flags = 0;
194 config.queue_size = 1;
195 config.pre_cb = nullptr;
196 config.post_cb = nullptr;
197 if (this->bit_order_ == BIT_ORDER_LSB_FIRST)
198 config.flags |= SPI_DEVICE_BIT_LSBFIRST;
199 if (this->write_only_)
200 config.flags |= SPI_DEVICE_HALFDUPLEX | SPI_DEVICE_NO_DUMMY;
201 esp_err_t const err = spi_bus_add_device(this->channel_, &config, &this->handle_);
202 if (err != ESP_OK) {
203 ESP_LOGE(TAG, "Add device failed - err %X", err);
204 return false;
205 }
206 return true;
207 }
208
209 SPIInterface channel_{};
210 spi_device_handle_t handle_{};
211 bool release_device_{false};
212 bool write_only_{false};
213};
214
215class SPIBusHw : public SPIBus {
216 public:
217 SPIBusHw(GPIOPin *clk, GPIOPin *sdo, GPIOPin *sdi, SPIInterface channel, std::vector<uint8_t> data_pins)
218 : SPIBus(clk, sdo, sdi), channel_(channel) {
219 spi_bus_config_t buscfg = {};
220 buscfg.sclk_io_num = Utility::get_pin_no(clk);
221 buscfg.flags = SPICOMMON_BUSFLAG_MASTER | SPICOMMON_BUSFLAG_SCLK;
222 if (data_pins.empty()) {
223 buscfg.mosi_io_num = Utility::get_pin_no(sdo);
224 buscfg.miso_io_num = Utility::get_pin_no(sdi);
225 buscfg.quadwp_io_num = -1;
226 buscfg.quadhd_io_num = -1;
227 } else {
228 buscfg.data0_io_num = data_pins[0];
229 buscfg.data1_io_num = data_pins[1];
230 buscfg.data2_io_num = data_pins[2];
231 buscfg.data3_io_num = data_pins[3];
232 if (data_pins.size() == 8) {
233 buscfg.data4_io_num = data_pins[4];
234 buscfg.data5_io_num = data_pins[5];
235 buscfg.data6_io_num = data_pins[6];
236 buscfg.data7_io_num = data_pins[7];
237 buscfg.flags |= SPICOMMON_BUSFLAG_OCTAL;
238 } else {
239 buscfg.data4_io_num = -1;
240 buscfg.data5_io_num = -1;
241 buscfg.data6_io_num = -1;
242 buscfg.data7_io_num = -1;
243 buscfg.flags |= SPICOMMON_BUSFLAG_QUAD;
244 }
245 }
246 buscfg.max_transfer_sz = MAX_TRANSFER_SIZE;
247 auto err = spi_bus_initialize(channel, &buscfg, SPI_DMA_CH_AUTO);
248 if (err != ESP_OK)
249 ESP_LOGE(TAG, "Bus init failed - err %X", err);
250 }
251
252 SPIDelegate *get_delegate(uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin,
253 bool release_device, bool write_only) override {
254 return new SPIDelegateHw(this->channel_, data_rate, bit_order, mode, cs_pin, release_device,
255 write_only || Utility::get_pin_no(this->sdi_pin_) == -1);
256 }
257
258 protected:
259 SPIInterface channel_{};
260
261 bool is_hw() override { return true; }
262};
263
264SPIBus *SPIComponent::get_bus(SPIInterface interface, GPIOPin *clk, GPIOPin *sdo, GPIOPin *sdi,
265 const std::vector<uint8_t> &data_pins) {
266 return new SPIBusHw(clk, sdo, sdi, interface, data_pins);
267}
268
269#endif
270} // namespace spi
271} // namespace esphome
BedjetMode mode
BedJet operating mode.
uint8_t address
Definition bl0906.h:4
GPIOPin * sdi_pin_
Definition spi.h:330
static SPIBus * get_bus(SPIInterface interface, GPIOPin *clk, GPIOPin *sdo, GPIOPin *sdi, const std::vector< uint8_t > &data_pins)
virtual void end_transaction()
Definition spi.h:196
virtual void begin_transaction()
Definition spi.h:193
SPIBitOrder bit_order_
Definition spi.h:255
static int get_pin_no(GPIOPin *pin)
Definition spi.h:136
SPIMode
Modes mapping to clock phase and polarity.
Definition spi.h:80
const char *const TAG
Definition spi.cpp:8
SPIBitOrder
The bit-order for SPI devices. This defines how the data read from and written to the device is inter...
Definition spi.h:42
@ BIT_ORDER_LSB_FIRST
The least significant bit is transmitted/received first.
Definition spi.h:44
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
SPIClassRP2040 * SPIInterface
Definition spi.h:15
uint16_t length
Definition tt21100.cpp:0