Teensy 3.2 SD print issues.

Status
Not open for further replies.

jim lee

Well-known member
Few odd issues.

A) I can't seem to be able to create a new file. This is intermittent. Sometimes the new file is created.
B) I can't write to an empty file using print(). This is NOT intermittent. Just can't do it.
C) If there is any data in the file? Everything seems to work fine.

This came up working on a library file for event logging. So I pulled out the offending part and set it up in its own .ino file to demonstrate the issue. Can anyone have a look?

Code:
#include "SD.h"


#define SD_CS     4
#define MAX_BYTES 200

char  dataBuff[MAX_BYTES+1];
int   i;

// These guys are here to emulate the stuff from the class this stuff was pulled from.
char mPath[] = "/logTest.txt";
bool getLogging() { return true; }
int mSeries = 1;


void setup() {

   i = 0;
   Serial.begin(9600);
   
   if (!SD.begin(SD_CS)) {
      Serial.println("NO SD CARD!");
      while(1);
   }
   Serial.println("Logging's up and running.");
}


bool addEvent(char* eventTxt) {
   
   File  logFile;
   bool  success;
   int   gSec = 99; // Also from the class.
   
   success = false;                                   // Not been a success yet.
   if (getLogging()) {                                // If we -are- logging..
      logFile = SD.open(mPath, FILE_WRITE);           // Lets try to open/create the logfile.
      if (logFile) {                                  // If we had success..

         Serial.print(mSeries);Serial.print('\t');
         Serial.print(gSec);Serial.print('\t');
         Serial.println(eventTxt);  
         
         logFile.print(mSeries);logFile.print('\t');  // First we print out the series number.
         logFile.print(gSec);logFile.print('\t');     // Next the number of seconds in this series.
         logFile.println(eventTxt);                   // And whatever their data ends up being.
         logFile.close();                             // Close the file. (Always leave files closed!)
         success = true;
      }
   }
   return success;
}


void loop() {

  char   inChar;
    
  if (Serial.available()) { 
      inChar = Serial.read();                       
      if (inChar=='\n') {
         dataBuff[i] = '\0';
         if (addEvent(dataBuff)) {
            Serial.print(dataBuff);Serial.println(", added to event log.");
         } else {
            Serial.println("No data added.");
         }
         i = 0;
      } else if (i<MAX_BYTES) {
         dataBuff[i] = inChar;
         i++;
      }
   }
}

There you go. Change the chip select to match your system, and it should compile and run.

Thanks millions!

-jim lee
 
I tried the supplied Teensy readwrite example and it behaves the same way. First time through it can't create the file. Subsequent times it can create it, but not write to it.

-jim lee
 
I've now tried the supplied Teensy readwrite example on two different sets of proven hardware, and the results look to be the same.

-jim lee
 
Have you tried other uSD cards? tried reformatting uSD card? what does SD -> examples -> Cardinfo report

what version of IDE and Teensyduino?

how is uSD attached to T3.2, photo?

works for me on 1.8.12 and 1.52. I ran T3.2 with audio shield and its uSD. I ran example ReadWrite twice
Code:
Initializing SD card...initialization done.
Writing to test.txt...done.
test.txt:
testing 1, 2, 3.
testing 1, 2, 3.
looks good


Cardinfo
Code:
Initializing SD card...Wiring is correct and a card is present.

Card type: SDHC

Volume type is FAT32

Volume size (Kbytes): 7630848
Volume size (Mbytes): 7452

Files found on the card (name, date and size in bytes): 
SDTEST1.WAV   2016-06-18 00:05:52 16787550
SDTEST2.WAV   2016-06-18 00:05:52 16425698
SDTEST3.WAV   2016-06-18 00:05:54 13617358
SDTEST4.WAV   2016-06-18 00:05:54 17173152
WORKSHOP.PDF  2016-06-18 00:05:56 8894873
TOPGUN.WAV    2017-03-17 09:32:14 4233644
DAVE.WAV      2017-06-09 20:48:46 617444
RNG.DAT       2000-01-01 01:00:00 1024000
TEST.TXT      2000-01-01 01:00:00 36


maybe add a while(!Serial); after Serial.begin(), with that i could see all the terminal output and your sketch worked for me
 
Last edited:
THANK YOU for testing this!

Both SD cards are mounted on Adafruit #1947 Cap Touchscreen shields.

My arduino is 1.8.12
My teensyduino is 1.51
I've tried 3 different cards all were in use by their respective Arduinos.

