21
Building Embedded Systems Programmable Hardware Changyi Gu

Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

Embed Size (px)

Citation preview

Page 1: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

Building Embedded Systems

Programmable Hardware

Changyi Gu

Page 2: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

Building Embedded Systems

Changyi Gu San DiegoCalifornia, USA

ISBN-13 (pbk): 978-1-4842-1918-8 ISBN-13 (electronic): 978-1-4842-1919-5DOI 10.1007/978-1-4842-1919-5

Library of Congress Control Number: 2016941346

Copyright © 2016 by Changyi Gu

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Managing Director: Welmoed SpahrLead Editor: Jonathan GennickDevelopment Editor: Douglas PundickTechnical Reviewer: Brendan HoranEditorial Board: Steve Anglin, Pramila Balen, Louise Corrigan, Jim DeWolf, Jonathan Gennick,

Robert Hutchinson, Celestin Suresh John, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing

Coordinating Editor: Jill BalzanoCopy Editor: Kezia EndsleyCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi Global

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected] , or visit www.springer.com . Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

For information on translations, please e-mail [email protected] , or visit www.apress.com .

Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales .

Any source code or other supplementary material referenced by the author in this text is available to readers at www.apress.com . For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ .

Printed on acid-free paper

Page 3: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

To those who enjoy doing things that are otherwise meaningless in the eyes of many.

Page 4: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051
Page 5: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

Contents at a Glance

About the Author .....................................................................................................xv

About the Technical Reviewer ...............................................................................xvii

Acknowledgments ..................................................................................................xix

Introduction ............................................................................................................xxi

■Chapter 1: The Whole Picture ................................................................................ 1

■Chapter 2: Power On and Bootloader .................................................................... 5

■Chapter 3: Inside the CPU .................................................................................... 27

■Chapter 4: RAM, DMA, and Interrupt ................................................................... 47

■Chapter 5: Bus Architecture ................................................................................ 69

■Chapter 6: Firmware Coding in C ....................................................................... 103

■Chapter 7: Firmware Coding in the C++ and Script Languages ........................ 129

■Chapter 8: Building and Deployment ................................................................. 149

■Chapter 9: Field-Programmable Gate Arrays ..................................................... 191

■Chapter 10: System on Programmable Chip (SOPC) .......................................... 233

■Chapter 11: De-Bounce, Power Management, and Display ................................ 249

■Chapter 12: Fixed Point Math ............................................................................ 271

■Chapter 13: Popular Ways of Console Communications: Prepare the Tools ...... 305

■Chapter 14: Work Flow ...................................................................................... 313

Index ..................................................................................................................... 317

v

Page 6: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051
Page 7: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

Contents

About the Author .....................................................................................................xv

About the Technical Reviewer ...............................................................................xvii

Acknowledgments ..................................................................................................xix

Introduction ............................................................................................................xxi

■Chapter 1: The Whole Picture ................................................................................ 1

Organization of the Book .................................................................................................. 3

Companion Materials for this Book .................................................................................. 4

References ....................................................................................................................... 4

■Chapter 2: Power On and Bootloader .................................................................... 5

What Happens After Powering On .................................................................................... 5

What Does Bootloader Do? ............................................................................................... 7

User Image (ROM Image).................................................................................................. 8

Image of Embedded OS .......................................................................................................................... 8

Image of Object File ................................................................................................................................ 9

Flash Image and XIP (Execute In Place) ................................................................................................ 14

Flash Memory ................................................................................................................. 20

Burn the Flash (PROM Program) ........................................................................................................... 20

NOR Flash and NAND Flash .................................................................................................................. 20

Pre-Loader ............................................................................................................................................ 22

Flash File System ................................................................................................................................. 23

Summary ........................................................................................................................ 24

References ..................................................................................................................... 25

vii

Page 8: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

■ CONTENTS

viii

■Chapter 3: Inside the CPU .................................................................................... 27

Von Neumann Architecture and Memory Barrier ............................................................ 27

Harvard Architecture, Modifi ed Harvard Architecture ..................................................... 29

CISC and RISC ................................................................................................................ 31

SIMD/VLIW ...................................................................................................................... 34

CPU Debugging ............................................................................................................... 34

CPU Simulator ....................................................................................................................................... 34

ROM Monitor ......................................................................................................................................... 35

Introduction to ICE (In Circuit Emulator) ............................................................................................... 35

Non-JTAG ICE ........................................................................................................................................ 36

JTAG-Based ICE .................................................................................................................................... 38

Using Breakpoints ................................................................................................................................ 42

Trace ..................................................................................................................................................... 44

Summary ........................................................................................................................ 46

References ..................................................................................................................... 46

■Chapter 4: RAM, DMA, and Interrupt ................................................................... 47

Types of Random Access Memory .................................................................................. 47

NVRAM (Non-Volatile Random Access Memory) ................................................................................... 47

Traditional SRAM: Intel 8080 vs. Motorola 6800 ................................................................................... 47

DDR SDRAM (Dual Date Rate Synchronous Dynamic RAM) .................................................................. 50

QDR SRAM ............................................................................................................................................ 53

Other Types of RAM .............................................................................................................................. 54

PIO and DMA .................................................................................................................. 55

PIO ........................................................................................................................................................ 55

DMA and MMU ...................................................................................................................................... 56

Memory Allocation for DMA .................................................................................................................. 59

Cache/Write Buffer and Memory Incoherence ...................................................................................... 60

Interrupt ......................................................................................................................... 61

Interrupt Mechanism ............................................................................................................................ 61

Shared Interrupt ................................................................................................................................... 62

Page 9: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

■ CONTENTS

ix

VIC (Vectored Interrupt Controller) ........................................................................................................ 66

Interrupt from Watchdog Timer ............................................................................................................. 67

Message Signaled Interrupt (MSI) ........................................................................................................ 67

Summary ........................................................................................................................ 68

References ..................................................................................................................... 68

■Chapter 5: Bus Architecture ................................................................................ 69

Bus with System Synchronous Timing ........................................................................... 69

Bus with Source Synchronous Timing ............................................................................ 70

From Parallel Bus to Serial Bus ...................................................................................... 72

The Drawbacks of Parallel Bus ............................................................................................................. 73

The Benefi t of Differential Serial Link ................................................................................................... 75

Implementation of Differential Serial Link ...................................................................... 77

Out-of-band Clocking ........................................................................................................................... 77

Embedded Clocking .............................................................................................................................. 79

8b/10b Encoding and Signal Conditioning ............................................................................................ 80

Bus Standards ................................................................................................................ 80

RS-232 .................................................................................................................................................. 80

I2C and SMBus ..................................................................................................................................... 81

SPI ........................................................................................................................................................ 82

PCI overview ......................................................................................................................................... 82

USB overview ....................................................................................................................................... 85

USB device implementation.................................................................................................................. 87

Ethernet ................................................................................................................................................ 92

Summary ...................................................................................................................... 101

References ................................................................................................................... 101

■Chapter 6: Firmware Coding in C ....................................................................... 103

Overview ...................................................................................................................... 103

The Confusion of Tongues ............................................................................................ 105

Page 10: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

■ CONTENTS

x

Firmware Coding in the C Language ............................................................................ 106

Explicit Type of Bit-Width .................................................................................................................... 106

Align the Data Structure ..................................................................................................................... 107

Debug Print ......................................................................................................................................... 109

Compile Time Assertion ...................................................................................................................... 112

Volatile, Const, and Peripheral Registers ............................................................................................ 112

Finite State Machine ........................................................................................................................... 121

Firmware Version and About Signature .............................................................................................. 125

Summary ...................................................................................................................... 127

References ................................................................................................................... 127

■Chapter 7: Firmware Coding in the C++ and Script Languages ........................ 129

Firmware Coding in C++ .............................................................................................. 129

Enhancements to the C Language ...................................................................................................... 130

New Concepts Offered by C++ ........................................................................................................... 136

C/C++ Mixed Programming ......................................................................................... 142

Script Languages Overview.......................................................................................... 143

Compiled Versus Script Languages .................................................................................................... 143

Tcl/Tk, Expect, Perl, and Python ......................................................................................................... 144

Documentation Generator ................................................................................................................... 145

Summary ...................................................................................................................... 146

References ................................................................................................................... 147

■Chapter 8: Building and Deployment ................................................................. 149

The Build Process ......................................................................................................... 149

Makefi le Basics .................................................................................................................................. 149

Simple Makefi le .................................................................................................................................. 152

Makefi les that Handle Dependency .................................................................................................... 154

Recursive Makefi les ........................................................................................................................... 156

Makefi le with Kernel Confi guration Language.................................................................................... 163

Other Ways to Implement a Build Process .......................................................................................... 174

Page 11: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

■ CONTENTS

xi

Embedded OS ............................................................................................................... 181

Choose an Embedded OS ................................................................................................................... 181

Embedded Linux Development Flow .................................................................................................. 183

Development Machine Setup .............................................................................................................. 183

Summary ...................................................................................................................... 188

References ................................................................................................................... 188

■Chapter 9: Field-Programmable Gate Arrays ..................................................... 191

Hardware Overview ...................................................................................................... 191

FPGA ............................................................................................................................. 192

CPU vs. FPGA ...................................................................................................................................... 192

Verilog vs. VHDL .................................................................................................................................. 194

Bi-Directional Bus ............................................................................................................................... 206

Clock ................................................................................................................................................... 208

Reset................................................................................................................................................... 210

CDC (Clock Domain Crossing) ............................................................................................................. 213

Area-Time Tradeoff and AT2 Boundary ................................................................................................ 219

Inter-FPGA Communication ................................................................................................................. 225

Protect Your IP .............................................................................................................. 226

Encrypt Your Source Code .................................................................................................................. 227

Obfuscate Your Source Code .............................................................................................................. 227

Protect Your Bit Stream ...................................................................................................................... 227

Anti-Tamper ........................................................................................................................................ 228

Summary ...................................................................................................................... 230

References ................................................................................................................... 230

■Chapter 10: System on Programmable Chip (SOPC) .......................................... 233

Economics 101 ............................................................................................................. 233

MCU vs. Soft MCU ............................................................................................................................... 234

MCU Plus External Peripherals ........................................................................................................... 234

Multiple Processors ............................................................................................................................ 235

Page 12: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

■ CONTENTS

xii

Processor Core ............................................................................................................. 236

Interconnection ............................................................................................................ 238

Design and Use Your Own 1T 8051 .............................................................................. 238

History 101 ......................................................................................................................................... 238

Patents and Copyrights....................................................................................................................... 239

Address Spaces .................................................................................................................................. 240

CISC Instruction Set ............................................................................................................................ 242

RISC Implementation .......................................................................................................................... 244

Compilers and Debuggers .................................................................................................................. 246

Summary ...................................................................................................................... 247

References ................................................................................................................... 247

■Chapter 11: De-Bounce, Power Management, and Display ................................ 249

PoR (Power-On Reset) and Switch De-Bounce ............................................................. 249

Power-On Reset .................................................................................................................................. 249

Switch De-Bounce .............................................................................................................................. 251

Power Management ..................................................................................................... 253

LCD Display .................................................................................................................. 253

How LCD Works .................................................................................................................................. 254

LCD Programming Interfaces .............................................................................................................. 258

Using Touchscreens...................................................................................................... 263

Resistive Touchscreens ...................................................................................................................... 263

Capacitive Touchscreen ...................................................................................................................... 265

Summary ...................................................................................................................... 268

References ................................................................................................................... 268

■Chapter 12: Fixed Point Math ............................................................................ 271

Q Format ....................................................................................................................... 271

Two’s Complement and Symmetric Range .................................................................. 272

Basic Operations .......................................................................................................... 272

Rounding and Truncation .................................................................................................................... 272

Saturation ........................................................................................................................................... 273

Page 13: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

■ CONTENTS

xiii

Negation ............................................................................................................................................. 275

Absolute Value .................................................................................................................................... 276

Unsigned Division ............................................................................................................................... 276

Sin/Cos Table ...................................................................................................................................... 284

CORDIC Algorithm ......................................................................................................... 286

Basic Ideas ......................................................................................................................................... 286

Circular Coordinate System ................................................................................................................ 288

Linear Coordinate System .................................................................................................................. 293

Hyperbolic Coordinate System ........................................................................................................... 297

Summary ...................................................................................................................... 302

References ................................................................................................................... 303

■Chapter 13: Popular Ways of Console Communications: Prepare the Tools ...... 305

Software Tools .............................................................................................................. 305

Cygwin ................................................................................................................................................ 305

Text Editor ........................................................................................................................................... 305

File/Folder Comparison....................................................................................................................... 306

Version Control ................................................................................................................................... 306

Issue (Bug) Tracking ........................................................................................................................... 307

Terminal Emulator .............................................................................................................................. 308

Virtual Machine ................................................................................................................................... 309

Cluster Workload Management .......................................................................................................... 309

Hardware Tools ............................................................................................................. 309

Oscilloscope ....................................................................................................................................... 309

Logic Analyzer .................................................................................................................................... 310

Bus/Protocol Analyzer-Exerciser ......................................................................................................... 310

Power Analyzer ................................................................................................................................... 310

Multimeter .......................................................................................................................................... 311

Others ................................................................................................................................................. 311

Summary ...................................................................................................................... 311

References ................................................................................................................... 312

Page 14: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

■ CONTENTS

xiv

■Chapter 14: Work Flow ...................................................................................... 313

Project Inception .......................................................................................................... 314

System Design ............................................................................................................. 314

Software ............................................................................................................................................. 314

Hardware ............................................................................................................................................ 315

Mechanical ......................................................................................................................................... 315

Manufacturing and Quality Control ............................................................................... 315

Summary ...................................................................................................................... 316

References ................................................................................................................... 316

Index ..................................................................................................................... 317

Page 15: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

About the Author

Changyi Gu has worked for multiple high-tech companies across California for over 15 years. He is also the founder of PulseRain Technology, LLC, a company that excels at FPGA-based embedded systems. He started out his career in firmware development. With a deep passion in engineering, he later devoted himself to wireless hardware and programmable logic. Thus, he is one of the lucky few who gets to watch things closely on both the software and hardware sides.

Changyi graduated from the University of Southern California with a master’s degree in VLSI. And he holds another master’s degree in Communications and a bachelor’s degree in CS from Shanghai Jiaotong University, China. In his leisure time, he likes to tinker with things.

xv

Page 16: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051
Page 17: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

About the Technical Reviewer

Brendan Horan is a hardware fanatic, with a full high rack of all types of machine architectures in his home. He has more than 10 years of experience working with large UNIX systems and tuning the underlying hardware for optimal performance and stability. Brendan’s love for all forms of hardware has helped him throughout his IT career, from fixing laptops to tuning servers and their hardware in order to suit the needs of high-availability designs and ultra low-latency applications. Brendan takes pride in the Open Source Movement and is happy to say that every computer in his house is powered by open source technology. He resides in Hong Kong with his wife, Vikki, who continues daily to teach him more Cantonese.

xvii

Page 18: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051
Page 19: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

Acknowledgments

Family:

First of all, I would like to thank my parents for nurturing my interest in engineering since childhood, which has given rise to this book.

I would also like to thank my wife and daughter for bearing with my nocturnal lifestyle for so long. The midnight oil I burned is in your names.

Tech Reviewers:

Special thanks to Brendan Horan for his sound suggestions to my writing. Those feedbacks are highly valued.

And I also owe a big thank you to my ex-coworkers—Cynthia Xu and On Wa Yeung—for offering assistance in reading my book. I really appreciate your kind help.

Editors:

A hug to Jonathan Gennick for giving me the opportunity on this book, and for his passion in embedded systems. I really appreciate your prompt response to my book proposal and your hard work throughout the whole project.

I would also like to thank Jill Balzano for her wonderful job managing the project. This book will not be possible without you.

My development editor, Douglas Pundick, offered valuable suggestions to save me from possible embarrassment. Your thoroughness is appreciated.

And to the rest of Apress team—thanks for your hard work!

Readers:

Thank you for choosing my book!

xix

Page 20: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051
Page 21: Building Embedded Systems - Home - Springer978-1-4842-1919-5/1.pdf · Building Embedded Systems Changyi Gu San Diego ... Using Breakpoints ... Design and Use Your Own 1T 8051

Introduction

“Almost no one comes down here (the engineering level) unless, of course, there is a problem. That’s how it is with people. Nobody cares how it works as long as it works. I like it down here.”

—The Matrix Reloaded (2003)

Embedded systems are conspicuous by their omnipresence. But most people take them for granted despite the fact that behind every system is a design that involves multiple engineering disciplines. Even something as humble as a toaster might be the joint effort of software and hardware engineers. Assuming you still have the unabated curiosity to know how stuff works, this book will try to fill any knowledge gaps you might run into.

Actually, this book is a brain dump of what I have picked up over the past 15+ years. Some of this knowledge has been learned the hard way, which is passively through insomnia and hair loss. And it is my best wishes that you don’t have to go through the same ordeals I endured.

In addition to the traditional design approach, the semiconductor industry’s latest juggernaut has made SOPC (System on Programmable Chips) a viable option both technologically and financially. It is a trail blazed by many newly designed embedded systems, which this book explores in depth.

Lastly, this book will serve you better if you’ve taken some college-level courses in CS or EE, or maybe a little bit of both. Basic knowledge in C/C++ and FPGA can help you sail through this book more smoothly.

Thank you for your faith in my book!

—Changyi Gu 02/14/2016 in San Diego, CA

xxi