0

To start off, I'll attach my code.

uint8_t SD_init(void)
{
    uint8_t res[5], cmdAttempts = 0;

        // Pulls CS high, waits 10ms, then sends 80 dummy bytes
        // all with the card de-selected
    SD_powerUpSeq();

        // SD_goIdleState() sends CMD0, argument, and CRC
        // Repeats until the result is 0x01 (idle state)
    while((res[0] = SD_goIdleState()) != SD_IN_IDLE_STATE)
    {
        cmdAttempts++;
            // 255 attempts
        if(cmdAttempts == CMD0_MAX_ATTEMPTS)
        {
            return SD_ERROR;
        }
    }

    delayMs(1);
        // SD_sendIfCond() sends CMD8, argument, and CRC
    SD_sendIfCond(res);
        
        // checks if the first response is 0x01 (idle state)
    if(res[0] != SD_IN_IDLE_STATE)
    {
        return SD_ERROR;
    }

        // checks if echo is correct (I have never gotten stuck here)
    if(res[4] != 0xAA)
    {
        return SD_ERROR;
    }

    cmdAttempts = 0;
    do
    {           // 255 attempts
        if(cmdAttempts == CMD55_MAX_ATTEMPTS)
        {
            return SD_ERROR;
        }
                // sends CMD55 in preparation for ACMD41
                // First response is 0x01, then 0xFF on subsequent attempts
        res[0] = SD_sendApp();
                //#define SD_R1_NO_ERROR(X)   X < 0x02
        if(SD_R1_NO_ERROR(res[0]))
        {
            // Sends ACMD41
            // Always returns 0xFF (unless I step through it, odd)
            // (even if I do step through it, the card does not respond 
            // (response = 0xFF) to read/write commands afterwards)
            res[0] = SD_sendOpCond();
        }

        delayMs(1);

        cmdAttempts++;
    }
    while(res[0] != SD_READY);
        
    delayMs(1);
        // I never get to this point
    SD_readOCR(res);

    return SD_SUCCESS;
}

and just in case someone desires it:

#define CMD55               55         // (CMD|0x40) is sent to the card
#define CMD55_ARG           0x00000000
#define CMD55_CRC           0x65        
#define ACMD41              41
#define ACMD41_ARG          0x40000000
#define ACMD41_CRC          0x77

This is a 16 GB SDHC card. Initialization goes fine up until ACMD41, which consistently returns 0xFF, indicating that the card is doing absolutely nothing.

Attempted Fix #1: Send CMD1 after surpassing the allotted amount of ACMD41 attempts. Still 0xFF.

Attempted Fix #2: Step through the function.

This one is interesting, because when stepped through, the response does become 0x00. Only issue is, the card does not respond to any subsequent read/write commands.

Attempted Fix #3: Try different SD card.

This card is much older, 8GB, but I believe it is still SDHC (the label is very worn, and I'm having trouble finding any documentation on either of the cards). It does respond properly to the CMD55 and ACMD41 commands, and responds well to the read/write commands, other than the fact that it doesn't actually write anything. Changing the read address always returns the same data, and viewing the drive in Hex Workshop after a write reveals that nothing has actually changed. Other than that, response tokens are as they should be with the substituted card.

Many of the answers to SPI interfacing with SD cards on this site are quite old, so there is a possibility that something has changed over the years. If anyone knows anything, please let me know.

Newer, desired card output:

Sending CMD0....
Sucess! Response: 01
Sending CMD8....
Sucess! Response: 01000001AA
Sending CMD55....
Response: 01
Sending ACMD41....
Response: FF
Sending CMD55....
Response: FF
Sending CMD55....
Response: FF
Sending CMD55....
Response: FF
Sending CMD55....
Response: FF
Sending CMD55....
Response: FF

and so on.

Older, 8GB card output:

Sending CMD0....
Sucess! Response: 01
Sending CMD8....
Sucess! Response: 01000001AA
Sending CMD55....
Response: 01
Sending ACMD41....
Response: 01
Sending CMD55....
Response: 01
Sending ACMD41....
Response: 01
Sending CMD55....
Response: 01
Sending ACMD41....
Response: 01
Sending CMD55....
Response: 01
Sending ACMD41....
Response: 01

This repeats until the time-out value.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
cwjensen
  • 1
  • 2
  • What is the platform setup do you have? If it's a Linux kernel, there is already a driver. If it's a micro-controller, then yeah, you probably can google for the existing solutions. But first of all, ensure that your card supports SPI protocol. Note, not all of them are doing that. – 0andriy Apr 16 '23 at 20:54

0 Answers0