使用PS端的SD口测试
基于xilinx官方读写例程修改
这里仅测试了连续读写
测试代码如下:
/******************************************************************************
*
* Copyright (C) 2013 - 2015 Xilinx, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/
/*****************************************************************************/
/**
*
* @file xilffs_polled_example.c
*
*
* @note This example uses file system with SD to write to and read from
* an SD card using ADMA2 in polled mode.
* To test this example File System should not be in Read Only mode.
* To test this example USE_MKFS option should be true.
*
* This example was tested using SD2.0 card and eMMC (using eMMC to SD adaptor).
*
* To test with different logical drives, drive number should be mentioned in
* both FileName and Path variables. By default, it will take drive 0 if drive
* number is not mentioned in the FileName variable.
* For example, to test logical drive 1
* FileName = "1:/<file_name>" and Path = "1:/"
* Similarly to test logical drive N, FileName = "N:/<file_name>" and
* Path = "N:/"
*
* None.
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver Who Date Changes
* ----- --- -------- -----------------------------------------------
* 1.00a hk 10/17/13 First release
* 2.2 hk 07/28/14 Make changes to enable use of data cache.
* 2.5 sk 07/15/15 Used File size as 8KB to test on emulation platform.
* 2.9 sk 06/09/16 Added support for mkfs.
* 3.10 mn 08/18/18 Change file size to 8MB from 8KB for ZynqMP platform
*
*</pre>
*
******************************************************************************/
/***************************** 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"
#include "xtime_l.h"
#include "sleep.h"
#include "stdio.h"
#define TIME_FREQ (XPAR_CPU_CORTEXA9_CORE_CLOCK_FREQ_HZ / (2U))
/************************** Constant Definitions *****************************/
/**************************** Type Definitions *******************************/
/***************** Macros (Inline Functions) Definitions *********************/
/************************** Function Prototypes ******************************/
int FfsSdPolledExample(void);
/************************** Variable Definitions *****************************/
static FIL fil; /* File object */
static FATFS fatfs;
/*
* To test logical drive 0, FileName should be "0:/<File name>" or
* "<file_name>". For logical drive 1, FileName should be "1:/<file_name>"
*/
static char FileName[32] = "Test2.bin";
static char *SD_File;
#ifdef __ICCARM__
#pragma data_alignment = 32
u8 DestinationAddress[10*1024];
u8 SourceAddress[10*1024];
#pragma data_alignment = 4
#else
u8 DestinationAddress[100*1024*1024] __attribute__ ((aligned(32)));
u8 SourceAddress[100*1024*1024] __attribute__ ((aligned(32)));
#endif
#define TEST 7
/*****************************************************************************/
/**
*
* Main function to call the SD example.
*
* @param None
*
* @return XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note None
*
******************************************************************************/
#include "xgpiops.h"
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;
BYTE work[FF_MAX_SS];
#ifdef __ICCARM__
u32 FileSize = (8*1024);
#else
u32 FileSize = (50*1024*1024);
#endif
XTime tEnd, tCur;
XTime tUse;
/*
* To test logical drive 0, Path should be "0:/"
* For logical drive 1, Path should be "1:/"
*/
TCHAR *Path = "0:/";
for(BuffCnt = 0; BuffCnt < FileSize; BuffCnt++){
SourceAddress[BuffCnt] = TEST + BuffCnt;
}
/*
* Register volume work area, initialize device
*/
do{
Res = f_mount(&fatfs, Path, 0);
}while(Res != FR_OK);
if (Res != FR_OK) {
return XST_FAILURE;
}
/*
* Path - Path to logical driver, 0 - FDISK format.
* 0 - Cluster size is automatically determined based on Vol size.
*/
Res = f_mkfs(Path, FM_FAT32, 0, work, sizeof work);
if (Res != FR_OK) {
return XST_FAILURE;
}
/*
* Open file with required permissions.
* Here - Creating new file with read/write permissions. .
* To open file with write permissions, file system should not
* be in Read Only mode.
*/
SD_File = (char *)FileName;
Res = f_open(&fil, SD_File, FA_CREATE_ALWAYS | FA_WRITE | FA_READ);
if (Res) {
return XST_FAILURE;
}
/*
* Pointer to beginning of file .
*/
Res = f_lseek(&fil, 0);
if (Res) {
return XST_FAILURE;
}
/*
* Write data to file.
*/
XTime_GetTime(&tCur);
Res = f_write(&fil, (const void*)SourceAddress, FileSize,
&NumBytesWritten);
if (Res) {
return XST_FAILURE;
}
XTime_GetTime(&tEnd);
tUse = tEnd - tCur; //count
float sec = tUse / (float)TIME_FREQ;
printf("Write use time(s):%f ",sec);
xil_printf("\r\n");
printf("Write Speed = %f MBytes/s ", ((float)FileSize / sec ) / (1024 * 1024));
xil_printf("\r\n");
/*
* Pointer to beginning of file .
*/
Res = f_lseek(&fil, 0);
if (Res) {
return XST_FAILURE;
}
/*
* Read data from file.
*/
XTime_GetTime(&tCur);
Res = f_read(&fil, (void*)DestinationAddress, FileSize,
&NumBytesRead);
if (Res) {
return XST_FAILURE;
}
XTime_GetTime(&tEnd);
tUse = tEnd - tCur;
sec = tUse / (float)TIME_FREQ;
printf("Read use time(s):%f ",sec);
xil_printf("\r\n");
printf("Read Speed = %f Bytes/s", ((float)FileSize / sec ) / (1024 * 1024));
xil_printf("\r\n");
/*
* Data verification
*/
for(BuffCnt = 0; BuffCnt < FileSize; BuffCnt++){
if(SourceAddress[BuffCnt] != DestinationAddress[BuffCnt]){
return XST_FAILURE;
}
}
/*
* Close file.
*/
Res = f_close(&fil);
if (Res) {
return XST_FAILURE;
}
return XST_SUCCESS;
}