Skip to content

Commit 0e5e823

Browse files
committed
Updated SD_MMC lib and examples
1 parent f8c5b98 commit 0e5e823

File tree

6 files changed

+322
-52
lines changed

6 files changed

+322
-52
lines changed

libraries/SD/examples/SD_Test/SD_Test.ino

+52-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,56 @@
11
/*
2-
* Connect the SD card to the following pins:
2+
* pin 1 - not used | Micro SD card |
3+
* pin 2 - CS (SS) | /
4+
* pin 3 - DI (MOSI) | |__
5+
* pin 4 - VDD (3.3V) | |
6+
* pin 5 - SCK (SCLK) | 8 7 6 5 4 3 2 1 /
7+
* pin 6 - VSS (GND) | ▄ ▄ ▄ ▄ ▄ ▄ ▄ ▄ /
8+
* pin 7 - DO (MISO) | ▀ ▀ █ ▀ █ ▀ ▀ ▀ |
9+
* pin 8 - not used |_________________|
10+
* ║ ║ ║ ║ ║ ║ ║ ║
11+
* ╔═══════╝ ║ ║ ║ ║ ║ ║ ╚═════════╗
12+
* ║ ║ ║ ║ ║ ║ ╚══════╗ ║
13+
* ║ ╔═════╝ ║ ║ ║ ╚═════╗ ║ ║
14+
* Connections for ║ ║ ╔═══╩═║═║═══╗ ║ ║ ║
15+
* full-sized ║ ║ ║ ╔═╝ ║ ║ ║ ║ ║
16+
* SD card ║ ║ ║ ║ ║ ║ ║ ║ ║
17+
* Pin name | - DO VSS SCK VDD VSS DI CS - |
18+
* SD pin number | 8 7 6 5 4 3 2 1 9 /
19+
* | █/
20+
* |__▍___▊___█___█___█___█___█___█___/
321
*
4-
* SD Card | ESP32
5-
* D2 -
6-
* D3 SS
7-
* CMD MOSI
8-
* VSS GND
9-
* VDD 3.3V
10-
* CLK SCK
11-
* VSS GND
12-
* D0 MISO
13-
* D1 -
22+
* Note: that SPI pins can be configured by using `SPI.begin(sck, miso, mosi, cs);`
23+
* alternatively you can change only the CS pin with `SD.begin(CSpin)`
24+
*
25+
* +--------------+---------+-------+----------+----------+----------+
26+
* | SPI Pin Name | ESP8266 | ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 |
27+
* +==============+=========+=======+==========+==========+==========+
28+
* | CS (SS) | GPIO15 | GPIO5 | GPIO5 | GPIO13 | GPIO13 |
29+
* +--------------+---------+-------+----------+----------+----------+
30+
* | DI (MOSI) | GPIO13 | GPIO23| GPIO24 | GPIO14 | GPIO14 |
31+
* +--------------+---------+-------+----------+----------+----------+
32+
* | DO (MISO) | GPIO12 | GPIO19| GPIO25 | GPIO15 | GPIO15 |
33+
* +--------------+---------+-------+----------+----------+----------+
34+
* | SCK (SCLK) | GPIO14 | GPIO18| GPIO19 | GPIO16 | GPIO16 |
35+
* +--------------+---------+-------+----------+----------+----------+
36+
*
37+
* For more info see file README.md in this library or on URL:
38+
* https://github.com/espressif/arduino-esp32/tree/master/libraries/SD
1439
*/
40+
1541
#include "FS.h"
1642
#include "SD.h"
1743
#include "SPI.h"
1844

45+
/*
46+
Uncomment and setup pins you want to use for the SPI communication
47+
#define REASSIGN_PINS
48+
int sck = -1;
49+
int miso = -1;
50+
int mosi = -1;
51+
int cs = -1;
52+
*/
53+
1954
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
2055
Serial.printf("Listing directory: %s\n", dirname);
2156

