Programming Embedded Systems With C and GNU Development Tools, 2nd Edition (Michael Barr, Anthony Massa) (z-library.sk, 1lib.sk, z-lib.sk)

Author: Michael Barr, Anthony Massa

C/C++/C#

If you have programming experience and a familiarity with C--the dominant language in embedded systems--Programming Embedded Systems, Second Edition is exactly what you need to get started with embedded software. This software is ubiquitous, hidden away inside our watches, DVD players, mobile phones, anti-lock 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 have been nearly impossible to create without embedded software. The first edition of Programming Embedded Systems taught the subject to tens of thousands of people around the world and is now considered the bible of embedded programming. This second edition has been updated to cover all the latest hardware designs and development methodologies. The techniques and code examples presented here are directly applicable to real-world embedded software projects of all sorts. Examples use the free GNU software programming tools, the eCos and Linux operating systems, and a low-cost hardware platform specially developed for this book. If you obtain these tools along with Programming Embedded Systems, Second Edition, you'll have a full environment for exploring embedded systems in depth. But even if you work with different hardware and software, the principles covered in this book apply. Whether you are new to embedded systems or have done embedded work before, you'll benefit from the topics in this book, which include:How building and loading programs differ from desktop or server computers Basic debugging techniques--a critical skill when working with minimally endowed embedded systems Handling different types of memory Interrupts, and the monitoring and control of on-chip and external peripherals Determining whether you have real-time requirements, and whether your operating system and application can meet those requirements Task synchronization with real-time op

