USB HOST Technology in Car Driving Recorder
[Copy link]
1. Introduction to USB bus USB (Universal Serial Bus) is a universal serial bus and a standard connection interface. When connecting with external USB slave structures and USB HOST, there is no need to reconfigure the system and adjust the interface. The USB HOST automatically identifies these interface devices and configures appropriate drivers, thus achieving plug-and-play and hot-swappability, which is convenient for users. With the development, popularization and maturity of this technology, the characteristics of the USB bus have made it widely used in data acquisition and embedded users. The demand for mobile storage of electronic products is increasing, and the USB bus is often used to achieve large-scale data exchange with its excellent cost-effectiveness and flexibility. USB interface technology is divided into USB SLAVE and USB HOST. Most of the current electronic products with USB functions are SLAVE technology, and USB HOST technology has always been a difficult point in development. USB communication can be represented by Figure 1. In Figure 1, the left half is the USB HOST end, which consists of two parts, namely USB software and USB hardware. In fact, there are three software that make up the USB HOST solution, namely USB client driver, USB driver and USB host controller driver. The transaction processing of the application is initiated by the USB client driver. The client driver treats the USB device as a set of accessible endpoints, which can be controlled and communicate with its functional units. The USB system software includes the USB driver and the USB host controller driver. The USB driver is responsible for configuration management, user management, bus management and transmission management, and the USB host controller driver is responsible for scheduling management, queue management, control management, as well as data bit encoding, packaging, cycle checking, sending and error handling. Figure 1 Hierarchical relationship of USB communication model 2. Overall design of USB HOST related modules The overall design of USB HOS related modules consists of single-chip microcomputer, SL811HST, FLASH, ferroelectric storage, real-time clock, power management and other single-chip microcomputer interface circuits. When the car driving recorder is working, the single-chip microcomputer picks up various information of the car through the front-end interface circuit, including vehicle speed, engine speed, various vehicle switch signals, etc. The car driving recorder uses the real-time clock as a reference to store vehicle information in ferroelectric storage and FLASH storage according to categories. When it is necessary to obtain vehicle information from the car driving recorder, the user inserts a USB flash drive, the single-chip microcomputer automatically identifies the USB flash drive and loads the driver. After completing the device enumeration and Bulk_Only transmission protocol, the single-chip microcomputer can transfer the information in the car driving recorder to the USB flash drive in the form of a file. The USB host interface design includes physical layer implementation, SL811HST bottom-level driver implementation, device enumeration protocol implementation, Bulk_Only transmission protocol implementation and FAT16 file structure implementation. The module function block diagram is shown in Figure 2. Figure 2 System overall design function diagram 3. Hardware design of USB HOST related modules In the USB HOST module of the car driving recorder, the single-chip microcomputer interface chip uses AT89C51RD2, and the USB HOST interface chip uses SL811HST. SL811HST is compatible with the standard USB1.1 protocol, has interrupt and register query mode, and provides parallel bus data protocol. Using the read and write signal lines of the single-chip microcomputer, it is relatively easy to connect the single-chip microcomputer and SL811HST. Through the A0 signal line of SL811HST, the single-chip microcomputer can access the data and address of SL811HST. At the same time, because SL811HST has an automatic address growth mode, it is relatively easy for the software to implement Bulk_Only data transmission. SL811HST can work in HOST mode to realize data exchange between the host of the car driving recorder and the USB flash drive; it can also work in SLAVE mode to realize data exchange with a PC. Because the data structures of software such as the USB device enumeration protocol, Bulk_Only transmission protocol, and FAT16 file structure are relatively complex, the RAM of AT89C51RD2 cannot meet the system requirements, so 32K RAM is expanded to improve system performance. In addition, the electrical characteristics of the USB bus require that a 22Ω~44Ω resistor must be connected in series before the USB transceiver. At the same time, the position of the pull-up resistor and the pull-down resistor must be configured according to the different USB device transmission speeds (full speed or slow speed). 4. The USB host end still has some work to do to enumerate the device from the time the USB device is inserted into the interface to the time the client driver can use the device. This section can be called the device identification process, also known as the enumeration process. The enumeration process is a necessary process before any USB device is used. Before using it, the USB HOST end needs to know whether it is a slow device or a full-speed device, and needs to know some of the characteristics and capabilities of the device in order to load the corresponding driver. The USB enumeration process is implemented as follows: (1) The USB device is inserted into the USB HOST interface. The USB HOST continuously queries the interface at regular intervals to check the potential changes of D+ and D- to understand the connection status of the device. The USB device is a plug-and-play device. When the system is inserted, the D+ and D- signal lines are almost grounded due to the pull-down relationship. If a device is just connected, the pull-up resistor and the pull-down resistor form a voltage divider. At this time, the voltage of one data line is close to VCC, and the other signal line is almost grounded. In this way, it can be confirmed that a device has been connected. When the HUB end of SL811HST detects that a device has been connected, it immediately reports to SL811HST, and SL811HST sends the relevant information to the relevant register for the host to query and generate an interrupt. After the host queries the relevant register or receives the interrupt, it can understand the information of the inserted device through data exchange. (2) Reset the USB device. After detecting a device, the host issues a reset command on the device side. The device can respond to the processing operation of the bus only after receiving a reset signal from the bus. After receiving the reset signal, the device uses the default address (00H) to address it. (3) Address allocation. When the host receives a device response to the default address (00H), it then sends a Set_Address request to assign a single address to the USB device. The device reads the request, returns a confirmation message and saves the new address. (4) Read the USB device descriptor. The USB device descriptor is a description of the properties of the USB device. Standard USB devices have five descriptors, namely device descriptors, configuration descriptors, interface descriptors, endpoint descriptors and string descriptors. USB descriptors are read through Get_Descriptor. (5) Device configuration. The host configures according to the read USB device descriptor. If the USB resources required by the device are met, it sends a configuration command to the USB device to configure the device with the configuration number. (6) Suspend. In order to save power, the device driver will enter the suspended state after the bus remains idle for 3ms. In the suspended state, the device consumes very little current. When suspended, the USB device retains all internal states including its address and configuration information. After completing the above steps, the USB device can be used. During the enumeration process, the device does not necessarily require entering the suspended state. 5. Bulk_Only transmission protocol After the USB HOST configures the USB device, it identifies the Bulk_Only Mass Storage device through the information provided by the descriptor, and then enters the Bulk_Only transmission mode. In this mode, all data between the USB and the device are transmitted through Bulk_In and Bulk_Out, and no data is transmitted through the control endpoint. In this transmission mode, there are three types of data transmitted between the USB and the device, CBW, CSW and ordinary data. CBW (Command Block Wrapper, i.e. command block packet) is a command sent from the USB HOST to the device. The format of the command complies with the command block specified by the bInterfaceSubClass in the interface, which is the SCSI transmission command set. The USB device needs to extract the SCSI command from the CBW, execute the corresponding command, and after completion, send a CSW (Command Status Wrapper) to the HOST to reflect the current command execution status. The HOST decides whether to continue to transmit the next CBW or data based on the CSW. The command that the USB HOST requires the USB device to execute may be to send data. In this case, specific data needs to be transmitted. After the transmission is completed, a CSW is issued to allow the USB HOST to proceed to the next operation. The operations performed by the USB device can be represented by Figure 3.
Figure 3 Bulk Only Device Execution Operation From Figure 3, we can see that the data segment is divided into two parts, one is data out, which means the host sends data to the device; the other is data in, which means the host gets data from the USB device. The following describes how to implement this process in an embedded system based on SL811HST. The host sends CBW, which is the payload data of the transfer descriptor and is transmitted to the device. The CBW packet consists of two parts, the packet header and the command block. The packet header represents the logical unit of the data transmission direction and the length of the command block; the command block is the command to be actually implemented. Depending on the device, the command cluster used by the command block is also different, one is the UFI instruction set, the other is the SCSI instruction set, and here is the SCSI instruction set. In the normal data stage, the data will occupy the entire payload length of the transfer description, and the data to be sent must be loaded into the buffer before sending. When reading in, the data will be automatically placed in the payload data part after the transfer descriptor is executed. The CSW stage reflects the execution of the command, including the CSW flag, the length of data to be transmitted, the correct return flag, etc. 6. FAT16 file structure and USB flash drive reading and writing A FLASH USB flash drive FAT16 file system consists of 4 parts: reserved area, FAT area, root directory area and file and directory data area. The first sector in the reserved area must be BPB (BIOS Parameter Block), which is sometimes called "boot sector", "reserved sector" or "zero sector" because it contains key information for identifying the file system, so it is very important. The FAT area is the File Allocation Table. The operating system disk space is allocated by cluster. Therefore, when a file occupies disk space, the basic unit is not a byte but a cluster. Even if a file has only one byte, the operating system will allocate a minimum unit, namely a cluster. In order to allocate disk space to the corresponding files in an orderly manner, the file can also be read from the corresponding address when reading the file. The entire disk space is divided into 32KB clusters for management. Each cluster occupies a 16-bit position in the FAT table, which is called a table entry. The data of the same file is not necessarily stored completely in a continuous area of the disk, but is often divided into several segments, stored like a chain. In order to realize the chain storage of files, the USB flash drive must accurately record which clusters have been occupied by files, and must also indicate the cluster number of the next cluster of the storage body for each occupied cluster. For the last cluster of the file, it is indicated that there is no successor cluster for this cluster. The structure of the FAT area has a table, please refer to relevant literature. The root directory area, the sector immediately after the second FAT table, is the root directory area. The root directory area stores directory entries. Each directory is 32 bytes and records a file or directory information. File and directory data area. After the last sector occupied by the directory entry is the location where the file data or directory is actually stored. When reading a file, the steps are as follows: Read the root directory Read FAT1 Read the data area. First, you must find the location of the file name. The files and directories under the root directory are all in the root directory area. Each directory or file item occupies 32 bytes. Read a sector each time and compare the data. If there is no such sector, continue to read the next sector until the file is found. In these 32 bytes, the 26th to 27th bytes represent the starting cluster number of the file, and the 28th to 31st bytes represent the size of the file. According to the starting cluster number, search the FAT area, find the file cluster chain, and read the corresponding clusters in sequence to get the corresponding data of the file. Writing a file is a little different from reading a file. The steps are as follows: write FAT1 write FAT2 write root directory area write data area. First read the FAT area, search for available clusters, mark them as the starting cluster number of the file, continue searching, find available cluster numbers, and write the cluster number at the last available cluster mark (0000), and then go down in sequence until the entire file is written. 7. Summary Through self-development, the USB HOST function is realized in the car driving recorder: USB device enumeration, Bulk_Only transmission protocol, FAT16 file structure, etc. It meets the needs of fast exchange of large amounts of data between the car driving recorder and the USB flash drive. During the inspection, certification and use of the product, the technical performance and product indicators of the USB HOST have met the product design requirements and technical requirements, which provides the product with good high-end technical content and facilitates the user's rapid collection of large amounts of data, making the car driving recorder more competitive in the market.
|