[spi] Use ESP-IDF driver for ESP32 Arduino (#12420)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Jonathan Swoboda
2025-12-16 19:44:01 -05:00
committed by GitHub
parent 9727c7135c
commit 9cd888cef6
5 changed files with 30 additions and 40 deletions

View File

@@ -272,10 +272,11 @@ def validate_spi_config(config):
# Given an SPI index, convert to a string that represents the C++ object for it.
def get_spi_interface(index):
if CORE.using_esp_idf:
platform = get_target_platform()
if platform == PLATFORM_ESP32:
# ESP32 uses ESP-IDF SPI driver for both Arduino and IDF frameworks
return ["SPI2_HOST", "SPI3_HOST"][index]
# Arduino code follows
platform = get_target_platform()
if platform == PLATFORM_RP2040:
return ["&SPI", "&SPI1"][index]
if index == 0:
@@ -356,7 +357,7 @@ CONFIG_SCHEMA = cv.All(
async def to_code(configs):
cg.add_define("USE_SPI")
cg.add_global(spi_ns.using)
if CORE.using_arduino:
if CORE.using_arduino and not CORE.is_esp32:
cg.add_library("SPI", None)
for spi in configs:
var = cg.new_Pvariable(spi[CONF_ID])
@@ -447,13 +448,15 @@ def final_validate_device_schema(name: str, *, require_mosi: bool, require_miso:
FILTER_SOURCE_FILES = filter_source_files_from_platform(
{
"spi_arduino.cpp": {
PlatformFramework.ESP32_ARDUINO,
PlatformFramework.ESP8266_ARDUINO,
PlatformFramework.RP2040_ARDUINO,
PlatformFramework.BK72XX_ARDUINO,
PlatformFramework.RTL87XX_ARDUINO,
PlatformFramework.LN882X_ARDUINO,
},
"spi_esp_idf.cpp": {PlatformFramework.ESP32_IDF},
"spi_esp_idf.cpp": {
PlatformFramework.ESP32_ARDUINO,
PlatformFramework.ESP32_IDF,
},
}
)

View File

@@ -2,8 +2,7 @@
#include "esphome/core/log.h"
#include "esphome/core/application.h"
namespace esphome {
namespace spi {
namespace esphome::spi {
const char *const TAG = "spi";
@@ -119,5 +118,4 @@ uint16_t SPIDelegateBitBash::transfer_(uint16_t data, size_t num_bits) {
return out_data;
}
} // namespace spi
} // namespace esphome
} // namespace esphome::spi

View File

@@ -1,4 +1,5 @@
#pragma once
#ifndef USE_ZEPHYR
#include "esphome/core/application.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
@@ -7,7 +8,13 @@
#include <utility>
#include <vector>
#ifdef USE_ARDUINO
#ifdef USE_ESP32
#include "driver/spi_master.h"
using SPIInterface = spi_host_device_t;
#elif defined(USE_ARDUINO)
#include <SPI.h>
@@ -17,26 +24,12 @@ using SPIInterface = SPIClassRP2040 *;
using SPIInterface = SPIClass *;
#endif
#endif
#ifdef USE_ESP_IDF
#include "driver/spi_master.h"
using SPIInterface = spi_host_device_t;
#endif // USE_ESP_IDF
#ifdef USE_ZEPHYR
// TODO supprse clang-tidy. Remove after SPI driver for nrf52 is added.
using SPIInterface = void *;
#endif
#endif // USE_ESP32 / USE_ARDUINO
/**
* Implementation of SPI Controller mode.
*/
namespace esphome {
namespace spi {
namespace esphome::spi {
/// The bit-order for SPI devices. This defines how the data read from and written to the device is interpreted.
enum SPIBitOrder {
@@ -509,5 +502,5 @@ class SPIDevice : public SPIClient {
template<size_t N> void transfer_array(std::array<uint8_t, N> &data) { this->transfer_array(data.data(), N); }
};
} // namespace spi
} // namespace esphome
} // namespace esphome::spi
#endif // USE_ZEPHYR

View File

@@ -1,9 +1,8 @@
#include "spi.h"
#include <vector>
namespace esphome {
namespace spi {
#ifdef USE_ARDUINO
namespace esphome::spi {
#if defined(USE_ARDUINO) && !defined(USE_ESP32)
static const char *const TAG = "spi-esp-arduino";
class SPIDelegateHw : public SPIDelegate {
@@ -101,6 +100,5 @@ SPIBus *SPIComponent::get_bus(SPIInterface interface, GPIOPin *clk, GPIOPin *sdo
return new SPIBusHw(clk, sdo, sdi, interface);
}
#endif // USE_ARDUINO
} // namespace spi
} // namespace esphome
#endif // USE_ARDUINO && !USE_ESP32
} // namespace esphome::spi

View File

@@ -1,10 +1,9 @@
#include "spi.h"
#include <vector>
namespace esphome {
namespace spi {
namespace esphome::spi {
#ifdef USE_ESP_IDF
#ifdef USE_ESP32
static const char *const TAG = "spi-esp-idf";
static const size_t MAX_TRANSFER_SIZE = 4092; // dictated by ESP-IDF API.
@@ -266,6 +265,5 @@ SPIBus *SPIComponent::get_bus(SPIInterface interface, GPIOPin *clk, GPIOPin *sdo
return new SPIBusHw(clk, sdo, sdi, interface, data_pins);
}
#endif
} // namespace spi
} // namespace esphome
#endif // USE_ESP32
} // namespace esphome::spi