USB Component  Version 6.17.0
MDK Middleware for USB Device and Host Communication
Resource Requirements

This section describes the resource requirements of the USB component.

USB Device Resource Requirements

The following section documents the requirements for the USB Device component. The actual requirements depend on the components used in the application and the configuration of these components.

Stack Requirements

The USB Device Core receives events sent from the interrupt service routine (ISR) of the USB Device Driver. The stack requirements for the ISR are typically less than 512 Bytes. The total stack space required for ISR depends on the interrupt nesting and therefore on the priority settings of these ISR. The stack requirements for ISR are configured in the startup_device.s file located under the Device component class.

Option (under section Stack Configuration) Increase Value by
Stack Size (in Bytes) + 512 for USB Device Driver
Note
When using a CMSIS-RTOS, the Stack Size in the startup_device.s file configures only the stack space that is used by exception and interrupt service routines of the drivers. The stack size requirements depend on the maximum nesting of exception and ISR execution and therefore on the priority settings of the various interrupt and exception sources.

User code that calls API functions of the USB Device Component should have a minimum of 512 Bytes of stack space available. Since API functions are frequently called from threads, the thread stack size should be at least 512 Bytes (see below).

CMSIS-RTOS Requirements

Currently, the USB Device component works in four different RTOS configurations:

  1. CMSIS-RTOS2 RTX5: This mode uses Keil RTX5 natively. In the Manage Run-Time Environment window, the component CMSIS:RTOS2 (API):Keil RTX5 must be selected. The RTOS resources are static, configured by the system. There is no need for additional user configuration. This is the suggested option for new development.
  2. CMSIS-RTOS RTX5: this mode uses RTX5 under a compatibility layer, which uses CMSIS-RTOS v1. In the Manage Run-Time Environment window, the components CMSIS:RTOS (API):Keil RTX5 and CMSIS:RTOS2 (API):Keil RTX5 must be selected.
  3. CMSIS-RTOS RTX: In the Manage Run-Time Environment window, the component CMSIS:RTOS (API):Keil RTX must be selected. For the RTOS configuration, please refer to section RTX v4 Requirements.
  4. CMSIS-RTOS2 with a generic RTOS: this mode uses a generic RTOS (such as FreeRTOS). The RTOS resources (thread, mutex and semaphore control blocks and thread stacks) are dynamically allocated. The user needs to provide memory pools big enough for the allocations in RTOS configuration.

The USB Device component uses threads. Each instance of a component starts its own threads, for example two HID instances start two threads with the name: USBD_HID0_Thread, USBD_HID1_Thread. The thread execution starts with USBD_Initialize.

The following table lists the components, number of threads, thread name, default thread priority, default stack size and the configuration file with OS Resource Settings for stack size adjustments.

Component Number of Threads Thread Name Default Priority Default Stack Size Configuration File
::USB:Device 1 USBDn_Core_Thread osPriorityAboveNormal 512 Bytes USBD_Config_n.c
::USB:Device:ADC 1 USBD_ADCn_Thread osPriorityAboveNormal 512 Bytes USBD_Config_ADC_n.c
::USB:Device:CDC 2 USBD_CDCn_Bulk_Thread, USBD_CDCn_Int_Thread osPriorityAboveNormal 512 Bytes USBD_Config_CDC_n.c
::USB:Device:Custom Class 1...15 USBD_CustomClassn_EPm_Thread osPriorityAboveNormal 512 Bytes USBD_Config_CustomClass_n.c
::USB:Device:HID 1 USBD_HIDn_Thread osPriorityAboveNormal 512 Bytes USBD_Config_HID_n.c
::USB:Device:MSC 1 USBD_MSCn_Thread osPriorityAboveNormal 512 Bytes USBD_Config_MSC_n.c

n is instance number, m is Endpoint number 1...15

Note
  • For ::USB:Device:Custom Class each endpoint In/Out pair executes in separate thread. The number of threads depends on the number of endpoints enabled in the configuration file USBD_Config_CustomClass_n.c.
  • The Default Stack Size in the table above can be changed in the USB configuration files (for example USBD_Config_n.c and USBD_Config_DeviceClass_n.c). At the end of each configuration file, you will find OS Resource Settings and below that the value for the Thread stack size.

RTX v5 Requirements

The USB Device component allocates all necessary resources statically so no changes to RTX settings are necessary.

RTX v4 Requirements

