logo资料库

Real-Time Embedded Multithreading Using ThreadX.pdf

第1页 / 共543页
第2页 / 共543页
第3页 / 共543页
第4页 / 共543页
第5页 / 共543页
第6页 / 共543页
第7页 / 共543页
第8页 / 共543页
资料共543页,剩余部分请下载后查看
Cover Page
Copyright Page
Copyright Page
Dedication
Dedication
Preface
Preface
Embedded and Real-time Systems
Embedded and Real-time Systems
Introduction
What is an Embedded System?
Characteristics of Embedded Systems
Real-time Systems
Real-time Operating Systems and Real-time Kernels
Processes, Tasks, and Threads
Architecture of Real-time Systems
Embedded Systems Development
Key Terms and Phrases
First Look at a System Using an RTOS
First Look at a System Using an RTOS
Operating Environment
Installation of the ThreadX Demonstration System
Sample System with Two Threads
Creating the ThreadX Objects
Compiling and Executing the Sample System
Analysis of the System and the Resulting Output
Listing of 02_sample_system.c
Key Terms and Phrases
Problems
RTOS Concepts and Definitions
RTOS Concepts and Definitions
Introduction
Priorities
Ready Threads and Suspended Threads
Preemptive, Priority-Based Scheduling
Round-Robin Scheduling
Determinism
Kernel
RTOS
Context Switch
Time-Slice
Interrupt Handling
Thread Starvation
Priority Inversion
Priority Inheritance
Preemption-Threshold
Key Terms and Phrases
Problems
RTOS Building Blocks for System Development
RTOS Building Blocks for System Development
Introduction
Defining Public Resources
ThreadX Data Types
Thread
Memory Pools
Application Timer
Mutex
Counting Semaphore
Event Flags Group
Message Queue
Summary of Thread Synchronization and Communication Components
Key Terms and Phrases
Problems
The Thread—The Essential Component
The Thread—The Essential Component
Introduction
Thread Control Block
Summary of Thread Services
Thread Creation
Thread Deletion
Identify Thread
Get Thread Information
Preemption-Threshold Change
Priority Change
Relinquish Control
Resume Thread Execution
Thread Sleep
Suspend Thread Execution
Terminate Application Thread
Time-Slice Change
Abort Thread Suspension
Thread Notification Services
Execution Overview
Thread States
Thread Design
Minimize the Number of Threads
Choose Priorities Carefully
Minimize the Number of Priorities
Consider Preemption-Threshold
Consider Priority Inheritance
Consider Round-Robin Scheduling
Consider Time-Slicing
Thread Internals
Overview
Key Terms and Phrases
Problems
Mutual Exclusion Challenges and Considerations
Mutual Exclusion Challenges and Considerations
Introduction
Protecting a Critical Section
Providing Exclusive Access to Shared Resources
Mutex Control Block
Summary of Mutex Services
Creating a Mutex
Deleting a Mutex
Obtaining Ownership of a Mutex
Retrieving Mutex Information
Prioritizing the Mutex Suspension List
Releasing Ownership of a Mutex
Avoiding the Deadly Embrace
Sample System Using a Mutex to Protect Critical Sections
Output Produced by Sample System
Listing for 06_sample_system.c
Mutex Internals
Overview
Key Terms and Phrases
Problems
Memory Management: Byte Pools and Block Pools
Memory Management: Byte Pools and Block Pools
Introduction
Summary of Memory Byte Pools
Memory Byte Pool Control Block
Pitfalls of Memory Byte Pools
Summary of Memory Byte Pool Services
Creating a Memory Byte Pool
Allocating from a Memory Byte Pool
Deleting a Memory Byte Pool
Retrieving Memory Byte Pool Information
Prioritizing a Memory Byte Pool Suspension List
Releasing Memory to a Byte Pool
Memory Byte Pool Example—Allocating Thread Stacks
Memory Byte Pool Internals
Summary of Memory Block Pools
Memory Block Pool Control Block
Summary of Memory Block Pool Services
Creating a Memory Block Pool
Allocating a Memory Block Pool
Deleting a Memory Block Pool
Retrieving Memory Block Pool Information
Prioritizing a Memory Block Pool Suspension List
Releasing a Memory Block
Memory Block Pool Example—Allocating Thread Stacks
Memory Block Pool Internals
Overview and Comparison
Key Terms and Phrases
Problems
Internal System Clock and Application Timers
Internal System Clock and Application Timers
Introduction
Internal System Clock Services
Application Timer Control Block
Summary of Application Timer Services
Creating an Application Timer
Activating an Application Timer
Changing an Application Timer
Deactivating an Application Timer
Deleting an Application Timer
Retrieving Application Timer Information
Sample System Using Timers to Measure Thread Performance
Listing for 8_sample_system.c
Overview
Key Terms and Phrases
Problems
Event Notification and Synchronization with Counting Semaphores
Event Notification and Synchronization with Counting Semaphores
Introduction
Counting Semaphore Control Block
Avoiding Deadly Embrace
Avoiding Priority Inversion
Summary of Counting Semaphore Services
Creating a Counting Semaphore
Deleting a Counting Semaphore
Getting an Instance of a Counting Semaphore
Retrieving Information about Counting Semaphores
Prioritizing a Counting Semaphore Suspension List
Placing an Instance in a Counting Semaphore
Placing an Instance in a Semaphore Using a Ceiling
Semaphore Notification and Event-Chaining
Comparing a Counting Semaphore with a Mutex
Sample System Using a Binary Semaphore in Place of a Mutex
Listing for 9a_sample_system.c
Sample System Using a Counting Semaphore in a Producer-Consumer Application
Listing for 9b_sample_system.c
Counting Semaphore Internals
Overview
Key Terms and Phrases
Problems
Synchronization of Threads Using Event Flags Groups
Synchronization of Threads Using Event Flags Groups
Introduction
Event Flags Group Control Block
Summary of Event Flags Group Control Services
Creating an Event Flags Group
Deleting an Event Flags Group
Getting Event Flags from an Event Flags Group
Retrieving Information about an Event Flags Group
Setting Event Flags in an Event Flags Group
Event Flags Group Notification and Event-Chaining
Sample System Using an Event Flags Group to Synchronize Two Threads
Listing for 10_sample_system.c
Event Flags Group Internals
Overview
Key Terms and Phrases
Problems
Thread Communication with Message Queues
Thread Communication with Message Queues
Introduction
Message Queue Control Block
Summary of Message Queue Services
Creating a Message Queue
Sending a Message to a Message Queue
Receiving a Message from a Message Queue
Deleting a Message Queue
Flushing the Contents of a Message Queue
Sending a Message to the Front of a Message Queue
Retrieving Message Queue Information
Prioritizing a Message Queue Suspension List
Message Queue Notification and Event-Chaining
Sample System Using a Message Queue for Interthread Communication
Listing for 11_sample_system.c
Message Queue Internals
Overview
Key Terms and Phrases
Problems
Advanced Topics
Advanced Topics
Introduction
Event-Chaining
Queue Event-Chaining
Semaphore Event-Chaining
Event Flags Group Event-Chaining
Sample System Using Event-Chaining
Listing for 12_sample_system.c
Run-Time Performance Metrics
Block Pool Performance Information
Byte Pool Performance Information
Event Flags Performance Information
Mutex Performance Information
Queue Performance Information
Semaphore Performance Information
Thread Performance Information
Timer Performance Information
Key Terms and Phrases
Case Study: Designing a Multithreaded System
Case Study: Designing a Multithreaded System
Introduction
Statement of Problem
Analysis of the Problem
Design of the System
Thread Design
Public Resources Design
Implementation
Listing of VAM System
Overview
Appendix A: Memory Block Pool Services
Appendix A: Memory Block Pool Services
Appendix B: Memory Byte Pool Services
Appendix B: Memory Byte Pool Services
Appendix C: Event Flags Group Services
Appendix C: Event Flags Group Services
Appendix D: Interrupt Control Service
Appendix D: Interrupt Control Service
Appendix E: Mutex Services
Appendix E: Mutex Services
Appendix F: Message Queue Services
Appendix F: Message Queue Services
Appendix G: Counting Semaphore Services
Appendix G: Counting Semaphore Services
Appendix H: Thread Services
Appendix H: Thread Services
Appendix I: Internal System Clock Services
Appendix I: Internal System Clock Services
Appendix J: Application Timer Services
Appendix J: Application Timer Services
Appendix K: ThreadX API
Appendix K: ThreadX API
Appendix L: ThreadX and the ARM Microprocessor
Appendix L: ThreadX and the ARM Microprocessor
Appendix M: ThreadX and the ColdFireMicroprocessor
Appendix M: ThreadX and the ColdFireMicroprocessor
Appendix N: ThreadX and the MIPS Microprocessor
Appendix N: ThreadX and the MIPS Microprocessor
Appendix O: ThreadX and the PowerPC Microprocessor
Appendix O: ThreadX and the PowerPC Microprocessor
Index
Index
A
B
C
D
E
F
G
H
I
K
L
M
N
O
P
Q
R
S
T
U
V
W
Newnes is an imprint of Elsevier 30 Corporate Drive, Suite 400, Burlington, MA 01803, USA Linacre House, Jordan Hill, Oxford OX2 8DP, UK Copyright © 2009, Elsevier Inc. 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. Permissions may be sought directly from Elsevier’s Science & Technology Rights   Department in Oxford, UK: phone: (44) 1865 843830, fax: (44) 1865 853333,   E-mail: permissions@elsevier.com. You may also complete your request   online via the Elsevier homepage (http://elsevier.com), by selecting “Support & Contact”   then “Copyright and Permission” and then “Obtaining Permissions.” Library of Congress Cataloging-in-Publication Data Application submitted British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library. ISBN: 978-1-85617-601-9 For information on all Newnes publications visit our Web site at www.elsevierdirect.com Typeset by Macmillan Publishing Solutions (www.macmillansolutions.com) 09 10 11   10 9 8 7 6 5 4 3 2 1   Printed in the United States of America
à mes ancêtres québecois
Preface The first edition of this book covered ThreadX1 (version 4) as well as information about the ARM® processor relative to ThreadX. The second edition of this book has been enhanced to address the features of ThreadX (version 5) and it includes a variety of concepts including real-time event-chaining2 and real-time performance metrics. Chapters 1 through 4 cover fundamental terminology and concepts of embedded and real-time systems. Chapters 5 through 11 investigate major ThreadX services and analyze several sample systems as well as solutions to classical problem areas. Chapter 12 is devoted to a study of advanced topics that include event-chaining and performance metrics. Chapter 13 contains a case study that illustrates how a system could be developed and implemented. Appendices A through K contain details of the ThreadX API and these appendices serve as a compact guide to all the available services. Appendices L though O contain information about the ARM3, Coldfire4, MIPS5, and PowerPC6 processors as used with ThreadX. Each of these appendices contains technical information, register set information, processor modes, exception and interrupt handling, thread scheduling, and context switching. Embedded systems are ubiquitous. These systems are found in most consumer electronics, automotive, government, military, communications, and medical equipment. Most individuals in developed countries have many such systems and use them daily, but relatively few people realize that these systems actually contain embedded computer systems. Although the field of embedded systems is young, the use and importance of these systems is increasing, and the field is rapidly growing and maturing. 1ThreadX is a registered trademark of Express Logic, Inc. The ThreadX API, associated data structures, and data types are copyrights of Express Logic, Inc. 2Event-chaining is a registered trademark of Express Logic, Inc. 3ARM is a registered trademark of ARM Limited 4ColdFire is a registered trademark of Freescale, Inc. 5MIPS is a registered trademark of MIPS Processors, Inc. 6PowerPC is a registered trademark of IBM Corporation www.newnespress.com
xvi    Preface This book is intended for persons who develop embedded systems, or for those who would like to know more about the process of developing such systems. Although embedded systems developers are typically software engineers or electrical engineers, many people from other disciplines have made significant contributions to this field. This book is specifically targeted toward embedded applications that must be small, fast, reliable, and deterministic.7 I assume the reader has a programming background in C or C, so we won’t devote any time to programming fundamentals. Depending on the background of the reader, the chapters of the book may be read independently. There are several excellent books written about embedded systems. However, most of these books are written from a generalist point of view. This book is unique because it is based on embedded systems development using a typical commercial RTOS, as well as widely used microprocessors. This approach has the advantage of providing specific knowledge and techniques, rather than generic concepts that must be converted to your specific system. Thus, you can immediately apply the topics in this book to your development efforts. Because an actual RTOS is used as the primary tool for embedded application development, there is no discussion about the merits of building your own RTOS or forgoing an RTOS altogether. I believe that the relatively modest cost of a commercial RTOS provides a number of significant advantages over attempts to “build your own.” For example, most commercial RTOS companies have spent years refining and optimizing their systems. Their expertise and product support may play an important role in the successful development of your system. As noted previously, the RTOS chosen for use in this book is ThreadX (version 5). This RTOS was selected for a variety of reasons including reliability, ease of use, low cost, widespread use, and the maturity of the product due to the extensive experience of its developers. This RTOS contains most of the features found in contemporary RTOSes, as well as several advanced features that are not. Another notable feature of this RTOS is the consistent and readable coding convention used within its application programming interface (API). Developing applications is highly intuitive because of the logical approach of the API. 7Such systems are sometimes called deeply embedded systems. www.newnespress.com
Preface    xvii Although I chose the C programming language for this book, you could use C instead for any of the applications described in this book. There is a CD included with this book that contains a limited ThreadX8 system. You may use this system to perform your own experiments, run the included demonstration system, and experiment with the projects described throughout the book. Typographical conventions are used throughout this book so that key concepts are communicated easily and unambiguously. For example, keywords such as main or int are displayed in a distinctive typeface, whether these keywords are in a program or appear in the discussion about a program. This typeface is also used for all program segment listings or when actual input or output is illustrated. When an identifier name such as MyVar is used in the narrative portion of the book, it will appear in italics. The italics typeface will also be used when new topics are introduced or to provide emphasis. 8Express Logic, Inc. has granted permission to use this demonstration version of ThreadX for the sample systems and the case study in this book. www.newnespress.com
Embedded and Real-time Systems Chapter 1 1.1 Introduction Although the history of embedded systems is relatively short,1 the advances and successes of this field have been profound. Embedded systems are found in a vast array of applications such as consumer electronics, “smart” devices, communication equipment, automobiles, desktop computers, and medical equipment.2 1.2 What is an embedded System? In recent years, the line between embedded and nonembedded systems has blurred, largely because embedded systems have expanded to a vast array of applications. However, for practical purposes, an embedded system is defined here as one dedicated to a specific purpose and consisting of a compact, fast, and extremely reliable operating system that controls the microprocessor located inside a device. Included in the embedded system is a collection of programs that run under that operating system, and of course, the microprocessor.3 1The first embedded system was developed in 1971 by the Intel Corporation, which produced the 4004 microprocessor chip for a variety of business calculators. The same chip was used for all the calculators, but software in ROM provided unique functionality for each calculator. Source: The Intel 4004 website at http://www.intel4004.com/ 2Approximately 98% of all microprocessors are used in embedded systems. Turley, Jim, The Two Percent Solution, Embedded Systems Programming, Vol. 16, No. 1, January 2003. 3The microprocessor is often called a microcontroller, embedded microcontroller, network processor, or digital signal processor; it consists of a CPU, RAM, ROM, I/O ports, and timers. www.newnespress.com
 Chapter 1 Because an embedded system is part of a larger system or device, it is typically housed on a single microprocessor board and the associated programs are stored in ROM.4 Because most embedded systems must respond to inputs within a small period of time, these systems are frequently classified as real-time systems. For simple applications, it might be possible for a single program (without an RTOS) to control an embedded system, but typically an RTOS or kernel is used as the engine to control the embedded system. 1.3 Characteristics of embedded Systems Another important feature of embedded systems is determinism. There are several aspects to this concept, but each is built on the assumption that for each possible state and each set of inputs, a unique set of outputs and next state of the system can be, in principle, predicted. This kind of determinism is not unique to embedded systems; it is the basis for virtually all kinds of computing systems. When you say that an embedded system is deterministic, you are usually referring to temporal determinism. A system exhibits temporal determinism if the time required to process any task is finite and predictable. In particular, we are less concerned with average response time than we are with worst-case response time. In the latter case, we must have a guarantee on the upper time limit, which is an example of temporal determinism. An embedded system is typically encapsulated by the hardware it controls, so end-users are usually unaware of its presence. Thus, an embedded system is actually a computer system that does not have the outward appearances of a computer system. An embedded system typically interacts with the external world, but it usually has a primitive or nonexistent user interface. The embedded systems field is a hybrid that draws extensively from disciplines such as software engineering, operating systems, and electrical engineering. Embedded systems has borrowed liberally from other disciplines and has adapted, refined, and enhanced those concepts and techniques for use in this relatively young field. 1.4 real-time Systems As noted above, an embedded system typically must operate within specified time constraints. When such constraints exist, we call the embedded system a real-time system. 4We often say that embedded systems are ROMable or scalable. www.newnespress.com
分享到:
收藏