Teensy 4.0 SD Buggy Implementation

Status
Not open for further replies.

jimmie

Well-known member
I have been having lots of problems with my SD card attachment which is connected to a Teensy 4.0 along with a WIZ850IO.

I have followed all the guidelines regarding setup. I have also tried different SD cards. Using the SD library, the card would only be recognized about half the time.

After I gave up on the Teensy's SD library, I switched to the SDFat-Beta library. In this case, the card is recognized 100% of the time.

However, after writing a couple of relatively small files (20kb), the card stops writing and the filesystem is corrupted with very large files (1 MB).

Are there any guidelines to avoid this? I have spent hours trying to resolve this but no luck.

My setup code is below. Thanks in advance for any help.


Code:
#include <SPI.h>
#include "SdFat.h"
SdFat SD;

File myFile;
const int chipSelect = 4;   // Wiz820+SD board: pin 4

void setup()
{
  //------------------------------------------------------------
  //https://www.pjrc.com/store/wiz820_sd_adaptor.html#init
  //https://github.com/PaulStoffregen/Ethernet

  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);    // begin reset the WIZ820io
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);  // de-select WIZ820io
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);   // de-select the SD Card
  digitalWrite(9, HIGH);   // end reset pulse

  //------------------------------------------------------------
  int SDFail = 0;
  if (!SD.begin(chipSelect))
  {
    if (SD.card()->errorCode())
    {
      Serial.println("   SD initialization failed!");
      SDFail = 1;
      //return;
    }
  }
  if (SDFail == 0)  Serial.println(">>>   SD SUCCESS.");
   //------------------------------------------------------------
   for (int i=0; i <= 4; i++){
      write2SD();
      delay(1000);
   } 
}

void write2SD()
{
  String tempStuff = String(truckID) + ".scn";
  myFile = SD.open(tempStuff.c_str(), FILE_WRITE);

  if (myFile)
  {
    myFile.print(triggerTime[1]);
    myFile.print("  ");
    myFile.println(triggerTime[0]);


    for (int SN = 0; SN < 3; SN++)           
    {
      for (int j = 1; j <= scan[SN] - 1; j++)
      {
        myFile.println("   ");
        tempStuff =  "Scan: " + String(j) ;
        myFile.println(tempStuff.c_str());
        for (int k = 0; k < 16; k++)
        {
          tempStuff =  String(k) + "," + String(BUFFER[SN][j][k][0]) + "," + String(BUFFER[SN][j][k][1]);
          myFile.println(tempStuff.c_str());
        }    
      }    
    }    

    myFile.close();
    Serial.println("SD File Closed");
  }
  else
  {
    Serial.println("ERROR: Cannot open SD File!");
  }
}
 
I have isolated the reason to the W5500 chip.

When I did not use Ethernet.begin, the SD card worked 100% of the time.

When I executed the following statements before writing to the SD card, they did not help. I still got file corruption.

Code:
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);

I have tried both the Ethernet and the Ethernet2 libraries.

Can one of the more experienced members help? I am out of options ...

Thanks in advance.
 
I would like to investigate this problem.

Can you give me a complete program, using only the libraries Teensyduino installs (SD, not SdFat), which reliably reproduces the issue?

If you've started other threads on this forum about this issue, please consider linking to them from this thread, and linking from those others to this one. Duplicate threads may seem like a good idea to draw more attention to your issue, but ultimately it's a self-defecting strategy since it dilutes attention. Please, show all the info here and try to resist the urge to start more threads.
 
Thank you very much Paul and apologies for the second post. I will research how to link those posts.

I will also edit the code to use the SD library and will post it here per your request.
 
Hello Paul,

I have taken out all the extraneous parts of my code and was able to duplicate the problem. I have also attached the directory image and a trace showing the files being saved to the SD. It works a few times before the problem occurs.

BTW, sometimes the code works so please try it a couple of times ....
 

Attachments

  • SDTest.zip
    140.3 KB · Views: 95
Even if you comment out the Ethernet code, the problem occurs but this time after writing about 30 files ....

With the Ethernet code, it crashes after a few files are written.
 
Any chance you could trim this code down to a smaller program which does not require the U8g2lib.h library (not one of the libs provided by Teensyduino) and a display?

