Several chapters cover the basic steps of starting up an embedded system and getting a working application including the basic boot in assembler, exception handling, flash memory drivers
Trang 2Embedded Systems
Firmware Demystified
Ed Sutter
CMP Books Lawrence, Kansas 66046
CMP Books CMP Media LLC 1601 West 23rd Street, Suite 200 Lawrence, Kansas 66046 USA www.cmpbooks.com
Designations used by companies to distinguish their products are often claimed as trademarks
In all instances where CMP Books is aware of a trademark claim, the product name appears
in initial capital letters, in all capital letters, or in accordance with the vendor’s capitalization preference Readers should contact the appropriate companies for more complete information on trademarks and trademark registrations All trademarks and registered trademarks in this book are the property of their respective holders Copyright © 2002 by Lucent Technologies, except where noted otherwise Published by CMP Books, CMP Media LLC All rights reserved Printed in the United States of America No part
of this publication may be reproduced or distributed in any form or by any means, or stored
in a database or retrieval system, without the prior written permission of the publisher The programs in this book are presented for instructional value The programs have been carefully tested, but are not guaranteed for any particular purpose The publisher does not offer any warranties and does not guarantee the accuracy, adequacy, or completeness
of any information herein and is not responsible for any errors or omissions The publisher
Trang 3assumes no liability for damages resulting from the use of the information in this book
or for any infringement of the intellectual property rights of third parties that would result from the use of this information
Acquisition Editor: Robert Ward
Editors: Joe Casad and Catherine Janzen
Layout production: James Hoyt, Justin Fulmer, and Madeleine Reardon Dimond
Managing Editor: Michelle O’Neal
Cover art: Rupert Adley (www.solidimages.com)
Cover design: Damien Castaneda
Distributed in the U.S and Canada by:
Publishers Group West 1700 Fourth Street Berkeley, CA 94710
Trang 4vii
Reader?
ix
What’s Covered in this Book?
ix Conventions
Trang 5
xii
Source Code
xii Acknowledgments
xii
Trang 6Chapter 1 A Hard
Start
1 System Requirements
2
Central Processing Unit
Trang 7
3
System Memory
10 CPU Supervision
Trang 8
12 Serial Port Drivers
15
Ethernet Interface
Trang 9
17
Options
18
The CPU/Memory Interface
19
Trang 10Summary
27
Chapter 2 Getting Started
29 How Is It Done on a PC?
Trang 11
30
Building
Libraries
45 Up Front
47
Trang 12Run
Time
55
Hardware Sanity Tests for the Firmware Developer
55
Trang 13Summary
60
Chapter 3 Introducing MicroMonitor
61
An Embedded System Boot Platform
61
Trang 14Chapter 4 Assembly
Required
69
Just After Reset
70
I/O Initialization
Trang 15
76
Establish Exception Handlers
76
Summary
86
Trang 16Interface
.87
CLI Features
88 CLI Data Structure and the Command Table
89
Trang 17CLI
Processing
90 The Functions Beneath the Command Name
91
Shell Variable and Symbol
Trang 18Processing
96
Command-Line Redirection
99
Command Line Editing and History
105
Trang 19User
Levels
111 Password Protection
115
Trang 20Summary
116
Chapter 6 Interfacing to Flash Memory
.117
The Interface Functions
Trang 21
118
Flash Driver Front End
132
Summary
136
Trang 22Chapter 7 A Flash File
Criteria 138
Trang 23File
Attributes
139
High-Level
Details 141
Trang 24Required by TFS 143
Defragmentation
144
TFS
Implementation
Trang 25
146
Files
155
Load
Trang 26
165
File
Decompression
170
Place
Trang 27171
Summary 172
.173
Trang 28Runner 173
Conditional
Branching 181
Examples
Trang 29187
Summary 191
.193
Trang 30Ethernet
194
ARP 194
IP
Trang 31195
ICMP 196
TCP 197
Trang 32DHCP/BOOTP
198
Systems
199
Summary
Trang 33209
211
Xmodem 211 TFTP 219
Capability
230
Summary 231
Chapter 11 Adding the Application 233
Trang 34Establishing an Application Stack
Chapter 12 Monitor-Based Debugging 247
Trang 35Different Type of Debug Philosophy
248
Breakpoints 249
Overflow
283
System
Profiling 289 Summary 299
Trang 36Chapter 13 Porting MicroMonitor to the
Conclusion
Appendix A Building a Host-Based Toolbox 337
Host
338
Trang 37Appendix B RTOS Overview .349
The
Scheduler 350
Trang 38Good Concurrency vs Bad Concurrency 357
Summary 358
Surpris-a tSurpris-ask Surpris-and Surpris-a process? Do I need to worry Surpris-about reentrSurpris-ancy? As we progress through
Embedded Systems Firmware Demystified, you will come to see that these questions are not as complex as they first appear
Embedded systems programming spans a wide range of activities from building programmable logic at the most concrete end to writing a UNIX™ process at the most abstract end Bracketed by these poles, the industry has exploded in the last 20 years In the late seventies, assemblers were considered luxuries A typical embedded system used less than 64Kb of system memory (bits, not bytes!) There was no hardware hand-off to the firmware developer The same person that drew the schematics and soldered the prototype also wrote the firmware that pulled the whole thing together By the time Intel introduced the 8085 chip,
Trang 39it was clear that those pesky microprocessors were here to stay In the eighties, the Motorola versus Intel CPU wars started, and C became a popular choice for the elite few who dared to mess with a high-level language and an EPROM programmer Today, microprocessors are everywhere and range from the 4- and 8-bit workhorses still dominating the industry to 1GHz 64-bit processors that almost need a freezer (microprocessor-controlled, no doubt) to keep them cool Over the years, the complexity of these systems has snowballed The industry has transitioned from programming a DEC PDP machine with binary codes on the front panel to applying object-oriented design to a microcontroller in a toaster If you let the trend get to you, the changes can seem frazzling There are microprocessors and microcontrollers; RAM, DRAM, and SDRAM; pipelining and superscalar; EPROM and flash memory; RISC and CISC; and RAS and CAS and cache — and that’s just the beginning
Now, everything from toothbrushes (no kidding) to fighter jets are likely to have
a version controlled by a microprocessor of some kind With this trend come tools and technologies One can easily be overwhelmed by the available choices in hardware (integrated circuits that the firmware must make work) and software (tools that are used to build the firmware application)
The goal of this book is to prepare you for a real embedded systems project by walking you through an entire embedded systems design Not coincidentally, the project source code included is a piece of firmware — an embedded boot platform
— that can simplify all your future projects I assume a small hardware design with CPU, memory, and some peripherals I present a basic schematic and walk you through the method in which instructions are fetched from memory I discuss devices,
as well as concepts I examine flash memory versus EPROM, SRAM versus DRAM, microcontroller versus microprocessor, and data bus versus address bus I also explain how you convert your C and assembly language source code to a binary image that ends up in the memory device from which the CPU boots (the boot flash memory) Several chapters cover the basic steps of starting up an embedded system and getting a working application (including the basic boot in assembler), exception handling, flash memory drivers, a flash file system, and serial and Ethernet connections The result is an understanding of how an embedded systems project gets started and how to build a platform on which an embedded systems application can reside
Sound exciting? Great! Sound scary? It’s not! The intent of this book is not to discuss the latest superscalar architecture or the antenna effects of copper routes
on a printed circuit board, nor does it present a high-level abstract design process (Advanced architectures and transmission-line effects are certainly important, but they are not the topic of this book.) This book is for those who want to get their hands dirty without being overwhelmed by industry jargon or design-specific techni-cal details By the end of this book, you will know how to read a basic schematic, know what goes into the boot flash device, and understand the major components of
a complete embedded systems development platform
Who Is the Reader?
Trang 40At the minimum, I assume the reader has some experience with C and basic assembly language concepts I do not assume any electronics or hardware background Thus, readers with a wide range of programming backgrounds will find this book useful
Computer science or electrical engineering students without a significant background in firmware development, but at least an interest, can obviously benefit from this book
Low-level firmware developers will find the working example helpful, as it includes documentation and code explanations for an extensible firmware development platform I explain the details associated with booting new hardware and the way in which the CPU interacts with peripherals Topics range from the lowest-level boot to a Trivial File Transfer Protocol (TFTP) implementation over Ethernet You can port the code in this book to your own target system or integrate snippets of this code with your existing firmware platform
Hardware developers will find the completed platform useful for helping analyze and debug hardware beyond the CPU complex For those inquisitive enough to step away from hardware to learn more about the firmware process, this book provides
a way to get started without getting too far from the hardware (Hardware designers regularly make the transition to programming in the firmware/software world.)
Project leaders will also find this book useful, as the firmware package presented
is a mature platform The platform is applicable to a wide range of real-time operating systems (RTOS) and target architectures, and it is extremely easy
to port to new systems The fact that the platform is fairly target- and RTOS-independent makes the transition to a different target or RTOS much less painful
What’s Covered in this Book?
Chapter 1: A Hard Start describes a basic CPU structure, with support peripherals, on which the firmware discussed in this book can execute The design includes, among other things, a core microprocessor, RAM, flash memory, serial I/O, and CPU supervision Each element of the design is discussed (for example, CPU supervision and types of volatile and non-volatile memory devices) This chapter also examines the basics of how microprocessors fetch instructions from the memory device and how cache makes these fetches more efficient The chapter covers the typical peripherals found in many microprocessors today, without being specific to a particular device
Chapter 2: Getting Started introduces the writing and building of programs for embedded devices This chapter explains the differences between a native-compila-tion and a cross-compilation environment I discuss file formats and break down the task of burning the boot flash memory into simple steps I also explain
Trang 41the importance of the link editor file and show how it is used to allocate sections
of the application code to portions of the target memory The chapter also discusses some preliminary tests and preparations you must undertake before you start program-ming the firmware
Chapter 3:Introducing MicroMonitor introduces the boot platform, or boot monitor, used in embedded systems This monitor, called MicroMonitor, has a full list of features and serves as a learning tool to teach you about the firmware development process
Chapter 4: Assembly Required describes MicroMonitor’s reset vector, or startup code
I then build some serial drivers and discuss an implementation for establishing exception handlers
Chapter 5:Command Line Interface describes how to build a shell around the core functionality introduced in Chapter 4 I start with the command line interface (CLI).Next, I explain the processing that parses the input command line and that eventually (through the table of structures) calls a function corresponding to the command line The command line interface includes shell variables and symbols, command line editing and history, user levels, and password protection
Chapter 6:Interfacing to Flash Memory is the first of three chapters dealing rily with flash memory I describe a sample platform with (potentially several) banks
prima-of flash memory This chapter discusses the lowest-level details involved in programming flash memory and presents a useful approach for target hardware with different flash memory configurations
Chapter 7:A Flash File System discusses the creation of a tiny file system (TFS) which resides in flash memory This simple file system is offered as an alternative
to some of the more complicated approaches taken by many flash file system designers
I also describe some of the advantages and disadvantages of the trade-offs made
in the design of TFS
Chapter 8: Executing Scripts describes a feature that, with a CLI for command cution and a TFS for file storage, lets a file execute as a script or small program This chapter examines the surprisingly simple details that combine the CLI and TFS
exe-to create a simple interpreter The CLI commands are chosen exe-to provide the CLI/TFS user with a simple but useful programming environment Conditional branching and subroutines are just a few of the capabilities of this system
Chapter 9: Network Connectivity examines the elements needed to connect as a node
on a network Just as flash memory is becoming a standard boot memory device, so too is Ethernet becoming a standard interface on many embedded systems This chapter discusses several protocols used with embedded systems, including Ethernet, ARP, ICMP, IP, UDP, and BOOTP/DHCP Finally, the chapter examines encapsulation of network layers — one on top of another — by small blocks of code that know only about their own piece of the packet payload
Chapter 10: File/Data Transfer discusses two common file-transfer protocols: dem for serial interfaces and TFTP for networks Here I explain each of these proto-