Design of universal USB Interface Module Based on AVR Microcontroller
[Date:2008-6-25] |
Source:China Power GridAuthor:Zhang Hong, Dong xiucheng |
[Font: large, medium, and small] |
1 Introduction
USB interfaces have become one of the main ways for peripherals to communicate with PCs due to their fast data transmission, simple connection, easy scalability, and support for hot swapping. With the development of embedded systems, embedded microprocessor needs to add general USB interfaces to achieve communication with USB host systems such as PCs. To address this need, we use the USB interface device PDIUSBD12 of Philips and the AVR series single chip microcomputer Atmel to design a universal USB interface module. This module allows you to easily add USB interfaces to various embedded microprocessors to achieve high-speed communication with the USB host system.
2 system hardware design
The core of the system module is the AVR high-speed single-chip microcomputer ATmega8. AVR microcontroller is a new generation of high-speed Proteus microcontroller based on the Harvard structure. It features fast speed, low price, high reliability, strong I/O port driving capability and a rich array of On-Chip Integrated peripheral resources, its internal integration includes flash, EEPROM, fuse bit and lock bit for ISP download programming. The ISP download cable of the AVR microcontroller is simple and cost-effective. It also supports free download software (such as ponyprog. PDIUSBD12 is a cost-effective USB interface device that fully complies with USB 1.l specifications and is easy to interface with various microprocessor.
The circuit connection between the system module AVR Microcontroller and PDIUSBD12 is shown in 1.
As shown in figure 1, due to the high speed of the AVR microcontroller, I/O ports can be used to simulate the time series of PDIUSBD12 in software mode and read and write it. This method allows you to flexibly control the time sequence and address of PDIUSBD12 based on the speed of different microprocessor, without decoding circuit, thus simplifying the hardware design and reducing costs.
The interface module integrates UART, SPI, I2C, and other interfaces, which can be used to communicate with other systems, making the interface module A universal interface converter. The system hardware architecture is shown in figure 2.
3 USB firmwareProgramDesign
The USB firmware program of the system module adopts the GCC compiler design that complies with the ansi c standard, and combines hierarchical modular structure to ensure high portability, you only need to modify the hardware interface layer to port it to another hardware platform, which can be reused.Code.
The USB firmware program is designed based on the state machine and standard front-end desktop program architecture. The modular hierarchy of the entire same program is shown in 3. First, write the command interface layers of the hardware interface layer Hal. C and PDIUSBD12 devices for the upper-layer module to call. The hardware interface layer contains a function for PDIUSBD12 to write commands and read/write data for the upper-layer module to call. When the CPU is different, you only need to modify these functions. Because the CPU access PDIUS-BD12 and general memory, just according to the hardware connection relationship, in the hardware abstraction layer to write PDIUSBD12 write command, write the function of read data, for the upper layer to call. The command interface layer that implements PDIUSBD12 must call the hardware abstraction layer function for the upper-layer module to call. Design the frontend and backend programs and standard device request program modules.
First, a global state variable is defined using the C language's shared body and bit domain as follows:
Users can add status signs as needed, such as UART, SPI, I2C, and other interfaces to send and receive data to meet the needs of various situations, then define a struct variable to store USB standard device requests.
Initialize global variables and other peripheral devices in the foreground Main program, and then check whether the status variable value changes in the while (1) endless loop, call the corresponding function at the lower layer based on different state variable values to complete related operations. In the background interrupt service program, the data received by PDIUSBD12 is migrated to the CPU memory or written into the buffer zone of the PDIUSBD12 sending endpoint Based on the read interrupt register value; on the other hand, the status variable value is changed according to the actual situation.
The program code for the foreground Main program is as follows:
When the current main program detects that the status variable has received the setup package event bevent_flags.bits.setup_packet as 1, the flag is cleared and the standard device request module stdreq is called. c's control_handler () function completes the enumeration of USB devices.
Design the standard device request module. First, use the struct to define the various descriptors required for USB enumeration for different device requests, and then write 11 standard device request processing functions. An important function of the request module at this layer is the Protocol control subroutine control Han-Dler (), which calls any of the 11 functions based on the different USB device request types identified in controldata. In addition, the request module of this layer also provides the control endpoint receiving and sending interrupt processing functions called by the interrupt service program. The relationship between the above functions and controldata variables is shown in Figure 4.
4. system integration and application
4.1 PC driver and Application Design
To use this system module to implement PC communication, you need to write related drivers and applications for the PC. You can use DriverStudio to generate drivers for this system module in windows. For application design, software development tools such as VB, Vc, Delphi, and LabVIEW, which are widely used in Virtual Instrument Software Development, can be used to directly call the API functions in the dynamic link library generated by the driver, you can write related applications based on specific tasks.
4.2 apply to other systems
The characteristics of various peripheral interfaces of the AVR microcontroller, such as UART, SPI, and I2C interfaces, can be used to effectively communicate with other microprocessors and embed the module into various systems, implement communication with other devices. The firmware contains UART, SPI, I2C, and other interface programs. You only need to use a simple jumper connection to select the corresponding interface, so as to add a USB interface to the application system.
4.3 porting firmware programs to other platforms
Since the firmware program is portable to the maximum extent, a slight modification of the firmware program can be applied to a variety of microprocessors with C language compilers, so that PDIUSBD12 can communicate directly with the microprocessor.
The main work of porting a firmware program is to modify the three read/Write Functions in the hardware abstraction layer based on the hardware connection relationship to realize the communication between the CPU and the device; check whether the communication between the CPU and the device is normal by calling the PDIUSBD12 command interface layer to check whether the return value of the read chip ID function is 0x1012.
5. application in data collectors
Due to the integration of the first-to-last-compare ADC and six analog input channels, you only need to select the corresponding sensor for the collected physical quantity and adjust the output signal to 0 ~ Within the range of VCC, you can use this module to implement a USB data collector. The AVR Microcontroller integrates the ADC with its own sampling and holding circuit, and provides internal reference voltage and Noise Suppression Based on sleep mode, which greatly improves the ADC precision and achieves high-precision data collection. In the design, you only need to add the function to obtain the ADC result in the corresponding firmware towel, and set the corresponding status flag to complete the design of the USB data collector.
6 conclusion
The general USB interface module is designed based on the core of ATmega8 and PDIUSBD12 and applied to the Data Collector Based on the USB interface. The experimental results show that the USB interface module runs stably and reliably, communicates fast, and is easy to modify and transplant, meeting the requirements of embedded systems for USB interfaces, and can quickly add USB interfaces for various microprocessor, has a wide range of application prospects.