cancel
Showing results for 
Search instead for 
Did you mean: 

USBX Device Stack: UX_MEMORY_INSUFFICIENT During MSC Endpoint Allocation

Grace_04
Associate II

I am using STM32N657 with USBX (MSC device).
The function ux_device_stack_initialize() is returning UX_ERROR, and after debugging inside USBX I found that the failure occurs when USBX tries to allocate endpoint transfer buffers.

The failing code inside USBX is:

#if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 0

/* Obtain some memory. */

endpoints_pool -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer =

_ux_utility_memory_allocate(UX_NO_ALIGN, UX_CACHE_SAFE_MEMORY, UX_SLAVE_REQUEST_DATA_MAX_LENGTH);

/* Ensure we could allocate memory. */

if (endpoints_pool -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer == UX_NULL)

{

status = UX_MEMORY_INSUFFICIENT; //0x12

break;

}

#endif

So the device stack initialize fails because of UX_MEMORY_INSUFFICIENT.

My current USBX memory setup:

#define UX_APP_MEM_POOL_SIZE (128 * 1024) // 128 KB total pool
#define USBX_DEVICE_MEMORY_STACK_SIZE (5 * 1024) // only 5 KB given to USBX


I allocate memory for USBX like this:

tx_byte_allocate(byte_pool, (VOID **) &pointer, USBX_DEVICE_MEMORY_STACK_SIZE, TX_NO_WAIT);

ux_system_initialize(pointer, USBX_DEVICE_MEMORY_STACK_SIZE, UX_NULL, 0);

This means that even though my total pool is 128 KB, USBX actually receives only 5 KB for all internal allocations (endpoint buffers, MSC class buffers, descriptors, etc.).

My question:

Is 5 KB far too small for USBX MSC on STM32N6?
What is the recommended minimum memory size that must be passed to ux_system_initialize() when using MSC (Mass Storage Class)?

From debugging, USBX cannot allocate the MSC endpoint transfer buffers with only 5 KB, so initialization fails with UX_MEMORY_INSUFFICIENT.
I want to know the correct recommended memory size for MSC on STM32N6.

I am using USBX specifically for Mass Storage Device (MSC).

Thank you.

3 REPLIES 3
T_Hamdi
ST Employee

Hello @Grace_04,

you can refer to the USBX Mass Storage Class (MSC) Device application provided in the STM32CubeN6 software package. This example will help you understand how to configure and use the MSC class with USBX.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Hamdi Teyeb
Grace_04
Associate II

Hello ,

Thank you for the reference. We have already gone through the USBX MSC working example from the STM32CubeN6 package. Since the example does not include an IOC file, we manually replicated the same configuration and code in our project.

However, even with the exact implementation, the USBX device stack still returns UX_MEMORY_INSUFFICIENT during initialization. We are trying to understand why the memory allocation succeeds in the example project but fails in our setup with identical settings.

FBL
ST Employee

Hi @Grace_04 

The 5 KB memory size is generally considered the default size for the USBX MSC device stack. However, this is not applicable in all cases. This may need to be adjusted according to your configuration (including number of Endpoints used). The total USBX device memory pool size should be adjusted to accommodate not only the USBX Device System Stack Size but also the stack sizes of the threads used within the USBX application.

If your application uses more threads or larger stacks, you should increase the memory pool accordingly to ensure successful initialization and operation.

So, you need to increase the amount of memory allocated for the USBX Device System Stack Size beyond 5 KB, for example to 10–15 KB.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.




Best regards,
FBL