cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 USB OTG Mass storage in FS mode.

kobus
Associate II
Posted on November 19, 2013 at 11:10

I have an STM32F4 design that currently uses a FAT formatted uSD to store firmware binary images and text logs. I managed to get this working a while back by modifying Clive1's uSD examples on the F4 discovery.

My problem is that the customer has now requested that the end user not have access to the SD card, and as such I decided the easiest way to access the files on it would be to bring out the USB of the F4 and connect it to a PC in device mode.

Unfortunately I cant seem to get ANY SMT32 board to connect as a mass storage device using the OTG_FS peripheral. I have tried it om my own board, as well as the STM3220G-EVAL and STM3210C-EVAL; none of them work in FS mode. The STM3220G works using the HS peripheral, but I don't have a PHY on my board so this doesn't help.

I have looked at the F4 discovery example that Clive posted (

/1addcc5f

), and I've imported it into my code and the eval board code but it made no difference. I can see where he edited the SDIO code (with the multi-block_fixed functions), but I can't see any differences to the USB driver....

Whenever I plug in the device, my computer (win 7 64-bit) sees a mass storage device and the VID/PID is correct, but under device manager it says ''This device cannot start. (Code 10)''.

I have entered debug strings in all the USB_usr functions and the SD interface functions the USB uses and I get the following display:

->USB_init

->USB_connected

->USB_device_reset

->USB_device_reset

->SD_init_success

->USB_device_configured

->USB_device_reset

which leads me to believe the device isn't configuring correctly. It never even gets around to reading the card or checking its size.

Has anyone managed to get the MSC device examples of the usb-host-device_lib_v2.1.0 working in FS mode?????  

#usb-fs-msc
8 REPLIES 8
Posted on November 19, 2013 at 13:28

Has anyone managed to get the MSC device examples of the usb-host-device_lib_v2.1.0 working in FS mode?????

I guess the real question is if anyone else has it working? I have my own set of examples working with F2 and F4 designs.

As I recall, I modified the SCSI READ/WRITE/CAPACITY functions to work natively with BLOCK NUMBERS as they are supposed too instead of translating to/from the BYTE OFFSET idiocy that ST keeps using.

The original ST examples should also work with cards up to 2GB. There should be defines to select between FS and HS interfaces, support for several board. One might want to double check pin assignments, and interrupt handlers.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kobus
Associate II
Posted on November 19, 2013 at 14:53

Thanks for the reply Clive.

Okay, for the sake of my sanity I decided to start with a clean slate. I downloaded the STM3220G MSC device example again and ran it on the eval board with a 2GB card (HS mode). At first it didn't work; same code 10 error. then I removed the card, put it in my PC to check and put it back. for some reason it worked after this step...

I changed the compiler directive from USE_USB_OTG_HS to USE_USB_OTG_FS and tried again. The first time I ran it it worked! I reset and power-cycled the board a few times to see if it stayed that way and it did... at least until I removed the debugger. after that nothing. computer just says unknown device; no PID/VID, nothing. I tried putting the card in my PC again to see if that trick would work again, but no. Even after the debugger was put back and the program rerun it did nothing, the LCD just says :''device started, device suspended''.

I recompiled for HS and it worked, recompiled for FS, and it didn't... tried a different computer and it worked, back to mine and it didn't, tried another port and it did...

okay, so the issue is at least partially due to my crappy USB hub...

I put in Clive's code and reran. everything seemed to work. even tried an 8GB now and it also worked.

So everything does seem functional now. I will try port what I have in the demo board code over to mine again. and report back. 

Another thing I would like to know is, are there any practical reasons you wouldn't turn off#define VBUS_SENSING_ENABLED if you have a self powered device only application? I just tried it with the eval board and it seems to work fine. I'm asking because I would have to move a few pins to get the access to the VBUS pin in my application. 

kobus
Associate II
Posted on November 19, 2013 at 16:27

