Can't List Files on SD Card Teensy 3.2 + Audio Adaptor

Status
Not open for further replies.

lerxstrulz

Well-known member
Hi,

I have a sketch that contains the ability to list all the files on the SD card (like the ListFiles example.) All of a sudden this is not working any longer. On the first iteration it is failing. HOWEVER, I can still read and write individual files on the SD Card.

I was thinking maybe it was a memory issue with my sketch, but even the simple program below is not working. It fails on the first call of dir.openNextFile(). Anyone have any ideas why this would just stop working, especially when I seem to be able to access the SD card just fine?

I'm using a Teensy 3.2 with the Audio Adaptor, Arduino IDE 1.6.8 and TeensyDuino 1.29.

Thanks in advance!

Code:
#include <SPI.h>
#include <SD.h>

// These pins are for the Teensy Audio Adaptor SD Card reader - DO NOT CHANGE!
const int SDCARD_CS_PIN    = 10;
const int SDCARD_MOSI_PIN  = 7;
const int SDCARD_SCK_PIN   = 14;

String listFiles(File dir) 
{
  while(true) {
     File entry = dir.openNextFile();
     if (! entry) {
      Serial.println("NO MORE FILES!");
       // no more files
       break;
     }
     // convert to string to make it easier to work with...
     String entryName = (String)entry.name();
     Serial.println("FOUND: " + entryName);
     entry.close();
   }
}

/***
 * Initial setup...runs only once when the board is turned on.
 */
void setup() 
{
  
  Serial.begin(57600);
  
  delay(500);
  
  // Check SD card
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
     Serial.println("Unable to access the SD card");
  } else {
    // Read the contents of the SD card
    File root = SD.open("/");
    root.rewindDirectory();
    listFiles(root);
    root.close();
  }
}

/***
 * Main program loop
 */
void loop() 
{
   
   
}


// END
 
I ran it here on a Teensy 3.2 with audio shield. It seems to work fine, showing all 6 files on my SD card.

I tested with Arduino 1.8.1 and Teensyduino 1.36-beta1 and Tools > Optimize set to "Faster".

Code:
FOUND: PARROT.BMP
FOUND: BACKGND.WAV
FOUND: SDTEST1.WAV
FOUND: SDTEST2.WAV
FOUND: SDTEST3.WAV
FOUND: SDTEST4.WAV
NO MORE FILES!
 
Ah...well...nevermind....turns out the SD card was bad :( I realized I had another card to try and it works fine. Been messing with it for over and hour lol.

Thanks for responding so quickly, Paul!
 
Hello @lerxsturlz ,

how's your card's power? the operation voltage should be safe with 3.3V

I've been playing with micro SD around last year, and burnt dozens of them.
I mostly ran them with a single lithium battery , without any power regulation,
and that is 4.2V~3V, ( sometimes even 5V with USB powered)
the cards, they work fine usually,
but randomly goes bad and some goes permanently fails
with a chance of .... maybe .... 1~4% per boot
I uses cheap cards so I don't quite care

and that's another point, cheap cards with small storage spaces burns more often.
 
Hello @lerxsturlz ,

how's your card's power? the operation voltage should be safe with 3.3V

I've been playing with micro SD around last year, and burnt dozens of them.
I mostly ran them with a single lithium battery , without any power regulation,
and that is 4.2V~3V, ( sometimes even 5V with USB powered)
the cards, they work fine usually,
but randomly goes bad and some goes permanently fails
with a chance of .... maybe .... 1~4% per boot
I uses cheap cards so I don't quite care

and that's another point, cheap cards with small storage spaces burns more often.

I'm using SanDisk Ultra 8G SD cards, and I'm having intermittent issues still. I replaced what I thought was a faulty card with a new one and everything seemed to work, but then all of a sudden weird things start happening again like a .WAV file getting corrupted, and then .WAV files playing sometimes but not others. Not sure if it's an issue with the Audio Adapter (since it has the SD card reader) or the card itself. Since it's happening to two different cards and just started happening, I'm starting to think maybe something is going wrong with the Audio Adaptor...I'll keep an eye on it.
 
I ran it here on a Teensy 3.2 with audio shield. It seems to work fine, showing all 6 files on my SD card.

I tested with Arduino 1.8.1 and Teensyduino 1.36-beta1 and Tools > Optimize set to "Faster".

Code:
FOUND: PARROT.BMP
FOUND: BACKGND.WAV
FOUND: SDTEST1.WAV
FOUND: SDTEST2.WAV
FOUND: SDTEST3.WAV
FOUND: SDTEST4.WAV
NO MORE FILES!

Arduino 1.8.1 TeensyDuino 1.35

Update: It's happening again. I can access individual files, but when I try to list all files on the SD card it fails on the first iteration. I had to copy all the files from the card, reformat it and then copy them back and it started working. About a week later...same thing. I bought another SD card (this makes 3) and same thing. Worked for a while, then could not list files but could access individual files....reformat...works fine...

When this happens it doesn't matter what sketch I used to try and read the files...get the same result. My laptop can access the card just fine. Could it be a bad Audio Adaptor corrupting the cards?
 
Last edited:
No, it can not corrupt cards. But the cards are not very well protected against ESD, and they don't like removal or plug-in when the circuit is powered. Sometimes re-formattings helps - but use the official SD-Formatter, not Windows.
https://www.sdcard.org/downloads/formatter_4/

Hmmm...pretty sure there's been no ESD, and I've never pulled or inserted the card while the board is powered. It's weird how it's happened to 3 different cards. In fact, while the board is powered, it will all of a sudden say it cannot access the SD card. Unplug it, power it back up and then it can read it. Sometimes it takes several times. I'm starting to think that maybe it's just a bad board.

I do use SDFormatter when I format them ;)
 
