First figure out why you want the students to learn the subject and what you want them to know, and the method will result more or less by common sense.
Richard Feynman
Embedded software is in almost every electronic device in use today. There is software hidden away inside our watches, DVD players, mobile phones, antilock brakes, and even a few toasters. The military uses embedded software to guide missiles, detect enemy aircraft, and pilot UAVs. Communication satellites, deep-space probes, and many medical instruments would’ve been nearly impossible to create without it.
Someone has to write all that software, and there are tens of thousands of electrical engineers, computer scientists, and other professionals who actually do. We are two of them, and we know from our personal experiences just how hard it can be to learn the craft.
Each embedded system is unique, and the hardware is highly specialized to the application domain. As a result, embedded systems programming can be a widely varying experience and can take years to master. However, one common denominator across almost all embedded software development is the use of the C programming language. This book will teach you how to use C in any embedded system.
Even if you already know how to write embedded software, you can still learn a lot from this book. In addition to learning how to use C more effectively, you’ll also benefit from the detailed explanations and source code associated with common embedded software problems. Among the advanced topics covered in the book are memory testing and verification, device driver design and implementation, real-time operating system internals, and code optimization techniques.
Each year, globally, approximately one new processor is manufactured per person. That’s more than six billion new processors each year, fewer than two percent of which are the Pentiums and PowerPCs at the heart of new personal computers. You may wonder whether there are really that many computers surrounding us. But we bet that within five minutes you can probably spot dozens of products in your own home that contain processors: televisions, stereos, MP3 players, coffee makers, alarm clocks, VCRs, DVD players, microwaves, dishwashers, remote controls, bread machines, digital watches, and so on. And those are just the personal possessions—many more such devices are used at work. The fact that every one of those products contains not only a processor, but also software, is the impetus for this book.
One of the hardest things about this subject is knowing when to stop writing. Each embedded system is unique, and we have therefore learned that there is an exception to every rule. Nevertheless, we have tried to boil the subject down to its essence and present the things that programmers definitely need to know about embedded systems.
This is a book about programming embedded systems in C. As such, it assumes that the reader already has some programming experience and is at least familiar with the syntax of the C language. It also helps if you have some familiarity with basic data structures, such as linked lists. The book does not assume that you have a great deal of knowledge about computer hardware, but it does expect that you are willing to learn a little bit about hardware along the way. This is, after all, a part of the job of an embedded programmer.
While writing this book, we had two types of readers in mind. The first reader is a beginner—much as we were once. He has a background in computer science or engineering and a few years of programming experience. The beginner is interested in writing embedded software for a living but is not sure just how to get started. After reading the first several chapters, he will be able to put his programming skills to work developing simple embedded programs. The rest of the book will act as a reference for the more advanced topics encountered in the coming months and years of his career.
The second reader is already an embedded systems programmer. She is familiar with embedded hardware and knows how to write software for it but is looking for a reference book that explains key topics. Perhaps the embedded systems programmer has experience only with assembly language programming and is relatively new to C. In that case, the book will teach her how to use the C language effectively in an embedded system, and the later chapters will provide advanced material on real-time operating systems, peripherals, and code optimizations.
Whether you fall into one of these categories or not, we hope this book provides the information you are looking for in a format that is friendly and easily accessible.
The book contains 14 chapters and 5 appendixes. The chapters can be divided quite nicely into two parts. The first part consists of Chapters 1 through 5 and is intended mainly for newcomers to embedded systems. These chapters should be read in their entirety and in the order that they appear. This will bring you up to speed quickly and introduce you to the basics of embedded software development. After completing Chapter 5, you will be ready to develop small pieces of embedded software on your own.
The second part of the book consists of Chapters 6 through 14 and discusses advanced topics that are of interest to inexperienced and experienced embedded programmers alike. These chapters are mostly self-contained and can be read in any order. In addition, Chapters 6 through 12 contain example programs that might be useful to you on a future embedded software project.
Explains the field of embedded programming and lays out the parameters of the book, including the reference hardware used for examples
Shows how to explore the documentation for your hardware and represent the components you need to interact with in C
Creates a simple blinking light application that illustrates basic principles of embedded programming
Goes over the ways that embedded systems differ from conventional computer systems during program building steps, covering such issues as cross-compilers
Introduces the tools you’ll need in order to iron out problems in both hardware and software
Describes the different types of memory that developers choose for embedded systems and the issues involved in using each type
Introduces the notion of a device driver, along with other coding techniques for working with devices
Covers this central area of working with peripherals
Combines the concepts and code from the previous chapter with convenience functions and a main program, to create a loadable, testable application
Introduces common operating system concepts, including tasks (or threads) and synchronization mechanisms, along with the reasons for adding a real-time operating system
Shows how to use some features of the eCos real-time operating system
Accomplishes the same task as the previous chapter, but for the embedded Linux operating system
Describes options for adding buses, networking, and other communication features to a system
Describes ways to decrease code size, reduce memory use, and conserve power
Describes the board used for the examples in this book and how to order one for yourself
Gives instructions for loading the software described in this book on your host Windows or Linux computer
Shows you how to compile the GNU development tools
Shows you how to build an eCos library appropriate for your embedded system so you can compile programs to run on your system
Describes how to install the embedded Linux tools for your Arcom system and build and run a program on it
Throughout the book, we have tried to strike a balance between specific examples and general information. Whenever possible, we have eliminated minor details in the hope of making the book more readable. You will gain the most from the book if you view the examples, as we do, primarily as tools for understanding important concepts. Try not to get bogged down in the details of any one circuit board or chip. If you understand the general C programming concepts, you should be able to apply them to any embedded system you encounter.
To focus the book’s example code on specific concepts, we intentionally left it incomplete—for example, by eliminating certain include files and redundant variable declarations. For complete details about the code, refer to the full example source code on the book’s web site.
The following typographical conventions are used throughout the book:
Indicates names of files, programs, methods, and options when they appear in the body of a paragraph. Italic is also used for emphasis and to introduce new terms.
Constant Width
In examples, indicates the contents of files and the output of commands. In regular text, this style indicates keywords, functions, variable names, classes, objects, parameters, and other code snippets.
Constant Width
Bold
Indicates commands and options to be typed literally. This style is used in examples only.
Constant Width Bold
Italic
Indicates text to be replaced with user values; for example, a filename on your system. This style is used in examples only.
This symbol is used to indicate a tip, suggestion, or general note.
This symbol is used to indicate a warning.
Other conventions relate to gender and roles. With respect to gender, we have purposefully used both “he” and “she” throughout the book. With respect to roles, we have occasionally distinguished between the tasks of hardware engineers, embedded software engineers, and application programmers. But these titles refer only to roles played by individual engineers, and it should be noted that it can and often does happen that a single individual fills more than one of these roles on an embedded-project team.
This book includes many source code listing, and all but the most trivial snippets are available online. These examples are organized by chapter number and include build instructions (makefiles) to help you recreate each of the executables. The complete archive is available at http://examples.oreilly.com/embsys2.
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 us 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: “Programming Embedded Systems with C and GNU Development Tools, Second Edition, by Michael Barr and Anthony Massa. Copyright 2007 O’Reilly Media, Inc., 978-0-596-00983-0.”
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com.
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 or local) |
707-829-0104 (fax) |
We have a web page for this book, where we list errata, code examples, and any additional information. Corresponding files for code examples are mentioned on the first line of the example. You can access this page at:
http://www.oreilly.com/catalog/9780596009830 |
To comment or ask technical questions about this book, send email to:
bookquestions@oreilly.com |
For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our web site at:
http://www.oreilly.com |
When you see a Safari® Enabled icon on the cover of your favorite technology book, that 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 tech 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.
For as long as I can remember, I have been interested in writing a book or two. But now that I have written several, I must confess that I was naive when I started. I had no idea how much work it would take, or how many other people would have to get involved in the process. Another thing that surprised me was how easy it was to find a willing publisher. I had expected that to be the hard part.
I continue to be thankful to all of the following people for sharing their ideas and reviewing my work on the first edition: Toby Bennett, Paul Cabler (and the other great folks at Arcom), Mike Corish, Kevin D’Souza, Don Davis, Steve Edwards, Mike Ficco, Barbara Flanagan, Jack Ganssle, Stephen Harpster, Jonathan Harris, Jim Jensen, Mark Kohler, Andy Kollegger, Jeff Mallory, Ian Miller, Henry Neugauss, Chris Schanck, Brian Silverman, John Snyder, Jason Steinhorn, Ian Taylor, Lindsey Vereen, Jeff Whipple, and Greg Young.
I would also like to thank our editor, Andy Oram. Without his enthusiasm for my initial proposal, overabundant patience, and constant encouragement, neither the first nor the second edition of this book would have been completed.
And, of course, I am extremely thankful to Anthony Massa. Anthony’s interest in updating this book with additional materials, new hardware and examples, and a change to the GNU tools came at just the right time. It has been difficult to watch someone else update a first edition that I felt good about and that sold so surprisingly well. But the new book is significantly better for Anthony’s tireless efforts. This second edition would not exist if not for Anthony’s hard work and dedication to the project.
This is my second adventure in the realm of book writing. I thought writing a second edition would be a lot less work because most of the material was already finished. Boy, was I wrong. The second edition was as bit of a struggle and took more effort and time than I expected, but I think the book turned out better as a result.
I am very thankful to our editor, Andy Oram. His feedback was fantastic, he was a guiding light to push the book to completion, he always provided the needed spark to pull things together, and he even stepped in to test the code when needed. The second edition of this book is much better because of him and would not have been possible without his support and determination.
I would like to thank Michael Barr for the opportunity to work with him on this project. I know how attached a writer can become to such a project; thank you for entrusting me with the new edition. Michael provided extremely helpful input and helped me guide the text in the right direction. There were some struggles getting things just right, but I think that working through them has improved the book. Michael is truly a great mind in the embedded software development community.
Thanks to the folks at Arcom that so graciously provided the very impressive and top-notch development system for this book. A big thank you to Glen Middleton, who was always there to make sure I got whatever I needed. And thanks to Arcom’s extremely helpful development team of Ian Campbell, Martyn Blackwell, and David Vrabel.
I am very fortunate that the following people gave their valuable time to help make this book better by sharing ideas and reviewing the second edition. This outstanding team was made up of Michael Boerner, John Catsoulis, Brian Jepson, Nigel Jones, Alfredo Knecht, Jon Masters, Tony Montiel, Andrea Pellegrini, Jack Quinlan, Galen Seitz, and David Simon. A special thanks to Jonathan Larmour for being there in the clutch when I had a question for you—you came through for me, again.
A special thanks to my A-1 review crew of Greg Babbitt, my brother Sean Hughes, Brian Kingston, Anthony Taranto, and Joseph Terzoli.
I would like to thank two great people for all their support throughout my life—Nonno and Nonna. They were always there for me with love and guidance.
Thanks to my brother, Laurie, and my sister, Catherine, for their support. I am grateful that both of you are in my life.
I would like to give a very special thank you to my Mom and Dad for giving me the foundation to succeed. You are very special people, are very supportive in everything I do in life, and are always there whenever I need anything. I feel blessed that I have you for my parents.
I am thankful to my daughters, Katie and Ashley, who are always there to cheer me up when I’m down or stressed out. You are precious, special girls, and I love you both with all my heart.
Finally, I would like to thank my wonderful wife, Deanna. I know this journey was tough at times, but you were always patient and supportive. I’m grateful that you are in my life. Thanks for being my best friend.