Application Design of USB Device Firmware Based on Single-chip Microcomputer

USB is a new interface technology applied in the PC field. It is composed of three parts: a PC system with a USB interface, a system software that can support the USB, and a device that uses the USB interface. It is proposed based on the use of universal connection technology to realize simple and fast connection of peripherals, achieving the purpose of convenience for users, reducing costs, and expanding the range of PC connection peripherals. Its most significant advantage is that it supports hot plugging. When the user inserts a USB peripheral, the computer can automatically identify the connected peripheral without restarting the system.

Authors: Tu Wangming; Wei Youguo; Chen Yuntao; Duan Daoju

I. Introduction

USB is a new interface technology applied in the PC field. It is composed of three parts: a PC system with a USB interface, a system software that can support the USB, and a device that uses the USB interface. It is proposed based on the use of universal connection technology to realize simple and fast connection of peripherals, achieving the purpose of convenience for users, reducing costs, and expanding the range of PC connection peripherals. Its most significant advantage is that it supports hot plugging. When the user inserts a USB peripheral, the computer can automatically identify the connected peripheral without restarting the system.

2. USB device hardware structure

In order to improve the scalability of the system, it is also convenient to choose a high-performance single-chip microcomputer to achieve more functions, we use the more extensive Philip company’s simple interface chip PDIUSBD12. The following describes the relevant situation of the USB device hardware.

1. The internal structure of the PDIUSBD12 chip

The chip integrates SIE, 320B FIFO buffer, transceiver, voltage adjustment circuit and termination resistor, provides a parallel interface at a rate of 2MB/s, and has a fully autonomous local DMA function. The multiple interrupt modes provided by the chip are conducive to batch and isochronous data Transmission mode. The chip also provides programmable clock, power-on reset and low-voltage reset circuits.

2. Control word of PDIUSBD12 chip

The control word of D12 is divided into three basic types: initialization command, data flow command and general command. Use initialization commands in the USB device enumeration process. These commands are used to start the device, set the address, endpoint and work mode. The data flow command is used to manage the data flow between the USB and the microcontroller. Many data streams are started by an interrupt request sent to the microcontroller. The controller uses the data flow command to access the data and confirm whether the data in the FIFO is valid. Common commands are mainly used during data transmission, including restoring and reading the current frame number.

3. Connection between PDIUSBD12 and MCU

Take the connection between PDIUSBD12 and 89C52 single-chip microcomputer as shown in the figure, ALE is connected to the ALE of the single-chip microcomputer, using the address bus multiplexing mode. A0 is connected to VCC through a resistor. VCC is connected to +5V, VOUT3.3 is grounded through the capacitor, and pin 20RESET_N and pin 18DMACK_N are connected to high level. GND is grounded. 8 data lines are connected to the data bus. Pin 11CS_N is connected to the corresponding end of the controller or decoder, and PDIUSBD12 can be mapped to any address of the controller through the CS_N end. Pin 14 interrupts the interrupt line connected to the controller and pulls it high with a resistor. XTAL1 and XTAL2 are connected to 6MHZ crystal oscillator, EOT_N is pulled high, D- and D+ are respectively connected to D- and D+ of the USB bus. In the actual product, pin 21GL_N can be connected to LED to Display the status of the device.

Application Design of USB Device Firmware Based on Single-chip Microcomputer

Third, the design of USB device firmware

The design of the firmware requires very familiarity with the specific communication steps of the USB device and the instruction system of the single-chip microcomputer.

1. Firmware introduction

PDIUSBD12 is a high-speed parallel bus USB interface device that supports local DMA transfer. The goal of firmware design is to make D12 achieve the fastest transfer rate on USB. Peripheral devices such as printers use D12 to transmit large amounts of data. The CPU in these devices is busy with many tasks like device control, data and image processing. The firmware of D12 is designed into a complete interrupt-driven mode. When the CPU is processing the foreground task, the USB transfer is processed in the background. This guarantees the best transmission rate and better software structure, simplifying programming and debugging.

The data exchange between the background interrupt service program and the foreground main program is achieved through flags and data buffers. For example, a circular data buffer is used for D12’s main mass output terminal. When D12 receives a data packet from the USB, it generates an interrupt request to the CPU, and the CPU immediately executes the interrupt service routine. In the interrupt service routine, the firmware sends the data packet from the internal buffer of D12 to the circular buffer, and then clears the internal buffer of PDIUSBD12 so that it can receive the next new data packet. The CPU can continue to execute the current foreground task until it is completed. Then it returns to the main loop, checks whether there is new data in the circular buffer, and starts another foreground task.

With this structure, the main function does not care whether the data source comes from a USB device, a serial port or a parallel port. The main function only checks whether there is new data in the circular buffer to be processed. Therefore, the main function sets the target as data processing, and the work done by the interrupt service routine is to transmit data as fast as possible. Similarly, the control endpoint uses the same concept in data packet processing. The interrupt service routine receives and stores the control transmission content in the data buffer and sets the corresponding flag register. The main function will send the request to the protocol handler. Once all standard equipment, classes, and user requests are processed in the protocol handler, the interrupt service routine can maintain its efficiency.

2. Firmware structure and tasks

1) Firmware structure

The firmware consists of 6 parts, which are as follows: Main program: Send USB device request, read test key, control light-emitting diode, handle USB bus transaction, etc., standard request, user request, interrupt service program, PDIUSBD12 command interface, hardware layer. The following is a brief introduction:

Hardware layer: This is the lowest layer in the firmware, which completes the hardware channel to PDIUSBD12. When the firmware is transferred to other CPU platforms, this part needs to be modified or added.

PDIUSBD12 command interface: In order to further simplify programming with PDIUSBD12, the firmware defines a series of command interfaces, which compress all the functions of using PDIUSBD12.

Interrupt service routine: This part of the code handles the interrupt generated by PDIUSBD12. It receives the data from the FIFO area in the PDIUSBD12 to the CPU memory, and establishes appropriate flags to notify the main program to process.

Main program: The main program checks the event flags and transfers to the appropriate subroutine for further processing. It also contains man-machine interface codes, for example, LED and keyboard scanning.

Protocol layer: The protocol layer handles standard USB device requests, as well as specific user requests such as DMA and TWAIN.

2) Firmware task

In the process of writing the firmware program, PDIUSBD12 as the interface chip hardware has done a lot of work for us, including:

a. Detect new incoming transactions; translate the information obtained from the USB bus; b. Detect the destination address of the transaction to determine whether to respond; e. Determine the type of transaction; d. Receive data from the USB bus to the FIFO; e. Send data from the FIFO to the USB bus; f. Calculate the check bit, detect and report the result.

But there is still a considerable part of the work to be completed by the firmware program. This part of the task includes:

a. Initialize PDIUSBD12 and handle the interrupt of PDIUSBD12; b. In response to various requests, at least 11 standard requests must be responded to; c. Select appropriate data to write into the FIFO of PDIUSBD12; d. Read data from the FIFO of PDIUSBD12;

3) Initialization of PDIUSBD12

Like the general interface chip, PDIUSBD12 must be initialized before use. The initialization steps are as follows:

a. Use the Set Address/Enable command to enable the chip function, the address should be set to 0; b. Use the Set Endpoint Enable command to enable all endpoints on the chip except for endpoints 0 and 1; c. Use the Set Mode command to disconnect, Delay 1-2s; use Set Mode command to connect; d. Use command Read Interrupt Register to clear interrupt register;

After initialization, D12 can receive information on the USB bus. After that, the host reads the device information through endpoints 0 and 1, configures the device, sets the device address, and completes the device enumeration.

4) List of USB devices

When a USB device is connected to the host, the host’s USB bus driver and root hub driver are notified of the device connection and start to enumerate the device. On the host side, the process of enumerating devices is to send a series of standard requests to the devices. At the device end, it needs to respond to the request sent by the host. The sequence is as follows:

a Respond to the request to obtain the device descriptor. Be sure to ensure that the device descriptor is sent correctly; b. In response to the request to set the address, the device enters the addressing state; c. Responding to the request to obtain the device descriptor three times in a row; d. In response to the request to obtain the configuration descriptor, be sure to ensure the configuration descriptor Send correctly; e. Send all configuration descriptors, and send all interface descriptors and endpoint descriptors at the same time;

If the above steps are correct, the host will prompt to find a new device and install the driver. Otherwise, it prompts to find an unknown device and the device is unavailable.

5) Implementation of the USB device firmware framework

From the above discussion, it is found that writing the firmware of a USB device is essentially processing requests from various hosts. Among them, the standard 11 requests must be implemented, and if the device is required to have other functions, a custom request must be added. According to the definition in USB 2.0, these firmware programs are called the framework of USB devices. The following describes the implementation of the USB device framework.

