我试图通过SPI将8GB SDHC与STM32L0控制器连接。正如这个帖子所暗示的那样,我遵循命令序列如下:- CMD0ARG:0x0,CRC: 0x95(响应:0x01)
- CMD8ARG:0x000001AA,CRC: 0x87(响应:0x01)
我送一些时钟与CS活化和讨论了这些命令之间去激活这里。不知怎的,我得到的响应CMD0是0x01,但CMD8总是与响应0xFF。以下是我用来初始化SD卡的代码: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);
}
我得到的回应readTempBuff是:- readTempBuff[0] : 0xFF
- readTempBuff[1] : 0xFF
- readTempBuff[2] : 0xFF
- readTempBuff[3] : 0xFF
- readTempBuff[4] : 0xFF
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
那只是100个响应字节,我的代码每个SD命令使用1000个。有些卡可能需要额外的时间/时钟。
请注意,IIRC还需要处理CMD8的R7响应。
SDHC卡代码并非易事,但有许多开源实现需要学习
一周热门 更多>