/***************************** Include Files *********************************/ #include "xparameters.h" /* SDK generated parameters */ #include "xsdps.h" /* SD device driver */ #include "xil_printf.h" #include "ff.h" #include "xil_cache.h" #include "xplatform_info.h" int FfsSdPolledExample(void); ////////////////////// //static void Delay(uint32_t nCount) // //{ // // uint32_t index = 0; // // for (index = (100000 * nCount); index != 0; index--); // //} ///////////////// /************************** Variable Definitions *****************************/ static FIL fil; /* File object */ static FATFS fatfs; /* * To test logical drive 0, FileName should be "0:/" or * "". For logical drive 1, FileName should be "1:/" */ static char FileName[32] = "ABCD"; static char *SD_File; u32 Platform; u8 DestinationAddress[10*1024*1024] __attribute__ ((aligned(32))); u8 SourceAddress[10*1024*1024] __attribute__ ((aligned(32))); #define TEST 7 int main(void) { int Status; xil_printf("SD Polled File System Example Test \r\n"); Status = FfsSdPolledExample(); if (Status != XST_SUCCESS) { xil_printf("SD Polled File System Example Test failed \r\n"); return XST_FAILURE; } xil_printf("Successfully ran SD Polled File System Example Test \r\n"); return XST_SUCCESS; } /*****************************************************************************/ /** ** File system example using SD driver to write to and read from an SD card * in polled mode. This example creates a new file on an * SD card (which is previously formatted with FATFS), write data to the file * and reads the same data back to verify. * * @param None * @return XST_SUCCESS if successful, otherwise XST_FAILURE. * @note None * ******************************************************************************/ int FfsSdPolledExample(void) { FRESULT Res; UINT NumBytesRead; UINT NumBytesWritten; u32 BuffCnt; u32 FileSize = (8*1024*1024); /* * To test logical drive 0, Path should be "0:/" * For logical drive 1, Path should be "1:/" */ TCHAR *Path = "0:/"; Platform = XGetPlatform_Info(); for(BuffCnt = 0; BuffCnt < FileSize; BuffCnt++){ SourceAddress[BuffCnt] = TEST + BuffCnt; } /* Register volume work area, initialize device */ Res = f_mount(&fatfs, Path, 0); if (Res != FR_OK) { xil_printf("SD f_mount failed %d\n\r", Res); return XST_FAILURE; } Res = f_mkfs(Path, 0, 0); if (Res != FR_OK) { xil_printf("SD f_mkfs failed %d\n\r", Res); return XST_FAILURE; } SD_File = (char *)FileName; Res = f_open(&fil, SD_File, FA_CREATE_ALWAYS | FA_WRITE | FA_READ); if (Res) { xil_printf("SD f_open failed %d\n\r", Res); return XST_FAILURE; } /* Pointer to beginning of file . */ Res = f_lseek(&fil, 0); if (Res) { xil_printf("SD f_lseek failed %d\n\r", Res); return XST_FAILURE; } //Delay(200); /* Write data to file. */ Res = f_write(&fil, (const void*)SourceAddress, FileSize, &NumBytesWritten); if (Res) { xil_printf("SD f_write failed %d\n\r", Res); return XST_FAILURE; } /* Pointer to beginning of file .*/ Res = f_lseek(&fil, 0); if (Res) { return XST_FAILURE; } /* Read data from file. */ Res = f_read(&fil, (void*)DestinationAddress, FileSize, &NumBytesRead); if (Res) { return XST_FAILURE; } /* Data verification */ for(BuffCnt = 0; BuffCnt < FileSize; BuffCnt++){ if(SourceAddress[BuffCnt] != DestinationAddress[BuffCnt]){ return XST_FAILURE; } } Res = f_close(&fil); if (Res) { return XST_FAILURE; } return XST_SUCCESS; }