sd卡读写——sd example阅读

改mss后import example

主要是用fat的函数读写sd

  1 /******************************************************************************
  2 *版权这里可以忽略
  3 * Copyright (C) 2013 - 2015 Xilinx, Inc.  All rights reserved.
  4 *
  5 * Permission is hereby granted, free of charge, to any person obtaining a copy
  6 * of this software and associated documentation files (the "Software"), to deal
  7 * in the Software without restriction, including without limitation the rights
  8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9 * copies of the Software, and to permit persons to whom the Software is
 10 * furnished to do so, subject to the following conditions:
 11 *
 12 * The above copyright notice and this permission notice shall be included in
 13 * all copies or substantial portions of the Software.
 14 *
 15 * Use of the Software is limited solely to applications:
 16 * (a) running on a Xilinx device, or
 17 * (b) that interact with a Xilinx device through a bus or interconnect.
 18 *
 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 22 * XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 25 * SOFTWARE.
 26 *
 27 * Except as contained in this notice, the name of the Xilinx shall not be used
 28 * in advertising or otherwise to promote the sale, use or other dealings in
 29 * this Software without prior written authorization from Xilinx.
 30 *
 31 ******************************************************************************/
 32 /*****************************************************************************/
 33 /**
 34 *这里的注释很重要,不能忽略
 35 * @file xilffs_polled_example.c
 36 *
 37 *
 38 * @note This example uses file system with SD to write to and read from
 39 * an SD card using ADMA2 in polled mode.轮询读写adma2
 40 * To test this example File System should not be in Read Only mode测试要求:文件系统不能只读
 41 * To test this example USE_MKFS option should be true.zynq上需要选ues_mkfs
 42 *
 43 * This example was tested using SD2.0 card and eMMC (using eMMC to SD adaptor).
 44 *
 45 * To test with different logical drives, drive number should be mentioned in
 46 * both FileName and Path variables. By default, it will take drive 0 if drive
 47 * number is not mentioned in the FileName variable.板子选项里sd口有两个,需要选和对应
 48 * For example, to test logical drive 1
 49 * FileName =  "1:/<file_name>" and Path = "1:/"
 50 * Similarly to test logical drive N, FileName = "N:/<file_name>" and
 51 * Path = "N:/"
 52 *
 53 * None.
 54 *
 55 * <pre>
 56 * MODIFICATION HISTORY:
 57 *
 58 * Ver   Who Date     Changes
 59 * ----- --- -------- -----------------------------------------------
 60 * 1.00a hk  10/17/13 First release
 61 * 2.2   hk  07/28/14 Make changes to enable use of data cache.
 62 * 2.5   sk  07/15/15 Used File size as 8KB to test on emulation platform.
 63 * 2.9   sk  06/09/16 Added support for mkfs.
 64 *
 65 *</pre>
 66 *
 67 ******************************************************************************/
 68
 69 /***************************** Include Files *********************************/
 70
 71 #include "xparameters.h"    /* SDK generated parameters */
 72 #include "xsdps.h"        /* SD device driver */
 73 #include "xil_printf.h"
 74 #include "ff.h"
 75 #include "xil_cache.h"
 76 #include "xplatform_info.h"
 77
 78 /************************** Constant Definitions *****************************/
 79
 80
 81 /**************************** Type Definitions *******************************/
 82
 83 /***************** Macros (Inline Functions) Definitions *********************/
 84
 85 /************************** Function Prototypes ******************************/
 86 int FfsSdPolledExample(void);
 87
 88 /************************** Variable Definitions *****************************/
 89 static FIL fil;        /* File object */
 90 static FATFS fatfs;
 91 /*
 92  * To test logical drive 0, FileName should be "0:/<File name>" or
 93  * "<file_name>". For logical drive 1, FileName should be "1:/<file_name>"
 94  */
 95 static char FileName[32] = "Test.bin";//文件名
 96 static char *SD_File;//申请指针
 97 u32 Platform;//后面用来存平台的信息
 98
 99 #ifdef __ICCARM__
