www.SafeTTy.net
Patterns for time-triggered embedded systems
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net
ACM PRESS BOOKS
This book is published as part of the ACM Press Books – a collaboration between the Association for Computing Machinery and Addison-Wesley. ACM is the oldest and largest education and scientific society in the information technology field. Through its high-quality publications and services, ACM is a major force in advancing the skills and knowledge of IT professionals throughout the world. For further information about ACM contact:
ACM Member Services ACM European Service Center 1515 Broadway, 17th Floor 108 Cowley Road New York NY 10036-5701 Oxford OX4 1JF Phone: +1 212 626 0500 United Kingdom Fax: +1 212 944 1318 Phone: +44 1865 382338 Email: [email protected] Fax: +44 1865 381338 Email: [email protected] URL: http://www.acm.org
SELECTED ACM TITLES:
Software Requirements and Specification: A Lexicon of Software Practice, Principles and Prejudices Michael Jackson
Software Test Automation: Effective Use of Text Execution Tools Mark Fewster and Dorothy Graham
Test Process Improvement: A Practical Step-by-step Guide to Structured Testing Tim Koomen and Martin Pol
Mastering the Requirements Process Suzanne Robertson and James Robertson
Bringing Design to Software: Expanding Software Development to Include Design Terry Winograd, John Bennett, Laura de Young, Bradley Hartfield
Software for Use: A Practical Guide to the Models and Methods of Usage Centered Design Larry L. Constantine and Lucy A. D. Lockwood
Problem Frames: Analyzing and Structuring Software Development Problems Michael Jackson
Software Blueprints: Lightweight Uses of Logic in Conceptual Modelling David Robertson and Jaume Agusti
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net
Patterns for time-triggered embedded systems
Building reliable applications with the 8051 family of microcontrollers
Michael J. Pont
The Keil compiler (demo) and associated files on the CD-ROM enclosed with this book have been authored and developed by Keil (UK) Ltd. © Keil (UK) Ltd 2001.
acm PRESS
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net
PEARSON EDUCATION LIMITED
Head Office: London Office: Edinburgh Gate 128 Long Acre Harlow CM20 2JE London WC2E 9AN Tel: +44 (0)1279 623623 Tel: +44 (0)20 7447 2000 Fax: +44 (0)1279 431059 Fax: +44 (0)20 7240 5771 Websites: www.it-minds.com www.aw.com/cseng/
First published in Great Britain in 2001
© ACM Press 2001
ISBN 0 201 33138 1
The right of Michael Pont to be identified as Author of this Work has been asserted by him in accordance with the Copyright, Designs, and Patents Act 1988.
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 either the prior written permission of the Publishers or a licence permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London W1P 0LP. This book may not be lent, resold, hired out or otherwise disposed of by way of trade in any form of binding or cover other than that in which it is published, without the prior consent of the Publishers.
The programs in this book have been included for their instructional value. The publisher does not offer any warranties or representations in respect of their fitness for a particular purpose, nor does the publisher accept any liability for any loss or damage arising from their use.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Pearson Education Limited has made every attempt to supply trademark information about manufacturers and their products mentioned in this book.
The publishers wish to thank the following for permission to reproduce the material: Arizona Microchip Technology Ltd; Allegro Microsystems; Atmel Corporation; Infineon; Philips Semiconductors; Texas Instruments.
Two of the figures in this book (Figure 3.2 and 3.4) reproduce information provided by Atmel Corporation. Atmel® warrants that it owns these materials and all intellectual property related thereto. Atmel, however, expressly and explicitly excludes all other warranties, insofar as it relates to this book, including accuracy or applicability of the subject matter of the Atmel materials for any purpose.
British Library Cataloguing-in-Publication Data A CIP catalogue record for this book can be obtained from the British Library.
Library of Congress Cataloging in Publication Data Applied for.
10 9 8 7 6 5 4 3 2 1
Designed by Claire Brodmann Book Designs, Lichfield, Staffs Typeset by Pantek Arts Ltd, Maidstone, Kent. Printed and bound in the United States of America.
The Publishers’ policy is to use paper manufactured from sustainable forests.
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net
This book is dedicated to my parents, Barbara and Gordon Pont
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net
Contents
Foreword page xiv Preface xvi
Introduction 1
1 What is a time-triggered embedded system? 3 1.1 Introduction 3 1.2 Information systems 3 1.3 Desktop systems 5 1.4 Real-time systems 6 1.5 Embedded systems 8 1.6 Event-triggered systems 10 1.7 Time-triggered systems 11 1.8 Conclusions 14
2 Designing embedded systems using patterns 15 2.1 Introduction 15 2.2 Limitations of existing software design techniques 17 2.3 Patterns 22 2.4 Patterns for time-triggered systems 24 2.5 Conclusions 25
Part A Hardware foundations 27
3 The 8051 microcontroller family 29 STANDARD 8051 30 SMALL 8051 41 EXTENDED 8051 46
4 Oscillator hardware 53 CRYSTAL OSCILLATOR 54 CERAMIC RESONATOR 64
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net viii CONTENTS
5 Reset hardware 67 RC RESET 68 ROBUST RESET 77
6 Memory issues 81 ON- CHIP MEMORY 82 OFF- CHIP DATA MEMORY 94 OFF- CHIP CODE MEMORY 100
7 Driving DC loads 109 NAKED LED 110 NAKED LOAD 115 IC BUFFER 118 BJT DRIVER 124 IC DRIVER 134 MOSFET DRIVER 139 SSR DRIVER (DC) 144
8 Driving AC loads 148 EMR DRIVER 149 SSR DRIVER ( AC) 156
Part B Software foundations 159
9 A rudimentary software architecture 161 SUPER LOOP 162 PROJECT HEADER 169
10 Using the ports 173 PORT I/ O 174 PORT HEADER 184
11 Delays 193 HARDWARE DELAY 194 SOFTWARE DELAY 206
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net CONTENTS ix
12 Watchdogs 215 HARDWARE WATCHDOG 217
Part C Time-triggered architectures for single-processor systems 229
13 An introduction to schedulers 231 13.1 Introduction 231 13.2 The desktop OS 231 13.3 Assessing the super loop architecture 233 13.4 A better solution 235 13.5 Example: Flashing an LED 239 13.6 Executing multiple tasks at different time intervals 243 13.7 What is a scheduler? 245 13.8 Co-operative and pre-emptive scheduling 246 13.9 A closer look at pre-emptive schedulers 250 13.10 Conclusions 253
14 Co-operative schedulers 254 CO- OPERATIVE SCHEDULER 255
15 Learning to think co-operatively 297 LOOP TIMEOUT 298 HARDWARE TIMEOUT 305
16 Task-oriented design 316 MULTI- STAGE TASK 317 MULTI- STATE TASK 322
17 Hybrid schedulers 332 HYBRID SCHEDULER 333
Part D The user interface 359
18 Communicating with PCs via RS-232 361 PC LINK ( RS-232) 362
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net x CONTENTS
19 Switch interfaces 397 SWITCH INTERFACE ( SOFTWARE) 399 SWITCH INTERFACE ( HARDWARE) 410 ON- OFF SWITCH 414 MULTI- STATE SWITCH 423
20 Keypad interfaces 433 KEYPAD INTERFACE 434
21 Multiplexed LED displays 449 MX LED DISPLAY 450
22 Controlling LCD panels 465 LCD CHARACTER PANEL 467
Part E Using serial peripherals 491
23 Using ‘I2C’ peripherals 493 I 2C PERIPHERAL 494
24 Using ‘SPI’ peripherals 520 SPI PERIPHERAL 521
Part F Time-triggered architectures for multiprocessor systems 537
25 An introduction to shared-clock schedulers 539 25.1 Introduction 539 25.2 Additional CPU performance and hardware facilities 539 25.3 The benefits of modular design 541 25.4 How do we link more than one processor 543 25.5 Why additional processors may not always improve reliability 550 25.6 Conclusions 552
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net CONTENTS xi
26 Shared-clock schedulers using external interrupts 553 SCI SCHEDULER ( TICK) 554 SCI SCHEDULER ( DATA) 593
27 Shared-clock schedulers using the UART 608 SCU SCHEDULER ( LOCAL) 609 SCU SCHEDULER ( RS-232) 642 SCU SCHEDULER ( RS-485) 646
28 Shared-clock schedulers using CAN 675 SCC SCHEDULER 677
29 Designing multiprocessor applications 711 DATA UNION 712 LONG TASK 716 DOMINO TASK 720
Part G Monitoring and control components 725
30 Pulse-rate sensing 727 HARDWARE PULSE COUNT 728 SOFTWARE PULSE COUNT 736
31 Pulse-rate modulation 741 HARDWARE PRM 742 SOFTWARE PRM 748
32 Using analogue-to-digital converters (ADCs) 756 ONE- SHOT ADC 757 ADC PRE- AMP 777 SEQUENTIAL ADC 782 A - A FILTER 794 CURRENT SENSOR 802
33 Pulse-width modulation 807 HARDWARE PWM 808
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net xii CONTENTS
PWM SMOOTHER 818 3-LEVEL PWM 822 SOFTWARE PWM 831
34 Using digital-to-analog converters (DACs) 840 DAC OUTPUT 841 DAC SMOOTHER 853 DAC DRIVER 857
35 Taking control 860 PID CONTROLLER 861
Part H Specialized time-triggered architectures 891
36 Reducing the system overheads 893 255-TICK SCHEDULER 894 ONE- TASK SCHEDULER 911 ONE- YEAR SCHEDULER 919
37 Increasing the stability of the scheduling 931 STABLE SCHEDULER 932
Conclusions 941
38 What this book has tried to do 941 38.1 Introduction 943 38.2 What this book has tried to do 943 38.3 Conclusions 943
39 Collected references and bibliography 946 39.1 Complete list of publications 946 39.2 Other pattern collections 952 39.3 Design techniques for real-time/embedded systems 952 39.4 Design techniques for high-reliability systems 953 39.5 The 8051 microcontroller 954 39.6 Related publications by the author 954
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net CONTENTS xiii
Appendices 955
A The design notation and CASE tool 957 Overview 957 The CASE tool 957 The notation 957
B Guide to the CD 980 Overview 980 The basis of the CD 980 The source code for this book 980
C Guide to the WWW site 982 Overview 982 The URL 982 Contents of the WWW site 982 Bug reports and code updates 982
Index 985
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net
Foreword
You hold in your hands a pattern language. If you could measure its distance from the topics of my patterns, you would find a sizeable gap. In spirit, however, Michael is right on target. Ward Cunningham and I worked during the early days of the commercialization of Smalltalk. Smalltalk had been designed from the beginning to be a seamless environ- ment. You could be using a word processor written in Smalltalk, start up a debugger, modify the program and continue typing. Some of the first Tektronix customers for Smalltalk were pretty odd. We often talked about Ray, an old guy from a big chemical company who latched onto Smalltalk and really made it jump and run, presenting and manipulating experimen- tal data. Watching one of his demos was a delight, because he was so proud of what he had accomplished. Reading Ray’s code was another matter entirely. He would do anything and every- thing, no matter how hideous, to get his programs to work. The result was a mess that was totally unmaintainable and only used a fraction of the power of Smalltalk. We often used Ray as the personification of the audience we wanted for our software – people who have problems to solve and have to construct software to solve them. We could easily contrast this utilitarian attitude with our ‘no compromise engineering’ atti- tude towards software, where the simplicity and elegance of the solution were more important than the problem solved. We could see that if we wanted to affect the world, we couldn’t just pursue our visions of beauty, we would have to try to help Ray at the same time. The resulting pattern language was a curious blend of high-minded advice (‘never use a computer you can’t personally turn off’) and banal bookkeeping chores (‘have the braces in your source code form rectangles’). The intent was to help Ray get more out of Smalltalk. In this we largely failed. Looking at my career since then, I have drift- ed more and more to giving advice to people coaching people who write programs for people who write programs for... That’s why I loved reading Michael’s early draft. It brought back that feeling of opening up a field of endeavour to someone who just has a problem to solve and who doesn’t want to be an expert in the solution. Now I’m Ray. I’d love to whip together little microcontrollers to solve various problems (okay, so I’m a nerd). Reading this pat- tern language gives me the confidence that I could do just that. Far from just giving me the smell of rosin in my nose and the feel of a wire wrap gun in my hand, these patterns stand as an example of how much more can be done with patterns than is commonly attempted. Patterns at their best bridge the gap between
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net FOREWORD xv problem and solution. They connect human needs and emotions with technology. And they open up new possibilities for people who just have a problem to solve. Fire up your soldering iron and enjoy.
Kent Beck Three Rivers Institute Merlin, Oregon
Copyright (c) 2014 SafeTTy Systems Ltd www.SafeTTy.net
Preface
Embedded software is ubiquitous. It forms a core component of an enormous range of systems, from aircraft, passenger cars and medical equipment, to children’s toys, video recorders and microwave ovens. This book provides a complete and coherent set of software patterns to support the development of this type of application. The remainder of this preface attempts to provide answers to more detailed ques- tions which prospective readers may have about the contents.
I What are the key features of this book?