Skip to main content
SORei.1
Associate II
June 18, 2026
Question

Possible error in USBD_conf.c produced by cubemx (with solution provided)

  • June 18, 2026
  • 5 replies
  • 53 views

I have been chasing a very intermitant issue with some STM32L100 based products.

The problem shows itself with the MCU being reset by the WDT part way through the initialisation sequence.

This issue only displays itself when the MCU has reset itself using  NVIC_SystemReset();

E.G. the program flow is as follows

Running program

NVIC_SystemReset(); is called

MCU resets and goes through the init section in main.c

part way through the init section, it locks up and the wdt resets the mcu.

 

Once it has started to do this, it can stay in this reboot loop until you re-power the MCU OR it can recover after only one reboot. There is no pattern to how it recovers without a power cycle

This does not happen when you power up the mcu, it only happens when the mcu has been reset using NVIC_SystemReset();

Plugging in a USB cable into the board and a pc stops the reboot cycle. (no app on the pc needs to be using the usb com port)

 

We have several thousand products in the field and we come across this issue once every three to four months, so it has been very difficult to reproduce and fix.

 

The problem was found to be a race condition between the USBD_Start() function being called and the usb interrupt being raised. If the usb interrupt was raised before USBD_Start() had completed, you would enter a continual interrupt loop and stay there until the mcu was reset.

The solution involved the following

1: Remove the line of code in HAL_PCD_MspInit() that enabled the USB interrupt

2: In the function MX_USB_DEVICE_Init(), after the USBD_Start() function has been called, clear any pending interrupts and then enable the interrupts using the following lines.

  NVIC_ClearPendingIRQ(USB_LP_IRQn);
  NVIC_EnableIRQ(USB_LP_IRQn);

 

We have the same code running on STM32G0 mcu’s and we have not seen this issue with them (yet).

As this code is auto generate in this way by CubeMx,  can this change be introduced into its code base for fuiture releases.

Thanks

 

 

 

 

5 replies

ST Technical Moderator
June 26, 2026

Hi ​@SORei.1 

Apologies for the late reply, would you attach your ioc file?

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL
SORei.1
SORei.1Author
Associate II
June 29, 2026

Here it is

Thanks

ST Technical Moderator
June 29, 2026

Hi ​@SORei.1 

It seems you have generated your project using CubeMX V6.2. Please use v6.5 or earlier to migrate the project, as it cannot be opened. 

Also, it would be better to update your STM32CubeL1 to latest version.

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL
SORei.1
SORei.1Author
Associate II
June 30, 2026

Hello,

 I have opened it with V6.5 an I am unable to migrate it to anything higher due to the message below. I have updated the drivers to the latest and the issue is still there.

The issue we are experiencing is seen on the STM32L100 BUT the reason it happens is in the driver code for  both the chip models that we use. E.g. STM32G0, STM32L100 and is present in the code generated by the later versions of CubeMx.

E.g. Create a project in CubeMX latest version for the STM32G0 and enable USB for CDC, and then look at the code that i mentioned in my first message. If you do a comparison between the functions below and the functions generated, you can see the changes that I have done.

 

void MX_USB_DEVICE_Init(void)

{

  /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */

  /* USER CODE END USB_DEVICE_Init_PreTreatment */

  /* Init Device Library, add supported class and start the library. */

  if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK)

  {

    Error_Handler();

  }

  if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC) != USBD_OK)

  {

    Error_Handler();

  }

  if (USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS) != USBD_OK)

  {

    Error_Handler();

  }

  if (USBD_Start(&hUsbDeviceFS) != USBD_OK)

  {

    Error_Handler();

  }

//the next two lines were added

  NVIC_ClearPendingIRQ(USB_LP_IRQn);

  NVIC_EnableIRQ(USB_LP_IRQn);

  /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */

  /* USER CODE END USB_DEVICE_Init_PostTreatment */

}


 

void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)

{

  if(pcdHandle->Instance==USB)

  {

  /* USER CODE BEGIN USB_MspInit 0 */

  /* USER CODE END USB_MspInit 0 */

    /* Peripheral clock enable */

    __HAL_RCC_USB_CLK_ENABLE();

    /* Peripheral interrupt init */

    HAL_NVIC_SetPriority(USB_LP_IRQn, 0, 0);

//    HAL_NVIC_EnableIRQ(USB_LP_IRQn);

  /* USER CODE BEGIN USB_MspInit 1 */

  /* USER CODE END USB_MspInit 1 */

  }

}

 

 

 

 

ST Technical Moderator
June 30, 2026

Hi ​@SORei.1 

HAL_NVIC_EnableIRQ should not be called after starting USBD_Start(). You can refer to the example provided. It should be called after clocking USB controller.
About MX generated code, did you enable USB interrupt here? On my end, it gets generated as expected.

 

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL