How machines talk.
Look along the edge of a laptop, or at the back of the box that brings the internet into a house, and you find a row of sockets, each with a name made of letters and numbers. Inside the machine, between one chip and the next, there are more links of the same kind with no sockets at all. Every one of them is a way of getting bits from here to there over wires, and every one answers the same four questions. How many wires are there? Who keeps the time? Who is allowed to speak? How far does it reach, and how fast?
The last three chapters supplied the parts. Chapter 26 said what a one and a zero are on a wire. Chapter 27 said why two wires that disagree carry bits farther and faster than one, and chapter 28 said what fast edges cost. This chapter is a tour of the links that people built from those parts, and it begins with the simplest one there ever was.
1. A wire for every bit the parallel port
For twenty years nearly every personal computer had a wide socket on its back with 25 holes in it, meant for a printer. Eight of its pins carried data, one bit to a pin. Picture a wall plate with eight light switches side by side, and a hand that can set all eight at once. A program wrote a number to the port, and the eight pins took on the bits of that number together, with 5 volts for a one and 0 volts for a zero. That is the whole of it, and chapter 26 has already said everything there is to say about the signal.
Because it was so plain, the printer port was the place where a great many people first made a computer do something out in the world. A small lamp of the kind called a light-emitting diode, wired from one of those pins through a resistor to ground, lit up whenever the bit on that pin was a one. One line of a program turned it on and another turned it off. Eight pins ran eight lamps. With a transistor to lend each pin some strength, they ran relays, motors and the track switches of a model railroad. Other pins of the socket worked the opposite way and let the program read a push button. I was one of those people. Growing up, I wrote a few lines of Visual Basic, wired a lamp to a pin, and watched it come on when the program said so. It was great fun.
The drawing is that port, with a lamp on each data pin. Inside the computer the port usually answered to the number 888, so the one line of the program says to send a value out to 888. Change the value, or press the pins one at a time.
A wire for every bit is easy to understand, and it is quick, since all eight bits arrive together. As a way of moving data it met the fate that chapter 27 described. As speeds rose, the eight bits stopped arriving together, and the cables grew thick and short. Nearly every link since then has sent its bits one after another down as few wires as it can.
As a way of switching things on and off, it is with us still. The small computers on a single board that students and engineers build with today each carry a row of pins for exactly this purpose. A program sets a pin high or low, or reads whether something outside has done so. They are called general-purpose input and output pins, or GPIO for short, and the first thing nearly everyone does with one is light a lamp. The rest of this chapter is about sending data, and the different ways of doing it over a few wires.
2. One wire and no clock the UART
Send bits one after another, and a new problem appears at once. The receiver sees a wire that is high for a while and then low for a while. How many ones was that? Three ones in a row look exactly like one long one, unless both ends agree on how long a bit lasts.
The oldest answer comes down from the teleprinters of a century ago, and it is an agreement made beforehand. Both ends are set to the same speed, counted in the baud of chapter 28, and each keeps time by a clock of its own. A common setting is 9,600, and since each symbol here is one bit, that is 9,600 bits a second. Two musicians in separate rooms could play together in the same way for a few bars, if they had agreed on the tempo and someone gave them both the downbeat.
The downbeat is called the start bit. The wire rests high. To send a letter, the sender first pulls the wire low for one bit time. The receiver has been waiting for exactly that fall, and it starts its own stopwatch there. It then looks at the wire in the middle of each of the next eight bit times and writes down what it finds. The sender finishes with one bit time of high, called the stop bit, so that the next start bit has something to fall from.
The two clocks need to agree only roughly, because the stopwatch is started afresh for every letter. The two musicians would drift apart over a long piece, and over ten notes they hardly can. An error in the receiver's clock has just ten bit times in which to build up. As long as the last look still lands inside the last bit, the letter is read correctly. The circuit that does all of this is called a UART, and nearly every small computer has one. The letters stand for universal asynchronous receiver and transmitter, and asynchronous is the engineer's word for working without a shared clock.
In the drawing a letter goes down the wire, and the marks show where the receiver looks. Choose a letter, and then let the receiver's clock run fast or slow.
A link of this kind needs three wires in all, one for each direction and a ground between the two machines. The two data wires are marked TX and RX, for transmit and receive, and the TX of each machine goes to the RX of the other. The link joins exactly two machines. A wrong bit looks just like a right one to it. Some links spend a ninth bit, called the parity bit, to say whether the number of ones in the letter was odd or even, and that catches any single wrong bit.
After all these years it is still the first thing an engineer connects to a new circuit board, because it is usually the first thing on the board to work.
3. A wire for the clock SPI, with its MOSI and MISO
Between chips that sit a finger's width apart on one board there is an easier way, which is to add a wire that carries the clock. The musicians now have a drummer whom both can hear. The chip in charge ticks that wire, and on every tick both chips know that one more bit has gone by. Nobody has to agree on a speed beforehand, and the speed can be whatever the slower chip can stand, often tens of millions of bits a second.
The commonest arrangement of this kind is called SPI, for serial peripheral interface, and it uses four wires. One is the clock, marked SCK. One carries data out from the chip in charge, and its name is MOSI. One carries data back, and its name is MISO. The last is a select wire, marked CS or SS. There is one of these for each chip that might be spoken to, and the chip in charge pulls it low to say which of them it means.
The names were coined in the 1980s, when the chip in charge was called the master and the others its slaves, so that MOSI stood for master out, slave in, and MISO for master in, slave out. Newer drawings call the two the controller and the peripheral. They label the same wires COPI and CIPO, or else SDO and SDI, for serial data out and serial data in. The older letters are still printed on a great many boards.
What happens on the wires is a trade, and it works like a revolving door, which lets one person out for each person it lets in. Each chip holds its byte in a row of eight cells. On every tick the top bit of each row goes out onto its wire, the other seven bits move up one place, and the bit arriving from the other chip drops into the bottom. The two rows are joined in a ring, and after eight ticks the two bytes have changed places.
SPI is quick and simple, and it takes everything on trust. Who is on the other end, whether the far chip heard, and what the bytes mean are all left for the two chips to have settled beforehand. It also spends a select wire on every chip, which is the price of having no addresses.
4. Two wires for a whole board I2C
A board with a dozen small chips on it, a thermometer here and a clock there, would need a great many wires if each chip had four of its own. A second arrangement serves all of them with two. It is written I2C and said aloud as I squared C, and the letters stand for inter-integrated circuit, which means between chips. One wire carries the clock and is marked SCL. The other carries the data and is marked SDA. Every chip on the board is joined to both.
Two things make the sharing possible. The first is an address, and it works like a roll call. The teacher reads out one name, the whole room hears it, and one voice answers. Each chip has a number of seven bits built into it, and every conversation opens with the chip in charge calling out the number of the chip it wants.
The second lies in the way the wires are driven. Each wire is held up to the supply voltage by a resistor, and a chip is only ever allowed to pull it down. It is like the cord along the windows of a city bus that rings the bell for the next stop. Any passenger may pull it, and two who pull at the same moment ring the same bell. Two ordinary outputs that fought over a wire, one driving it high and the other low, would make a short circuit.
The same arrangement gives the listener a voice. The chip in charge sends the seven bits of the address, and one more bit to say whether it means to write to the chip or to read from it. Then it lets go of the data wire for one tick and watches. If a chip with that address is there, it pulls the wire low for that one tick, which is its way of answering here. If the wire stays high, nobody is home.
The drawing has three chips on the two wires, and underneath it shows what the two wires carry from one moment to the next. A conversation begins when the data wire falls while the clock wire is high, and it ends when the data wire rises while the clock wire is high. Neither of those happens at any other time, so every chip can tell where a conversation starts and stops. In between, the data wire changes only while the clock is low, and each pulse of the clock is the moment to read it. Call each chip by its address, and then call an address that none of them owns.
The price of sharing is speed and distance. A wire that is only pulled up by a resistor rises slowly, and the more chips and the more wire hang on it, the more slowly it rises. I2C ordinarily runs at 100,000 or 400,000 bits a second, and it stays within one circuit board.
5. Out of the box and down the hall RS-232, RS-422 and RS-485
The three links above live inside one machine. The first standard links between machines kept the frame of section 2, with its start bit and its stop bit, and changed only what the bits look like on the wire. Their names all begin with RS, for recommended standard, and an American trade association handed the numbers out as it went along.
RS-232, from the early 1960s, is the serial port of every old computer and modem. It deals with noise in the plainest way there is, which is to speak up. It also turns the signal over. A one arrives as anything from 3 to 15 volts below ground, and a zero as the same amount above it, on a single wire against a shared ground. It joins two machines across a room.
RS-422 sends each signal on a pair of wires that disagree, which is the differential pair of chapter 27, twisted in the way chapter 31 describes. It is chapter 27's seesaw on a boat, where the message is in the tilt and the waves lift both ends alike. One sender can then be heard by as many as ten receivers, more than a kilometer away.
RS-485 goes one step further and lets every machine on the pair send as well as listen, so long as they take turns. Thirty-two machines can share one pair of wires, with a resistor across each end of the pair to swallow the echoes of chapter 14. Most of the machinery in a factory talks this way, and so do the lights above a concert stage. It is the link that chapter 31 finds in trouble beside a motor drive.
The drawing sends the letter K of section 2 three ways, and it shows the volts as the receiver finds them, measured from the receiver's own ground. Two machines in different rooms seldom agree exactly on where ground is. Let their grounds drift apart, and see which kind of wire still gets the letter through.
That is the whole difference between the three. On a board every chip stands on the same ground. RS-232 meets a drifting ground with larger signals, which is enough across a room. The pair carries its own reference with it, in its second wire, and that is enough across a factory. Even the pair has limits, drawn as the two dashed lines. Its receiver promises to work only while both wires stay between 7 volts below its own ground and 12 volts above, and chapter 31 tells what happens beside a motor drive when they are carried outside.
| link | a one and a zero | wires for one direction | who is on it | reach |
|---|---|---|---|---|
| RS-232 | 3 to 15 volts below ground, and the same above it | one wire and a shared ground | two machines | about 15 meters |
| RS-422 | the pair differs by 2 volts or more, one way or the other | one pair | one sender and up to ten listeners | 1,200 meters at low speeds |
| RS-485 | the pair differs by 1.5 volts or more, and a receiver needs only 0.2 | one pair, used both ways by turns | 32 machines, any of which may send | 1,200 meters at low speeds |
6. USB
The Universal Serial Bus arrived in 1996 to replace the serial port, the parallel port and several others with one socket. Its first cable holds four wires. Two of them bring 5 volts of power and a ground, which is why a USB socket can charge a phone. The other two are a single differential pair.
USB has no clock wire, and no speed is agreed by hand. The receiver finds the sender's rhythm in the edges of the data itself, the way a listener can tap a foot to a song with no drummer in sight. The sender is required to make an edge at least once in every seven bits, so that there is always a beat to follow. One pair carries the traffic in both directions, by turns.
USB also settles the question of who may speak, and it settles it firmly. It is the roll call of section 4 again, on a larger scale. One machine, called the host, is in charge. Every keyboard, camera and memory stick speaks only when the host asks it to, and as many as 127 of them can hang from one host through a tree of hubs.
The one name has covered a long climb in speed.
| year | the name on the box | bits a second | how it is done |
|---|---|---|---|
| 1996 | USB, low speed and full speed | 1.5 million and 12 million | one pair, used both ways by turns |
| 2000 | USB 2.0, high speed | 480 million | the same pair, with smaller and quicker swings |
| 2008 | USB 3.0 | 5 billion | two more pairs, one for each direction, beside the old one |
| 2013 | USB 3.1 | 10 billion | the same pairs, driven twice as fast |
| 2017 | USB 3.2 | 20 billion | two pairs in each direction, which takes the newer plug |
| 2019 | USB4 | 40 billion | the same four pairs, faster again |
| 2022 | USB4, version 2 | 80 billion | three levels on the wire where there had been two |
The small oval plug called USB-C, from 2014, has 24 contacts. Among them are four fast pairs, the original slow pair, and power wires that can now deliver as much as 240 watts, which is enough to run a large laptop. It goes in either way up because its contacts are laid out the same from both sides.
7. Ethernet
Ethernet began in 1973 as one thick cable that every computer in a building was clamped onto, and its first rule was a matter of good manners. Listen before you speak, and if two speak at once, both stop and each waits a random moment before trying again. That shared cable is gone. Since 1990 the usual Ethernet has been the cable of chapter 31, with four twisted pairs in one jacket, and it runs from each machine to a box called a switch. A switch works like a mail room. It reads the address on the front of each message and passes the message on toward that machine alone.
Three things set Ethernet apart from USB. The first is that nobody is in charge. Any machine may send whenever it has something to send, and every message carries the address, 48 bits long, of the machine it is meant for. The second is reach, for the cable can be 100 meters long. The third is that at each end the pairs pass through small transformers. The signal crosses a transformer by magnetism, as a voice crosses a closed window, and no wire runs straight from the circuits of one machine into the circuits of another. Two machines in different rooms, or in different buildings, can then stand on different grounds without harm, which is the trouble of chapter 31 designed away from the start.
| year | bits a second | pairs in use | how each bit is sent |
|---|---|---|---|
| 1990 | 10 million | two of the four, one for each direction | every bit has an edge in its middle, and the edges carry the clock |
| 1995 | 100 million | two, one for each direction | three levels on the wire, with every four bits sent as five |
| 1999 | 1 billion | all four, each in both directions at once | five levels on each pair |
| 2006 | 10 billion | all four, each in both directions at once | sixteen levels on each pair |
The step of 1999 deserves a second look. Each end talks and listens on the same pair at the same moment. It can do so because it knows exactly what it is sending, and it subtracts that from what it hears. Headphones that cancel noise work by the same subtraction. Chapter 28 had the other half of the method, which is to carry more than one bit on each step of the signal by using more than two levels. The table stops at copper. Ethernet also runs over glass fiber, faster and farther than any of this.
USB and Ethernet are alike in the ways that matter to the earlier chapters. Both use differential pairs, both find the clock in the data, and both send their bits in packets with a check on the end, so that a damaged packet is recognized and thrown away, to be sent again. They differ in who is in charge and in how far they reach. USB is one host with its belongings, a few meters apart, fed with power down the same cable. Ethernet is a crowd of equals, a hundred meters apart, each kept electrically to itself.
The drawing follows one piece of news through each of them. Choose a link, and then step through what happens.
8. The same four questions
| link | wires | who keeps the time | who may speak | how fast | how far |
|---|---|---|---|---|---|
| parallel port | 8 for data and a dozen more | a wire that says "now" | the computer | tens of thousands of bytes a second | a few meters |
| UART | 1 each way and a ground | a clock at each end, set alike beforehand | either of two | 9,600 to about a million bits a second | a meter or two |
| SPI | 4 | a clock wire | the chip in charge | tens of millions | one circuit board |
| I2C | 2 | a clock wire | the chip in charge, by address | 100,000 to a few million | one circuit board |
| RS-232 | 1 each way and a ground | set alike beforehand | either of two | up to about 115,000 | 15 meters |
| RS-485 | 1 pair | set alike beforehand | any of 32, by turns | 100,000 at full reach | 1,200 meters |
| USB | 1 pair and power, later 5 pairs | found in the data | whoever the host asks | 1.5 million to 80 billion | a few meters |
| Ethernet | 2 or 4 pairs | found in the data | anyone | 10 million to 10 billion | 100 meters |
Read down any column and the trades are plain to see. A clock wire buys speed and costs distance. A pair buys distance. Sharing wires costs speed, or else it needs a rule about turns. Every link in the table is one way of making those trades, chosen for a particular job.
9. The arithmetic and a program
eight pins the number written = 128 x D7 + 64 x D6 + ... + 2 x D1 + D0 37 lights D5, D2 and D0 one bit lasts 1 / bits a second at 9,600: 104 millionths of a second one letter takes 10 bit times: a start bit, 8 bits and a stop bit at 9,600: 960 letters a second the clocks may differ by about 5 parts in 100, because the tenth look must still land in the tenth bit a clocked trade 8 ticks move 8 bits each way at once 16 ticks bring both bytes home an address 7 bits, so 128 numbers, of which 112 are free for chips to use RS-232 a one is 3 to 15 volts below ground, and a zero is 3 to 15 volts above it a differential pair a one or a zero is which wire is higher, by 0.2 volts or more
The program sends the word Hi the way section 2 describes. Then it reads the letter H back with the receiver's clock in step, a little fast, too fast and too slow.
#include <stdio.h>
#include <string.h>
/* A byte as it goes down the wire: a start bit that pulls the resting line low, the eight bits with the
least first, and a stop bit that lets the line rest high again. */
void sent_frame(int byte, int levels[10])
{
levels[0] = 0;
for (int k = 0; k < 8; k++) levels[k + 1] = byte >> k & 1;
levels[9] = 1;
}
/* What a receiver makes of a frame when its clock runs `fast` percent fast. It looks at the line where it
believes the middle of each bit to be, and adds up the eight bits it finds. Returns 1 if the start and
stop bits were found where they belong. */
int read_frame(const int levels[10], double fast, int *byte)
{
int found[10];
for (int k = 0; k < 10; k++) {
double at = (k + 0.5) / (1.0 + fast / 100.0);
found[k] = at < 10.0 ? levels[(int)at] : 1;
}
*byte = 0;
for (int k = 0; k < 8; k++) *byte |= found[k + 1] << k;
return found[0] == 0 && found[9] == 1;
}
int main(void)
{
const int baud = 9600; /* bits a second, agreed beforehand at both ends */
const char *word = "Hi";
const double clocks[] = { 0.0, 4.0, 6.0, -6.0 };
int levels[10], byte;
printf("letter code start, eight bits least first, stop\n");
for (size_t i = 0; i < strlen(word); i++) {
sent_frame(word[i], levels);
printf(" %c %4d %d ", word[i], word[i], levels[0]);
for (int k = 1; k < 9; k++) printf("%d", levels[k]);
printf(" %d\n", levels[9]);
}
printf("each letter takes %.2f thousandths of a second, so %d letters go by in a second\n", 10 * 1000.0 / baud, baud / 10);
printf("receiver's clock reads the letter H as\n");
sent_frame('H', levels);
for (int i = 0; i < 4; i++) {
if (!read_frame(levels, clocks[i], &byte)) printf("%+8.1f percent nothing: no stop bit was found\n", clocks[i]);
else if (byte == 'H') printf("%+8.1f percent H\n", clocks[i]);
else printf("%+8.1f percent a wrong byte (%d), with no warning\n", clocks[i], byte);
}
return 0;
}
/* It prints:
letter code start, eight bits least first, stop
H 72 0 00010010 1
i 105 0 10010110 1
each letter takes 1.04 thousandths of a second, so 960 letters go by in a second
receiver's clock reads the letter H as
+0.0 percent H
+4.0 percent H
+6.0 percent nothing: no stop bit was found
-6.0 percent a wrong byte (200), with no warning */
def sent_frame(byte):
"""A byte as it goes down the wire: a start bit that pulls the resting line low, the eight bits with the
least first, and a stop bit that lets the line rest high again."""
return [0] + [byte >> k & 1 for k in range(8)] + [1]
def read_frame(levels, fast=0.0):
"""What a receiver makes of a frame when its clock runs `fast` percent fast. It looks at the line where it
believes the middle of each bit to be, and adds up the eight bits it finds."""
found = [levels[int((k + 0.5) / (1 + fast / 100))] if (k + 0.5) / (1 + fast / 100) < len(levels) else 1 for k in range(10)]
return sum(bit << k for k, bit in enumerate(found[1:9])), found[0] == 0 and found[9] == 1
BAUD = 9600 # bits a second, agreed beforehand at both ends
print("letter code start, eight bits least first, stop")
for letter in "Hi":
frame = sent_frame(ord(letter))
print(f" {letter} {ord(letter):4d} {frame[0]} {''.join(map(str, frame[1:9]))} {frame[9]}")
print(f"each letter takes {10 * 1000 / BAUD:.2f} thousandths of a second, so {BAUD // 10} letters go by in a second")
print("receiver's clock reads the letter H as")
for fast in (0.0, 4.0, 6.0, -6.0):
byte, framed = read_frame(sent_frame(ord("H")), fast)
told = "nothing: no stop bit was found" if not framed else "H" if byte == ord("H") else f"a wrong byte ({byte}), with no warning"
print(f"{fast:+8.1f} percent {told}")
# It prints:
# letter code start, eight bits least first, stop
# H 72 0 00010010 1
# i 105 0 10010110 1
# each letter takes 1.04 thousandths of a second, so 960 letters go by in a second
# receiver's clock reads the letter H as
# +0.0 percent H
# +4.0 percent H
# +6.0 percent nothing: no stop bit was found
# -6.0 percent a wrong byte (200), with no warning
/// A byte as it goes down the wire: a start bit that pulls the resting line low, the eight bits with the
/// least first, and a stop bit that lets the line rest high again.
fn sent_frame(byte: u8) -> [u8; 10] {
let mut levels = [0u8; 10];
for k in 0..8 {
levels[k + 1] = byte >> k & 1;
}
levels[9] = 1;
levels
}
/// What a receiver makes of a frame when its clock runs `fast` percent fast. It looks at the line where it
/// believes the middle of each bit to be, and adds up the eight bits it finds. The second answer says whether
/// the start and stop bits were found where they belong.
fn read_frame(levels: &[u8; 10], fast: f64) -> (u8, bool) {
let mut found = [1u8; 10];
for k in 0..10 {
let at = (k as f64 + 0.5) / (1.0 + fast / 100.0);
if at < 10.0 {
found[k] = levels[at as usize];
}
}
let byte = (0..8).fold(0u8, |sum, k| sum | found[k + 1] << k);
(byte, found[0] == 0 && found[9] == 1)
}
fn main() {
let baud = 9600; // bits a second, agreed beforehand at both ends
println!("letter code start, eight bits least first, stop");
for letter in "Hi".bytes() {
let frame = sent_frame(letter);
let bits: String = frame[1..9].iter().map(|bit| bit.to_string()).collect();
println!(" {} {:4} {} {} {}", letter as char, letter, frame[0], bits, frame[9]);
}
println!("each letter takes {:.2} thousandths of a second, so {} letters go by in a second", 10.0 * 1000.0 / baud as f64, baud / 10);
println!("receiver's clock reads the letter H as");
for fast in [0.0, 4.0, 6.0, -6.0] {
let (byte, framed) = read_frame(&sent_frame(b'H'), fast);
if !framed {
println!("{:+8.1} percent nothing: no stop bit was found", fast);
} else if byte == b'H' {
println!("{:+8.1} percent H", fast);
} else {
println!("{:+8.1} percent a wrong byte ({}), with no warning", fast, byte);
}
}
}
// It prints:
// letter code start, eight bits least first, stop
// H 72 0 00010010 1
// i 105 0 10010110 1
// each letter takes 1.04 thousandths of a second, so 960 letters go by in a second
// receiver's clock reads the letter H as
// +0.0 percent H
// +4.0 percent H
// +6.0 percent nothing: no stop bit was found
// -6.0 percent a wrong byte (200), with no warning
Look at the last two lines. A receiver that runs too fast finds no stop bit and knows that something is wrong. One that runs too slow reads a wrong letter and is none the wiser, which is why careful links add a check of their own.
Try it: sockets, contacts and a clock that drifts
- Go along the sockets of a computer and name the link behind each one. Then answer the four questions for it from the table in section 8.
- Look into the end of a USB-C plug with a magnifying glass. The tiny contacts run in two rows of twelve places, one row above the other, and a cable made only for charging leaves some of the places empty. An older flat USB plug has four wide contacts, and five more hide at the back of it if the plastic inside is blue.
- Hold a network plug up to the light. Its eight wires show their colors through the clear plastic, a solid color and a striped one for each of the four pairs.
- In the drawing of section 2, send the letter U, which is ones and zeros by turns, and move the receiver's clock a half step at a time until the letter is misread. Then try it the other way.
- In the drawing of section 5, find how far the grounds can drift before each of the three wires loses its first bit.
That is the end of the tour inside the computer. The next part goes outdoors, to the largest wires there are.