问题是,使用SPI2初始化Micro SD卡的时候,可以复位成功,但是使用CMD8读取卡类型的时候,返回值有错误始终不正确,及时不检测卡类型直接进行CMD55+ACMD41,返回值也出错;请大家帮忙找找原因,卡用STM32读写正常;程序如下:uint32_t SD_SPI_WRITEREAD_CMD(uint32_t com_data)
{
uint32_t au32SourceData;
uint32_t au32DestinationData;
au32SourceData=com_data;
SPI_SingleWrite(SPI2, &au32SourceData);
while (SPI_IsBusy(SPI2)); //等
SPI_DumpRxRegister(SPI2, &au32DestinationData, 1);
return au32DestinationData;
}
uint8_t SD_SendCommand(uint8_t cmd, uint32_t arg, uint8_t crc)
{
uint32_t au32DestinationData;
uint8_t out_delay;
SPI_ClrSS(SPI2,SPI_SS0);
SD_SPI_WRITEREAD_CMD(0XFF);
SPI_SetSS(SPI2,SPI_SS0);
SD_SPI_WRITEREAD_CMD(cmd|0x40);
SPI_SetBitLength(SPI2, 32);
SD_SPI_WRITEREAD_CMD(arg);
SPI_SetBitLength(SPI2, 8);
SD_SPI_WRITEREAD_CMD(crc);
if(cmd==CMD12) SD_SPI_WRITEREAD_CMD(0xff);
out_delay=0;
while((out_delay<200)&&(au32DestinationData==0xff))
{
au32DestinationData=SD_SPI_WRITEREAD_CMD(0xff);
out_delay++;
}
return au32DestinationData;
}
uint8_t SD_Idle_Sta(void)
{
uint16_t i;
uint32_t au32SourceData;
uint8_t au8DestinationData;
SPI_ClrSS(SPI2,SPI_SS0);
au32SourceData=0xff;
for(i=0;i<10;i++)
{
SPI_SingleWrite(SPI2, &au32SourceData);
}
for(i=0;;i++)
{
au8DestinationData=SD_SendCommand(CMD0,0x0000,0x95);
if(au8DestinationData==0x01)
return 0; //成功
else if(i>=200)
return 1; //失败
}
}
uint8_t SDCARD_Open(void) //SPI开始
{
uint32_t au32DestinationData;
uint16_t out_delay;
sd_spi2_init();
if(SD_Idle_Sta()==0) //SD卡复位成功
{
SPI_ClrSS(SPI2,SPI_SS0);
SD_SPI_WRITEREAD_CMD(0XFF);
//读取卡信息;以便判断卡类型;
SD_TYPE=0;
SPI_SetSS(SPI2,SPI_SS0);
au32DestinationData=SD_SendCommand(CMD8,0X1AA,0X87);
/************************/
//判断卡类型
if(au32DestinationData==0x05)
{
SD_TYPE= SD_TYPE_V1;
//设置卡类型为SD卡1.0,如果后面检测到为MMC卡,从新修改;
out_delay=0;
//发卡初始化命令CMD55+ACMD41,应答正确,是SD卡,否则为MMC卡
while(au32DestinationData!=0) //一直发送200次,直到返回0
{
au32DestinationData=SD_SendCommand(CMD55,0,0);
au32DestinationData=SD_SendCommand(ACMD41,0,0);
out_delay++;
if(out_delay>=200)
return 1; //错误
}
if(out_delay==200)
{
out_delay=0;
while(au32DestinationData!=0) //一直发送200次,直到返回0
{
au32DestinationData=SD_SendCommand(CMD1,0,0);
out_delay++;
if(out_delay>=200)
return 1; //错误
}
SD_TYPE= SD_TYPE_MMC;
}
}
}
SPI_ClrSS(SPI2,SPI_SS0); //退出IDLE
SD_SPI_WRITEREAD_CMD(0XFF);
return 0;
}
|