📄 File Format: PDF
💾 File Size: 4.2 MB
8
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
(This page has no text content)
📄 Page 3
Programming Embedded Systems with C and GNU Development Tools
📄 Page 4
Other resources from O’Reilly Related titles Building Embedded Linux Systems C in a Nutshell Designing Embedded Hardware Home Hacking Projects for Geeks Linux in a Nutshell RFID Essentials Practical C Programming oreilly.com oreilly.com is more than a complete catalog of O’Reilly books. You’ll also find links to news, events, articles, weblogs, sample chapters, and code examples. oreillynet.com is the essential portal for developers interested in open and emerging technologies, including new platforms, pro- gramming languages, and operating systems. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in document- ing the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit conferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today for free.
📄 Page 5
Programming Embedded Systems with C and GNU Development Tools SECOND EDITION Michael Barr and Anthony Massa Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo
📄 Page 6
Programming Embedded Systems with C and GNU Development Tools, Second Edition by Michael Barr and Anthony Massa Copyright © 2007, 1999 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editor: Andy Oram Production Editor: Lydia Onofrei Copyeditor: Lydia Onofrei Proofreader: Mary Brady Indexer: Ellen Troutman Zaig Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrators: Robert Romano and Jessamyn Read Printing History: January 1999: First Edition. October 2006: Second Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Programming Embedded Systems with C and GNU Development Tools, the image of a tick, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book uses RepKover™, a durable and flexible lay-flat binding. ISBN-10: 0-596-00983-6 ISBN-13: 978-0-596-00983-0 [M] [12/07]
📄 Page 7
For my son, Vikram. —Michael Barr This book is dedicated to my wonderful daughters, Katie and Ashley, and my beautiful wife, Deanna. You mean everything to me. I love you. —Anthony Massa
📄 Page 8
(This page has no text content)
📄 Page 9
vii Table of Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What Is an Embedded System? 1 Variations on a Theme 4 Embedded Design Examples 8 Life As an Embedded Software Developer 11 The C Language: The Lowest Common Denominator 13 A Few Words About Hardware 15 2. Getting to Know the Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Understanding the Big Picture 19 Hardware Basics 21 Examine the Landscape 28 Learn How to Communicate 32 Getting to Know the Processor 34 Study the External Peripherals 38 Initialize the Hardware 39 3. Your First Embedded Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Hello, World! 43 The Blinking LED Program 44 The Role of the Infinite Loop 51
📄 Page 10
viii | Table of Contents 4. Compiling, Linking, and Locating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 The Build Process 54 Building the Blinking LED Program 61 A Quick Look at Makefiles 66 5. Downloading and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Downloading the Blinking LED Program 70 Remote Debuggers 77 Emulators 84 Other Useful Tools 86 Dig into the Hardware 92 6. Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Types of Memory 93 Direct Memory Access 98 Endian Issues 98 Memory Testing 102 Validating Memory Contents 114 Using Flash Memory 118 7. Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Control and Status Registers 122 The Device Driver Philosophy 130 Device Driver Design 140 8. Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Overview 142 Interrupt Map 148 Interrupt Service Routine 150 The Improved Blinking LED Program 155 Summary of Interrupt Issues 161 9. Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Application Overview 164 Working with Serial Ports 166 Command-Line Interface Processing 167 10. Operating Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 History and Purpose 173 The Scheduler 174 Tasks 180
📄 Page 11
Table of Contents | ix Task Synchronization 185 Message Passing 190 Other Functionality 191 Interrupt Handling 191 Real-Time Characteristics 192 To Use or Not to Use an RTOS 194 Additional Resources 197 11. eCos Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Introduction 198 Task Mechanics 199 Mutex Task Synchronization 202 Semaphore Task Synchronization 205 Message Passing 210 eCos Interrupt Handling 213 12. Embedded Linux Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Introduction 219 Accessing Hardware in Linux 220 Task Mechanics 220 Mutex Task Synchronization 222 Semaphore Task Synchronization 224 Message Passing 227 13. Extending Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Common Peripherals 232 Networking for All Devices Great and Small 242 14. Optimization Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Increasing Code Efficiency 249 Decreasing Code Size 252 Problems with Optimizing Compilers 254 Reducing Memory Usage 255 Power-Saving Techniques 256 Limiting the Impact of C++ 259
📄 Page 12
x | Table of Contents A. The Arcom VIPER-Lite Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 B. Setting Up Your Software Development Environment . . . . . . . . . . . . . . . . . 266 C. Building the GNU Software Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 D. Setting Up the eCos Development Environment . . . . . . . . . . . . . . . . . . . . . . 274 E. Setting Up the Embedded Linux Development Environment . . . . . . . . . . . 277 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
📄 Page 13
xi Foreword1 If you mention the word embedded to most people, they’ll assume you’re talking about reporters in a war zone. Few dictionaries—including the canonical Oxford English Dictionary—link embedded to computer systems. Yet embedded systems underlie nearly all of the electronic devices used today, from cell phones to garage door openers to medical instruments. By now, it’s nearly impossible to build any- thing electronic without adding at least a small microprocessor and associated software. Vendors produce some nine billion microprocessors every year. Perhaps 100 or 150 million of those go into PCs. That’s only about one percent of the units shipped. The other 99 percent go into embedded systems; clearly, this stealth business represents the very fabric of our highly technological society. And use of these technologies will only increase. Solutions to looming environmen- tal problems will surely rest on the smarter use of resources enabled by embedded systems. One only has to look at the network of 32-bit processors in Toyota’s hybrid Prius to get a glimpse of the future. Though prognostications are difficult, it is absolutely clear that consumers will con- tinue to demand ever-brainier products requiring more microprocessors and huge increases in the corresponding software. Estimates suggest that the firmware content of most products doubles every 10 to 24 months. While the demand for more code is increasing, our productivity rates creep up only slowly. So it’s also clear that the industry will need more embedded systems people in order to meet the demand. What skills will these people need? In the PC world, one must be a competent C/C++ programmer. But embedded developers must have a deep understanding of both the programming languages and the hardware itself; no one can design, code, and test an interrupt service routine, for instance, without knowing where the interrupts come from, how the hardware prioritizes them, the tricks behind servicing that hardware, and machine-level details about saving and preserving the system’s context. A firm- ware developer must have detailed insight into the hardware implementation of his system’s peripherals before he can write a single line of driver code.
📄 Page 14
xii | Foreword In the PC world, the magic of the hardware is hidden behind an extensive API. In an embedded system, that API is always written by the engineers that are developing the product. In this book, Michael Barr and Anthony Massa show how the software and hard- ware form a synergistic gestalt. They don’t shy away from the intricacies of inter- rupts and I/O, or priority inversion and mutexes. The authors appropriately demonstrate building embedded systems using a variety of open source tools, including the GNU compiler suite, which is a standard tool widely used in this industry. eCos and Linux, both free/open source products, are used to demonstrate small and large operating systems. The original version of this book used an x86 target board, which has been replaced in this edition by an ARM-based product. Coincidently, as this volume was in pro- duction, Intel made an end-of-life announcement for all of its embedded x86 proces- sors. Readers can be assured that the ARM will be around for a very long time, as it’s supported by an enormous infrastructure of vendors. The hardware is inexpensive and easily available; the software is free. Together they represent the mainstream of embedded systems development. Readers can be sure they’ll use these tools in the future. Buy the development kit, read the book, and execute the examples. You’ll get the hands-on experience that employers demand: building and working with real embed- ded applications. —Jack Ganssle
📄 Page 15
xiii Preface2 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 soft- ware 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 appli- cation 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 lan- guage. 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. Why We Wrote This Book 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
📄 Page 16
xiv | Preface 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 excep- tion 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. Intended Audience 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 engi- neering 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 refer- ence 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 ref- erence 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.
📄 Page 17
Preface | xv Organization 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 complet- ing 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 pro- grammers 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. Chapter 1, Introduction Explains the field of embedded programming and lays out the parameters of the book, including the reference hardware used for examples Chapter 2, Getting to Know the Hardware Shows how to explore the documentation for your hardware and represent the components you need to interact with in C Chapter 3, Your First Embedded Program Creates a simple blinking light application that illustrates basic principles of embedded programming Chapter 4, Compiling, Linking, and Locating Goes over the ways that embedded systems differ from conventional computer systems during program building steps, covering such issues as cross-compilers Chapter 5, Downloading and Debugging Introduces the tools you’ll need in order to iron out problems in both hardware and software Chapter 6, Memory Describes the different types of memory that developers choose for embedded systems and the issues involved in using each type Chapter 7, Peripherals Introduces the notion of a device driver, along with other coding techniques for working with devices Chapter 8, Interrupts Covers this central area of working with peripherals Chapter 9, Putting It All Together Combines the concepts and code from the previous chapter with convenience functions and a main program, to create a loadable, testable application
📄 Page 18
xvi | Preface Chapter 10, Operating Systems Introduces common operating system concepts, including tasks (or threads) and synchronization mechanisms, along with the reasons for adding a real-time oper- ating system Chapter 11, eCos Examples Shows how to use some features of the eCos real-time operating system Chapter 12, Embedded Linux Examples Accomplishes the same task as the previous chapter, but for the embedded Linux operating system Chapter 13, Extending Functionality Describes options for adding buses, networking, and other communication fea- tures to a system Chapter 14, Optimization Techniques Describes ways to decrease code size, reduce memory use, and conserve power Appendix A, The Arcom VIPER-Lite Development Kit Describes the board used for the examples in this book and how to order one for yourself Appendix B, Setting Up Your Software Development Environment Gives instructions for loading the software described in this book on your host Windows or Linux computer Appendix C, Building the GNU Software Tools Shows you how to compile the GNU development tools Appendix D, Setting Up the eCos Development Environment Shows you how to build an eCos library appropriate for your embedded system so you can compile programs to run on your system Appendix E, Setting Up the Embedded Linux Development Environment 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 con- cepts. 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 vari- able declarations. For complete details about the code, refer to the full example source code on the book’s web site.
📄 Page 19
Preface | xvii Conventions, Typographical and Otherwise The following typographical conventions are used throughout the book: Italic 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 reg- ular 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 exam- ples 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 pur- posefully 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 hap- pen that a single individual fills more than one of these roles on an embedded-project team. Obtaining the Examples Online 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.
📄 Page 20
xviii | Preface Using Code 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 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. Comments and Questions 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/progembsys2 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
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now

Recommended for You

Loading recommended books...
Failed to load, please try again later
Back to List