M5Stack: switch from SD to sdFat library

  • Good evening,
    I would like to switch from the SD library to the sdFat library, as it also supports long file names.
    Unfortunately, the SD library is hard-coded and integrated in the M5Stack library.

    How can I now use sdFat exclusively?

    Thank you very much for tips and help.

    Kind regards, Armin

  • I am trying the same SD library shift but seems complicated at first instance.

    • I have removed the <SD.h> include call from my main.cpp program.
    • Also removed the "SD.h" include call from M5Stack.h header.
    • Removed the initialization call SD.begin line at M5Stack.cpp
    • Removed all the SD.h library dependencies from platformio.ini archive. In my case the SD(esp32) 1.0.5 library.
    • Checked and removed any other library SD.h include link (see the Dependency Graph from the build Task).

    Now the main.cpp seems to be free from any SD.h dependency. But tried to use the SdFat.h and get crashed. Maybe it is my fault because at the initialization of the sd.begin() from SdFat???

    I used this call for M5Stack Core 1 (Basic and Grey modules):

    #include <SDFat.h>
    SdFat sd;

    sd.begin(4); // CS_PIN TFCARD M5STACK is 4

    Could you please check this method??? Maybe I am forgetting something...


  • Hello Gorka,
    after a lot of trial and error I found a solution, wich is working (for me) on 2 M5Stack gray with different SD cards.

    It is possible to use both librarys (SD and sdFat) in parallel, but there are some settings to make:

    // in the main file define the max SPI speed
    #define SPI_SPEED SD_SCK_MHZ(25)                             // MHz: OK 4, 10, 20, 25  ->  too much: 29, 30, 40, 50 causes errors
    #define SD_CONFIG SdSpiConfig(TFCARD_CS_PIN, SHARED_SPI, SPI_SPEED) // TFCARD_CS_PIN is defined in M5Stack Config.h (Pin 4)
    if (!sd.begin(SD_CONFIG)) {}
    // in file SdFat\src\SdFatConfig.h    at Line 100:
    #define ENABLE_DEDICATED_SPI 0        // (from 1 -> 0)
    • TFCARD_CS_PIN is defined in M5Stack Config.h (Pin 4)
    • SHARED_SPI / ENABLE_DEDICATED_SPI 0 is choosen because SD card and TFT are sharing one SPI bus
    • SPI_SPEED is max 25 MHz, I don't know why, but at >25 MHz the sdFat library crashes...

    I made some benchmarks and a test file, they are located at my github repository, feel free to test it...

    Kind regards, Armin

  • Oh! Thanks! Gonna try it tomorrow with my two m5stack units. :)

  • Hello, try to compile the first example file and got the error
    in thus part:

    SdFat32 sd; // for FAT16/FAT32
    File32 file;
    File32 root;

    exit status 1
    'File32' does not name a type

    do I need to delte file?

    Thanks for any help.

  • Found the problem. After deleting an old sdfat and installing a new one it was working perfect on my Core2.
    Thanks for hiis fine explanation. Was verry helpfull.