FAT SL - Problem with creating files. pxFile points to NULL

I am trying to set up FAT SL filesystem to run in the FLASH memory of the MPC5748G – I am using a block with a size of 32KB. Initializing, and formatting seem to run fine (the functions return 0), however when I try to create a file using f_open(), it returns a NULL pointer.
What I don’t understand is that the pxFILE struct, that gets returned has other variables filled out, and they seem correct. I’m including a picture from the debugger.

I’m really stuck and don’t understand why this is happening, since the formatting and initializing the system seem to work. I would be grateful for any suggestions.

My code follows.


xTaskCreate(vfileSystemTestTask, "vfileSystemTestTask", 1024U, NULL, 3, NULL); 



void vfileSystemTestTask(void* pvParameters){
status_t ret = STATUS_SUCCESS;
unsigned char ucStatus;


	// Flash Initialization
	ret = FLASH_DRV_Init();


	// First create the volume.
	ucStatus = f_initvolume( flash_initfunc );

	// It is expected that the volume is not formatted.
	if( ucStatus == F_ERR_NOTFORMATTED ){
		// Format the created volume.
		ucStatus = f_format( F_FAT12_MEDIA );
		UARTPrint("Formatting returns: %d", ucStatus); //returns 0

	if( ucStatus == F_NO_ERROR ){
		UARTPrint("Filesystem already intialised and formatted. ");

      vTaskDelete(NULL);  //tried also with a while loop here. seems to make no difference


BaseType_t prvTOUCHCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString ){
const char *pcParameter;
BaseType_t xParameterStringLength;
unsigned char ucReturned;
char cFileName[ fsMAX_FILE_NAME_LEN ];
F_FILE *pxFile;

	// This function assumes xWriteBufferLen is large enough!
	( void ) xWriteBufferLen;

	// Obtain the parameter string.
	pcParameter = FreeRTOS_CLIGetParameter(
						pcCommandString,		// The command string itself.
						1,						// Return the first parameter.
						&xParameterStringLength	// Store the parameter string length.

	// Sanity check something was returned.
	configASSERT( pcParameter );

	// check if the file name isn't tooo big
	if (xParameterStringLength < fsMAX_FILE_NAME_LEN){
		// Attempt to create the file.
		pxFile = f_open( pcParameter, "w" ); //THIS POINTER IS 0x00!!!

		sprintf( pcWriteBuffer, "File has been created.");
		//print that error msg
		sprintf( pcWriteBuffer, "Filename too long. Max allowed filename length: %d", fsMAX_FILE_NAME_LEN);

		strcat( pcWriteBuffer, cliNEW_LINE );
		return pdFALSE;

Filesystem specific:

#define F_FLASH_DRIVE0 0


#define MDRIVER_FLASH_SECTOR_SIZE   512       /* Sector size */

#define MDRIVER_FLASH_VOLUME0_SIZE  (512 * 64) /* defintion for size of flashdrive*/

#define MDRIVER_MEM_LONG_ACCESS   0         /* set this value to 1 if 32bit access available WAS 1 */
F_DRIVER * flash_initfunc ( unsigned long driver_param ){
  ( void ) driver_param;

  if( in_use )
    return NULL;

  (void)psp_memset( &t_driver, 0, sizeof( F_DRIVER ) );

  t_driver.readsector = flash_readsector;
  t_driver.writesector = flash_writesector;
  t_driver.getphy = flash_getphy;
  t_driver.release = flash_release;
  t_driver.getstatus = flash_getstatus;
  in_use = 1;

  return &t_driver;
} // flash_initfunc


#define configUSE_PREEMPTION                     1
#define configCPU_CLOCK_HZ                       ( 40000000UL )
#define configTICK_RATE_HZ                       ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES                     ( 8 )
#define configMINIMAL_STACK_SIZE                 ( ( unsigned short ) 512 )
#define configMAX_TASK_NAME_LEN                  ( 12 )
#define configUSE_16_BIT_TICKS                   0
#define configIDLE_SHOULD_YIELD                  1
#define configUSE_TASK_NOTIFICATIONS             1
#define configUSE_MUTEXES                        1
#define configUSE_RECURSIVE_MUTEXES              1
#define configUSE_COUNTING_SEMAPHORES            1
#define configQUEUE_REGISTRY_SIZE                0
#define configUSE_QUEUE_SETS                     0
#define configUSE_TIME_SLICING                   1
#define configUSE_NEWLIB_REENTRANT               0
#define configUSE_APPLICATION_TASK_TAG           0

/* Memory allocation related definitions. */
#define configSUPPORT_STATIC_ALLOCATION          0
#define configSUPPORT_DYNAMIC_ALLOCATION         1
#define configTOTAL_HEAP_SIZE                    ( ( size_t ) 98304 )
#define configAPPLICATION_ALLOCATED_HEAP         0

/* Hook function related definitions. */
#define configUSE_IDLE_HOOK                      0
#define configUSE_TICK_HOOK                      0
#define configCHECK_FOR_STACK_OVERFLOW           0
#define configUSE_MALLOC_FAILED_HOOK             0
#define configUSE_DAEMON_TASK_STARTUP_HOOK       1

/* Run time and task stats gathering related definitions. */
#define configGENERATE_RUN_TIME_STATS            0 
#define configUSE_TRACE_FACILITY                 1

/* Co-routine related definitions. */
#define configUSE_CO_ROUTINES                    0
#define configMAX_CO_ROUTINE_PRIORITIES          ( 2 )

/* Software timer related definitions. */
#define configUSE_TIMERS                         1
#define configTIMER_TASK_PRIORITY                ( 7 ) 
#define configTIMER_QUEUE_LENGTH                 5
#define configTIMER_TASK_STACK_DEPTH             1024

/* Optional functions - most linkers will remove unused functions anyway. */
#define INCLUDE_vTaskPrioritySet                 1
#define INCLUDE_uxTaskPriorityGet                1
#define INCLUDE_vTaskDelete                      1
#define INCLUDE_vTaskSuspend                     1
#define INCLUDE_vTaskDelayUntil                  1
#define INCLUDE_vTaskDelay                       1
#define INCLUDE_xTaskGetSchedulerState           1
#define INCLUDE_xTaskGetCurrentTaskHandle        1
#define INCLUDE_uxTaskGetStackHighWaterMark      1
#define INCLUDE_xTaskGetIdleTaskHandle           0
#define INCLUDE_eTaskGetState                    1
#define INCLUDE_xEventGroupSetBitFromISR         1
#define INCLUDE_xTimerPendFunctionCall           1
#define INCLUDE_xTaskAbortDelay                  1
#define INCLUDE_xTaskGetHandle                   1
#define INCLUDE_xTaskResumeFromISR               1
#define INCLUDE_xQueueGetMutexHolder             1

I adapted the CLI functions from the example (‘FreeRTOS_Plus_FAT_SL_with_CLI’), and also noticed that when I run the ‘dir’ command for the first time, it show the message: “Error: f_findfirst() failed.”. Next time I call the function this error message disappears.

It’s a long time since I’ve looked at the FAT SL code - it’s not been in the distribution for a number of years. I suggest you step through the code in the debugger to see why NULL is returned. The first image in your post looks like it is dereferencing a NULL pointer - the structure being expanded is at an address of zero.

thanks for the suggestion. I will try debugging it this way.

It just struck me that there might be a different problem - the initialization procedures for the flash memory, as well as the filesystem, take place in a task, that is getting deleted.

Could this be a problem?
(initializing the flash memory means setting some registers, so in this case, deleting the stack set up for this task is fine however, I’m not so sure about the filesystem init)

I don’t see an issue with that - provided you delete the task by calling vTaskDelete() rather than just letting it run off the end of its implementing function.