cancel
Showing results for 
Search instead for 
Did you mean: 

Using STM32U5G-DK1 in USB OTG mode

Sudarshan
Associate II

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

1 REPLY 1
Gyessine
ST Employee

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 required USB host class files
  • The required USB device class files
  • The necessary USB middleware components for both roles
  • The correct include paths and project settings to ensure the project builds properly

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:

  • The initial role setup
  • The role switching sequence
  • The state transitions between host and device operation
  • The handling of any application-level conditions required for switching

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:

  • SRP: Session Request Protocol, used by the peripheral to request that the host powers the bus or starts a session
  • HNP: Host Negotiation Protocol, used to negotiate a role swap between host and peripheral when OTG exchange is supported

You must work with OTG-related registers, such as:

  • OTG_GOTGCTL
  • OTG_GOTGINT
  • OTG_GUSBCFG
  • OTG_GINTSTS

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.