THE EXPERT’S VOICE® IN EMBEDDED SYSTEMS Building Embedded Systems Programmable Hardware — Changyi Gu Building Embedded Systems Programmable Hardware Changyi Gu Building Embedded Systems Changyi Gu San Diego California, USA ISBN-13 (pbk): 978-1-4842-1918-8 ISBN-13 (electronic): 978-1-4842-1919-5 DOI 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 Spahr Lead Editor: Jonathan Gennick Development Editor: Douglas Pundick Technical Reviewer: Brendan Horan Editorial 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 Balzano Copy Editor: Kezia Endsley Compositor: SPi Global Indexer: SPi Global Artist: 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 w ww.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 w ww.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 w ww.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 To those who enjoy doing things that are otherwise meaningless in the eyes of many. 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 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 ■ CONTENTS ■ 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 viii ■ CONTENTS 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 Implementation of Differential Serial Link ......................................................................77 Out-of-band Clocking ...........................................................................................................................77 Embedded Clocking ..............................................................................................................................79 8b/10b Encoding and Signal Conditioning ............................................................................................80 Bus Standards ......................................................................................................................................80 Driver for PCI Device .............................................................................................................................84 Selecting the Hardware Component .....................................................................................................88 Implementing the USB fi rmware ..........................................................................................................90 Implementing the USB Device Driver (for the Host OS) ........................................................................91 Overview ...............................................................................................................................................93 Hardware Implementation ....................................................................................................................95 Summary ......................................................................................................................101 References ...................................................................................................................101 ■ Chapter 6: Firmware Coding in C .......................................................................103 Overview ......................................................................................................................103 The Confusion of Tongues ............................................................................................105 Firmware Coding in the C Language ............................................................................106 Explicit Type of Bit-Width ....................................................................................................................106 Align the Data Structure .....................................................................................................................107 ix
Description: