AnsweredAssumed Answered

USB Host with MFS problems

Question asked by Adrian Rockall on May 1, 2014
Latest reply on Oct 19, 2015 by Adrian Rockall

I am using MQX version 4.1 and a modified sample web-hvac project with the IAR workbench 7.1. I am running the project on the TWR-K60F120M and the TWR-SER boards.
I have only been using this system for a few weeks and it is my first time with MQX and USB stacks, etc.

The sample functions are all working fine but I have added a shell command to do a file read speed test. This simply records the time to read the contents of a file from a mounted device into a memory buffer in a loop.


uint32_t bufSize = 10 * 512;
buffer = (char*)_mem_alloc_system_zero( bufSize );
if (NULL == buffer)
  printf( "Failed to allocate read buffer" );
  fclose( fd );
  return_code = SHELL_EXIT_ERROR;

printf( "Reading from %s:\n", argv[ 1 ] );
_time_get_elapsed( &startTime );
while ((count = read( fd, buffer, bufSize )) > 0)
  bytes += count;
_time_get_elapsed( &endTime );
int32_t err = ferror( fd );
fclose( fd );
_time_diff( &startTime, &endTime, &diffTime );
if (err != FS_EOF)
  printf( "\nRead Error Code %i\n", err );
float time1 = (float)((diffTime.SECONDS * 1000) + diffTime.MILLISECONDS);
float kbytes_sec = (float)bytes / time1;

printf( "\nRead %i bytes in %i.%i S - %.3f KB/S\n", bytes, diffTime.SECONDS, diffTime.MILLISECONDS, kbytes_sec );
_mem_free( buffer );


Files on an SD card are working fine at around 3.5MB/s but I am having problems with files on a USB stick. If I connect the stick directly to the TWR-SER USB port I can read a 97660 bytes file with no problems but a 1234920448

bytes file fails. If I connect the stick via a hub then even the smaller file sometimes fails.
The failure modes are below:

1. When connect directly. Stops reading part way through the file (read function returns 0 bytes read). No other errors but ferror returns 0. Once the error occurs all further speed tests return 0 bytes immediately but I can still

perform the dir function correctly. Unplugging and plugging the stick back in resolves the problem until a new speed test fails.

2. When connected via a hub. The detach event occurs which tries to close the file system but that fails as a file is still open. After that a reboot is required as unplugging and plugging in the stick fails to mount the file

system that was not closed properly. The MQX plugin, Check for Errors command, shows an Error Code: Invalid Pointer (0x0001), State Active.


I have used the IAR - MQX plugin to check the stacks and the highest usage is only 56 %.

My questions are:

1. Has anyone got any ideas why the read is failing or where to start looking?


2. Can anyone suggest why it is less reliable through the hub and where to start looking for what is going wrong?


3. Any recommendations on a way to close the file system on the unexpected detach event so that the system can recover when the attach event occurs? I have found that ignoring the "if ( !drive_ptr->MFS_FILE_PTR )" in MFS_Uninstall

to force the destroy does at least remove the requirement to reboot the board but the automatic attach that follows the unexpected detach shows "NOT A DOS DISK! You must format to continue.". Unplugging and plugging back in

resolves that.


Unfortunately I don't have a USB analyser and I can't make any sense of the Jet-Trace trace buffer so I'm hoping there is someone that might have already solved a similar problem or some experts on the USB stack that might be able to give me some suggestions.


Best regards,