使用SPI接口初始化SD卡

时间:2010-05-17 16:20:44

标签: initialization response spi sd-card

我的SD卡(CMD8,CMD55和CMD41)的响应代码无效。

初始化程序:

SDCS = 1; // MMC deaktiviert
SPI1CON1bits.SMP = 0;
SPI1CON1bits.CKE = 1;
SPI1CON1bits.MSTEN = 1;
SPI1CON1bits.CKP = 0;
SPI1STATbits.SPIEN = 1;

for(i=0; i<10; i++)
    SPI(0xFF);

// RESET
unsigned char rr=Command(CMD0,0);
SDCS=1; // MMC deactivated
/* OK response == 1 */

r = Command(CMD8,0); // Check voltage
SDCS=1;
/*
  esponse ==  0xC1 ?!?
*/

r = Command(CMD58,0); // READ_OCR

unsigned char ocr1 = SPI(0xFF);
unsigned char ocr2 = SPI(0xFF);
unsigned char ocr3 = SPI(0xFF);
unsigned char ocr4 = SPI(0xFF);
unsigned char ocr5 = SPI(0xFF);

/*
  r = 0xF8; ?!?
  ocr1 = 0x0F;
  ocr2 = 0xFF;
  ocr3 = 0xFF;
  ocr4 = 0xFF;
  ocr5 = 0xFF;
*/

SDCS=1;

// INIT
unsigned char rrr = 0;
i = 10000;
do
{
    rrr = Command(55,0); // Next is APP CMD
    SDCS = 1;
    if(r)
        break;
} while(--i>0);

/*
OK response == 1
*/

// APP CMD 41 with OCR = 0x0F??

您可以在评论中阅读回复代码。

CMD8的响应代码是否可能是0xC1?第7位应为0,对吧?

是硬件错误吗?

1 个答案:

答案 0 :(得分:1)

解决方案可以在Stack Overflow问题 Initializing SD card in SPI issues 中找到。

简而言之:在每个命令(CMD0除外)之前,发送0xFF,直到获得0xFF作为回复。