SD card reading and writing of Arduino+ESP32

Background knowledge:

ESP32 There are two ways to use SD card. One is to use SPI interface to access SD card, and the other is to use SDMMC interface to access SD card.

In Arduino core for the ESP32, SPI mode occupies 4 IO ports and SDMMC mode occupies 6 IO ports. Generally speaking, SDMMC mode is faster than SPI mode.

 

1. SDMMC mode

Although ESP32 has two groups of SDMMC interfaces, only one group is used in Arduino core for the ESP32. The IO port connections are: DAT2 - IO12, DAT3 - IO13, CMD - IO15, CLK - IO14, DAT0 - IO2, DAT1 - IO4;

bool begin(const char * mountpoint="/sdcard", bool mode1bit=false)
Mount the memory card, and the input parameters are the mount point and whether to use the first-line mode;

void end()
Cancel mounting;

sdcard_type_t cardType() returns the memory card type, 0, 1, 2, 3, 4, as follows:
CARD_NONE , no memory card is connected;
CARD_MMC} mmc card;
CARD_SD card, 2G maximum;
CARD_SDHC # sdhc card, 32G maximum;
CARD_UNKNOWN = unknown memory card;

uint64_t cardSize() returns the number of bytes of memory card size;
uint64_t totalBytes() returns the total number of bytes in the file system;
uint64_t usedBytes() returns the number of bytes used by the file system;

 

2. SPI mode

bool begin(uint8_t ssPin=SS, SPIClass &spi=SPI, uint32_t frequency=4000000, const char * mountpoint="/sd", uint8_t max_files=5)
Mount the memory card, and the input parameters are SS pin number, SPI object, clock frequency, mount point and the maximum number of files opened at the same time;
The default IO port connections are: CS - IO5, DI - IO23, SCLK - IO18, DO - IO19;
void end()
Cancel mounting;
sdcard_type_t cardType() returns the memory card type, 0, 1, 2, 3, 4, as follows:
CARD_NONE , no memory card is connected;
CARD_MMC} mmc card;
CARD_SD card, 2G maximum;
CARD_SDHC # sdhc card, 32G maximum;
CARD_UNKNOWN = unknown memory card;
uint64_t cardSize() returns the number of bytes of memory card size;
uint64_t totalBytes() returns the total number of bytes in the file system;
uint64_t usedBytes(); Returns the number of bytes used by the file system

 

3. My experiment

My esp32_ Schematic diagram of cam board

As can be seen from the figure on the right, my SD card is connected to the six pins of GPIO12 13 14 15 2 4 of ESP32. According to the above, this conforms to the IO connection of SDMMC mode. Therefore, I need to use SDMMC to drive SD card in software.

 

Create a new arduino project file, copy and paste the following code, that is, you can drive the SD card in SDMMC mode

//Reference related Library
//#include "FS.h"
#include "SD_MMC.h"

// The interface connections are as follows:
// SD card - ESP32
// ------------
// DAT2 - IO12
// DAT3 - IO13
// CMD  - IO15
// CLK  - IO14
// DAT0 - IO2
// DAT1 - IO4

void setup()
{
  Serial.begin(115200);
  Serial.println();

  //Mount file system
  if (!SD_MMC.begin())
  {
    Serial.println("Memory card mount failed");
    return;
  }
  uint8_t cardType = SD_MMC.cardType();

  if (cardType == CARD_NONE)
  {
    Serial.println("No memory card connected");
    return;
  }
  else if (cardType == CARD_MMC)
  {
    Serial.println("Mounted MMC card");
  }
  else if (cardType == CARD_SD)
  {
    Serial.println("Mounted SDSC card");
  }
  else if (cardType == CARD_SDHC)
  {
    Serial.println("Mounted SDHC card");
  }
  else
  {
    Serial.println("An unknown memory card is mounted");
  }

  //open/Create and write data
  File file = SD_MMC.open("/test.txt", FILE_WRITE);
  if (file)
  {
    Serial.println("open/Create root directory test.txt File!");
  }

  char data[] = "hello world\r\n";
  file.write((uint8_t *)data, strlen(data));
  file.close();

  //rename file
  if (SD_MMC.rename("/test.txt", "/retest.txt"))
  {
    Serial.println("test.txt Rename to retest.txt !");
  }

  //Read file data
  file = SD_MMC.open("/retest.txt", FILE_READ);
  if (file)
  {
    Serial.print("The contents of the document are:");
    while (file.available())
    {
      Serial.print((char)file.read());
    }
  }

  //Print memory card information
  Serial.printf("The total size of the memory card is: %lluMB \n", SD_MMC.cardSize() / (1024 * 1024)); // "/ (1024 * 1024)"You can change it to">> 20"
  Serial.printf("The total file system size is: %lluB \n", SD_MMC.totalBytes());
  Serial.printf("The file system used size is: %lluB \n", SD_MMC.usedBytes());
}

void loop()
{
}

 

4. Experimental effect

 

5. In addition, if you do not pursue the speed of reading and writing SD card, you can read and write SD card in SPI mode, so you can save two valuable gpios of ESP32 for other purposes.

In fact, after we have installed the ESP32 support package of Arduino, we already have these routines locally. Refer to the following path to find these routines.

 

 

 

.

Keywords: arduino ESP32

Added by susannah on Thu, 03 Feb 2022 01:37:32 +0200