Add an option to force SPI into software mode, useful when (#4556)

reusing pins for different purposes.
This commit is contained in:
Witold Krecicki 2023-03-15 23:21:25 +01:00 committed by GitHub
parent 3d4c0e6667
commit a8bb2a42a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 1 deletions

View file

@ -17,6 +17,7 @@ spi_ns = cg.esphome_ns.namespace("spi")
SPIComponent = spi_ns.class_("SPIComponent", cg.Component) SPIComponent = spi_ns.class_("SPIComponent", cg.Component)
SPIDevice = spi_ns.class_("SPIDevice") SPIDevice = spi_ns.class_("SPIDevice")
MULTI_CONF = True MULTI_CONF = True
CONF_FORCE_SW = "force_sw"
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
cv.Schema( cv.Schema(
@ -25,6 +26,7 @@ CONFIG_SCHEMA = cv.All(
cv.Required(CONF_CLK_PIN): pins.gpio_output_pin_schema, cv.Required(CONF_CLK_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_MISO_PIN): pins.gpio_input_pin_schema, cv.Optional(CONF_MISO_PIN): pins.gpio_input_pin_schema,
cv.Optional(CONF_MOSI_PIN): pins.gpio_output_pin_schema, cv.Optional(CONF_MOSI_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_FORCE_SW, default=False): cv.boolean,
} }
), ),
cv.has_at_least_one_key(CONF_MISO_PIN, CONF_MOSI_PIN), cv.has_at_least_one_key(CONF_MISO_PIN, CONF_MOSI_PIN),
@ -39,6 +41,7 @@ async def to_code(config):
clk = await cg.gpio_pin_expression(config[CONF_CLK_PIN]) clk = await cg.gpio_pin_expression(config[CONF_CLK_PIN])
cg.add(var.set_clk(clk)) cg.add(var.set_clk(clk))
cg.add(var.set_force_sw(config[CONF_FORCE_SW]))
if CONF_MISO_PIN in config: if CONF_MISO_PIN in config:
miso = await cg.gpio_pin_expression(config[CONF_MISO_PIN]) miso = await cg.gpio_pin_expression(config[CONF_MISO_PIN])
cg.add(var.set_miso(miso)) cg.add(var.set_miso(miso))

View file

@ -25,7 +25,7 @@ void SPIComponent::setup() {
this->clk_->digital_write(true); this->clk_->digital_write(true);
#ifdef USE_SPI_ARDUINO_BACKEND #ifdef USE_SPI_ARDUINO_BACKEND
bool use_hw_spi = true; bool use_hw_spi = !this->force_sw_;
const bool has_miso = this->miso_ != nullptr; const bool has_miso = this->miso_ != nullptr;
const bool has_mosi = this->mosi_ != nullptr; const bool has_mosi = this->mosi_ != nullptr;
int8_t clk_pin = -1, miso_pin = -1, mosi_pin = -1; int8_t clk_pin = -1, miso_pin = -1, mosi_pin = -1;

View file

@ -74,6 +74,7 @@ class SPIComponent : public Component {
void set_clk(GPIOPin *clk) { clk_ = clk; } void set_clk(GPIOPin *clk) { clk_ = clk; }
void set_miso(GPIOPin *miso) { miso_ = miso; } void set_miso(GPIOPin *miso) { miso_ = miso; }
void set_mosi(GPIOPin *mosi) { mosi_ = mosi; } void set_mosi(GPIOPin *mosi) { mosi_ = mosi; }
void set_force_sw(bool force_sw) { force_sw_ = force_sw; }
void setup() override; void setup() override;
@ -260,6 +261,7 @@ class SPIComponent : public Component {
GPIOPin *miso_{nullptr}; GPIOPin *miso_{nullptr};
GPIOPin *mosi_{nullptr}; GPIOPin *mosi_{nullptr};
GPIOPin *active_cs_{nullptr}; GPIOPin *active_cs_{nullptr};
bool force_sw_{false};
#ifdef USE_SPI_ARDUINO_BACKEND #ifdef USE_SPI_ARDUINO_BACKEND
SPIClass *hw_spi_{nullptr}; SPIClass *hw_spi_{nullptr};
#endif // USE_SPI_ARDUINO_BACKEND #endif // USE_SPI_ARDUINO_BACKEND