@@ -175,6 +210,12 @@ void testFileIO(fs::FS &fs, const char * path){
175210

176211
void setup(){
177212
Serial.begin(115200);
213+
while(!Serial) { delay (10); }
214+
215+
#ifdef REASSIGN_PINS
216+
SPI.begin(sck, miso, mosi, cs);
217+
#endif
218+
//if(!SD.begin(cs)){ //Change to this function to manually change CS pin
178219
if(!SD.begin()){
179220
Serial.println("Card Mount Failed");
180221
return;

libraries/SD/examples/SD_time/SD_time.ino

+49-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
/*
2-
* Connect the SD card to the following pins:
2+
* pin 1 - not used | Micro SD card |
3+
* pin 2 - CS (SS) | /
4+
* pin 3 - DI (MOSI) | |__
5+
* pin 4 - VDD (3.3V) | |
6+
* pin 5 - SCK (SCLK) | 8 7 6 5 4 3 2 1 /
7+
* pin 6 - VSS (GND) | ▄ ▄ ▄ ▄ ▄ ▄ ▄ ▄ /
8+
* pin 7 - DO (MISO) | ▀ ▀ █ ▀ █ ▀ ▀ ▀ |
9+
* pin 8 - not used |_________________|
10+
* ║ ║ ║ ║ ║ ║ ║ ║
11+
* ╔═══════╝ ║ ║ ║ ║ ║ ║ ╚═════════╗
12+
* ║ ║ ║ ║ ║ ║ ╚══════╗ ║
13+
* ║ ╔═════╝ ║ ║ ║ ╚═════╗ ║ ║
14+
* Connections for ║ ║ ╔═══╩═║═║═══╗ ║ ║ ║
15+
* full-sized ║ ║ ║ ╔═╝ ║ ║ ║ ║ ║
16+
* SD card ║ ║ ║ ║ ║ ║ ║ ║ ║
17+
* Pin name | - DO VSS SCK VDD VSS DI CS - |
18+
* SD pin number | 8 7 6 5 4 3 2 1 9 /
19+
* | █/
20+
* |__▍___▊___█___█___█___█___█___█___/
321
*
4-
* SD Card | ESP32
5-
* D2 -
6-
* D3 SS
7-
* CMD MOSI
8-
* VSS GND
9-
* VDD 3.3V
10-
* CLK SCK
11-
* VSS GND
12-
* D0 MISO
13-
* D1 -
22+
* Note: that SPI pins can be configured by using `SPI.begin(sck, miso, mosi, cs);`
23+
* alternatively you can change only the CS pin with `SD.begin(CSpin)`
24+
*
25+
* +--------------+---------+-------+----------+----------+----------+
26+
* | SPI Pin Name | ESP8266 | ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 |
27+
* +==============+=========+=======+==========+==========+==========+
28+
* | CS (SS) | GPIO15 | GPIO5 | GPIO5 | GPIO13 | GPIO13 |
29+
* +--------------+---------+-------+----------+----------+----------+
30+
* | DI (MOSI) | GPIO13 | GPIO23| GPIO24 | GPIO14 | GPIO14 |
31+
* +--------------+---------+-------+----------+----------+----------+
32+
* | DO (MISO) | GPIO12 | GPIO19| GPIO25 | GPIO15 | GPIO15 |
33+
* +--------------+---------+-------+----------+----------+----------+
34+
* | SCK (SCLK) | GPIO14 | GPIO18| GPIO19 | GPIO16 | GPIO16 |
35+
* +--------------+---------+-------+----------+----------+----------+
36+
*
37+
* For more info see file README.md in this library or on URL:
38+
* https://github.com/espressif/arduino-esp32/tree/master/libraries/SD
1439
*/
1540

1641
#include "FS.h"
@@ -25,6 +50,15 @@ const char* password = "your-password";
2550
long timezone = 1;
2651
byte daysavetime = 1;
2752

53+
/*
54+
Uncomment and setup pins you want to use for the SPI communication
55+
#define REASSIGN_PINS
56+
int sck = -1;
57+
int miso = -1;
58+
int mosi = -1;
59+
int cs = -1;
60+
*/
61+
2862
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
2963
Serial.printf("Listing directory: %s\n", dirname);
3064

@@ -172,6 +206,10 @@ void setup(){
172206
Serial.printf("\nNow is : %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct.tm_year)+1900,( tmstruct.tm_mon)+1, tmstruct.tm_mday,tmstruct.tm_hour , tmstruct.tm_min, tmstruct.tm_sec);
173207
Serial.println("");
174208

209+
#ifdef REASSIGN_PINS
210+
SPI.begin(sck, miso, mosi, cs);
211+
#endif
212+
//if(!SD.begin(cs)){ //Change to this function to manually change CS pin
175213
if(!SD.begin()){
176214
Serial.println("Card Mount Failed");
177215
return;

libraries/SD_MMC/examples/SDMMC_Test/SDMMC_Test.ino

+77-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,59 @@
11
/*
2-
* Connect the SD card to the following pins:
2+
* pin 1 - D2 | Micro SD card |
3+
* pin 2 - D3 | /
4+
* pin 3 - CMD | |__
5+
* pin 4 - VDD (3.3V) | |
6+
* pin 5 - CLK | 8 7 6 5 4 3 2 1 /
7+
* pin 6 - VSS (GND) | ▄ ▄ ▄ ▄ ▄ ▄ ▄ ▄ /
8+
* pin 7 - D0 | ▀ ▀ █ ▀ █ ▀ ▀ ▀ |
9+
* pin 8 - D1 |_________________|
10+
* ║ ║ ║ ║ ║ ║ ║ ║
11+
* ╔═══════╝ ║ ║ ║ ║ ║ ║ ╚═════════╗
12+
* ║ ║ ║ ║ ║ ║ ╚══════╗ ║
13+
* ║ ╔═════╝ ║ ║ ║ ╚═════╗ ║ ║
14+
* Connections for ║ ║ ╔═══╩═║═║═══╗ ║ ║ ║
15+
* full-sized ║ ║ ║ ╔═╝ ║ ║ ║ ║ ║
16+
* SD card ║ ║ ║ ║ ║ ║ ║ ║ ║
17+
* ESP32-S3 DevKit | 21 47 GND 39 3V3 GND 40 41 42 |
18+
* ESP32-S3-USB-OTG | 38 37 GND 36 3V3 GND 35 34 33 |
19+
* ESP32 | 4 2 GND 14 3V3 GND 15 13 12 |
20+
* Pin name | D1 D0 VSS CLK VDD VSS CMD D3 D2 |
21+
* SD pin number | 8 7 6 5 4 3 2 1 9 /
22+
* | █/
23+
* |__▍___▊___█___█___█___█___█___█___/
24+
* WARNING: ALL data pins must be pulled up to 3.3V with external 10k Ohm resistor!
25+
* Note to ESP32 pin 2 (D0) : add 1K pull up after flashing
326
*
4-
* SD Card | ESP32
5-
* D2 12
6-
* D3 13
7-
* CMD 15
8-
* VSS GND
9-
* VDD 3.3V
10-
* CLK 14
11-
* VSS GND
12-
* D0 2 (add 1K pull up after flashing)
13-
* D1 4
27+
* For more info see file README.md in this library or on URL:
28+
* https://github.com/espressif/arduino-esp32/tree/master/libraries/SD_MMC
1429
*/
1530

1631
#include "FS.h"
1732
#include "SD_MMC.h"
1833

34+
// Default pins for ESP-S3
35+
// Warning: ESP32-S3-WROOM-2 is using most of the default SD_MMC GPIOs (33-37) to interface with on-board OPI flash.
36+
// If the SD_MMC is initialized with default pins it will result in rebooting loop - please
37+
// reassign the pins elsewhere using the mentioned command `setPins`.
38+
// Note: ESP32-S3-WROOM-1 does not have GPIO 33 and 34 broken out.
39+
// Note: The board SP32-S3-USB-OTG has predefined default pins and the following definitions with the setPins() call will not be compiled.
40+
// Note: Pins in this definition block are ordered from top down in order in which they are on the full-sized SD card
41+
// from left to right when facing the pins down (when connected to a breadboard)
42+
43+
#if defined(SOC_SDMMC_USE_GPIO_MATRIX) && not defined(BOARD_HAS_SDMMC)
44+
int d1 = 21; // SD pin 8 - add 10k Pullup to 3.3V if using 4-bit mode (use_1_bit_mode = false)
45+
int d0 = 47; // SD pin 7 - add 10k Pullup
46+
// GND pin - SD pin 6
47+
int clk = 39; // SD pin 5 - add 10k Pullup
48+
// 3.3V pin - SD pin 4
49+
// GND pin - SD pin 3
50+
int cmd = 40; // SD pin 2 - add 10k Pullup
51+
int d3 = 41; // SD pin 1 - add 10k Pullup to card's pin even when using 1-bit mode
52+
int d2 = 42; // SD pin 9 - add 10k Pullup to 3.3V if using 4-bit mode (use_1_bit_mode = false)
53+
#endif
54+
55+
bool use_1_bit_mode = false; // Change the value to `true` to use 1-bit mode instead of the 4-bit mode
56+
1957
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
2058
Serial.printf("Listing directory: %s\n", dirname);
2159

@@ -172,8 +210,34 @@ void testFileIO(fs::FS &fs, const char * path){
172210

173211
void setup(){
174212
Serial.begin(115200);
175-
if(!SD_MMC.begin()){
176-
Serial.println("Card Mount Failed");
213+
while(!Serial) { delay(10); }
214+
Serial.println("SDMMC_Test.ino starting!");
215+
216+
// If you are using any other ESP32-S3 board than ESP32-S3-USB-OTG which has preset default pins, you will
217+
// need to specify the pins with the following example of SD_MMC.setPins()
218+
// If you want to use only 1-bit mode, you can use the line with only one data pin (d0) begin changed.
219+
// Please note that ESP32 does not allow pin change and will fail unless you enter the same pin config as is the hardwired.
220+
#if defined(SOC_SDMMC_USE_GPIO_MATRIX) && not defined(BOARD_HAS_SDMMC)
221+
//if(! SD_MMC.setPins(clk, cmd, d0)){ // 1-bit line version
222+
if(! SD_MMC.setPins(clk, cmd, d0, d1, d2, d3)){ // 4-bit line version
223+
Serial.println("Pin change failed!");
224+
return;
225+
}
226+
#endif
227+
228+
if(use_1_bit_mode){
229+
Serial.printf("Begin in 1-bit mode; pins: CLK=%d, CMD=%d, D0=%d\n", SD_MMC.getClkPin(), SD_MMC.getCmdPin(), SD_MMC.getD0Pin());
230+
}else{
231+
Serial.printf("Begin in 4-bit mode; pins: CLK=%d, CMD=%d, D0=%d, D1=%d, D2=%d, D3=%d\n", SD_MMC.getClkPin(), SD_MMC.getCmdPin(), SD_MMC.getD0Pin(), SD_MMC.getD1Pin(), SD_MMC.getD2Pin(), SD_MMC.getD3Pin());
232+
}
233+
234+
if(!SD_MMC.begin("/sdcard", use_1_bit_mode)){
235+
Serial.println("Card Mount Failed.");
236+
Serial.println("Increase log level to see more info: Tools > Core Debug Level > Verbose");
237+
Serial.println("Make sure that all data pins have 10 kOhm pull-up resistor");
238+
#ifdef SOC_SDMMC_USE_GPIO_MATRIX
239+
Serial.println("Make sure that when using generic ESP32-S3 board the pins are setup using SD_MMC.setPins()");
240+
#endif
177241
return;
178242
}
179243
uint8_t cardType = SD_MMC.cardType();

0 commit comments

Comments
 (0)