diff --git a/libraries/ESPhost/src/EspChipManager.cpp b/libraries/ESPhost/src/EspChipManager.cpp new file mode 100644 index 00000000..db79d13a --- /dev/null +++ b/libraries/ESPhost/src/EspChipManager.cpp @@ -0,0 +1,41 @@ +/* ########################################################################## */ +/* - 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 DATA_READY_PIN 100 + +CEspChipManager& CEspChipManager::getInstance() { + static CEspChipManager instance; + return instance; +} + +void CEspChipManager::initialize() { + if (!isInitialized) { + R_IOPORT_PinCfg(NULL, digitalPinToBspPin(DATA_READY_PIN), (uint32_t) (IOPORT_CFG_IRQ_ENABLE | IOPORT_CFG_PORT_DIRECTION_INPUT | IOPORT_CFG_PULLUP_ENABLE)); + R_IOPORT_PinCfg(NULL, ESP_RESET, IOPORT_CFG_PORT_DIRECTION_OUTPUT); + 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); + isInitialized = true; + } +} + diff --git a/libraries/ESPhost/src/EspChipManager.h b/libraries/ESPhost/src/EspChipManager.h new file mode 100644 index 00000000..56e0bdb9 --- /dev/null +++ b/libraries/ESPhost/src/EspChipManager.h @@ -0,0 +1,39 @@ +/* ########################################################################## */ +/* - 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 + +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(); + 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..82cea4d1 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 @@ -58,7 +59,6 @@ #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 @@ -157,15 +157,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 +266,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; }