“In order to solve the communication problem between 51 single-chip microcomputer and computer serial bus (USB), based on the PDIUSB12 chip, the STC89C52 in 51 single-chip microcomputer was selected as an example, and a USB interface circuit was designed to solve the problem of multiple 51 single-chip microcomputers and computers. USB communication is difficult.
Introduction: In order to solve the communication problem between 51 single-chip microcomputer and computer serial bus (USB), based on the PDIUSB12 chip, the STC89C52 of 51 single-chip microcomputer is selected as an example, and a USB interface circuit is designed to solve the problem of multiple 51 single-chip microcomputers and The computer’s USB communication is difficult.
USB is widely favored by users and computer manufacturers because of its convenient use, fast transmission speed, and flexible connection. Microcontrollers (MCUs) mostly rely on USB to achieve communication with computers. Among MCUs, 51 single-chip microcomputers are one of the most widely used single-chip microcomputers in China, but because many 51 single-chip microcomputers cannot directly realize USB communication with computers, it brings inconvenience to many users’ development and use. This text uses the typical representative STC89C52 of 51 single-chip microcomputers and PDIUSB12 of NXP Semiconductors to design the USB interface circuit based on it, which solves the problem of USB communication between MCU and computer. This solution has the advantages of low price, simple use, no need to write complicated USB driver, plug and play, etc.
1 Circuit hardware design
1.1 Introduction to the chip
This system is based on the MCU STC89C52 and PDIUSB12 interface chip. STC89C52 is a low-power, high-performance 8-bit CMOS microprocessor produced by STC. It has a classic MCS-51 core. It mainly has the following features: flexible 8 KB Flash program memory and 512 B RAM data memory, It also contains 32-bit I/O ports and built-in 4 KB EEPROM, 3 16-bit timers/counters, 4 external interrupts, a 7-vector 4-level interrupt structure, full-duplex serial port; in power-down protection mode, The RAM content is saved, the oscillator is frozen, and all the work of the single-chip microcomputer is stopped until the next interrupt or hardware reset; the maximum operating frequency is 35 MHz, which is a commonly used MCU with higher cost performance.
The PDIUSB12 used by the USB interface chip is a high-performance USB interface device that integrates SIE, FIFO memory, transceiver and Voltage regulator.It supports local DMA transfer, can realize 2 Mb/s high-speed parallel interface with external microprocessor, integrates 320 B multi-configuration FIFO memory, and has 1 Mb/s data transfer rate in block transfer mode. Connect to USB through software, and realize batch and synchronous transmission in multi-interrupt mode. You can connect PDIUSB12 and MCU with the computer. The structure diagram connected with the computer in this design is shown in Figure 1.
1.2 Hardware circuit
The USB interface circuit diagram composed of STC89C52 and PDIUSB12 is shown in Figure 2. The 8-bit parallel data of PDIUSB12 are respectively connected to the P0 port of STC89C52; ALE represents the address latch function, connected to GND, and the address information latch is turned off; CS_N represents chip select Function, connected to GND, the chip is always in working state; SUSPEND connected to GND, the suspend function is invalid; interrupt request pin INT_N is connected to the external interrupt INT0 of the MCU, valid at low level; RD_N and WR_N are control signals, respectively connected to the microcontroller Port P3.3 is connected to port P3.4; address bit A0 is connected to port P3.5 of the single-chip microcomputer. When A0 is high, it means the single-chip microcomputer sends a command instruction, when A0 is low, it means the single-chip microcomputer sends a data command; DMA request pin DMREQ is floating; when the DMA response pin DMACK_N is active at low level, it is connected to VCC without DMA response; the programmable clock output DMACK_N is floating.
There are two power supply modes for USB devices: bus-powered and self-powered. In order to facilitate use and reduce the complexity of peripheral circuits, this article chooses the bus-powered mode. The USB interface has 4 pins, which are 5 V VCC port, GND, data port D+ and D-. The LED light D2 is connected to the VCC port and GND of the USB. When the system is connected to the USB port, the D2 light is on. The data ports D+ and D- are used for communication, and the potential state of D+ and D- can also be used to judge whether the system is recognized by the computer. When the potential difference is close to GND, it means that the connection between the system and the computer is unsuccessful; when the potential difference is close to VCC, it means that the system is ready to communicate with the computer. It can also be judged by the LED light D1 on the GL_N pin: when D1 flashes intermittently, it means that the system is identifying with the computer; when D1 is always on, it means that the identification has been successfully completed and communication is possible.
2 System software design
For the USB system of 51 single-chip microcomputer and PDIUSB12, its core part is the program design, which mainly includes the driver software part, the firmware program part and the application software part. In order to simplify the design, the driver software uses the driver software that comes with Windows and uses the HID system interface protocol, eliminating the need for driver software development. Therefore, firmware programs and application software programs are key parts of the system.
2.1 USB device and computer communication process
PDIUSB12 has the function of PSIE (Philips Serial Interface Engine). PSIE implements all USB protocol layers and is completely implemented by hardware without the participation of software. The functions of this module include synchronization mode recognition, parallel/serial conversion, bit filling/unfilling, CRC check/generation, PID check/generation, address recognition and handshake evaluation/generation.To realize the communication between the computer and the USB device, the enumeration process needs to be carried out when the USB device is connected to the computer, That is, let the computer recognize and set the USB device, and the communication between the device and the host is carried out through the control transmission mode. The process is mainly as follows:
(1) When the USB device is connected to the computer, the USB device is in the power-on state, and the microcontroller and PDIUSB12 are powered on. There are 1.5 kΩ redundant pull-up resistors integrated on D+ and D- inside the PDIUSB12 chip. By default, this pull-up resistor is not connected to VCC. After power-on, if D+ is pulled to a high level, according to the data changes monitored by the hub, it is recognized that a high-speed device is connected; if the pull-up resistor pulls D- to a high level, it is recognized that a low-speed device is connected. This system sets a high-speed USB device. After the MCU completes the initialization operation, set D+ to a high level.
(2) When the computer detects that the USB device has been connected and determines whether it is a high-speed device or a low-speed device, the computer will send a Set_Port_Feature request to the hub to manage the port USB device, and set D+ and D- to a low state (ie reset Status), return to the communication status after 10 ms. After the computer finishes sending Set_Port_Feature, it will continuously send Get_Port_Status requests to the hub to inquire whether the USB device has completed the reset function. When the hub is reset, the USB device is in an idle state, waiting for the computer to send a new command. The default address for USB device and computer transmission is 0 and endpoint 0.
(3) The computer assigns a unique address to the USB device by sending a Set_Address request. After that, all communications between the computer and the USB device are realized through this address.
(4) After the address is allocated, the computer obtains the device descriptor from the new address. One or more configuration descriptors are specified in the device descriptor, and the Get_Descriptor() command is sent multiple times to read these configuration descriptors. The device descriptor includes the maximum packet length of endpoint 0, device support configuration and number, device type, PID, VID, etc. After the computer receives it, it sends a 0 B data packet as a response.
(5) The computer configures the device after reading the device descriptor. At this time, the computer port will pop up a window to Display the information, manufacturer, product description, model, etc. of the new device found.
(6) After the computer gets enough information from the descriptor, it starts to install the driver for the device.After loading the driver, the computer sends a Set_Configration() command request to select a suitable configuration for the device, and the USB enumeration process ends.. At this point, if the computer sends a certain data command to the USB, the data is transferred to the PDIUSB12 through the USB port, and then sent to the MCU for processing through the PDIUSB12. After the processing is completed, the result is returned to the host computer software through the USB interface for Display. So far, the most basic communication between the USB device and the computer is realized.
2.2 Firmware programming
2.2.1 Firmware structure
The firmware structure and data flow is shown in Figure 3.In the hardware abstraction layer, the system must be set up and identified at the bottom, which is mainly the process of identifying and loading the I/O ports of PDIUSB12 and MCU; the PDIUSB12 command interface contains several subprogram commands, which can be directly used when needed. Call; the main function of the interrupt service program (ISR) is when PDIUSB12 sends an interrupt request, it receives the transmitted data and sets the flag to pass to the main loop program; standard equipment processing means processing the USB standard equipment request; manufacturer request processing Indicates that the manufacturer’s request added by the user is processed; The main loop is mainly to process user-defined functions such as user buttons, I/O ports, and receive data processing functions of each module.
2.2.2 PDIUSB12 interface commands
This module contains all the subroutines of PDIUSB12, which can be called directly when needed. Here are some of the main functions:
INT16U D12_ReadChipID(void)//Read chip ID
void D12_SetEndpointEnable (INT8U bEnable)
//Set endpoint enable
void D12_SetMode (INT8U bConfig, INT8U bClkDiv)
void D12_SetDMA (INT8U bMode)//DMA working mode setting
//Read interrupt register value
INT8U D12_SelectEndpoint (INT8U bEndp)//Endpoint selection
INT8U D12_ReadEndpointStatus (INT8U bEndp)
//Read endpoint status
INT8U D12_WriteEndpoint (INT8U endp, INT8U len,
void D12_AcknowledgeEndpoint (INT8U endp) // promise to setup
Most of the functions under this module start with D12. Its detailed functions will not be introduced one by one, please refer to the PDIUSB12 data manual.
2.3 Application software and programming
The application software is to process the data sent from the computer by the single-chip microcomputer and then return it to the computer, and display the sent and received data through the upper computer.
Taking the +1 operation of the number sent to the MCU by the computer as an example, the application program to realize this function is as follows:
D12_WriteEndpoint (5, EP2_PACKET_SIZE, EpBuf);
This design chooses the single-chip multi-function debugging assistant as the host computer software. This is a collection of serial/USB/network debugging, hexadecimal conversion, font and digital tube font code production, common check value calculation, UNICODE code conversion, and bitmap A comprehensive debugging software that outputs C files and many other functions. The experimental data and experimental results are displayed, and the implementation results are shown in Figure 4.
This paper studies the USB interface design based on STC 51 single-chip microcomputer and PDIUSB12, and puts forward an overall research plan based on STC89C52 as an example. The program adopts the HID system interface protocol, and it is found through experiments that it has stable performance, convenient use, and high cost performance. It can be used as a communication interface between the STC 51 single-chip microcomputer and a computer. At the same time, it can also provide solutions for the research of other USB interface circuits.