"open" a file - return error using FatFs plus (Lab) - with NOR Flash as a device

Hello,
with your assistance, I successfully used a NOR flash as a fatFs media.
First, manage to mount the media . : Following the next steps:

  1. FF_CreateIOManager(&xParameters, &xFFError);
  2. FF_Partition(pxDisk, &xPartition);
  3. FF_Format(pxDisk, 0, pdTRUE, pdTRUE).
  4. FF_FlashMount(PxDisk)
    next:
    I attempt to open a file with the following simple code:
 /* Generate a file name. */ 
char cFileName[fsMAX_FILE_NAME_LEN]; sprintf(cFileName, "root%03d.txt", (int) xFileNumber)
/* Open the file, creating it if it doesn't exist. */
pxFile = ff_fopen(cFileName, "w");

The ff_fopen function calls FF_FS_Find(pcFile, &xHandler), which in turn calls the following code:

int FF_FS_Find( const char * pcPath,  FF_DirHandler_t * pxHandler )
{
    FF_SubSystem_t * pxSubSystem;
    size_t uxPathLength;
    BaseType_t xUseIndex;
    int iReturn;

    pxSubSystem = file_systems.xSystems + 1;
    uxPathLength = strlen( pcPath );

    memset( pxHandler, '\0', sizeof( *pxHandler ) );

    for( xUseIndex = 1; xUseIndex < file_systems.xFileSystemCount; xUseIndex++, pxSubSystem++ )
    {
        /* System "/ram" should not match with "/ram/etc". */
        if( ( uxPathLength >= ( size_t ) pxSubSystem->xPathlen ) &&
            ( memcmp( pxSubSystem->pcPath, pcPath, ( size_t ) pxSubSystem->xPathlen ) == 0 ) &&
            ( ( pcPath[ pxSubSystem->xPathlen ] == '\0' ) || ( pcPath[ pxSubSystem->xPathlen ] == '/' ) ) )
        {
            if( pcPath[ pxSubSystem->xPathlen ] == '\0' )
            {
                pxHandler->pcPath = rootDir;
            }
            else
            {
                pxHandler->pcPath = pcPath + pxSubSystem->xPathlen;
            }

            pxHandler->pxManager = pxSubSystem->pxManager;
            break;
        }
    }

    if( xUseIndex == file_systems.xFileSystemCount )
    {
        pxHandler->pcPath = pcPath;
        pxHandler->pxManager = file_systems.xSystems[ 0 ].pxManager;
    }

    if( FF_Mounted( pxHandler->pxManager ) )
    {
        iReturn = pdTRUE;
    }
    else
    {
        iReturn = pdFALSE;
    }

    return iReturn;
}

The ‘if’ statement inside the for loop evaluates to FALSE.
then FF_Mounted - failed because pxIOManager==0 (NULL)

Is there a problem with the FatFs configuration?
If you require any additional information, please let me know.
Thnaks
William

Just added the file_systems content that helps - - this is not inspected by the code since the condition in the for loop stops on xUseIndex =1 and does not check xUseIndex = 2 (because the condition is not met).

and this the first index (0):as you can see it is actual empty - and this what the FF_Mounted try to use !!

Hi @wshany1234
Looking at the memory dump
xSystems[1].pcPath = "/flash"
But since the if-condition failed, that caused the fallback to trigger:

pxHandler->pxManager = file_systems.xSystems[0].pxManager;

Since xSystems[0].pxManager = NULL , FF_Mounted fails.

Your pcPath is set via

sprintf(cFileName, "root%03d.txt", (int) xFileNumber);

pcPath = cFileName = "root000.txt" ( for xFileNumber = 0 ).

It might be possible when trying to do memcmp("/flash", "root000.txt", 6) , it fails because the mounted path prefix is “/flash”.

You should try changing how you build your file path

char cFileName[fsMAX_FILE_NAME_LEN];
sprintf(cFileName, "/flash/root%03d.txt", (int)xFileNumber);
1 Like