Using the Renesas RL78 Microcontroller
Total Page:16
File Type:pdf, Size:1020Kb
Creating Fast, Responsive and Energy-Efficient Embedded Systems using the Renesas RL78 Microcontroller NorthAlexander Carolina G. State Dean University UniversityJames M. of Conrad North Carolina at Charlotte Micrim Press 1290 Weston Road, Suite 306 Weston, FL 33326 USA www.micrium.com Designations used by companies to distinguish their products are often claimed as trademarks. In all instances where Micrim Press 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 © 2012 by Alexander G. Dean and James M. Conrad except where noted otherwise. Published by Micrim Press. All rights reserved. Printed in the United States of America. No part of this publication may be reproduced or dis- tributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher; with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication. The programs and code examples in this book are presented for instructional value. The programs and examples have been carefully tested, but are not guaranteed to any particular purpose. The publisher and content contributors do 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 and content contributors assume 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. Library of Congress subject headings: 1. Embedded computer systems 2. Real-time data processing 3. Computer software—Development For bulk orders, please contact Micrium Press at: ϩ1 954 217 2036 ISBN: 978-1-935772-98-9 Please report errors or forward any comments and suggestions to [email protected] Preface When Renesas asked us to create a book on the RL78 microcontroller family, we jumped at the chance. It gave us the opportunity to present embedded systems as built upon con- cepts from critical areas (such as computer architecture and microarchitecture, memory systems, compilation methods, software engineering, kernels and operating systems, inter- process communication, and real-time systems) in a way which applies to traditional em- bedded systems. Many embedded texts present these concepts as they are applied to re- source-rich PC-like devices such as smartphones — the trade-offs and resulting outcomes are quite different. We hope we have succeeded. This book can be used on its own for an Introduction to Embedded Systems class or as a graduate level Embedded System Design, Analysis and Optimization class. It can also be used as a supplement in other types of classes where a microcontroller-based implementa- tion is necessary. This book would not have been possible had it not been for the assistance of numerous people. Several students and educators contributed to some of the chapters, including: Adam Harris (2), Suganya Jebasingh (2, 7), Michael Plautz (8), Sunil Gurram (11), Aswin Ramakrishnan (11), Vivek Srikantan (12), and Paul Mohan Das (12). Thanks go to the compositor, Linda Foegen, and especially to June Hay-Harris, Rob Dautel, and Todd DeBoer of Renesas for their help in getting this book produced and pub- lished (and for their patience!). Many, many thanks go to the reviewers who offered valu- able suggestions to make this book better, especially Mitch Ferguson and Anthony Harris. Alex Dean: I would like to thank Bill Trosky and Phil Koopman for opening the doors into so many embedded systems through in-depth design reviews. I would like to thank my students in my NCSU embedded systems courses for bringing their imagination, excite- ment, and persistence to class projects. I would also like to thank my wife Sonya for shar- ing her passion of seeking out and seizing opportunities, and our daughters Katherine and Jacqueline for making me smile every time I leave work to head home. Finally, I would like to thank my parents for planting the seeds of curiosity in my mind. Jim Conrad: I would like to personally thank my parents, the Conrads, and my in-laws, the Warrens, for their continued assistance and guidance through the years while I worked on books. Also, I would especially like to thank my children, Jay, Mary Beth, and Caroline, and my wife Stephanie, for their understanding when I needed to spend more time on the book than I spent with them. Alexander G. Dean and James M. Conrad March 2012 iii Foreword The world of MCU-based embedded designs can be divided into those that take advantage of existing code and MCUs, and those that require leading edge MCU architectures and im- plementations. Dr. Dean, with assistance from Dr. Conrad, spent more than a year internal- izing the inner workings and surrounding ecosystem of the newly-developed RL78 archi- tecture, and have generated a book showcasing the RL78 line which has a level of ultra low power and performance that has not been seen before. Indeed, by taking the best DNA of both the ex-NEC 78K MCU core and the Renesas R8C peripherals, and combining them into a new breed of MCU, the RL78 line enables design approaches that were previously spread across the low power or high performance camps and inaccessible in one device line. The authors leverage the capabilities of the RL78 and demonstrate their own expert grasp of all the dynamics that differentiate successful end solutions from those of the “me- too” variety. They recognize that successful embedded systems require more than just good hardware and software engineering. Alex and Jim have masterfully applied RL78 ca- pabilities to real world challenges using examples, applications and approaches that will surely empower individuals and teams of designers. Readers aspire to find books that have the right balance between depth and breadth. Where there is too much detail, the relevance can be obscured; while a broad brush ap- proach may trivialize the essence of key topics. This is especially true of books relating to embedded designs that must achieve a utilitarian purpose. Here, Alex and Jim skillfully navigate from topic to topic, knowing exactly when to throttle for maximum utility. Whether you are a university student preparing for the real world, a design engineer looking for leading edge approaches to time-critical processes, or a manager attempting to further your risk management techniques, you will find Alex’s approach to embedded sys- tems to be stimulating and compelling. Ali Sebt Renesas March 8, 2012 v Contents Preface iii Foreword v CHAPTER ONE Introduction to Embedded Systems 1 1.1 Learning Objectives 1 1.2 Concepts 1 1.2.1 Economics and Microcontrollers 1 1.2.2 Embedded Networks 2 1.3 Typical Benefits of Embedded Systems 2 1.3.1 Greater Performance and Efficiency 3 1.3.2 Lower Costs 3 1.3.3 More Features 4 1.3.4 Better Dependability 4 1.4 Embedded System Functions 4 1.5 Attributes of Embedded Systems 5 1.6 Constraints on Embedded Systems 6 1.7 Designing and Manufacturing Embedded Systems 6 1.8 An Example of an Embedded System: The EIN GreenEval Zigbee Module 7 1.9 Summary of Book Contents 8 1.10 Recap 9 1.11 References 9 CHAPTER TWO Microcontroller Concepts, Infrastructure, and Interfacing 11 2.1 Learning Objectives 11 2.2 Microcontroller-based Embedded System Hardware Basics 11 vii viii CONTENTS 2.3 Infrastructure: Power, Clock, and Reset 12 2.3.1 Power Supply 12 2.3.2 Clock Signal Generation 13 2.3.3 Reset Circuit 15 2.4 Interfacing with Digital Signals 15 2.4.1 General Purpose Digital I/O Basics 15 2.4.1.1 Control Registers 16 2.4.1.2 C Code Support for Accessing Control Registers 16 2.4.1.3 Automatic Code Generation 17 2.4.2 Using LEDs as Outputs 21 2.4.3 Driving a Common Signal with Multiple MCUs 22 2.4.4 Using Switches as Inputs 24 2.4.5 Scanning Matrix Keypads 25 2.4.6 Driving Motors and Coils 27 2.4.7 Voltage Level Shifting 29 2.5 Interfacing with Analog Signals 29 2.5.1 Analog Comparison 30 2.5.2 Multi-bit Analog to Digital Conversion 30 2.5.3 Digital to Analog Conversion 32 2.5.3.1 Resistor Networks 33 2.5.3.2 Pulse Width Modulation 35 2.6 Recap 35 2.7 References 35 2.8 Exercises 35 CHAPTER THREE RL78 CPU Core and Interrupts 37 3.1 Learning Objectives 37 3.2 CPU Concepts 37 CONTENTS ix 3.3 RL78 CPU Core 40 3.3.1 RL78 Instruction Set Architecture 40 3.3.1.1 Data Types 40 3.3.1.2 Instruction Set 40 3.3.2 Program Memory, Instruction Fetch, and Decode 41 3.3.3 General Purpose and Special Registers 42 3.3.3.1 General Purpose Registers 42 3.3.3.2 Special Registers 42 3.3.4 Data Memory 43 3.3.4.1 Memory Map 44 3.3.4.2 Addressing Modes 45 3.3.5 Arithmetic/Logic Unit 47 3.3.6 Instruction Processing Pipeline 47 3.3.6.1 RL78 Pipeline Structure 48 3.3.6.2 RL78 Pipeline Hazards 48 3.4 Interrupts 49 3.4.1 Breakfast with Polling vs.