This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

USB Mass Storage + FATFS issue

Other Parts Discussed in Thread: TM4C129XNCZAD

Platform:

TM4C129XNCZAD microcontroller based custom board (based on DK reference design)

TI-ARM compiler  5.2.5

TI-RTOS  2.1.4

FATFs enabled 

Hello,

I have modified the TI-RTOS Tiva USB driver to support USB hub and is working. Is able to detect the Mass storage device as well the USB Hub.

I have multi task application, and below are two tasks in the application, where i am facing issues

1. USB task --> enumerates the USB Mass Storage  device and terminates

2. log task --> log data into USB drive 

The USB drive is enumerated  correctly and I am able to log data into USB drive for the 1st time.  Subsequent  attempts at writing to USB drive fails. The "fopen" returns an  NULL  even though the "fopen" is called with append plus flage ( "a+").  

I did a debug by single stepping into fopen and it gets DEV_BUSY and return NULL

I have attached the code for both tasks as well as the data logging function.

 Can anyone please point me where am going wrong and why is the fopen call returning DEV_BUSY, even though I have flushed and closed the file?

Thanks in advance

Narendra

#define  LOG_DATA_LEN      648

/* String conversion macro */
#define STR_(n)             #n
#define STR(n)              STR_(n)

#define USB_DRIVE_NUM       1
char outputfile[] = "fat:"STR(USB_DRIVE_NUM)":output.txt";

void initTask(UArg arg0, UArg arg1)
{
    /* Other variables */
    USBMSCHFatFs_Handle usbmschfatfsHandle;
    USBMSCHFatFs_Params usbmschfatfsParams;
    ....
    /* Other variables */
    
    
    
    ...... 
    /* Other Init code */
    ......
   	/* Mount and register the USB Drive */
   	USBMSCHFatFs_Params_init(&usbmschfatfsParams);
   	usbmschfatfsParams.serviceTaskStackPtr = usbServiceTaskStack;
   	usbmschfatfsParams.serviceTaskStackSize = sizeof(usbServiceTaskStack);
   	usbmschfatfsHandle = USBMSCHFatFs_open(UI_BOARD_USBMSCHFatFs0,
   												USB_DRIVE_NUM,
												&usbmschfatfsParams);
   	if (usbmschfatfsHandle == NULL) {
   		System_abort("Error starting the USB Drive\n");
   	}
   	else {
   		/* Need to block until a USB Drive has been enumerated */
        if (USBMSCHFatFs_waitForConnect(usbmschfatfsHandle, 10000))
        {
            driveMounted = true;  /* Global variable if USB drive is enumerated */ 
        }
        else
        {
            USBMSCHFatFs_close(usbmschfatfsHandle);
        }
    }

}



/* Task for logging data */
void LoggingTask(UArg arg0, UArg arg1)
{
    while(1)
    {
    	uint32_t events;

    	events = Event_pend(ButtonEvent, Event_Id_NONE, (Event_Id_00 + Event_Id_01 + Event_Id_02 + Event_Id_03 + \
    			                                         Event_Id_04 + Event_Id_05 + Event_Id_06),BIOS_WAIT_FOREVER);

        if(events & Event_Id_00)
        {
            retValue = LogDataToUSB();
        }
        ....
        ....  /* Other events handled */
       

    }
}

int8_t LogDataToUSB(void)
{
    FILE *dst;
    /* Variables to keep track of the file copy progress */
    uint32_t bytesWritten = 0;
    unsigned int len = LOG_DATA_LEN;


    if(driveMounted == true)
    {
    	/* Create a new file object for the file copy */
    	dst = fopen(outputfile,"a+");
    	if (dst)
    	{
    		 /* Global array filled with logged data */
    		bytesWritten = fwrite(logData, 1, len, dst);
    		if (bytesWritten == len)
    		{
                /* flush the contents of the stream */
                len = fflush(dst);
                if (len == 0)
                {
                    /* Close file */
                    len = fclose(dst);
                    if (len == 0)
                    {
                        return 0;
                    }           
                }       
            }
        }
    }
    return -1;
}
 

  • Narendra,

    The code looks correct when I compared with the TI-RTOS example. Have you tried the default example from TI-RTOS?

    I was looking through the documentation and I found this statement:

    Once the driver has been opened, the application may used the FatFs APIs or the standard C runtime file I/O calls (fopen, fclose, etc...). Once the driver has been closed, ensure the application does NOT make any file I/O calls.

    Looking through your code snippet, I couldn't find if you are closing the USB driver after your logging. Is there a possibility that some other Task may be closing the USB driver?

    Vikram

  • Hello Vikram,

    Sorry for asking the question with out properly looking into the issue. We were resetting the USB Hub (this was done to check an issue with a SPI device ). This was causing the the issue I had mentioned. Once the USB hub reset was removed, it worked like a charm

    Thanks

    Narendra