I am exploring SD card use, and after having some problems, found a question that you have resolved by giving a link to code that performs the following (This code runs perfectly in on my board).
listDir(SD, “/”, 0);
listDir(SD, “/”, 0);
listDir(SD, “/”, 2);
writeFile(SD, “/hello.txt”, “Hello “);
appendFile(SD, “/hello.txt”, “World!\n”);
renameFile(SD, “/hello.txt”, “/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));
This runs fine, just the once, in setup() as expected. However, when I copy that section of code and place it in loop(), along with a delay of 2 seconds, it successfully performs the File root = fs.open(dirname); command in the listDir(SD, “/”, 0); module, but causes a reboot when it continues and tries to do the File file = root.openNextFile(); command. I increased the delay between these 2 commands to 10 seconds, and same result – a reboot.
I am focusing on this since I have the very same issue – rebooting – in other code I developed before trying the RNT troubleshooting code. Any help would be appreciated. Thanks.
Ok, I seem to be answering my own questions — maybe I should just wait and keep experimenting !! However, the answer could be useful to others with the same issue.
Here’s how I solved the “rebooting” problem. For reasons I’m not clear about, the SPI needs to be started again. I put these 2 lines in the section of loop() that repeats going through all the tests (these 2 lines are in the setup(), but I didn’t copy them into the loop(), figuring the SPI was already started – apparently something shut it down).
SPIClass spi = SPIClass(VSPI);
spi.begin(SCK, MISO, MOSI, CS);
These 2 lines restart the SPI every swing through loop(), and allow the access to continue (the ESP32 does not reboot — that must happen when the SPI is not started up and an access is attempted).
Anyway, happy camper, for now 😉
… well sort of a Happy Camper … still don’t know why I need to spi.begin again after this was already done in setup(). … according to SPI documentation: “SPI.begin Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.” … does reading the SD card leave SCLK and MOSI = hi and/or SS = Lo ? and thus in the wrong state to do another read – causing a reboot? (I’ll get to checking this with a scope soon if no one else has any insight).
I’m sorry for taking so long to get back to you.
That is a weird behavior, and not what I was expecting.
Can you share the code you’re testing? So that I can take a quick look?
Hi Sara … here is the github link to the file. Basically, if I comment out the 2 lines just after loop() (as the file in github has), it will reboot just after printing out “Listing directory: /” … i.e. it fails at File root = fs.open(dirname); … uncommenting those 2 lines and it works fine. (link: https://github.com/jamargevicius/jamargevicius). thanks for any insight into why I need to redo the spi.begin() command.
I think I found the cause and solution.
Check this discussion: https://github.com/espressif/arduino-esp32/issues/1513
Let me know if the suggestion solves your problem.
Thank you Sara — yes, that was the issue: SPIClass spi = SPIClass(VSPI); must be declared before/outside any modules, and then the SPI can be started, once and for all, in setup() by spi.begin(SCK, MISO, MOSI, CS); On later running stuff in loop(), the spi doesn’t need to be declared, started, or whatever 🙂 (The espressif-written code, which was the source of this test code, was not written well – something I’ve seen in many other places 🙁 )
This issue can be closed. Thanks.