目前只有一个问题,就是擦除函数用了后其他函数就会卡住不能使用,擦除后加上延时也不行比如擦除后,再继续写,就会 卡死在 “连续读直到读到XXX0 0101表示写入成功”这里
//输入参数1 擦除的起始扇区地址
//输入参数2 擦除的结束扇区地址
//返回值 擦除扇区是否成功
uint8_t SD_erase_sector(uint32_t sector_start, uint32_t sector_stop)
{
CS1; //无效上一次片选
SPI1_ReadWriteByte(0xff);
if(SD_SendCmd(32,sector_start,0xff))
{
CS1; //无效片选
return ERASE_SECTOR_ERROR;
}
if(SD_SendCmd(33,sector_stop,0xff))
{
CS1; //无效片选
return ERASE_SECTOR_ERROR;
}
if(SD_SendCmd(38,0,0xff))
{
CS1; //无效片选
return ERASE_SECTOR_ERROR;
}
CS1; //无效片选
SPI1_ReadWriteByte(0xff);
printf("
擦除成功
");
return ERASE_SECTOR_SUCCESS;
}
//向SD中的一个扇区写数据
//输入参数1 写入扇区地址
//输入参数2 存储512个写入数据的数组
//输入参数3 写入扇区的个数
//返回值是写入成功还是不成功
uint8_t SD_write_sector_data(uint32_t sector ,const uint8_t data[], uint32_t count)
{
uint16_t i;
uint8_t response;
uint32_t j;
uint32_t add=0;
CS1; //无效片选
SPI1_ReadWriteByte(0xff); //片选无效之后,在发送8个时钟
if(count == 1) //单个扇区数据写入
{
//发送24命令,即写扇区命令
//正常响应应该为0x00
if(SD_SendCmd(24,sector,0xff))
{
CS1; //无效片选
return WRITE_SECTOR_DATA_ERROR;
}
SPI1_ReadWriteByte(0xff); //????????
//写数据起始令牌
SPI1_ReadWriteByte(0xfe);//发送数据块标志0xfe
for(i=0; i<512; i++)
{
SPI1_ReadWriteByte(data[add + i]);
}
//发送CRC码
SPI1_ReadWriteByte(0xff);
SPI1_ReadWriteByte(0xff);
for(i=0; ; i++) //连续读直到读到XXX0 0101表示写入成功
{
response = SPI1_ReadWriteByte(0xff);
if((response & 0x0f) == 0x05)
break;
if(i==100)
{
CS1; //无效片选
return WRITE_SECTOR_DATA_ERROR;
}
}
while(SPI1_ReadWriteByte(0xff) != 0xff);//读到0x00表示忙,等于0xff表示写入完成
}
else //多个扇区数据写入
{
//发送25命令,即写多个扇区命令
//正常响应应该为0x00
if(SD_SendCmd(25,sector,0xff))
{
CS1; //无效片选
return WRITE_SECTOR_DATA_ERROR;
}
for(j=1; ; j++)
{
SPI1_ReadWriteByte(0xff); //?????????
//写数据起始令牌
SPI1_ReadWriteByte(0xfc);//发送数据块标志0xfc
for(i=0; i<512; i++)
{
SPI1_ReadWriteByte(data[add + i]);
}
//发送CRC码
SPI1_ReadWriteByte(0xff);
SPI1_ReadWriteByte(0xff);
for(i=0; ; i++) //连续读直到读到XXX0 0101表示写入成功
{
response = SPI1_ReadWriteByte(0xff);
if((response & 0x0f) == 0x05)
break;
if(i==100)
{
CS1; //无效片选
return WRITE_SECTOR_DATA_ERROR;
}
}
while(SPI1_ReadWriteByte(0xff) != 0xff); //读到0x00表示忙,等于0xff表示写入完成
add += 512;
if(j == count) //多块写入如果完成
{
SPI1_ReadWriteByte(0xFD);
break;
}
}
}
CS1; //无效片选
SPI1_ReadWriteByte(0xff);
printf("
写入成功
");
return WRITE_SECTOR_SUCCESS;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>