最近接受一个项目,用的是MSP4305438A sd卡初始化时 程序一直卡死在SD_WaitReady(void)函数中的i = SD_SPI_ReadWriteByte(0XFF)语句中,也就是说一直收不到SPI的应答。函数代码如下:
//等待卡准备好
//返回值:0,准备好了;其他,错误代码
u8 SD_WaitReady(void)
{
u32 t=0;
u8 i = 0;
do
{
i = SD_SPI_ReadWriteByte(0XFF);
if(i==0XFF)
return 0;//OK
t++;
}while(t<0XFFFFFF);//等待
return 1;
}
硬件上测试过SD卡供电电压是3.05V 且初始化的74个脉冲正常发送出去了。希望做过这块的朋友们给点思路。
Uint8 SD_Init(void)
{
Uint8 time=0;
temp = 0;
Uint8 Rev_Data[4]={0};
Uint8 pcmd8[] = {0x48,0x00,0x00,0x01,0xAA,0x87}; //CMD8
Uint8 pcmd55[] = {0x77,0x00,0x00,0x00,0x00,0x00}; //CMD55
Uint8 pcmd41[] = {0x69,0x40,0x00,0x00,0x00,0x00}; //CMD41
Uint8 pcmd58[] = {0x7A,0x00,0x00,0x00,0x00,0x00}; //CMD58
P9OUT &= ~BIT0; //打开片选
temp = SD_WriteCmd(pcmd8); //读取卡片版本
if(temp==0x05)
{
SD_Type =SDV1;
//设置卡类型为SD V1.0,如果是V1.0卡,CM8指令后没有后续数据
P9OUT |= BIT0;
SD_ReadWriteByte(0xff); //多发8时钟
P9OUT &= ~BIT0;
do
{
temp = SD_WriteCmd(pcmd55);
if(temp!=0x01) return temp;
temp = SD_WriteCmd(pcmd41);
time++;
}while((temp!= 0x00)&& (time<200));
P9OUT |= BIT0;
}// if(temp==0x05)
else if(temp == 0x01)
{
//V2.0卡,CM8命令后会传回4字节的数据,要跳过再结束本命令
Rev_Data[0] = SD_ReadWriteByte(0xff); //返回0x00
Rev_Data[1] = SD_ReadWriteByte(0xff); //返回0x00
Rev_Data[2] = SD_ReadWriteByte(0xff); //返回0x01
Rev_Data[3] = SD_ReadWriteByte(0xff); //返回0xAA
P9OUT |= BIT0;
SD_ReadWriteByte(0xff); //多发8时钟
P9OUT &= ~BIT0;
do
{
temp = SD_WriteCmd(pcmd55);
if(temp!=0x01) return temp;
temp = SD_WriteCmd(pcmd41);
time++;
if(time>250) return temp;
}while(temp!= 0x00);
//初始化指令发送完成,接下来获取OCR信息
/*------鉴别SD2.0卡版本--------------*/
temp=SD_WriteCmd(pcmd58);
if(temp != 0x00)return temp;
//读OCR指令发出后,紧接着是4字节的OCR信息
Rev_Data[0] = SD_ReadWriteByte(0xff);
Rev_Data[1] = SD_ReadWriteByte(0xff);
Rev_Data[2] = SD_ReadWriteByte(0xff);
Rev_Data[3] = SD_ReadWriteByte(0xff);
P9OUT |= BIT0;
SD_ReadWriteByte(0xff);
if(Rev_Data[0] & 0x40) SD_Type = SDHC;
else SD_Type =SDV2;
}// else if(temp == 0x01)
UCB2BR0 = 0x01; //SD为高速
UCB2BR1 = 0x00;
return 1;
一周热门 更多>