Building a RS-232 to USB Bridge Using a AT89C5131

advertisement
A T M E L
A SINGLE USB PORT CAN BE USED TO
CONNECT UP TO 127 PERIPHERALS
DEVICES WITHOUT HAVING TO ADD
AN ADAPTER OR EVEN TURN OFF
THE COMPUTER.
89C5131 USB Microcontoller
Atmel offers an extensive portfolio of
USB products that include USB hubs,
microcontollers with integrated USB
interface, and embedded USB hosts.
These devices are designed specifically
to hide the complexities of USB protocol
form your embedded design. The
availability of the USB firmware library,
sample code, and software design tools
such as the USB Wizard (a GUI based
source code generator) further reduces
your learning curve and ensures the
shortest design.
A P P L I C A T I O N S
J O U R N A L
Building a RS-232 to USB Bridge Using a
AT89C5131 Microcontroller
By Michael Panfil
Long live the RS-232 COM port! In the world of
personal computers, the RS-232 COM port is
disappearing, especially with laptops. The age-old
serial port interface is being replaced by the USB, a
bus that supports higher speeds and is significantly more flexible. The final USB-capable application
can use the flexibility of the USB with its data
buffering and automatic flow control. The USB link
offers a power supply for the application. The
overwhelming success of this simple universal
serial PC interface is also due to its plug-and-play
and hot plugging capabilities. A single USB port can
be used to connect up to 127 peripherals devices
without having to add an adapter or even turn off
the computer. Furthermore, USB is expected to
completely replace other serial, parallel and game
interfaces in PC and peripherals markets. USB
connectivity can be found in a wide range of
devices and markets nowadays including industrial
equipment and data acquisition, security
applications (i.e. fingerprint reader, ID mouse, smart
card reader), consumer electronics (i.e. Flash disk),
communication equipment (i.e. mobile phone), and
PC and peripherals (i.e. multimedia key-board,
audio and game peripherals).
As the world embraces this transition to USB, there
is still a fundamental, underlying issue - There are
still many legacy applications that use the RS-232
standard. The obvious solution is to make the USB
bus appear as an RS-232 interface (Figure 1). The
obvious advantages of this masquerade are that
there will be no need to change the PC application
and fewer modifications are required for the
embedded hardware and software.
Making the Switch
There are several ways to make the switch from
RS-232 to USB, but the aim is to perform this switch
with a minimum time and effort. With the Atmel USB
RS-232 Virtual COM port library, you can easily build
an RS-232 to USB gateway. This gateway uses the
USB Communication Device Class (CDC) drivers to
take advantage of the installed PC RS-232 software
to talk over the USB. The CDC document, available at
www.usb.org, describes a way to implement
devices such as ISDN modems and virtual COM
ports. The CDC class is implemented in all releases
of Windows, from Windows 98SE to Windows XP.
The 89C5131 is a high-performance Flash version of
the 80C51 single-chip 8-bit micocontroller with full
speed USB funcions. It features a full-speed USB
compatible with the USB specifications Version 1.1
and 2.0. This module integrates the USB tranceivers
with a 3.3V voltage regulator and the Serial Interface
Engine with Digital Phase Locked Loop and 48 MHz
clock recovery.
800
Millions of
Units
600
400
200
0
2001
2002
2003
2004
2005
2006
Forecast for USB-enabled Devices
Source: In-Stat/MDR 2002
Figure 1: The aim of a Virtual COM port implementation is to move from a real UART system to a UART-USB system
www.atmel.com
page 21
A T M E L
A P P L I C A T I O N S
J O U R N A L
The architecture of this gateway requires very few
modifications. On the PC side, there is nothing to
change! The PC views the USB device as a COM
port and the application doesn’t need to be
changed. On the embedded side, UART USB drivers
replace the UART driver. The embedded application
simply calls UART-USB functions instead of UART
functions. In order to be considered a COM port,
the USB device declares 2 interfaces: (1) Abstract
Control Model Communication, with 1 Interrupt IN
endpoint; and (2) Abstract Control Model Data, with
1 Bulk IN and 1 Bulk OUT endpoint.
When plugging in a new USB device, Windows
checks all its INF files to load the appropriate driver. The INF file contains the Vendor ID (VID), the
Product ID (PID), or the USB Class definition. If the
VID/PID or the USB Class Definition of the USB
device matches with an INF file, Windows will load
the driver described in this file. But Microsoft does
not provide a standard INF file for the CDC driver,
so Atmel provides an INF file that allows this driver to load under Windows 2000 and Windows XP.
When plugged in for the first time, the user
instructs the Operating System which driver to use
by selecting this INF file. The application
manufacturer, using its own VID and PID, modifies
these values in the embedded application (in
the config.h file) and the INF file given in the example on this page.
Modifying the Embedded
Application Firmware
From a hardware perspective, the USB to UART
bridge can be implemented using Atmel’s
AT89C5131 microcontroller with one of its 7 USB
endpoints. The embedded firmware must be modified to include an intermediate buffer that will
handle data in the event that the USB is not ready
(Figure 2). The software modules required to
implement all the embedded firmware are available on Atmel’s website.
According to a 2002 report by In-Stat/MDR, almost
800 million devices will be USB enabled by 2006.
Obviously, this implies that many new devices will
not require the RS-232 to USB bridging. However,
it’s good to know that there’s a relatively simple
solution for all the legacy devices with only a UART
interface.
Figure 2: In this implementation, all characters received from the UART are stored in an intermediate
buffer to avoid lost data when the USB is unable to handle the data.
Windows 2000 and XP Setup File
for AT89C5131 Demo
usbser.sys
[Reader.NT.AddReg]
[Version]
Signature="$Windows NT$"
HKR,,DevLoader,,*ntkern
HKR,,NTMPDriver,,usbser.sys
Class=Ports
ClassGuid={4D36E978-E325-11CEBFC1-08002BE10318}
HKR,,EnumPropPages32,,"MsPorts.dll
,SerialPortPropPageProvider"
[Reader.NT.Services]
Provider=%ATMEL%
LayoutFile=layout.inf
DriverVer=10/15/1999,5.0.2153.1
[Manufacturer]
%ATMEL%=ATMEL
[ATMEL]
%ATMEL_CDC%=Reader,
USB\VID_03EB&PID_2009
[Reader_Install.NTx86]
;Windows2000
[DestinationDirs]
DefaultDestDir=12
Reader.NT.Copy=12
AddService = usbser, 0x00000002,
Service_Inst
[Service_Inst]
DisplayName = %Serial.SvcDesc%
ServiceType = 1 ; SERVICE_KERNEL_DRIVER
StartType = 3 ;
SERVICE_DEMAND_START
ErrorControl = 1 ;
SERVICE_ERROR_NORMAL
ServiceBinary = %12%\usbser.sys
LoadOrderGroup = Base
[Strings]
ATMEL = "ATMEL, Inc."
ATMEL_CDC = "AT89C5131 CDC USB to
UART"
[Reader.NT]
CopyFiles=Reader.NT.Copy
AddReg=Reader.NT.AddReg
www.atmel.com
[Reader.NT.Copy]
page 22
Serial.SvcDesc = "USB Serial
emulation driver"
Download