Need some help with STM32 USB Mass Storage - Page 1
Forum section: electronics / general

author: rheb1026
author_rank: Regular Contributor
timestamp: September 06, 2017, 02:46:36 am
content: I'm trying to get the STM32F469 Discovery board to act as a mass storage device with the on board microSD card. I currently have the SD card working fine, standard peripheral USB library set up and compiling fine, but I seem to be missing something just after the USB Init function is called. When the board is plugged in the USBD_Init function is called, this seems to happen successfully because the USBD User Init callback function gets called (I'm just turning on a LED to verify). Then the program sits in a forever loop waiting for the USB cable to be plugged in. When I plug in the USB cable none of the other USB interrupts or USB User callback functions are ever called and the program just sits in the loop.I'm almost 99% certain I have everything initialized correctly since I took most of the code from one of ST's example projects, but I must be missing something. I'm very inexperienced with USB and really don't know where to begin looking.I know that many of the STM32 problems stem from incorrect clock configuration, but I've checked and rechecked that many times.I'm hoping that someone more experienced here can offer me a few places to look for problems. I'll try to get all the code uploaded to someplace like github tomorrow so it can all be viewed easily.I should add that the microcontroller never enumerates with the computer (or even attempts as far as I can tell)

author: stmdude
author_rank: Frequent Contributor
timestamp: September 06, 2017, 03:55:17 am
content: I just went through the same fun experience. STs USB stack isn't perfect, that's for sure...Which stack are you using (I'd assume you're talking about STM32_USB_OTG_Driver + STM32_USB_Device_Library) ?If so, make sure you've:1. Turned on the RCCs you need (RCC_AHB1Periph_GPIOB, RCC_APB1Periph_PWR, RCC_AHB1Periph_OTG_HS)2. Configured pins 14 and 15 on GPIOB to AF GPIO_AF_OTG2_FS3. Enabled the OTG_HS_IRQn IRQ in the NVIC4. Have a custom IRQ handler for OTG_FS_IRQHandler which calls USBD_OTG_ISR_HandlerThat _should_ get you to a point where it's actually trying to enumerate.It's not related to enumeration, but I had to disable USB_OTG_HS_DEDICATED_EP1_ENABLED as well as DMA transfers to get it going fully.Also, keep in mind. The Micro-USB connector on the STM32F429I-DISCO (which I'm assuming you're using) is connected to the USB_HS block, using the internal transceiver.

author: rheb1026
author_rank: Regular Contributor
timestamp: September 06, 2017, 02:30:08 pm
content: Yes, the stack that I'm using is the USB OTG Driver and the Device Library.Thank you for the suggestions. I fear that your last point is exactly my issue! I'm using the USB library in FS mode on PA11 and 12...I'll try switching to the other pins and see how that goes

author: rheb1026
author_rank: Regular Contributor
timestamp: September 07, 2017, 04:06:11 am
content: Small success, but still not there 100%. I decided to just use a STM32F429 Nucleo board with the USB OTG FS connector with the original code. When I plugged in the USB cable to the PC and nucleo board I got a chime sound out of the PC. The USBD User Device Connected callback function was also called at this point (turned on another LED).Unfortunately if I disconnect the nucleo board the USBD User Device Disconnected callback function is never called (should turn on a third LED).And if I go into device manager in Windows (Win 10) a USB Mass Storage device shows up for about five to ten seconds before it disconnects and disappears. If I plug the board into a Win 7 machine it will stay there in the device manager and not disconnect, but on both computers the nucleo board never shows up as a USB drive under 'my computer'.

author: stmdude
author_rank: Frequent Contributor
timestamp: September 07, 2017, 05:08:33 am
content: Does your device show up, or just an unknown device?If your device actually enumerates properly, but disconnects after just a few seconds, my guess would be that you're either blocking in the DataIn/DataOut callback, or that you're running from the HSI (which isn't stable enough for USB).If it shows up as an unknown device, make sure the IRQs are working (NVIC configured, ISRs in place).I do most of my development on Linux, but if I remember correctly, it takes very little for Windows to go "bing" when you connect an USB device. It might be as little as just having the D+ pullup..

author: rheb1026
author_rank: Regular Contributor
timestamp: September 07, 2017, 03:59:47 pm
content: I made sure to use an external crystal so that shouldn't be a problemThe device shows up with the correct name. I think there might be an issue with my Mass Storage Read/Write Functions because if I delete all the code from the functions (besides a return 0) the board will successfully call the user connect and disconnect functions when I plug and unplug the USB cable and it doesn't disconnect after a short period of time.It also now gives me an option to "browse files" from the Windows Devices and Printers.

author: stmdude
author_rank: Frequent Contributor
timestamp: September 07, 2017, 04:20:21 pm
content: Alright, cool!That seems to confirm that all the lower-layer stuff is working. You're definitely on the right track when you're looking at your implementation of the mass-storage class. That's where I would have started as well.

author: rheb1026
author_rank: Regular Contributor
timestamp: September 07, 2017, 04:43:08 pm
content: Thanks for taking the time to help! I appreciate it!