0
\$\begingroup\$

I am using an STM32F0 to communicate with an SD card over SPI. However, when I send CMD0 after performing the required initialization, the SD card does not respond with anything on MISO. What's strange is that, originally, there was some response, but then I took out the SD card temporarily, and there's no longer any response. I have verified the card still works using a laptop, and I have also tried another SD card (both 32 GB). I am using this Adafruit breakout board, and I have a pullup resistor from MISO to 3.3 V. The following routine is what I use for initialization.

int sd_init(void) {
    int res;

    _spi_init();
    _pulse();
    res = _spi_sendCommand(0, 0); 
    if (res != 0x01) {
        ui_writeFormat(1, "%d", res);
        return SD_INIT_FAIL;
    }   

    return SD_OKAY;
}

static void _pulse(void) {
    int i;

    //send at least 74 (80) clock pulses following power on
    //NSS remains high
    for (i = 0; i < 10; i++) {
        _spi_sendByte(0xFF);
    }
}

static uint8_t _spi_sendCommand(uint8_t idx, uint32_t arg) {
    uint8_t resp;

    _spi_sendByte(0xFF); //these are optional, tried using them after finding them online
    CLR_CS;
    _spi_sendByte(0xFF);

    _spi_sendByte(idx | 0x40); //send index, prefixed with '01'
    _spi_sendWord(arg); //send argument
    if (idx == 0) {
        _spi_sendByte(0x95);
    } else {
        _spi_sendByte(0x00); //send CRC (N/A)
    }
    while (!(SPI2->SR & SPI_SR_RXNE)); //wait for byte to be received
    resp = (uint8_t)SPI2->DR;

    _spi_sendByte(0xFF);
    SET_CS;
    _spi_sendByte(0xFF);

    return resp;
}

I believe I'm performing the initialization procedure correctly, but I can't get it to work at all. My SPI clock phase and polarity are both 0 (rising edge, active high), although I also tried switching those. My baud rate is 375 kHz, but I also tried lower ones. I am using an oscilloscope to verify that I am not receiving anything, and it shows MISO as always high (due to the pullup). I've tried repeating CMD0 ad infinitum with no success. I am using the 5 V rail on the STM32F0DISCOVERY board as power for the SD, stepped down to 3.3 V on the breakout; I think this should provide sufficient current, although I could be wrong. Can anyone see what exactly I'm missing here?

\$\endgroup\$
  • \$\begingroup\$ I actually had this same issue in school. The FAT32 software provided by ST does not work with SPI, only SDIO. Unfortunately, the level translation circuit on the Adafruit breakout makes it impossible to use the native SD Bus (which uses bidirectional data pins). I would recommend getting a different breakout, such as this one from Sparkfun. \$\endgroup\$ – Caleb Reister Sep 11 at 2:39
  • \$\begingroup\$ The other thing to keep in mind is that SD cards tend to require very clean signals. Try to keep any breadboard jumpers short. \$\endgroup\$ – Caleb Reister Sep 11 at 2:41
  • \$\begingroup\$ Do ensure your SPI lines are not Inadvertently connected elsewhere. E. G. to your programmer/other device. \$\endgroup\$ – Soldersmoke Sep 11 at 8:03
  • \$\begingroup\$ Yes @CalebReister \$\endgroup\$ – Chris Loonam Sep 11 at 11:38
  • \$\begingroup\$ @CalebReister not really. 40cm wires 24MHz no prob at all. Tested yesterday as I was writing the SDIO driver. \$\endgroup\$ – P__J__ Sep 12 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.