参考博客:
https://blog.csdn.net/husipeng86/article/details/52262070
参考上述博客中的程序,报错:ERROR : f_write returned 1写失败,又参考了以下博客:
https://blog.csdn.net/fanwenjieok/article/details/37693625
将原博客中f_open函数中用的标志由FA_CHACK_ALWAYS | FA_WRITE 改为 FA_OPEN_ALWAYS | FA_WRITE
程序如下:
#include
#include "platform.h"
#include "xparameters.h"
#include "xil_printf.h"
#include "ff.h"
#include "xdevcfg.h"
static FATFS fatfs;
int SD_Init()
{
FRESULT rc;
rc = f_mount(&fatfs,"",0);
if(rc)
{
xil_printf("ERROR : f_mount returned %d\r\n",rc);
return XST_FAILURE;
}
return XST_SUCCESS;
}
int SD_Transfer_read(char *FileName,u32 DestinationAddress,u32 ByteLength)
{
FIL fil;
FRESULT rc;
UINT br;
rc = f_open(&fil,FileName,FA_READ);
if(rc)
{
xil_printf("ERROR : f_open returned %d\r\n",rc);
return XST_FAILURE;
}
rc = f_lseek(&fil, 0);
if(rc)
{
xil_printf("ERROR : f_lseek returned %d\r\n",rc);
return XST_FAILURE;
}
rc = f_read(&fil, (void*)DestinationAddress,ByteLength,&br);
if(rc)
{
xil_printf("ERROR : f_read returned %d\r\n",rc);
return XST_FAILURE;
}
rc = f_close(&fil);
if(rc)
{
xil_printf(" ERROR : f_close returned %d\r\n", rc);
return XST_FAILURE;
}
return XST_SUCCESS;
}
int SD_Transfer_write(char *FileName,u32 SourceAddress,u32 ByteLength)
{
FIL fil;
FRESULT rc;
UINT bw;
rc = f_open(&fil,FileName,FA_OPEN_ALWAYS | FA_WRITE);
if(rc)
{
xil_printf("ERROR : f_open returned %d\r\n",rc);
return XST_FAILURE;
}
rc = f_lseek(&fil, 0);
if(rc)
{
xil_printf("ERROR : f_lseek returned %d\r\n",rc);
return XST_FAILURE;
}
rc = f_write(&fil,(void*) SourceAddress,ByteLength,&bw);
if(rc)
{
xil_printf("ERROR : f_write returned %d\r\n", rc);
return XST_FAILURE;
}
rc = f_close(&fil);
if(rc){
xil_printf("ERROR : f_close returned %d\r\n",rc);
return XST_FAILURE;
}
return XST_SUCCESS;
}
#define FILE "test.txt"
int main()
{
init_platform();
const char src_str[] = "hsp test sd card write and read!";
u32 len = strlen(src_str);
SD_Init();
SD_Transfer_write(FILE,(u32)src_str,(len+1000));//当直接指定len时没有写出,需要指定较大的长度才会写出,原因未知
char dest_str[33];//len<=33
SD_Init();
SD_Transfer_read(FILE,(u32)dest_str,(len+1));
xil_printf("%s\r\n",dest_str);
print("SD write and read over!\r\n");
cleanup_platform();
return 0;
}
上述程序可以把字符串写入SD卡的文档中,也可以读出,但还有一个错误,但不知道什么原因造成的!!!!!
另外可参考链接:
http://elm-chan.org/fsw/ff/00index_e.html
https://blog.csdn.net/mcupro/article/details/73694460
https://blog.csdn.net/love_ljq/article/details/79117738