导入到SDK后直接模板生成一个HELLO WORLD项目,之后在XILINX TOOLS-》BOARD SUPPORT PACKAGE SETTING里面设置选择XILFFS。
XILFFS各项可以设置参数按照默认,如下图:
之后修改main函数所在文件,直接拷贝一下内容覆盖原文件 :
#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_CREATE_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);
printf("SD_Transfer_write -> f_write result len is %d \n\r" ,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 "test123.txt"
#define MAX_LEN 1024*1024*200
static char src_str[MAX_LEN] = { 0 } ;
static char dst_str[MAX_LEN] = { 0 } ;
int main()
{
int i,r;
init_platform();
u32 len = strlen(src_str);
r = SD_Init();
if ( XST_SUCCESS != r ){
printf("fail to open SD1\n\r");
return 1 ;
}
for(i=0;i<MAX_LEN;++i) src_str[i] = i + 5 ;
printf("OK to open SD1\n\r");
printf("write SD1\n\r");
printf("%d\n",src_str[0]);
SD_Transfer_write(FILE,(void *)src_str,(MAX_LEN));//当直接指定len时没有写出,需要指定较大的长度才会写出,原因未知
printf("%d\n",src_str[0]);
printf("write done SD1\n\r");
SD_Init();
printf("read SD1\n\r");
SD_Transfer_read(FILE,(void*)dst_str,(MAX_LEN));
printf("read done SD1\n\r");
printf("start check data ...\n\r");
for(i=0;i<MAX_LEN;++i) if (src_str[i]!=dst_str[i] ){
printf("error @ %d src %d <> dst %d \n\r",i,src_str[i],dst_str[i]);
}
print("SD write and read over!\r\n");
cleanup_platform();
return 0;
}
之后链接JTAG直接下载到板子上在线运行,注意可以不少些PL部分,因为这里仅仅是PS程序。
下一篇我重点分析一下这个代码,介绍一下注意事项。