1
\$\begingroup\$

I am trying to interface a 8GB SDHC with STM32L0 controller through SPI.

Followed this tutorial.

As this thread suggests, I followed the the Command Sequence as follows:

  1. CMD0 arg: 0x0, CRC: 0x95 (response: 0x01)
  2. CMD8 arg: 0x000001AA, CRC: 0x87 (response: 0x01)

I am sending some clocks with CS Activated and Deactivated in between these commands as discussed here.

Somehow, I get the response from CMD0 as 0x01 but CMD8 always responds with 0xFF.

Following is the code I am using to init my SD card:

void SD_INIT(void){
    CS_HIGH;
    for(count = 0; count < 8; count++){
            SPI_SEND_CMD_DUMMY();
    }
    CS_LOW;
    for(count = 0; count < 8; count++){
            SPI_SEND_CMD_DUMMY();
    }

    uint8_t txBuff[] = {0x40,0x00, 0x00, 0x00, 0x00, 0x95 };
    HAL_SPI_TransmitReceive(&hspi2,txBuff,readBuff,6,100);

    //count = 255;   //Try 255 times..
    for(count = 255;count>0;count--){
            uint8_t dummy[] = {0xFF};
            HAL_SPI_TransmitReceive(&hspi2,dummy,readBuff,1,100);
            if(readBuff[0] == 0x01){
                    status = 1;
                    break;
        }

    }

    for(count = 0; count < 8; count++){
            SPI_SEND_CMD_DUMMY();
    }
    CS_HIGH;
    for(count = 0; count < 8; count++){
            SPI_SEND_CMD_DUMMY();
    }


/*-------------------SDHC TRY----------------*/

    if(status==1){

    //status=0;
    CS_LOW;
    for(count = 0; count < 8; count++){
            SPI_SEND_CMD_DUMMY();
    }
        //HIGH CAPACITY 0x000001AA 
    uint8_t tx1Buff[] = {0x48,0x00, 0x00, 0x01, 0xAA, 0x87 };
    HAL_SPI_TransmitReceive(&hspi2,tx1Buff,readTempBuff,6,100);

    //count = 255;   //Try 255 times.. //10ms = 160000
    for(count = 100;count>0;count--){
            uint8_t dummy[] = {0xFF};
            HAL_SPI_TransmitReceive(&hspi2,dummy,readTempBuff,1,100);
            if(readTempBuff[0] == 0x05){  //Invalid Command
                    status = 3;
                    break;
        }
    }

    for(count = 0; count < 8; count++){
            SPI_SEND_CMD_DUMMY();
    }
    CS_HIGH;
    for(count = 0; count < 8; count++){
            SPI_SEND_CMD_DUMMY();
    }
    }

void SPI_SEND_CMD_DUMMY(void){

        uint8_t txBuff = 0xFF;
        HAL_SPI_Transmit(&hspi2,&txBuff,1,100);
}

The response I am getting in readTempBuff is:

readTempBuff[0]  :  0xFF
readTempBuff[1]  :  0xFF
readTempBuff[2]  :  0xFF
readTempBuff[3]  :  0xFF
readTempBuff[4]  :  0xFF
\$\endgroup\$
  • \$\begingroup\$ You are waiting for a response of 0x05 (Illegal command) for CMD8, that wouldn't work if the card accepted it and returned an OK R1 (with Idle set). You also need to read in the complete R7 response, otherwise there are cards which will just deadlock there ( electronics.stackexchange.com/questions/321491/… ). \$\endgroup\$ – Jubatian Nov 16 '17 at 12:55
1
\$\begingroup\$

for(count = 100;count>0;count--){

That is only 100 response bytes, my code uses 1000 for every SD command. Some cards may need the extra time/clocks.

Note that IIRC you also need to process the R7 response of the CMD8.

SDHC card code is non-trivial, but there are a lot of open source implementations to learn from.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.