title: Initializing SD card in SPI issues
body: <p>I've had a look at Stack Overflow question <em><a href="https://stackoverflow.com/questions/955587">Initialization of a microSD card using an SPI interface</a></em> and didn't see any answers that matched my issue (that is, things I haven't already tried). </p>
  
  <p>I have a similar issue where I'm trying to access a SD card through a microcontroller's SPI interface (specifically an <a href="http://www.physics.mcmaster.ca/tech/HC908/HC908Intro.htm" rel="nofollow noreferrer">HC908</a>). I've tried following the flow charts in the Physical Layer Simplified Specification v2.00 and it seems to initialize correctly on Transcend 1&nbsp;GB &amp; 2&nbsp;GB and an AE&amp;C 1&nbsp;GB card. But I'm having problems on three other random cards from my stash of old cards that I've used on my camera.</p>
  
  <p>My code is all HC908 assembler. I scoped out the SPI clock line and during initialization it's running about 350&nbsp;kHz (the only speed multiplier that the HC908 supplies at my low MCU clock speed that falls within the 100 - 400&nbsp;kHz window).</p>
  
  <p>Here are the results of the three cards that aren't completing my initialization routine (all done consecutively without changing any code or timing parameters):</p>
  
  <pre><code>Canon 16Meg card (labeled as SD):
  Set card select high
  Send 80 SPI clock cycles (done by writing 0xFF 10 times)
  Set card select low
  Send CMD0 [0x400000000095] and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x01 (indicates idle)
  Send CMD8 [0x48000001AA87] and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x05 (idle and illegal command)
  Because illegal command set local flag to indicate v1 or MMC card
  Send CMD58 [0x7A00000000FD] and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x05 (idle and illegal command)
  because illegal command branch to error routine
  Send CMD13 [0x4D000000000D] (show status buffer) and Loop up to 8 times waiting for high bit on response to go low
  R1= 0x05 (idle and illegal command)
  </code></pre>
  
  <p>Is the illegal command flag stuck? Should I be doing something after CMD8 to clear that flag?</p>
  
  <pre><code>SanDisk UltraII 256Meg
  Set card select high
  Send 80 SPI clock cycles (done by writing 0xFF 10 times)
  Set card select low
  Send CMD0 [0x400000000095] and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x01 (idle)
  Send CMD8 [0x48000001AA87] and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x05 (idle and illegal command)
  Because illegal command set local flag to indicate v1 or MMC card
  Send CMD58 [0x7A00000000FD] and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x01 (idle)
  Send 0xFF 4 times to read OCR
  OCR = 0xFFFFFFFF
  Send CMD55 [0x770000000065] (1st part of ACMD41) and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x01 (idle)
  Send CMD41 [0x6900000000E5] (2nd part of ACMD41) and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x05 (idle and illegal command)
  Because illegal command, assume card is MMC
  Send CMD1 [0x4100000000F9] (for MMC) and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x05 (idle and illegal command)
  Repeat the CMD1 50 times (my arbitrary number to wait until idle clears)
  Every R1 response is 0x05 (idle and illegal command)
  </code></pre>
  
  <p>Why is OCR all F? Doesn't seem proper at all. Also, why does ACMD41 and CMD1 respond illegal command? Is CMD1 failing because the card is waiting for a valid ACMD after the CMD55 even with the illegal command response?</p>
  
  <pre><code>SanDisk ExtremeIII 2G:
  Set card select high
  Send 80 SPI clock cycles (done by writing 0xFF 10 times)
  Set card select low
  Send CMD0 [0x400000000095] and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x01 (idle)
  Send CMD8 [0x40000001AA87] and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x7F (??? My loop shows the responses for each iteration and I got 0xFF 0xFF 0xC1 0x7F... is the card getting out of sync?)
  Send CMD58 [0x7A00000000FD] and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x01 (idle and back in sync)
  Send 0xFF 4 times to read OCR
  OCR = 0x00FF80
  Send CMD55 [0x770000000065] (1st part of ACMD41) and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x5F (??? loop responses are 0xFF 0xFF 0xF0 0x5F... again out of sync?)
  Send CMD41 [0x6900000000E5] (2nd part of ACMD41) and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x05 (idle and illegal command, but back in sync???)
  Because illegal command, assume card is MMC
  Send CMD1 [0x4100000000F9] (for MMC) and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x7F (??? loop responses are 0xFF 0xFF 0xC1 0x7F... again out of sync?)
  Repeat CMD1 and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x01 (idle)
  Repeat CMD1 and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x7F (??? loop responses are 0xFF 0xFF 0xC1 0x7F... again out of sync?)
  Repeat CMD1 and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x00 (out of idle)
  Send CMD9 [0x4900000000AF] (get CSD) and Loop up to 8 times waiting for high bit on response to go low
  R1 = 0x3F (??? loop responses are 0xFF 0xFF 0xC1 0x3F... again out of sync?)
  Code craps out because Illegal command bit is high.
  </code></pre>
  
  <p>What on Earth is wrong with that card? </p>
  
  <p>Sometimes it is in sync, other times not. (The above pattern <em>is</em> repeatable.) I've scoped this one out and I'm not seeing any rogue clock cycles going through between MOSI/MISO transfers.</p>