new SdFat library beta - sensing card detect and write protect

Sorry if this is simple, but i looked through the docs and didn;t see it.

is there a way to check to see if (1) there is a card in the SD socket (card detect) and (2) id the card has its write protect switch on?


Please modify the title of the thread to reflect your question:

is there a way to check to see if (1) there is a card in the SD socket (card detect) and (2) id the card has its write protect switch on?

(1) The card detection requires an IO line which is default not connected, so I expect it is not supported in the library. You have to look at the schematics of your SDcard and solder a wire to some fee pin and the GND or so. You might connect it to an interrupt pin of the arduino and write code to handle the insert and remove.

(2) Don't know but I think the write will fail with a specific code if writeprotected or full. It should be not to difficult to write a function that checks writeprotection. Try to create a dummy file on SD and delete it if succesfull. In pseudo code this would involve something like :

boolean SDReadProtected()
  if createFile fails return true;  // or a directory is also possible...
  return false;

Drawback is it takes some extra time but you need to call it only once...

The library will definitely write to a write protected card. I can directly check an IO pin if the library has not already grabbed it.

Thanks for the reply.

I do suppose you mean "will definitely try to write to a write protected card"? My guess is that it'll return a different error code, as for example, a not available card. That looks to be enough for sensing write protection. Or am I missing the point?

Actually I meant what I said. If i put a write protected card in, I can write to it. I can open files. I can delete files. All that stuff. I did npot se a method in the class library structure to check the status of the write protect switch. So I asked.

Hmmm that's awkward!

I never tried it, but will do so later. I know the card holder has a write protect sense you could connect to the Arduino, but I was under the assumption that the card itself (or some cards?) would also block these operations. Now, if a host doesn't implement it, the write-protect doesn't really offer any protection at all!

The write protect tab is not electrically connected to the card. It can be sensed by most SD sockets and can be used to implement write protect in software. In practice it is not very useful with the Arduino since most apps write to files.

You could connect the socket write protect sensor to a digital pin and refuse to open files for write. It is probably better to sense write protect in your application and warn the user.

There is no standard way to detect write protect on SD shields so I did not implement it.

Card detect is not very useful either. Users expect to use it to implement card removal or card change.

You can't just remove a card since all files open for write must be closed or you will lose data or cause file system corruption. This means the user must signal the application to prepare for card removal. The application must the tell the user to change cards. At this point you could use card detect.

Sometimes the app tell the user when it wants to change the card. In any case most of the programming must be done in the app so card detect is best handled by the app.

Thanks! That explains :wink:

I use the card detect to decide if I should make logs. If there is no card, my app will merily go along its business without making logs. This is handy for my app.

I check the WP manually, and if it is asserted, I don't write logs, but I do read update info for my app from the card.

I expect most times a user puts a write protected card in, it will be a mistake. But the potential for data destruction if I ignore WP is not a risk i want to accept (and get complaints about).