ESPHome 2026.11.0b6
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
10namespace esphome::ethernet {
11
12namespace {
13
14// Context returned by init() and handed back to read/write/deinit. There is one W5500 per device, so a
15// single static instance replaces a heap allocation that could fail. It is always clear when init() runs:
16// esp_eth_mac_new_w5500() calls deinit() on every failure after init() succeeded, and nothing else
17// uninstalls the driver
18struct W5500CustomSpiContext {
19 spi_device_handle_t handle;
20 SemaphoreHandle_t lock;
21};
22// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) - intentional mutable state
23W5500CustomSpiContext w5500_context{};
24
25// Transfers up to the ESP32 SPI hardware FIFO size (64 bytes) stay on the polling path; larger
26// transfers (the frame payloads) use the blocking, DMA-backed transmit.
27constexpr uint32_t W5500_SPI_BULK_THRESHOLD = 64;
28constexpr uint32_t W5500_SPI_LOCK_TIMEOUT_MS = 50;
29
30void *w5500_custom_spi_init(const void *spi_config) {
31 const auto *config = static_cast<const eth_w5500_config_t *>(spi_config);
32 auto *ctx = &w5500_context;
33 // The W5500 SPI frame carries the 16-bit address in the command phase and the 8-bit control
34 // byte in the address phase; mirror what the stock driver configures.
35#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
36 spi_device_interface_config_t devcfg = *config->base.spi_devcfg;
37 const spi_host_device_t spi_host_id = config->base.spi_host_id;
38#else
39 spi_device_interface_config_t devcfg = *config->spi_devcfg;
40 const spi_host_device_t spi_host_id = config->spi_host_id;
41#endif
42 devcfg.command_bits = 16;
43 devcfg.address_bits = 8;
44 if (spi_bus_add_device(spi_host_id, &devcfg, &ctx->handle) != ESP_OK) {
45 ctx->handle = nullptr;
46 return nullptr;
47 }
48 ctx->lock = xSemaphoreCreateMutex();
49 if (ctx->lock == nullptr) {
50 spi_bus_remove_device(ctx->handle);
51 ctx->handle = nullptr;
52 return nullptr;
53 }
54 return ctx;
55}
56
57esp_err_t w5500_custom_spi_deinit(void *spi_ctx) {
58 auto *ctx = static_cast<W5500CustomSpiContext *>(spi_ctx);
59 spi_bus_remove_device(ctx->handle);
60 vSemaphoreDelete(ctx->lock);
61 *ctx = {};
62 return ESP_OK;
63}
64
65// Runs one transaction under the device lock, choosing the polling vs blocking transmit by size.
66// Bulk payloads (> FIFO size) block so the calling task sleeps while DMA runs; small register
67// accesses stay on the cheaper polling path. Used by both read and write.
68esp_err_t w5500_custom_spi_transfer(W5500CustomSpiContext *ctx, spi_transaction_t *trans, uint32_t len) {
69 if (xSemaphoreTake(ctx->lock, pdMS_TO_TICKS(W5500_SPI_LOCK_TIMEOUT_MS)) != pdTRUE) {
70 return ESP_ERR_TIMEOUT;
71 }
73 if (len > W5500_SPI_BULK_THRESHOLD) {
74 ret = spi_device_transmit(ctx->handle, trans);
75 } else {
76 ret = spi_device_polling_transmit(ctx->handle, trans);
77 }
78 xSemaphoreGive(ctx->lock);
79 return ret;
80}
81
82esp_err_t w5500_custom_spi_write(void *spi_ctx, uint32_t cmd, uint32_t addr, const void *data, uint32_t len) {
83 auto *ctx = static_cast<W5500CustomSpiContext *>(spi_ctx);
84 spi_transaction_t trans = {};
85 trans.cmd = static_cast<uint16_t>(cmd);
86 trans.addr = addr;
87 trans.length = 8 * len;
88 trans.tx_buffer = data;
89 return w5500_custom_spi_transfer(ctx, &trans, len);
90}
91
92esp_err_t w5500_custom_spi_read(void *spi_ctx, uint32_t cmd, uint32_t addr, void *data, uint32_t len) {
93 auto *ctx = static_cast<W5500CustomSpiContext *>(spi_ctx);
94 spi_transaction_t trans = {};
95 // Reads of <= 4 bytes use the transaction's inline RX buffer to avoid 4-byte boundary
96 // overwrites of adjacent registers (same guard the stock driver uses).
97 const bool use_rxdata = len <= 4;
98 trans.flags = use_rxdata ? SPI_TRANS_USE_RXDATA : 0;
99 trans.cmd = static_cast<uint16_t>(cmd);
100 trans.addr = addr;
101 trans.length = 8 * len;
102 trans.rx_buffer = data;
103 esp_err_t ret = w5500_custom_spi_transfer(ctx, &trans, len);
104 if (use_rxdata && (ret == ESP_OK)) {
105 memcpy(data, trans.rx_data, len);
106 }
107 return ret;
108}
109
110} // namespace
111
112void install_w5500_async_spi(eth_w5500_config_t &config) {
113 // Point the custom driver's config at the W5500 config itself; init() reads spi_host_id and
114 // spi_devcfg back out of it. The self-reference is valid because both the config and the
115 // spi_devcfg it points at outlive the esp_eth_mac_new_w5500() call that runs init().
116#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
117 config.base.custom_spi_driver.config = &config;
118 config.base.custom_spi_driver.init = w5500_custom_spi_init;
119 config.base.custom_spi_driver.deinit = w5500_custom_spi_deinit;
120 config.base.custom_spi_driver.read = w5500_custom_spi_read;
121 config.base.custom_spi_driver.write = w5500_custom_spi_write;
122#else
123 config.custom_spi_driver.config = &config;
124 config.custom_spi_driver.init = w5500_custom_spi_init;
125 config.custom_spi_driver.deinit = w5500_custom_spi_deinit;
126 config.custom_spi_driver.read = w5500_custom_spi_read;
127 config.custom_spi_driver.write = w5500_custom_spi_write;
128#endif
129}
130
131} // namespace esphome::ethernet
132
133#endif // USE_ESP32 && USE_ETHERNET_W5500
int ret
void install_w5500_async_spi(eth_w5500_config_t &config)
std::span< const uint8_t > data
const void size_t len
Definition hal.h:68
static void uint32_t
SemaphoreHandle_t lock
spi_device_handle_t handle