最近一直在做zynq方面的学习,用到哪就写到哪吧
读写sd步骤如下
1、首先新建zynq的工程,连线如下
2、然后综合,布线生成后bin文件,并按照下图操作
打开sdk后新建工程
file 》new 》application project
新建helloworld工程
下面导入sd卡的库
点击xilinx 》board support package setting
选中xilffs库,其他默认即可
连接上板载的串口
#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;
}
float power(float a,int b){
float c=1;
if(b>0){
for(int i=0;i<b;i++){
c=c*a;
}
}
else{
for(int i=0;i<(0-b);i++){
c=c/a;
}
}
return c;
}
#define FILE "test123.txt"
#define FILE2 "W_CONV3.txt"
//#define MAX_LEN 1024*1024*200
#define MAX_LEN 900000
static char src_str[MAX_LEN] = { 0 } ;
static char dst_str[MAX_LEN] = { 0 } ;
static float outfloat[48000];
int main()
{
int r;
unsigned int a;
int myfloat[15];
int i=0;//point location
unsigned int j=0;
int point=0;
int length=0;
float out;
float out1=1.1231;
int Boolfloatnext=0;
int Boolfloatnow=0;
int mode=0;
int nummode[5]={0};
int nummode_num=0;
int num=0;
init_platform();
r = SD_Init();
if ( XST_SUCCESS != r ){
printf("fail to open SD1\n\r");
return 1 ;
}
printf("read SD1\n\r");
SD_Transfer_read(FILE2,(void*)dst_str,(MAX_LEN));
printf("read done SD1\n\r");
printf("start check data ...\n\r");
for(a=0;(a<MAX_LEN)&&(j<48000);a++){
if(dst_str[a]!=32){
if(mode==0){
if(dst_str[a]==46) {
point=i;
}
//else if(dst_str[a]==45) {}//-operation
else if(((dst_str[a-1]==101)||(dst_str[a-1]==69))&&(dst_str[a]==45)) {mode=1;}
else if((dst_str[a]>47)&&(dst_str[a]<58)){
myfloat[i]=dst_str[a]-48;
i++;
}
}
else
{nummode[nummode_num]=dst_str[a]-48;
nummode_num++;
}
}
else if(dst_str[a+1]!=32){//next num
mode=0;
for(int k=0;k<nummode_num;k++){
num+=(nummode[k]*power(10,nummode_num-k-1));
nummode[k]=0;
}
if(dst_str[a+1]==45) {//next unsigned or not
Boolfloatnow=Boolfloatnext;
Boolfloatnext=1;
}
else{
Boolfloatnow=Boolfloatnext;
Boolfloatnext=0;
}
length=i;
if(point==0) point=length;
// xil_printf("%d %d\r\n\r\n",length,point);
for(int k=0;k<length;k++){
out+=(myfloat[k]*power(10,point-k-1));
myfloat[k]=0;
}
xil_printf("%d\r\n",j);
if(Boolfloatnow==0) {outfloat[j]=out;}
else {outfloat[j]=0-out;}
outfloat[j]=outfloat[j]/(power(10,num));
out=0;
j++;
i=0;
nummode_num=0;
num=0;
length=0;
point=0;
}
// xil_printf("%d\r\n",a);
}
printf(" dst %d \n\r",dst_str[150000]);
printf(" dst %f \n\r",outfloat[0]);
printf(" dst %f \n\r",outfloat[1]);
printf(" dst %f \n\r",outfloat[47992]);
printf(" dst %f \n\r",outfloat[47993]);
printf(" dst %f \n\r",outfloat[47998]);
printf(" dst %f \n\r",outfloat[47999]);
print("SD over!\r\n");
cleanup_platform();
return 0;
}