Is it possible to reproduce this problem with only a Teensy 4.0, the WIZ820+SD adaptor board, a WIZ850io, and a SD card? Or does it depend on the presence of a display and other hardware?
 
Hello Paul,

The display is not needed. I had not taken it out thinking that an i2c display will not affect the SPI peripherals.

When I stripped the display code, the same problem persists. However, it happens now after writing a relatively larger number of files (> 50 files). So still, eventually, the card still stops writing.

The code (sans display) is attached.

Thanks again.
 

Attachments

  • SDTest.ino
    2.9 KB · Views: 93
  • SDStuff.ino
    2.1 KB · Views: 136
Good Evening Paul,

Any luck finding out what is causing the SD problems? Per your request, I cleaned the code and deleted all references to non-Teensy libraries but I am still having the same problem per my September 4 post above.

Thanks again for all your time and help.
 
jimmie said:
Hello @mjs513:

Thank you for responding as this problem has been extremely frustrating to me. I am glad to hear that the SD card is working.

Because my Wiznet is soldered to the SD attachment, it is not possible for me to separate the two but I can build another rig that does not have the Ethernet attachment and test.

I was under the impression though that since I commented out the Ethernet code, that the SD circuitry should not be affected? And indeed, the system lasts a lot longer without Ethernet but eventually crashes.

So is it possible that the Ethernet electronics would interfere even if it is not initialized in software? I do not have enough experience .....

Is there a workaround as I need both devices in my project ....

Thanks again for your help.
Posting back here since I don't want to hijack the other thread.

Not sure how much I can help since I don't have the WIZ820io & Micro SD Card Adaptor that you are talking about. But in going through https://www.pjrc.com/store/wiz820_sd_adaptor.html I did notice the one comment about the rev 2 board:
These resistors keep the SD card and WIZ820io/WIZ850io from responding to other SPI communication. Some WIZ820io can disrupt SPI communication before being reset, even of their chip select pin is pulled high. The new reset chip guarantees proper reset at startup, even if the recommended code below is not used.
Not sure what rev you have. You might try adding pull-up resistors to the CS lines.

As another test with just the SD Card you might just try adding:
Code:
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);  // de-select WIZ820io
and retest with just the SD Card.

Just trying to give you a couple of things to try. Without the hardware tough to figure it out.
 
Thank you very much @mjs513.

I am using the WIZ850 (not 820). I am also using Rev.2 (latest) of the SD board.

Also, in all my testing, I have pulled pin 10 high. So it looks like there is something else .....

I will need to build another rig (without Ethernet) to test SD without Ethernet circuitry. I will also see if adding a pull-up resistor on the CS line will help. BTW, would value would you recommend?

Thanks again.
 
Rev 2 board looks like there are 10k pullups on pins 4, 9 and 10 already.

To be honest never really never saw a recommended value. Have seen 10K and 100k.

Another thing is that @KurtE pushed a change to the SPI library a couple of days ago to change the drive strength on the MISO line for the T4. Maybe grab that change - its a one liner. That seemed to fix a few problems.

Trying SD Card by itself might be a good start. Can you post your test sketch for the SD Card only - I can give that a try and see what happens.
 
Thank you @mjs513. The SPI library change is potentially great news. I will try the new library first. Can you tell me where does one download it from? The SPI library referenced in the PJRC website is 2 years old ...

The code I am planning to use in testing are the two files SDTest.ino, and SDStuff.ino which are included in a few posts above this one (in my reply to Paul).

Thanks again.

========================================================

Thank you, I found the new SPI library:

https://github.com/PaulStoffregen/SPI
 
Last edited:
I downloaded the above files. I don't have the WIZ850IO so I disabled the section, from about line 83 - 98 of SDTest.ino and change the SD CS for the T4 breakout.

I ran SD.h for about 300 files with no errors and the SD had no errors when I checked with Window 10.

I replaced the include for SD.h to use SdFat-beta.

Code:
#include "SdFat.h"
SdFat SD;

I ran the test for many hundreds of files with no problems.

Could you use SdFat-beta on your hardware with the following config changes.

In src/SdFatConfig.h at about line 156 set USE_SD_CRC to one.
Code:
#define USE_SD_CRC 1