I remember there's problem about the SD library at 1.8.0 ?

did you use the same function when accessing individual files? and while list them all?

Try SdFat, it works better, at least for me.
just comment the
Code:
#include <SD.h>  [\CODE]
and add the lines, the same sketch should work without too much changing.
[CODE]
 #include <SdFat.h>
SdFat SD;   [\CODE]
 
It's weird how it's happened to 3 different cards.

That is indeed weird.

As a first sanity check, please try editing SD_t3.h. Uncomment the line which turns on the experimental optimizations. That causes the entire SD library code to be replaced by a version I made, which is currently still read-only. It has absolutely no code which can write to the SD card.

Pay attention to the RAM memory usage summary Arduino prints. The optimized lib has a small cache of 7 sectors, instead of just a single sector buffer in the normal SD lib. So you should see the RAM usage jump about 4K or so, as a sign the optimized lib is really being used. Of course, if you try any code which writes data, it will fail because there's no ability to write to the card at all using the optimized code. At least not today... someday I'll add write support (so anyone finding this info later, this advice will become obsolete info....)

If you *still* get SD cards mysteriously changing with the read-only optimized code, well, something very strange is going on! Either way, I might want to investigate if you can give me the exact code and conditions to reproduce your results... but first, let's see if the cards mysteriously change using the optimized code which can't write at all.
 
That is indeed weird.

As a first sanity check, please try editing SD_t3.h. Uncomment the line which turns on the experimental optimizations. That causes the entire SD library code to be replaced by a version I made, which is currently still read-only. It has absolutely no code which can write to the SD card.

Pay attention to the RAM memory usage summary Arduino prints. The optimized lib has a small cache of 7 sectors, instead of just a single sector buffer in the normal SD lib. So you should see the RAM usage jump about 4K or so, as a sign the optimized lib is really being used. Of course, if you try any code which writes data, it will fail because there's no ability to write to the card at all using the optimized code. At least not today... someday I'll add write support (so anyone finding this info later, this advice will become obsolete info....)

If you *still* get SD cards mysteriously changing with the read-only optimized code, well, something very strange is going on! Either way, I might want to investigate if you can give me the exact code and conditions to reproduce your results... but first, let's see if the cards mysteriously change using the optimized code which can't write at all.

Thanks Paul. I'll try that. It happened again today but I saw this before reformatting ;) Let me try the edits you suggest and see if it is able to read the card.
 
That is indeed weird.

As a first sanity check, please try editing SD_t3.h. Uncomment the line which turns on the experimental optimizations. That causes the entire SD library code to be replaced by a version I made, which is currently still read-only. It has absolutely no code which can write to the SD card.

Pay attention to the RAM memory usage summary Arduino prints. The optimized lib has a small cache of 7 sectors, instead of just a single sector buffer in the normal SD lib. So you should see the RAM usage jump about 4K or so, as a sign the optimized lib is really being used. Of course, if you try any code which writes data, it will fail because there's no ability to write to the card at all using the optimized code. At least not today... someday I'll add write support (so anyone finding this info later, this advice will become obsolete info....)

If you *still* get SD cards mysteriously changing with the read-only optimized code, well, something very strange is going on! Either way, I might want to investigate if you can give me the exact code and conditions to reproduce your results... but first, let's see if the cards mysteriously change using the optimized code which can't write at all.


Well, good and bad news ;)

When I activated the optimized code, my sketch could perform a file listing! When I disabled the optimized code....no listings. So the optimizations definitely help.

The bad news....my sketch depends upon the ability to list files on the SD card. It also writes to SD card.... Also, this is a project that I've made available to anyone and the ability to read/write to the SD is pretty crucial.

If I switch to the standard SD library or even SD Fat for now, do those play well with Teensy?
 
The bad news....my sketch depends upon the ability to list files on the SD card.

Well, that seems like new information.... something I wouldn't have guess from the 1 and only program you've shown us so far.

In terms of blind guesswork, maybe you'll need to make use of AudioNoInterrupts() whenever you access the SD card?

