An Introduction to Computer Engineering Using the Renesas Sakura Microcontroller Board
Total Page:16
File Type:pdf, Size:1020Kb
An Introduction to Computer Engineering using the Renesas Sakura Microcontroller Board BY JAMES M. CONRAD Micri m 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 Micri m 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 © 2014 by James M. Conrad except where noted otherwise. Published by Micri m Press. All rights reserved. Printed in the United States of America. No part of this publication may be reproduced or distributed 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 ex - ception that the program listings may be entered, stored, and executed in a computer system, but they may not be repro - duced 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 Micriµm Press at: ϩ1 954 217 2036 ISBN: 978-1-935772-92-7 Please report errors or forward any comments and suggestions to [email protected]. Preface This book is the result of a long relationship the author has enjoyed with Renesas Electronics America, Inc. (and one of its predecessors, Mitsubishi Electronics). I originally worked with this company because of their commitment to providing a low-cost evaluation board and free development software that students could purchase and use in classes and senior design projects. Over the years the boards have remained as affordable (and popu - lar) as ever, and the software development tools available have added more functionality while still available for free to our students. I have been teaching embedded systems courses for over fourteen years (and working in the field even longer). I had not been able to find a book suitable for using in an Introduction to Computer Engineering course that would lend itself to the theoretical and applied nature of the discipline and embedded systems design. When Renesas released the GR-SAKURA board, I knew I have the perfect platform to use in the course. This book was developed to augment the hands-on exercises we use. This book also has a radical fea - ture not seen in many books currently on the market (if any). It is freely available for down - load. It is also available for purchase in hardcopy form for a modest price. This book would not have been possible had it not been for the assistance of numerous people. Several students and educators contributed to and extensively tested some of the chapters, including:Yevgeny Fridlyand (2, 3, 4), Adam Harris (1, 2, 3), Anthony Harris (3), Onkar Raut (2, 4) Suganya Jebasingh (2, 4), and Steven Erdmanczyk (4) . Thanks go to the publisher, Linda Foegen, and especially June Harris, Rob Dautel and Todd DeBoer of Renesas for their help in getting this book produced and published (and for their patience!). Many, many thanks go to the reviewers who offered valuable suggestions to make this book better, especially David Brown and students from my UNC Charlotte Introduction to Engineering and Embedded Systems courses. 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. James M. Conrad, March 2014 iii Foreword For more than a decade the microcontroller world has been dominated by the quest for ultra-low power, high performance devices—two goals that are typically mutually exclu - sive. The Renesas RX MCU quickly achieved market leadership by achieving both of these goals with a highly innovative architecture. The RX family enables embedded designs that previously would have required some uncomfortable tradeoffs. However there are no simple solutions to complex problems, and mastering all of the RX63N’s features is not a task to be undertaken lightly. Fortunately in this book Dr. Conrad, has crafted a guidebook for embedded developers that moves smoothly from concepts to coding in a manner that is neither too high level to be useful nor too detailed to be clear. It explains beginning software engineering techniques and shows how to implement them in RX63N-based applications, moving from a clear explanation of problems to techniques for solving them to line-by-line explanations of example code. Modern embedded applications increasingly require hardware/software co-design, though few engineers are equally conversant with both of these disciplines. In this book the author takes a holistic approach to design, both explaining and demonstrating just how soft - ware needs to interact with Sakura hardware. Striking a balance between breadth and depth it should prove equally useful and illuminating for both hardware and software engineers. Whether you are a university student honing your design skills, a design engineer look - ing for leading edge approaches to time-critical processes, or a manager attempting to fur - ther your risk management techniques, you will find Jim’s approach to embedded systems to be stimulating and compelling. Peter Carbone Renesas v Contents Preface iii Foreword v CHAPTER 1 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 Developing Embedded Systems 6 1.7.1 Product Development 7 1.7.2 Designing and Manufacturing Embedded Systems 8 1.7.3 The Role of a Computer Engineer 9 1.8 An Example of an Embedded System: The Renesas Sakura Board 10 1.9 Summary of Book Contents 10 1.10 Recap 10 1.11 References 11 vii viii CONTENTS CHAPTER 2 Introduction to Software Development 13 2.1 Learning Objectives 13 2.2 Computer Organization & Architecture 13 2.3 Microcontroller Basics 16 2.3.1 Bits and Bytes 16 2.3.2 Ports 17 Port Direction Register (PDR) 17 Set Switch 1 (Port A bit 7) as Input 18 Port Output Data Register (PODR) 18 Set LED0 (Port A bit 0) as Output 19 Sets LEDs 1, 2, 3, and 4 (Port A bit 0, 1, 2, and 3) as Outputs 19 Port Input Data Register (PIDR) 19 Port Mode Register (PMR) 20 2.3.3 Data Types 20 2.4 Software Development Tools 22 2.4.1 Compilation Mechanism 22 2.4.2 Compilers for Embedded Systems 23 2.4.3 Debugger 24 2.4.4 Example of an Integrated Development Environment 24 2.5 Basic Software Development 26 2.5.1 Development Lifecycle Overview 26 2.5.2 Define System Requirements 27 2.5.3 Requirements—Robotics Applications 29 2.5.4 Developing Code 30 2.5.5 Start with an Algorithm 30 2.5.6 Convert your Algorithms to Code 31 2.6 Recap 33 2.7 References 33 2.8 Exercises 33 CONTENTS ix CHAPTER 3 Getting Started with GR-Sakura 35 3.1 Learning Objectives 35 3.2 Sakura Embedded System 35 3.2.1 Sakura Board Design 35 3.2.2 Basic Concepts of the RX63N Microprocessor 38 3.3 Sakura Software Development 44 3.4 Sakura Example Project 46 3.5 Updating the GR-Sakura Firmware 50 3.6 Setting Up an Account with MyRenesas 54 3.7 BASIC Motor & Sensor Control 57 3.7.1 Switch Controlled LEDs 57 Using LEDs as Outputs 58 Using Switches as Inputs 58 3.7.2 Driving Motors and Coils 61 3.8 Basic Robotics Application 63 3.8.1 Motor Robot Kit Assembly 63 3.8.2 Understanding the Hardware 68 3.8.3 Developing Code 72 3.9 Recap 75 3.10 References 75 3.11 Exercises 76 CHAPTER 4 Interfacing with the Outside World 77 4.1 Learning Objectives 77 4.2 Event Counters and Timers 77 Event Counter 78 x CONTENTS 4.2.1 Setting Up a Timer for Counting Events 81 Timer Count Register 81 Timer Counter Control Register 81 Time Constant Register 83 Timer Control/Status Register 84 Timer Control Register 85 4.2.2 Cascading Timers 87 4.2.3 Using Interrupts as Event Counters 92 void interrupts (void) 93 void noInterrupts (void) 93 void detachInterrupt (unsigned char interrupt) 93 interrupt—number (0–7) 93 void attachInterrupt (unsigned char interrupt, void (*) (void) func, int mode) 94 interrupt—numbers (0–7) 94 4.2.4 Setting Up a Timer for Pulse Output 97 4.2.5 Setting up Timer interrupts for Pulse Output 101 4.2.6 Frequency and Period Control 105 4.2.7 Advanced Motor Control: Robotics Application 106