100 #pragma data_alignment = 32 //数据位数,后面根据平台信息变大小
101 u8 DestinationAddress[10*1024*1024];//读出数据
102 u8 SourceAddress[10*1024*1024];//源数据
103 #pragma data_alignment = 4
104 #else
105 u8 DestinationAddress[10*1024*1024] __attribute__ ((aligned(32)));
106 u8 SourceAddress[10*1024*1024] __attribute__ ((aligned(32)));
107 #endif
108
109 #define TEST 7
110
111 /*****************************************************************************/
112 /**
113 *调用方法,想起了OO
114 * Main function to call the SD example.
115 *
116 * @param    None
117 *
118 * @return    XST_SUCCESS if successful, otherwise XST_FAILURE.
119 *
120 * @note        None
121 *
122 ******************************************************************************/
123 int main(void)
124 {
125     int Status;
126
127     xil_printf("SD Polled File System Example Test \r\n");//这个是板子打出来的,要接串口
128
129     Status = FfsSdPolledExample();
130     if (Status != XST_SUCCESS) {
131         xil_printf("SD Polled File System Example Test failed \r\n");
132         return XST_FAILURE;
133     }
134
135     xil_printf("Successfully ran SD Polled File System Example Test \r\n");
136
137     return XST_SUCCESS;
138
139 }
140
141 /*****************************************************************************/
142 /**
143 *
144 * File system example using SD driver to write to and read from an SD card
145 * in polled mode. This example creates a new file on an
146 * SD card (which is previously formatted with FATFS), write data to the file
147 * and reads the same data back to verify.
148 *写完读出并检查
149 * @param    None
150 *
151 * @return    XST_SUCCESS if successful, otherwise XST_FAILURE.
152 *
153 * @note        None
154 *
155 ******************************************************************************/
156 int FfsSdPolledExample(void)
157 {
158     FRESULT Res;//
159     UINT NumBytesRead;
160     UINT NumBytesWritten;
161     u32 BuffCnt;
162     u32 FileSize = (8*1024*1024);
163     /*
164      * To test logical drive 0, Path should be "0:/"
165      * For logical drive 1, Path should be "1:/"
166      */
167     TCHAR *Path = "0:/";
168
169     Platform = XGetPlatform_Info();//获得平台信息
170     if (Platform == XPLAT_ZYNQ_ULTRA_MP) {//如果是ultra
171         /*
172          * Since 8MB in Emulation Platform taking long time, reduced
173          * file size to 8KB.减大小
174          */
175         FileSize = 8*1024;
176     }
177
178     for(BuffCnt = 0; BuffCnt < FileSize; BuffCnt++){
179         SourceAddress[BuffCnt] = TEST + BuffCnt;//给地址赋值,test应该是基地址?前面宏定义为7
180     }
181
182     /*
183      * Register volume work area, initialize device
184      */
185     Res = f_mount(&fatfs, Path, 0);//初始化?文件系统指针(前面的静态变量),通道,模式                        //fatfs在哪里赋的值?                        //https://blog.csdn.net/lbaihao/article/details/75144011
186
187     if (Res != FR_OK) { //失败
188         return XST_FAILURE;
189     }
190
191     /*
192      * Path - Path to logical driver, 0 - FDISK format.
193      * 0 - Cluster size is automatically determined based on Vol size.
194      */
195     Res = f_mkfs(Path, 0, 0);//建立fat系统
196     if (Res != FR_OK) {
197         return XST_FAILURE;
198     }
199
200     /*
201      * Open file with required permissions.
202      * Here - Creating new file with read/write permissions. .
203      * To open file with write permissions, file system should not
204      * be in Read Only mode.
205      */
206     SD_File = (char *)FileName;//FileName是之前的常量"Test.bin"
207
208     Res = f_open(&fil, SD_File, FA_CREATE_ALWAYS | FA_WRITE | FA_READ);//新建读写文件并打开,给指针fil
209     if (Res) {
210         return XST_FAILURE;
211     }
212
213     /*
214      * Pointer to beginning of file .
215      */
216     Res = f_lseek(&fil, 0);//移到文件开始位
217     if (Res) {
218         return XST_FAILURE;
219     }
220
221     /*
222      * Write data to file.写数据
223      */
224     Res = f_write(&fil, (const void*)SourceAddress, FileSize,
225             &NumBytesWritten);//源数据要是const,第二个参数是buf,写的内容
226     if (Res) {
227         return XST_FAILURE;
228     }
229
230     /*
231      * Pointer to beginning of file .
232      */
233     Res = f_lseek(&fil, 0);
234     if (Res) {
235         return XST_FAILURE;
236     }
237
238     /*
239      * Read data from file.读数据
240      */
241     Res = f_read(&fil, (void*)DestinationAddress, FileSize,
242             &NumBytesRead);
243     if (Res) {
244         return XST_FAILURE;
245     }
246
247     /*
248      * Data verification 比较读写内容是否一致
249      */
250     for(BuffCnt = 0; BuffCnt < FileSize; BuffCnt++){
251         if(SourceAddress[BuffCnt] != DestinationAddress[BuffCnt]){
252             return XST_FAILURE;
253         }
254     }
255
256     /*
257      * Close file.关文件
258      */
259     Res = f_close(&fil);
260     if (Res) {
261         return XST_FAILURE;
262     }
263
264     return XST_SUCCESS;
265 }

