-
Notifications
You must be signed in to change notification settings - Fork 143
ESP32-S2 msc_sdfat example crashes #178
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
Comments
I run feather esp32s2 with adalogger, and has no issue with both Serial and MSC, therefore couldn't reproduce this issue. However, from what you said, this is probably the conflict when both loop() and msc callback try to access SD card at the same time (especially when writing files), which cause conflict in SPI driver and cause an data corruption which lead to hardfault (or something like that). It is not an bug with tinyusb, but rather the example sketch's. Maybe you should try to introduce a semaphore (or busy flag variable) to skip reading file in loop() when msc writing/reading is in the way. |
Are you saying that msc_sdfat example works on adalogger? For my project I need WiFi, MSC and access to the FAT filesystem, thus the choice of the board(s). But if adalogger works with MSC/FAT combination I may try that board with a WiFi breakout instead maybe. BTW, Serial is not an issue here, as disabling it makes no difference - the sketch is still crashing the board. As for using semaphore, how do I make sure that callbacks are not called while I am already checked, found it not in use and called file or directory methods in loop? I could probably set another flag before calling those methods and wait until they finish before calling read/writeBlocks, but the callbacks are very time sensitive as I learned, so that may break the MSC anyway. |
yes. The conflict is just my guess based on your input. You cannot prevent callback to be invoked, but you could either delay() within callback or simply set the unit to not ready when wanting to access the SD card. Let me know if adalogger work with stock example for you with Feather ESP32s2 (which are my testing hardware). |
What are the exact boards that work for you? Which ESP32-SP2 and which adalogger? |
As Adalogger also uses SPI for SD it should not be any different from my setup. The only minor difference is the CS pin used. I am puzzled. |
Okay, I used millis() with 300 ms delay to fix the sketch to work for me. Attaching the working version here if you want to test it with your setup. It does not print out directory list every second, but only after changes have been made to the file system. 100ms was the minimum that more or less reliably worked on my computer, 150ms was almost ideal, but failed a couple of times when I copied/deleted 100+ files. 300ms seems to be solid for now.
|
I used feather esp32s2 + adalogger
I don't copy files, just stop and running the sketch as reproducing step said.
adding time delay may not fix the issue, it probably just make the issue harder to reproduce. I won't try to fix this since this is off-USB and purely the SPI/SD peripheral usage conflict between the loop() and the callback. The loop print out directory is for visual demonstration only, you can just remove it entirely. |
I connected debug pin to serial port and ran the crashing code again. This is what I get:
I tracked that assert to esp32/2.0.3/tools/sdk/esp32s2/qspi_qspi/libfreertos.a so it has to do with using FreeRTOS. It will take me forever to figure this on out, so I thought I'd share it and see if it tells you something right away. Thanks |
the trace literally prove my mentioned point above. This looks more to the spi/sd conflict than usb issue, maybe you should find an way to access SD on multiple thread safely. This is out of my knowledge and I couldn't help any further. Issue is closed since it is off-USB |
Just wanted to share here the (temporary?) solution to the issue. Apparently, it was not a problem before ESP32 Core version prior to 2.0.0 as per espressif/arduino-esp32#6079 Unfortunately, I cannot downgrade the Core because my board (Adafruit ESP32-S2 Feather with BME280 Sensor - STEMMA QT) is not supported in any version earlier than 2.0.3. However, following the hint in the linked above discussion about disabling HAL locks I set CONFIG_DISABLE_HAL_LOCKS to 1 and bingo! My sketch is working fine now. As ESP32-S2 only has one core, HAL locks appear to be not important anyway. |
thank you for sharing the solution, I am sure other with the same issue will appreciate your finding. Too bad this is out of my knowledge to help with. |
For anyone stumbling on this issue, on my esp32-s2 with the latest Arduino ESP32 Core this example sketch still crashes. Must admit, it's too bad the example doesn't work at the moment. It indeed seems that it's related to the SPI class or how SdFat is accessing it, so nothing to do with TinyUSB. Even when using SdFat together with another MSC library in just the same way, the program will hang. I ended up leaving TinyUSB and using the USBMSC class which comes with the ESP32 Core. After digging into the libraries and testing some different setups I discovered that I could make the code working by ending SdFat together with the SPI class by calling |
Operating System
MacOS
IDE version
1.8.19
Board
Adafruit ESP32-S2 Feather with BME280 Sensor - STEMMA QT (4MB Flash + 2 MB PSRAM) + Adafruit Micro SD SPI or SDIO Card Breakout Board connected as SPI device with CS = 15
ArduinoCore version
2.0.3
TinyUSB Library version
1.12.0
Sketch (attached txt file)
examples/MassStorage/msc_sdfat with two minor changes:
What happened ?
If during loop() directory printing process one of the usb_msc.setReadWriteCallback callbacks is called, or in other words when USB driver is working on reading or writing SD card (SPI interface, CS = 15) the ESP32-S2 Feather crashes and reboots.
If I disable root. and file. calls in loop() the device is working normally as a USB storage device.
If I leave usb_msc.setUnitReady in FALSE state and keep fs_changed flag as TRUE, loop is printing directory listing periodically without any issues.
If both usb_msc.setUnitReady is TRUE, and access to SD card file system is enabled in loop(), the device crashes and restarts constantly.
How to reproduce ?
Debug Log
output.txt
Screenshots
No response
The text was updated successfully, but these errors were encountered: