ebook img

Linux for Embedded and Real-Time Applications PDF

272 Pages·2003·1.288 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Linux for Embedded and Real-Time Applications

Linux for Embedded and Real-time Applications . Linux for Embedded and Real-time Applications by Doug Abbott Amsterdam Boston 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-7546-2 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-4880 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 Preface................................................................................................... ix Audience and Prerequisites................................................................. x Personal Biases.................................................................................. xi Chapter 1: The Embedded and Real-time Space.....................................1 What Is Embedded? ...........................................................................1 What Is Real-time?.............................................................................2 How and Why Does Linux Fit in? ........................................................3 Resources...........................................................................................7 Chapter 2: Introducing Linux .................................................................9 Features.............................................................................................9 Protected Mode Architecture............................................................11 The Linux Process Model ..................................................................16 The Linux Filesystem.........................................................................21 The “root” User................................................................................23 The /usr hierarchy ............................................................................28 The Shell..........................................................................................29 Resources.........................................................................................29 Chapter 3: The Host Development Environment..................................31 Cross-Development Tools—the GNU Tool Chain ...............................32 Configuring and Building the Kernel .................................................33 Summary .........................................................................................46 Resources.........................................................................................46 Chapter 4: BlueCat Linux.....................................................................49 The “Less Is More” Philosophy..........................................................49 Installing BlueCat Linux....................................................................50 X86 Target for Blue Cat Linux ...........................................................52 v Linux for Embedded and Real-time Applications Configuring the Workstation ............................................................55 First Test Program............................................................................58 Directories .......................................................................................58 Configuration Files...........................................................................59 Makefile...........................................................................................59 Target Files.......................................................................................59 Resources.........................................................................................63 Chapter 5: Debugging Embedded Software .........................................65 The Target Setup..............................................................................65 GDB ................................................................................................66 Debugging a Sample Program...........................................................68 The Host as a Debug Environment....................................................73 Adding Programmable Setpoint and Limit ........................................76 Resources.........................................................................................79 Chapter 6: Kernel Modules and Device Drivers ....................................81 Kernel Modules ................................................................................81 What’s a Device Driver Anyway?........................................................86 Linux Device Drivers..........................................................................87 Internal Driver Structure...................................................................90 The Hardware ..................................................................................95 The Target Version of Thermostat.....................................................96 Debugging Kernel Code....................................................................96 Building Your Driver into the Kernel ................................................100 An Alternative—uCLinux .................................................................103 Resources.......................................................................................104 Chapter 7: Embedded Networking.....................................................105 Sockets ..........................................................................................105 A Simple Example...........................................................................108 A Remote Thermostat ....................................................................111 Embedded Web Servers..................................................................113 Resources.......................................................................................116 vi Contents Chapter 8: Introduction to Real-time Programming...........................117 Polling vs. Interrupts.......................................................................118 Tasks..............................................................................................125 Scheduling .....................................................................................128 Kernel Services................................................................................132 Inter-task Communication..............................................................134 Problems with Solving the Resource Sharing Problem— Priority Inversion ............................................................................141 Interrupts and Exceptions...............................................................143 Critical Sections .............................................................................144 Resources.......................................................................................145 Chapter 9: Linux and Real-time..........................................................147 Why Linux Isn’t Real-time ...............................................................147 Two Approaches ............................................................................150 Resources—Obtaining Real-time Linux Implementations..................153 Chapter 10: The RTAI Environment...................................................155 Installing RTAI................................................................................155 The RTAI Architecture ....................................................................159 Intertask Communication and Synchronization...............................162 Communicating with Linux Processes..............................................163 Real-time in User Space—LXRT .......................................................164 One Shot vs. Periodic Timing..........................................................167 Moving to Kernel Space..................................................................170 Real-time FIFOs and Shared Memory..............................................171 Suggested Exercises.........................................................................173 Chapter 11: Posix Threads.................................................................175 Threads..........................................................................................176 Synchronization—Mutexes ..............................................................178 Communication—Condition Variables.............................................181 Pthreads in User Space ...................................................................182 Moving to Kernel Space..................................................................188 Message Queues.............................................................................190 Suggestions for Further Exploration ................................................192 Resources.......................................................................................193 vii Linux for Embedded and Real-time Applications Appendix A: RTAI Application Programming Interface (API).............195 Appendix B: Posix Threads (Pthreads) Application Programming Interface........................................................................................227 Appendix C: Why Software Should Not Have Owners .......................243 Index...................................................................................................251 viii Preface “ ’You are in a maze of twisty little passages, all alike’ Before you looms one of the most complex and utterly intimidating systems ever written. Linux, the free UNIX clone for the personal computer, produced by a mishmash team of UNIX gurus, hackers, and the occasional loon. The system itself reflects this complex heritage, and although the development of Linux may appear to be a disorganized volunteer effort, the system is powerful, fast, and free. It is a true 32-bit operating system solution.”1 I have a confession to make. Until about three years ago, I didn’t like Unix and avoided it as much as possible. I always considered it deliberately obscure and difficult to use. I still do. Working with Linux has been one of the most frustrating experiences in my long career as a computer engineer. I can do a Windows installation in about 15 minutes without ever referring to a manual. I can’t do that with Linux. But, while Linux is far from being ready for prime time in the world of con- sumer computing, there are some good things about it that have forced me to soften my bias and grin and bear it. In the embedded space where I work, Linux can no longer be ignored or avoided, nor should it be. Linux is indeed complex and, unless you’re already a Unix guru, the learning curve is quite steep. The information is out there on the web but it is often neither easy to find nor readable. There are probably hundreds of books in print on Linux covering every aspect from beginners’ guides to the internal workings of the kernel. But until recently little has been written about Linux in embedded or real-time environments. 1 Linux Installation and Getting Started, Matt Welsh, et al. ix Linux for Embedded and Real-time Applications I decided to climb the Linux learning curve partly because I saw it as an emerging market opportunity and partly because I was intrigued by the Open Source development model. The idea of programmers all over the world contributing to the development of a highly sophisticated operating system just for the fun of it is truly mind-boggling. Having the complete source code not only allows you to modify it to your heart’s content, it allows you (in principle at least) to understand how the code works. Unfortunately, my experience has been that a lot of Linux code is “write-only.” Someone obvi- ously wrote it, but no one else can read it. Open Source has the potential to be a major paradigm shift in how our society conducts business because it demonstrates that cooperation can be as useful in developing solutions to problems as competition. Yet at the time this book is being written, serious questions are being raised concerning whether or not it is possible to actually make money with Open Source software. Is there a business model that works? The jury is still out. Audience and Prerequisites This book is directed at two different audiences: ■ The primary audience is embedded programmers who need an intro- duction to Linux in the embedded space. This is where I came from and how I got into Linux so it seems like a reasonable way to struc- ture the book. ■ The other audience is Linux programmers who need an introduction to the concepts of embedded and real-time programming. Consequently, each group will see some material that is review although it may be presented with a fresh perspective. This book is not a beginners’ guide. I assume that you have successfully installed a Linux system and have at least played around with it some. You know how to log in, you’ve experimented with some of the command utilities and have probably fired up X-windows. Chapter 2 is a cursory introduction to some of the features and characteristics of Linux that are of interest to embedded and real-time programmers. x

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.