2024-11-27 05:17 AM - last edited on 2024-11-27 06:55 AM by Andrew Neil
Hello STM32 community!
I'm relatively new to the embedded field and have been diving into communication protocols through small projects. Currently, I'm working on implementing USB CDC on an STM32H563ZI, and I've been facing challenges for the past two weeks.
I've extensively searched the internet and followed various tutorials, but unfortunately, none of them seem to work.
One of the tutorial(from STM community) that I followed: https://www.youtube.com/watch?v=LdY2bieaPwk
I have added read functions to receive the data. I have made two separate semaphores for read and write functions. (It should work even without semaphores though). When I run the code, it's not taking any input from the terminal.
Inside app_usbx_device.c file,
these two threads(for read and write) were created inside MX_USBX_Device_Init() function.
I am giving semaphore inside the while loop here...
Inside ux_device_cdc_acm.c file,
I am declaring variables...
I am defining the read and write functions like this...
it's not taking any input data
this is the heap and stack size I'm giving,
clock configuration,
to USB,
I would greatly appreciate it if anyone who can debug the mistake in this process and could guide the further steps to take.
Your insights and guidance would be invaluable to me. Thank you in advance for any assistance you can provide!
Please find the attached, project folder for further info.
2024-11-27 06:39 AM
The easiest way is to open an existing example. Here is probably the simplest one - it copies data from a hardware uart to the usb cdc:
\STM32Cube\Repository\STM32Cube_FW_H5_V1.3.0\Projects\NUCLEO-H533RE\Applications\USBX\Ux_Device_CDC_ACM\USBX\App\app_usbx_device.c
Doing it from a blank CubeMx project is actually seriously non-trivial. There are multiple files and settings which need to be changed compared to the default USBX configuration (memory pool sizes, interrupt priorities etc.). None of it is documented properly anywhere.
I did it once, for a CDC device in a USBX standalone mode (without ThreadX) and it was a lot of work.
If you want to go through that, you would need to compare the entire source tree with the example project (Core and USBX directories) and pick out all the changes they made in the example. There will be dozens of them.
2024-11-28 01:44 AM - edited 2024-11-28 01:52 AM
Hello @Pranathi and welcome to the Community,
This article may help you on your implementation of the USB Device CDC in STM32H5 microcontrollers:
How to implement a USB device composite in STM32H5... - STMicroelectronics Community
2024-11-28 02:44 AM
One item to be careful with is the PCD buffer configuration. It seems to be dependent on the chip family, device profile, and different examples have different values.
The G4 article quoted by @Imen.D has these values for CDC on a G4:
HAL_PCDEx_PMAConfig(&hpcd_USB_FS, 0x00 , PCD_SNG_BUF, 0x40);
HAL_PCDEx_PMAConfig(&hpcd_USB_FS, 0x80 , PCD_SNG_BUF, 0x80);
HAL_PCDEx_PMAConfig(&hpcd_USB_FS, 0x01, PCD_SNG_BUF, 0xC0);
HAL_PCDEx_PMAConfig(&hpcd_USB_FS, 0x81, PCD_SNG_BUF, 0x100);
HAL_PCDEx_PMAConfig(&hpcd_USB_FS, 0x82, PCD_SNG_BUF, 0x140)
this example for CDC on H533:
\STM32Cube\Repository\STM32Cube_FW_H5_V1.3.0\Projects\NUCLEO-H533RE\Applications\USBX\Ux_Device_CDC_ACM\USBX\App\app_usbx_device.c
has these values:
HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x00, PCD_SNG_BUF, 0x14);
HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x80, PCD_SNG_BUF, 0x54);
HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x81, PCD_SNG_BUF, 0x94);
HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x01, PCD_SNG_BUF, 0xD4);
HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x82, PCD_SNG_BUF, 0x114);
and this example, for CDC+HID on H563:
\STM32Cube\Repository\STM32Cube_FW_H5_V1.3.0\Projects\NUCLEO-H563ZI\Applications\USBX\Ux_Device_HID_CDC_ACM\USBX\App\app_usbx_device.c
has this:
HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x00, PCD_SNG_BUF, 0x20);
HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x80, PCD_SNG_BUF, 0x60);
HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x81, PCD_SNG_BUF, 0xA0);
HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x01, PCD_SNG_BUF, 0xE0);
HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x82, PCD_SNG_BUF, 0x120);
HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x83, PCD_SNG_BUF, 0x140);
so a detailed study of the Reference Manual for the chip to be used is required.
2024-11-28 02:53 AM
I updated the right link related to STM32H5 article :