Code:
Initializing SD card...initialization done.
Writing to test.txt...done.
error opening test.txt

Then tried Cardinfo

Code:
Initializing SD card...initialization failed. Things to check:
* is a card inserted?
* is your wiring correct?
* did you change the chipSelect pin to match your shield or module?

Cycled the power..
Tried Card info..

Code:
Initializing SD card...Wiring is correct and a card is present.

Card type: SDHC

Volume type is FAT32

Volume size (Kbytes): 7753728
Volume size (Mbytes): 7572

Files found on the card (name, date and size in bytes): 
SPOTLI~1/     2020-05-20 23:57:56
  STORE-V2/     2020-05-20 23:57:56
    83A43F~1/     2020-05-20 23:57:56
      PSID.DB       2020-05-21 09:37:28 8192
      TM~1.SNO      2020-05-20 23:57:56 0
      TM~1.LIO      2020-05-20 23:57:56 0
      LIO~1.CRE     2020-05-20 23:57:56 0
      TMP.CAB       2020-05-20 23:57:56 0
      CA~1.CRE      2020-05-20 23:57:56 0
      INDEXS~1      2020-05-21 09:37:28 28
      ~1.IND        2020-05-20 23:58:10 2056
      ~~2.IND       2020-05-20 23:58:10 8
      ~~~3.IND      2020-05-21 09:18:18 64
      ~~~~4.IND     2020-05-20 23:58:32 4096
      ~1.SHA        2020-05-20 23:58:10 4096
      ~~~~~5.IND    2020-05-20 23:58:10 8
      ~~~~~~37.IND  2020-05-20 23:58:10 4
      ~~~~~~40.IND  2020-05-20 23:58:10 1
      ~1.DIR        2020-05-20 23:58:32 65536
      STOR~1.UPD    2020-05-21 09:37:28 8
      0DIREC~1.SHA  2020-05-20 23:58:10 1088
      ~~2.SHA       2020-05-20 23:58:10 1
      JOURNA~1.2    2020-05-21 09:37:28 140
      PERMST~1      2020-05-21 09:37:28 553
      ~~~~~~59.IND  2020-05-20 23:58:10 11
      ~~~~~~62.IND  2020-05-20 23:58:10 8
      ~~~~~~65.IND  2020-05-20 23:58:10 8
      LIVE~~68.IND  2020-05-21 09:37:28 811
      JOURNA~1.REP/ 2020-05-21 09:37:16

Ran readwrite again..

Code:
Initializing SD card...initialization failed!

Power cycled..

Code:
Initializing SD card...initialization done.
Writing to test.txt...done.
error opening test.txt

It seems to me that the hardware gets into a state it can't pull out of even on reboot. But power cycling clears it. It seems to be something to do with either creating a file or adding data to an empty file.

I'll try updating my teensyduino and see if that helps.

Thanks again for trying this stuff out. This is driving me bonkers.

-jim lee
 
I'll try updating my teensyduino and see if that helps.

Thanks again for trying this stuff out. This is driving me bonkers.

-jim lee

i'm guessin the adafruit board is corrupting the SPI signals some how. A scope might show something ...
 
Upgrading to Teensyduino 1.52 didn't help. The other change I did was to use ILI9341_t3.h instead of the usual library. I'll have to swap that back and see if it changes anything. If its a scope thing I'm sunk. Not really a wire guy.

Thanks again for testing this. It helps a LOT knowing that other systems work fine. I just have to locate what's different now.

-jim lee
 
GOT IT!

If you DON'T initialize the Adafruit ILI9341 screen. Even though your not using the screen. It subtilely messes up the SD card function. Init the screen drivers, and everything's fine.

Arrgh, what a twisted puzzle that was.

Thanks again!

-jim lee
 
GOT IT!

If you DON'T initialize the Adafruit ILI9341 screen. Even though your not using the screen. It subtilely messes up the SD card function. Init the screen drivers, and everything's fine.

Arrgh, what a twisted puzzle that was.

Thanks again!

-jim lee

Ahhh, shared SPI. You'd need to pull the display CS pin HIGH (digitalWrite) to keep the display's SPI "quiet" .... or as you noted, init the screen. Good job.
 
Thanks! And again, thank you! Finding that it worked on your machine kinda' tipped the scales on getting this figured out.

-jim lee
 
Status
Not open for further replies.
Back
Top