Microcontroller-based
USB
Interface Design
The Design of USB interface based on the single chipLiu jingquan, Wang xiandai (School of Communication and control engineering, Jiangnan University, Wuxi 214122, China)
SummaryA usb interface extension method based on single chip microcomputer is proposed. The structure and performance of the USB interface chip SL811HS [1] and the hardware circuit diagram of the USB interface are introduced, detailed analysis of the USB interface driver design method and fat16 [2] file system structure. Using the SCSI [3] transmission command set, the data communication between the host and the USB flash drive device is realized through the bulk-in and bulk-out [3] endpoints. Experiments and application results show that this scheme has the advantages of convenient control, fast transmission speed, and stable and reliable storage data.
KeywordsUSB endpoint enumeration Configuration File System
AbstractA Method of Intelligent Instrument expandation USB interface based on Singlechip is stated. the structure and capability of the USB interface chip SL811HS, hardware circuit dimo-of the USB interface is introduced, and the design method of the USB interface drivers, structure of the fat16 file systems is analyzed detailedly. The communication between the host computer and U disk device is completed via bulk-in and bulk-out endpoint using SCSI command class. the results of experiment and application show that the scheme features convenient control, fast transmitting data and reliably storaging data.
Key WordsUSB endpoint enumerate File System
IntroductionUSB (Universal Serial Bus) is a fast and flexible bus interface developed in recent years. It is easy to use, hot swappable, flexible interface connection, and can provide peripheral power [4], which is widely used in embedded systems and intelligent meters. The 51 series single-chip microcomputer is widely used in measurement and control instruments and automation fields with its superior performance, mature technology and high cost effectiveness. Therefore, 51 series single-chip microcomputer is used to implement USB host interface and control USB peripherals, which plays a significant role in improving the data storage, data transmission, device control, and other performance of the entire system. The solution discussed in this article is based on the application environment of the pressure tester. In the process of developing the pressure tester, it is required to store a large amount of data according to actual needs. The previous solution was the rs232c interface, however, due to the slow transmission speed, data loss may occur in the high-speed sampling system, and the participation of the upper computer is required, which is inconvenient for outdoor operations. The USB interface overcomes the preceding disadvantages. The test instrument can save the collected data to the USB flash disk. The staff can take the USB flash disk at any time and take the data to a remote location for analysis, in addition, I also developed a file system for reading and writing USB flash drives. the stored data can be opened directly on a PC without writing other analysis software. Another advantage of this design is its low cost, which can be applied to various SMART instruments and embedded devices to store massive data.
1
Hardware Design1.1 SL811HS overview SL811HS is a USB controller launched by Cypress with Master/Slave working modes [4]. It complies with the USB1.1 specification and can automatically detect bus speed, supports 12 Mbit/s and 256 Mbit/s devices at full speed. It has an 8-bit bidirectional data bus and is easy to connect to a single-chip microcomputer. The-byte SRAM in the chip (16 bytes for the working register) is used for data transmission; sof and crc5/16 can be automatically generated to simplify software workload. The root hub is available in the chip. The suspended/Wakeup mode is supported to reduce power consumption. The address auto-adding function is supported, in the continuous read/write process, you only need to set the address once, and its internal register address is automatically increased, which is necessary for large data communication. 1.2 single-chip microcomputer and SL811HS Interface Design Figure 1 shows the AT89C51 and SL811HS hardware connection circuit. In this design, because the operating voltage of the selected single-chip microcomputer AT89C51 and its peripheral components is 5 V, and the operating voltage of SL811HS is 3.3 V, therefore, the system should provide 5 V voltage for Voltage conversion at the same time. Although SL811HS can use 12 MHz crystal oscillator, but in the actual use process, if the crystal oscillator quality is not good, the circuit stability will be relatively poor. Therefore, we recommend using a 48 MHz Active crystal oscillator during design. The SL811HS interrupt request outputs a high level, so it needs to be converted to a low level with a reverse converter to meet the requirements of the AT89C51 interrupt input. In addition, note that SL811HS is a low-level reset. To facilitate debugging, the system has extended the LCD display. After the hardware is complete, perform the test. First, write data to the SL811HS register, then read the data and display it on the LCD. If the data is the same as the written data, the SL811HS is correctly connected to the microcontroller. Use an oscilloscope to check whether the source crystal oscillator is vibrating. After all the operations are normal, you can debug the software.
2
Software Design(I. USB enumeration process: the USB Bus generally contains four basic data transmission types: control transmission, interrupt transmission, batch transmission, and synchronous transmission. The system uses control transmission and batch transmission. The basic function is to read and write the SL811HS register. Code 4 is as follows: xdata byte sl811h_addr _ AT _ 0x9800; xdata byte sl811h_data _ AT _ 0x9801; read a single register: byte sl811read (byte A) {sl811h_addr = A; Return (sl811h_data);} write Single Register: void sl811write (byte A, byte d) {sl811h_addr = A; sl811h_data = D ;} continuous read SL811HS register: void sl811bufread (byte ADDR, byte * s, byte c) {byte idata I; I = C; sl811h_addr = ADDR; while (I --) * s ++ = sl811h_data;} continuous write SL811HS register: void sl811bufwrite (Byte ADDR, byte * s, byte c) {byte idata I; I = C; sl811h_addr = ADDR; while (I --) sl811h_data = * s ++ ;} after the read and write registers are normal, write the underlying function 6 of SL811HS. For specific functions, refer to the code modification provided by Cypress. After debugging, the enumeration process is started. Enumeration is a process of correct configuration of the USB interface, including obtaining the device descriptor, configuration descriptor, interface descriptor, endpoint descriptor, and correct configuration of interfaces and endpoints. The setup packet requesting the device descriptor is 80 h 06 00 01 00 00 12 00. by reading the device descriptor, you can obtain the sub-class of the device. The setup packet for the request configuration descriptor is 80 h 06 00 01 00 00 09 00. For the request configuration descriptor, the first request can be performed first, and the packet length must be 9. Receives the data returned by the device, obtains the total length of the descriptor, and then sends a second request to obtain all the descriptor data. The content of the setup packet sent for the second time is 80 h 06 00 01 00 00 2EH 00. At this time, the returned data includes all the device configurations, interfaces, and endpoints. You must configure the SL811HS chip correctly before reading and writing the USB flash drive. Code 6 of the enumeration process is as follows: udev. wpayload [0] = 64; // the payload of address 0 and Endpoint 0 is 64 bytes if (usbaddr = 2) // set the USB address to 2 usbreset (); // chip SL811HS reset pdev = (pdevdesc) bbuf; If (! Getdesc (uaddr, device, bbuf) // get the descriptor return false through address 0; udev. wpayload [0] = pdev-> bmaxpacketsize0; If (! Setaddress (usbaddr) // set the USB address value. Here it is 2 Return false; uaddr = usbaddr; // The transmission uses the new address if (! Getdesc (uaddr, device, 0, (pdev-> blength), bbuf) // get the device descriptor return false with the new address; pcfg = (pcfgdesc) bbuf; If (! Getdesc (uaddr, configuration, bbuf) // get the configuration descriptor return false; If (! Getdesc (uaddr, configuration, 0, wordswap (pcfg-> wlength), bbuf) // The second request configuration descriptor return false; pcfg = (pcfgdesc) bbuf; pifc = (pintfdesc) (bbuf + 9); // detaches the Interface Information udev. bclass = pifc-> iclass; udev. bnumofeps = (pifc-> bendpoints <= max_ep )? Pifc-> bendpoints: max_ep; If (! Setconfiguration (uaddr, device) // sets the USB configuration information return false;
(Ii. USB flash drive read/write and fat16 File System 5: the USB flash drive is a large-capacity storage device. After the USB flash drive is successfully enumerated, the host and USB devices transmit data through bulk-only transmission, all communication data is transmitted through the bulk-in and bulk-out endpoints. In this transmission mode, there are three types of data transmitted between the USB and the device, CBW, CSW and common data 5cbw (command block wrapper, namely the command block package) is the Command sent from the USB host to the device. The command format complies with the command block specified by binterfacesubclass in the interface. This is the SCSI transmission command set. The USB device needs to extract the SCSI command from the CBW and execute the corresponding command. After the command is completed, the CSW (command status wrapper) that reflects the execution status of the current command will be sent to the host ), the host determines whether to send the next CBW or data based on CSW. The host requires that the command executed by the USB device may be sending data. In this case, you need to transmit the specific data and then send the CSW for the host to perform the next step .. Generally, a USB flash drive supports the fat16 file system. It can be divided into four parts: reserved area, fat area, root directory area, and data area. The first sector in the reserved area is also called the Boot Sector, which contains key information for file system identification. The root directory stores directory items. Each directory item contains 32 bytes and records the information of one file or directory. The fat area is the file allocation table. The disk space allocated by the operating system is distributed by cluster. The data of the same file is not necessarily completely stored in a continuous area of the disk, but is often divided into several segments and stored like a chain. This storage method is called the chained storage of files. To achieve file chain storage, the hard disk must accurately record which clusters are already occupied by files, and specify the cluster number of the next cluster for each occupied cluster, for the last cluster of a file, you must specify that the cluster has no successor cluster. These are all saved by the fat table. The corresponding table items in the fat table record the information about the clusters it represents: such as whether it is empty or whether it is a bad cluster, whether it is the end cluster of a file or not. After the last sector occupied by the Directory item, it is the location where the file data is actually stored. Only a clear understanding of the structure of fat16 can correctly read and write the file. Figure 4 shows a flowchart for writing the file. Reading the file is relatively simple and will not be repeated here.
3
ConclusionThe system has successfully read and write USB flash disks. After multiple tests, the read and write data is stable and reliable, and compatible with the vast majority of USB flash disks on the market. In the single-chip microcomputer system, the author can create, delete, and modify files, and store data in multiple file formats. In particular, the binfile and HEX file established in the single chip microcomputer system can be opened directly on the PC using UltraEdit-32 software, which provides a great convenience for data analysis, instead of writing analysis software.
References1 Cypress Semiconductor Corporation. SL811HS Date Sheet [Z]. 2002.2 Xu Yonghe. 8051 Single-Chip Microcomputer USB Interface Program Design. beijing: Beijing University of Aeronautics and Astronautics Press. 2004.3 Shi Bo, Tian kai.com. overview of Universal Serial Bus USB technology [J]. information technology. 2001 (4 ). 4 Ma Zhongmei, Ji shunxin, Zhang Kai, etc. C language application design for Single-Chip Microcomputer. beijing: Beijing University of Aeronautics and Astronautics Press. 2003.5 Microsoft Corporation. FAT file system specification [Z]. 1999.6 Xu Aijun, Peng xiuhua. the design of the application of the advanced language of single chip microcomputer. beijing: Electronics Industry Press. 1995.7 Compaq, Inter, Microsoft, NEC. universal Serial Bus specification (revision1.1 ). 1998.