cancel
Showing results for 
Search instead for 
Did you mean: 

STM32U5 - USBX memory defaults woefully small

dmarks-ls
Associate II

TL;DR - adding USBX and the USB CDC device class to my STM32U5 TouchGFX project causes it to fail during initialization, because the default memory settings are woefully inadequate.

I have a TouchGFX project running on an STM32U5G9J-DK2 using ThreadX.  The TouchGFX component tosses up a splashscreen, and I have other tasks running, including a debug task putting out UART data.  So that all works fine.

What doesn't work is when I add USBX and a simple USB CDC device class to the project.  I can add it using CubeMX, but when I build and run it, it dies during initialization because of memory allocation issues.  I would at least expect it to run out-of-the-box when adding it to a new or existing project.

Here are my steps to reproduce the failure:

  1. Make a workspace directory (I called mine C:\ST\workspace\touchgfx_usb_issue).
  2. Open TouchGFX, create a new project, select STM32U5G9J Discovery Kit 2 running ThreadX, select the workspace directory as the "Application Directory", and set "Application Name" to "usb_issue", and click Create.
  3. Drop a green box onto the canvas so there's something there, then click Program and Run Target.  Confirm that the green box/whatever appears on the screen.
  4. Open STM32CubeIDE, and switch to the workspace directory.
  5. Select Import / Existing projects into workspace, select the "usb_issue" directory in the workspace directory.  Confirm that "STM32U5G9J-DK2-ThreadX" appears as the project name.  Finish importing.
  6. Click the hammer to build the project, then click the green bug to debug and run the project.  Confirm that the green box/whatever appears on the screen.
  7. Open the IOC (CubeMX) file.  Under Pinout and Configuration / Connectivity, select USB_OTG_HS, then under Mode / Internal HS Phy, select "Device Only".  Confirm that PA11 and PA12 are now selected for USB.
  8. Select the Clock Configuration tab, and decline to run the clock issue solver.  Scroll to the bottom, and for "OTG HS Clock Mux", select "HSE".  Confirm that "To OTG Mux (MHz)" shows 16 MHz.
  9. Go back to Pinout and Configuration, and under Middleware, select USBX.  Check "Core System", then under "UX Device HS", select "Device CoreStack HS".  Then under "Device Class HS", select "CDC_ACM".  Leave all USBX (UX_...) settings at their defaults.
  10. Save the IOC.  This should trigger generation of source code.
  11. Click the hammer to build the project, then click the green bug to debug and run the project.  Click Resume (green right arrow), and observe that the green box has not appeared on the screen.
  12. Click Suspend (pause symbol), and observe that the program is stopped in the section "MX_USBX_Device_Init_Error".

USBX init failsUSBX init fails

Tracing into the application, the call to ux_device_stack_initialize() (app_usbx_device.c, line 117) is failing because a call to _ux_utility_memory_allocate() (ux_device_stack_initialize.c, line 175) is failing.  There appears to be only 24 bytes left in the USBX device driver memory pool, which is what's left over from the 512 bytes of "USBX Device System Stack Size".  That's not enough to allocate even the first thing that ux_device_stack_initialize() wants to create.

If I put a breakpoint inside _ux_utility_memory_allocate() and add up all the memory that's allocated (including three allocations of UX_SLAVE_REQUEST_DATA_MAX_LENGTH == 2048), it totals 7124 bytes.  This value dictates the minimum "USBX Device System Stack Size".  This value plus the "USBX Device Application Thread Stack Size" (default 1024 bytes) must come in under the "UXDevice memory pool size".  So if I set my USBX Device System Stack Size to 8000 bytes, and set the UXDevice memory pool size to 10000 bytes, then my application actually comes up.

So bottom line... please try to ensure that out-of-the-box configurations for components like USBX are able to function when built, instead of sending users down a rabbit hole.  Thanks.

Dana M.

0 REPLIES 0