Host environment: Windows 7 SP1
Development environment: MDK5.14
Target board: STM32F103C8T6
Development library: STM32F1Cube library and STM32_USB_Device_Library
I got an STM32 development board from the STM32 forum before. I thought it had a USB module interface. But I found out that there was no external USB module in NUCLEO. So I bought a single board with my own money, as shown in the figure below:
The single board is very simple, purely for learning about the STM32 USB module. The reason for choosing this is that it is cheap, and ST officially provides a USB module library to facilitate development.
USB development is extensive and profound. It has many more things than serial ports, SPI, and I2C. A simple example is that serial ports, SPI, and I2C can all be simulated using general IO ports, but few people do this with USB. To learn USB modules, you must learn the USB protocol. The USB protocol has evolved from 1.1 to 2.0 and then to 3.0. Now it seems to be version 3.1. There have been so many version changes, but it does not affect our learning, because USB protocols are backward compatible, and learning one is enough.
The USB module of STM32F103C8T6 implements USB2.0 full-speed bus, complies with the counting specification of USB2.0 full-speed devices, supports 8 bidirectional endpoints, etc.
The USB bus uses two wires to achieve differential transmission D+ and D-, which is similar to the 485 bus. Therefore, only one direction of transmission can be carried out on the USB bus at the same time.
The USB2.0 protocol specification can be downloaded from the USB official website. Just focus on Chapter 9. It is very boring to read a 600-page document from beginning to end. Of course, if you have the energy, it is also very helpful to read the entire specification. There are many other specifications related to the USB2.0 specification. You can read them again if necessary during development.
1. USB Device Library Overview
The USB documents provided by ST official website include STM32Cube USB device library and some documents for USB training. It is better to focus on the USB device library documents, because we treat the USB module of STM32 as a slave device. Of course, if STM32 supports USB host, there is another host library (not discussed here). By looking at the STM32Cube USB device library, we can quickly understand the architecture of the USB module and simplify application development. The location of the STM32CubeUSB device library in Cube is as follows:
The USB device library is located above the HAL driver layer. In development, we only need to focus on the USB device library, not the HAL driver of the USB device, otherwise the USB device library will have no meaning. In addition, the USB device library code is read-only and does not need to be modified.
The main structure used in the device library is the device handle, whose type is USBD_HandleTypeDef. We don't have to pay special attention to the data structures used by USB in the USB HAL driver, because they are hidden from us. We only need to pay attention to the interface exposed to us by the device library. The device library is closely related to the USB2.0 protocol, so we need to be familiar with the USB2.0 documentation to help us understand the workflow of the device library.
The USB device library directory is divided into two layers: one is the Core layer; the other is the Class layer. The directory structure is as follows:
The structure is relatively clear, the core layer does not need to be changed, and the class layer is for expansion.
The usbd_conf file in the core layer of the USB device library needs to be implemented by the user to configure the properties of the USB. The functions of each file are as follows:
Among them, usbd_req is changed to usbd_ioreq. Table 7 shows that Chapter 9 in the USB2.0 specification is very important. The USB device library document also explains the control endpoint 0 in detail, which is an important endpoint in USB development. As the default control endpoint, it is used to receive various requests from the host. The request is sent to the device as a Setup packet. The request is divided into standard request and non-standard request. Standard request can be processed on control endpoint 0. For non-standard request, it is passed to a specific class through a callback function for further processing.
The functions of each file in the class layer of the USB device library are as follows:
The usbd_xxx_if_template file needs to be implemented by the user. Currently, there are two files related to the USB device library that need to be implemented by the user, namely the usbd_conf file and the usbd_xxx_if_template file. This is the end of the overview of the USB device library. For more details, please refer to the USB device library document.
2. USB Protocol Overview
Some logical components used in the USB protocol:
Endpoint: The final object of data transmission
Pipe: A model for data transfer between host and device endpoints
Interface: used to describe a specific function (consisting of a set of endpoints)
Device: consists of one or more interfaces
USB transmission is divided into three layers: Packet, Transaction, and Transfer. The three are from small to large, and the relationship is as follows:
A transfer is divided into several transactions, and a transaction consists of several packers.
The packet format is as follows:
All USB transmissions are initiated by the USB host, which is somewhat similar to I2C communication.
USB transmission is divided into four categories: control transmission, interrupt transmission, synchronous transmission, and bulk transmission, which leads to four types of endpoints: control endpoint, interrupt endpoint, synchronous endpoint, and bulk endpoint.
Control transmission is bidirectional, requiring endpoint numbers for both IN and OUT directions. Other types of transmission are unidirectional. The differences between the various types of transmission are not very large, except for the different timing requirements.
This is the general introduction to the USB protocol. For more details, please refer to the USB2.0 specification and the STM32F103 reference manual. Later, USB development will be analyzed through VCP specific examples.
Previous article:STM32 USB learning notes 2
Next article:STM32 TFT Learning Notes - Touch Screen
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- [Silicon Labs BG22-EK4108A Bluetooth Development Review] Received the fastest, unboxing
- Introduction to three interrupt debugging methods of ARM
- Ask a superconductivity question
- CC2640R2F Single Carrier Code
- Essential PCB wiring rules for electronic hardware engineers: basic theories of right-angle routing, differential routing, and serpentine routing
- 【EETALK】What is the difference between traditional Bluetooth and low-power Bluetooth? Will traditional Bluetooth be replaced by low-power Bluetooth in the future?
- Design of multi-parameter water quality monitoring system based on Internet of Things
- How to select PCB isolated power supply
- Technical Articles | Frontline engineers explain to you: Tips for testing the dark current of passive device PD
- BSL source code for DM642EVM