Introduction to USB with STM32

Revision as of 08:08, 16 June 2020 by Registered User (→‎STM32 compliant with USB)
Under construction.png Coming soon

On this article, you will find application examples, document references, tips and tricks and so on related to STM32 USB

1. What is the Universal Serial Bus (USB)

The large diversity of ports (parallel, serial, midi, joystick, etc) with their specific requirements and the lack of plug-and-play feature were almost the main reasons that pushed the most known companies in the technology domain to seek for a substitution. These companies developed the USB standard and formed the USB Implementers Forum.
The USB provided the most successful serial interface having the following characteristics:

  • simplicity and flexibility (plug-and-play)
  • bi-directionality
  • increasing speeds
  • low cost

Since developed, the USB has been continuously ameliorated always keeping compatibility with the new technologies evolution and requirements.
The STM32 USB hardware and software are compliant with USB1.1 and USB2.0 specifications and all the following sections will speak about these standard devices.

1.1. USB 2.0 Standard overview

This section focuses on the USB2.0 standard and provides a general overview about the rich characteristics of this standard.

1.1.1. Speed

The USB2.0 supports three speeds:

  • Low speed (LS): supports the transfer rate of 1.5 Mb/s. This speed is mainly dedicated to interactive devices (mouse, keyboard, etc)
  • Full speed (FS): supports the transfer rate of 12 Mb/s. This speed is mainly dedicated to phone and audio devices (microphone, speaker, etc)
  • High speed (HS): supports the transfer rate of 480 Mb/s. This speed is mainly dedicated to video and storage devices (printer, camera, etc)

At protocol level, the USB grants a very high compatibility, so users can not see big differences between dealing with different speeds

1.1.2. USB interconnect components

The USB interconnect has three main components:

  • Host or Root Hub: it is unique for every USB system. It is responsible of initiating all transactions.
  • Function or Device: the final point in the interconnect ensuring the user's required roles (keyboard, mouse, microphone, etc)
  • Hub: a bridge ensuring the communication between the host and many devices. It has one upstream port to be connected directly (point to point connection through USB cable) or indirectly (connection through another hub) to the host and many downstream ports to be connected directly or indirectly to the USB Functions.

The previous components can be connected to each other through USB cables with a maximum length of 5 meters.

1.1.3. USB interconnect topology

The USB physical interconnect is characterized by a tired star topology. Each star has a hub at the center with one upstream connection directly or indirectly with the host and one or more downstream connection(s) with function or other hub.
A maximum of 127 devices (functions or hubs) can be connected to one host (root hub) with a maximum of 5 hubs connected in series.

1.1.4. Power

Generally, the host (root hub) provides power for functions direct connection. Some hubs may supply power for directly connected downstream functions. For the functions, there are two types:

  • Bus powered functions: these devices rely totally on the bus power coming from the upstream hub.
  • Self powered functions: these devices are capable of providing their own power independently from the bus.

1.1.5. System configuration

The USB system has an intelligent mechanism to detect the device attachment and detachment at any time.

  • Device attachment: the host can detect at any time the detachment of a device by continuously querying a bit for all the connected hub ports. Once a device is attached, the host enables the port and gives the device a unique address then establish a communication with this newly connected device to conclude if it is a function or a hub.
  • Device detachment: once a device is detached, the corresponding port will be disabled. If a hub is detached, all the downstream devices' ports that were attached to the removed hub will be disabled and the detached hub's upstream port will be disabled.
  • Bus enumeration: it is a set of hardware and software events allowing the host to continuously detect and address and recognize the newly connected device. It includes also the set of events ensuring the removal of a device.

1.1.6. USB Data transfers

USB communication is based on four main transfer types:

  • Control transfer: mainly used for the configuration data of the newly attached device.
  • Bulk transfer: used for large amounts of data transmission or reception.
  • Interrupt transfer: used for limited data transmission with minimal latency.
  • Isochronous transfer: used for data transfer with real-time requirements.

More detailed explanations of the USB characteristics and data flow will be explained with code examples in the following pages.

2. Getting started with STM32 and USB

This section provides answers that can be asked when starting the use of the STM32 MCU's USB.

2.1. What does the STM32 support?

All the STM32 families (not all products) include the USB IP. Depending on the hardware, each STM32 MCU including the USB can support:

  • Device in FS speed only.
  • OTG ( dual role: device and host) in FS speed.
  • OTG in HS speed.

