From 7c5ec51abbd32c1d3ee56f1ddcf2342f8fedc03e Mon Sep 17 00:00:00 2001 From: elmo128 <60213508+elmo128@users.noreply.github.com> Date: Thu, 21 Sep 2023 17:19:53 +0200 Subject: [PATCH] add spi access to sd card --- src/drivers/devices/esp32CAM.h | 20 ++++++++++++++++- src/drivers/storage/SDCard.cpp | 41 ++++++++++++++++++++++++---------- src/drivers/storage/SDCard.h | 23 ++++++++++++++++--- 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/src/drivers/devices/esp32CAM.h b/src/drivers/devices/esp32CAM.h index 54d7460..f58bd6d 100644 --- a/src/drivers/devices/esp32CAM.h +++ b/src/drivers/devices/esp32CAM.h @@ -6,7 +6,14 @@ #define NO_DISPLAY -// SDMMC interface: 1-bit mode (might cause issues): +// example how to configure SD card. +// if you would define everything, +// to select 1 bit mode, make sure SDMMC_D1-3 are undefined +// to use spi mode, make sure all SDMMC_x pins are undefined + +/* +// use SDMMC interface: +// 1-bit mode (might cause issues): #define SDMMC_CLK 14 #define SDMMC_CMD 15 #define SDMMC_D0 2 @@ -14,5 +21,16 @@ #define SDMMC_D1 4 #define SDMMC_D2 12 #define SDMMC_D3 13 +*/ + +// use SPI interface +// (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. +#define SDSPI_CLK 14 +#define SDSPI_MOSI 15 +#define SDSPI_MISO 2 #endif // _ESP32_CAM_H_ diff --git a/src/drivers/storage/SDCard.cpp b/src/drivers/storage/SDCard.cpp index d4bd273..e852bf0 100644 --- a/src/drivers/storage/SDCard.cpp +++ b/src/drivers/storage/SDCard.cpp @@ -9,16 +9,22 @@ #include "SDCard.h" #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) - #include +#elif defined (BUILD_SDSPI) +#include +#include +#endif // interface type + +#if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) || defined (BUILD_SDSPI) SDCard::SDCard() { #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) iSD_ = &SD_MMC; #elif defined (BUILD_SDSPI) -#error You chose to run the sd card in SPI mode. This is not implemented yet. -#endif + ispi_ = &SPI; + iSD_ = &SD; +#endif // interface type } SDCard::~SDCard() @@ -94,6 +100,9 @@ bool SDCard::loadConfigFile(TSettings* Settings) void SDCard::unmount() { iSD_->end(); +#ifdef BUILD_SDSPI_SETUP + ispi_->end(); +#endif // BUILD_SDSPI_SETUP Serial.println("SDCard: Unmounted"); } @@ -111,19 +120,27 @@ bool SDCard::initSDcard() if (iSD_->cardType() == CARD_NONE) { iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0, SDMMC_D1, SDMMC_D2, SDMMC_D3); - Serial.println("SDCard: 4-Bit Mode."); 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); - Serial.println("SDCard: 1-Bit Mode."); - cardInitialized = iSD_->begin("/sd", true); - } + #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) -#error You chose to run the sd card in SPI mode. This is not implemented yet. + 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; diff --git a/src/drivers/storage/SDCard.h b/src/drivers/storage/SDCard.h index d1da16f..37d0bd4 100644 --- a/src/drivers/storage/SDCard.h +++ b/src/drivers/storage/SDCard.h @@ -5,14 +5,31 @@ #include "nvMemory.h" #include "..\devices\device.h" +// configuration example and description in /devices/esp32cam.h + +// 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! #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 -#else -#warning SD card support disabled! +#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! #endif // Handles the transfer of settings from sd card to nv memory (wifi credentials are handled by wifimanager) @@ -30,7 +47,7 @@ private: #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) fs::SDMMCFS* iSD_; #elif defined (BUILD_SDSPI) -#error You chose to run the SD card in SPI mode. This is not implemented yet. + SPIClass* ispi_; fs::SDFS* iSD_; #endif };