diff --git a/lib/lib_display/UDisplay/uDisplay.cpp b/lib/lib_display/UDisplay/uDisplay.cpp index 0bd51ae40..5b572177d 100755 --- a/lib/lib_display/UDisplay/uDisplay.cpp +++ b/lib/lib_display/UDisplay/uDisplay.cpp @@ -2021,14 +2021,14 @@ bool uDisplay::utouch_Init(char **name) { attachInterrupt(ut_irq, ut_touch_irq, FALLING); } -extern SPIClass *Init_SPI_Bus(uint32 bus); +extern SPIClass *SpiBegin(uint32 bus); if (ut_spi_nr == spi_nr) { // same as display ut_spi = uspi; } else { #ifdef ESP32 - ut_spi = Init_SPI_Bus(ut_spi_nr); + ut_spi = SpiBegin(ut_spi_nr); #endif } return ut_execute(ut_init_code); diff --git a/tasmota/tasmota_support/support.ino b/tasmota/tasmota_support/support.ino index ae2308c49..805bfeca4 100755 --- a/tasmota/tasmota_support/support.ino +++ b/tasmota/tasmota_support/support.ino @@ -1791,29 +1791,6 @@ bool ValidSpiPinUsed(uint32_t gpio) { return result; } -#ifdef ESP32 -#ifndef FIRMWARE_SAFEBOOT -SPIClass *Init_SPI_Bus(uint32 bus) { - SPIClass *spi; - if (1 == bus) { - if (TasmotaGlobal.spi_enabled) { - spi = &SPI; - spi->begin(Pin(GPIO_SPI_CLK, 0), Pin(GPIO_SPI_MISO, 0), Pin(GPIO_SPI_MOSI, 0), -1); - return spi; - } - } - if (2 == bus) { - if (TasmotaGlobal.spi_enabled2) { - spi = new SPIClass(HSPI); - spi->begin(Pin(GPIO_SPI_CLK, 1), Pin(GPIO_SPI_MISO, 1), Pin(GPIO_SPI_MOSI, 1), -1); - return spi; - } - } - return nullptr; -} -#endif // FIRMWARE_SAFEBOOT -#endif // ESP32 - bool JsonTemplate(char* dataBuf) { // Old: {"NAME":"Shelly 2.5","GPIO":[56,0,17,0,21,83,0,0,6,82,5,22,156],"FLAG":2,"BASE":18} @@ -2673,38 +2650,6 @@ void AddLogMissed(const char *sensor, uint32_t misses) AddLog(LOG_LEVEL_DEBUG, PSTR("SNS: %s missed %d"), sensor, SENSOR_MAX_MISS - misses); } -void AddLogSpi(uint32_t hardware, int clk, int mosi, int miso) { - uint32_t enabled = TasmotaGlobal.soft_spi_enabled; - char hwswbus[8]; - if (hardware) { -#ifdef ESP8266 - strcpy_P(hwswbus, PSTR("Hard")); - enabled = TasmotaGlobal.spi_enabled; -#endif -#ifdef ESP32 - strcpy_P(hwswbus, PSTR("Bus0")); - hwswbus[3] += (char)hardware; - enabled = (1 == hardware) ? TasmotaGlobal.spi_enabled : TasmotaGlobal.spi_enabled2; -#endif - } else { - strcpy_P(hwswbus, PSTR("Soft")); - } - switch(enabled) { - case SPI_MOSI: - AddLog(LOG_LEVEL_INFO, PSTR("SPI: %s using GPIO%02d(CLK) and GPIO%02d(MOSI)"), - hwswbus, clk, mosi); - break; - case SPI_MISO: - AddLog(LOG_LEVEL_INFO, PSTR("SPI: %s using GPIO%02d(CLK) and GPIO%02d(MISO)"), - hwswbus, clk, miso); - break; - case SPI_MOSI_MISO: - AddLog(LOG_LEVEL_INFO, PSTR("SPI: %s using GPIO%02d(CLK), GPIO%02d(MOSI) and GPIO%02d(MISO)"), - hwswbus, clk, mosi, miso); - break; - } -} - /*********************************************************************************************\ * HTML and URL encode \*********************************************************************************************/ diff --git a/tasmota/tasmota_support/support_a_spi.ino b/tasmota/tasmota_support/support_a_spi.ino new file mode 100644 index 000000000..fe349ad33 --- /dev/null +++ b/tasmota/tasmota_support/support_a_spi.ino @@ -0,0 +1,82 @@ +/* + support_a_spi.ino - SPI support for Tasmota + + SPDX-FileCopyrightText: 2024 Theo Arends + + SPDX-License-Identifier: GPL-3.0-only +*/ + +#ifdef USE_SPI +/*********************************************************************************************\ + * Basic SPI routines supporting two busses +\*********************************************************************************************/ + +SPIClass *SpiBegin(uint32 bus = 1); +SPIClass *SpiBegin(uint32 bus) { + SPIClass *spi; + if (1 == bus) { // SPI bus 1 + if (TasmotaGlobal.spi_enabled) { + spi = &SPI; // This uses HSPI by default +#ifdef ESP8266 + spi->begin(); +#endif // ESP8266 +#ifdef ESP32 + spi->begin(Pin(GPIO_SPI_CLK), Pin(GPIO_SPI_MISO), Pin(GPIO_SPI_MOSI), -1); +#endif // ESP32 + return spi; + } + } +#ifdef ESP32 + if (2 == bus) { // SPI bus 2 + if (TasmotaGlobal.spi_enabled2) { + // See framework-arduinoespressif32 esp32-hal-spi.h +#if CONFIG_IDF_TARGET_ESP32S2 + spi = new SPIClass(SPI3); +#elif CONFIG_IDF_TARGET_ESP32 + spi = new SPIClass(VSPI); +#else + spi = new SPIClass(HSPI); // Same as SPI bus 1 ??? +#endif + spi->begin(Pin(GPIO_SPI_CLK, 1), Pin(GPIO_SPI_MISO, 1), Pin(GPIO_SPI_MOSI, 1), -1); + return spi; + } + } +#endif // ESP32 + return nullptr; +} + +#endif // USE_SPI + +/********************************************************************************************/ + +void AddLogSpi(uint32_t hardware, int clk, int mosi, int miso) { + uint32_t enabled = TasmotaGlobal.soft_spi_enabled; + char hwswbus[8]; + if (hardware) { +#ifdef ESP8266 + strcpy_P(hwswbus, PSTR("Hard")); + enabled = TasmotaGlobal.spi_enabled; +#endif +#ifdef ESP32 + strcpy_P(hwswbus, PSTR("Bus0")); + hwswbus[3] += (char)hardware; + enabled = (1 == hardware) ? TasmotaGlobal.spi_enabled : TasmotaGlobal.spi_enabled2; +#endif + } else { + strcpy_P(hwswbus, PSTR("Soft")); + } + switch(enabled) { + case SPI_MOSI: + AddLog(LOG_LEVEL_INFO, PSTR("SPI: %s using GPIO%02d(CLK) and GPIO%02d(MOSI)"), + hwswbus, clk, mosi); + break; + case SPI_MISO: + AddLog(LOG_LEVEL_INFO, PSTR("SPI: %s using GPIO%02d(CLK) and GPIO%02d(MISO)"), + hwswbus, clk, miso); + break; + case SPI_MOSI_MISO: + AddLog(LOG_LEVEL_INFO, PSTR("SPI: %s using GPIO%02d(CLK), GPIO%02d(MOSI) and GPIO%02d(MISO)"), + hwswbus, clk, mosi, miso); + break; + } +}