https://www.pjrc.com/teensy/td_libs_AudioProcessorUsage.html

To actually comment without blind guessing, you really need to post a complete program which demonstrates the problem. Until you do so, you're keeping everyone in the dark here. I'm trying to help you here, but I hope you can understand how little I can do when I have to guess what your program really does.
 
Ok, I upgraded to Arduino 1.8.2 and TeensyDuino 1.36. Still not listing all the folders.

I created a sample sketch using SDFat and it lists all of the folders on the card. Below is the SDFat sketch I used:

Code:
/*
 * Print size, modify date/time, and name for all files in root.
 */
#include <BlockDriver.h>
#include <FreeStack.h>
#include <MinimumSerial.h>
#include <SdFat.h>
#include <SdFatConfig.h>
#include <SysCall.h>

#include <SPI.h>

const int SDCARD_CS_PIN    = 10;  
const int SDCARD_MOSI_PIN  = 7;
const int SDCARD_SCK_PIN   = 14;

// SD default chip select pin.
const uint8_t chipSelect = SS;

// file system objectg
SdFat sd;

SdFile file;
//------------------------------------------------------------------------------
void setup() {
  Serial.begin(57600);
  
  delay(500);
  
  // Check SD card
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  
  // Wait for USB Serial 
  while (!Serial) {
    SysCall::yield();
  }
  
  Serial.println("Type any character to start");
  while (!Serial.available()) {
    SysCall::yield();
  }

  // Initialize at the highest speed supported by the board that is
  // not over 50 MHz. Try a lower speed if SPI errors occur.
  if (!sd.begin(SDCARD_CS_PIN, SD_SCK_MHZ(50))) {
    sd.initErrorHalt();
  }

  sd.chvol();
  
  // Open next file in root.  The volume working directory, vwd, is root.
  // Warning, openNext starts at the current position of sd.vwd() so a
  // rewind may be neccessary in your application.
  sd.vwd()->rewind();
  while (file.openNext(sd.vwd(), O_READ)) {
    file.printFileSize(&Serial);
    Serial.write(' ');
    file.printModifyDateTime(&Serial);
    Serial.write(' ');
    file.printName(&Serial);
    if (file.isDir()) {
      // Indicate a directory.
      Serial.write('/');
    }
    Serial.println();
    file.close();
  }
  Serial.println("Done!");
}
//------------------------------------------------------------------------------
void loop() {}

and this is the output (the "missing" directories that do not appear when the SD library is used are in red):

Code:
Type any character to start
      4096 2017-04-06 00:09:18 ._.Trashes
         0 2017-04-06 00:09:36 effects/
         0 2017-04-06 00:09:18 .Trashes/
        55 2000-01-01 01:00:00 SETTINGS.TXT
         0 2017-04-06 01:21:48 tktalki/
         0 2017-04-06 00:09:18 .Spotlight-V100/
       322 2017-03-01 13:12:58 CALIBRATE.TXT
      4096 2017-04-06 00:09:36 ._CALIBRATE.TXT
      2011 2017-03-01 13:10:48 HELP.TXT
      4096 2017-04-06 00:09:38 ._HELP.TXT
         0 2017-04-06 00:09:36 loops/
         0 2017-04-06 00:09:36 profiles/
         [COLOR="#FF0000"][B]0 2017-04-06 00:13:58 tktalkie/[/B][/COLOR]
      4096 2017-04-06 00:10:20 ._SETTINGS.TXT
         [COLOR="#FF0000"][B]0 2017-04-06 00:09:36 sounds/[/B][/COLOR]
        [COLOR="#FF0000"] [B]0 2017-04-06 01:34:12 sounds2/[/B][/COLOR]
        [COLOR="#FF0000"] [B]0 2017-04-06 01:22:10 data/[/B][/COLOR]
         [COLOR="#FF0000"][B]0 2017-04-06 01:23:56 test1/[/B][/COLOR]
         [COLOR="#FF0000"][B]0 2017-04-06 01:24:18 test1e/[/B][/COLOR]
         0 2017-04-06 01:34:52 .TemporaryItems/
      4096 2017-04-06 01:34:52 ._.TemporaryItems
Done!

When I run the code I originally posted using the SD library (/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/SD), these are the results:

Code:
FOUND: ~1.TRA
FOUND: EFFECTS
FOUND: TRASHE~1
FOUND: SETTINGS.TXT
FOUND: TKTALKI
FOUND: SPOTLI~1
FOUND: CALIBR~1.TXT
FOUND: _CALIB~1.TXT
FOUND: HELP.TXT
FOUND: _HEL~1.TXT
FOUND: LOOPS
FOUND: PROFILES
NO MORE FILES!

As you can see there are 6 directories that are not showing up when using the SD library.

So the next question....does anyone have the Audio library working with SDFat? It would also be nice to be able to use longer file names ;)
 
Last edited:
Status
Not open for further replies.
Back
Top