]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Source/FreeRTOS-Plus-FAT/portable/Zynq/ff_sddisk.c
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Source / FreeRTOS-Plus-FAT / portable / Zynq / ff_sddisk.c
1 /*\r
2  * FreeRTOS+FAT build 191128 - Note:  FreeRTOS+FAT is still in the lab!\r
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  * Authors include James Walmsley, Hein Tibosch and Richard Barry\r
5  *\r
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
7  * this software and associated documentation files (the "Software"), to deal in\r
8  * the Software without restriction, including without limitation the rights to\r
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
10  * the Software, and to permit persons to whom the Software is furnished to do so,\r
11  * subject to the following conditions:\r
12  *\r
13  * The above copyright notice and this permission notice shall be included in all\r
14  * copies or substantial portions of the Software.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * https://www.FreeRTOS.org\r
24  *\r
25  */\r
26 \r
27 /* Standard includes. */\r
28 #include <stdlib.h>\r
29 #include <string.h>\r
30 #include <stdarg.h>\r
31 #include <stdio.h>\r
32 \r
33 /* Xilinx library includes. */\r
34 #include "xparameters.h"\r
35 #include "xil_types.h"\r
36 #include "xsdps.h"              /* SD device driver */\r
37 #include "xsdps_info.h" /* SD info */\r
38 \r
39 /* FreeRTOS includes. */\r
40 #include "FreeRTOS.h"\r
41 #include "task.h"\r
42 #include "semphr.h"\r
43 #include "portmacro.h"\r
44 \r
45 /* FreeRTOS+FAT includes. */\r
46 #include "ff_headers.h"\r
47 #include "ff_sddisk.h"\r
48 #include "ff_sys.h"\r
49 \r
50 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )\r
51         #include "xil_exception.h"\r
52         #include "xscugic_hw.h"\r
53 #endif /* ffconfigSDIO_DRIVER_USES_INTERRUPT */\r
54 \r
55 #include "uncached_memory.h"\r
56 \r
57 #define sdSIGNATURE 0x41404342\r
58 \r
59 #ifndef ARRAY_SIZE\r
60         #define ARRAY_SIZE(x)   (int) (sizeof(x)/sizeof(x)[0])\r
61 #endif\r
62 \r
63 #define STA_NOINIT              0x01    /* Drive not initialized */\r
64 #define STA_NODISK              0x02    /* No medium in the drive */\r
65 #define STA_PROTECT             0x04    /* Write protected */\r
66 \r
67 #define SD_DEVICE_ID                                    XPAR_XSDPS_0_DEVICE_ID\r
68 #define HIGH_SPEED_SUPPORT                              0x01\r
69 #define WIDTH_4_BIT_SUPPORT                             0x4\r
70 #define SD_CLK_12_MHZ                                   12000000\r
71 #define SD_CLK_25_MHZ                                   25000000\r
72 #define SD_CLK_26_MHZ                                   26000000\r
73 #define SD_CLK_52_MHZ                                   52000000\r
74 #define EXT_CSD_DEVICE_TYPE_BYTE                196\r
75 #define EXT_CSD_4_BIT_WIDTH_BYTE                183\r
76 #define EXT_CSD_HIGH_SPEED_BYTE                 185\r
77 #define EXT_CSD_DEVICE_TYPE_HIGH_SPEED  0x3\r
78 \r
79 #define HUNDRED_64_BIT                  100ULL\r
80 #define BYTES_PER_MB                    ( 1024ull * 1024ull )\r
81 #define SECTORS_PER_MB                  ( BYTES_PER_MB / 512ull )\r
82 \r
83 #define XSDPS_INTR_NORMAL_ENABLE ( XSDPS_INTR_CC_MASK | XSDPS_INTR_TC_MASK | \\r
84         XSDPS_INTR_DMA_MASK | XSDPS_INTR_CARD_INSRT_MASK | XSDPS_INTR_CARD_REM_MASK | \\r
85         XSDPS_INTR_ERR_MASK )\r
86 \r
87 /* Two defines used to set or clear the interrupt */\r
88 #define INTC_BASE_ADDR          XPAR_SCUGIC_CPU_BASEADDR\r
89 #define INTC_DIST_BASE_ADDR     XPAR_SCUGIC_DIST_BASEADDR\r
90 \r
91 /* Interupt numbers for SDIO units 0 and 1: */\r
92 #define SCUGIC_SDIO_0_INTR      0x38\r
93 #define SCUGIC_SDIO_1_INTR      0x4F\r
94 \r
95 /* Define a timeout on data transfers for SDIO: */\r
96 #define sdWAIT_INT_TIME_OUT_MS          5000UL\r
97 \r
98 /* Define a short timeout, used during card-detection only (CMD1): */\r
99 #define sdQUICK_WAIT_INT_TIME_OUT_MS    1000UL\r
100 \r
101 /* XSdPs xSDCardInstance; */\r
102 static XSdPs *pxSDCardInstance;\r
103 \r
104 static int sd_disk_status = STA_NOINIT; /* Disk status */\r
105 const int drive_nr = 0;\r
106 static SemaphoreHandle_t xPlusFATMutex;\r
107 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )\r
108         /* Create a semaphore for each of the two memory-card slots. */\r
109         static SemaphoreHandle_t xSDSemaphores[ 2 ];\r
110 #endif\r
111 \r
112 static int vSDMMC_Init( int iDriveNumber );\r
113 static int vSDMMC_Status( int iDriveNumber );\r
114 \r
115 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )\r
116         static void vInstallInterrupt( void );\r
117 #endif\r
118 \r
119 struct xCACHE_MEMORY_INFO\r
120 {\r
121         /* Reserve 'uncached' memory for caching sectors, will be passed to the +FAT library. */\r
122         uint8_t pucCacheMemory[ 0x10000 ];\r
123         /* Reserve 'uncached' memory for i/o to the SD-card. */\r
124         uint8_t pucHelpMemory[ 0x40000 ];\r
125         XSdPs xSDCardInstance;\r
126 };\r
127 \r
128 struct xCACHE_STATS\r
129 {\r
130         uint32_t xMemcpyReadCount;\r
131         uint32_t xMemcpyWriteCount;\r
132         uint32_t xPassReadCount;\r
133         uint32_t xPassWriteCount;\r
134         uint32_t xFailReadCount;\r
135         uint32_t xFailWriteCount;\r
136 };\r
137 \r
138 struct xCACHE_STATS xCacheStats;\r
139 struct xCACHE_MEMORY_INFO *pxCacheMem = NULL;\r
140 \r
141 static const uint8_t *prvStoreSDCardData( const uint8_t *pucBuffer, uint32_t ulByteCount );\r
142 static uint8_t *prvReadSDCardData( uint8_t *pucBuffer, uint32_t ulByteCount );\r
143 \r
144 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )\r
145         void XSdPs_IntrHandler(void *XSdPsPtr);\r
146 #endif /* ffconfigSDIO_DRIVER_USES_INTERRUPT */\r
147 \r
148 static int32_t prvFFRead( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk )\r
149 {\r
150 int32_t lReturnCode;\r
151 int iResult;\r
152 uint8_t *pucReadBuffer;\r
153 \r
154         if( ( pxDisk != NULL ) &&               /*_RB_ Could this be changed to an assert? */\r
155                 ( pxDisk->ulSignature == sdSIGNATURE ) &&\r
156                 ( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&\r
157                 ( ulSectorNumber < pxDisk->ulNumberOfSectors ) &&\r
158                 ( pxDisk->ulNumberOfSectors - ulSectorNumber ) >= ulSectorCount )\r
159         {\r
160                 iResult = vSDMMC_Status( drive_nr );\r
161                 if( ( iResult & STA_NODISK ) != 0 )\r
162                 {\r
163                         lReturnCode = FF_ERR_DRIVER_NOMEDIUM | FF_ERRFLAG;\r
164                         FF_PRINTF( "prvFFRead: NOMEDIUM\n" );\r
165                 }\r
166                 else if( ( iResult & STA_NOINIT ) != 0 )\r
167                 {\r
168                         lReturnCode = FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG;\r
169                         FF_PRINTF( "prvFFRead: NOINIT\n" );\r
170                 }\r
171                 else if( ulSectorCount == 0ul )\r
172                 {\r
173                         lReturnCode = 0;\r
174                 }\r
175                 else\r
176                 {\r
177                         /* Convert LBA to byte address if needed */\r
178                         if( pxSDCardInstance->HCS == 0 )\r
179                         {\r
180                                 ulSectorNumber *= XSDPS_BLK_SIZE_512_MASK;\r
181                         }\r
182 \r
183                         pucReadBuffer = prvReadSDCardData( pucBuffer, 512UL * ulSectorCount );\r
184 \r
185                         if( ucIsCachedMemory( pucReadBuffer ) != pdFALSE )\r
186                         {\r
187                                 xCacheStats.xFailReadCount++;\r
188                         }\r
189 \r
190                         iResult  = XSdPs_ReadPolled( pxSDCardInstance, ulSectorNumber, ulSectorCount, pucReadBuffer );\r
191                         if( pucBuffer != pucReadBuffer )\r
192                         {\r
193                                 xCacheStats.xMemcpyReadCount++;\r
194                                 memcpy( pucBuffer, pucReadBuffer, 512 * ulSectorCount );\r
195                         }\r
196                         else\r
197                         {\r
198                                 xCacheStats.xPassReadCount++;\r
199                         }\r
200                         if( iResult == XST_SUCCESS )\r
201                         {\r
202                                 lReturnCode = 0l;\r
203                         }\r
204                         else\r
205                         {\r
206                                 lReturnCode = FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG;\r
207                         }\r
208                 }\r
209         }\r
210         else\r
211         {\r
212                 memset( ( void *) pucBuffer, '\0', ulSectorCount * 512 );\r
213 \r
214                 if( pxDisk->xStatus.bIsInitialised != pdFALSE )\r
215                 {\r
216                         FF_PRINTF( "prvFFRead: warning: %lu + %lu > %lu\n", ulSectorNumber, ulSectorCount, pxDisk->ulNumberOfSectors );\r
217                 }\r
218 \r
219                 lReturnCode = FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG;\r
220         }\r
221 \r
222         return lReturnCode;\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 static int32_t prvFFWrite( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk )\r
227 {\r
228 int32_t lReturnCode;\r
229 \r
230         if( ( pxDisk != NULL ) &&\r
231                 ( pxDisk->ulSignature == sdSIGNATURE ) &&\r
232                 ( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&\r
233                 ( ulSectorNumber < pxDisk->ulNumberOfSectors ) &&\r
234                 ( ( pxDisk->ulNumberOfSectors - ulSectorNumber ) >= ulSectorCount ) )\r
235         {\r
236                 int iResult;\r
237                 iResult = vSDMMC_Status(drive_nr);\r
238 \r
239                 if( ( iResult & STA_NODISK ) != 0 )\r
240                 {\r
241                         lReturnCode = FF_ERR_DRIVER_NOMEDIUM | FF_ERRFLAG;\r
242                         FF_PRINTF( "prvFFWrite: NOMEDIUM\n" );\r
243                 }\r
244                 else if( ( iResult & STA_NOINIT ) != 0 )\r
245                 {\r
246                         lReturnCode = FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG;\r
247                         FF_PRINTF( "prvFFWrite: NOINIT\n" );\r
248                 }\r
249                 else\r
250                 {\r
251                         if( ulSectorCount == 0ul )\r
252                         {\r
253                                 lReturnCode = 0l;\r
254                         }\r
255                         else\r
256                         {\r
257                                 /* Convert LBA to byte address if needed */\r
258                                 if (!(pxSDCardInstance->HCS)) ulSectorNumber *= XSDPS_BLK_SIZE_512_MASK;\r
259 \r
260                                 pucBuffer = ( uint8_t * )prvStoreSDCardData( pucBuffer, 512UL * ulSectorCount );\r
261 \r
262                                 if( ucIsCachedMemory( pucBuffer ) != pdFALSE )\r
263                                 {\r
264                                         xCacheStats.xFailWriteCount++;\r
265                                 }\r
266 \r
267                                 iResult = XSdPs_WritePolled( pxSDCardInstance, ulSectorNumber, ulSectorCount, pucBuffer );\r
268 \r
269                                 if( iResult == XST_SUCCESS )\r
270                                 {\r
271                                         lReturnCode = 0;\r
272                                 }\r
273                                 else\r
274                                 {\r
275                                         FF_PRINTF( "prvFFWrite[%d]: at 0x%X count %ld : %d\n",\r
276                                                 (int)drive_nr, (unsigned)ulSectorNumber, ulSectorCount, iResult );\r
277                                         lReturnCode = FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG;\r
278                                 }\r
279                         }\r
280                 }\r
281         }\r
282         else\r
283         {\r
284                 lReturnCode = FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG;\r
285                 if( pxDisk->xStatus.bIsInitialised )\r
286                 {\r
287                         FF_PRINTF( "prvFFWrite::read: warning: %lu + %lu > %lu\n",\r
288                                 ulSectorNumber, ulSectorCount, pxDisk->ulNumberOfSectors );\r
289                 }\r
290         }\r
291 \r
292         return lReturnCode;\r
293 }\r
294 /*-----------------------------------------------------------*/\r
295 \r
296 void FF_SDDiskFlush( FF_Disk_t *pxDisk )\r
297 {\r
298         if( ( pxDisk != NULL ) &&\r
299                 ( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&\r
300                 ( pxDisk->pxIOManager != NULL ) )\r
301         {\r
302                 FF_FlushCache( pxDisk->pxIOManager );\r
303         }\r
304 }\r
305 /*-----------------------------------------------------------*/\r
306 \r
307 static const uint8_t *prvStoreSDCardData( const uint8_t *pucBuffer, uint32_t ulByteCount )\r
308 {\r
309 const uint8_t *pucReturn;\r
310 \r
311         if( ( ucIsCachedMemory( pucBuffer ) != pdFALSE ) && ( ulByteCount <= sizeof( pxCacheMem->pucHelpMemory ) ) )\r
312         {\r
313                 memcpy( pxCacheMem->pucHelpMemory, pucBuffer, ulByteCount );\r
314                 pucReturn = pxCacheMem->pucHelpMemory;\r
315                 xCacheStats.xMemcpyWriteCount++;\r
316         }\r
317         else\r
318         {\r
319                 pucReturn = pucBuffer;\r
320                 xCacheStats.xPassWriteCount++;\r
321         }\r
322 \r
323         return pucReturn;\r
324 }\r
325 /*-----------------------------------------------------------*/\r
326 \r
327 static uint8_t *prvReadSDCardData( uint8_t *pucBuffer, uint32_t ulByteCount )\r
328 {\r
329 uint8_t *pucReturn;\r
330 \r
331         if( ( ucIsCachedMemory( pucBuffer ) != pdFALSE ) && ( ulByteCount <= sizeof( pxCacheMem->pucHelpMemory ) ) )\r
332         {\r
333                 pucReturn = pxCacheMem->pucHelpMemory;\r
334         }\r
335         else\r
336         {\r
337                 pucReturn = pucBuffer;\r
338         }\r
339 \r
340         return pucReturn;\r
341 }\r
342 /*-----------------------------------------------------------*/\r
343 \r
344 static struct xCACHE_MEMORY_INFO *pucGetSDIOCacheMemory( )\r
345 {\r
346         if( pxCacheMem == NULL )\r
347         {\r
348                 pxCacheMem = ( struct xCACHE_MEMORY_INFO * ) pucGetUncachedMemory( sizeof( *pxCacheMem ) );\r
349                 memset( pxCacheMem, '\0', sizeof( *pxCacheMem ) );\r
350         }\r
351         return pxCacheMem;\r
352 }\r
353 /*-----------------------------------------------------------*/\r
354 \r
355 /* Initialise the SDIO driver and mount an SD card */\r
356 FF_Disk_t *FF_SDDiskInit( const char *pcName )\r
357 {\r
358 FF_Error_t xFFError;\r
359 BaseType_t xPartitionNumber = 0;\r
360 FF_CreationParameters_t xParameters;\r
361 FF_Disk_t * pxDisk;\r
362 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )\r
363         int iIndex;\r
364 #endif\r
365 \r
366         pucGetSDIOCacheMemory();\r
367 \r
368         pxDisk = (FF_Disk_t *)pvPortMalloc( sizeof( *pxDisk ) );\r
369         if( pxDisk == NULL )\r
370         {\r
371                 FF_PRINTF( "FF_SDDiskInit: Malloc failed\n" );\r
372         }\r
373         else if( pxCacheMem == NULL )\r
374         {\r
375                 FF_PRINTF( "FF_SDDiskInit: Cached memory failed\n" );\r
376         }\r
377         else\r
378         {\r
379                 pxSDCardInstance = &( pxCacheMem->xSDCardInstance );\r
380 \r
381                 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )\r
382                 {\r
383                         for( iIndex = 0; iIndex < ARRAY_SIZE( xSDSemaphores ); iIndex++ )\r
384                         {\r
385                                 if( xSDSemaphores[ iIndex ] == NULL )\r
386                                 {\r
387                                         xSDSemaphores[ iIndex ] = xSemaphoreCreateBinary();\r
388                                         configASSERT( xSDSemaphores[ iIndex ] != NULL );\r
389                                 }\r
390                         }\r
391                 }\r
392                 #endif\r
393 \r
394                 vSDMMC_Init( 0 );\r
395 \r
396                 /* Initialise the created disk structure. */\r
397                 memset( pxDisk, '\0', sizeof( *pxDisk ) );\r
398 \r
399                 pxDisk->ulNumberOfSectors = myCSD.sd_last_block_address + 1;\r
400 \r
401                 if( xPlusFATMutex == NULL )\r
402                 {\r
403                         xPlusFATMutex = xSemaphoreCreateRecursiveMutex();\r
404                 }\r
405                 pxDisk->ulSignature = sdSIGNATURE;\r
406 \r
407                 if( xPlusFATMutex != NULL)\r
408                 {\r
409                         memset( &xParameters, '\0', sizeof( xParameters ) );\r
410                         xParameters.pucCacheMemory = pxCacheMem->pucCacheMemory;\r
411                         xParameters.ulMemorySize = sizeof( pxCacheMem->pucCacheMemory );\r
412                         xParameters.ulSectorSize = 512;\r
413                         xParameters.fnWriteBlocks = prvFFWrite;\r
414                         xParameters.fnReadBlocks = prvFFRead;\r
415                         xParameters.pxDisk = pxDisk;\r
416 \r
417                         /* prvFFRead()/prvFFWrite() are not re-entrant and must be protected with\r
418                         the use of a semaphore. */\r
419                         xParameters.xBlockDeviceIsReentrant = pdFALSE;\r
420 \r
421                         /* The semaphore will be used to protect critical sections in the +FAT driver,\r
422                         and also to avoid concurrent calls to prvFFRead()/prvFFWrite() from different tasks. */\r
423                         xParameters.pvSemaphore = ( void * ) xPlusFATMutex;\r
424 \r
425                         pxDisk->pxIOManager = FF_CreateIOManger( &xParameters, &xFFError );\r
426                         if( pxDisk->pxIOManager == NULL )\r
427                         {\r
428                                 FF_PRINTF( "FF_SDDiskInit: FF_CreateIOManger: %s\n", (const char*)FF_GetErrMessage( xFFError ) );\r
429                                 FF_SDDiskDelete( pxDisk );\r
430                                 pxDisk = NULL;\r
431                         }\r
432                         else\r
433                         {\r
434                                 pxDisk->xStatus.bIsInitialised = pdTRUE;\r
435                                 pxDisk->xStatus.bPartitionNumber = xPartitionNumber;\r
436 \r
437                                 if( FF_SDDiskMount( pxDisk ) == 0 )\r
438                                 {\r
439                                         FF_SDDiskDelete( pxDisk );\r
440                                         pxDisk = NULL;\r
441                                 }\r
442                                 else\r
443                                 {\r
444                                         if( pcName == NULL )\r
445                                         {\r
446                                                 pcName = "/";\r
447                                         }\r
448 \r
449                                         FF_FS_Add( pcName, pxDisk );\r
450                                         FF_PRINTF( "FF_SDDiskInit: Mounted SD-card as root \"%s\"\n", pcName );\r
451                                         FF_SDDiskShowPartition( pxDisk );\r
452                                 }\r
453                         }\r
454                 }\r
455         }\r
456 \r
457         return pxDisk;\r
458 }\r
459 /*-----------------------------------------------------------*/\r
460 \r
461 BaseType_t FF_SDDiskFormat( FF_Disk_t *pxDisk, BaseType_t aPart )\r
462 {\r
463 FF_Error_t xError;\r
464 BaseType_t xReturn = 0;\r
465 \r
466         FF_SDDiskUnmount( pxDisk );\r
467         {\r
468                 /* Format the drive */\r
469                 xError = FF_Format( pxDisk, aPart, pdFALSE, pdFALSE);  // Try FAT32 with large clusters\r
470                 if( FF_isERR( xError ) )\r
471                 {\r
472                         FF_PRINTF( "FF_SDDiskFormat: %s\n", (const char*)FF_GetErrMessage( xError ) );\r
473                         return 0;\r
474                 }\r
475                 else\r
476                 {\r
477                         FF_PRINTF( "FF_SDDiskFormat: OK, now remounting\n" );\r
478                         pxDisk->xStatus.bPartitionNumber = aPart;\r
479                         xError = FF_SDDiskMount( pxDisk );\r
480                         FF_PRINTF( "FF_SDDiskFormat: rc %08x\n", ( unsigned )xError );\r
481                         if( FF_isERR( xError ) == pdFALSE )\r
482                         {\r
483                                 xReturn = 1;\r
484                                 FF_SDDiskShowPartition( pxDisk );\r
485                         }\r
486                 }\r
487         }\r
488         return xReturn;\r
489 }\r
490 /*-----------------------------------------------------------*/\r
491 \r
492 /* Unmount the volume */\r
493 BaseType_t FF_SDDiskUnmount( FF_Disk_t *pxDisk )\r
494 {\r
495 FF_Error_t xFFError;\r
496 BaseType_t xReturn = 1;\r
497 \r
498         if( ( pxDisk != NULL ) && ( pxDisk->xStatus.bIsMounted != pdFALSE ) )\r
499         {\r
500                 pxDisk->xStatus.bIsMounted = pdFALSE;\r
501                 xFFError = FF_Unmount( pxDisk );\r
502                 FF_PRINTF( "FF_SDDiskUnmount: rc %08x\n", ( unsigned )xFFError );\r
503                 if( FF_isERR( xFFError ) )\r
504                 {\r
505                         xReturn = 0;\r
506                 }\r
507                 else\r
508                 {\r
509                         FF_PRINTF( "Drive unmounted\n" );\r
510                 }\r
511         }\r
512 \r
513         return xReturn;\r
514 }\r
515 /*-----------------------------------------------------------*/\r
516 \r
517 BaseType_t FF_SDDiskReinit( FF_Disk_t *pxDisk )\r
518 {\r
519 int iStatus = vSDMMC_Init( 0 ); /* Hard coded index. */\r
520 \r
521         /*_RB_ parameter not used. */\r
522         ( void ) pxDisk;\r
523 \r
524         FF_PRINTF( "FF_SDDiskReinit: rc %08x\n", ( unsigned )iStatus );\r
525         return iStatus;\r
526 }\r
527 /*-----------------------------------------------------------*/\r
528 \r
529 BaseType_t FF_SDDiskMount( FF_Disk_t *pxDisk )\r
530 {\r
531 FF_Error_t xFFError;\r
532 BaseType_t xReturn = 1;\r
533 \r
534         /* Mount the partition */\r
535         xFFError = FF_Mount( pxDisk, pxDisk->xStatus.bPartitionNumber );\r
536 \r
537         if( FF_isERR( xFFError ) )\r
538         {\r
539                 FF_PRINTF( "FF_SDDiskMount: %08lX\n", xFFError );\r
540                 xReturn = 0;\r
541         }\r
542         else\r
543         {\r
544                 pxDisk->xStatus.bIsMounted = pdTRUE;\r
545                 FF_PRINTF( "****** FreeRTOS+FAT initialized %lu sectors\n", pxDisk->pxIOManager->xPartition.ulTotalSectors );\r
546         }\r
547 \r
548         return xReturn;\r
549 }\r
550 /*-----------------------------------------------------------*/\r
551 \r
552 /* Get a pointer to IOMAN, which can be used for all FreeRTOS+FAT functions */\r
553 FF_IOManager_t *sddisk_ioman( FF_Disk_t *pxDisk )\r
554 {\r
555 FF_IOManager_t *pxReturn;\r
556 \r
557         if( ( pxDisk != NULL ) && ( pxDisk->xStatus.bIsInitialised != pdFALSE ) )\r
558         {\r
559                 pxReturn = pxDisk->pxIOManager;\r
560         }\r
561         else\r
562         {\r
563                 pxReturn = NULL;\r
564         }\r
565         return pxReturn;\r
566 }\r
567 /*-----------------------------------------------------------*/\r
568 \r
569 /* Release all resources */\r
570 BaseType_t FF_SDDiskDelete( FF_Disk_t *pxDisk )\r
571 {\r
572         if( pxDisk != NULL )\r
573         {\r
574                 pxDisk->ulSignature = 0;\r
575                 pxDisk->xStatus.bIsInitialised = 0;\r
576                 if( pxDisk->pxIOManager != NULL )\r
577                 {\r
578                         if( FF_Mounted( pxDisk->pxIOManager ) != pdFALSE )\r
579                         {\r
580                                 FF_Unmount( pxDisk );\r
581                         }\r
582                         FF_DeleteIOManager( pxDisk->pxIOManager );\r
583                 }\r
584 \r
585                 vPortFree( pxDisk );\r
586         }\r
587         return 1;\r
588 }\r
589 /*-----------------------------------------------------------*/\r
590 \r
591 BaseType_t FF_SDDiskShowPartition( FF_Disk_t *pxDisk )\r
592 {\r
593 FF_Error_t xError;\r
594 uint64_t ullFreeSectors;\r
595 uint32_t ulTotalSizeMB, ulFreeSizeMB;\r
596 int iPercentageFree;\r
597 FF_IOManager_t *pxIOManager;\r
598 const char *pcTypeName = "unknown type";\r
599 BaseType_t xReturn = pdPASS;\r
600 \r
601         if( pxDisk == NULL )\r
602         {\r
603                 xReturn = pdFAIL;\r
604         }\r
605         else\r
606         {\r
607                 pxIOManager = pxDisk->pxIOManager;\r
608 \r
609                 FF_PRINTF( "Reading FAT and calculating Free Space\n" );\r
610 \r
611                 switch( pxIOManager->xPartition.ucType )\r
612                 {\r
613                         case FF_T_FAT12:\r
614                                 pcTypeName = "FAT12";\r
615                                 break;\r
616 \r
617                         case FF_T_FAT16:\r
618                                 pcTypeName = "FAT16";\r
619                                 break;\r
620 \r
621                         case FF_T_FAT32:\r
622                                 pcTypeName = "FAT32";\r
623                                 break;\r
624 \r
625                         default:\r
626                                 pcTypeName = "UNKOWN";\r
627                                 break;\r
628                 }\r
629 \r
630                 FF_GetFreeSize( pxIOManager, &xError );\r
631 \r
632                 ullFreeSectors = pxIOManager->xPartition.ulFreeClusterCount * pxIOManager->xPartition.ulSectorsPerCluster;\r
633                 iPercentageFree = ( int ) ( ( HUNDRED_64_BIT * ullFreeSectors + pxIOManager->xPartition.ulDataSectors / 2 ) /\r
634                         ( ( uint64_t )pxIOManager->xPartition.ulDataSectors ) );\r
635 \r
636                 ulTotalSizeMB = pxIOManager->xPartition.ulDataSectors / SECTORS_PER_MB;\r
637                 ulFreeSizeMB = ( uint32_t ) ( ullFreeSectors / SECTORS_PER_MB );\r
638 \r
639                 /* It is better not to use the 64-bit format such as %Lu because it\r
640                 might not be implemented. */\r
641                 FF_PRINTF( "Partition Nr   %8u\n", pxDisk->xStatus.bPartitionNumber );\r
642                 FF_PRINTF( "Type           %8u (%s)\n", pxIOManager->xPartition.ucType, pcTypeName );\r
643                 FF_PRINTF( "VolLabel       '%8s' \n", pxIOManager->xPartition.pcVolumeLabel );\r
644                 FF_PRINTF( "TotalSectors   %8lu\n", pxIOManager->xPartition.ulTotalSectors );\r
645                 FF_PRINTF( "DataSectors    %8lu\n", pxIOManager->xPartition.ulDataSectors );\r
646                 FF_PRINTF( "SecsPerCluster %8lu\n", pxIOManager->xPartition.ulSectorsPerCluster );\r
647                 FF_PRINTF( "Size           %8lu MB\n", ulTotalSizeMB );\r
648                 FF_PRINTF( "FreeSize       %8lu MB ( %d perc free )\n", ulFreeSizeMB, iPercentageFree );\r
649                 FF_PRINTF( "BeginLBA       %8lu\n", pxIOManager->xPartition.ulBeginLBA );\r
650                 FF_PRINTF( "FATBeginLBA    %8lu\n", pxIOManager->xPartition.ulFATBeginLBA );\r
651         }\r
652 \r
653         return xReturn;\r
654 }\r
655 /*-----------------------------------------------------------*/\r
656 \r
657 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )\r
658         static void vInstallInterrupt( void )\r
659         {\r
660                 /* Install an interrupt handler for SDIO_0 */\r
661                 XScuGic_RegisterHandler( INTC_BASE_ADDR, SCUGIC_SDIO_0_INTR,\r
662                         ( Xil_ExceptionHandler )XSdPs_IntrHandler,\r
663                         ( void * )pxSDCardInstance );\r
664 \r
665                 /* Enable this interrupt. */\r
666                 XScuGic_EnableIntr( INTC_DIST_BASE_ADDR, SCUGIC_SDIO_0_INTR );\r
667 \r
668                 /* Choose the signals. */\r
669                 XSdPs_WriteReg16(pxSDCardInstance->Config.BaseAddress,\r
670                                 XSDPS_NORM_INTR_SIG_EN_OFFSET,\r
671                                 XSDPS_INTR_NORMAL_ENABLE );\r
672                 XSdPs_WriteReg16(pxSDCardInstance->Config.BaseAddress,\r
673                                 XSDPS_ERR_INTR_SIG_EN_OFFSET,\r
674                                 0x0 );\r
675         }\r
676 #endif /* ffconfigSDIO_DRIVER_USES_INTERRUPT */\r
677 /*-----------------------------------------------------------*/\r
678 \r
679 static int vSDMMC_Init( int iDriveNumber )\r
680 {\r
681 int iReturnCode, iStatus;\r
682 XSdPs_Config *SdConfig;\r
683 \r
684         /*_RB_ Function name not following convention, parameter not used, parameter\r
685         using plain int type. */\r
686 \r
687 \r
688         /* Open a do {} while(0) loop to allow the use of break. */\r
689         do\r
690         {\r
691                 /* Check if card is in the socket */\r
692                 iStatus = vSDMMC_Status( iDriveNumber );\r
693                 if( ( iStatus & STA_NODISK ) != 0 )\r
694                 {\r
695                         break;\r
696                 }\r
697 \r
698                 /* Assume that the initialisation will fail: set the 'STA_NOINIT' bit. */\r
699                 iStatus |= STA_NOINIT;\r
700 \r
701                 /* Initialize the host controller */\r
702                 SdConfig = XSdPs_LookupConfig(SD_DEVICE_ID);\r
703                 if( SdConfig == NULL )\r
704                 {\r
705                         break;\r
706                 }\r
707 \r
708                 iReturnCode = XSdPs_CfgInitialize(pxSDCardInstance, SdConfig, SdConfig->BaseAddress);\r
709                 if( iReturnCode != XST_SUCCESS )\r
710                 {\r
711                         break;\r
712                 }\r
713 \r
714                 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )\r
715                 {\r
716                         vInstallInterrupt();\r
717                 }\r
718                 #endif  /* ffconfigSDIO_DRIVER_USES_INTERRUPT */\r
719                 iReturnCode = XSdPs_CardInitialize( pxSDCardInstance );\r
720                 if( iReturnCode != XST_SUCCESS )\r
721                 {\r
722                         break;\r
723                 }\r
724 \r
725                 /* Disk is initialized OK: clear the 'STA_NOINIT' bit. */\r
726                 iStatus &= ~( STA_NOINIT );\r
727         } while( 0 );\r
728 \r
729         sd_disk_status = iStatus;\r
730 \r
731         return iStatus;\r
732 }\r
733 /*-----------------------------------------------------------*/\r
734 \r
735 static int vSDMMC_Status( int iDriveNumber )\r
736 {\r
737 int iStatus = sd_disk_status;\r
738 u32 ulStatusReg;\r
739 \r
740         /*_RB_ Function name not following convention, parameter not used, parameter\r
741         using plain int type. */\r
742         ( void ) iDriveNumber;\r
743 \r
744         ulStatusReg = XSdPs_GetPresentStatusReg( XPAR_XSDPS_0_BASEADDR );\r
745         if( ( ulStatusReg & XSDPS_PSR_CARD_INSRT_MASK ) == 0 )\r
746         {\r
747                 iStatus = STA_NODISK | STA_NOINIT;\r
748         }\r
749         else\r
750         {\r
751                 iStatus &= ~STA_NODISK;\r
752                 if( ( ulStatusReg & XSDPS_PSR_WPS_PL_MASK ) != 0 )\r
753                 {\r
754                         iStatus &= ~STA_PROTECT;\r
755                 }\r
756                 else\r
757                 {\r
758                         iStatus |= STA_PROTECT;\r
759                 }\r
760         }\r
761 \r
762         sd_disk_status = iStatus;\r
763         return iStatus;\r
764 }\r
765 /*-----------------------------------------------------------*/\r
766 \r
767 BaseType_t FF_SDDiskInserted( BaseType_t xDriveNr )\r
768 {\r
769 BaseType_t xReturn;\r
770 int iStatus;\r
771 \r
772         /* Check if card is in the socket */\r
773         iStatus = vSDMMC_Status( xDriveNr );\r
774         if( ( iStatus & STA_NODISK ) != 0 )\r
775         {\r
776                 xReturn = pdFALSE;\r
777         }\r
778         else\r
779         {\r
780                 xReturn = pdTRUE;\r
781         }\r
782 \r
783         return xReturn;\r
784 }\r
785 \r
786 volatile unsigned sd_int_count = 0;\r
787 \r
788 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )\r
789         volatile u32 ulSDInterruptStatus[2];\r
790 \r
791         void XSdPs_IntrHandler(void *XSdPsPtr)\r
792         {\r
793         XSdPs *InstancePtr = (XSdPs *)XSdPsPtr;\r
794         int iIndex = InstancePtr->Config.DeviceId;\r
795         uint32_t ulStatusReg;\r
796 \r
797                 configASSERT( iIndex <= 1 );\r
798                 sd_int_count++;\r
799 \r
800                 /* Read the current status. */\r
801                 ulStatusReg = XSdPs_ReadReg( InstancePtr->Config.BaseAddress, XSDPS_NORM_INTR_STS_OFFSET );\r
802 \r
803                 /* Write to clear error bits. */\r
804                 XSdPs_WriteReg( InstancePtr->Config.BaseAddress, XSDPS_NORM_INTR_STS_OFFSET, ulStatusReg );\r
805 \r
806                 /* The new value must be OR-ed, if not the\r
807                 Command Complete (CC) event might get overwritten\r
808                 by the Transfer Complete (TC) event. */\r
809                 ulSDInterruptStatus[ iIndex ] |= ulStatusReg;\r
810 \r
811                 if( ( ulStatusReg & ( XSDPS_INTR_CARD_INSRT_MASK | XSDPS_INTR_CARD_REM_MASK ) ) != 0 )\r
812                 {\r
813                         /* Could wake-up another task. */\r
814                 }\r
815                 if( xSDSemaphores[ iIndex ] != NULL )\r
816                 {\r
817                 BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
818 \r
819                         xSemaphoreGiveFromISR( xSDSemaphores[ iIndex ], &xHigherPriorityTaskWoken );\r
820                         if( xHigherPriorityTaskWoken != 0 )\r
821                         {\r
822                                 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
823                         }\r
824                 }\r
825         }\r
826 #endif /* ffconfigSDIO_DRIVER_USES_INTERRUPT */\r
827 /*-----------------------------------------------------------*/\r
828 \r
829 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )\r
830         void XSdPs_ClearInterrupt( XSdPs *InstancePtr )\r
831         {\r
832         int iIndex = InstancePtr->Config.DeviceId;\r
833 \r
834                 configASSERT( iIndex <= 1 );\r
835                 ulSDInterruptStatus[ iIndex ] = 0;\r
836         }\r
837 #endif /* ffconfigSDIO_DRIVER_USES_INTERRUPT */\r
838 /*-----------------------------------------------------------*/\r
839 \r
840 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )\r
841         /* Wait for an interrupt and return the 32 bits of the status register.\r
842         A return value of 0 means: time-out. */\r
843         u32 XSdPs_WaitInterrupt( XSdPs *InstancePtr, u32 ulMask, u32 ulWait )\r
844         {\r
845         u32 ulStatusReg;\r
846         int iIndex = InstancePtr->Config.DeviceId;\r
847         TickType_t xRemainingTime = pdMS_TO_TICKS( sdWAIT_INT_TIME_OUT_MS );\r
848         TimeOut_t xTimeOut;\r
849 \r
850                 if( ulWait == 0UL )\r
851                 {\r
852                         xRemainingTime = pdMS_TO_TICKS( sdQUICK_WAIT_INT_TIME_OUT_MS );\r
853                 }\r
854 \r
855                 configASSERT( iIndex <= 1 );\r
856                 configASSERT( xSDSemaphores[ iIndex ] != 0 );\r
857                 vTaskSetTimeOutState( &xTimeOut );\r
858                 /* Loop until:\r
859                 1. Expected bit (ulMask) becomes high\r
860                 2. Time-out reached (normally 2 seconds)\r
861                 */\r
862                 do\r
863                 {\r
864                         if( xRemainingTime != 0 )\r
865                         {\r
866                                 xSemaphoreTake( xSDSemaphores[ iIndex ], xRemainingTime );\r
867                         }\r
868                         ulStatusReg = ulSDInterruptStatus[ iIndex ];\r
869                         if( ( ulStatusReg & XSDPS_INTR_ERR_MASK ) != 0 )\r
870                         {\r
871                                 break;\r
872                         }\r
873                 }\r
874                 while( ( xTaskCheckForTimeOut( &xTimeOut, &xRemainingTime ) == pdFALSE ) &&\r
875                            ( ( ulStatusReg & ulMask ) == 0 ) );\r
876 \r
877                 if( ( ulStatusReg & ulMask ) == 0 )\r
878                 {\r
879                 ulStatusReg = XSdPs_ReadReg( InstancePtr->Config.BaseAddress, XSDPS_NORM_INTR_STS_OFFSET );\r
880                         if( ulWait != 0UL )\r
881                         {\r
882                                 FF_PRINTF( "XSdPs_WaitInterrupt[ %d ]: Got %08lx, expect %08lx ints: %d\n",\r
883                                         iIndex,\r
884                                         ulStatusReg,\r
885                                         ulMask,\r
886                                         sd_int_count );\r
887                         }\r
888                 }\r
889 \r
890                 return ulStatusReg;\r
891         }\r
892 \r
893 #endif /* ffconfigSDIO_DRIVER_USES_INTERRUPT */\r
894 /*-----------------------------------------------------------*/\r