• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Rebooting ESP32 after 2nd attempt to run "File file = root.openNextFile()"

Q&A Forum › Rebooting ESP32 after 2nd attempt to run "File file = root.openNextFile()"
0 Vote Up Vote Down
Joe Margevicius asked 3 years ago
8 Answers
0 Vote Up Vote Down
Joe Margevicius answered 3 years ago

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);
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));

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.

0 Vote Up Vote Down
Joe Margevicius answered 3 years ago

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 😉

0 Vote Up Vote Down
Joe Margevicius answered 3 years ago

… 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).

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
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?
Regards,
Sara

0 Vote Up Vote Down
Joe Margevicius answered 3 years ago

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. 

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
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.
Regards,
Sara

0 Vote Up Vote Down
Joe Margevicius answered 3 years ago

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.

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Great!
I’m glad this answered your question.
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.