1. Trang chủ
  2. » Kinh Doanh - Tiếp Thị

Propeller programming using assembler, spin, and c

317 173 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Propeller Programming Using Assembler, Spin, and C — Sridhar Anandakrishnan Propeller Programming Using Assembler, Spin, and C Sridhar Anandakrishnan Propeller Programming: Using Assembler, Spin, and C Sridhar Anandakrishnan Department of Geosciences, University Park, Pennsylvania, USA ISBN-13 (pbk): 978-1-4842-3353-5 https://doi.org/10.1007/978-1-4842-3354-2 ISBN-13 (electronic): 978-1-4842-3354-2 Library of Congress Control Number: 2018935236 Copyright © 2018 by Sridhar Anandakrishnan 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 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, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Natalie Pao Development Editor: James Markham Coordinating Editor: Jessica Vakili Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Cover photo: Double slip at Munich Central, 2005 Photo by Bjorn Laczay, license CC-BY-SA 2.0 https://goo.gl/q7EHSv 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 orders-ny@springer-sbm.com, or visit www.springeronline.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 rights@apress.com, or visit www.apress.com/ rights-permissions Apress titles 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 Print and eBook Bulk Sales web page at www.apress.com/bulk-sales Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484233535 For more detailed information, please visit www.apress.com/source-code Printed on acid-free paper Table of Contents About the Author���������������������������������������������������������������������������������xi Acknowledgments�����������������������������������������������������������������������������xiii Preface�����������������������������������������������������������������������������������������������xv Part I: Introduction����������������������������������������������������������������������1 Chapter 1: Introduction������������������������������������������������������������������������3 1.1 The Propeller Eight-­Cog Processor�����������������������������������������������������������������5 1.1.1 Cogs��������������������������������������������������������������������������������������������������������6 1.1.2 Hubs and Cogs���������������������������������������������������������������������������������������9 1.2 Memory Layout���������������������������������������������������������������������������������������������11 1.2.1 Hub Memory�����������������������������������������������������������������������������������������13 1.2.2 Cog Memory�����������������������������������������������������������������������������������������14 1.3 Layout of This Book���������������������������������������������������������������������������������������16 Chapter 2: Steim Compression�����������������������������������������������������������17 2.1 Packing and Compressing Data��������������������������������������������������������������������18 2.2 Specification�������������������������������������������������������������������������������������������������21 2.3 Implementation���������������������������������������������������������������������������������������������23 Chapter 3: Introduction to Spin����������������������������������������������������������25 3.1 Negative Numbers����������������������������������������������������������������������������������������28 3.2 Memory Layout���������������������������������������������������������������������������������������������29 iii Table of Contents 3.3 Spin Template�����������������������������������������������������������������������������������������������30 3.3.1 Hello, World�������������������������������������������������������������������������������������������30 3.3.2 Running the Program���������������������������������������������������������������������������35 3.4 PASM Template���������������������������������������������������������������������������������������������37 3.5 Template for PASM Code in a Separate File��������������������������������������������������40 3.6 Summary������������������������������������������������������������������������������������������������������43 Chapter 4: Test-Driven Development��������������������������������������������������45 4.1 TDD Spin Code����������������������������������������������������������������������������������������������49 4.2 Summary������������������������������������������������������������������������������������������������������51 Chapter 5: Compression in Spin���������������������������������������������������������53 5.1 Structure of the Project���������������������������������������������������������������������������������53 5.2 Goals of This Chapter������������������������������������������������������������������������������������54 5.3 First Iteration�������������������������������������������������������������������������������������������������55 5.4 Passing Arrays to Methods���������������������������������������������������������������������������61 5.5 Testing�����������������������������������������������������������������������������������������������������������62 5.6 Final Code�����������������������������������������������������������������������������������������������������64 5.6.1 Compression in Spin�����������������������������������������������������������������������������64 5.6.2 Decompression in Spin�������������������������������������������������������������������������66 5.7 The Need for Speed���������������������������������������������������������������������������������������70 5.7.1 Timing in PASM�������������������������������������������������������������������������������������71 5.7.2 PASM Timing Estimate��������������������������������������������������������������������������72 5.8 Summary������������������������������������������������������������������������������������������������������73 Part II: Spin and PASM���������������������������������������������������������������������75 Chapter 6: Propeller Assembler: PASM����������������������������������������������77 6.1 Instructions in PASM�������������������������������������������������������������������������������������78 6.1.1 The Add Instruction�������������������������������������������������������������������������������81 6.1.2 The mov Instruction������������������������������������������������������������������������������82 iv Table of Contents 6.1.3 Variables�����������������������������������������������������������������������������������������������83 6.1.4 Effects��������������������������������������������������������������������������������������������������84 6.1.5 Literals��������������������������������������������������������������������������������������������������85 6.1.6 Labels���������������������������������������������������������������������������������������������������85 6.1.7 Conditional Evaluation��������������������������������������������������������������������������86 6.1.8 Branching���������������������������������������������������������������������������������������������88 6.2 Reading the PASM Manual����������������������������������������������������������������������������89 6.3 Categories of PASM Instruction and Registers���������������������������������������������90 6.3.1 Copying�������������������������������������������������������������������������������������������������90 6.3.2 Arithmetic���������������������������������������������������������������������������������������������91 6.3.3 Boolean, Comparison, and Bit-Shift Operators�������������������������������������92 6.3.4 Process Control������������������������������������������������������������������������������������94 6.3.5 Hub Reads/Writes���������������������������������������������������������������������������������95 6.3.6 Locks����������������������������������������������������������������������������������������������������95 6.3.7 Variables�����������������������������������������������������������������������������������������������96 6.3.8 Special Registers����������������������������������������������������������������������������������96 6.3.9 Counters�����������������������������������������������������������������������������������������������97 6.4 The Structure of PASM Programs������������������������������������������������������������������97 6.5 Passing Parameters to PASM����������������������������������������������������������������������102 6.6 Summary����������������������������������������������������������������������������������������������������102 Chapter 7: Interacting with the World����������������������������������������������105 7.1 Outline���������������������������������������������������������������������������������������������������������108 7.2 Timing in Spin and PASM����������������������������������������������������������������������������108 7.3 Spin�������������������������������������������������������������������������������������������������������������109 7.4 PASM�����������������������������������������������������������������������������������������������������������111 7.4.1 Toggle a Pin in PASM��������������������������������������������������������������������������111 7.4.2 Monitor a Switch��������������������������������������������������������������������������������114 7.5 Communication Protocols���������������������������������������������������������������������������115 v Table of Contents 7.6 SPI Logging�������������������������������������������������������������������������������������������������119 7.6.1 PASM SPI Write�����������������������������������������������������������������������������������123 7.6.2 Logging Deadlock�������������������������������������������������������������������������������126 7.7 Locks�����������������������������������������������������������������������������������������������������������127 7.7.1 Introduction to Locks��������������������������������������������������������������������������127 7.7.2 Using Locks for Logging���������������������������������������������������������������������130 7.8 Some Common Tasks����������������������������������������������������������������������������������132 7.8.1 Assignment�����������������������������������������������������������������������������������������132 7.8.2 Multiplication��������������������������������������������������������������������������������������133 7.8.3 Division�����������������������������������������������������������������������������������������������135 7.8.4 Loops��������������������������������������������������������������������������������������������������136 7.8.5 Conditionals����������������������������������������������������������������������������������������136 7.9 Summary����������������������������������������������������������������������������������������������������137 Chapter 8: Implementing the Compression Code in PASM���������������139 8.1 Passing Parameters to PASM����������������������������������������������������������������������139 8.2 Setting Up steim_pasm0�����������������������������������������������������������������������������140 8.3 Passing Parameters in the cognew Command�������������������������������������������149 8.3.1 Using PAR�������������������������������������������������������������������������������������������151 8.3.2 Using PAR Some More������������������������������������������������������������������������151 8.3.3 Using the Addresses���������������������������������������������������������������������������153 8.3.4 Starting the Compression�������������������������������������������������������������������154 8.4 Passing Parameters: Method 2�������������������������������������������������������������������155 8.5 Summary����������������������������������������������������������������������������������������������������159 Chapter 9: Compression in PASM with TDD��������������������������������������163 9.1 Overall Flowchart����������������������������������������������������������������������������������������165 9.2 Test 1: Passing nsamps and ncompr�����������������������������������������������������������167 9.2.1 Spin Code�������������������������������������������������������������������������������������������167 9.2.2 PASM Code�����������������������������������������������������������������������������������������168 vi Table of Contents 9.3 Test 2: Packing Sample 0����������������������������������������������������������������������������171 9.3.1 Spin Code�������������������������������������������������������������������������������������������171 9.3.2 Memory Layout of Arrays and Parameters�����������������������������������������172 9.3.3 PASM Code�����������������������������������������������������������������������������������������174 9.3.4 Subroutines in PASM��������������������������������������������������������������������������176 9.3.5 Testing the Compression of Sample 0������������������������������������������������178 9.4 Packing Differences for Latter Samples�����������������������������������������������������179 9.4.1 Testing Compressing Two Samples!���������������������������������������������������184 9.4.2 Test Compressing an Arbitrary Number of Samples���������������������������187 9.5 Success?�����������������������������������������������������������������������������������������������������190 9.6 Summary����������������������������������������������������������������������������������������������������191 Chapter 10: Decompression in PASM�����������������������������������������������193 10.1 Getting the Sign Right�������������������������������������������������������������������������������196 10.2 Overall Flowchart��������������������������������������������������������������������������������������196 10.3 Spin Code��������������������������������������������������������������������������������������������������197 10.4 PASM: Main Decompression Loop������������������������������������������������������������198 10.5 Subroutines for Unpacking�����������������������������������������������������������������������201 10.6 Testing Decompression of Two Samples���������������������������������������������������206 10.7 Testing Decompression of 128 Samples���������������������������������������������������208 Chapter 11: Debugging PASM Code��������������������������������������������������213 11.1 Logging to a Hub Array������������������������������������������������������������������������������215 11.2 Spin Code��������������������������������������������������������������������������������������������������217 11.3 PASM Code������������������������������������������������������������������������������������������������218 11.4 Bug Fix������������������������������������������������������������������������������������������������������221 vii Table of Contents Part III: C Language�����������������������������������������������������������������225 Chapter 12: C Programming for the Propeller����������������������������������227 12.1 The C Language����������������������������������������������������������������������������������������229 12.2 Programming the Propeller in C����������������������������������������������������������������235 12.2.1 SimpleIDE�����������������������������������������������������������������������������������������236 12.2.2 Hello World���������������������������������������������������������������������������������������238 12.2.3 Launching a New Cog�����������������������������������������������������������������������239 12.2.4 Compression Code in C��������������������������������������������������������������������245 12.3 Summary��������������������������������������������������������������������������������������������������250 Chapter 13: Programming in Cog-C Mode����������������������������������������253 13.1 Cog-C Mixed Mode Programming�������������������������������������������������������������254 13.1.1 Main Cog Code���������������������������������������������������������������������������������255 13.1.2 Compression Cog-C Code�����������������������������������������������������������������258 13.1.3 Header File compr_cogc.h����������������������������������������������������������������261 13.1.4 Running the Cog-C Code������������������������������������������������������������������262 13.2 Summary��������������������������������������������������������������������������������������������������263 Chapter 14: Programming with C and PASM������������������������������������267 14.1 Compression with C and PASM�����������������������������������������������������������������269 14.1.1 C Code for Main Cog�������������������������������������������������������������������������269 14.1.2 PASM Code���������������������������������������������������������������������������������������273 14.2 Summary��������������������������������������������������������������������������������������������������274 Chapter 15: Hardware I/O with C������������������������������������������������������279 15.1 Referencing Hardware in C�����������������������������������������������������������������������280 15.2 simpletools Library�����������������������������������������������������������������������������������282 viii Table of Contents 15.3 Using the Registers Directly����������������������������������������������������������������������283 15.3.1 Set a Pin�������������������������������������������������������������������������������������������283 15.3.2 Read a Pin����������������������������������������������������������������������������������������284 15.4 Implementing SPI in Cog-C�����������������������������������������������������������������������286 15.5 Goals of This Chapter��������������������������������������������������������������������������������286 15.5.1 Main Cog (Controller)������������������������������������������������������������������������287 15.5.2 SPI Master����������������������������������������������������������������������������������������291 15.5.3 SPI Slave (Simulated Data Producing Device)����������������������������������294 15.5.4 Running the SPI Code�����������������������������������������������������������������������297 15.6 Summary��������������������������������������������������������������������������������������������������297 Chapter 16: Using Inline Assembly Instructions in C Code���������������301 16.1 Inline Assembler���������������������������������������������������������������������������������������303 16.2 spiSlave.cogc Inline Assembly������������������������������������������������������������������306 16.3 Timing�������������������������������������������������������������������������������������������������������307 16.4 Summary��������������������������������������������������������������������������������������������������307 Chapter 17: Concluding Thoughts����������������������������������������������������309 Index�������������������������������������������������������������������������������������������������311 ix ... the Propeller is something closer to 160MIPS • Each cog has access to all 32 input/output pins of the Propeller Each cog has access to an internal counter that increments once per clock cycle and. . .Propeller Programming Using Assembler, Spin, and C Sridhar Anandakrishnan Propeller Programming: Using Assembler, Spin, and C Sridhar Anandakrishnan Department of Geosciences, University... window for PropellerIDE Congratulations! You have successfully seen that the Propeller is attached, that you can communicate with it, that the clock is set correctly, and that you can compile and download

Ngày đăng: 15/09/2020, 11:41

Xem thêm:

TỪ KHÓA LIÊN QUAN

Mục lục

    1.1 The Propeller Eight-Cog Processor

    1.3 Layout of This Book

    2.1 Packing and Compressing Data

    Chapter 3: Introduction to Spin

    3.5 Template for PASM Code in a Separate File

    Chapter 5: Compression in Spin

    5.1 Structure of the Project

    5.2 Goals of This Chapter

    5.4 Passing Arrays to Methods

    5.7 The Need for Speed

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN