[ieee 2008 electronics, robotics and automotive mechanics conference (cerma) - cuernavaca, mexico...

6
USB bulk transfers between a PC and a PIC microcontroller for embedded applications Ruben Posada-Gomez, Jose Jorge Enriquez-Rodriguez, Giner Alor-Hernandez, Albino Martinez-Sibaja Instituto Tecnologico de Orizaba, Division of Research and Postgraduate Studies Orizaba, Veracruz, Mexico {rposada, jenriquez, galor, amartinez}@itorizaba.edu.mx Abstract The Universal Serial Bus (USB) has become the most popular communication interface among personal computers and embedded devices because of its ease of use, low cost, data bandwidth and availability in most computing systems. Microchip’s PIC 18Fx550 microcontroller has an embedded USB controller that allows rapid development of USB enabled devices. This paper presents a system for bidirectional communication between a personal computer and an embedded device using a PIC 18F2550 microcontroller in USB bulk transfer mode. A comparison between three different drivers for host application was done to determine the advantages of each one. Finally, a case of study for a remote home lighting control system is presented. 1. Introduction For years, embedded application developers have required to enable an interface for communicating their devices with a personal computer, they have chosen either a parallel port (originally designed as a printer interface) or a serial port. However, as technology evolves, the need for faster and better interfaces arises. The Universal Serial Bus (USB) has been developed to overcome disadvantages of previously available communication interfaces; it is a fast, bi-directional, isochronous, low-cost, dynamically attachable serial interface that is consistent with the requirements of the PC platform now and future [1]. The USB is versatile enough for a wide range of peripheral devices [2], including input devices such as mice [3], keyboards, disks, printers, audio and video devices. It is also used for data acquisition devices [4, 5], control and monitoring systems [6, 7], multimedia devices [8], and even security ones [9]. In this work, we present the development of an embedded device that uses the USB port as the interface to a personal computer, using a remote home lighting system as a case study. On the hardware side, the design is based on a Microchip PIC 18f2550 which has an USB interface. We used USB communication in bulk mode since this is the preferred mode for data acquisition devices because of its fast data transmission rate. This paper is structured as follows. In Section 2 we present the development of the hardware device, including firmware design. We cover the development of the low level software for the PC in Section 3. In Section 4, we present the tests and results for the three different interfaces under test. Using the hardware and software previously designed, in Section 5 we present the design of a remote home lighting control system as a case study of the design of an USB enabled embedded device. Section 6 summarizes some related works and in Section 7 we present our conclusions and point to future directions. 2. Hardware design Firstly, we should clearly define what is being designed before work was started, which is hardware- capable of receiving commands from a PC through the USB port, and then setting some digital outputs. There are a lot of ways to transmit data from a PC to external hardware, such as serial port, parallel port, a general purpose input/output card and USB port. The major disadvantage of serial port is the level of voltages used (±12V) for communications; this requires an IC to translate the signal levels to levels a microprocessor can handle. Also, it has a data rate limit of 115 kb/s, which is not enough for common data acquisition devices required today. There have been four standards for parallel port over the course of time, and there are a variety of chipsets that operate the port in different modes. General purpose input/output Electronics, Robotics and Automotive Mechanics Conference 2008 978-0-7695-3320-9/08 $25.00 © 2008 IEEE DOI 10.1109/CERMA.2008.21 559

Upload: albino

Post on 04-Dec-2016

216 views

Category:

Documents


4 download

TRANSCRIPT

USB bulk transfers between a PC and a PIC microcontroller for embedded applications

Ruben Posada-Gomez, Jose Jorge Enriquez-Rodriguez, Giner Alor-Hernandez, Albino Martinez-Sibaja

Instituto Tecnologico de Orizaba, Division of Research and Postgraduate Studies Orizaba, Veracruz, Mexico

{rposada, jenriquez, galor, amartinez}@itorizaba.edu.mx

Abstract

The Universal Serial Bus (USB) has become the

most popular communication interface among personal computers and embedded devices because of its ease of use, low cost, data bandwidth and availability in most computing systems. Microchip’s PIC 18Fx550 microcontroller has an embedded USB controller that allows rapid development of USB enabled devices. This paper presents a system for bidirectional communication between a personal computer and an embedded device using a PIC 18F2550 microcontroller in USB bulk transfer mode. A comparison between three different drivers for host application was done to determine the advantages of each one. Finally, a case of study for a remote home lighting control system is presented. 1. Introduction

For years, embedded application developers have required to enable an interface for communicating their devices with a personal computer, they have chosen either a parallel port (originally designed as a printer interface) or a serial port. However, as technology evolves, the need for faster and better interfaces arises.

The Universal Serial Bus (USB) has been developed to overcome disadvantages of previously available communication interfaces; it is a fast, bi-directional, isochronous, low-cost, dynamically attachable serial interface that is consistent with the requirements of the PC platform now and future [1].

The USB is versatile enough for a wide range of peripheral devices [2], including input devices such as mice [3], keyboards, disks, printers, audio and video devices. It is also used for data acquisition devices [4, 5], control and monitoring systems [6, 7], multimedia devices [8], and even security ones [9].

In this work, we present the development of an embedded device that uses the USB port as the interface to a personal computer, using a remote home lighting system as a case study. On the hardware side, the design is based on a Microchip PIC 18f2550 which has an USB interface. We used USB communication in bulk mode since this is the preferred mode for data acquisition devices because of its fast data transmission rate. This paper is structured as follows. In Section 2 we present the development of the hardware device, including firmware design. We cover the development of the low level software for the PC in Section 3. In Section 4, we present the tests and results for the three different interfaces under test. Using the hardware and software previously designed, in Section 5 we present the design of a remote home lighting control system as a case study of the design of an USB enabled embedded device. Section 6 summarizes some related works and in Section 7 we present our conclusions and point to future directions. 2. Hardware design

Firstly, we should clearly define what is being designed before work was started, which is hardware-capable of receiving commands from a PC through the USB port, and then setting some digital outputs.

There are a lot of ways to transmit data from a PC to external hardware, such as serial port, parallel port, a general purpose input/output card and USB port. The major disadvantage of serial port is the level of voltages used (±12V) for communications; this requires an IC to translate the signal levels to levels a microprocessor can handle. Also, it has a data rate limit of 115 kb/s, which is not enough for common data acquisition devices required today. There have been four standards for parallel port over the course of time, and there are a variety of chipsets that operate the port in different modes. General purpose input/output

Electronics, Robotics and Automotive Mechanics Conference 2008

978-0-7695-3320-9/08 $25.00 © 2008 IEEE

DOI 10.1109/CERMA.2008.21

559

(GPIO) cards are not standard equipment on PCs, so they must be acquired separately after the PC is purchased. Furthermore, there is no GPIO card standard.

The USB port has become a popular option, being its primary advantages the ability to power larger circuits and the bandwidth of the connection (480 Mb/s for high speed starting with the release of version 2.0 of the USB specification). From the user point of view, the benefits of USB are ease of use, fast and reliable data transfers, flexibility, low cost, and power conservation [2]. From the developer point of view, the USB port makes the firmware and software become more complex since they must be able to communicate using device drivers. In this paper, we show it is relatively easy to implement an USB enabled device using today available tools.

Next, PIC circuit board is described to show our hardware design functionality.

2.1. PIC circuit board

For communication between PC and PIC via the

USB port, it is necessary to have some elements around the PIC that allow correct performance of the circuit. The following items were design goals for this circuit:

1. USB interface to PC 2. Vcc range of 3.0 – 5.5V 3. Inexpensive components 4. The board should be as small as possible To accomplish these goals, a PIC18F2550 was

chosen primarily because of the USB internal controller and low cost. Figure 1 shows a block diagram of the hardware circuit.

Figure 1. Hardware block diagram

The circuit is powered by the PC USB port. Signals

RC4 and RC5 are used to communicate with the PC using an available USB port. RA7 and RA6 are connected to an external crystal oscillator. On a PIC

microcontroller, MCLR is an active-low reset signal, in our circuit, we connect it to Vcc since we do not need to hard reset the device. The system manages eight digital outputs through port B of the PIC microcontroller and five analogical inputs using signals RA0, RA1, RA2, RA3 and RA5.

2.2. Firmware

The PIC program was written in C language, and

compiled using CCS PIC-C compiler. This compiler includes an USB library that supports Microchip’s PIC microcontrollers 18F2550 and 18F4550, and the USBN9603 USB controller from National Semiconductor.

The firmware was designed to be a low-overhead protocol executing any given command as fast as possible. This allows the software to make decisions and control the application command flow, while the firmware just receives basic commands and executes them. Table 1 shows the command protocol used in our design.

Table 1. USB commands supported by the PIC

circuit.

Command First byte sent

Second byte sent

Returned bytes

Clear Output 0x00 Output

number None

Set Output 0x01 Output number None

Get Output Status 0x02 Output

number

Output Status 0x00 = Clear 0x01 = Set

Read Analog Input

0x03 Input number

Analog Value (1 Byte)

The items considered essential were the Clear

Output and Set Output commands. The Get Output Status command was implemented so that it is possible to confirm whether the set/clear commands worked or not.

To implement the firmware discussed above, the program flow shown in Figure 2 was designed.

Once the device has been enumerated by the operative system, the program waits to receive two command bytes, after receiving them, it checks the first byte to select the desired operation mode, and then the value of the second byte is evaluated so that the correct input/output is set. The two command bytes are sent from the PC by the low level software interface, described in next section.

560

Figure 2. Firmware program flow

3. Low level software design

The host application needs to make use of a driver to communicate with the device. We tested three different driver alternatives, Microchip MCHPUSB, Microsoft WinUSB and the libusb Windows port (libusb-win32).

We focused on making the low-level PC code reusable. This makes using the PIC circuit with multiple projects simple once the initial work to provide PC communications code is complete. This section concentrates on the software that allows the PC to communicate with the PIC firmware through the USB port.

For achieving reusability, we developed a C++ class to handle all communication to the PIC circuit. Because all of the communication requirements are contained within a single class, any instance of the class is hence able to command the circuit to do any of its functions. The provided class constructor receives the VID and PID device identifiers, along with an integer value that indicates which driver interface to use, where the options are USE_MCHPUSB, USE_WINUSB and USE_LIBUSB.

Once the class has been instantiated, the USB port is initialized using the initUSB function, and then all of the functions related to sending and receiving data of the class can be called as long as the class remains within scope. These functions include setOutput(), clearOutput(), getStatus() and readAnalog(). To communicate with the PIC’s firmware, these commands call several routines to read and write data

via the USB port. These routines depend on the driver alternative in use.

3.1. Microchip MCHPUSB

The use of the API provided by Microchip is very straightforward; it includes functions for opening the pipes to the device endpoints, reading and writing data to the opened pipes.

3.2. Microsoft WinUSB

This option is a bit more complicated than

MCHPUSB because it requires a Global Unique Identifier (GUID) to be created for our device. We based our WinUSB implementation on [10]. However, source code provided by Microsoft is not the full working version and needs some corrections. In particular, there is no reference to the GUID declaration in the code, which can be done using the following code (assuming our device GUID is d3dd9df5-241b-4052-a6de-04e621dde5b1).

DEFINE_GUID(

InterfaceClassGuidConstant, 0xd3dd9df5, 0x241b, 0x4052, 0xa6, 0xDe, 0x04, 0xe6, 0x21, 0xdd, 0xe5, 0xb1

); GUID GUID_DEVINTERFACE =

InterfaceClassGuidConstant; Besides, the program makes use of a devInfo

variable, but it is neither defined nor declared anywhere in the sample source code. We defined devInfo as an instance of a _DEVINFO struct declared as follows

struct _DEVINFO { WINUSB_INTERFACE_HANDLE

winUSBHandle; UCHAR deviceSpeed; UCHAR bulkInPipe; UCHAR bulkOutPipe; UCHAR interruptPipe; };

3.3. Libusb Windows port Libusb-win32 is a port of the USB library libusb to

the Windows operating systems. The library allows user space applications to access any USB device on

Wait to receive a two byte

packet from PC

Is device enumerated?

Process command bytes, Execute command and Return data if required

Start

y

n

561

Windows in a generic way without writing a line of kernel driver code. The biggest advantage of using libusb is that it is a fully portable API, it is supported by several operative systems including Windows, Linux and MacOS X. 4. Tests and results

In order to test the three different drivers, we developed a small application in C++. We created three versions of this test application, each one using a different driver interface so that we could compare all of them. The test program flow is showed in Figure 3. The developed Applications were executed under Windows XP using a Pentium D E2160 @ 1.8 GHz PC.

Figure 3. Testing application main loop. A comparison of speed results for each driver is

presented in Table 2. Table 2. Time results sending and receiving

64 bytes packets 1000 times.

Test

MCHPUSB WinUSB Libusb Send/Receive

time (ms)

Send/Receive time (ms)

Send/Receive time (ms)

1 1844 / 2156 1984 / 2032 2063 / 1953 2 1983 / 2017 2078 / 1938 2031 / 1985 3 2032 / 1968 1938 / 2062 2046 / 1970 4 2016 / 2000 2094 / 1953 2032 / 1984 5 2032 / 1983 2093 / 1938 1985 / 2015

MEAN

1981.4/2024.8 2037.4/1984.6 2031.4/1981.4 2003.10 2011 2006.40

Useful information can be extracted from Table 2,

we observe that alternative drivers have very similar

performance to Microchip’s one. While Microchip driver has shown a smaller sending time, it has a bigger receiving latency. So, if we were developing an embedded device which would mostly send data to a PC, WinUSB and libusb drivers would be a better alternative. If the device would send data as much as receive it, Microchip driver would be the option since it has shown the best overall performance. Another design requirement could be portability to different operative systems. Table 3 shows the operative systems supported by each library.

MCHPUSB and libusb offer functions for listing and initializing USB devices; in WinUSB, initialization code is mandatory through different Windows API functions. For fast prototyping, we suggest using Microchip MCHPUSB library. If there is the possibility of switching from Microchip to another USB controller vendor, we would suggest using WinUSB or developing a custom driver using the new Microsoft Windows Driver Foundation (WDF) specification. Finally, if there are plans on supporting another operative system besides Windows, the option would be libusb.

Table 3. Operative system support per USB

library

Driver Win XP

Win Vista

Other Windows Linux

MCHP X X Untested - WinUSB X X X -

libusb X X X X On next section, we present the development of a

remote home lighting control system based on the tools previously designed in sections 2 and 3. 5. Case of study: a remote home light control system

A remote home lighting control system has been developed as case study of the development of an embedded device that communicates with a PC using an USB port. The goal of the system is to switch on/off some home lights through an internet connection. We make use of the hardware and low-level software presented previously in sections 2 and 3.

There are three tasks to complete to build an application based on the PIC reference design.

1. Program the PIC 18F2550 microcontroller using the firmware developed in section 2.

2. Build the hardware circuit. 3. Write the application software including the low-

level code developed in section 3.

Repeat 1000 times

Create a 64 bytes array

Send data array to PIC

Get current time in ms

Compare received data

Get current time in ms

Read data from PIC

Get current time in ms

Compute transfer time

562

An additional task is needed for our system: configuring the web server and developing the ASP web page that serves as interface to the user/administrator. We will not go into detail with the web server application, since our focus is on the USB communication part of the system.

A block diagram of the complete remote home lighting system is depicted in Figure 4.

The software for the home lighting system is divided in two parts: a web server running an ASP page as interface for the user, and a Visual C++ application running on the host PC that works as interface between the web server and the microcontroller board through an USB port.

Figure 4. Remote home lighting system

block diagram. 5.1. Web server

An IIS (Internet Information Server) was setup on a HP dv2000 laptop running Windows XP; it was configured to serve a password protected ASP webpage that works as user interface. This ASP page shows the actual status of the controlled home lights and lets the user set a new status for the lights; this is saved to a Microsoft Access database stored in the host computer. 5.2. PC host application

A Visual C++ application keeps running on the host PC, this application periodically queries the database to check whether the light status should be changed, if so, it sends the required commands to the hardware board through the USB port using the class created in section 3, after sending the command, it reads back the new light status to confirm correct operation of the system. Program flow is shown in Figure 5. 5.3. Tests

Once the web server was started and the VC++ host application was running, we execute several tests accessing the ASP web page from five different

locations: two computers in the same local network, and three located in other cities, we successfully set the light status every time and without any errors. We set the database query time to 1ms, 10ms, 100ms, 1s and 10s, it worked fine in all cases.

Figure 5. Host application program flow.

6. Related works

The need for USB enabled devices and adding USB support to existing ones is rapidly increasing. In [11], design principles for a handheld haptic device are discussed. Also, the authors describe the implementation of a semi-portable handheld force feedback display consisting of single actuated degree of freedom, orthogonal force sensing and a USB controller for ease of connectivity to a variety of hosts.

The development of a virtual instrument using a PC and USB communication is presented in [12]. By the method of engineering mathematics, an instrument with the ability to learn by the method of engineering mathematics was made, along with a progress in system measure range and measurement accuracy.

An EEG analyzing system based on Advanced RISC Machines (ARM) and uC/os-II real time operating system is discussed in [13]. Furthermore, the details of data acquisition, data preprocessing, data transmitting through USB and system configurations were contained in the system design. The design of realizing multi-task system in uC/os-II, the definition of communication protocols between PC and the equipment and the detail configurations of USB are given out. Finally, the feasibility of the filter behaviors of the equipment was shown.

Our system could be used by all mentioned works and for similar ones.

Query database

Are there any

changes?

Send new light status to PIC board

y

n

Start

Remote PC Host PC

PIC

Internet

USB

563

7. Conclusions and future work

The required process for performing USB transfers in bulk mode using a Microchip PIC 18fX550 has been shown, three different driver interfaces were tested, and the developed test application has been useful in evaluating their advantages and disadvantages.

From our case study, it is shown that Microchip’s PIC 18fx550 allows rapid development of USB enabled embedded devices, offering great flexibility and easy programming using the standard C language. The USB interface gives good flexibility and is user friendly since the user does not need to open his/her system in order to install it, it’s plug & play, fast and cheap. The use of a PC web server offers several advantages over embedded web servers, it has no limit on the number of simultaneous connections, there’s no limit neither for the size of served web pages nor the number of variables that can be set through it. Also, a PC can offer more flexibility; we could easily add a webcam to the system and set it to show an image of the system, whether to show a picture of interest or to confirm the good operation of the system.

As future work, work has started in the development of a custom user mode driver based on Microsoft User Mode Driver Foundation (UMDF) specification. The goal of the project is to offer a common interface for USB communication between a Windows based PC and embedded devices based on USB controller chips from any vendor. 8. References [1] “Universal Serial Bus Specification”, Revision 2.0, April 2007. [2] Axelson, Jan. USB Complete, Lakeview Research LLC, Third Edition, 2005. [3] Kudo, Hiroyuki et al, “Force Feedback Mouse with Differential Mechanism for Omni-Traveling”, International Symposium on Micro-NanoMechatronics and Human Science, pp. 430-435, 2007.

[4] Bi Bo, Sun Shuying and Wang Chunping, “Design of Data Acquisition Equipment Based on USB”, Conference on Electronic Measurement and Instruments, pp. 1-866 – 1-869, 2007. [5] Ho, C. S. et al, “Design of Portable ECG Recorder with USB Storage”, IEEE Conference on Electron Devices and Solid-State Circuits, pp. 1095-1098, 2007. [6] Yong-Seok Kim, Hee-Sun Kim and Chang-Goo Lee, “The development of USB home control network system”, Control, Automation, Robotics and Vision Conference, pp. 289-293 Vol. 1, 2004. [7] Wang Weiguo et al, “Design and Implementation of Automatic Test Instrument for Digital Circuits Based on USB 2.0 Interface”, 8th International Conference on Electronic Measurement and Instruments, pp. 4-758 – 4-761, 2007. [8] Ching-Chang Wong et al, “Remote controlled game platform by USB joysticks”, IEEE Conference on Mechatronics, pp. 136-139, 2005. [9] Lin, Kun, Yuan, Lin and Qu, Gang, “SecureGo: A Hardware-Software Co-Protection against Identity Theft in Online Transaction”, ECSIS Symposium on Bio-inspired, Learning, and Ingelligent Systems for Security, pp. 59-64, 2007. [10] Microsoft Corporation, How to use WinUSB to Communicate with a USB Device, 2007. [11] Karon E. MacLean, Michael J. Shaver and Dinesh K. Pai, "Handheld Haptics: A USB Media Controller with Force Sensing”, 10th Symposium on Haptic Interfaces for Virtual Environment and Teleoperator Systems, pp. 311, 2002. [12] Shi Guangfan et al, “The design and implement of virtual instrument based on computing technique and USB platform”, Proceedings of the Third International Conference on Information Technology and Applications, 2005. [13] Liu, Boqiang et al, “An Embedded EEG Analyzing System Based on uC/os-II”, Engineering in Medicine and Biology Society, Annual International Conference of the IEEE, pp. 2468-2471, 2007.

564