For the USB Device component, the number of threads and thread stack size requirements are calculated as follows:

  • Number of Threads: add all threads of enabled components listed above.
  • Total stack size: add the stack sizes of the related threads.

With default stack size of 512 Bytes, the Total stack size is Number of Threads * 512 Bytes. When the default OS Resource Settings are modified, the changes need to be considered for the calculation of the Total stack size.

Examples (with default stack size of 512 Bytes):

  • USB Device with HID uses 2 threads (1 for USB Device + 1 for HID) and 1024 Bytes stack.
  • Composite USB Device with HID and MSC uses 3 threads (1 for USB Device + 1 for HID + 1 for MSC) and 1536 Bytes stack.
  • USB Device with CDC uses 3 threads (1 for USB Device + 2 for CDC) and 1536 Bytes stack.
  • USB Device with Custom Class that has 3 endpoints uses 4 threads (1 for USB Device + 3 for Custom Class) and 2048 Bytes stack.

Requirements are configured in the file located under the CMSIS component class:

  • Thread Configuration
    Option Value
    Number of concurrent running user threads + Number of Threads
    Number of threads with user-provided stack size + Number of Threads
    Total stack size [bytes] for threads with user-provided stack size + Total stack size
  • System Configuration (if HID class is used)
    Option Value
    User Timers Enable

Timers

The ::USB:Device:HID component requires timers which need to be enabled in RTOS.

Semaphores

Number of semaphores used by the USB Device components:

Component Number of Semaphores per instance
::USB:Device 3
::USB:Device:ADC 1..2
::USB:Device:CDC 3
::USB:Device:Custom Class 1..30
::USB:Device:HID 1 + 1..2
::USB:Device:MSC 2
Note
For values specified as range (..) one semaphore per endpoint is used.
For example if custom class uses one Endpoint In and one Endpoint Out two additional semaphores are allocated and used.

Memory Requirements

The following table shows the memory requirements for read-only (Flash) and read-write (RAM) memory. These numbers may be used to estimate the total size requirements of the application, but the actual numbers depend on compiler optimizations and target processor and may be therefore slightly different.

Component RO Memory [Bytes] RW Memory (Data) [Bytes]
::USB:Core (Device) 6.0 k < 470
::USB:Device:ADC 2.5 k < 110 + Audio Subframe Sizes * Buffer Sizes (configured in the USBD_Config_ADC_n.h)
::USB:Device:HID 2.7 k < 130 + Maximum Report Sizes (configured in the USBD_Config_HID_n.h)
::USB:Device:MSC 4.1 k < 160 + Maximum Bulk Buffer Size (configured in the USBD_Config_MSC_n.h)
::USB:Device:CDC ACM 3.2 k < 220 + Maximum Send/Receive Buffer Sizes (configured in the USBD_Config_CDC_n.h)
::USB:Device:Custom Class 2.0 k < 80
USB Device Driver 1 .. 4 k 0.5 .. 4 k

USB Host Resource Requirements

The following section documents the requirements for the USB Host component. The actual requirements depend on the components used in the application and the configuration of these components.

Stack Requirements

The USB Host Core receives events sent from the interrupt service routine (ISR) of the USB Host Driver. The stack requirements for the ISR are typically less than 512 Bytes. The total stack space required for ISR depends on the interrupt nesting and therefore on the priority settings of these ISR. The stack requirements for ISR is configured in the startup_device.s file located in the Device components.

Option (under section Stack Configuration) Increase Value by
Stack Size (in Bytes) + 512 for USB Host Driver
Note
When using a CMSIS-RTOS, the Stack Size in the startup_device.s file configures only the stack space that is used by exception and interrupt service routines. The stack size requirements depend on the maximum nesting of exception and ISR execution and therefore on the priority settings of the various interrupt and exception sources.

User code that calls API functions of the USB Host Component should have a minimum of 512 Bytes of stack space available. Since API functions are frequently called from threads, the thread stack size should be at least 512 Bytes.

CMSIS-RTOS Requirements

Currently, the USB Host component works in four different RTOS configurations:

  1. CMSIS-RTOS2 RTX5: This mode uses Keil RTX5 natively. In the Manage Run-Time Environment window, the component CMSIS:RTOS2 (API):Keil RTX5 must be selected. The RTOS resources are static, configured by the system. There is no need for additional user configuration. This is the suggested option for new development.
  2. CMSIS-RTOS RTX5: this mode uses RTX5 under a compatibility layer, which uses CMSIS-RTOS v1. In the Manage Run-Time Environment window, the components CMSIS:RTOS (API):Keil RTX5 and CMSIS:RTOS2 (API):Keil RTX5 must be selected.
  3. CMSIS-RTOS RTX: In the Manage Run-Time Environment window, the component CMSIS:RTOS (API):Keil RTX must be selected. For the RTOS configuration, please refer to section RTX v4 Requirements.
  4. CMSIS-RTOS2 with a generic RTOS: this mode uses a generic RTOS (such as FreeRTOS). The RTOS resources (thread, mutex and semaphore control blocks and thread stacks) are dynamically allocated. The user needs to provide memory pools big enough for the allocations in RTOS configuration.

The USB Host component uses threads. Each USB Host instance starts its own thread (USBHn_Core_Thread). The thread execution starts with USBH_Initialize. Additionally each instance of HID and CDC class also start their own thread (USBH_HIDn_IntIn_Thread or USBH_CDCn_IntIn_Thread) when HID or CDC device is connected.

The following table lists the components, number of threads, thread name, default thread priority, default stack size and the configuration file with OS Resource Settings for stack size adjustments.

Component Number of Threads Thread Name Default Priority Default Stack Size Configuration File
::USB:Host 1 USBHn_Core_Thread osPriorityAboveNormal 1024 Bytes USBH_Config_n.c
::USB:Host:CDC 1 USBH_CDCn_IntIn_Thread osPriorityAboveNormal 512 Bytes USBH_Config_CDC.h
::USB:Host:HID 1 USBH_HIDn_IntIn_Thread osPriorityAboveNormal 512 Bytes USBH_Config_HID.h

n is instance number

Note
  • The Default Stack Size in the table above can be changed in the USB configuration files (for example USBH_Config_n.c and USBH_Config_DeviceClass.c). At the end of each configuration file, you will find OS Resource Settings and below that the value for the Thread stack size.
  • If File System is used retargeted to USB Host Mass Storage the stack size for thread calling File System functions should be at least 1536 Bytes.

RTX v5 Requirements

The USB Host component allocates all necessary resources statically so no changes to RTX settings are necessary.

RTX v4 Requirements

For the USB Host component, the number of threads and thread stack size requirements are calculated as follows:

  • Number of Threads: add all threads of enabled components listed above.
  • Total stack size: add the stack sizes of the related threads.

With default stack sizes as defined above the Total stack size is sum of Default Stack Sizes. When the default OS Resource Settings are modified, the changes need to be considered for the calculation of the Total stack size.

Examples (with default stack sizes):

  • Single USB Host with MSC uses 1 thread (for USB Host) and 1024 Bytes stack.
  • Single USB Host with MSC and single HID instance uses 2 threads (1 for USB Host + 1 for HID) and 1536 Bytes stack.
  • Dual USB Host with MSC and two HID instances uses 4 threads (2 for USB Host + 2 for HID) and 3072 Bytes stack.

Requirements are configured in the file located under the CMSIS component class:

  • Thread Configuration
    Option Value
    Number of concurrent running user threads + Number of Threads
    Number of threads with user-provided stack size + Number of Threads
    Total stack size [bytes] for threads with user-provided stack size + Total stack size
  • System Configuration
    Option Value
    User Timers Enable

Timers

The ::USB:Host component requires timers which need to be enabled in RTOS.

Semaphores

The ::USB:Host component uses one semaphore per instance.

Mutexes

The ::USB:Host component uses one mutex per instance.

Note
If custom class component is used, user must insure pipe operations on the same pipe are not interrupted.

Signals

Any user thread using USB Host transfer functions is blocked internally in the library by waiting on signals. Signals (bits 0. to 12.) are reserved for the USB Host communication and must not be used by the user application in that thread.

Memory Requirements

The following table shows the memory requirements for read-only (Flash) and read-write (RAM) memory. These numbers may be used to estimate the total size requirements of the application, but the actual numbers depend on compiler optimizations and target processor and may be therefore slightly different.

Component RO Memory [Bytes] RW Memory (Data) [Bytes]
::USB:Core (Host) < 9.5 k < 370 + Memory Pool Size (configured in the USBH_Config_n.c)
::USB:Host:HID < 1.3 k < 70
::USB:Host:MSC < 3.4 k < 40
::USB:Host:CDC < 1.0 k < 30
USB Host OHCI support < 3.5 k < 0.5 k
USB Host EHCI support < 5.0 k < 4.5 k