chegewara / EspTinyUSB

ESP32S2 native USB library. Implemented few common classes, like MIDI, CDC, HID or DFU (update).
MIT License
475 stars 70 forks source link

SdFat32 support #103

Closed skobkars closed 1 year ago

skobkars commented 1 year ago

Hi chegewara,

I've added support of the Bill Greiman's SdFat on SD/SPI card. It uses SdFat32 class.

Please consider merging to your code.

chegewara commented 1 year ago

Hi, thanks for this PR. How this new class is different from sdcard class?

skobkars commented 1 year ago

SD.h uses old version of SdFat (Copyright (C) 2009 by William Greiman, Copyright (c) 2010 SparkFun Electronics) and for example doesn't support long file names.

So, using SdFat directly may be preferable in some cases.

That said, I just realized that I was to quick to send the PR, and have not implemented a few features like stop/start, etc. sorry. If you think this switch to SdFat can be useful I will implement them and update the PR.

chegewara commented 1 year ago

I dont mind to merge this PR and to have 2 classes, so please let me know when it is ready to merge.

skobkars commented 1 year ago

I made some deep changes in MSCCallbacks class in order to be able to override even single callbacks. Also, made SDFat2USB to derive from both MSCusb and MSCCallbacks to simplify its structure.

Fixed onStop, and made some other changes, i.e. new example code to show how callbacks work.

Tried to make MSCCallbacks* defaultCallbacks static, so there is no need to save them in the overloaded MSCCallbacks, but that breaks compiling under Arduino IDE for some reason.

Now, I've been discussing issues with SdFat (older SD.h, Arduino Fork and the new v2.x) with Bill Greiman, as all version suffer from the same problem: https://github.com/greiman/SdFat/issues/349#issuecomment-1188235279 But, apparently, this is due to the SdFat (in any reincarnation) being not thread safe: https://github.com/greiman/SdFat/issues/349#issuecomment-1189061629

Setting CONFIG_DISABLE_HAL_LOCKS in ESP32-S2 cores helps to some degree, but adding fs_changed flag and also adding time delays (not shown in my example) helps even more. Unfortunately, the nature of SdFat makes it inherently unstable if the file system on SD card is used both through USB and directly on ESP32 device.

It will work for my project, as I don't necessarily need to access files both ways at the same time, but this maybe an issue for others.