USB to RS232 communication interface principle

Publisher:中原读书客Latest update time:2013-12-04 Keywords:USB Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I. Introduction
USB, as a new PC interconnection protocol, makes the connection of peripherals to computers more efficient and convenient. This interface is suitable for a variety of devices. It is not only fast, plug-and-play, and supports hot plugging, but also can connect up to 127 devices at the same time, solving problems such as resource conflicts, interrupt requests (IRQs) and direct data channels (DMAs). Therefore, more and more developers want to use this standard interface in their products. RS-232 is a common access method for a single device to access a computer. Its hardware implementation is simple, so many traditional devices use this communication method.

2. USB System Overview
The USB specification describes the bus characteristics, protocol definition, programming interface, and other characteristics required when designing and building a system. USB is a master-slave bus. When working, the USB host is in master mode and the device is in slave mode. The only system resources required by the USB system are the memory space used by the USB system software, the memory address space (I/O address space) used by the USB host controller, and the interrupt request (IRQ) line. USB devices can be functional such as displays, mice, or hubs. They can be implemented as low-speed or high-speed devices. The maximum rate of low-speed devices is limited to 1.5 Mb/s. Each device has some proprietary registers, that is, endpoints. When exchanging data, it can be accessed indirectly through the device driver. Each endpoint supports several special transmission types and has a unique address and transmission direction. The difference is that endpoint 0 is only used for control transmission, and its transmission can be bidirectional. After the system is powered on, the USB host is responsible for detecting the connection and removal of devices, initializing the device enumeration process, and automatically reconfiguring the system after installing the device driver according to the device description table, and collecting the status information of each device. The device description table identifies the device's attributes, characteristics and describes the device's communication requirements. The USB host configures the device, finds the driver, and communicates with the device based on this information. Typical USB data transmission is initiated by the device driver. When it needs to communicate with the device, the device driver provides a memory buffer to store the data received or about to be sent by the device. The USB driver provides an interface between the USB device driver and the USB host controller, and converts the transfer request into a USB transaction. The conversion needs to be consistent with the bandwidth requirements and protocol structure. Some transmissions consist of large blocks of data. In this case, it is necessary to divide it into several transactions before transmitting. Devices with similar functions can be grouped into a class, which makes it easier to share common features and use common device drivers. Each class can define its own descriptors, such as HID class descriptors and Report descriptors. The HID class is composed of devices that control computer systems. It defines a structure that describes HID devices and indicates the communication requirements of the device. The HID device descriptor must support endpoint input interrupts, and the firmware must also include a report descriptor to indicate the format of received and sent data. After the RS-232 to USB interface conversion module is introduced into the IC card access control and attendance system, it should be a HID device from the characteristics of the system. Therefore, two special HID class requests must be supported: SetReport and GetReport. These requests enable the device to receive and send general device information to the host. When there is no interrupt output terminal, SetReport is the only way for the host to send data to the HID device.
  3. System requirements
In order to realize the RS-232-USB interface conversion in the IC card access control and attendance system, a host that supports USB is required, and a driver for communicating with peripherals on the host must also be provided, which is generally provided by the operating system. In addition, a client application that is executed on the host needs to be developed. On the device side, a host controller chip with a USB interface needs to be provided, as well as the USB communication code executed on the host controller and the related code for executing peripheral functions.
  1. Host requirements
  The host must be able to receive USB data through the device driver and make this data valid for the application that handles these requests. There must be a driver in the host responsible for handling USB transmission, identifying devices, and sending and receiving data to USB devices; at the same time, there must be a device driver - a virtualized serial port that emulates a real serial port. This driver must be able to receive and send USB data like a real serial port.
  From the application's point of view, the device driver must be able to send and receive data, which can be achieved by using a virtualized serial port or by converting to USB data. Microsoft provides a device driver called USB POS, which allows applications to access USB devices as if they were connected to a standard serial port. The general structure of the system is shown in Figure 1.

2. Equipment requirements
  When defining the microcontroller to be used, some communication requirements must be stated, such as: communication rate, frequency, amount of data transmitted, etc. Considering the effective communication rate of the IC card access control and attendance system, the converter can be used as a low-speed device. The communication speed of the low-speed device can vary in the range of 10~100 Kb/s. Considering the amount of data transmitted and the frequency of transmission, the interrupt transmission type is used in this system. Interrupt transmission can be carried out in two directions, but not at the same time. This type of transmission requires a considerable amount of data to be transmitted within a specified time.
  For the conversion module, it can be used for data transmission and reception of the PC. The operating system provides HID drivers, allowing the use of interrupt transmission mode. For a transaction of a low-speed device, the maximum packet capacity of the interrupt transmission is 8 bytes. If a large amount of data needs to be sent. It must be divided into many transactions.
  Another feature to be defined by the conversion module is the number of required endpoints. As mentioned above, the endpoint is the buffer used by the microcontroller to send and receive data during USB communication. In this system, the converter defines two endpoints: one endpoint (endpoint 0) is used to control the transmission, and the other endpoint is the interrupt input endpoint, which is defined as sending data to the PC.
  According to the above requirements, by studying and comparing existing microcontrollers, and considering factors such as memory space, price and development kit, we chose an 8-bit RISC microcontroller CY7C*XX/5XX from the Cypress family. It uses the Harvard bus structure and is a low-cost solution for low-speed application devices with higher I/O requirements.

