title: SD over SPI returns invalid response on CMD8
body: <p>I'm working on an SPI SD driver for an LPC4088 microcontroller. However, when I send <code>CMD8</code> (after <code>CMD0</code>) to detect if I'm working with a V2 or high capacity card, I get a weird response. Instead of getting a correct response( <code>0x01</code> for highest bits, <code>0x1AA</code> for the lower 12) or an error I get <code>0x00 00 00 02 1F</code>, which doesn't make any sense to me at all. </p>
  
  <p>Code I use for initialization:</p>
  
  <pre><code>bool initialize () {
      //0. Initialize SPI
      SPI::init();
  
      //1. Set clock to 400kHz
      SPI::set_clock(400000);
  
  
      //2. Assert CS signal (=0)
      assert_cs();
  
      // 3. Delay at least 74 clocks
      delay8(10);
      // 4. Deassert CS signal
      deassert_cs();
  
      // 5. Delay at least 16 clocks
      delay8(2);
  
      uint8_t argument[4];
      reset_argument(argument);
  
      // 6. Send CMD0 (reset, go to idle)
      if (!send_command(CMD::GO_IDLE_STATE, CMD_RESPONSE_SIZE::GO_IDLE_STATE, response, argument)) {
          return false;
      }
  
      // 7. Send CMD8
      bool version2;
      reset_argument(argument);
      pack_argument(argument, 0x1AA);
  
      if (!send_command(CMD::SEND_IF_COND, CMD_RESPONSE_SIZE::SEND_IF_COND, response, argument)) { 
          return false; 
      }
      if ((response[0] &amp; 0xFE) == 0x04) {
          //unknown command. This means we have version1
          version2 = false;
      } else if (response[0] &amp; 0xFE) {
          //other error, let's bail
           return false;
      } else {
          //Response, we're version2
          version2 = true;
          if (response[4] != 0xAA) {
              return false;
          }
      }
      //....
  }
  </code></pre>
  
  <p>send_command code:</p>
  
  <pre><code>    bool send_command(CMD::value cmd, uint8_t response_size, uint8_t *response, uint8_t *argument) {
  
          assert_cs();
          Crc7_SD crc;
          crc += cmd | 0x40;
          crc += argument[3] &amp; 0xFF;
          crc += argument[2] &amp; 0xFF;
          crc += argument[1] &amp; 0xFF;
          crc += argument[0] &amp; 0xFF;
  
          SPI::send(cmd | 0x40);
          SPI::send(argument[3] &amp; 0xFF);
          SPI::send(argument[2] &amp; 0xFF);
          SPI::send(argument[1] &amp; 0xFF);
          SPI::send(argument[0] &amp; 0xFF);
          SPI::send((crc &lt;&lt; 1) | 1);
  
          volatile uint8_t data;
          {
              unsigned int timeout = SD_CMD_TIMEOUT;
              do {
                  data = SPI::receive();
                  --timeout;
              } while(timeout &amp;&amp; (data &amp; 0x80));
              if (timeout == 0) {
                  deassert_cs();
                  return false;
              }
          }
  
          for (int i = 0; i &lt; response_size; i++) {
              //First byte is already read above
              if (response) {
                  response[i] = data;
              }
              data = SPI::receive();
  
          }
  
          deassert_cs();
  
          return true;
      }
  </code></pre>
  
  <p>To make sure that I didn't had an error in the SPI protocol I've verified the input and output with a logic analyzer. Result: <a href="http://i.imgur.com/rbS6WnC.png" rel="nofollow">CMD0</a> followed by <a href="http://i.imgur.com/CbtrUtR.png" rel="nofollow">CMD8</a>. It seems that I'm sending the correct commands, but still I get this weird response. </p>
  
  <p>Additional info about the setup:</p>
  
  <ul>
  <li><p>Microcontroller is an LPC4088</p></li>
  <li><p>Microcontroller is connected with <a href="http://www.embeddedartists.com/products/oem/lpc4088_oem.php" rel="nofollow">this OEM board</a></p></li>
  <li><p>The SD card module is connected to the serial expansion connector of the OEM board</p></li>
  <li><p>The logic analyzer is connected to the SD card module</p></li>
  <li><p>I've used 2 different versions of both the microcontroller and the OEM board to rule out that there is a hardware error in one of those. Unfortunately I don't have a second SD controller available.</p></li>
  <li><p>I've used a SanDisk Ultra SDHC 4GB Class 6 SD card and a Transcend SDHC 4GB Class 4 SD card, both gave exactly the same result.</p></li>
  </ul>
  
  <p>And last but not least, I have very little experience with embedded software, so it might just be some small stupid error. </p>