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
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
13 * The above copyright notice and this permission notice shall be included in all
\r
14 * copies or substantial portions of the Software.
\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
23 * https://www.FreeRTOS.org
\r
27 /* Standard includes. */
\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
39 /* FreeRTOS includes. */
\r
40 #include "FreeRTOS.h"
\r
43 #include "portmacro.h"
\r
45 /* FreeRTOS+FAT includes. */
\r
46 #include "ff_headers.h"
\r
47 #include "ff_sddisk.h"
\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
55 #include "uncached_memory.h"
\r
57 #define sdSIGNATURE 0x41404342
\r
60 #define ARRAY_SIZE(x) (int) (sizeof(x)/sizeof(x)[0])
\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
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
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
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
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
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
95 /* Define a timeout on data transfers for SDIO: */
\r
96 #define sdWAIT_INT_TIME_OUT_MS 5000UL
\r
98 /* Define a short timeout, used during card-detection only (CMD1): */
\r
99 #define sdQUICK_WAIT_INT_TIME_OUT_MS 1000UL
\r
101 /* XSdPs xSDCardInstance; */
\r
102 static XSdPs *pxSDCardInstance;
\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
112 static int vSDMMC_Init( int iDriveNumber );
\r
113 static int vSDMMC_Status( int iDriveNumber );
\r
115 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )
\r
116 static void vInstallInterrupt( void );
\r
119 struct xCACHE_MEMORY_INFO
\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
128 struct xCACHE_STATS
\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
138 struct xCACHE_STATS xCacheStats;
\r
139 struct xCACHE_MEMORY_INFO *pxCacheMem = NULL;
\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
144 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )
\r
145 void XSdPs_IntrHandler(void *XSdPsPtr);
\r
146 #endif /* ffconfigSDIO_DRIVER_USES_INTERRUPT */
\r
148 static int32_t prvFFRead( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk )
\r
150 int32_t lReturnCode;
\r
152 uint8_t *pucReadBuffer;
\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
160 iResult = vSDMMC_Status( drive_nr );
\r
161 if( ( iResult & STA_NODISK ) != 0 )
\r
163 lReturnCode = FF_ERR_DRIVER_NOMEDIUM | FF_ERRFLAG;
\r
164 FF_PRINTF( "prvFFRead: NOMEDIUM\n" );
\r
166 else if( ( iResult & STA_NOINIT ) != 0 )
\r
168 lReturnCode = FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG;
\r
169 FF_PRINTF( "prvFFRead: NOINIT\n" );
\r
171 else if( ulSectorCount == 0ul )
\r
177 /* Convert LBA to byte address if needed */
\r
178 if( pxSDCardInstance->HCS == 0 )
\r
180 ulSectorNumber *= XSDPS_BLK_SIZE_512_MASK;
\r
183 pucReadBuffer = prvReadSDCardData( pucBuffer, 512UL * ulSectorCount );
\r
185 if( ucIsCachedMemory( pucReadBuffer ) != pdFALSE )
\r
187 xCacheStats.xFailReadCount++;
\r
190 iResult = XSdPs_ReadPolled( pxSDCardInstance, ulSectorNumber, ulSectorCount, pucReadBuffer );
\r
191 if( pucBuffer != pucReadBuffer )
\r
193 xCacheStats.xMemcpyReadCount++;
\r
194 memcpy( pucBuffer, pucReadBuffer, 512 * ulSectorCount );
\r
198 xCacheStats.xPassReadCount++;
\r
200 if( iResult == XST_SUCCESS )
\r
206 lReturnCode = FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG;
\r
212 memset( ( void *) pucBuffer, '\0', ulSectorCount * 512 );
\r
214 if( pxDisk->xStatus.bIsInitialised != pdFALSE )
\r
216 FF_PRINTF( "prvFFRead: warning: %lu + %lu > %lu\n", ulSectorNumber, ulSectorCount, pxDisk->ulNumberOfSectors );
\r
219 lReturnCode = FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG;
\r
222 return lReturnCode;
\r
224 /*-----------------------------------------------------------*/
\r
226 static int32_t prvFFWrite( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk )
\r
228 int32_t lReturnCode;
\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
237 iResult = vSDMMC_Status(drive_nr);
\r
239 if( ( iResult & STA_NODISK ) != 0 )
\r
241 lReturnCode = FF_ERR_DRIVER_NOMEDIUM | FF_ERRFLAG;
\r
242 FF_PRINTF( "prvFFWrite: NOMEDIUM\n" );
\r
244 else if( ( iResult & STA_NOINIT ) != 0 )
\r
246 lReturnCode = FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG;
\r
247 FF_PRINTF( "prvFFWrite: NOINIT\n" );
\r
251 if( ulSectorCount == 0ul )
\r
257 /* Convert LBA to byte address if needed */
\r
258 if (!(pxSDCardInstance->HCS)) ulSectorNumber *= XSDPS_BLK_SIZE_512_MASK;
\r
260 pucBuffer = ( uint8_t * )prvStoreSDCardData( pucBuffer, 512UL * ulSectorCount );
\r
262 if( ucIsCachedMemory( pucBuffer ) != pdFALSE )
\r
264 xCacheStats.xFailWriteCount++;
\r
267 iResult = XSdPs_WritePolled( pxSDCardInstance, ulSectorNumber, ulSectorCount, pucBuffer );
\r
269 if( iResult == XST_SUCCESS )
\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
284 lReturnCode = FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG;
\r
285 if( pxDisk->xStatus.bIsInitialised )
\r
287 FF_PRINTF( "prvFFWrite::read: warning: %lu + %lu > %lu\n",
\r
288 ulSectorNumber, ulSectorCount, pxDisk->ulNumberOfSectors );
\r
292 return lReturnCode;
\r
294 /*-----------------------------------------------------------*/
\r
296 void FF_SDDiskFlush( FF_Disk_t *pxDisk )
\r
298 if( ( pxDisk != NULL ) &&
\r
299 ( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&
\r
300 ( pxDisk->pxIOManager != NULL ) )
\r
302 FF_FlushCache( pxDisk->pxIOManager );
\r
305 /*-----------------------------------------------------------*/
\r
307 static const uint8_t *prvStoreSDCardData( const uint8_t *pucBuffer, uint32_t ulByteCount )
\r
309 const uint8_t *pucReturn;
\r
311 if( ( ucIsCachedMemory( pucBuffer ) != pdFALSE ) && ( ulByteCount <= sizeof( pxCacheMem->pucHelpMemory ) ) )
\r
313 memcpy( pxCacheMem->pucHelpMemory, pucBuffer, ulByteCount );
\r
314 pucReturn = pxCacheMem->pucHelpMemory;
\r
315 xCacheStats.xMemcpyWriteCount++;
\r
319 pucReturn = pucBuffer;
\r
320 xCacheStats.xPassWriteCount++;
\r
325 /*-----------------------------------------------------------*/
\r
327 static uint8_t *prvReadSDCardData( uint8_t *pucBuffer, uint32_t ulByteCount )
\r
329 uint8_t *pucReturn;
\r
331 if( ( ucIsCachedMemory( pucBuffer ) != pdFALSE ) && ( ulByteCount <= sizeof( pxCacheMem->pucHelpMemory ) ) )
\r
333 pucReturn = pxCacheMem->pucHelpMemory;
\r
337 pucReturn = pucBuffer;
\r
342 /*-----------------------------------------------------------*/
\r
344 static struct xCACHE_MEMORY_INFO *pucGetSDIOCacheMemory( )
\r
346 if( pxCacheMem == NULL )
\r
348 pxCacheMem = ( struct xCACHE_MEMORY_INFO * ) pucGetUncachedMemory( sizeof( *pxCacheMem ) );
\r
349 memset( pxCacheMem, '\0', sizeof( *pxCacheMem ) );
\r
353 /*-----------------------------------------------------------*/
\r
355 /* Initialise the SDIO driver and mount an SD card */
\r
356 FF_Disk_t *FF_SDDiskInit( const char *pcName )
\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
366 pucGetSDIOCacheMemory();
\r
368 pxDisk = (FF_Disk_t *)pvPortMalloc( sizeof( *pxDisk ) );
\r
369 if( pxDisk == NULL )
\r
371 FF_PRINTF( "FF_SDDiskInit: Malloc failed\n" );
\r
373 else if( pxCacheMem == NULL )
\r
375 FF_PRINTF( "FF_SDDiskInit: Cached memory failed\n" );
\r
379 pxSDCardInstance = &( pxCacheMem->xSDCardInstance );
\r
381 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )
\r
383 for( iIndex = 0; iIndex < ARRAY_SIZE( xSDSemaphores ); iIndex++ )
\r
385 if( xSDSemaphores[ iIndex ] == NULL )
\r
387 xSDSemaphores[ iIndex ] = xSemaphoreCreateBinary();
\r
388 configASSERT( xSDSemaphores[ iIndex ] != NULL );
\r
396 /* Initialise the created disk structure. */
\r
397 memset( pxDisk, '\0', sizeof( *pxDisk ) );
\r
399 pxDisk->ulNumberOfSectors = myCSD.sd_last_block_address + 1;
\r
401 if( xPlusFATMutex == NULL )
\r
403 xPlusFATMutex = xSemaphoreCreateRecursiveMutex();
\r
405 pxDisk->ulSignature = sdSIGNATURE;
\r
407 if( xPlusFATMutex != NULL)
\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
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
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
425 pxDisk->pxIOManager = FF_CreateIOManger( &xParameters, &xFFError );
\r
426 if( pxDisk->pxIOManager == NULL )
\r
428 FF_PRINTF( "FF_SDDiskInit: FF_CreateIOManger: %s\n", (const char*)FF_GetErrMessage( xFFError ) );
\r
429 FF_SDDiskDelete( pxDisk );
\r
434 pxDisk->xStatus.bIsInitialised = pdTRUE;
\r
435 pxDisk->xStatus.bPartitionNumber = xPartitionNumber;
\r
437 if( FF_SDDiskMount( pxDisk ) == 0 )
\r
439 FF_SDDiskDelete( pxDisk );
\r
444 if( pcName == NULL )
\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
459 /*-----------------------------------------------------------*/
\r
461 BaseType_t FF_SDDiskFormat( FF_Disk_t *pxDisk, BaseType_t aPart )
\r
464 BaseType_t xReturn = 0;
\r
466 FF_SDDiskUnmount( pxDisk );
\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
472 FF_PRINTF( "FF_SDDiskFormat: %s\n", (const char*)FF_GetErrMessage( xError ) );
\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
484 FF_SDDiskShowPartition( pxDisk );
\r
490 /*-----------------------------------------------------------*/
\r
492 /* Unmount the volume */
\r
493 BaseType_t FF_SDDiskUnmount( FF_Disk_t *pxDisk )
\r
495 FF_Error_t xFFError;
\r
496 BaseType_t xReturn = 1;
\r
498 if( ( pxDisk != NULL ) && ( pxDisk->xStatus.bIsMounted != pdFALSE ) )
\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
509 FF_PRINTF( "Drive unmounted\n" );
\r
515 /*-----------------------------------------------------------*/
\r
517 BaseType_t FF_SDDiskReinit( FF_Disk_t *pxDisk )
\r
519 int iStatus = vSDMMC_Init( 0 ); /* Hard coded index. */
\r
521 /*_RB_ parameter not used. */
\r
524 FF_PRINTF( "FF_SDDiskReinit: rc %08x\n", ( unsigned )iStatus );
\r
527 /*-----------------------------------------------------------*/
\r
529 BaseType_t FF_SDDiskMount( FF_Disk_t *pxDisk )
\r
531 FF_Error_t xFFError;
\r
532 BaseType_t xReturn = 1;
\r
534 /* Mount the partition */
\r
535 xFFError = FF_Mount( pxDisk, pxDisk->xStatus.bPartitionNumber );
\r
537 if( FF_isERR( xFFError ) )
\r
539 FF_PRINTF( "FF_SDDiskMount: %08lX\n", xFFError );
\r
544 pxDisk->xStatus.bIsMounted = pdTRUE;
\r
545 FF_PRINTF( "****** FreeRTOS+FAT initialized %lu sectors\n", pxDisk->pxIOManager->xPartition.ulTotalSectors );
\r
550 /*-----------------------------------------------------------*/
\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
555 FF_IOManager_t *pxReturn;
\r
557 if( ( pxDisk != NULL ) && ( pxDisk->xStatus.bIsInitialised != pdFALSE ) )
\r
559 pxReturn = pxDisk->pxIOManager;
\r
567 /*-----------------------------------------------------------*/
\r
569 /* Release all resources */
\r
570 BaseType_t FF_SDDiskDelete( FF_Disk_t *pxDisk )
\r
572 if( pxDisk != NULL )
\r
574 pxDisk->ulSignature = 0;
\r
575 pxDisk->xStatus.bIsInitialised = 0;
\r
576 if( pxDisk->pxIOManager != NULL )
\r
578 if( FF_Mounted( pxDisk->pxIOManager ) != pdFALSE )
\r
580 FF_Unmount( pxDisk );
\r
582 FF_DeleteIOManager( pxDisk->pxIOManager );
\r
585 vPortFree( pxDisk );
\r
589 /*-----------------------------------------------------------*/
\r
591 BaseType_t FF_SDDiskShowPartition( FF_Disk_t *pxDisk )
\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
601 if( pxDisk == NULL )
\r
607 pxIOManager = pxDisk->pxIOManager;
\r
609 FF_PRINTF( "Reading FAT and calculating Free Space\n" );
\r
611 switch( pxIOManager->xPartition.ucType )
\r
614 pcTypeName = "FAT12";
\r
618 pcTypeName = "FAT16";
\r
622 pcTypeName = "FAT32";
\r
626 pcTypeName = "UNKOWN";
\r
630 FF_GetFreeSize( pxIOManager, &xError );
\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
636 ulTotalSizeMB = pxIOManager->xPartition.ulDataSectors / SECTORS_PER_MB;
\r
637 ulFreeSizeMB = ( uint32_t ) ( ullFreeSectors / SECTORS_PER_MB );
\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
655 /*-----------------------------------------------------------*/
\r
657 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )
\r
658 static void vInstallInterrupt( void )
\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
665 /* Enable this interrupt. */
\r
666 XScuGic_EnableIntr( INTC_DIST_BASE_ADDR, SCUGIC_SDIO_0_INTR );
\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
676 #endif /* ffconfigSDIO_DRIVER_USES_INTERRUPT */
\r
677 /*-----------------------------------------------------------*/
\r
679 static int vSDMMC_Init( int iDriveNumber )
\r
681 int iReturnCode, iStatus;
\r
682 XSdPs_Config *SdConfig;
\r
684 /*_RB_ Function name not following convention, parameter not used, parameter
\r
685 using plain int type. */
\r
688 /* Open a do {} while(0) loop to allow the use of break. */
\r
691 /* Check if card is in the socket */
\r
692 iStatus = vSDMMC_Status( iDriveNumber );
\r
693 if( ( iStatus & STA_NODISK ) != 0 )
\r
698 /* Assume that the initialisation will fail: set the 'STA_NOINIT' bit. */
\r
699 iStatus |= STA_NOINIT;
\r
701 /* Initialize the host controller */
\r
702 SdConfig = XSdPs_LookupConfig(SD_DEVICE_ID);
\r
703 if( SdConfig == NULL )
\r
708 iReturnCode = XSdPs_CfgInitialize(pxSDCardInstance, SdConfig, SdConfig->BaseAddress);
\r
709 if( iReturnCode != XST_SUCCESS )
\r
714 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )
\r
716 vInstallInterrupt();
\r
718 #endif /* ffconfigSDIO_DRIVER_USES_INTERRUPT */
\r
719 iReturnCode = XSdPs_CardInitialize( pxSDCardInstance );
\r
720 if( iReturnCode != XST_SUCCESS )
\r
725 /* Disk is initialized OK: clear the 'STA_NOINIT' bit. */
\r
726 iStatus &= ~( STA_NOINIT );
\r
729 sd_disk_status = iStatus;
\r
733 /*-----------------------------------------------------------*/
\r
735 static int vSDMMC_Status( int iDriveNumber )
\r
737 int iStatus = sd_disk_status;
\r
740 /*_RB_ Function name not following convention, parameter not used, parameter
\r
741 using plain int type. */
\r
742 ( void ) iDriveNumber;
\r
744 ulStatusReg = XSdPs_GetPresentStatusReg( XPAR_XSDPS_0_BASEADDR );
\r
745 if( ( ulStatusReg & XSDPS_PSR_CARD_INSRT_MASK ) == 0 )
\r
747 iStatus = STA_NODISK | STA_NOINIT;
\r
751 iStatus &= ~STA_NODISK;
\r
752 if( ( ulStatusReg & XSDPS_PSR_WPS_PL_MASK ) != 0 )
\r
754 iStatus &= ~STA_PROTECT;
\r
758 iStatus |= STA_PROTECT;
\r
762 sd_disk_status = iStatus;
\r
765 /*-----------------------------------------------------------*/
\r
767 BaseType_t FF_SDDiskInserted( BaseType_t xDriveNr )
\r
769 BaseType_t xReturn;
\r
772 /* Check if card is in the socket */
\r
773 iStatus = vSDMMC_Status( xDriveNr );
\r
774 if( ( iStatus & STA_NODISK ) != 0 )
\r
786 volatile unsigned sd_int_count = 0;
\r
788 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )
\r
789 volatile u32 ulSDInterruptStatus[2];
\r
791 void XSdPs_IntrHandler(void *XSdPsPtr)
\r
793 XSdPs *InstancePtr = (XSdPs *)XSdPsPtr;
\r
794 int iIndex = InstancePtr->Config.DeviceId;
\r
795 uint32_t ulStatusReg;
\r
797 configASSERT( iIndex <= 1 );
\r
800 /* Read the current status. */
\r
801 ulStatusReg = XSdPs_ReadReg( InstancePtr->Config.BaseAddress, XSDPS_NORM_INTR_STS_OFFSET );
\r
803 /* Write to clear error bits. */
\r
804 XSdPs_WriteReg( InstancePtr->Config.BaseAddress, XSDPS_NORM_INTR_STS_OFFSET, ulStatusReg );
\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
811 if( ( ulStatusReg & ( XSDPS_INTR_CARD_INSRT_MASK | XSDPS_INTR_CARD_REM_MASK ) ) != 0 )
\r
813 /* Could wake-up another task. */
\r
815 if( xSDSemaphores[ iIndex ] != NULL )
\r
817 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
819 xSemaphoreGiveFromISR( xSDSemaphores[ iIndex ], &xHigherPriorityTaskWoken );
\r
820 if( xHigherPriorityTaskWoken != 0 )
\r
822 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
826 #endif /* ffconfigSDIO_DRIVER_USES_INTERRUPT */
\r
827 /*-----------------------------------------------------------*/
\r
829 #if( ffconfigSDIO_DRIVER_USES_INTERRUPT != 0 )
\r
830 void XSdPs_ClearInterrupt( XSdPs *InstancePtr )
\r
832 int iIndex = InstancePtr->Config.DeviceId;
\r
834 configASSERT( iIndex <= 1 );
\r
835 ulSDInterruptStatus[ iIndex ] = 0;
\r
837 #endif /* ffconfigSDIO_DRIVER_USES_INTERRUPT */
\r
838 /*-----------------------------------------------------------*/
\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
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
850 if( ulWait == 0UL )
\r
852 xRemainingTime = pdMS_TO_TICKS( sdQUICK_WAIT_INT_TIME_OUT_MS );
\r
855 configASSERT( iIndex <= 1 );
\r
856 configASSERT( xSDSemaphores[ iIndex ] != 0 );
\r
857 vTaskSetTimeOutState( &xTimeOut );
\r
859 1. Expected bit (ulMask) becomes high
\r
860 2. Time-out reached (normally 2 seconds)
\r
864 if( xRemainingTime != 0 )
\r
866 xSemaphoreTake( xSDSemaphores[ iIndex ], xRemainingTime );
\r
868 ulStatusReg = ulSDInterruptStatus[ iIndex ];
\r
869 if( ( ulStatusReg & XSDPS_INTR_ERR_MASK ) != 0 )
\r
874 while( ( xTaskCheckForTimeOut( &xTimeOut, &xRemainingTime ) == pdFALSE ) &&
\r
875 ( ( ulStatusReg & ulMask ) == 0 ) );
\r
877 if( ( ulStatusReg & ulMask ) == 0 )
\r
879 ulStatusReg = XSdPs_ReadReg( InstancePtr->Config.BaseAddress, XSDPS_NORM_INTR_STS_OFFSET );
\r
880 if( ulWait != 0UL )
\r
882 FF_PRINTF( "XSdPs_WaitInterrupt[ %d ]: Got %08lx, expect %08lx ints: %d\n",
\r
890 return ulStatusReg;
\r
893 #endif /* ffconfigSDIO_DRIVER_USES_INTERRUPT */
\r
894 /*-----------------------------------------------------------*/
\r