USB virtual serial port

Source: Internet
Author: User
In modern embedded systems, asynchronous serial communication interfaces are usually used as standard peripherals in single-chip microcomputer and embedded systems. However, as the general peripheral devices of personal computers use less and less serial ports, serial ports are gradually disappearing from personal computers, especially portable computers. As a result, embedded developers often find that their new computers do not have serial ports, or there is an embarrassing situation where the computers that Debug on-site users do not have serial ports. On the contrary, personal computers generally have more than four USB interfaces. Can I use USB interfaces instead of serial ports to complete the communication between PCs and embedded systems?

  1. feasibility of replacing physical serial ports with USB virtual serial ports

First, more and more devices with USB interfaces are emerging, such as single-chip microcomputer with USB interfaces or independent USB interface devices, the cost of these devices is very close to the cost of using the RS232 level conversion chip.

Secondly, there are also some chips on the market that convert USB interfaces to serial ports. One of these chips is a serial port and the other is a USB interface, and the conversion from the serial port to the USB protocol is completed within the chip. After the chip is connected to a PC through a USB port, it is displayed as a serial port device in the operating system, which means that the USB interface is applicable to the traditional hyperterninal) it is transparent to applications based on serial ports, and developers do not need to change PC-side debugging and applications at all.

However, the USB category of these devices does not belong to the standard USB category. Therefore, you need to install additional device drivers on Windows and Linux operating systems. In addition, because it is not the device driver of the operating system, and the communication goes through the serial port to the serial port, the USB from the device to the USB host is converted multiple times, when debugging encounters problems, it is often difficult to determine whether the serial port is faulty or the USB is faulty. Therefore, the embedded system should be directly connected to the PC through the USB Bus Interface (through the on-Chip USB interface or the On-Chip USB interface chip), and the MCU can directly complete the protocol conversion of the USB virtual serial port.

In the USB standard subclass, there is a type of protocol called CDC that can implement virtual serial port communication, and most operating systems (Windows and Linux) the device drivers that support the CDC class can automatically identify the devices of the CDC class. This not only removes the burden of writing dedicated device drivers, but also simplifies the installation of device drivers.

  2. What is CDC?

Usb cdc class is short for USB communication device class. The CDC category is a type of USB subclass defined by the USB organization for various communication devices (telecom communication devices and medium-speed network communication devices. According to the communication devices of the CDC category, the CDC category is divided into the following different models: USB traditional pure telephone service (POTS) model, usb isdn model and USB network model. Among them, the traditional USB pure phone business model can be divided into direct line control model and abstract control model) and USB telephone model, as shown in figure 1. The virtual serial port discussed in this article belongs to the abstract control model under the USB traditional pure telephone business model.

 

Generally, a CDC class consists of two subcategories: Communication Interface Class and data interface class ). The author manages and controls the device through the communication interface class, and transmits data through the data interface class. These two interface subclasses occupy different numbers and types of endpoints, as shown in figure 2.

 

