on 2025-10-22 8:00 AM
Following the previous article, this one provides STM32 developers with practical examples and guidance on configuring USB applications using the packet memory area (PMA) for different USB classes: communication device class (CDC), human interface device (HID), and mass storage class (MSC). It covers endpoint configuration, buffer allocation, and best practices for maximizing USB communication efficiency.
Each endpoint type has specific size and buffering requirements:
Ensure that data payloads do not exceed these sizes to prevent aborted transfers.
Endpoints have unique addresses comprising direction and endpoint number:
Example:
#define EP1_OUT 0x01 // Endpoint 1, OUT direction
#define EP1_IN 0x81 // Endpoint 1, IN direction
The HAL_PCDEx_PMAConfig() function is used to configure the PMA for each endpoint in the application. This function is typically called in the USBD_LL_Init function in the usbd_conf.c file, which is generated by STM32CubeMX.
HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd, uint16_t ep_addr, uint16_t ep_kind, uint32_t pmaadress);
hpcd : Pointer to the USB device instance.ep_addr : Endpoint address (direction + number.)ep_kind : Endpoint type (USB_SNG_BUF for single buffer, USB_DBL_BUF for double buffer.)pmaaddress : PMA address for the endpoint buffer(s.)STM32 developers must not rely on STM32CubeMX code generation for PMA configuration. Instead, follow these guidelines:
HAL_PCDEx_PMAConfig() for each endpoint in your USB device configuration.0x00 to store endpoint buffer descriptors.CDC applications typically use three endpoints: Bulk IN, Bulk OUT, and Command Interrupt IN.
HAL_PCDEx_PMAConfig(&hpcd_USB_FS, EP1_IN, PCD_SNG_BUF, 0x40); //EP1 BULK IN
HAL_PCDEx_PMAConfig(&hpcd_USB_FS, EP1_OUT, PCD_SNG_BUF, 0x80); // EP1 Bulk OUT
HAL_PCDEx_PMAConfig(&hpcd_USB_FS, EP2_IN, PCD_SNG_BUF, 0xC0); //EP2 Interrupt IN and Size of each region is 64B
HID applications typically use an Interrupt IN endpoint to send data to the host.
HAL_PCDEx_PMAConfig(&hpcd_USB_FS, EP3_IN, PCD_SNG_BUF, 0x100); //EP3 Interrupt IN 64B
MSC applications use Bulk IN and Bulk OUT endpoints for data transfer.
HAL_PCDEx_PMAConfig(&hpcd_USB_FS, EP4_IN, PCD_SNG_BUF, 0x140); //EP4 Bulk IN 64B
HAL_PCDEx_PMAConfig(&hpcd_USB_FS, EP4_OUT, PCD_SNG_BUF, 0x180); // EP4 Bulk Out 64B
Audio speaker or headset application use isochronous OUT endpoints for data transfer.
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData, AUDIO_OUT1_EP, PCD_DBL_BUF, 0x01500090);
// EP4 ISO Out : 32-bit PMA addresses split into two 16-bit
Configuring USB applications on STM32 involves careful management of endpoints and buffer memory. By following these guidelines and examples, developers can optimize their USB applications for efficient data transfer and reliable communication.
For further detailed technical information, consult the USB section in the reference manual applicable to your specific STM32 product series. If you have further questions, feel free to ask on ST Community forums.