Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Al Williams

Dr. Dobb's Bloggers

The USB Differential

July 23, 2011

Embedded systems often need to communicate with other systems of some kind. In the past, RS232 was a popular choice for communication with other computers (PCs or other embedded systems like smart sensors). The hardware was cheap and plentiful. A lot of CPUs had built-in support for doing the serial conversion and if not, it was easy to bit bang an RS232 port at logic levels completely in software. A little extra hardware for level conversion and you were good to go.

But the sun is setting on serial ports. The motherboard I just installed has a serial port, but no connector for it. Most of the motherboards I looked at didn't have one at all. I haven't seen a laptop with a serial port in a long time.

There are a few reasons for this, and a few solutions. The first is that you can find USB to serial adapters that are robust and will work in nearly all cases that you would use a serial port. You can also find PCI cards that provide serial ports. So even though I have my motherboard serial port disabled (I was too lazy to mount the connector in my case), it didn't matter because I have 2 PCI-based ports, and 9 (that's right... 9) USB serial ports, most of which come from an Edgeport USB 8 port adapter (these show up on eBay regularly).

This is fine for you and me. But if you are delivering a piece of hardware to end users, it might be too much to expect them to have a slew of serial ports on their modern computer. The predominant choice now is USB. However, most people choose to essentially build in a USB to RS232 cable — often in the form of the chips these cables use instead of an actual cable. There are many modules that offer this function, and they allow your embedded system to stay the same. I've used cheap no-name adapters from eBay and in production quantities I've used this little module.

If you are really worried about preserving your investment in RS232, you can even get a DB9 connector that has the cable built in.

However, these are halfway measures. Some CPUs are including USB support these days. And I am always surprised that more people are not bit banging USB. If you look around, some people are using the term "bit bang" to mean using a USB interface chip to provide discrete I/O. That's not what I mean here. What I mean is using software in a microprocessor to convert a few general-purpose I/O pins into a USB port.

It isn't as trivial as bit banging USB, but it can be done. Click here for one example. Click here for another.

One of the challenges to USB (and many other modern protocols) is that they are differential. I'll talk more about that next time. In a bit banged RS232 implementation, the inputs and outputs are single ended (and often inverted):

for (bitcount=0;bitcount<7;bitcount++)
   SETBIT(PORTC,SERIAL_OUT_BIT, (byte & (1<<bitcount))?0:1);

With a differential input (or output), two pins have to be in the opposite state which make it more complicated. For example:

   for (bitcount=0;bitcount<7;bitcount++)
       int state=(byte & (1<<bitcount))?0:1;
       SETBITS(PORTC, OUT_BIT_A, state, OUT_BIT_B, state?0:1);

I'll talk next time about why differential encoding is a good thing. The extra processing does put more demand on the processor though. In addition, USB requires things like bit stuffing and timing for NRZI decoding. This article has a good overview of the issues encountered by a developer building a software USB stack.

I miss the days when I could count on serial ports that didn't require me to mess with getting vendor IDs and worrying about driver support. But those days are nearly gone and we have to learn to move on to the new technologies.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.