Teensy 4.1 SD.open failing to create files randomly

ladansedesdamnes

Active member
I'm creating a data logger sketch using SD card. The teensy receives the log name via external message, checks if that file exists and, if so, increments it with a suffix ("_X"). That part is working fine.
Whenever I try to actually create the files (not even writing data to them yet) I start getting erratic behavior. Specifically, here's what happened:
Sketch was able to create "original_name" file without issue. Then checks, file exists, increments and creates "original_name_1" without issue. Now when it tries to create "original_name_2", that fails. Restart teensy, fails again. Remove SD card from slot, open in reader, check for files (all created ok), delete all, re-insert into teensy.
"original_name", ""original_name_1" and "original_name_2" all created successfully. Now fails on "original_name_3".
Edit the sketch change to other log name, now fails on creating "original_name" base file. Changing again sometimes fails at base name, sometimes writes some iterations, but never managed to get past the third increment.

I'm stumped.

Code is as simple as can be.
Code:
File log_file = SD.open(log_name, FILE_WRITE);

if (!log_file) {
    Serial.println("File open failed!");
}

if(log_file) log_file.close();

Using
#include <SD.h>
#include <SPI.h>

Other info:
I'm using the onboard SD card slot (obviously), an OctoWS2811 Adaptor and a common anode RGB led connected to pins 24, 28, and 37 (free PWM pins). Using Visual Micro on Visual Studio.
32GB SD card formatted using official SD Memory Card Formatter, and shows no errors when checked.
 
Last edited:
For reference, I found the problem. SD library follows classic fat rules, so filenames are limited to 8 characters + extension. Adding the increments was going over that.
 
That is not correct, except you are using a very old TD installation. New SD is a wrapper to SdFat which supports long filenames etc.
 
I tried including the Teensy SD library locally in the project to avoid there being conflicts with the Arduino SD library and still get the same error. Only way to make it go away is to reduce character count or straight up use SdFat

EDIT: just checked the library, version=2.0.0
 
SD library follows classic fat rules, so filenames are limited to 8 characters + extension.

This was true for Teensyduino 1.53 and earlier. Starting with 1.54 we replaced Arduino SD library with a thin wrapper which actually uses SdFat.


I tried including the Teensy SD library locally in the project to avoid there being conflicts with the Arduino SD library and still get the same error.

This definitely should work if you are using Teensyduino 1.54 or later. If you are using the new Arduino IDE version 2, the oldest version available is 1.56. Versions 1.55 and earlier only worked on the older java-based Arduino IDE (1.8.x).

If you have explicitly installed the SD and/or SdFat library, perhaps Arduino IDE is using those copies and ignoring the SD and SdFat libraries installed by Teensyduino? You can check by turning on verbose output during compile in File > Preferences. Then when you compile, Arduino IDE will show a list of the libraries it actually used and any it found but did not use, with their full pathnames. You can see by the subdirectories in the pathname whether the library used was from the Teensyduino package.

Just to restart, long filenames definitely do work if using Teensyduino 1.54 or later. But it only works if you use the libraries we provide in that package. If you install libraries with the same name and Arduino IDE uses your copy, then the behavior you get will depend on the libraries you installed. To get working long filename with SD library compatibility, use the libraries from the Teensyduino package.

If this still doesn't solve your problem, please know this is supposed to work, but we can't see your computer screen. To help more directly, you need to show us the code and enough info so anyone can reproduce the problem.
 
Just to add a bit more info, the SdFat library installed by Teensyduino has additions beyond Bill's official version.

The SD.h thin wrapper checks for a special define, so if you use a plain copy of SdFat without Teensy's special extensions you should see an error explaining the situation. If you've been installing these libraries yourself rather than using the copies Teensyduino provides, you'll very likely see that error. Please don't ignore it or circumvent it. That error exists because we have some special extensions (which Bill didn't want to add) and we test these together, so features like long file names are known to work.
 
Based on the "it should work" assumption I checked the compiler output, which showed it was indeed using the correct version of the SD library (which it was). I created a clean sketch from scratch with just my SD card code in the Arduino IDE and everything works as expected, even with 8+ characters, as everyone was saying it should. If I open that with Visual Micro I get errors regarding the conflicting SdFat libraries.

I believe this is an issue with Visual Micro's inclusion of the libraries, and will have to check how that is being done.

Thank you all for your help!
 
Definitely an issue to report to Visual Micro. Maybe they've updated only the core library but still shipping a very old version for SD?
 
I believe VM is falling back to the basic SdFat library and not defaulting to the platform-appropriate version of the library. I will report this to the forums and see what they suggest.
I can obviously manually add the library to the project, but that seems like something that should be default automated behavior.
 
I believe VM is falling back to the basic SdFat library and not defaulting to the platform-appropriate version of the library. I will report this to the forums and see what they suggest.
I can obviously manually add the library to the project, but that seems like something that should be default automated behavior.
I wish there was a good complete answer for this. But currently it can be sort of messy, especially if you also do stuff on other
platform boards, like boards made by Arduino, example: UNO R4

If you don't download SDFat using Library manager, (or install from Github a different version), then you are unable to use SDFat on the other boards. If you do download it, you cannot use the Teensy SD library.

I can normally get around this by installing the Adafruit version of SDFat, which downloads into a different directory name: SdFat_-_Adafruit_Fork
And the Arduino library search code, tie breaker, with all things considered equal will choose the SDFat directory over this one... So on Teensy it uses the built in.... On others it uses the Adafruit fork.

However if you want to use the absolute most up to date code, like Bill's changes for UNO R4, then you break Teensy support.

Wish there was a clean way for both the standard and Teensy versions to coexist, maybe different directory name like Adafruit?
Although not sure if that would help much with the generic platform type of AVR.
 
Back
Top