From db6ca0a3b52362e84ca51e206ad055f583efc9c8 Mon Sep 17 00:00:00 2001 From: fabik111 Date: Fri, 30 May 2025 14:37:15 +0200 Subject: [PATCH] add esp32 chip manager on ESPHome for portenta C33 --- libraries/ESPhost/src/EspChipManager.cpp | 50 ++++++++++++++++++ libraries/ESPhost/src/EspChipManager.h | 66 ++++++++++++++++++++++++ libraries/ESPhost/src/EspSpiDriver.cpp | 34 ++---------- 3 files changed, 119 insertions(+), 31 deletions(-) create mode 100644 libraries/ESPhost/src/EspChipManager.cpp create mode 100644 libraries/ESPhost/src/EspChipManager.h diff --git a/libraries/ESPhost/src/EspChipManager.cpp b/libraries/ESPhost/src/EspChipManager.cpp new file mode 100644 index 00000000..1a771c3e --- /dev/null +++ b/libraries/ESPhost/src/EspChipManager.cpp @@ -0,0 +1,50 @@ +/* ########################################################################## */ +/* - File: EspChipManager.h + - Copyright (c): 2025 Arduino srl. + - Author: Fabio Massimo Centonze (f.centonze@arduino.cc) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc.,51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +/* ########################################################################## */ + +#include "EspChipManager.h" + +#define ESP_RESET BSP_IO_PORT_08_PIN_04 +#define ESP_DOWNLOAD DATA_READY // The DATA_READY pin is shared with the ESP_DOWNLOAD pin + +CEspChipManager& CEspChipManager::getInstance() { + static CEspChipManager instance; + return instance; +} + +void CEspChipManager::initialize() { + if (!isInitialized) { + forceReset(); + isInitialized = true; + } +} + +void CEspChipManager::forceReset() { + R_IOPORT_PinCfg(NULL, ESP_RESET, IOPORT_CFG_PORT_DIRECTION_OUTPUT); + // Set the ESP_DOWNLOAD pin to high to ensure the ESP doesn't enter in download mode when resetting + R_IOPORT_PinCfg(NULL, ESP_DOWNLOAD, (uint32_t) (IOPORT_CFG_PORT_DIRECTION_OUTPUT)); + R_IOPORT_PinWrite(NULL, ESP_DOWNLOAD, BSP_IO_LEVEL_HIGH); + delay(100); + R_IOPORT_PinWrite(NULL, ESP_RESET, BSP_IO_LEVEL_HIGH); + R_IOPORT_PinWrite(NULL, ESP_RESET, BSP_IO_LEVEL_LOW); + R_IOPORT_PinWrite(NULL, ESP_RESET, BSP_IO_LEVEL_HIGH); + R_IOPORT_PinWrite(NULL, ESP_DOWNLOAD, BSP_IO_LEVEL_LOW); + R_IOPORT_PinCfg(NULL, ESP_DOWNLOAD, (uint32_t) (IOPORT_CFG_IRQ_ENABLE | IOPORT_CFG_PORT_DIRECTION_INPUT)); +} + diff --git a/libraries/ESPhost/src/EspChipManager.h b/libraries/ESPhost/src/EspChipManager.h new file mode 100644 index 00000000..b4ab1d0d --- /dev/null +++ b/libraries/ESPhost/src/EspChipManager.h @@ -0,0 +1,66 @@ +/* ########################################################################## */ +/* - File: EspChipManager.h + - Copyright (c): 2025 Arduino srl. + - Author: Fabio Massimo Centonze (f.centonze@arduino.cc) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc.,51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +/* ########################################################################## */ + +#ifndef ESP_CHIP_MANAGER_H +#define ESP_CHIP_MANAGER_H +#include + +#ifdef USE_ESP32_C3_DEVKIT_RUST_1 +/* GPIOs */ +#define HANDSHAKE BSP_IO_PORT_05_PIN_05 +#define DATA_READY BSP_IO_PORT_08_PIN_02 +#define DATA_READY_PIN 33 + +/* SPI PIN definition */ +#define ESP_MISO BSP_IO_PORT_01_PIN_00 +#define ESP_MOSI BSP_IO_PORT_01_PIN_01 +#define ESP_CK BSP_IO_PORT_01_PIN_02 +#define ESP_CS BSP_IO_PORT_01_PIN_03 +#else +/* GPIOs */ +#define HANDSHAKE BSP_IO_PORT_08_PIN_06 +#define DATA_READY BSP_IO_PORT_08_PIN_03 +#define DATA_READY_PIN 100 + +/* SPI PIN definition */ +#define ESP_MISO BSP_IO_PORT_01_PIN_00 +#define ESP_MOSI BSP_IO_PORT_01_PIN_01 +#define ESP_CK BSP_IO_PORT_01_PIN_02 +#define ESP_CS BSP_IO_PORT_01_PIN_04 +#endif + + +class CEspChipManager { + public: + static CEspChipManager& getInstance(); + + // Delete copy constructor and assignment operator to enforce singleton + CEspChipManager(const CEspChipManager&) = delete; + CEspChipManager& operator=(const CEspChipManager&) = delete; + // General initialization + void initialize(); + // Force a reset of the ESP chip, don't use for initialization + void forceReset(); + private: + CEspChipManager() = default; + ~CEspChipManager() = default; + bool isInitialized = false; +}; +#endif // ESP_CHIP_MANAGER_H diff --git a/libraries/ESPhost/src/EspSpiDriver.cpp b/libraries/ESPhost/src/EspSpiDriver.cpp index 5644f6cf..0ea89fbd 100644 --- a/libraries/ESPhost/src/EspSpiDriver.cpp +++ b/libraries/ESPhost/src/EspSpiDriver.cpp @@ -29,6 +29,7 @@ * ######## */ #include "EspSpiDriver.h" +#include "EspChipManager.h" /* ##################### * Configuration defines @@ -45,31 +46,6 @@ #define EXT_IRQ_CHANNEL (0) -#ifdef USE_ESP32_C3_DEVKIT_RUST_1 -/* GPIOs */ -#define HANDSHAKE BSP_IO_PORT_05_PIN_05 -#define DATA_READY BSP_IO_PORT_08_PIN_02 -#define DATA_READY_PIN 33 - -/* SPI PIN definition */ -#define ESP_MISO BSP_IO_PORT_01_PIN_00 -#define ESP_MOSI BSP_IO_PORT_01_PIN_01 -#define ESP_CK BSP_IO_PORT_01_PIN_02 -#define ESP_CS BSP_IO_PORT_01_PIN_03 -#else -/* GPIOs */ -#define ESP_RESET BSP_IO_PORT_08_PIN_04 -#define HANDSHAKE BSP_IO_PORT_08_PIN_06 -#define DATA_READY BSP_IO_PORT_08_PIN_03 -#define DATA_READY_PIN 100 - -/* SPI PIN definition */ -#define ESP_MISO BSP_IO_PORT_01_PIN_00 -#define ESP_MOSI BSP_IO_PORT_01_PIN_01 -#define ESP_CK BSP_IO_PORT_01_PIN_02 -#define ESP_CS BSP_IO_PORT_01_PIN_04 -#endif - /* ################# * PRIVATE Variables * ################# */ @@ -157,15 +133,11 @@ int esp_host_spi_init(void) { } /* ++++++++++++++++++++++++++++++++++ - * GPIOs (HANDSHAKE and DATA_READY) + * GPIOs (HANDSHAKE and CS) * ++++++++++++++++++++++++++++++++++ */ R_IOPORT_PinCfg(NULL, HANDSHAKE, IOPORT_CFG_PORT_DIRECTION_INPUT); - /* DATA READY is configure in attach interrupt function below */ //#ifdef EXPLICIT_PIN_CONFIGURATION - R_IOPORT_PinCfg(NULL, DATA_READY, (uint32_t) (IOPORT_CFG_IRQ_ENABLE | IOPORT_CFG_PORT_DIRECTION_INPUT )); - R_IOPORT_PinCfg(NULL, ESP_CS, IOPORT_CFG_PORT_DIRECTION_OUTPUT); - R_IOPORT_PinCfg(NULL, ESP_RESET, IOPORT_CFG_PORT_DIRECTION_OUTPUT); //#endif /* +++++ @@ -270,7 +242,7 @@ int esp_host_spi_init(void) { return ESP_HOSTED_SPI_DRIVER_SPI_FAIL_OPEN; } - R_IOPORT_PinWrite(NULL, ESP_RESET, BSP_IO_LEVEL_HIGH); + CEspChipManager::getInstance().initialize(); spi_driver_initialized = true; return ESP_HOSTED_SPI_DRIVER_OK; }