Skip to main content
Associate II
July 28, 2025
Solved

VS-Code extension 3.5.1: HardFault during USBX initialization on STM32U5A5

  • July 28, 2025
  • 3 replies
  • 698 views

I'm trying the new VS-Code extension 3.5.1, but I end up in the HardFault_Handler function during the initialization, in the "MX_USBX_Device_Init" function. The same project works correctly when using the "cortex-debug" plugin (separate VS Code profile to avoid conflicts).

It's a very simple project where I did the very minimal config to enable USBX, and "UX Device HS -> Device Class HS -> HID -> MSC" on my STM32U5A5AJH6Q. The IOC file is attached.

It crash in the function "USBD_FrameWork_MSCDesc", at line

 /* Append Endpoint descriptor to Configuration descriptor */
 __USBD_FRAMEWORK_SET_EP((pdev->tclasslist[pdev->classId].Eps[1].add),
 (USBD_EP_TYPE_BULK),
 (uint16_t)(pdev->tclasslist[pdev->classId].Eps[1].size),
 (0U), (0U));

 

Best answer by ElliotAlderson

This means that executing the code causes an unaligned access without the "-mno-unaligned-access" option during compilation.

 

However, it is still possible to ignore it without this option if the UNALIGN_TRP register is not set (this is the behavior of Cortex-Debug and CLion).

 

If the UNALIGN_TRP is set, then execution goes to the error handler.

This is the case with the ST debug configuration, which sets the register by default.

3 replies

Graduate
July 29, 2025

Hi PierreB,

There is a view that shows the type of error occurring:

1000020417.png

And I know that the "unaligned access" error is enabled by default in the ST debug configuration.

You can check the attributes starting with "fault", ex: faultUnalignedAccess

PierreBAuthor
Associate II
July 29, 2025

Yes, it's an Unaligned fault, and there is no error when I add the argument "-mno-unaligned-access", but I'm not sure exactly what that implies, and I'd rather not modify the default cmake configuration, since it works with the cortex-debug plugin and with CLion (with the same elf file).

PierreB_0-1753775065500.png

 

ElliotAldersonBest answer
Graduate
July 29, 2025

This means that executing the code causes an unaligned access without the "-mno-unaligned-access" option during compilation.

 

However, it is still possible to ignore it without this option if the UNALIGN_TRP register is not set (this is the behavior of Cortex-Debug and CLion).

 

If the UNALIGN_TRP is set, then execution goes to the error handler.

This is the case with the ST debug configuration, which sets the register by default.

PierreBAuthor
Associate II
July 29, 2025

Thanks a lot for that explanation!

I was able to find some documentation about the UNALIGN_TRP register and unaligned access, and it confirms exactly what you described. I found several other developers complaining about the same issue with ST's debug configuration.

Do you know if we can change this option in a setting or something without changing the code with something like that:

SCB->CCR &= ~SCB_CCR_UNALIGN_TRP_Msk;

Thanks again for the help!

Graduate
July 29, 2025

On my side, I simply disable it by setting the attribute to false in my debug configuration. 

1000020418.png

 

PierreBAuthor
Associate II
July 29, 2025

It's perfect, I checked on CLion and cortex-debug, the “faultDivByZero” is also disabled on this registry.

Anyway, it's all clear to me now.

 

Thanks again