Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esp32c3 SPI SD can't not work #9082

Closed
1 task done
TasLR opened this issue Jan 9, 2024 · 13 comments · Fixed by #9422
Closed
1 task done

esp32c3 SPI SD can't not work #9082

TasLR opened this issue Jan 9, 2024 · 13 comments · Fixed by #9422
Labels
Type: Question Only question

Comments

@TasLR
Copy link

TasLR commented Jan 9, 2024

Board

esp32-c3-wroom-02

Device Description

esp32-c3-wroom-02-N4

Hardware Configuration

#define PIN_NUM_MISO 10
#define PIN_NUM_MOSI 19
#define PIN_NUM_CLK 18
#define PIN_NUM_CS 3

Version

v2.0.14

IDE Name

platformIO

Operating System

windows10

Flash frequency

40

PSRAM enabled

yes

Upload speed

115200

Description

I want to run spi sd test with esp32c3, but It can't work under arduino framework,and It can work under the esp idf framework. I use the same ports on these example. under arduino framework, the log is sd drive fail, can you tell how to do slove this problem? thank you !

Sketch

/*
 * pin 1 - not used          |  Micro SD card     |
 * pin 2 - CS (SS)           |                   /
 * pin 3 - DI (MOSI)         |                  |__
 * pin 4 - VDD (3.3V)        |                    |
 * pin 5 - SCK (SCLK)        | 8 7 6 5 4 3 2 1   /
 * pin 6 - VSS (GND)         | ▄ ▄ ▄ ▄ ▄ ▄ ▄ ▄  /
 * pin 7 - DO (MISO)         | ? ? █ ? █ ? ? ? |
 * pin 8 - not used          |_________________|
 *                             ║ ║ ║ ║ ║ ║ ║ ║
 *                     ╔═══════╝ ║ ║ ║ ║ ║ ║ ╚═════════╗
 *                     ║         ║ ║ ║ ║ ║ ╚══════╗    ║
 *                     ║   ╔═════╝ ║ ║ ║ ╚═════╗  ║    ║
 * Connections for     ║   ║   ╔═══╩═║═║═══╗   ║  ║    ║
 * full-sized          ║   ║   ║   ╔═╝ ║   ║   ║  ║    ║
 * SD card             ║   ║   ║   ║   ║   ║   ║  ║    ║
 * Pin name         |  -  DO  VSS SCK VDD VSS DI CS    -  |
 * SD pin number    |  8   7   6   5   4   3   2   1   9 /
 *                  |                                  █/
 *                  |__▍___▊___█___█___█___█___█___█___/
 *
 * Note:  The SPI pins can be manually configured by using `SPI.begin(sck, miso, mosi, cs).`
 *        Alternatively, you can change the CS pin and use the other default settings by using `SD.begin(cs)`.
 *
 * +--------------+---------+-------+----------+----------+----------+
 * | SPI Pin Name | ESP8266 | ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 |
 * +==============+=========+=======+==========+==========+==========+
 * | CS (SS)      | GPIO15  | GPIO5 | GPIO5    | GPIO13   | GPIO13   |
 * +--------------+---------+-------+----------+----------+----------+
 * | DI (MOSI)    | GPIO13  | GPIO23| GPIO24   | GPIO14   | GPIO14   |
 * +--------------+---------+-------+----------+----------+----------+
 * | DO (MISO)    | GPIO12  | GPIO19| GPIO25   | GPIO15   | GPIO15   |
 * +--------------+---------+-------+----------+----------+----------+
 * | SCK (SCLK)   | GPIO14  | GPIO18| GPIO19   | GPIO16   | GPIO16   |
 * +--------------+---------+-------+----------+----------+----------+
 *
 * For more info see file README.md in this library or on URL:
 * https://github.com/espressif/arduino-esp32/tree/master/libraries/SD
 */

#include "FS.h"
#include "SD.h"
#include "SPI.h"

/*
Uncomment and set up if you want to use custom pins for the SPI communication
#define REASSIGN_PINS
int sck = -1;
int miso = -1;
int mosi = -1;
int cs = -1;
*/
#define REASSIGN_PINS

int sck = 18;
int miso = 10;
int mosi = 19;
int cs = 3;

void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
    Serial.printf("Listing directory: %s\n", dirname);

    File root = fs.open(dirname);
    if(!root){
        Serial.println("Failed to open directory");
        return;
    }
    if(!root.isDirectory()){
        Serial.println("Not a directory");
        return;
    }

    File file = root.openNextFile();
    while(file){
        if(file.isDirectory()){
            Serial.print("  DIR : ");
            Serial.println(file.name());
            if(levels){
                listDir(fs, file.path(), levels -1);
            }
        } else {
            Serial.print("  FILE: ");
            Serial.print(file.name());
            Serial.print("  SIZE: ");
            Serial.println(file.size());
        }
        file = root.openNextFile();
    }
}

void createDir(fs::FS &fs, const char * path){
    Serial.printf("Creating Dir: %s\n", path);
    if(fs.mkdir(path)){
        Serial.println("Dir created");
    } else {
        Serial.println("mkdir failed");
    }
}

void removeDir(fs::FS &fs, const char * path){
    Serial.printf("Removing Dir: %s\n", path);
    if(fs.rmdir(path)){
        Serial.println("Dir removed");
    } else {
        Serial.println("rmdir failed");
    }
}

void readFile(fs::FS &fs, const char * path){
    Serial.printf("Reading file: %s\n", path);

    File file = fs.open(path);
    if(!file){
        Serial.println("Failed to open file for reading");
        return;
    }

    Serial.print("Read from file: ");
    while(file.available()){
        Serial.write(file.read());
    }
    file.close();
}

void writeFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Writing file: %s\n", path);

    File file = fs.open(path, FILE_WRITE);
    if(!file){
        Serial.println("Failed to open file for writing");
        return;
    }
    if(file.print(message)){
        Serial.println("File written");
    } else {
        Serial.println("Write failed");
    }
    file.close();
}

void appendFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Appending to file: %s\n", path);

    File file = fs.open(path, FILE_APPEND);
    if(!file){
        Serial.println("Failed to open file for appending");
        return;
    }
    if(file.print(message)){
        Serial.println("Message appended");
    } else {
        Serial.println("Append failed");
    }
    file.close();
}

void renameFile(fs::FS &fs, const char * path1, const char * path2){
    Serial.printf("Renaming file %s to %s\n", path1, path2);
    if (fs.rename(path1, path2)) {
        Serial.println("File renamed");
    } else {
        Serial.println("Rename failed");
    }
}

void deleteFile(fs::FS &fs, const char * path){
    Serial.printf("Deleting file: %s\n", path);
    if(fs.remove(path)){
        Serial.println("File deleted");
    } else {
        Serial.println("Delete failed");
    }
}

void testFileIO(fs::FS &fs, const char * path){
    File file = fs.open(path);
    static uint8_t buf[512];
    size_t len = 0;
    uint32_t start = millis();
    uint32_t end = start;
    if(file){
        len = file.size();
        size_t flen = len;
        start = millis();
        while(len){
            size_t toRead = len;
            if(toRead > 512){
                toRead = 512;
            }
            file.read(buf, toRead);
            len -= toRead;
        }
        end = millis() - start;
        Serial.printf("%u bytes read for %lu ms\n", flen, end);
        file.close();
    } else {
        Serial.println("Failed to open file for reading");
    }


    file = fs.open(path, FILE_WRITE);
    if(!file){
        Serial.println("Failed to open file for writing");
        return;
    }

    size_t i;
    start = millis();
    for(i=0; i<2048; i++){
        file.write(buf, 512);
    }
    end = millis() - start;
    Serial.printf("%u bytes written for %lu ms\n", 2048 * 512, end);
    file.close();
}

void setup(){
    Serial.begin(115200);
    while(!Serial) { delay (10); }

#ifdef REASSIGN_PINS
    SPI.begin(sck, miso, mosi, cs);
#endif
    //if(!SD.begin(cs)){ //Change to this function to manually change CS pin
    if(!SD.begin()){
        Serial.println("Card Mount Failed");
        return;
    }
    uint8_t cardType = SD.cardType();

    if(cardType == CARD_NONE){
        Serial.println("No SD card attached");
        return;
    }

    Serial.print("SD Card Type: ");
    if(cardType == CARD_MMC){
        Serial.println("MMC");
    } else if(cardType == CARD_SD){
        Serial.println("SDSC");
    } else if(cardType == CARD_SDHC){
        Serial.println("SDHC");
    } else {
        Serial.println("UNKNOWN");
    }

    uint64_t cardSize = SD.cardSize() / (1024 * 1024);
    Serial.printf("SD Card Size: %lluMB\n", cardSize);

    listDir(SD, "/", 0);
    createDir(SD, "/mydir");
    listDir(SD, "/", 0);
    removeDir(SD, "/mydir");
    listDir(SD, "/", 2);
    writeFile(SD, "/hello.txt", "Hello ");
    appendFile(SD, "/hello.txt", "World!\n");
    readFile(SD, "/hello.txt");
    deleteFile(SD, "/foo.txt");
    renameFile(SD, "/hello.txt", "/foo.txt");
    readFile(SD, "/foo.txt");
    testFileIO(SD, "/test.txt");
    Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
    Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
}

void loop(){

}

Debug Message

ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x1 (POWERON),boot:0xf (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5810,len:0x438
load:0x403cc710,len:0x918
load:0x403ce710,len:0x25f4
entry 0x403cc710
[   394][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x00
[   394][E][sd_diskio.cpp:802] sdcard_mount(): f_mount failed: (3) The physical drive cannot work
[   698][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x00
Card Mount Failed

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@TasLR TasLR added the Status: Awaiting triage Issue is waiting for triage label Jan 9, 2024
@me-no-dev
Copy link
Member

please post your PIO and Arduino IDE board configuration

@VojtechBartoska VojtechBartoska added Type: Question Only question and removed Status: Awaiting triage Issue is waiting for triage labels Jan 9, 2024
@TasLR
Copy link
Author

TasLR commented Jan 9, 2024

please post your PIO and Arduino IDE board configuration
I select ESP32-C3-DevKitC-02 board to create the project ,and all pio board configuration is default

@SuGlider
Copy link
Collaborator

SuGlider commented Jan 9, 2024

PIO default means Arduino Core 2.0.10 and IDF 4.4.5 Edit: IDF used in default 2.0.10 static libs.

PACKAGES:

  • framework-arduinoespressif32 @ 2.0.10
  • tool-esptoolpy @ 1.40602.0 (4.6.2)
  • tool-mklittlefs @ 1.203.210628 (2.3)
  • tool-mkspiffs @ 2.230.0 (2.30)
  • toolchain-riscv32-esp @ 8.4.0+2021r2-patch5

@SuGlider
Copy link
Collaborator

SuGlider commented Jan 9, 2024

@TasLR - Please change your Platformi.ini to use Arduino Core 2.0.14:

[env:esp32-c3-devkitc-02]
platform = espressif32 @ ^6.5.0  ; based on Arduino Core 2.0.14
board = esp32-c3-devkitc-02
framework = arduino

Test the sketch again and let us know.

@SuGlider
Copy link
Collaborator

SuGlider commented Jan 9, 2024

Building the sketch using PIO 6.5.0 should report:

PACKAGES:
 - framework-arduinoespressif32 @ 3.20014.231204 (2.0.14)
 - tool-esptoolpy @ 1.40501.0 (4.5.1)
 - toolchain-riscv32-esp @ 8.4.0+2021r2-patch5
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 33 compatible libraries
Scanning dependencies...
Dependency Graph
|-- FS @ 2.0.0
|-- SD @ 2.0.0
|-- SPI @ 2.0.0

@Jason2866
Copy link
Collaborator

Jason2866 commented Jan 9, 2024

@SuGlider
PIO default means Arduino Core 2.0.14

to be sure to use latest, it is recommended to pin the version
So latest is used with:

platform = espressif32 @ 6.5.0

if the version is left out any version can be used which is "found" (formerly installed)

EDIT: I was to slow in my post. You wrote where I was still typing :-)

@SuGlider
Copy link
Collaborator

SuGlider commented Jan 9, 2024

@Jason2866 - Got the 2.0.10 out of a fresh new project. Using just the platform = espressif32.
The only way to make it install 2.0.14 was by setting the version with platform = espressif32 @ 6.5.0.

Is this how it should work, or it may happen because I installed PIO for the first time quite a time ago?

@Jason2866
Copy link
Collaborator

Jason2866 commented Jan 9, 2024

It may happen because I installed PIO for the first time quite a time ago?

Exactly this. If you nuke Platformio with all hidden folders and use platform = espressif32
it will install 6.5.0

So that's why Platformio recommends on github espressif32 platform to pin version.
If ever a version was installed, it can kick in with just platform = espressif32

I learnt the hard way too ;-). Now i am paranoid, if something unexpected happens i delete the .platformio folder and .cache

@SuGlider
Copy link
Collaborator

SuGlider commented Jan 9, 2024

Thanks @Jason2866

@TasLR
Copy link
Author

TasLR commented Jan 10, 2024

@TasLR - Please change your Platformi.ini to use Arduino Core 2.0.14:

[env:esp32-c3-devkitc-02]
platform = espressif32 @ ^6.5.0  ; based on Arduino Core 2.0.14
board = esp32-c3-devkitc-02
framework = arduino

Test the sketch again and let us know.

I went to configure my project file according to the configuration you showed, but I still have the same problem,the build log as following:
PACKAGES:

  • framework-arduinoespressif32 @ 3.20014.231204 (2.0.14)
  • tool-esptoolpy @ 1.40501.0 (4.5.1)
  • toolchain-riscv32-esp @ 8.4.0+2021r2-patch5
    LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
    LDF Modes: Finder ~ chain, Compatibility ~ soft
    Found 34 compatible libraries
    Scanning dependencies...
    Dependency Graph
    |-- FS @ 2.0.0
    |-- SD @ 2.0.0
    |-- SPI @ 2.0.0
    Building in release mode

@TasLR
Copy link
Author

TasLR commented Mar 26, 2024

@TasLR - Please change your Platformi.ini to use Arduino Core 2.0.14:

[env:esp32-c3-devkitc-02]
platform = espressif32 @ ^6.5.0  ; based on Arduino Core 2.0.14
board = esp32-c3-devkitc-02
framework = arduino

Test the sketch again and let us know.

I've reconfigured the project the way you said, but I'm still having the same problem, could you please test it out

@lbernstone
Copy link
Contributor

There's a spot in setup where you need to change the code to reassign pins. I'll submit a PR to fix the example.

@TasLR
Copy link
Author

TasLR commented Mar 27, 2024

There's a spot in setup where you need to change the code to reassign pins. I'll submit a PR to fix the example.

Isn't the SPI pin of ESP32 series microcontroller mapped to any pin? Because I can drive normally according to the pin test you use, but I can't change to other pins, is this the difference in the program framework or some other reason

me-no-dev pushed a commit that referenced this issue Mar 27, 2024
* Matched pin numbers in doco to numbers in variants files

* Non-breaking dashes

* Changed SD examples to make REASSIGN_PINS more transparent. Fixes #9082

---------

Co-authored-by: Jan Procházka <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Question Only question
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants