diff --git a/src/NerdMinerV2.ino.cpp b/src/NerdMinerV2.ino.cpp index 9904d15..95a4359 100644 --- a/src/NerdMinerV2.ino.cpp +++ b/src/NerdMinerV2.ino.cpp @@ -11,6 +11,7 @@ #include "mining.h" #include "monitor.h" #include "drivers/displays/display.h" +#include "drivers/storage/SDCard.h" //3 seconds WDT #define WDT_TIMEOUT 3 @@ -25,9 +26,10 @@ OneButton button2(PIN_BUTTON_2); #endif - extern monitor_data mMonitor; +SDCard SDCrd = SDCard(); + /**********************⚡ GLOBAL Vars *******************************/ unsigned long start = millis(); diff --git a/src/drivers/devices/esp32CAM.h b/src/drivers/devices/esp32CAM.h index 39d4391..2949491 100644 --- a/src/drivers/devices/esp32CAM.h +++ b/src/drivers/devices/esp32CAM.h @@ -27,8 +27,8 @@ // (default SPI unit provided by ) // setup SPI pins. #define SDSPI_CS 13 -// The following pins will be ignored, if a tft display is set up. (!defined NO_DISPLAY) -// check display settings to find the appropriate lines. +// The following pins can be retreived from the TFT_eSPI lib, +// if a display that is using it is activated. #define SDSPI_CLK 14 #define SDSPI_MOSI 15 #define SDSPI_MISO 2 diff --git a/src/drivers/storage/SDCard.cpp b/src/drivers/storage/SDCard.cpp index 48a5c73..89eb984 100644 --- a/src/drivers/storage/SDCard.cpp +++ b/src/drivers/storage/SDCard.cpp @@ -1,4 +1,3 @@ - #include #include #include @@ -7,6 +6,7 @@ #include "nvMemory.h" #include "..\devices\device.h" #include "SDCard.h" +//#include "..\lib\TFT_eSPI\User_Setup_Select.h" #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) #include @@ -17,19 +17,43 @@ #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) || defined (BUILD_SDSPI) -SDCard::SDCard() +/// @param int ID only relevant in SPI mode, if you want to use a non standard SPI Unit. +SDCard::SDCard(int ID):cardInitialized_(false),cardBusy_(false) { #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) iSD_ = &SD_MMC; #elif defined (BUILD_SDSPI) - ispi_ = &SPI; + if(ID>=0) + { + ispi_ = new SPIClass(ID); + newInstance_ = true; + } + else + { + ispi_ = &SPI; + newInstance_ = false; + } iSD_ = &SD; #endif // interface type +initSDcard(); } SDCard::~SDCard() { - unmount(); + iSD_->end(); + if(newInstance_) + { + ispi_->end(); + delete ispi_; + } + Serial.println("SDCard: Unmounted"); +} + +/// @brief Check if the card is accessed right now. +/// @return true if active +bool SDCard::cardBusy() +{ + return !cardBusy_; } /// @brief Transfer settings from config file on a SD card to the device. @@ -54,7 +78,7 @@ bool SDCard::loadConfigFile(TSettings* Settings) // Load existing configuration file // Read configuration from FS json - if (initSDcard()) + if (cardAvailable()) { if (iSD_->exists(JSON_CONFIG_FILE)) { @@ -62,15 +86,16 @@ bool SDCard::loadConfigFile(TSettings* Settings) File configFile = iSD_->open(JSON_CONFIG_FILE, "r"); if (configFile) { + cardBusy_ = true; StaticJsonDocument<512> json; DeserializationError error = deserializeJson(json, configFile); configFile.close(); + cardBusy_ = false; Serial.println("SDCard: Loading config file"); - serializeJsonPretty(json, Serial); - Serial.print('\n'); - unmount(); if (!error) { + serializeJsonPretty(json, Serial); + Serial.print('\n'); Settings->WifiSSID = json[JSON_KEY_SSID] | Settings->WifiSSID; Settings->WifiPW = json[JSON_KEY_PASW] | Settings->WifiPW; Settings->PoolAddress = json[JSON_KEY_POOLURL] | Settings->PoolAddress; @@ -98,82 +123,71 @@ bool SDCard::loadConfigFile(TSettings* Settings) { Serial.println("SDCard: No config file available!"); } - unmount(); } return false; } -void SDCard::unmount() +/// @brief Check if a SD card is inserted. +/// @return true if inserted. +bool SDCard::cardAvailable() { - iSD_->end(); -#ifdef BUILD_SDSPI_SETUP - ispi_->end(); -#endif // BUILD_SDSPI_SETUP - Serial.println("SDCard: Unmounted"); -} - -bool SDCard::initSDcard() -{ - if (iSD_->cardType() != CARD_NONE) - { - Serial.println("SDCard: Already mounted."); - return true; - } - Serial.println("SDCard: Mounting card."); - - bool cardInitialized = false; -#if defined (BUILD_SDMMC_4) - if (iSD_->cardType() == CARD_NONE) - { - iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0, SDMMC_D1, SDMMC_D2, SDMMC_D3); - cardInitialized = iSD_->begin("/sd", false); - Serial.println("SDCard: 4-Bit Mode."); - } -#elif defined (BUILD_SDMMC_1) - #warning SDMMC : 1 - bit mode is not always working. If you experience issues, try other modes. - if (iSD_->cardType() == CARD_NONE) - { - iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0); - cardInitialized = iSD_->begin("/sd", true); - Serial.println("SDCard: 1-Bit Mode."); - } -#elif defined (BUILD_SDSPI) - if (iSD_->cardType() == CARD_NONE) - { - #ifdef BUILD_SDSPI_SETUP - ispi_->end(); - ispi_->begin(SDSPI_CLK, SDSPI_MISO, SDSPI_MOSI, SDSPI_CS); - #endif //BUILD_SDSPI_SETUP - cardInitialized = iSD_->begin(SDSPI_CS, *ispi_); - Serial.println("SDCard: SPI mode."); - } -#else - Serial.println("SDCard: interface not available."); - return false; -#endif // dataPinsDefined - if (cardInitialized) + if (cardInitialized_) { if (iSD_->cardType() != CARD_NONE) { - Serial.println("SDCard: Mounted."); + Serial.println("SDCard: Inserted."); return true; } else { - Serial.println("SDCard: Mounting failed."); - iSD_->end(); + Serial.println("SDCard: Not inserted."); } } + else + { + Serial.println("SDCard: Interface not initialized."); + } return false; } +/// @brief Init SD card interface. Normaly not required, as this is called by the constructor. +/// @return true on success +bool SDCard::initSDcard() +{ + if (!cardAvailable()) + { + Serial.println("SDCard: init SD card interface."); +#if defined (BUILD_SDMMC_4) + iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0, SDMMC_D1, SDMMC_D2, SDMMC_D3); + cardInitialized_ = iSD_->begin("/sd", false); + Serial.println("SDCard: 4-Bit Mode."); + } +#elif defined (BUILD_SDMMC_1) +#warning SDMMC : 1 - bit mode is not always working. If you experience issues, try other modes. + iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0); + cardInitialized_ = iSD_->begin("/sd", true); + Serial.println("SDCard: 1-Bit Mode."); + } +#elif defined (BUILD_SDSPI) + ispi_->begin(SDSPI_CLK, SDSPI_MISO, SDSPI_MOSI, SDSPI_CS); + cardInitialized_ = iSD_->begin(SDSPI_CS, *ispi_); + Serial.println("SDCard: SPI mode."); + } +#else + Serial.println("SDCard: interface not defined."); + return false; +#endif // dataPinsDefined + cardAvailable(); + return cardInitialized_; +} + #else -SDCard::SDCard() {} +SDCard::SDCard(int ID) {} SDCard::~SDCard() {} void SDCard::SD2nvMemory(nvMemory* nvMem, TSettings* Settings) {}; bool SDCard::loadConfigFile(TSettings* Settings) { return false; } bool SDCard::initSDcard() { return false; } -void unmount() {} +bool SDCard::cardAvailable() { return false; } #endif //BUILD_SDMMC \ No newline at end of file diff --git a/src/drivers/storage/SDCard.h b/src/drivers/storage/SDCard.h index 37d0bd4..775adbb 100644 --- a/src/drivers/storage/SDCard.h +++ b/src/drivers/storage/SDCard.h @@ -9,46 +9,62 @@ // select interface and options according to provided pins #if defined (SDMMC_D0) && defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3) -#define BUILD_SDMMC_4 -#undef BUILD_SDMMC_1 -#undef BUILD_SDSPI -#include -#warning SD card support in 4-Bit mode enabled! + #define BUILD_SDMMC_4 + #undef BUILD_SDMMC_1 + #undef BUILD_SDSPI + #include + #warning SD card support in 4-Bit mode enabled! #elif defined (SDMMC_D0) && !(defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3)) -#define BUILD_SDMMC_1 -#undef BUILD_SDMMC_4 -#undef BUILD_SDSPI -#include -#warning SD card support in 1-Bit mode enabled! + #define BUILD_SDMMC_1 + #undef BUILD_SDMMC_4 + #undef BUILD_SDSPI + #include + #warning SD card support in 1-Bit mode enabled! #elif defined (SDSPI_CS) -#undef BUILD_SDMMC_1 -#undef BUILD_SDMMC_4 -#define BUILD_SDSPI -#if defined (SDSPI_CLK) && defined (SDSPI_MOSI) && defined (SDSPI_MISO) && defined (NO_DISPLAY) -#define BUILD_SDSPI_SETUP -#endif // SPIPINS -#include -#include -#warning SD card support in SPI mode enabled! + #undef BUILD_SDMMC_1 + #undef BUILD_SDMMC_4 + #define BUILD_SDSPI + + #include + #include + #include "..\lib\TFT_eSPI\User_Setup_Select.h" + + #ifndef NO_DISPLAY + #if !defined(SDSPI_CLK) && defined(TFT_CLK) + #define SDSPI_CLK TFT_CLK + #endif // SDSPI_CLK + #if !defined(SDSPI_MOSI) && defined(TFT_MOSI) + #define SDSPI_MOSI TFT_MOSI + #endif // SDSPI_MOSI + #if !defined(SDSPI_MISO) && defined(TFT_MISO) + #define SDSPI_MISO TFT_MISO + #endif // SDSPI_MISO + #elif !defined(SDSPI_CLK) || !defined(SDSPI_MOSI) || !defined(SDSPI_MISO) + #error: Please define SDSPI pins! + #endif // NO_DISPLAY + + #warning SD card support in SPI mode enabled! #endif -// Handles the transfer of settings from sd card to nv memory (wifi credentials are handled by wifimanager) class SDCard { public: - SDCard(); + SDCard(int ID=-1); ~SDCard(); void SD2nvMemory(nvMemory* nvMem, TSettings* Settings); bool loadConfigFile(TSettings* Settings); + bool cardAvailable(); + bool cardBusy(); private: bool initSDcard(); - void unmount(); - + bool cardInitialized_; + bool cardBusy_; #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) fs::SDMMCFS* iSD_; #elif defined (BUILD_SDSPI) SPIClass* ispi_; fs::SDFS* iSD_; + bool newInstance_; #endif }; diff --git a/src/wManager.cpp b/src/wManager.cpp index bd89146..41ebff3 100644 --- a/src/wManager.cpp +++ b/src/wManager.cpp @@ -27,6 +27,8 @@ extern monitor_data mMonitor; nvMemory nvMem; +extern SDCard SDCrd; + void saveConfigCallback() // Callback notifying us of the need to save configuration { @@ -87,7 +89,6 @@ void init_WifiManager() if (!nvMem.loadConfig(&Settings)) { //No config file on internal flash. - SDCard SDCrd; if (SDCrd.loadConfigFile(&Settings)) { //Config file on SD card.