码迷,mamicode.com
首页 > 其他好文 > 详细

sd卡读写——sd example阅读

时间:2018-11-24 14:04:53      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:base   att   ++   常量   iat   similar   macros   follow   void   

改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卡上。

sd卡读写——sd example阅读

标签:base   att   ++   常量   iat   similar   macros   follow   void   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!