ZYNQ系统中实现FAT32文件系统的SD卡读写之三 SDK编程调试

46 篇文章 8 订阅
29 篇文章 7 订阅


导入到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程序。



下一篇我重点分析一下这个代码,介绍一下注意事项。




  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值