Electrical – SD card responds with illegal command (0x04) when I send Write_Block (or Read_Block)

sdspi

I'm trying to roll my own SPI SD card management. So far, I can get the card initialised (details of that further down). Once initialised, when I send the Write_Block (CMD24) or Read_Single_Block (CMD17) commands, the SD card always returns 0x04 (illegal command) as an R1 response. Has anyone seen this before?

Some more detailed information about how I setup the card etc:

  • I send 74 empty bits to the card with CS high,
  • Send CMD0 with CRC 0x95 until I get 0x01 as a response,
  • Send CMD8, argument 0x1AA, with CRC 0x87, and get an R1 response of 0x01, R7 response of 0x1AA,
  • Send CMD55/ACMD41 until I get an R1 response of 0x00

(I've stepped through all these steps with a debugger to check that I'm getting the responses that I expect)

At this point I send CMD17 or CMD24 and get a response of 0x04. I've tried a few different arguments as addresses, 0x00, 0x01, 0x200, but all with the same result. Anyone got any ideas here?

Thanks in advance.

EDIT:
I had another kind of timing issue that caused this – my clock was relatively garbled (one very long pulse, and then 7 short ones for each byte), and that seems to have caused the problem (I'm not sure, since I'm still working through other issues). I'm surprised I managed to get the card to initialise at all.

Best Answer

As a last step of initialization you may want to explicitly set block length to 512 with CMD16 before trying to access the card for read or write.

Edit: as this quick possible fix did not work, let's make deeper troubleshooting:

  1. try another card, better several if you have, to see if any of them work; this way we may able to figure out if issue is related to card or its interoperability with your code;
  2. Check that you send correct CRC for the CMD17/CMD24 command. Illegal command is the response if CRC is wrong;
  3. After CMD17/CMD14 fails, issue CMD13 command to get R2 status - card may be locked or experience ECC errors;
  4. Read OCR register to see if card can operate at voltage you supply (in particular card's memory array, not just its SPI interface and its controller);
  5. I would also read CID and CSD to see what it contains for the card.