3. The structure of the main program

Once powered on, the MCU will initialize all ports, memories, timers and interrupt service routines. After that, the MCU needs to reconnect to the USB. This procedure is very important because it ensures that D12 will not act until the MCU is ready to serve D12.

In the main loop program, the MCU will ask for any actions on the keyboard. If any specific key is pressed, the command to process the key will execute the processing program, and then return to the main program. This program is only for debugging. The 1ms timing is used to start the subroutine to check the pressed key.

When the query arrives to check the Setup packet, it needs to first determine whether the setup flag is established. If the setup flag has been established, it will send a device request to the protocol layer for processing. The following flowchart shows the execution of the main program in the foreground.

Application Design of USB Device Firmware Based on Single-chip Microcomputer

4. Realization of interrupt service routine

The PDIUSBD12 firmware is completely interrupt driven, so the interrupt service routine is a very important part of the entire firmware writing process. After entering the interrupt service program, first issue the command Read Interrupt Register (control word is F4H) to read the interrupt register, determine the interrupt source according to the content of the interrupt register, and then jump to a different service program to handle the interrupt.

The interrupt service routine (ISR) communicates with the main program through the event flag “EPPFLAG” and the data buffer “Control-XFER”.

The task distribution between the main program and the interrupt service program (ISR) is that the ISR collects data from D12, and the main program will process the data. The ISR notifies the main program to prepare for processing only when it has collected enough data, which will reduce unnecessary main program service time and simplify the main program.

Bus reset and idle do not require special processing in ISR. ISR either sets the bus reset flag, or suspends this bit in EPPFLAG and exits.

Controlled transmission always starts with the setup phase, followed by the data phase. Then it ends with the status phase.

The following takes the processing of Control OUT and Control IN, the two most complex and important interrupt subroutines, as examples to illustrate some of the problems of writing interrupt service routines.

Control OUT is mainly used by the host to transfer control requests, and can also be used to transmit data. After entering the service program, the control word Read Last Transaction Status (40H) must be sent to PDIUSBD12 to read the pipeline status. According to the read status, it is judged because There is a data packet or the arrival of a SETUP packet and the interruption. When the SETUP packet is received by D12 of the USB device, the device will generate an interrupt to the MCU. The microcontroller will determine whether the packet is sent to the Control endpoint or Genric endpoint for processing by reading the contents of the D12 interrupt register. If the packet is sent to the Control endpoint, the MCU will further determine whether the data is a SETUP packet by reading the “Read Last Transaction Status Register” of D12. For a request to obtain a descriptor device, the first packet must be a SETUP packet.

After that, the MCU needs to read the content of the SETUP packet to determine whether the endpoint is full or empty by selecting the Control Out endpoint. If the control endpoint is full, the MCU reads its content from the buffer and stores it in the memory. Then, it will verify the legality of the host device request from the memory. If it is a legal request, the MCU must send the “Acknowledge Setup command” to the Control Out endpoint to enable the next packet to be received. In the next step, the MCU needs to determine whether the control transfer is control read or control write. This can be done by reading 8 bytes of bmRequestType from the SETUP packet. Control transmission is control reading, and the device needs to send back data packets to the host in the next data phase. The MCU needs to create a flag to indicate that the USB device is now in transfer mode. Prepare to send data at the request of the host.

Control IN is mainly used to transfer data to the host. After entering the service subroutine, the Control-In interrupt Bit must be cleared by reading Read Last Transaction Status (40H). After confirming that D12 is in the transfer mode, the MCU will transfer the data The packet is sent to the host. However, since D12 has only a 16-byte FIFO, the MCU must control the total amount of transmission if the required length is greater than 16 bytes. The MCU must check the current and remaining data to be sent to the host. If the remaining bytes are greater than 16 bytes, the MCU will send the first 16 bytes first. When the next Control-IN token packet arrives, the MCU will first determine whether the remaining bytes are 0. If so, the MCU will send an empty packet to the host to notify it that the data has been transmitted.

Concluding remarks:

The powerful USB specification has become one of the many standards of the PC, and the various developments of computer technology are inseparable from the knowledge of USB. The innovation of this article: use the specific protocol of USB communication, use the MCU assembly language to program, use the instruction system of the MCU to realize the specific communication between the USB device and the PC host, and design the USB device suitable for the user’s needs.

The Links:   CLAA150XG02 G104X1-L04

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *