本帖最后由 Akada 于 2017-8-25 19:55 编辑
FATFS 写文件, f_open 成功,f_write 也显示成功,但是打开 SD 卡,文件内容却是空白?
main 函数部分代码如下:
[mw_shl_code=c,true]u8 senddata[20];
FRESULT fr;
mmemset(senddata, 'b', sizeof(senddata));
mmeminit(SRAMIN);
FATFS_Init();
f_mount(fs[0], "0:", 1);
while(1)
{
key = KEY_Scan();
if(key == KEY_UP)
{
fr = f_open(file, "test.txt", FA_CREATE_ALWAYS|FA_WRITE);
if(fr) printf("Create File Fail
");
else
{
printf("Create File Success
");
fr = f_write(file, senddata, sizeof senddata, (UINT *)&temp);
if(fr) printf("Write Error. Error number:%d
", fr);
else printf("Write OK. Write number:%d. Write data:%s
", temp, senddata);
}
f_close(file);
}
}
[/mw_shl_code]
printf 显示 Create File Success,Write OK. Write number:20. Write data: 20个b
但是打开文件,空白。。。
底层是用 SPI 驱动 SD 的,应该没有问题。
底层 Write 代码:
[mw_shl_code=applescript,true]// Write single sector to SD
// @retval 0-success 1-SD not ready 2-SD Write overtime
u8 SD_WriteSector(u8 *p, u8 datatoken)
{
u16 i, retry = 0XFFFF;
u8 r1;
if(SD_WaitReady()) return 1;
SPI2_ReadWriteByte(datatoken); // Data token
if(datatoken != 0XFD) // Stop token for CMD25
{
for(i = 0; i < 512; i++) SPI2_ReadWriteByte(p); // Write 512 Bytes
SPI2_ReadWriteByte(0XFF); // CRC1
SPI2_ReadWriteByte(0XFF); // CRC2
r1 = SPI2_ReadWriteByte(0XFF); // Data response
if((r1&0X1F) != 0X05) return r1;
while(!SPI2_ReadWriteByte(0XFF) && retry)
{
retry--;
}
if(!retry) return 2;
SPI2_ReadWriteByte(0XFF);
}
return 0;
}
u8 SD_WriteDisk(u8 *p, u32 sector, u8 cnt)
{
u8 r1;
if(SD_Type != SD_TYPE_V2HC) sector <<= 9; // change to sector address
if(cnt == 1)
{
printf("Sector 1
");
if(!SD_WriteCmd(CMD24, sector)) // Send block write command
{
r1 = SD_WriteSector(p, 0XFE); // Write block in data paacket form
printf("Write Single Sector, result:%d
", r1);
}
}
else
{
printf("Sector 2
");
if(SD_Type != SD_TYPE_MMC)
{
SD_WriteCmd(CMD55, 0);
SD_WriteCmd(ACMD23, cnt);
}
if(!SD_WriteCmd(CMD25, sector)) // Send multiple block write command
{
do
{
r1 = SD_WriteSector(p, 0XFC);
p += 512;
cnt--;
}while(!r1 && cnt);
r1 = SD_WriteSector(0, 0XFD); // Send Stop Token
}
}
SD_DeSelect();
return r1;
}[/mw_shl_code]
输出:Sector 1. Write Single Sector, result: 0
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>