For the different CDC models described above, the terminal point requirements for the corresponding interfaces are also different. For example, the abstract control model needs to be discussed for terminal points. The communication interface class requires a control endpoint and an optional interrupt terminal point, the data interface subclass requires a periodic (isochronous) terminal point whose direction is input (in) and a periodic terminal point whose direction is output (out. The control terminal points are mainly used for enumeration of USB devices and communication between the baud rate and data type (Data bit, stop bit, and start bit) of virtual serial ports. The non-synchronous terminal point in the output direction is used for the host to send data to the slave device (slave), which is equivalent to the txd line in the traditional physical serial port (from the perspective of the microcontroller ), the non-synchronous terminal point in the input direction is used to send data from the device to the host, which is equivalent to the rxd line in the traditional physical serial port.

  3. Brief introduction to the standard

A single-chip microcomputer-based embedded system is usually used to implement USB bus communication through a dedicated USB bus interface chip (such as Philips D12 ). However, this solution not only increases the cost, but also increases the PCB area, so the Atmel Company integrated the full speed (full speed) from the interface peripherals 51 single chip microcomputer.

AT89C5131 is a 52-core microcontroller. In terms of memory, it integrates 32 KB flash memory for code storage, and 1 kb EEPROM Memory for user data storage, you can use the on-chip bootloader or flash API to program flash memory and EEPROM Memory by using the USB interface or other interfaces (such as uart and I2C bus) by ISP or IAP. In addition, the system integrates 10-bit ADC, I2C bus interface, PCA module, and other peripherals.

 

As shown in figure 3 of the USB2.0 full-speed slave interface structure of AT89C5131, it includes usb d +/D-Interface Buffer, Digital Phase Lock Loop, Serial Interface Engine (SIE) and universal function interface (UFI ). The Digital Phase-lock loop uses the MCU clock as the input to generate the 48 MHz clock required for other parts of the USB interface. The serial interface engine completes the encoding and decoding of the nrzi code in the USB communication physical layer, CRC generation, verification, and error correction. The universal function interface contains a dual-port data storage, with one end linked to the serial interface engine, and the other end connected to the single-chip microcomputer through the data bus, the microcontroller can use special function registers to control and communicate the USB2.0 slave interface.

The-full-speed sub-interface of AT89C5131 contains seven terminal points, wherein the 0 terminal point is configured as the default control terminal point. Other 1 ~ Terminal 6 can be configured as control, bulk, interrupt, and isochronous through special registers. Each terminal point is controlled, identified, and accessed by a group of independent registers, if these registers are directly mapped to the special function Register address space of 51 single chip microcomputer, it is obviously unable to accommodate. Therefore, the seven registers of these seven terminal points actually use the address of the same set of registers in the address space of the MCU, and use a special function register (uepnum) to select the register group for which the current Register is selected, which greatly saves the occupied address space and makes it possible to integrate other special peripherals.

  4. Implementation of the CDC Class Based on AT89C5131

A btype USB socket is used for the hardware connection of the USB interface. According to the USB specification, the USB socket of the btype is used for the device, and the USB socket of the Atype is used for the main device, connect the D + and D-feet of the btype USB socket with the D + and D-Feet on the 89c5131 respectively. Then, use a 1.5 kb pull-up resistor to connect the power supply and D +, because according to the USB specification, the USB master device determines whether the slave device is a full-speed device or a low-speed device by the absolute level on the D + and D-when the slave device is inserted, while the AT89C51 5131 is a full-speed device, so we need to pull D + up.

The following describes the design and implementation of the virtual serial port microcontroller software. First, let's take a look at the distribution of terminal points. According to the abstract Control Model of the CDC class, the single-chip microcomputer terminal 0 and Terminal 1 are allocated to the sub-class communication interface, as the control terminal point (completing enumeration and serial port parameter settings) and the interrupt terminal point respectively, the terminal points 2 and 3 are allocated to the data interface subclass, as the in and out terminal points, the data of the virtual serial port is mainly transmitted from these two terminal points.

 

Because the behavior of each terminal point is relatively independent, the control process of each terminal point is similar. Here we take Terminal 2 as the in terminal point of the data interface as an example, describes how the software operates and controls terminal points, as shown in figure 4. Terminal 2 is an in terminal. Its main task is to simulate the txd line of the physical serial port and send data to the master device. When the master device sends an in request, if the FIFO is not empty, it sends the FIFO content to the master device. If the FIFO is empty, it sends an empty packet to the master device as a response. When receiving an in request, the USB interrupt (if enabled) is triggered. In the Interrupt Processing Program, as shown in figure 4, the terminal of the interrupt source is determined first, if it is Terminal 2, map the USB register group to the group at Terminal 2, and then fill in the serial data to be sent into the FIFO register (uepdatx ), the txrdy bit of uepstax indicates that the data in the FIFO is ready. Then, the USB interface automatically responds to the in request and sends the data in the FIFO, and the program can exit the interrupted service program. For other terminal points, the processing process is similar.

The software uses Keil C51 as the compilation system. To facilitate integration with other programs of the system, the API interfaces usb_getc () and usb_putc () of the standard Community device are used to make the program highly portable. The Application Layer functions (usb_getc () and usb_putc () exchange data with the USB interrupt handler through two FIFO cyclic queues (TX and RX, this effectively serves as a buffer for sending and receiving to prevent Buffer Overflow.

  5. Summary

The realization of the CDC-based USB virtual serial port on the single chip microcomputer is well adapted to the development of the current computer peripheral interface, because such an interface is still mapped to a serial port in the PC operating system, therefore, a large number of PC-side debugging programs and application re-writing are avoided.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.