cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 DISCO USB DEVICE DOES NOT WORK

Leo_Panda
Associate III
Posted on April 11, 2016 at 11:19

I used CUBEMX (Version 4.14.0) to generate a small usb test program for stm32l4 discovery board. The L4 Lib is Version 1.4.0.

When i only initialize USB as device (class CDC) it doesnt' work. All registers seem to be ok, but it doesn't enumerate, the DP pin remains low after USB_Start.

If i also initialize teh UART1, USB works. I even don't need to initialze UART1 completely, but have to enable the UART1 Clock, initialze the GPIO-Pins, and set UART1.CR1 TE and RE to 1. It is strange because the USB core shuldn't have anything to do with UART1. I used a 32khz on LSE and PLL. The CubeMX generator doesn't show any error or warning. The same behavoir shows also the Arduino board.

Can one explains why? or If someone have a working ioc file which only initialze the USB for STM32L4 Discovery board or Arduino board?

#l476gdisco #usb #usb #stm32l4 #cubemx #device
7 REPLIES 7
Nesrine M_O
Lead II
Posted on April 11, 2016 at 14:23

Hi zhang.gang.001,

Thank you for your feedback. The issue has been reported internally.

-Syrine-

Leo_Panda
Associate III
Posted on April 11, 2016 at 15:09

Dear Syrine,

i made some more experiments on this behavior: if i only use usb (the uart is initialized) it worked fine, send/receive both directions are tested with a terminal program, no problem. but if i tried to send something via uart1, i got a suspend interrupt from the usb controller

(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP), it then closed the connection and tried to reopen it (i thing after the uart1 trasmitt completion), but it failed. the transmitt via uart1 was ok.

gbm
Lead III
Posted on April 11, 2016 at 20:30

While configuring your project under CubeMX, make sure to un-tick VBUS sensing in peripheral module configuration and to DISABLE it in USB device firmware configuration. By default even if you don't tick VBUS input in peripheral config, the firmware config (USB_OTG_FS Configuration dialog) has VBUS sensing enabled (looks like a simple mistake in Cube L4 package).

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
tsuneo
Senior
Posted on April 11, 2016 at 21:27

Hi zhang and Syrine,

Current version of CubeMX generates very dangerous code for USB devices on 32L476G DISCO board. You may burn out the OTG_FS_VBUS (PC11) pin, by output conflict, if you would run generated code without any fix.

STM32CubeMX: v4.0

STM32Cube_FW_L4_V1.4.0

Target: 32L476G DISCO board - all of USB device class

Problem around VBUS pin

1) MX_GPIO_Init() in main.c sets up OTG_FS_VBUS (PC11) pin as output, low-level.

2) STMPS2141 (VBUS switch chip for host) sources 5V to this pin. MX_GPIO_Init() enables (active-low) this chip.

3) Also, when you plug-in your board to PC USB port, 5V comes from host.

And then, PC11 drops these 5V sources into GND.

Here is fix for this problem.

main.c

void MX_GPIO_Init(void)

{

// line 399

/*Configure GPIO pins : OTG_FS_PowerSwitchOn_Pin OTG_FS_VBUS_Pin */

//GPIO_InitStruct.Pin = OTG_FS_PowerSwitchOn_Pin|OTG_FS_VBUS_Pin; // <--- (**)

GPIO_InitStruct.Pin = OTG_FS_PowerSwitchOn_Pin;

// line 415

/*Configure GPIO pin Output Level */

//HAL_GPIO_WritePin(GPIOC, OTG_FS_PowerSwitchOn_Pin|OTG_FS_VBUS_Pin, GPIO_PIN_RESET); // <--- (**)

HAL_GPIO_WritePin(GPIOC, OTG_FS_PowerSwitchOn_Pin, GPIO_PIN_SET);

Replace above (**) lines with next lines

This fix does,

- OTG_FS_VBUS is left in default (floating)

- Diable STMPS2141 chip

> If i also initialize teh UART1, USB works

It's because, UART1 TX shares VBUS (PA9) pin with OTG_FS.

Modify this line to USBD_LL_Init() in usbd_conf.c, so that OTG_FS ignores VBUS level.

usbd_conf.c

USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev)

{

// line 321

// hpcd_USB_OTG_FS.Init.vbus_sensing_enable = ENABLE;

hpcd_USB_OTG_FS.Init.vbus_sensing_enable = DISABLE;

> I used a 32khz on LSE and PLL.

Unfortunately, current CubeMX doesn't enable LSE-PLL for MSI

You have to enable it manually, inserting this code to SystemClock_Config() on main.c

main.c

void SystemClock_Config(void)

{

...

/* Enable MSI Auto-calibration through LSE */

HAL_RCCEx_EnableMSIPLLMode();

CubeMX setting for CDC and fixed sources (main.c and usbd_conf.c) are attached to this post.

Tsuneo

________________

Attachments :

cube_l476_disco_cdc_dev.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0d1&d=%2Fa%2F0X0000000bcJ%2FylCEHotWLxrnk1jbxqBIlHf6aMA80rJCnNYdJd4rArg&asPdf=false
Leo_Panda
Associate III
Posted on April 12, 2016 at 09:48

Hi chinzei.tsuneo,

You are right, for DISCO board one has to turn OTG_FS_PowerSwitchOn(PC9) as output hight AND OTG_FS_VBUS(PC11) as input with pulldown (or high z if unused).

recently i used the Arduino board for testing and there is no such problem.

As you mentioned the line

hpcd_USB_OTG_FS.Init.vbus_sensing_enable = ENABLE;

is wrong.

I didn't get Suspend interrupt any more after i disabled vbus sensing in code.

Now I'll try to remove uart1 initialization and see if usb device works along

thanks.

Posted on April 12, 2018 at 17:31

Hi

Zhang.Gang

,

CubeMx tool with latest version, i.e. 4.25 at current day, correctly disables vbus sensing according to project configuration.

Thanks in advance for helping to complete this thread.

Regards.

Cyril

Posted on June 04, 2018 at 11:42

Hi,

To get more into detail, I just want to remind from the UM1879, 'Discovery kit with STM32L476VG MCU', the following:

7.8 USB OTG FS

The STM32L476 Discovery board supports USB OTG Full Speed communication via a USB Micro-AB connector (CN7) and a USB power switch (U14) connected to VBUS. The board can be powered by this USB connection as described in Section 7.2.

A green LED LD6 will be lit in one of these cases:

• The power switch (U14) is ON and STM32L476 Discovery board works as a USB host

• VBUS is powered by another USB host when STM32L476 Discovery board works as a USB device

Red LED LD7 will be lit when an overcurrent occurs.

In order to connect the OTG_FS_VBUS and OTG_FS_ID signals from the connector CN7 to the OTG FS hardware IP of STM32L476VGT6, remove the LCD from its socket U5, and close SB24 and SB25.

The default configuration is: the LCD is connected to U5, and SB24 and SB25 are opened.

In this case the OTG_FS_VBUS and OTG_FS_ID signals from CN7 are connected to the OTG FS peripheral of the STM32L476VGT6 available on PC11 and PC12.

Due to these, PC11 is used for OTG_FS_VUB instead of PA9.

As consequence, one must disable VBUS sensing in USB IP but PC11 should be configured as 'Input mode' 'No pull-up and no-pull down'.

Hoping its clarifies.

Regards.

Cyril