cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Cube USB Device library

schauhan112
Associate II
Posted on January 15, 2015 at 21:02

Hi,

I have created a HID class device using STM32F4 Cube USB device library.  Everything is working fine and I am able to exchange data with PC.  However, I would like to turn on/off an indicator LED when device is attached/detached.  I am using the following code:

/**

  * @brief  Connect callback.

  * @param  hpcd: PCD handle

  * @retval None

  */

void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)

{

  USBD_LL_DevConnected(hpcd->pData);

  HAL_GPIO_WritePin(USB_LED_PORT, USB_LED_PIN, LED_ON);

}

/**

  * @brief  Disconnect callback.

  * @param  hpcd: PCD handle

  * @retval None

  */

void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)

{

  USBD_LL_DevDisconnected(hpcd->pData);

  HAL_GPIO_WritePin(USB_LED_PORT, USB_LED_PIN, LED_OFF); //This is not working

}

LED turn on fine when device is attached but HAL_PCD_ConnectCallback is not fired when device is disconnected from USB port.  

Any suggestion about how to accomplish this would be highly appreciated.

#usb #stm32cube
6 REPLIES 6
Posted on February 03, 2015 at 14:20

Hi chauhan.samir,

Could you provide a little more information about the STM32 HW based, the STM32CubeF4 FW package and the IDE toolchain you are using, so we can better explain it to our development team...

PS: Please Format Code Block - Paintbrush [<>] icon, upper left of Word-in-a-box(tm)

  interface..

Regards,

Heisenberg.

andrewandrew91
Associate II
Posted on August 12, 2015 at 21:31

Hi Heisenberg,

I realize this is an older thread but I have encountered the same issue with the latest version of STM32CubeMX where the USB connect and disconnect callback functions are never called when the interrupt is generated.  As such, the Keil V5.15 compiler optimizes the code out and will not allow a debug break point to be set on any of the 4 unused functions below.  Digging through the project code confirmed my suspicions and I would love a supported fix from ST before I end up implementing my own in-house solution that would break with the next Cube update.

void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)

void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)

USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef  *pdev)

USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef  *pdev)

The problem can be reproduced with:

STM32CubeMX version V1.0 (Version 4.9.0)

New Project

Target / Board Filter = STM32F072B-DISCO

Enable USB / Device (FS)

Configuration / USB_DEVICE / Class For FS IP / Human Interface Device Class (HID)

Or

Configuration / USB_DEVICE / Class For FS IP / Custom Human Interface Device Class (HID)

f2399
Associate
Posted on January 04, 2016 at 16:06

Same story here, (first fixed the heap size to get it even working)

managed to get it working by using HAL_PCD_ResetCallback() for usb connected and HAL_PCD_SuspendCallback for disconnected.

but reset can be called a few times

(using hs usb with external phy on a stm32f417)

torsten2
Associate II
Posted on June 30, 2016 at 11:01

Hello Heisenberg,

did your software team found the time to look into this meanwhile? I have a similar problem, I need to find out, when a mass storage device is disconnected from a host. Setting a break point in HAL_PCD_DisconnectCallback(), but that is never reached.

kind regards,

Torsten
Walid FTITI_O
Senior II
Posted on June 30, 2016 at 16:45

Hirobitzki.torsten,

Try work on VBus to detect connection/disconnection.

I recommend to take a look to other forum threads like :

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https%3a//my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Virtual%20Com%20Port%20-%20Detect%20USB%20Disconnect&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&...

,

https://my.st.com/public/STe2ecommunities/mcu/Lists/SPC56/Flat.aspx?RootFolder=https%3a//my.st.com/public/STe2ecommunities/mcu/Lists/SPC56/COM%20port%20disconnect%20from%20USB%20way%20to%20know%20it%20STM32F205&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000C7B65FFB6DB42540A4DAECE852348058&c...

,

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=https%3a//my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/ST32F072%20USB%20Simulation%20of%20Cable%20disconnect&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21&currentview...

,

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https%3a//my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Software%20USB%20plug%20and%20unplug%20detection&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&curr...

,

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/USB%20CDC%20problem&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21&currentviews=118

-Hannibal-

tsuneo
Senior
Posted on July 01, 2016 at 12:05

Detecting disconnection (unplug) on USB device

hi Torsten, > Setting a break point in HAL_PCD_DisconnectCallback(), but that is never reached. In the current Cube USB device stack (**1) for OTG_FS and OTG_HS on FS PHY, HAL_PCD_DisconnectCallback() is called in the HAL_PCD_IRQHandler() (stm32f4xx_hal_pcd.c), by GOTGINT.SEDET (session end detected) interrupt Required settings to fire this interrupt are, 1) VBUS (PA9) pin should be connected to VBUS (5V) pin of the USB connector (for OTG_HS on on-chip FS PHY, VBUS (PB13) pin) 2) vbus_sensing_enable is 'ENABLE'd in USBD_LL_Init()

usbd_conf.c
USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev)
{ 
/* Init USB_IP */
if (pdev->id == DEVICE_FS) {
...
hpcd_USB_OTG_FS.Init.vbus_sensing_enable = ENABLE; // <----

(**1) CubeMX: v4.1 (latest) CubeF7: v1.4.0 CubeF4: v1.0 CubeL4: v1.5.1 A couple of stack / hardware bugs disturb this operation. A) CubeMX bug: On Discovery board, with STMPS2141STR as USB host power switch, CubeMX generates bad initialization code, which supplies 5V rail to the VBUS pin. As the result, device can't detect plug off. Refer to

/public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Java/CubeMX%20bug%20USB%20devices%20wrongly%20enable%20host%20power%20switch%20on%20Discovery%20boards

. B) Wrong choice of protection diodes on USB lines If your board would mount protection diodes for D+/D- line with high-side diode, like USBLC6-2, the diode keeps VBUS voltage, passed by the D+ pull-up resister, after plug off. And then, VBUS drop isn't detected. 0690X00000605SQQAY.png 0690X00000605dIQAQ.png If such a TVS diode array like USBLC6-2 would mounted on your board, lift off the VBUS pin of the diode. For new design, select diode array without high-side diode. ST's Discovery boards mount EMIF02-USB03F2, or USBULC6-2F / SDA14V2-2BF3 Also, these parts are good for this purpose. - ST ESDAULC6-3BP6 - ON Semi NUP4060AXV6 (for full-/low-speed) - TI TPD4S012 Tsuneo