本次的sd就是把文件拷到sd上,命名。然后用open打开,读到数据,用dma写到ddr上。结束后用dma取出,写回到sd卡上。

原文地址:https://www.cnblogs.com/iwanna/p/10003244.html

时间: 2024-08-04 11:40:33

sd卡读写——sd example阅读的相关文章

第36章 SDIO—SD卡读写测试

第36章     SDIO-SD卡读写测试 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/firege 本章参考资料:<STM32F4xx参考手册>.<STM32F4xx规格书>.库帮助文档<stm32f4xx_dsp_stdperiph_lib_um.chm>以及SD简易规格文件<Physical Layer Simplified Specificatio

Android手机内存,SD卡读写

在手机上有两个存储位置 1 手机内部存储 2 SD卡 文件操作模式: 是否允许外部访问? 文件以覆盖/追加方式写? 手机内存读写 //保存文件到手机内存 public void save(String fileName,String content) throws IOException{ FileOutputStream fos = context.openFileOutput("a.txt", Context.MODE_PRIVATE); fos.write(content.get

Android中向SD卡读写数据,读SD卡和手机内存

package com.example.sdoperation; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import android.support.v7.app.Actio

Android Rom分区 与 SD卡读写

1.Rom分区 在Android中,对数据的保护是很严密的.除了放在SD卡中的数据,一个应用所拥有的数据库.文件等内容都是不允许其他应用直接访问的,这一部分数据都是在/data/data里面. 这里所说的SD卡是逻辑上的SD卡,比如我现在用的galaxy s4 的Rom是16g,Android系统会把这16g分成两个部分,一个是系统和应用程序数据区,另一个就是虚拟的SD卡,可以称它为内置SD卡. 我们平时外插进手机的是TF卡,简称Micro SD卡,具体的可以参考百度. 三星手机默认的内置SD卡

单片机SD 卡读写

1.迄今为止看到的最详细的关于SD卡SPI mode的分析和代码 http://elm-chan.org/docs/mmc/mmc_e.html 2.转载http://blog.csdn.net/ming1006/article/details/7281597 现在我们手机的内存卡多为Micro SD卡,又叫TF卡,所以Micro SD卡比SD卡常见.自己曾经也想写写SD卡的读取程序,但又不想特地再去买个SD卡,这时想起手机内存卡不是和SD卡很像吗?在网上查了以后发现SD卡和Micro SD卡其

SD卡读写之FileNotFoundException: /storage/emulated/0object.txt: open failed: ENOENT (No such file or dir

读写sd卡中的文件按照如下步骤:1调用Environment的getExternalStorageState()方法判断手机上是否插入了sd卡,并且应用程序具有读写SD卡的能力 //如果手机已经插入了SD卡,且具有读写sd卡的能力,下面的语句将会返回true Environment.getExternalStorageState().equals(Envronment.MEDIA_MOUNTED) 2)调用environment的getExternalStorageDIrectory()方法获取

Android入门开发之SD卡读写操作(转)

SD卡的读写是我们在开发android 应用程序过程中最常见的操作.下面介绍SD卡的读写操作方式: 1. 获取SD卡的根目录 [java] view plaincopy String  sdCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath(); 2. 在SD卡上创建文件夹目录 [java] view plaincopy /** * 在SD卡上创建目录 */ public File createDirOnSDC

android 64 sd卡读写的操作

package com.itheima.writesd; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.

Android SD卡读写

package com.jredu.zuiyou.activity; import android.os.Bundle;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.Toast; import com.jredu.zuiyou.R;imp