3#if defined(USE_ESP32) && defined(USE_ETHERNET_W5500)
5#include <driver/spi_master.h>
6#include <freertos/FreeRTOS.h>
7#include <freertos/semphr.h>
18struct W5500CustomSpiContext {
23W5500CustomSpiContext w5500_context{};
27constexpr uint32_t W5500_SPI_BULK_THRESHOLD = 64;
28constexpr uint32_t W5500_SPI_LOCK_TIMEOUT_MS = 50;
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;
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;
39 spi_device_interface_config_t devcfg = *config->spi_devcfg;
40 const spi_host_device_t spi_host_id = config->spi_host_id;
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;
48 ctx->lock = xSemaphoreCreateMutex();
49 if (ctx->lock ==
nullptr) {
50 spi_bus_remove_device(ctx->handle);
51 ctx->handle =
nullptr;
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);
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;
73 if (
len > W5500_SPI_BULK_THRESHOLD) {
74 ret = spi_device_transmit(ctx->handle, trans);
76 ret = spi_device_polling_transmit(ctx->handle, trans);
78 xSemaphoreGive(ctx->lock);
83 auto *ctx =
static_cast<W5500CustomSpiContext *
>(spi_ctx);
84 spi_transaction_t trans = {};
85 trans.cmd =
static_cast<uint16_t
>(cmd);
87 trans.length = 8 *
len;
88 trans.tx_buffer =
data;
89 return w5500_custom_spi_transfer(ctx, &trans,
len);
93 auto *ctx =
static_cast<W5500CustomSpiContext *
>(spi_ctx);
94 spi_transaction_t trans = {};
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);
101 trans.length = 8 *
len;
102 trans.rx_buffer =
data;
104 if (use_rxdata && (
ret == ESP_OK)) {
105 memcpy(data, trans.rx_data,
len);
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;
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;
void install_w5500_async_spi(eth_w5500_config_t &config)
std::span< const uint8_t > data
spi_device_handle_t handle