2026-05-24 9:09 PM
Hello,
I need to use STM32U5G-DK1 board in USB OTG mode. Right now with the help of examples provided by STM for STM32U5G-DK1 and DK2 board, I am able to use DK1 either in Device mode or in Host mode. In order to use it in OTG mode what changes need to be done so that it can adapt Device or Host mode accordingly without changing the code.
Please elaborate step by step. Thank you
With regards,
Sudarshan Chaudhary
2026-05-25 2:19 AM
Hello @Sudarshan
Currently, there is no ready-to-use STM32 example that implements the full USB OTG protocol end to end.
If you want to proceed with an OTG implementation, consider the following main points:
Project structure: When selecting OTG/DRD mode in STM32CubeMX, the tool does not currently generate a complete project structure with both host and device class files in a single project. The first step is to manually prepare the project to include:
The simplest method is to generate a device class project and a host device class project then overwrite the files of one project in the other to obtain a project that have both modes files
This preparation provides the foundation for implementing both roles in one application.
Additional software development: Implementing OTG behavior requires custom development beyond the default middleware integration. You must implement logic that handles:
Role identification and USB OTG concepts: In OTG, the connected roles are referred to as A-device and B-device. The role is determined by the ID pin or ID signal status on supported hardware. Typically, the A-device becomes the host, and the B-device becomes the peripheral. The actual behavior depends on the USB connector, cable type, and board hardware design.
Role switching sequence: The implementation involves handling the following mechanisms:
You must work with OTG-related registers, such as:
These registers are important for monitoring OTG status and controlling USB core behavior.
To reduce complexity, start with dual-role USB behavior in a simplified implementation, then extend it step by step toward the required OTG use case. I recommend following this article which show how to implement DRD protocol in a ST classic middleware product, it may differ a little bit from your case since STM32U5 is USBX native project (so threads and memory management will be required) but principal is the same, you can rely on this project as example since it's based on USBX.
The STM32U5 reference_manual contains helpful information. Consult sections 72.4.5, 72.5.1, 72.5.2, 72.5.3, and especially 72.16.7 (OTG programming mode).
I hope my answer helps you and meets your expectations.
BR
Gyessine
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.
We’re moving the ST Community to a new platform to give you a better and more reliable community experience.