Embedded Multitasking This Page Intentionally Left Blank Embedded Multitasking Keith Curtis AMSTERDAM(cid:1) BOSTON(cid:1) HEIDELBERG (cid:1) LONDON NEWYORK(cid:1) OXFORD(cid:1) PARIS(cid:1) SAN DIEGO SAN FRANCISCO (cid:1) SINGAPORE (cid:1) SYDNEY (cid:1) TOKYO (cid:3)(cid:5)(cid:17)(cid:10)(cid:5)(cid:14)(cid:1)(cid:7)(cid:14)(cid:1)(cid:4)(cid:10)(cid:1)(cid:7)(cid:9)(cid:12)(cid:13)(cid:7)(cid:10)(cid:15)(cid:1)(cid:11)(cid:6)(cid:1)(cid:2)(cid:8)(cid:14)(cid:5)(cid:16)(cid:7)(cid:5)(cid:13) Newnes is an imprint of Elsevier 30 Corporate Drive, Suite 400, Burlington, MA 01803, USA Linacre House, Jordan Hill, Oxford OX2 8DP, UK Copyright © 2006, 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 RightsDepartment inOxford, UK: phone: (+44) 1865 843830, fax: (+44) 1865 853333, e-mail: [email protected]. You may alsocomplete your request online via the Elsevier homepage (www.elsevier.com), byselecting “Customer Support” and then “Obtaining Permissions.” Recognizing the importance of preserving what has been written, Elsevier prints its books on acid-free paper whenever possible. Library of Congress Cataloging-in-Publication Data Curtis, Keith, 1961- Embedded multitasking / Keith Curtis. p. cm. Includes index. ISBN 0-7506-7918-2 (pbk. : alk. paper) 1. Embedded computer systems. 2. Computer firmware--Design. 3. Embedded computer systems--Programming. I. Title. TK7895.E42C87 2006 004’.35--dc22 2005029822 British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library. ISBN-13: 978-0-7506-7918-3 ISBN-10: 0-7506-7918-2 For information on all Newnes publications visit our website at www.books.elsevier.com. 06 07 08 09 10 10 9 8 7 6 5 4 3 2 1 Printed in the United States of America Dedication Paraphrasing an old saying: “Give a man a tool, he becomes a repair man.” “Teach him how to make his own tools, he becomes an engineer!” This book is dedicated to all the embedded engineers out there adding intelligence to the everyday things in our lives. And to my wife Shirley, who has put up with this, and all my other projects, through a combination of patience and understanding. This Page Intentionally Left Blank Contents About the Author xi What’s on the CD-ROM? xiii 1 What’s In This Book, and Why Should I Read It? 1 Engineering and Profits 4 2 Basic Embedded Programming Concepts 19 Numbering Systems 20 Binary Numbers 22 Signed Binary Numbers 23 Fixed-Point Binary Numbers 25 Floating-Point Binary Numbers 27 Alternate Numbering Systems 28 Binary-Coded Decimal 29 ASCII 30 Error Detection 32 Data Structures 33 Simple Data Types 34 Complex Data Types 43 Communications Protocols 51 Simple Data Broadcast 51 Event-Driven Single Transfer 54 Event-Driven Multielement Transfers 56 Mathematics 61 Binary Addition and Subtraction 61 Binary Multiplication 64 viii Contents Binary Division 66 Numeric Comparison 69 Conditional Statements 71 Loops 78 Other Flow Control Statements 81 State Machines 83 Data-Indexed State Machines 89 Execution-Indexed State Machines 92 Hybrid State Machines 97 Multitasking 99 Four Basic Requirements of Multitasking 101 Context Switching 101 Communications 102 Managing Priorities 102 Timing Control 103 Operating Systems 103 State Machine Multitasking 108 3 System-Level Design 111 Dissecting the Requirements Document 113 Function List 114 Function List Questions 115 The User Interface 120 Communications 129 Timing Requirements 139 System Priorities 146 Error Handling 154 System-Level Design 157 Task Definition 158 Criteria for Compatible Software Functions 161 Criteria for Incompatible Software Functions 164 Communications 171 Timing Analysis 182 Priority Handler 189 Error Recovery 192 System-Level Design Documentation 198 Contents ix 4 Component-Level Design 203 Task State Machines 203 Communications 223 System Timing 240 Priority Handler 252 Error Recovery System 271 5 Implementation and Testing 289 Building a Testing Driver Routine 307 6 Does It Do the Job? 379 Index 383