Regarding another issue,
I’d like to provide the nor flash fatfs initialize function (see below) , which was based on a web examples .
I have a couple questions about this code: slight_smile:
1.Could you kindly go over this flow and review it? Is it the proper technique to begin the fatfs?
2. The line xParameters.ulMemorySize = FLASH_MEMORY_TOTAL_SIZE; causes a problem inside FF_CreateIOManager() because it tries to allocate the FLASH_MEMORY_TOTAL_SIZE on the heap (0x400000), which can be done but exceeds the memory size. What is the correct value to run the code?
william
FF_Disk_t * FF_FlashDiskInit( const char * pcName )
{
FF_Error_t xFFError;
BaseType_t xPartitionNumber = 0;
FF_CreationParameters_t xParameters;
FF_Error_t perrot = 0;
xFlashStatus = prvFlashMMCInit( 0 );
if( xFlashStatus != pdPASS )
{
FF_PRINTF( "FF_FlashInit: prvFLASHMMCInit failed\n" );
pxDisk = NULL;
}
else
{
pxDisk = ( FF_Disk_t * ) ffconfigMALLOC( sizeof( *pxDisk ) );
}
if( pxDisk == NULL )
FF_PRINTF( "FF_FlashInit: Malloc failed\n" );
else
/* Initialise the created disk structure. */
memset( pxDisk, '\0', sizeof( *pxDisk ) );
pxDisk->ulNumberOfSectors = xFlashInfo.CardCapacity/ FLASH_MEMORY_SECTOR_SIZE;
if( xPlusFATMutex == NULL )
{
xPlusFATMutex = xSemaphoreCreateRecursiveMutex();
}
pxDisk->ulSignature = flashSIGNATURE;
if( xPlusFATMutex != NULL )
{
xParameters.pucCacheMemory = NULL;
memset( &xParameters, '\0', sizeof( xParameters ) );
xParameters.ulMemorySize = FLASH_MEMORY_TOTAL_SIZE;
xParameters.ulSectorSize = FLASH_MEMORY_SECTOR_SIZE;
xParameters.fnWriteBlocks = prvFFWrite;
xParameters.fnReadBlocks = prvFFRead;
xParameters.pxDisk = pxDisk;
#if ( FF_USE_STATIC_CACHE != 0 )
xParameters.pucCacheMemory = ( uint8_t * ) pucFFBuffer;
#endif
/* prvFFRead()/prvFFWrite() are not re-entrant and must be
* protected with the use of a semaphore. */
xParameters.xBlockDeviceIsReentrant = pdFALSE;
/* The semaphore will be used to protect critical sections in
* the +FAT driver, and also to avoid concurrent calls to
* prvFFRead()/prvFFWrite() from different tasks. */
xParameters.pvSemaphore = ( void * ) xPlusFATMutex;
pxDisk->pxIOManager = FF_CreateIOManager( &xParameters, &xFFError );
QUADSPI_InitTypeDef_s quadspi_flash_init_params = QUADSPI_DeviceHandleTypeDefArr[0];
if( pxDisk->pxIOManager == NULL )
{
FF_PRINTF( "FF_FlashInit: FF_CreateIOManager: %s\n", ( const char * ) FF_GetErrMessage( xFFError ) );
FF_FlashDelete( pxDisk );
uint16_t sector = 0x400;
//erase all the flash
QUADSPI_SectorErase(&quadspi_flash_init_params, 0 * FLASH_MEMORY_SECTOR_SIZE,(sector ) * FLASH_MEMORY_SECTOR_SIZE - 1);
pxDisk = NULL;
}
else
{
pxDisk->xStatus.bIsInitialised = pdTRUE;
pxDisk->xStatus.bPartitionNumber = xPartitionNumber;
FF_PartitionParameters_t xPartition;
/* Media cannot be used until it has been partitioned. In this
case a single partition is to be created that fills all available space - so
by clearing the xPartition structure to zero. */
memset( &xPartition, 0x00, sizeof( xPartition ) );
xPartition.ulSectorCount = pxDisk->ulNumberOfSectors;
xPartition.ulHiddenSectors = HIDDEN_SECTOR_COUNT;
xPartition.xPrimaryCount = PRIMARY_PARTITIONS;
xPartition.eSizeType = eSizeIsQuota;
perrot = FF_Partition( pxDisk, &xPartition );
//format the fatfs
FF_Format(pxDisk, 0 , pdTRUE ,pdTRUE );
/* Create the FLASH disk. */
// QUADSPI_EnableMemoryMap(quadspi_flash_init_params.device_handle);
if( FF_FlashMount( pxDisk ) == 0 )
{
FF_FlashDelete( pxDisk );
pxDisk = NULL;
}
else
{
if( pcName == NULL )
{
pcName = "/";
}
FF_FS_Add( pcName, pxDisk );
FF_PRINTF( "FF_FlashInit: Mounted Flash as root \"%s\"\n", pcName );
FF_FlashShowPartition( pxDisk );
}
} /* if( pxDisk->pxIOManager != NULL ) */
} /* if( xPlusFATMutex != NULL) */
return pxDisk;
}