Embedded System Design on a Shoestring Achieving High Performance with a Limited Budget by Lewin A.R.W. Edwards Amsterdam Boston Heidelberg London New York Oxford Paris San Diego San Francisco Singapore Sydney Tokyo Newnes is an imprint of Elsevier Science. Copyright © 2003, Elsevier Science (USA). All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Recognizing the importance of preserving what has been written, Elsevier Science prints its books on acid-free paper whenever possible. Library of Congress Cataloging-in-Publication Data ISBN: 0-7506-7609-4 British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library. The publisher offers special discounts on bulk orders of this book. For information, please contact: Manager of Special Sales Elsevier Science 200 Wheeler Road Burlington, MA 01803 Tel: 781-313-4700 Fax: 781-313-4882 For information on all Newnes publications available, contact our World Wide Web home page at: http://www.newnespress.com 10 9 8 7 6 5 4 3 2 1 Printed in the United States of America Contents Acknowledgments........................................................iv Chapter 1: Introduction..................................................................1 Chapter 2: Before You Start—Fundamental Decisions...................9 General Microcontroller Selection Considerations.......................................9 Choosing the Right Core ...........................................................................13 Building Custom Peripherals with FPGAs..................................................19 Whose Development Hardware to Use—Chicken or Egg? ........................21 Our Hardware Choice—The Atmel EB40 ...................................................29 Recommended Laboratory Equipment ......................................................30 Free Development Toolchains....................................................................32 Free Embedded Operating Systems...........................................................36 GNU and You—How Using “Free” Software Affects Your Product .................................................................................44 Choices of Development Operating System...............................................51 Special PCB Layout and Initial Bring-Up Rules for the Shoestring Prototype .....................................................................53 Hints for Surface-Mounting by Hand.........................................................62 Choosing PCB Layout Software.................................................................65 Chapter 3: The GNU Toolchain.....................................................71 Building the Toolchain ...............................................................................71 Overview of the GNU Build Environment...................................................76 GNU Make and an Introduction to Makefiles .............................................80 v Contents Gas—The GNU Assembler ........................................................................87 Comments...........................................................................................88 Symbols and Labels ............................................................................88 Code Sections and Section Directives .................................................90 Pseudo-Operations..............................................................................96 Conditional Assembly Directives .......................................................108 Macros, Assembler Loops and Synthetic Instructions ......................111 Ld—GNU Linker......................................................................................114 Introduction.......................................................................................114 The SECTIONS command..................................................................118 Symbol Assignments, Expressions and Functions ............................119 Output Section Descriptions..............................................................124 Overlay Section Descriptions.............................................................127 Emitting Data Directly into the Executable.........................................131 Input Section Descriptions ................................................................132 Named Memory Regions...................................................................134 Special Considerations for C++ .........................................................136 Further ld Information .......................................................................137 Converting Files with Objcopy .................................................................138 Objdump—Check Your Executable’s Layout............................................139 Size—Check the Load Size of Your Executable........................................143 Gdb—The GNU Debugger.......................................................................143 Invoking and Quitting gdb and Loading Your Program .....................145 Examining Target Memory.................................................................148 Breakpoints and Other Conditional Breaks ........................................149 Getting Further Help ..........................................................................151 Chapter 4: Example Firmware Walkthroughs and Debugging Techniques.....................................................153 A Quick Introduction to ARM and the Atmel EB40 ..................................153 First Step—the LED Flasher (in Assembler)............................................158 vi Contents Bringing Up a Simple C Program— The LED Flasher (in C).............................................................................167 Writing a Simple Flash-Loader (and Inspecting Memory with gdb) .........................................................172 A Simple ROM-Startup Program.............................................................180 A Complete ROM-Startup Application in C..............................................185 Blind-Debugging Your Program ..............................................................194 Miscellaneous Glue—Handling Hardware Exceptions in C with gcc............................................................................................199 Chapter 5: Portability and Reliability Considerations..................203 Chapter 6: Useful Vendors and Other Web Resources...............221 Index of CD-ROM Contents.....................................................................223 About the Author.......................................................227 Index.....................................................................229 vii Acknowledgments The author would like to extend his sincere thanks to the follow- ing individuals and corporations who have contributed directly and indirectly to the publication of this book: ■ Atmel developer support ■ Cadsoft Computer, Inc. ■ Cirrus Logic developer support ■ Michael Barr ■ Don McKenzie of dontronics.com ■ Spehro Pefhany ■ Rob Severson of USBmicro ■ Sharp Microelectronics developer support In keeping with the open-source nature of this book’s subject matter, the manuscript of this work was developed entirely using the free open-source OpenOffice.org office productivity suite, under Red Hat Linux 8.0. ix Chapter 1 Introduction There exists a large body of literature focused on teaching both general embedded systems principles and design techniques, and tips and tricks for specific microcontrollers. The majority of this literature is targeted at small 8-bit microcontrollers such as the Microchip PIC, Atmel AVR and the venerable 8051, principally because these devices are inexpensive and readily available in small quantity, and development hardware is available from a variety of sources at affordable prices. Historically, higher-per- formance 16- and 32-bit parts have been hard to obtain in small quantities, their development toolchains have been prohibitively expensive, and the devices themselves have been difficult to de- sign around, with tight electrical and timing requirements on external circuitry necessitating very careful hardware design. A dearth of royalty-free, open-source operating system and library code for these processors also meant that developing a new project was a huge from-the-ground-up effort. However, over the past few years we have simultaneously seen the size and price of 16- and 32-bit cores fall, and the devel- opment of many highly integrated parts that enable the easy development of almost single-chip 32-bit systems. In addition, many readily available appliances now contain a well-documented 32-bit microcontroller with ample flash memory, RAM and a variety of useful peripherals such as color LCDs, network inter- faces and so forth, which can be exploited by the cunning embedded developer as a ready-made hardware platform. Cross- platform assemblers, high-level language compilers and debugging tools are available free for the downloading and will run satisfactorily on the average desktop PC; it is no longer nec- 1 2 Chapter 1 essary to spend tens of thousands of dollars on proprietary com- pilers and special workstations on which to run them. As these systems have increased in complexity, to a certain extent the degree of specialization required to develop them has decreased. This might sound paradoxical, but consider the fact that high-end 32-bit embedded systems, and the tools used to develop for them, are effectively converging with the low-end mainstream PC. The skills required to develop an application for embedded Linux, NetBSD or Windows CE are by intention not radically different from the skills used in developing applica- tions for the desktop equivalents of these operating systems (though of course different coding best practices usually apply in embedded environments). In most cases there are mature off- the-shelf operating systems available ready-to-run for the common hardware reference designs and manufacturer-supplied evalua- tion boards, so we are usually spared even the initial bring-up phase and much of the effort required to debug device drivers. Given a working hardware platform with reasonably well- documented components, the only task for which traditional embedded expertise is absolutely necessary is to create the nec- essary bootstrap and “glue” code to get a C run-time working on the target platform, and perhaps create drivers for some periph- erals (and as discussed above, even this step can often be skipped if you are building around a reference platform). From that point on, most of the programming work to be done runs in the appli- cation layer and can be accomplished using high-level languages. There is a large workforce available almost ready-trained for this type of coding. The end result of this evolutionary process is that it is now well within the financial and logistical reach of a small company or even an individual hobbyist or student to develop (or at least repurpose) advanced embedded systems with exciting function- ality provided by these high-performance parts. Unfortunately, however, device vendors’ support infrastructures are still geared towards large-scale commercial developers. This raises two ma- jor obstacles: Introduction 3 1. Development hardware for high-end parts is, by and large, still too expensive for the average hobbyist or student. This is partly a chicken-and-egg problem; the only source for evalu- ation boards for 32-bit parts is usually the chip vendor, because there isn’t sufficient third-party interest to see third-party evalu- ation platforms developed. The resulting small volumes and lack of competition conspire to keep prices high. From hear- say, it seems likely that some chip vendors also have an intentional policy of excluding small customers from purchas- ing high-end devices. In many cases, the evaluation board is unavoidably expensive because it is designed to showcase what can be achieved in a maximally configured appliance in the chip’s target market; consequently, the board has a large num- ber of peripherals. 2. In order to ensure continued support from major embedded toolchain vendors, chip designers usually recommend only specific development environments, all of which are extremely expensive. It is still quite rare to find explicit manufacturer support for freely available compilers and debuggers, despite the widespread adoption of such tools in the industry at large. One underlying business reason for this is that in order for a new part to be credible, it should be supported from its re- lease by well-known commercial toolchain vendors. To encourage active interest from the developers of these toolchains, and to reduce their own support workload, the chip vendors generally avoid mention of free, user-supported tools. The main object of this book is to illustrate some methods of overcoming these obstacles and realizing exciting projects around today’s high-performance chips. A strong secondary objective is to assist developers in migrating from the coddled environment of one-click graphical integrated development environments to the command-line tools typical of free toolchains. Although there are many references for this available on the Internet and in printed form, the authors of such guides usually do not archive the tool versions they discuss in their text, and their instructions often con- tain information that is not applicable to the currently available versions of the tools. It can therefore be difficult for the neophyte 4 Chapter 1 to know which of his or her problems are genuine coding errors, and which are simply the result of documentation inconsistencies. Given these goals, this volume is aimed at the following groups of readers: Hobbyists and students. These people are typically financing the acquisition of development hardware and software out of their own pocket. They do not necessarily expect a direct cash return on this investment, and they are usually working alone or in small groups. This type of reader is interested in solutions that involve minimal startup expenses and don’t require large engineering teams. Entrepreneurs. This class of reader has a product idea that needs to be at least prototyped so that it can be shown to potential in- vestors with the aim of securing development financing. Not only does a real prototype have a much better “wow” value (and hence a better chance of attracting investors) than a sketch and verbal description, but developing the prototype will reveal and perhaps solve many of the engineering problems to be encountered in making the real product. This obviously translates directly into a shorter time-to-market. Engineers working alone or at small companies. Small engi- neering houses that currently work with 8-bit systems may realize significant gains by moving to 32-bit parts. In many cases, func- tionality provided by dedicated hardware in the 8-bit system can be synthesized in firmware on the 32-bit system – this has obvi- ous profit benefits because the company can thin its inventory to just a few standard hardware platforms, differentiating products by means of firmware features. Customer satisfaction can also be enhanced, since new features can be added with simple firm- ware upgrades. Because the hardware is standardized, reference designs have a longer lifespan, meaning that new projects may only require incremental software changes – potentially an enor- mous saving in development time. However, the leap from 8 to 32 bits is a significant one and it can seem prohibitively expen- sive in the short term even when the long-term benefits are well understood. After reading this book, it should be clear that the up-front investment is not necessarily huge, and it may be time to make the big jump to 32-bit cores.
Description: