• USB data communication design based on STM32F103

    • source: ivan;
    • Time: 3/23/2020 7:09:29 PM
  • USB bus is used for data transmission between microprocessor and PC. Based on STM32F103 chip, the USB module is used to design the USB hardware interface circuit, and the USB firmware library is introduced in layers. The designed system has been successfully applied and the designed method can be quickly transplanted to other USB devices.

    0 the introduction

    USB is the universal serial bus written by the bus is currently very widely used bus standard. USB devices with its support of hot plug, easy to carry, easy to use, unified standards, fast transmission speed, cheap price and other advantages in the current embedded system applications occupy a considerable share.

    With the development and application of embedded system, the use of the small terminal of the microprocessor is more and more popular, the early microprocessor interface between PC and usually use serial, parallel, compared with the USB interface, a serial port and parallel port interface large volume, low transfer rate, between the development of the microprocessor and PC USB data communication technology is becoming an important research topic. This paper USES STM32F103 microprocessor of ST company, integrated USB control module, no need to add other logic control circuit, simplified the hardware circuit design between microprocessor and PC, improved the reliability of the system; Using the USB firmware library of ST company, the development of custom USB device firmware program is realized. At the same time, the development of USB device driver in the PC, to achieve the data transmission between the microprocessor and PC.

    1 STM32F103 USB controller

    STM32F103 is a 32-bit processor based on ARM's cortex-m3 architecture introduced by stmicroelectronics. It has abundant peripheral interfaces, low power consumption and internal USB full-speed device interface. The USB full speed device interface provides a communication connection conforming to USB specification for data transmission between PC host and microprocessor. The data transfer between the PC host and the microcontroller is accomplished by sharing a dedicated data buffer. The size of the dedicated buffer is determined by the number of endpoints used for the data transfer and the maximum data group size for each endpoint. Typically, each endpoint has a maximum usable buffer of 512 bytes and supports up to 16 one-way or 8 two-way transport endpoints.

    Endpoint 0 is must control endpoint, is used to transfer between PC and microprocessor configuration, command and status information, the initial configuration of microprocessor enumeration process is accomplished by the endpoint 0, change the state of the other endpoint control command is performed by the endpoint 0, control transmission occurs only in the control endpoint. The remaining endpoints can be configured as bulk, synchronous, and interrupt transports. Among them, batch transmission is usually used in situations with large amount of data and low real-time requirement on data, such as USB printer, large capacity memory, etc. Synchronous transmission is suitable for occasions with large data volume and high real-time requirement, such as audio and video equipment; Interrupt transmission is a kind of transmission to ensure the query frequency, interrupt transmission is usually used in a small amount of data, but more strict time requirements of the device, such as the mouse, keyboard, etc. According to the actual needs, the USB module based on STM32F103 processor and PC adopt batch transmission mode.

    2. Hardware circuit design

    STM32F103 microprocessor is embedded with a compatible full speed USB2.0 controller, following the full speed USB device (12Mb/s) standard, with standby/wake function. The usb-specific 48MHz clock is directly generated by the internal main phase locked loop (PLL). The connection between microprocessor and PC is via USB cable. To reduce the design size, MiNiUSB connector is adopted at the microprocessor end

    MiniUSB connector has a total of 6 pins, among which DM/DP is the differential data transmission line, which is connected to the pin corresponding to the USB module of STM32. Considering electrostatic protection, an anti-static pipe is added to the pair of differential lines. VBUS pin is the USB power pin. The ID pin is the identification signal of USB master and slave devices. When the OTG device detects the ID pin for grounding, it means the host by default, and the device that detects the ID pin floating is considered as the slave device. The microprocessor is connected to the PC. The PC is the main device and the microprocessor is the slave device. The PC detects that the ID pin should be grounded through the USB data cable. SHIELD for connector housing, plate inside the digital ground.

    System software design

    USB is a master-slave topology structure, only the host can initiate data transmission, the slave (USB device) can only passively listen to the data. Between STM32F103 microprocessor and PC, PC as the host, STM32F103 microprocessor can only be used as a slave. In order to realize the USB data communication between the microprocessor and the PC, the system software design mainly includes two parts: the USB firmware program of the microprocessor and the device driver of the PC. Only by cooperating with each other can the reliable and high-speed data transmission be realized. The development of USB firmware program on microprocessor is the core of the whole software design.

    3.1 USB firmware library for STM32F103

    USB protocol itself is complex, debugging is difficult, ST company provides a USB firmware driver for STM32F103 microprocessor, the purpose is to simplify the development process of USB device side program, shorten the development cycle, at the same time, because it is a standard firmware library, easy to transplant.

    USB firmware library includes: kernel layer and application interface layer. The kernel layer mainly manages the underlying operation of the USB controller, USB hardware configuration, and the analysis and implementation of the USB protocol. Application interface layer is to provide direct interface functions for developers, so in the actual development process, the application interface layer file is mainly modified.

    3.2 application interface layer program development based on batch transmission

    USB bulk transmission between STM32F103 microprocessor and PC consists of two processes: device enumeration process and data transmission process. The application interface layer program development flow chart is shown in figure 3. The enumeration process is above the dotted line and the data transmission process is below the dotted line.

    Equipment enumeration process is the focus of the whole process.

    Through enumeration, the PC obtains various descriptors of the USB device. Through the descriptors, the PC can load the appropriate driver, so as to realize the data communication between the PC and the USB device. Therefore, the modification of the descriptor is the key to the success of the enumeration process. The definitions of the various descriptors are contained in the usb_desc.c file.

    First, make a change to the device descriptor. The standard device descriptor is an 18-byte data structure that specifies the vendor ID as 0x0488 in the device descriptor, the product ID as 0x5750, and the USB device as having only one configuration.

    Next, make changes to the configuration descriptor collection. The set of configuration descriptors includes configuration descriptors, interface descriptors, endpoint descriptors, and class-specific descriptors (if any). There are no class-specific descriptors for USB devices in this experiment. The standard configuration descriptor is a 9-byte data structure, in which the number of interfaces is specified as 1. Since the number of specified interfaces is 1, there is only one interface descriptor in which the number of endpoints used by the interface is specified to be 2 (excluding endpoint 0). Since the number of endpoints is specified as 2, there are two endpoint descriptors, in which endpoint 1 is specified as the input endpoint, endpoint 2 as the output endpoint, and both endpoints are batch endpoints, with a maximum endpoint buffer of 64 bytes.

    Finally, the string descriptor is changed. String descriptors are optional for USB devices. The string descriptor specifies the name that the USB device will display after successful enumeration on the PC. The USB device name is defined here as "STM32 Custm HID".

    The data transmission process includes bulk input and bulk output.

    For bulk input, PC first sends the token IN package, token IN the bag contains a device address and input port, USB devices that need to be ready to local data, and then call the USB firmware library provides data sending function can be prepared by entering the endpoint sent to PC, PC to receive data back an ACK, bulk input end of the process.

    For bulk output, PC first sends OUT the token packets, the token package also contains the output device address and port number, followed by a PC to send data, USB device called the USB firmware library provides data reception function with output endpoint receives the data and stored in a local, data reception is complete an ACK answering machine, bulk output end of the process.

    The sending of the token package and the reply of the status package are done automatically by the USB protocol. The developer only needs to prepare the receiving buffer or the data to be sent after further initialization of the endpoint. Therefore, the endpoint buffer addresses for endpoints 0, 1, and 2 need to be defined in the usb_conf.h file. USB is initialized in the usb_prop.c file, and endpoint 0 is initialized to control. Initialize endpoint 1 to batch input endpoint; Initialize endpoint 2 to the batch output endpoint.

    after the completion of the modification of the above files, with the Keil development tool for firmware library compilation, you can complete the development of STM32F103 device USB firmware program.

    3.3 PC USB device driver development

    PC USB device driver development using libusb-win32. Libusb-win32 is a USB device driver for Windows operating systems. This driver allows users to access any USB device on the Windows system without writing a single line of core driver code, thus greatly simplifying the difficulty of developing USB device drivers on the PC side and shortening the development cycle.

    3.4 The instance

    Through the development of the application interface layer and the PC side USB driver, STM32F103 microprocessor is inserted into the PC USB interface through the USB cable, STM32F103 module can be found in the PC device manager.

    By writing a reading and writing test program on the PC side, the USB batch transmission rate based on STM32F103 microprocessor platform can reach 6Mbps, which meets the actual use requirements and has been applied in the actual project.

    4 conclusion

    In this paper, the USB interface design based on STM32F103, the development of USB firmware library program and the development of host USB device driver are described in detail, and the USB data communication between STM32F103 and PC is realized. This paper adopts hierarchical development of STM32F103 firmware library, which is conducive to program transplantation. In the PC using libusb-win32 driver, greatly reduce the difficulty of development. The method of firmware library development introduced in this paper is also applicable to the development of other USB devices.