#include <stdio.h> #include "xparameters.h" #include "xsdps.h" #include "xil_printf.h" #include "ff.h"
static FATFS SD_Dev; // File System instance char *SD_Path = "0:/"; // string pointer to the logical drive number static char FileName[32] = "test.txt"; // name of the log
u8 WR_Buf[1024] __attribute__ ((aligned(32))); // Buffer should be word aligned (multiple of 4) u8 RD_Buf[1024] __attribute__ ((aligned(32))); // Buffer should be word aligned (multiple of 4)
int SD_init() { FRESULT result; //-----------------------mount dev----------------------------------------------- result = f_mount(&SD_Dev,SD_Path, 0); if (result != 0) { return XST_FAILURE; } return XST_SUCCESS; }
int SD_read(char *FileName,u8 *DestinationAddress,u32 ByteLength) { FIL file; FRESULT result; UINT BytesRd;
result = f_open(&file,FileName,FA_READ); if(result) { return XST_FAILURE; }
result = f_lseek(&file, 0); if(result) { return XST_FAILURE; }
result = f_read(&file, (void*)DestinationAddress,ByteLength,&BytesRd); if(result) { return XST_FAILURE; }
result = f_close(&file); if(result) { return XST_FAILURE; } return XST_SUCCESS; }
int SD_write(char *FileName,u8 *SourceAddress,u32 ByteLength) { FIL file; FRESULT result; UINT BytesWr;
result = f_open(&file,FileName,FA_CREATE_ALWAYS | FA_WRITE); if(result) { return XST_FAILURE; }
result = f_lseek(&file, 0); if(result) { return XST_FAILURE; }
result = f_write(&file,(void*) SourceAddress,ByteLength,&BytesWr); if(result) { return XST_FAILURE; }
result = f_close(&file); if(result){ return XST_FAILURE; }
return XST_SUCCESS; }
int main() {
int i; FRESULT result; u8 SD_ERROR=0; u32 Buffer_size=1024;
SD_init();
for(i=0;i<Buffer_size;i++) { WR_Buf[i]=i; } //-----------------------write test data to file----------------------------------- result=SD_write(FileName, WR_Buf, Buffer_size); if (result!=0) { return XST_FAILURE; } xil_printf("SD CARD written Successfully\r\n");
//-----------------------read test data ------------------------------------------ result=SD_read(FileName, RD_Buf, Buffer_size); if (result!=0) { return XST_FAILURE; } xil_printf("SD CARD Data read Successfully\r\n");
//-----------------------check data----------------------------------------------- for(i=0; i<Buffer_size;i++) {
if(WR_Buf[i]!=RD_Buf[i]) { SD_ERROR=1; break; } }
if(SD_ERROR) xil_printf("SD CARD Data checked Error\r\n"); else xil_printf("SD CARD Data checked Successfully\r\n");
return 0; } |