diff --git a/libraries/ESPhost/src/EspChipManager.cpp b/libraries/ESPhost/src/EspChipManager.cpp new file mode 100644 index 00000000..fda6065c --- /dev/null +++ b/libraries/ESPhost/src/EspChipManager.cpp @@ -0,0 +1,46 @@ +/* ########################################################################## */ +/* - 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_BLE_PIN_EN 100 + +CEspChipManager& CEspChipManager::getInstance() { + static CEspChipManager instance; + return instance; +} + +void CEspChipManager::initialize() { + if (!isInitialized) { + R_IOPORT_PinCfg(NULL, ESP_RESET, IOPORT_CFG_PORT_DIRECTION_OUTPUT); + R_IOPORT_PinWrite(NULL, ESP_RESET, BSP_IO_LEVEL_HIGH); + + isInitialized = true; + } +} + +void CEspChipManager::initializeBLE() { + if (!isBLEInitialized) { + R_IOPORT_PinCfg(NULL, digitalPinToBspPin(ESP_BLE_PIN_EN), IOPORT_CFG_PORT_DIRECTION_OUTPUT); + R_IOPORT_PinWrite(NULL, digitalPinToBspPin(ESP_BLE_PIN_EN), BSP_IO_LEVEL_HIGH); + isBLEInitialized = true; + } +} diff --git a/libraries/ESPhost/src/EspChipManager.h b/libraries/ESPhost/src/EspChipManager.h new file mode 100644 index 00000000..fa563d2b --- /dev/null +++ b/libraries/ESPhost/src/EspChipManager.h @@ -0,0 +1,42 @@ +/* ########################################################################## */ +/* - 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(); + // Initialize the BLE + void initializeBLE(); + private: + CEspChipManager() = default; + ~CEspChipManager() = default; + bool isInitialized = false; + bool isBLEInitialized = false; +}; +#endif // ESP_CHIP_MANAGER_H diff --git a/libraries/ESPhost/src/EspSpiDriver.cpp b/libraries/ESPhost/src/EspSpiDriver.cpp index 5644f6cf..ec35a165 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 @@ -165,7 +165,6 @@ int esp_host_spi_init(void) { 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 +269,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; }