sd卡讀寫——sd example閱讀
阿新 • • 發佈:2018-11-24
constant aligned sta beginning pri format lba () sdn
改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 deal7 * 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閱讀