Introduction to USB with STM32

Revision as of 13:16, 1 June 2020 by Registered User
Under construction.png Coming soon

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): the data bandwidth varies from 10 to 100 Kb/s. This speed is mainly
    dedicated to interactive devices (mouse, keyboard, etc)
  • Full speed (FS): the data bandwidth varies from 500 Kb/s to 10 Mb/s. This speed is mainly
    dedicated to phone and audio devices (microphone, speaker, etc)
  • High speed (HS): the data bandwidth varies from 500 Kb/s to 10 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.
  • Device and Host in FS speed.
  • Device and Host in HS speed.
  • Device and Host in HS speed.

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

2.2. How can I select one STM32 MCUs?

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.3. Where can I find more detailed information about the USB for the selected STM32 MCU?

For every STM32 MCU, there are a set of documents providing information about all the integrated peripherals and among others the USB. these documents can be found by following the following steps: 1- All STM32 families will be found with every family focus by clicking this link, the following figure will be displayed allowing users to select the convenient family:

File:STM32 families and main applications.png
STM32 families and main applications


2- After selecting the convenient STM32 family, an overview of this family will be displayed with every product line's features overview. If more detailed information about the difference between the different product lines, the "Product selector" icon leads to a page including a table with detailed features. This page allows to compare the product lines features. The convenient product line can be selected by clicking its name in the "overview" or the "Product selector" page.
3- After selecting one product line, a new page with all the STM32 MCUs included in the selected line will be displayed. The convenient STM32 MCU can be chosen by clicking its name. The MCU page will be displayed providing a generic overview with a direct link to the datasheet. Also, there two very helpful links:

  • "Resouces" leads to all the MCU documents with quick links as it is shown in the figure below.
File:MCU Resources quick links.png
MCU Resources quick links


In fact, most of these documents concern the STM32 MCU not only USB. But all the 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))

  • "Tools & Software" shows all the provided STMicroelectronis software facilitating the use of the selected MCU with quick links as shows the following figure.
File:Tools & Software quick links.png
Tools & Software quick links

3. Video related to STM32 USB

Special STM32 USB trainings videos were published to help you to understand easier the USB concepts and the STM32 supported features
The following videos focuse on explaining the USB 2.0 main concepts.

pc videol.png

MOOC - STM32 USB training

Learn how to use USB Device and USB Host within STM32 based application ...


pc videol.png

STM32 USB training - USB Introduction

pc videol.png

STM32 USB training - USB transfers

pc videol.png

STM32 USB training - USB descriptors

pc videol.png

STM32 USB training - USB standard class overview

pc videol.png

STM32 USB training - USB over STM32 family

pc videol.png

STM32 USB training - STM32 USB hardware design

pc videol.png

STM32 USB training - STM32 USB Device library

pc videol.png

STM32 USB training - USB CDC device basic labs

pc videol.png

STM32 USB training - USB VCP Zero Packet Length lab

pc videol.png

STM32 USB training - USB CDC libusb device lab

pc videol.png

STM32 USB training - USB MSC DFU host labs

pc videol.png

STM32 USB training - USB HID host labs

pc videol.png

USB training - USB debugging

pc videol.png

STM32 USB training -USB driver signature

pc videol.png

USB training - USB CDC throughput

pc videol.png

STM32 USB training - USB DFU device labs

pc videol.png

STM32 USB training - STM32 USB Host library

pc videol.png

STM32 USB training - USB VCP host labs

pc videol.png

STM32 USB training - USB MSC host labs

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 place to put a table with all STM32 compliant with the feature

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:]]