|
1 | 1 | /*
|
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 |
3 | 26 | *
|
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 |
14 | 29 | */
|
15 | 30 |
|
16 | 31 | #include "FS.h"
|
17 | 32 | #include "SD_MMC.h"
|
18 | 33 |
|
| 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 | + |
19 | 57 | void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
|
20 | 58 | Serial.printf("Listing directory: %s\n", dirname);
|
21 | 59 |
|
@@ -172,8 +210,34 @@ void testFileIO(fs::FS &fs, const char * path){
|
172 | 210 |
|
173 | 211 | void setup(){
|
174 | 212 | 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 |
177 | 241 | return;
|
178 | 242 | }
|
179 | 243 | uint8_t cardType = SD_MMC.cardType();
|
|
0 commit comments