best practice for detecting SD card eject with sdfat T4.1?

TeensyWolf

Well-known member
What's the best way to determine when card gets abruptly ejected during logging?

Using TeensyDuino 1.55 with SDfat 2.0.5beta

I tried to use

Code:
void writeMyFileSD() {
  if (myFile.isWritable() ) { 
    if ( myFile.isOpen() ) {
      myFile.printf( "%s\n", myBuffer );
      myFile.flush();
    }
  }
}


I'm opening the file once and using myFile.flush()

But it doesn't seem to detect card is not there and blocks.

TIA
 
Last edited:
Good question!

Earlier on some of the MTP integration, I was playing with card insertions and deletions... Mostly insertions...

Some of it depended on which card reader. My guess you are using the built in one...

But for example with external SD device, like: https://www.sparkfun.com/products/544
There is an extra IO pins for card detect, so you can detect when a card is in place by checking the state of the IO pin. Ran into a few simple complications, like needed to debounce it like a button,
and also needed to check a few times for the card as the switch might detect a card before it's pads are actually making good electrical contact and/or before the card powered up enough and respond...

As for the built-in, the IMXRT (Rev 2RM page 1492) has a dedicated pin defined for that CD_B defined, but it is not brought out nor setup on the T4.x... However there is a another signal: DAT3 which is
IO pin 46... More details in RM, but at startup time I would detect that SD did not start, at which point, I set that pin into INPUT mode and would at times read that pin and detect when the card was inserted.

Did not get to the point of detecting removal, as that pin is used in SDIO mode as one of the 4 data pins...

Paul changed some of this later to be able to use some card information to do this a different way.

That is now with current Beta Teensyduino, the SD library has a method: bool MediaPresent()

Which I need to play with!

You might take a look
 
Good question!

Earlier on some of the MTP integration, I was playing with card insertions and deletions... Mostly insertions...

Some of it depended on which card reader. My guess you are using the built in one...

But for example with external SD device, like: https://www.sparkfun.com/products/544
There is an extra IO pins for card detect, so you can detect when a card is in place by checking the state of the IO pin. Ran into a few simple complications, like needed to debounce it like a button,
and also needed to check a few times for the card as the switch might detect a card before it's pads are actually making good electrical contact and/or before the card powered up enough and respond...

As for the built-in, the IMXRT (Rev 2RM page 1492) has a dedicated pin defined for that CD_B defined, but it is not brought out nor setup on the T4.x... However there is a another signal: DAT3 which is
IO pin 46... More details in RM, but at startup time I would detect that SD did not start, at which point, I set that pin into INPUT mode and would at times read that pin and detect when the card was inserted.

Did not get to the point of detecting removal, as that pin is used in SDIO mode as one of the 4 data pins...

Paul changed some of this later to be able to use some card information to do this a different way.

That is now with current Beta Teensyduino, the SD library has a method: bool MediaPresent()

Which I need to play with!

You might take a look

Thanks KurtE, I looked at it, was using SD.h, but it was proclaimed to be a thin wrapper for sdfat, so I moved over to that library. Perhaps I should move back.

The 2 options I found:
A. wire the sdcard switch to a GPIO, not very excited about this one.
B. Check for an existing file that should be on the card: https://github.com/kamocat/plog/issues/6, which works, but now it appears to be disrupting my program cadence and I'm dropping incoming data.
 
I may be wrong, but I think DAT3 works only if there is a external pullup connected.

The component in the T4.1 looks very much like this one: DM3D-SF (https://www.hirose.com/product/docu...menttype=Catalog&lang=en&documentid=D49662_en, page9)

From this thread, it appears one side of the SPST-NO is tied to ground. I verified on my T4.1 here.

The other side, near Pin 37, could be jumped to P37 fairly easily.

DAT3/CD - that's a curious option to explore. Page 12, note 3 of the https://www.sdcard.org/downloads/pl...mplified_Specification_Ver8.00.pdf&e=EN_SS1_8 says all you need to do is check to see if the DAT3/CD line is pulled high at powerup (50K internal to the SD card).

Not sure if this could be read before an SDcard write (set pin to input, pull up or down enable(?), read pin hi/lo, then set pin back to what it was)?
 
Back
Top