ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
w5500_custom_spi.cpp
Go to the documentation of this file.
1#include "w5500_custom_spi.h"
2
3#if defined(USE_ESP32) && defined(USE_ETHERNET_W5500)
4
5#include <driver/spi_master.h>
6#include <freertos/FreeRTOS.h>
7#include <freertos/semphr.h>
8#include <cstring>
9#include <new>
10
11namespace esphome::ethernet {
12
13namespace {
14
15// Per-device context returned by init() and handed back to read/write/deinit.
16struct W5500CustomSpiContext {
17 spi_device_handle_t handle;
18 SemaphoreHandle_t lock;
19};
20
21// Transfers up to the ESP32 SPI hardware FIFO size (64 bytes) stay on the polling path; larger
22// transfers (the frame payloads) use the blocking, DMA-backed transmit.
23constexpr uint32_t W5500_SPI_BULK_THRESHOLD = 64;
24constexpr uint32_t W5500_SPI_LOCK_TIMEOUT_MS = 50;
25
26void *w5500_custom_spi_init(const void *spi_config) {
27 const auto *config = static_cast<const eth_w5500_config_t *>(spi_config);
28 auto *ctx = new (std::nothrow) W5500CustomSpiContext{};
29 if (ctx == nullptr) {
30 return nullptr;
31 }
32 // The W5500 SPI frame carries the 16-bit address in the command phase and the 8-bit control
33 // byte in the address phase; mirror what the stock driver configures.
34 spi_device_interface_config_t devcfg = *config->spi_devcfg;
35 devcfg.command_bits = 16;
36 devcfg.address_bits = 8;
37 if (spi_bus_add_device(config->spi_host_id, &devcfg, &ctx->handle) != ESP_OK) {
38 delete ctx;
39 return nullptr;
40 }
41 ctx->lock = xSemaphoreCreateMutex();
42 if (ctx->lock == nullptr) {
43 spi_bus_remove_device(ctx->handle);
44 delete ctx;
45 return nullptr;
46 }
47 return ctx;
48}
49
50esp_err_t w5500_custom_spi_deinit(void *spi_ctx) {
51 auto *ctx = static_cast<W5500CustomSpiContext *>(spi_ctx);
52 spi_bus_remove_device(ctx->handle);
53 vSemaphoreDelete(ctx->lock);
54 delete ctx;
55 return ESP_OK;
56}
57
58// Runs one transaction under the device lock, choosing the polling vs blocking transmit by size.
59// Bulk payloads (> FIFO size) block so the calling task sleeps while DMA runs; small register
60// accesses stay on the cheaper polling path. Used by both read and write.
61esp_err_t w5500_custom_spi_transfer(W5500CustomSpiContext *ctx, spi_transaction_t *trans, uint32_t len) {
62 if (xSemaphoreTake(ctx->lock, pdMS_TO_TICKS(W5500_SPI_LOCK_TIMEOUT_MS)) != pdTRUE) {
63 return ESP_ERR_TIMEOUT;
64 }
65 esp_err_t ret;
66 if (len > W5500_SPI_BULK_THRESHOLD) {
67 ret = spi_device_transmit(ctx->handle, trans);
68 } else {
69 ret = spi_device_polling_transmit(ctx->handle, trans);
70 }
71 xSemaphoreGive(ctx->lock);
72 return ret;
73}
74
75esp_err_t w5500_custom_spi_write(void *spi_ctx, uint32_t cmd, uint32_t addr, const void *data, uint32_t len) {
76 auto *ctx = static_cast<W5500CustomSpiContext *>(spi_ctx);
77 spi_transaction_t trans = {};
78 trans.cmd = static_cast<uint16_t>(cmd);
79 trans.addr = addr;
80 trans.length = 8 * len;
81 trans.tx_buffer = data;
82 return w5500_custom_spi_transfer(ctx, &trans, len);
83}
84
85esp_err_t w5500_custom_spi_read(void *spi_ctx, uint32_t cmd, uint32_t addr, void *data, uint32_t len) {
86 auto *ctx = static_cast<W5500CustomSpiContext *>(spi_ctx);
87 spi_transaction_t trans = {};
88 // Reads of <= 4 bytes use the transaction's inline RX buffer to avoid 4-byte boundary
89 // overwrites of adjacent registers (same guard the stock driver uses).
90 const bool use_rxdata = len <= 4;
91 trans.flags = use_rxdata ? SPI_TRANS_USE_RXDATA : 0;
92 trans.cmd = static_cast<uint16_t>(cmd);
93 trans.addr = addr;
94 trans.length = 8 * len;
95 trans.rx_buffer = data;
96 esp_err_t ret = w5500_custom_spi_transfer(ctx, &trans, len);
97 if (use_rxdata && (ret == ESP_OK)) {
98 memcpy(data, trans.rx_data, len);
99 }
100 return ret;
101}
102
103} // namespace
104
105void install_w5500_async_spi(eth_w5500_config_t &config) {
106 // Point the custom driver's config at the W5500 config itself; init() reads spi_host_id and
107 // spi_devcfg back out of it. The self-reference is valid because both the config and the
108 // spi_devcfg it points at outlive the esp_eth_mac_new_w5500() call that runs init().
109 config.custom_spi_driver.config = &config;
110 config.custom_spi_driver.init = w5500_custom_spi_init;
111 config.custom_spi_driver.deinit = w5500_custom_spi_deinit;
112 config.custom_spi_driver.read = w5500_custom_spi_read;
113 config.custom_spi_driver.write = w5500_custom_spi_write;
114}
115
116} // namespace esphome::ethernet
117
118#endif // USE_ESP32 && USE_ETHERNET_W5500
void install_w5500_async_spi(eth_w5500_config_t &config)
const std::vector< uint8_t > & data
const void size_t len
Definition hal.h:64
static void uint32_t
SemaphoreHandle_t lock
spi_device_handle_t handle