TL;DR: Is there a way, using only SPI commands, to tell if the SD card is in SD mode vs. SPI mode?
Edited TL;DR: Yes. If the SD card doesn't respond at all to your SPI commands, you know it's either disconnected or in SD mode. See my answer below.
I've gone down a debugging rabbit hole with some issues using a microSD card in SPI mode. Our SD card code works 99% of the time; we are just writing sequential blocks of memory using CMD24 as a sort of "black box" for our application, recording reams of data. But in some edge case scenarios, the SD card will respond to the CMD24 command with the R1 response value of 0x05, indicating the card is in idle mode and the command I sent was invalid.
To my knowledge, the only way to get the card back to Idle is either a CMD0 or a power cycle. It is not clear to me which of these the chip is experiencing. The only clear difference is that a power cycle would put the chip back into SD mode.
Is there a way, using only SPI commands, to tell if the SD card is in SD mode? Will the card even respond to SPI commands (besides CMD0) while in SD mode, or will it try to respond via the CMD line, which obviously is incompatible with SPI mode? Because if the latter, then the clear answer is that the card is receiving errant CMD0 commands. If the former, though, if the chip will respond to SPI commands in the Init state even if it is still in SD mode, then I have a harder debugging task ahead of me.
In response to a question, the primary document which I am referencing to find this knowledge is the "SD Specifications, Part 1, Physical Layer, Simplified Specification, Version 6.00" dated August 29, 2018. I've also done a ton of web searching and found no one else having this problem.
Obviously, there is at least one SPI command the SD card recognizes while in SD Mode, that being CMD0, which is used to tell the SD card to move to SD mode. The referenced document makes that plain. What it does not really make plain is what would happen if you send it any other SPI-based command while still in SD Mode; if there is some error message, or some expected behavior which an implementer can use to detect why the card has stopped behaving in an expected manner.
I'm basically trying to figure out how to tell if the SD card has "browned out", the only condition which would ever cause it to go BACK to SD Mode. How would I detect that this happened when I am only using SPI to communicate with it?
While digging into this question again, I noticed that there is a difference in how the SD card handles invalid commands while in SD Mode vs. SPI mode. In SPI mode, no matter whether the command you sent it valid or not, the SD card responds, usually with an R1 response (Physical Layer Simplified Specification Version 6.00, section 7.2.8). In SD Mode, however, it doesn't respond, and instead sets a register flag that has to be read via a separate command (ibid., section 4.6.1). This is further supported by this quote from section 7.2.1:
The SD Card is powered up in the SD mode. It will enter SPI mode if the CS signal is asserted (negative) during the reception of the reset command (CMD0). If the card recognizes that the SD mode is required it will not respond to the command and remain in SD mode. If the SPI mode is required, the card will switch to SPI and respond with the SPI mode R1 response.
So I guess the answer to my question is: If the SD card doesn't respond at all to your SPI commands, you know it's either disconnected or in SD mode.