add spi access to sd card

This commit is contained in:
elmo128 2023-09-21 17:19:53 +02:00
parent 71d539bc5c
commit 7c5ec51abb
3 changed files with 68 additions and 16 deletions

View File

@ -6,7 +6,14 @@
#define NO_DISPLAY #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_CLK 14
#define SDMMC_CMD 15 #define SDMMC_CMD 15
#define SDMMC_D0 2 #define SDMMC_D0 2
@ -14,5 +21,16 @@
#define SDMMC_D1 4 #define SDMMC_D1 4
#define SDMMC_D2 12 #define SDMMC_D2 12
#define SDMMC_D3 13 #define SDMMC_D3 13
*/
// use SPI interface
// (default SPI unit provided by <SPI.h>)
// 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_ #endif // _ESP32_CAM_H_

View File

@ -9,16 +9,22 @@
#include "SDCard.h" #include "SDCard.h"
#if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4)
#include <SD_MMC.h> #include <SD_MMC.h>
#elif defined (BUILD_SDSPI)
#include <SD.h>
#include <SPI.h>
#endif // interface type
#if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) || defined (BUILD_SDSPI)
SDCard::SDCard() SDCard::SDCard()
{ {
#if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4)
iSD_ = &SD_MMC; iSD_ = &SD_MMC;
#elif defined (BUILD_SDSPI) #elif defined (BUILD_SDSPI)
#error You chose to run the sd card in SPI mode. This is not implemented yet. ispi_ = &SPI;
#endif iSD_ = &SD;
#endif // interface type
} }
SDCard::~SDCard() SDCard::~SDCard()
@ -94,6 +100,9 @@ bool SDCard::loadConfigFile(TSettings* Settings)
void SDCard::unmount() void SDCard::unmount()
{ {
iSD_->end(); iSD_->end();
#ifdef BUILD_SDSPI_SETUP
ispi_->end();
#endif // BUILD_SDSPI_SETUP
Serial.println("SDCard: Unmounted"); Serial.println("SDCard: Unmounted");
} }
@ -111,19 +120,27 @@ bool SDCard::initSDcard()
if (iSD_->cardType() == CARD_NONE) if (iSD_->cardType() == CARD_NONE)
{ {
iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0, SDMMC_D1, SDMMC_D2, SDMMC_D3); 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); cardInitialized = iSD_->begin("/sd", false);
Serial.println("SDCard: 4-Bit Mode.");
} }
#elif defined (BUILD_SDMMC_1) #elif defined (BUILD_SDMMC_1)
#warning SDMMC : 1 - bit mode is not always working. If you experience issues, try other modes. #warning SDMMC : 1 - bit mode is not always working. If you experience issues, try other modes.
if (iSD_->cardType() == CARD_NONE) if (iSD_->cardType() == CARD_NONE)
{ {
iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0); iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0);
Serial.println("SDCard: 1-Bit Mode.");
cardInitialized = iSD_->begin("/sd", true); cardInitialized = iSD_->begin("/sd", true);
Serial.println("SDCard: 1-Bit Mode.");
} }
#elif defined (BUILD_SDSPI) #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 #else
Serial.println("SDCard: interface not available."); Serial.println("SDCard: interface not available.");
return false; return false;

View File

@ -5,14 +5,31 @@
#include "nvMemory.h" #include "nvMemory.h"
#include "..\devices\device.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) #if defined (SDMMC_D0) && defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3)
#define BUILD_SDMMC_4 #define BUILD_SDMMC_4
#undef BUILD_SDMMC_1
#undef BUILD_SDSPI
#include <SD_MMC.h> #include <SD_MMC.h>
#warning SD card support in 4-Bit mode enabled!
#elif defined (SDMMC_D0) && !(defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3)) #elif defined (SDMMC_D0) && !(defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3))
#define BUILD_SDMMC_1 #define BUILD_SDMMC_1
#undef BUILD_SDMMC_4
#undef BUILD_SDSPI
#include <SD_MMC.h> #include <SD_MMC.h>
#else #warning SD card support in 1-Bit mode enabled!
#warning SD card support disabled! #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 <SPI.h>
#include <SD.h>
#warning SD card support in SPI mode enabled!
#endif #endif
// Handles the transfer of settings from sd card to nv memory (wifi credentials are handled by wifimanager) // 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) #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4)
fs::SDMMCFS* iSD_; fs::SDMMCFS* iSD_;
#elif defined (BUILD_SDSPI) #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_; fs::SDFS* iSD_;
#endif #endif
}; };