Teensy 4.1 Sd Card writing

MBE

Member
Hi all,
I'm trying to write a lot of files to an Sd Card through the Teensy 4.1 SDIO interface and after a while the open function returns an error. I tried different Sd Cards (San Disk and Samsung) and different file size (64KB, 512 KB): in all cases , starting from an empty sd card, after 4094 files written correctly, the open function started to report an error indefinitely, also after a power cycle of the Teensy. Then I removed the card from the teensy and inserted it in the PC (Win): all the 4094 files were correct. Then, using the PC, I added a file manually. At that point the teensy found correctly 4095 files and started again to working correctly for another 4095 files and so on... So the first time teensy is able to write 4094 files and then 4095 files, each time adding a dummy file using the PC.
I'm using the SD library (SD ver 2.0.0 and SdFat ver. 2.1.0). All the SD Cards are formatted with a 512KB allocation unit size. Find attached the code to test the strange behavior:

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

#define SD_BUFFER_SIZE 32*512

SdFs mysd;
FsFile myfile;
FsFile root;
static char fileName[80];
char sdBuffer[SD_BUFFER_SIZE];
bool ret;
unsigned int file_counter;


void setup()
{
  uint16_t tries=0;
  unsigned int rootFileCount=0;
  while(!Serial) continue;
  pinMode(40,OUTPUT);
  pinMode(13, OUTPUT);
  memset(sdBuffer,0,SD_BUFFER_SIZE);
  file_counter=0;
  delay(1000);
  
  while(!mysd.begin(SdioConfig(FIFO_SDIO)) && tries<10){ 
    Serial.print(tries++); Serial.print(" "); 
    delay(1000); 
  } 
  if(tries>=10) 
  {
    Serial.println("No SD storage"); 
    while(1);
  }

  if (!root.open("/")) {
    Serial.println("error opening root");
  }
  while (myfile.openNext(&root, O_RDONLY)) {
    if (!myfile.isHidden()) {
      rootFileCount++;
    }
    myfile.close();
  }
  delay(5000);
  Serial.printf("Number of files in root folder: %d\n",rootFileCount);
  Serial.println("End of Setup");
  return;
}

void loop()
{
  DateTimeFields tm;
  breakTime(rtc_get(), tm);
	sprintf(fileName, "F_%04d%02d%02d_%02d%02d%02d_%05d.bin", (short int)(tm.year)+1970, tm.mon, tm.mday, tm.hour, tm.min, tm.sec, ++file_counter); 
  Serial.printf("%s\n",fileName);
  ret=myfile.open(fileName, O_RDWR | O_CREAT);
  if(!ret){
    Serial.printf("%s - open failed\n",fileName);
  }
  else{
    int i;
    size_t bytesWritten;
    digitalWrite(13, HIGH);
    for(i=0;i<32;i++){
      bytesWritten=myfile.write(sdBuffer,SD_BUFFER_SIZE);
      if(bytesWritten!=SD_BUFFER_SIZE){
        Serial.printf("file write return %d\n",bytesWritten);
        if(bytesWritten==0){
          Serial.printf("Error code = %d\n",myfile.getWriteError());
        }
      }
    }
    myfile.flush();
    ret=myfile.close();
    digitalWrite(13, LOW);
    if(!ret){
      Serial.printf("%s - close failed\n",fileName);
    }   
  } 
 
}
 

Attachments

  • main.cpp
    1.9 KB · Views: 49
Looks like you are not using SD library at all, but SDFat.

What version of Teensyduino are you using?

Also Where did SDFat come from... More specific. Is it a version that came with Teensyduino install, or did you install it some other way
(Library manager, git clone from github) If github, which Fork/Branch?

Some of the reasons I am asking this, is I think the latest SDFat in at least the Teensyduino 1.57 beta3 is 2.1.2 and the latest from
https://github.com/greiman/SdFat looks like 2.2.0 which looks like the current version shown in Library Manager.

My gut tells me that there may be some form of memory leak, but not sure where to look.
 
Looks like you are not using SD library at all, but SDFat.

What version of Teensyduino are you using?

Also Where did SDFat come from... More specific. Is it a version that came with Teensyduino install, or did you install it some other way
(Library manager, git clone from github) If github, which Fork/Branch?

Some of the reasons I am asking this, is I think the latest SDFat in at least the Teensyduino 1.57 beta3 is 2.1.2 and the latest from
https://github.com/greiman/SdFat looks like 2.2.0 which looks like the current version shown in Library Manager.

My gut tells me that there may be some form of memory leak, but not sure where to look.

I'm using VSCode+PlatformIO. This evening I'll try with a new fresh install of Arduino+Teensyduino in anothere laptop.
 
Make sure the SD library in the Arduino Libraries directory is deleted. If left in place it can be conflicting with the sd.h in the Teensy Libraries.
 
Back
Top