cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F xxx Strange USB File Write Error - HS Mode Only

louis
Associate II
Posted on February 10, 2014 at 17:31

Hi,

I am getting a strange file write error when using HS USB lib and FATfs. this error does not occur in FS mode.

When I write 2 bytes to a file and then 1024 bytes, some (2)  of the 1024 bytes are repeated at 0x200. See picture below.

If I don't write the 2 bytes first then all appears to be OK.

I am working on a STM32F407 and have a USB3200 PHY. ( However the same problem also occurs on the STM3220G-EVAL. board )

I run the following code 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <stdint.h>

#include ''ff.h''

FIL fileptr;

unsigned char line[1024];

int filetest(void)

{

uint16_t un16BytesWritten;

uint16_t un16Counter;

uint8_t un8ByteValue=0;

uint16_t un16Status;

// File the line with 0x00 - 0xFF

for(un16Counter=0;un16Counter<1024;un16Counter++){

line[un16Counter]=un8ByteValue++;

}

// Open File and Write Data

if(f_open(&fileptr, ''0:BIT_HS2.bmp'',FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)

   {

        un16Status=f_write(&fileptr,&line,2,&un16BytesWritten);

    un16Status=f_write(&fileptr,&line, 1024 , &un16BytesWritten);

   un16Status=f_close(&fileptr);

   return(0);

}

else

return(1);

}

0690X000006052HQAQ.png

In FS mode these bytes are not repeated.

Anybody have any ideas?

thanks for looking,

Louis

5 REPLIES 5
Posted on February 10, 2014 at 18:02

Got some odd stuff going on a the front of the buffer too, and it's all shifted!

Ponder if it's related to memory alignment, or some thing else going on in the abstraction layer below. Really not enough to work with here.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
chen
Associate II
Posted on February 10, 2014 at 18:12

Hi

See what happens if you do a fflush() in between the writes.

louis
Associate II
Posted on February 11, 2014 at 09:51

Thanks for the reply... I tried adding     f_sync(&fileptr); after the 1st write but same issue... very strange but thanks for your help.

Louis

louis
Associate II
Posted on February 11, 2014 at 10:05

Thanks for your reply Clive...

The start of the buffer is correct... If you can see in the code I write 2 bytes and then write 1024 bytes so it should be 0x00 0x01 and then 0x00 -> 0xFF repeats.

I tested again using the standard code ( Downloaded it fresh from ST Website STM32_USB-Host-Device_Lib_V2.1.0

from http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1743/PF257882# )

Simple added this function and called in int USBH_USR_MSC_Application(void) after the STM32.TXT file was created.

Function is 

unsigned char line[1024];

int filetest(void)

{

UINT        un16BytesWritten;

uint16_t un16Counter;

uint8_t un8ByteValue=0;

uint16_t un16Status;

// File the line with 0x00 - 0xFF

for(un16Counter=0;un16Counter<1024;un16Counter++){

line[un16Counter]=un8ByteValue++;

}

// Open File and Write Data

if(f_open(&file, ''0:USB_FS.bmp'',FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)

   {

            un16Status=f_write(&file,&line,2,&un16BytesWritten);

            f_sync(&file);

            un16Status=f_write(&file,&line, 1024 , &un16BytesWritten);

   un16Status=f_close(&file);

   return(0);

}

else

return(1);

}

When I run the USB in High Speed Mode it repeats the 2 bytes at 0x200 but when I run it in Full Speed mode it works as expected - no bytes repeated. 

I have tested it in IAR, KEIL and CooCox and also on the STM32F407 CPU.

I noticed it when writing a HD Bitmap file 1920 x 1080. It was happening almost every line. The problem was much reduced ( approx. 8 errors in the file ) when I commented out 

#define USB_OTG_HS_INTERNAL_DMA_ENABLED

But I am not an expert in the USB lib so that was just hacking :)

I would love to know is anybody out there had a High Speed setup and can write 2 bytes and then 1024 bytes and not see this error. 

Thanks,

Louis

louis
Associate II
Posted on February 11, 2014 at 10:06

Sorry for the repeats... getting lots of reported on browser when posting