2015-01-15 12:02 PM
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 #stm32cube2015-02-03 05:20 AM
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.2015-08-12 12:31 PM
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)
2016-01-04 07:06 AM
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)2016-06-30 02:01 AM
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,Torsten2016-06-30 07:45 AM
Hirobitzki.torsten,
Try work on VBus to detect connection/disconnection. I recommend to take a look to other forum threads like : , , , , -Hannibal-2016-07-01 03:05 AM
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. 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