Figure 2 is the hardware block diagram of the USB communication implementation of the IC card access control and attendance system.
  4. Software Design and Execution
  The system software consists of 6 parts: definition of descriptors, device detection and enumeration, endpoint interrupt service program, USB data exchange module, serial port data exchange module, and USB/Serial module interface. The following briefly describes the functions and implementation ideas of some of the module programs.
  1. Descriptor Definition
  The descriptor is a formatted block of data results or information, which allows the host to know the device. Each descriptor contains the overall information of the device or the information of a certain element. All USB peripherals must respond to requests for standard USB descriptors.
  The system uses 1 interface and 2 terminals (control and interrupt input). Due to the limitation of Win98, the interrupt output terminal cannot be used. Therefore, in order to solve this problem, we use SetReport in endpoint 0 to transmit the data that the PC wants to send to the IC card access control and attendance device.
  Data reception is completed in Output Reports. According to the maximum amount of data sent to the IC card access control and attendance device, the system is defined as 16K 8-bit fields. Sending data to the host is completed in the input report, which is 8K 8-bit fields.

 2. Device Detection and Enumeration
  When a USB Human Interface Class (HID) device is connected to the bus for the first time, it will be powered by the bus but still non-functional waiting for a bus reset. The pull-up resistor on the D-terminal notifies the Hub that a new device is connected, and the host also knows the newly connected USB device and resets it. Immediately after the input packet, the host sends a configuration packet to read the device descriptor from the default address 0. After reading the descriptor, the host will assign a new address to the device and continue to query information about the device description, configuration description, and human-machine report description. The device will begin to respond to the newly assigned address. Based on the information returned from the device. The host knows the number of data terminals supported by the device and completes the enumeration process. After the enumeration is completed, Windows will add the new device to the device manager in the control panel for display. To this end
  , the code to access the descriptor must be written in the microcontroller, so that it is easy to effectively identify and respond to the request sent by the host when enumerating the device. In terms of the device, an INF file needs to be created so that Windows can identify the device and find its driver for the device. Since the operating system provides a simple INF file, the development process only requires writing a program written into the microcontroller.
  3. Data sending and receiving process
  Sending data to the access control and attendance system is completed by using SetReport in the control endpoint 0. The host first requests the access control and attendance system to send data, and the host starts to execute after the device responds to the request. When data arrives at the terminal 0 of the device, an interrupt will be generated for the device. At this time, the corresponding interrupt service program will copy the data to the data buffer. Once the interrupt service program of endpoint 0 is entered, all interrupts must be turned off to ensure that the data can be copied correctly. The
  data buffer of the microprocessor is programmed to receive 64 bytes. This value is stored in the packet header request information of the setup packet. The maximum packet size received from the host is determined by the maximum amount of data it will send to the access control and attendance system.
  The system also uses the Put_command thread to send data to the serial port of the access control and attendance system through 1 I/O port pin. When executing this thread, the start bit, stop bit and corresponding delay are inserted according to the serial communication protocol. The process
  of receiving data from the access control and attendance system is completed using endpoint 1. Endpoint 1 is configured as an interrupt input endpoint. When a start bit arrives at the pin, the GPIO interrupt must be turned on and all other types of interrupts must be turned off. In the design, a Get_Serial thread is used to collect the serial data sent by the I/O pin and store it in the data buffer. At the same time, the thread is responsible for checking the correctness of the received start and stop bits. When 8 bytes are received, the data in the receive buffer is copied to the buffer of terminal 1, and the microprocessor is allowed to respond to the interrupt input request.
  Considering that the effective baud rate of the general serial port ranges from 300 to 19,200 bps, we consider the maximum baud rate of 19,200 bps. It takes nearly 0.75ms to transmit a character; and an input interrupt sends an 8-byte data packet approximately every 10 ms, so designing a 128-byte fast data buffer can ensure that no data is lost. The
  RS-232-USB interface conversion module is used to improve our IC card access control and attendance system, and the use effect is good.

Keywords:USB Reference address:USB to RS232 communication interface principle

Previous article:Design of SD card interface based on embedded system
Next article:Dedicated to low-cost applications, DSC assists sensorless BLDC control

Latest Analog Electronics 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号