Overview
Overview
Overview
Overview
Embedded computer systems literally surround us: they're in our cell phones, PDAs, cars,
TVs, refrigerators, heating systems, and more. In fact, embedded systems are one of the most
rapidly growing segments of the computer industry today.
Along with the growing list of devices for which embedded computer systems are appropriate,
interest is growing among programmers, hobbyists, and engineers of all types in how to
design and build devices of their own. Furthermore, the knowledge offered by this book into
the fundamentals of these computer systems can benefit anyone who has to evaluate and
apply the systems.
The second edition of Designing Embedded Hardware has been updated to include
information on the latest generation of processors and microcontrollers, including the new
MAXQ processor. If you're new to this and don't know what a MAXQ is, don't worry--the
book spells out the basics of embedded design for beginners while providing material useful
for advanced systems designers.
Designing Embedded Hardware steers a course between those books dedicated to writing
code for particular microprocessors, and those that stress the philosophy of embedded system
design without providing any practical information. Having designed 40 embedded computer
systems of his own, author John Catsoulis brings a wealth of real-world experience to show
readers how to design and create entirely new embedded devices and computerized gadgets,
as well as how to customize and extend off-the-shelf systems.
Loaded with real examples, this book also provides a roadmap to the pitfalls and traps to
avoid. DesigningEmbeddedHardwareincludes:
The theory and practice of embedded systems
Understanding schematics and data sheets
Powering an embedded system
Producing and debugging an embedded system
Processors such as the PIC, Atmel AVR, and Motorola 68000-series
Digital Signal Processing (DSP) architectures
Protocols (SPI and I2C) used to add peripherals
RS-232C, RS-422, infrared communication, and USB
CAN and Ethernet networking
Pulse Width Monitoring and motor control
If you want to build your own embedded system, or tweak an existing one, this invaluable
book gives you the understanding and practical skills you need.
Preface
Preface
Preface
Preface
[Enlightenment]residesascomfortablyinthecircuitsofadigitalcomputer...asatthetopof
amountainorinthepetalsofaflower.
—Robert M. Pirsig, ZenandtheArtofMotorcycleMaintenance
they meet all
Welcome to the second edition of DesigningEmbeddedHardware. In these pages, I hope to
give you an understanding of the design process for creating computer hardware. Just as there
is beauty in well-written software, there is beauty in well-designed hardware. With embedded
computers, you get to understand the machine at all levels, at once aware of currents flowing
through circuit traces and software executing complex algorithms. In fact, it is not possible to
write embedded software without understanding the hardware, nor is it possible to design
hardware without understanding software. You become involved with the machine to a degree
beyond that which is possible with desktop computers. Best of all, it's a lot of fun.
In selecting chips and designs for this book, I have tried to choose, where possible, parts that
are both trivial to use yet exceptionally useful. I have no connection, financial or otherwise,
with any of the companies or businesses mentioned in this book, and I receive no benefits
from them. Every component or product included in this book is there based on its own merits.
You may notice a prevalence of components from certain manufacturers. This simply reflects
my personal preference for using their chips, based on my experience. Such companies
produce chips that are easy to use, are reliable and robust, have great technical support, and
provide thorough and comprehensive technical data. In other words,
the
necessary requirements for inclusion in a book for beginners.
When the first edition of DesigningEmbeddedHardware was published, I deliberately left
out software. There were two reasons for this. First, there are many good books already
written on C programming, embedded firmware development
in C, porting Linux to
embedded systems, coding in Python, writing Java software, and so on. (And of course, the
best of these are naturally published by O'Reilly.) The second reason is that assembly
language, that most basic of programming tools, is so different from processor to processor
that it would not have been possible to cover all the instruction sets of the processors in the
book, let alone do them justice. However, I have decided to include some software in this
edition. I won't even attempt to cover the instructions of each processor in this book. What I
will do is show some simple assembly language techniques. While the instructions may be
wildly different between architectures, the basic concepts are the same.
Also new to this book is a chapter on the Forth programming language. Forth is a relatively
old language that has faded from the forefront of software development, and as such, it's rare
to find a book giving the language good coverage. Forth is a very useful tool for embedded
system development to which many engineers have yet to be exposed. The language is the
basis of the Open Firmware standard and is used by design engineers at Apple, Sun, and many
other manufacturers. It's a useful language to know, and it is worth taking the time to learn.
Many of the designs in this book look easy, and they are. They are intended as simple
building blocks, allowing you to mix and match to achieve the embedded systems you need. I
hope you will find this book useful. Once you've finished reading it, go and build something!
—John Catsoulis
Brisbane, Australia
January 2005
jtc@embedded.com.au
http://www.embedded.com.au
Organization
Organization ofofofof ThisThisThisThis BookBookBookBook
Organization
Organization
This book is informally divided into four sections. The first covers fundamental concepts and
introductory material. The second section gives an overview of assembly language and Forth.
From there, we'll look at peripherals and how to add functionality to your embedded systems.
Finally, we'll look at a variety of processors widely used in embedded systems, and look at the
design process for integrating them into computers.
Chapter 1 gives an overview of computer architectures and explains what constitutes an
embedded system. Chapters 2 and 3 explore software with assembly language and Forth.
Chapter 4 provides some background electronics theory and introduces some important
concepts. If you're already electronics-savvy, then you can skip on to Chapter 5, where we'll
look at providing power for your embedded system. We'll also look at how to protect your
embedded computer against electrical interference and other gremlins that can cause it grief.
In Chapter 6, you'll see how to physically produce and debug an embedded computer system.
Chapters 7 and 8 cover SPI and I2C, two protocols that allow a wide range of small
peripherals to be added to microcontrollers. Chapters 9, 10, and 11 cover serial interfaces.
These give our embedded system access to host computers and to external peripherals such as
modems. We'll look at RS-232C, RS-422, Infrared communication, and USB.
Networks are covered in Chapter 12, where we'll see how to add a low-cost industrial network
(CAN) to our embedded computer. Also in Chapter 12, we learn how to add an Ethernet port
to our embedded system, by which we can connect to other computers, servers, and gateways
and, through them, to the wider Internet.
In Chapter 13, we'll look at real-world interfacing. We'll see how to convert analog signals
into digital values for processing and, conversely, how to convert digital values back into
analog voltages. We'll also see how to interface sensors to our embedded system, whereby we
can measure temperature, light, pressure, acceleration and magnetic fields. Also in Chapter 13,
we'll look at Pulse Width Modulation and motor control. We'll see how to use an embedded
computer to control small electric motors.
Chapter 14 begins the microprocessor section of the book, where we'll look at the first of our
embedded processor architectures, the Microchip PIC. In subsequent chapters, we'll meet a
variety of processors, from tiny standalone, 8-bit microcontrollers to 32-bit, bus-based chips
with some computing grunt. While it is not possible to cover every embedded processor (as
there are literally many hundreds), the chips chosen are representative of various classes of
processor. The skills you learn will be adaptable to whatever processor you choose for your
application.
UsingUsingUsing CodeCodeCodeCode Examples
Examples
Examples
Using
Examples
This book is here to help you get your job done. In general, you may use the code in this book
in your programs and documentation. You do not need to contact O'Reilly for permission
unless you're reproducing a significant portion of the code. For example, writing a program
that uses several chunks of code from this book does not require permission. Selling or
distributing a CD-ROM of examples from O'Reilly books does require permission. Answering
a question by citing this book and quoting example code does not require permission.
Incorporating a significant amount of example code from this book into your product's
documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author,
publisher, and ISBN. For example: "Designing Embedded Hardware, by John Catsoulis.
Copyright 2005 O'Reilly Media, Inc., 0-596-00755-8."
If you feel your use of code examples falls outside fair use or the permission given above, feel
free to contact the publisher at permissions@oreilly.com.
Conventions
Conventions
Conventions
Conventions
The conventions used in this book are as follows:
Main text
Source Code
Signal
Signal
Signal (active high)
Signal
(active low)
Hexadecimal numbers are denoted with the prefix 0x, and sometimes with the prefix $, where
appropriate for certain processors.
Binary numbers are denoted by the prefix %.
K is 1,024, while k is 1,000.
This icon indicates a tip, suggestion, or general note.
This icon designates a caution or warning.
Safari
Enabled
Safari
Enabled
Safari®®®® Enabled
Safari
Enabled
When you see a Safari® Enabled icon on the cover of your favorite technology
book, it means the book is available online through the O'Reilly Network Safari Bookshelf.
Safari offers a solution that's better than e-books. It's a virtual library that lets you easily
search thousands of top technology books, cut and paste code samples, download chapters,
and find quick answers when you need the most accurate, current information. Try it for free
at http://safari.oreilly.com.
Contact
HowHowHowHow totototo Contact
Contact UsUsUsUs
Contact
Please address comments and questions concerning this book to the publisher:
O'Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
(800) 998-9938 (in the United States or Canada)
(707) 829-0515 (international/local)
(707) 829-0104 (fax)
There is a web page for this book, which lists errata, examples, and any additional
information. You can access this page at:
http://www.oreilly.com/catalog/dbhardware2/
To comment or ask technical questions about this book, send email to:
bookquestions@oreilly.com
To contact the author directly with comments or questions, send email to:
jtc@embedded.com.au
For more information about books, conferences, Resource Centers, and the O'Reilly Network,
see our web site at:
http://www.oreilly.com
Introduction totototo Computer
Introduction
Chapter 1.1.1.1. AnAnAnAn Introduction
Chapter
Computer
Chapter
Introduction
Computer
Chapter
Computer
Architecture
Architecture
Architecture
Architecture
Each machine has its own, unique personality which probably could be defined as the
intuitive sum total of everything you know and feel about it. This personality constantly
changes,usuallyfortheworse,butsometimessurprisinglyforthebetter...
—Robert M. Pirsig, Zen and the Art of Motorcycle Maintenance
This book is about designing and building specialized computers. We all know what a
computer is. It's that box that sits on your desk, quietly purring away (or rattling if the fan is
shot), running your programs and regularly crashing (if you're not running some variety of
Unix). Inside that box is the electronics that runs your software, stores your information, and
connects you to the world. It's all about processing information. Designing a computer,
therefore, is about designing a machine that holds and manipulates data.
Computer systems fall into essentially two separate categories. The first, and most obvious, is
that of the desktop computer. When you say "computer" to someone, this is the machine that
usually comes to her mind. The second type of computer is the embedded computer, a
computer that is integrated into another system for the purposes of control and/or monitoring.
Embedded computers are far more numerous than desktop systems, but far less obvious. Ask
the average person how many computers he has in his home, and he might reply that he has
one or two. In fact, he may have 30 or more, hidden inside his TVs, VCRs, DVD players,
remote controls, washing machines, cell phones, air conditioners, game consoles, ovens, toys,
and a host of other devices.
In this chapter, we'll look at computer architecture in general. This is applicable to both
embedded and desktop computers, because the primary difference between an embedded
machine and a general-purpose computer is its application. The basic principles of operation
and the underlying architectures are fundamentally the same.
Both have a processor, memory, and often several forms of input and output. The primary
difference lies in their intended use, and this is reflected in the system design and their
software. Desktop computers can run a variety of application programs, with system
resources orchestrated by an operating system. By running different application programs, the
functionality of the desktop computer is changed. One moment, it may be used as a word
processor; the next it is an MP3 player or a database client. Which software is loaded and run
is under user control.
In contrast, the embedded computer is normally dedicated to a specific task. In many cases,
an embedded system is used to replace application-specific electronics. The advantage of
using an embedded microprocessor over dedicated electronics is that the functionality of the
system is determined by the software, not the hardware. This makes the embedded system
easier to produce, and much easier to evolve, than a complicated circuit.
The embedded system typically has one application and one application only, which is
permanently running. The embedded computer may or may not have an operating system, and
rarely does it provide the user with the ability to arbitrarily install new software. The software
is normally contained in the system's nonvolatile memory, unlike a desktop computer where
the nonvolatile memory contains boot software and (maybe) low-level drivers only.
Embedded hardware is often much simpler than a desktop system, but it can also be far more
complex too. An embedded computer may be implemented in a single chip with just a few
support components, and its purpose may be as crude as a controller for a garden-watering
system. Alternatively, the embedded computer may be a 150-processor, distributed parallel
machine responsible for all the flight and control systems of a commercial jet. As diverse as
embedded hardware may be, the underlying principles of design are the same.
This chapter introduces some important concepts relating to computer architecture, with
specific emphasis on those topics relevant to embedded systems. Its purpose is to give you
grounding before moving on to the more hands-on information that begins in Chapter 2. In
this chapter, you'll learn about the basics of processors, interrupts, the difference between
RISC and CISC, parallel systems, memory, and I/O.
1.1.1.1.1.1.1.1. Concepts
Concepts
Concepts
Concepts
Let's start at the beginning.
In essence, a computer is a machine designed to process, store, and retrieve data. Data may be
numbers in a spreadsheet, characters of text in a document, dots of color in an image,
waveforms of sound, or the state of some system, such as an air conditioner or a CD player.
Alldataisstoredinthecomputerasnumbers. It's easy to forget this when we're deep in C
code, contemplating complex algorithms and data structures.
The computer manipulates the data by performing operations on the numbers. Displaying an
image on a screen is accomplished by moving an array of numbers to the video memory, each
number representing a pixel of color. To play an MP3 audio file, the computer reads an array
of numbers from disk and into memory, manipulates those numbers to convert
the
compressed audio data into raw audio data, and then outputs the new set of numbers (the raw
audio data) to the audio chip.
Everything that a computer does, from web browsing to printing, involves moving and
processing numbers. The electronics of a computer is nothing more than a system designed to
hold, move, and change numbers.
A computer system is composed of many parts, both hardware and software. At the heart of
the computer is the processor, the hardware that executes the computer programs. The
computer also has memory, often several different types in one system. The memory is used
to store programs while the processor is running them, as well as store the data that the
programs are manipulating. The computer also has devices for storing data, or exchanging
data with the outside world. These may allow the input of text via a keyboard, the display of
information on a screen, or the movement of programs and data to or from a disk drive.
The software controls the operation and functionality of the computer. There are many
"layers" of software in the computer (Figure 1-1). Typically, a given layer will only interact
with the layers immediately above or below it.
Software
Figure 1-1.1-1.1-1.1-1. Software
Figure
layers
Figure
Software
layers
Software layers
Figure
layers
At the lowest level, there are programs that are run by the processor when the computer first
powers up. These programs initialize the other hardware subsystems to a known state and
configure the computer for correct operation. This software, because it is permanently stored
in the computer's memory, is known as firmware.
The bootloader is located in the firmware. The bootloader is a special program run by the
processor that reads the operating system from disk (or nonvolatile memory or network
interface) and places it in memory so that the processor may then run it. The bootloader is
present in desktop computers and workstations, and may be present in some embedded
computers.
Above the firmware, the operating system controls the operation of the computer. It organizes
the use of memory and controls devices such as the keyboard, mouse, screen, disk drives, and
so on. It is also the software that often provides an interface to the user, enabling her to run
application programs and access her files on disk. The operating system typically provides a
set of software tools for application programs, providing a mechanism by which they too can
access the screen, disk drives, and so on. Not all embedded systems use or even need an
operating system. Often, an embedded system will simply run code dedicated to its task, and
the presence of an operating system is overkill. In other instances, such as network routers, an
operating system provides necessary software integration and greatly simplifies the
development process. Whether an operating system is needed and useful really depends on
the intended purpose of the embedded computer and, to a lesser degree, on the preference of
the designer.
software
software
At the highest level, the application software
software constitutes the programs that provide the
functionality of the computer. Everything below the application is considered systemsoftware.
For embedded computers, the boundary between application and system software is often
blurred. This reflects the underlying principle in embedded design that a system should be
designed to achieve its objective in as simple and straightforward a manner as possible.
1.1.1.
Processors
1.1.1.
Processors
1.1.1. Processors
1.1.1.
Processors
The processor is the most important part of a computer, the component around which
everything else is centered. In essence, the processor is the computing part of the computer. A
processor is an electronic device capable of manipulating data (information) in a way
specified by a sequence of instructions. The instructions are also known as opcodes or
machinecode. This sequence of instructions may be altered to suit the application, and, hence,
computers are programmable. A sequence of instructions is what constitutes a program.
Instructions in a computer are numbers, just like data. Different numbers, when read and
executed by a processor, cause different things to happen. A good analogy is the mechanism
of a music box. A music box has a rotating drum with little bumps, and a row of prongs. As
the drum rotates, different prongs in turn are activated by the bumps, and music is produced.
In a similar way, the bit patterns of instructions feed into the execution unit of the processor.
Different bit patterns activate or deactivate different parts of the processing core. Thus, the bit
pattern of a given instruction may activate an addition operation, while another bit pattern
may cause a byte to be stored to memory.
A sequence of instructions is a machine-code program. Each type of processor has a different
instruction set, meaning that the functionality of the instructions (and the bit patterns that
activate them) varies. Processor instructions are often quite simple, such as "add two
numbers" or "call this function." In some processors, however, they can be as complex and