STM32 USB learning notes 1

Publisher:炉火旁的YyeLatest update time:2019-03-11 Source: eefocusKeywords:STM32  USB Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


Keywords:STM32  USB Reference address:STM32 USB learning notes 1

Previous article:STM32 USB learning notes 2
Next article:STM32 TFT Learning Notes - Touch Screen

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号