USB interfacing

This article was originally published in VSJ, which is now part of Developer Fusion.

USB has changed the way many users handle the problem of connecting odd bits of hardware to their machines. Once, it might well have involved opening up the box, plugging in a card and then installing drivers that were almost guaranteed to crash the machine. USB on the other hand is as close to plug-and-play as plug-and-play gets, and users therefore like it. However from the programmer's point of view, USB is just as difficult as interfacing has always been, if not worse. Put simply, you have to write a driver conforming not only to the operating system's requirements, but also to the USB specification. Modules

This is clearly not an easy task, and certainly not cost-effective for a one-off project. However, the good news is that a UK electronics company, FTDI, ( has made building USB devices as easy as using them, and the drivers are royalty-free to download and use.

Implementing USB

USB, being an external bus, seems like the obvious way of building custom interfaces, and FTDI has some USB chips and development kits based on them that are ideal for one-off and low volume applications.

The development kits are particularly easy to use, and lend themselves to implementing anything from a custom interface for an existing piece of equipment to a general purpose I/O device. The USB device drivers supplied can be used from your own programs in C, Java, Visual Basic or any language that can talk to a COM port or work with a DLL.

Starting with the raw chips we have two types – the FT232BM is a USB to RS232 chip, and the FT245BM is a USB to parallel chip. You can buy these chips to build into your own products, but for one-off or custom designs you are more likely to find the same chips built into evaluation modules of more use.

USB development modules

The two main evaluation modules are the DLP-USB232M, which is a USB to TTL Serial module, and the DLP-USB245M, which is a USB to TTL Parallel module. Both modules take the form of 32-pin DIL plugs. This means that you can build them into your own projects and treat them as if they were 32-pin chips. A USB type-B connector is built into the boards, and they are both self-powered from the USB bus. You can also attach an EEPROM, which can hold USB identification values. For many applications, however, this can simply be ignored. Both modules support USB 1.1, and are USB 2 compatible.

The only difference between the modules is that DLP-USB232M has I/O pins that correspond to a standard RS-232 connector, and DLP-USB245M has pins that correspond to an 8-bit bi-directional parallel I/O. Clearly DLP-USB232M is ideal for converting existing RS232 devices to USB, but DLP-USB245M is capable of being used in a wider range of applications. In essence using DLP-USB232M is just a matter of soldering it to the existing device's RS232 port and then moving on to the software! Notice that one disadvantage of using the serial version to implement a new device is that it is limited to less than 1Mbit/s using RS232, and 3Mbits/s using RS485 or TTL. The parallel version can reach 1Mbyte/s. The rest of the description will concentrate on DLP-USB245M, but most of what is said applies equally to DLP-USB232M.

Using DLP-USB245M

From the hardware point of view using DLP-USB245M couldn't be easier. There are four "hand-shake" lines, which control data transfer, and eight data lines. There are two internal FIFO – First In First Out buffers – one for received data (128 bytes), and one for transmitted data (384 bytes). The FIFO buffers mean that both the PC and your device can more or less just pass data to the module and wait for it to be processed. The TXE# and RXF# pins indicate the state of the FIFO buffers. TXE# is high when the transmit buffer is full or busy, i.e. it only writes data when this is low. RXF# is low when there is data in the receive FIFO ready to be read. To control the data transfer we need to use the other two handshake pins – RD#, which when low places the first data byte in the receive buffer on the output pins, and WR, which when low reads the data byte into the transmit buffer. When both are high the data pins are in a high impedance state.

You can see that it would be very easy to hook the device up to a microprocessor like a PIC, but you don't have to be as complicated as this if all you want is a dumb I/O port. In this case, all you need is a couple of latches and logic gates. If you only want an output you don't even need this much – a single capacitor will do the job. There are a number of useful application notes at the FTDI website that explain the hardware interfacing requirement.

The drivers

If the hardware is so easy, perhaps this is at the cost of the software-part of the problem! In fact, the difficulty level on the software side turns out to be largely dependent on choices that you make. A set of serial port drivers is available for Windows 98/Me/NT/2000 and XP in either plug-and-play or non-plug-and-play format. The plug-and-play drivers interrogate any P&P serial device on the other end of the connection, which makes it easy to maintain the functioning of any existing P&P devices. Serial drivers are also available for Apple OS/8 and OS/9 and Linux.

Once you have the appropriate serial driver installed, you can talk to your USB device as if it was a serial port. Of course this makes more sense if you are using the DLP-USB232M or the corresponding chip, as these implement USB to RS232, and so working in RS232 on the PC fits in with the final data format, but it even makes sense in terms of the TTL parallel output as long as you regard the FIFO buffers as being equivalent to a sort of serial I/O facility.

When you install the USB COM port drivers a new COM port is activated when the USB device is connected. You can change the COM port number using the control panel but this is the COM port you use to exchange data with your USB device. The parallel output device simply ignores any baud rate setting and works at full speed! This behaviour will be familiar to any user of a serial port USB device.

If you want to do things properly then you need to install the Direct driver for Windows. This consists of a WDM driver and a DLL that allows you to use the driver from any language that can call DLLs, i.e. most. There are downloadable examples in VB, C++, and Delphi and these can form the basis for your own programs. Why bother with the more complicated WDM driver approach? The simple answer is that it gives you a great deal more control. The devious answer is that it makes your USB device look like a true USB device and not a serial device pretending to be a USB device!

Using the WDM drivers is still easy. First you have to call an open routine, FTOpen, after which you can read and write using the FTRead and FT_Write calls. In addition you have a reset function, a purge function which clears the FIFO buffers, a status function and a function which will list all the devices on the bus. The key factor is that this is all very easy and still manages to protect you from the complexities of USB!

Other costs

The range of USB modules from FTDI has made it possible to consider building your own USB devices or upgrading existing devices to make use of USB. One word of warning is needed, however, if you have any plans to move on to make your idea commercially available. In this case you need your own unique ID numbers – a VID and PID – which you program into the EEprom. This isn't technically difficult, but you have to join the USB Forum (at a cost of $2500) to get a number, or at the very least pay a one-off $1500 fee for an ID.

All of the devices discussed are available from A single USBDLP-USB245M costs approximately £20, and the software is all freely available for download from the web site.

Harry Fairhead is a hardware expert and author of a number of books and articles on computer architectures. His current interests include systems and embedded programming.

You might also like...



Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”