Some STM32 MCUs include two USB peripherals and support both OTG in FS and HS speeds.
An overview of the USB hardware over the STM32 MCUs will be provided in the next pages.

2.2. What role can the STM32 MCU play within a USB system?

A basic USB system can be established by a host and a device attached with a USB cable. The following figure shows the possible roles that can keep the STM32 MCU's USB:

STM32 USB role within a basic USB system

For more complex USB system, the device can be attached to the host through one or more hub(s). For such system, currently, the STM32 USB can play the role of the device as shown in the figure below.

STM32 USB role within a complex USB system

2.3. How can I select one STM32 MCU for my USB application?

For every USB application, it is important to select the correct STM32 with required USB role (Host or Device) and speed (LS or FS or HS). But there are many STM32 MCUs having the same USB hardware implementation, how to choose? Besides the USB features, the STM32 MCUs offer a large set of peripherals with large features portfolio. The diversity of peripherals and features guarantees the flexibility and ease of the required application's implementation. In fact, the convenient MCU selection is one of the most secrets of a successful USB application.
To select the convenient MCU, the following features must be taken into consideration depending on the application requirements:

  • The MCU performance have a direct impact on data transfer and processing in the STM32 system.
  • The memories (RAM and ROM) availability and sizes are very important for applications processing large amounts of data.
  • the required peripherals availability and features must be checked when selecting the STM32 MCU because peripherals versions and combinations can largely differ from one MCU to another.
  • The power consumption is a very important requirement for some applications

The STM32 compliant with USB section provides an overview of all the STM32 MCUs including USB, it also includes some of the most important features for USB applications requirements.

2.4. Where can I find more detailed information about the USB for the selected STM32 MCU?

For every STM32 MCU, there is a set of documents providing information about all the integrated peripherals and among others the USB. All the STM32 MCUs have a page providing a generic overview with a direct link to the MCU's datasheet. In fact, all the STM32 MCU's USB peripheral information are integrated mainly in the datasheet (specially the electrical characterization) and the reference manual (includes all the MCU's peripherals information thus it provides a detailed information about the integrated USB peripheral(s)).
All the STM32 MCUs technical documents can be found using this link.

2.5. Is there any provided STM32 USB code examples?

Every STM32 Family has a package that includes the MCU's drivers and peripherals examples called the STM32Cube Firmware package. This package includes the USB Device and Host (if supported by the MCU) drivers and code examples for all the supported speeds.
To access the USB code, the STM32Cube Firmware package must be installed. When opening the installed folder, the following steps must be followed:

  • Open "Projects" sub-folder.
  • Open the folder having the name of the convenient board including the selected MCU.
  • Open "Applications" sub-folder
  • Select the USB convenient sub-folder ("USB_Device" for device applications and USB_Host for host applications if supported by the MCU)
  • Select the convenient application
  • Select the convenient IDE (IAR, STM32CubeIDE, KEIL)

At this level, the application's code can be debugged and can be loaded into the STM32 t check the results as mentioned in the readme.txt file which is provided within the application sub-folder. All the STM32 MCUs tools and software can be accessed using this link.
A detailed explanation of all the STM32 USB code parts will be explained in the following pages.

3. Video related to STM32 USB

Special STM32 USB training videos were published to facilitate the understanding of the USB concepts and the STM32 USB supported features. It provides also an explanation of the STM32 USB host and device provided libraries. These videos cover many Hardware and Software concepts that target different USB knowledge levels.

pc videol.png

MOOC - STM32 USB training

In the following pages, some parts of the videos will be explained in more details with some code examples extracted from the STM32Cube firmware.

4. STM32 compliant with USB

The following table provides an overview about the USB hardware implementation through STM32 series. Some part numbers may not include same USB hardware compared to other part numbers in the same family include as shown in the table below:

STM32 series USB Device only USB OTG FS USB OTG HS USB OTG FS&HS
STM32F0
STM32F1
STM32F2
STM32F3
STM32F4
STM32F7
STM32G4
STM32H7
STM32L0
STM32L1
STM32L4
STM32L4+
STM32L5
STM32WB


More detailed information about every STM32 MCU and Board features and peripherals can be found using the ST-MCU-FINDER.

5. Specific tools

Links and explanations for the tech domain dedicated tools

6. STMicroelectronics Resources

The place for AN, UM... direct links

7. Examples

The area where FAE's, collab... can put examples (linked to Github)




[[Category:]]