ZYNQ裸机下SD卡读写速度测试

使用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;
}

### Zynq平台上的DMA与SD数据传输 在Zynq平台上实现DMA控制下的SD数据传输涉及硬件配置和软件编程两方面的工作。对于基于Xilinx Zynq系列器件的应用开发而言,通常会利用AXI Direct Memory Access (DMA) IP核来加速外设间的数据交换过程。 #### 硬件设计部分 为了使能这一功能,在Vivado工具中创建项目时需添加如下组件: - AXI SD Host Controller用于管理SD/SDIO/MMC设备接口; - AXI DMA模块负责高效地搬运大量数据而无需CPU干预; - Processing System (PS),即ARM Cortex-A9处理器子系统作为整个系统的控制器; 这些IP之间通过AXI总线互连形成完整的SoC架构[^1]。 #### 软件驱动编写 针对上述构建好的硬件环境,可以采用裸机程序或Linux操作系统两种方式完成具体的读写操作逻辑编码工作。这里以裸机为例说明基本流程: ```c #include "xparameters.h" #include "xsdps.h" #include "xdma.h" // 初始化SD并设置为高速模式 XSdPs_Config *SdConfig; int Status; SdConfig = XSdPs_LookupConfig(XPAR_SD_0_DEVICE_ID); Status = XSdPs_CfgInitialize(&Sd, SdConfig, SdConfig->BaseAddress); if (!Status){ // 配置成高频率模式 XSdPs_SetCardSpeed(&Sd,XSDPS_SPEED_HIGH); } // 设置DMA通道参数准备接收来自SD的数据流 XDma_DstChnCfg(DMA_BASEADDR,DST_CHANNEL, BUFFER_ADDRESS,BUFFER_SIZE); while(1){ // 启动一次DMA传输请求 XDma_StartTransfer(DMA_BASEADDR,SRC_CHANNEL,DMA_TO_DEV); // 等待直到当前事务结束再继续下一轮循环体内的其他指令... } ``` 此段伪代码展示了初始化阶段以及持续监听状态变化的过程。实际应用当中还需要考虑错误处理机制等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值