I've worked the EVAL board code into mine now, replacing all USB files with the ones that worked, and even went so far as to remove my own SD card files and use the ones I have in the EVAL code. 

I still get the same result: code 10 error, device cannot start. 

For some reason the debug output looks different now, it doesn't seem to call the usb_reset function anymore, it just goes

->usb init

->usb configured

I'll give a little bit more detail regarding my hardware:

my application runs on an F407. I have an SD card connected, but I don't have the ''card present'' pin anymore as I an using a connector which does not have this switch. in any case I found it wasn't necessary for my needs up til now. The USB DP and DM pins were brought out onto a header, originally in case we needed to use the can bus (they are also can rx and tx pins), but the other USB pins aren't. The SOF and VBUS pins were both used as outputs in my system, used as LCD_reset and LCD_PWM pins respectively. The SOF pin isnt an issue; I simply commented out its initialisation in the USB_BSP file. the LCD_PWM of the VBUS pin is connected to a pull-up resistor so it sort of works as a bus sense pin... if I keep the device connected. The ID pin is unconnected in my system. (could this maybe be the issue? should I pull it to GND/VCC maybe?)

So at this point I have a make shift USB cable connected to the GND, DP and DM pins, and I make sure my device is always plugged in as if it were bus powered. 

It seems a bit weird, but I suspect the issue on my board is related to the SD card, not the USB (even though the SD card is the only well tested piece of this setup). The reason I say so is because the LCD debug output looks identical whether I have a card in or not. 

if anyone has any advice, please post it. I have wasted 2 days on this now...
Posted on November 19, 2013 at 16:52

if anyone has any advice, please post it. I have wasted 2 days on this now...

Perhaps a good time to reflect on known working hardware. My original STM32F4-DISCO implementation used a wire-wrapped MicroSD socket. I also have a

http://www.mouser.com/ProductDetail/STMicroelectronics/STM32F4DIS-BB/?qs=tp9Gh8MiPay9fbHGaE7oZQ==

base board with a MicroSD socket on it now.

On my F2 design I have a MicroSD socket, and my Mini-AB USB socket is connected with

VBUS_FS (1) PA9

USB_FS_ID  (4) PA10

USB_FS_DM (2) PA11

USB_FS_DP (3) PA12

GND (5)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kobus
Associate II
Posted on November 19, 2013 at 17:28

I've Fixed it!

For now everything seems to be working. 

I decided to momentarily remove the code I added to see if I could still get my code to function the way it should. this basically meant re-enabling the uSD init and FatFS functions, and commenting the USB stuff. 

That worked, so I decided to leave FatFS to initialize the SD card and remove card initialization from the USB code completely, i.e. that the only interface the USB has with the card is the read/write functions. I re-enabled the USb and it worked. then I removed the while() trap after the USB initialization and let it run into my application. that didn't work so I created a flag I set in USB_configured and clear in USB_suspended and make sure I Never access the SD card when the flag is set. 

That did it. now I can run my application as usual and when the USB happens to be plugged in, the only things that don't work are my logging and BMP images....

I suppose the solution at the end of the day is make sure nothing accesses the card while the USB does. I'm not sure how this was stopping me before, as I commented out the SD and FatFS init code before, and I had a while loop trapping the application after the USB was initialized. Maybe I have an interrupt somewhere that tries to write to the log file....   

khandaynial_2000
Associate II
Posted on August 03, 2016 at 19:52

hello everyone 

I am having a perfect running code for stm32f407vg but in my application i have to use stm32f405rg. problem is that same code is running on stm32f405rg show error in debugging USB_NOT_READY. please help 

khandaynial_2000
Associate II
Posted on August 03, 2016 at 20:07

One thing that i was thinking that example I used was for stm32f4 discovery. So if there is any example for general stm32f4 series it will be much better. waiting for soon reply

Posted on August 03, 2016 at 20:56

Ok, and how else does your design deviate from the one that worked? Pins, clocks, etc?

I've got 405 designs doing USB, though I haven't checked OTG

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..