In src/common/DebugMacros.h at about line 28 set USE_DBG_MACROS

Code:
#define USE_DBG_MACROS 1

You should get messages like this for failures.

Code:
DBG_FAIL: FatPartition.cpp.57
DBG_FAIL: FatFile.cpp.1149
 
Thank you Bill.

I will makes these changes and try as soon as I return from my trip on Saturday.

Both you and mjs513 have ran the SD code (but without the Ethernet hardware) without any problems so it appears the culprit could be the SPI library which has been changed 4 days ago. So I will also try that.

On the SDFat-beta library, actually, I had already tried it before I wrote my post. The SDFat-Beta library ran much better in my testing than the SD library. The code ALWAYS found the SD card while it failed quite often with the regular SD library. Also, more writes were successfully completed with your library. That said, the file writes crashed after about 60 loops.

So in summary, the SPI library was just recently changed, and the recommended code changes in your library will hopefully resolve this problem.

I will update as soon as I return from my trip.
 
If you get an error print please add these additional changes.

In SDTest.ino add this routine:

Code:
void dbgSdError() {
  if (SD.sdErrorCode()) {
    SD.errorPrint(&Serial);
    while (true) {}
  }  
}

In DebugMacros.h add these two lines:

Code:
#endif  // DBG_FILE
void dbgSdError();  <<------------------ADD
static void dbgPrint(uint16_t line) {
  Serial.print(F("DBG_FAIL: "));
  Serial.print(F(DBG_FILE));
  Serial.write('.');
  Serial.println(line);
  dbgSdError();  <<------------------ADD  
}

This will stop the program if you get an SD hardware error caused by an SPI problem.

Here is example output when I pulled the SD card during a run:

Code:
>>> SD File Closed
1004.scn
DBG_FAIL: FatPartition.cpp.39
SdError: 0X18,0X0

You can run the SdErrorCodes example to get a current list of codes. Here is 0X18 from the list:
Code:
0X18,SD_CARD_ERROR_READ_TOKEN - Bad read data token
 
I downloaded the above files. I don't have the WIZ850IO so I disabled the section, from about line 83 - 98 of SDTest.ino and change the SD CS for the T4 breakout.

I ran SD.h for about 300 files with no errors and the SD had no errors when I checked with Window 10.

I replaced the include for SD.h to use SdFat-beta.

Code:
#include "SdFat.h"
SdFat SD;

I ran the test for many hundreds of files with no problems.

Could you use SdFat-beta on your hardware with the following config changes.

In src/SdFatConfig.h at about line 156 set USE_SD_CRC to one.
Code:
#define USE_SD_CRC 1

In src/common/DebugMacros.h at about line 28 set USE_DBG_MACROS

Code:
#define USE_DBG_MACROS 1

You should get messages like this for failures.

Code:
DBG_FAIL: FatPartition.cpp.57
DBG_FAIL: FatFile.cpp.1149

Offtopic: Bill is nice to hear from you. THX for sharing with us the great SdFat library, excuse the impertinence, do you have plans to update that great library, focused on Teensy 4.0?
 
Not meaning to put words in Bill's mouth, but my understanding is that the SDFat-beta is for supporting the T4.
 
Hello Bill,

Thank you very much. Your help was IMMENSE. I also wish to thank KurtE for the change he made in the SPI library because that solved the SD write problem.

I am happy to report that by using the new SD library, AND the WIZ850, everything worked the way it should with the SD card.

I then used the SDFat-beta library, and that also worked without any problems after (now) almost 500 writes.

So if you still want me to make the code changes in the SDFat-beta library files, I am happy to do it. Please let me know?
 
On the recommendation of Bill Greiman, I used the new SPI library which KurtE modified a few days ago and that solved the problem.
 
Hello Bill,

Thank you very much. Your help was IMMENSE. I also wish to thank KurtE for the change he made in the SPI library because that solved the SD write problem.

I am happy to report that by using the new SD library, AND the WIZ850, everything worked the way it should with the SD card.

I then used the SDFat-beta library, and that also worked without any problems after (now) almost 500 writes.

So if you still want me to make the code changes in the SDFat-beta library files, I am happy to do it. Please let me know?

No need to enable the debug prints unless it fails again.
 
Status
Not open for further replies.
Back
Top