cancel
Showing results for 
Search instead for 
Did you mean: 

USB_Device/HID_Standalone Example project crashes on USB disconnect on B-L475E-IOT01A2

exceptionthrower
Associate

Hi, I have a B-L475E-IOT01A2 board and I would like to implement an USB HID application on it. I use the example project found in the Github repository [1]. The application compiles and the USB device is detected on my computer as an input device. The problem is that upon a USB disconnect, the PC seems to jump to 0x0. When plugging the USB cable in again, the application runs again, but it seems I cannot run any code (in, e.g., the main while loop) after a USB disconnect interrupt while the cable is disconnected.

I see this problem both with STM32CubeIDE with ST-Link as well as with SEGGER j-link and Ozone.

In Ozone, I can set a breakpoint in the PCD Interrupt Handler for disconnect events (using USB_OTG_GINTSTS_OTGINT), which will trip upon disconnect. If I step through, I can finish HAL_PCD_IRQHandler to get back to OTG_FS_IRQHandler. As far as I can see, the PC clears to 0x0 after the POP {R7, PC} instruction which returns from the exception handler "OTG_FS_IRQHandler".

It can also be seen visually if I toggle an LED in the main while loop. After a USB disconnect, the blinking stops.

Steps to reproduce:

- Clone [1]

- Open STM32CubeIDE, go to File->Import

- Select 'Existing Projects into Workspace', Click Next and navigate to STM32CubeL4/Projects/B-L475E-IOT01A/Applications/USB_Device/HID_Standalone in the Git repo

- Debug the project, run it and plug in the USB OTG cable

- The device will enumerate as "STMicroelectronics HID Joystick in FS Mode" and your mouse will move

- Now disconnect the USB OTG cable and pause the execution

- Disassembly will fail as the PC is at 0x0

 

Any help regarding this issue would be greatly appreciated.

Thank you.

 

[1] https://github.com/STMicroelectronics/STM32CubeL4

1 ACCEPTED SOLUTION

Accepted Solutions

Hello @exceptionthrower 

The USB device is currently operating in low power mode, which limits its ability to perform debugging. To enable debugging, the low-power mode must be disabled. This can be achieved by adjusting the initialization settings in the `USBD_LL_Init()` function, available in the `usbd_conf.c` file.

Please refer to the code below for the exact changes that need to be made (line 13).

/**
  * @brief  Initializes the Low Level portion of the Device driver.
  * @param  pdev: Device handle
  * @retval USBD Status
  */
USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
{
  /* Set LL Driver parameters */
  hpcd.Instance = USB_OTG_FS;
  hpcd.Init.dev_endpoints = 5;
  hpcd.Init.use_dedicated_ep1 = 0;
  hpcd.Init.dma_enable = 0;
  hpcd.Init.low_power_enable = 0;
  hpcd.Init.lpm_enable = 0;
  hpcd.Init.battery_charging_enable = 0;
  hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
  hpcd.Init.Sof_enable = 0;
  hpcd.Init.speed = PCD_SPEED_FULL;
  hpcd.Init.vbus_sensing_enable = 1;
  /* Link The driver to the stack */
  hpcd.pData = pdev;
  pdev->pData = &hpcd;
  /* Initialize LL Driver */
  HAL_PCD_Init(&hpcd);

  /* configure EPs FIFOs */
  HAL_PCDEx_SetRxFiFo(&hpcd, 0x80);
  HAL_PCDEx_SetTxFiFo(&hpcd, 0, 0x40);
  HAL_PCDEx_SetTxFiFo(&hpcd, 1, 0x80);

  return USBD_OK;
}

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

View solution in original post

5 REPLIES 5
Saket_Om
ST Employee

Hello @exceptionthrower

Thank you for bringing the issue to our attention. I have reported it internally.

Internal ticket number: 178547 (This is an internal tracking number and is not accessible or usable by customers).

 

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar
Saket_Om
ST Employee

Hello @exceptionthrower 

The issue you raised has been addressed and resolved internally. The fix will be integrated in an upcoming release. Thank you again for your contribution.

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar
Saket_Om
ST Employee

Hello @exceptionthrower 

To resolve the issue, you should remove the 100ms delay (line 6 in the code below) in the HAL_PCD_SuspendCallback function located in the usbd_conf.c file.

 

void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
{
  __HAL_PCD_GATE_PHYCLOCK(hpcd);
  USBD_LL_Suspend(hpcd->pData);

   HAL_Delay(100);

    /*Enter in STOP mode */
  if (hpcd->Init.low_power_enable)
  {
    /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register */
    SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
  }
}

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

Hello @Saket_Om

Thank you for your reply.

 

I replicated the step above and removed the delay in the callback function (in a freshly cloned and compiled repository).

Unfortunately, the problem persists.

 

Thanks,

exceptionthrower

 

Hello @exceptionthrower 

The USB device is currently operating in low power mode, which limits its ability to perform debugging. To enable debugging, the low-power mode must be disabled. This can be achieved by adjusting the initialization settings in the `USBD_LL_Init()` function, available in the `usbd_conf.c` file.

Please refer to the code below for the exact changes that need to be made (line 13).

/**
  * @brief  Initializes the Low Level portion of the Device driver.
  * @param  pdev: Device handle
  * @retval USBD Status
  */
USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
{
  /* Set LL Driver parameters */
  hpcd.Instance = USB_OTG_FS;
  hpcd.Init.dev_endpoints = 5;
  hpcd.Init.use_dedicated_ep1 = 0;
  hpcd.Init.dma_enable = 0;
  hpcd.Init.low_power_enable = 0;
  hpcd.Init.lpm_enable = 0;
  hpcd.Init.battery_charging_enable = 0;
  hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
  hpcd.Init.Sof_enable = 0;
  hpcd.Init.speed = PCD_SPEED_FULL;
  hpcd.Init.vbus_sensing_enable = 1;
  /* Link The driver to the stack */
  hpcd.pData = pdev;
  pdev->pData = &hpcd;
  /* Initialize LL Driver */
  HAL_PCD_Init(&hpcd);

  /* configure EPs FIFOs */
  HAL_PCDEx_SetRxFiFo(&hpcd, 0x80);
  HAL_PCDEx_SetTxFiFo(&hpcd, 0, 0x40);
  HAL_PCDEx_SetTxFiFo(&hpcd, 1, 0x80);

  return USBD_OK;
}

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar