ebook img

Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C PDF

741 Pages·2018·135.338 MB·
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C

- Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C Dr. Yifeng Zhu Third edition June 2018 - Copyright© by E-Man Press LLC, 2017. All rights reserved. Printed in the United States of America ARM, Cortex, Cortex-M, Cortex-MO, Cortex-MO+, Cortex-Ml, Cortex-M3, Cortex-M4, Cortex-M7, uVision Keil, and others are registered trademarks of ARM Ltd., 110 Fulbourn Road, Cambridge, GB-CBl 9 J, Great Britain. STM32 discovery kit, STM32, STM32F4, STM32L, STM32L4, and others are trademarks of STMicroelectronics Ltd., 39, Chemin du Champ des Filles, Plan-Les-Ouates, CH1228 Geneve, Switzerland. All external peripheral or product names are trademarks of their owners. All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the publisher. The information contained in this book is believed to be reliable. However, neither the publisher nor the author guarantees the accuracy or completeness of any information contained herein. The publisher and the author shall not be liable for any errors, inaccuracies or omissions, and shall not have any responsibility or liability for any loss, damages, or costs arising out of the use of the information (including all programs) contained herein. This book is published with the understanding that the publisher and author are supplying information but are not attempting to render professional services. The author has tried to seek permissions from the copyright holders. Contact the author if any copyrighted work has not been acknowledged and cited. Future reprints may be modified. Book covered designed by Andrew Hayford Library of Congress Control Number: 2017944799 ISBN 978-0-9826926-6-0 llill Pref ace "I hear and I forget. I see and I remember. I do and I understand." Confucius (Chinese philosopher, 551--479 BC) Modern embedded systems exploit a single, highly integrated chip consisting of one or more general-purpose processor core, memories, advanced peripherals, digital logic, and miniaturized sensors. Due to small power dissipation, low fabrication cost, and small size, System-on-Chips (SoC) become increasingly more prevalent in many embedded applications, including cell phones, MP3 players, GPS, smart watches, medical devices, fitness gadgets, and automobile control. ARM Cortex processors are one of such successful SoC chips in the industry. Significant changes in the third edition include updated serial communication description (UART, SPI, and PC), new serial communication examples, incorporation of GNU gee compiler, low power modes, modification of example programs from STM32Ll (Cortex-M3) to STM32L4 (Cortex-M4). The book introduces basic programming of ARM Cortex-M cores in assembly and Cat the register level, and the fundamentals of embedded system design. It presents basic concepts such as data representations (integer, fixed-point, floating-point), assembly instructions, stack, and implementing basic controls and functions of C language at the assembly level. It covers advanced topics such as interrupts, mixing C and assembly, direct memory access (DMA), system timer (SysTick), multi-tasking, SIMD instructions for digital signal processing (DSP), and instruction encoding/decoding. The book also gives detailed examples of interfacing peripherals, such as general purpose I/0 (GPIO), LCD driver, keypad interaction, stepper motor control, PWM output, timer input capture, DAC, ADC, real-time clock (RTC), and serial communication (USART, PC, SPI, and USB). The book has the following features: • Focusing on register-level programming on bare metal hardware, with no or minimum usage of STM, CMSIS, and ARM APis • Emphasis on structured programming and top-down modular design in both C and assembly language • Line-by-line translation and comparison between C and ARM assembly • Mixture of C and assembly languages, such as a C program calling assembly subroutines, and an assembly program calling C subroutines • Balance between theory and practical examples • Valuable knowledge that prepares students for the courses of computer architecture and operating systems - Although assembly languages are used relatively less in modern embedded systems, learning assembly languages is still crucial. • First, assembly is not another programming language. It is a low-level interface between hardware and software. It provides a better understanding of how a processor executes a program. Assembly programming is the prerequisite knowledge of compilers, operating systems, and computer architecture. • Secondly, assembly programs can potentially run faster than C programs . Compilers sometimes cannot fully utilize hardware features of a particular processor, especially when the processor provides some specific operations of which compilers are not aware. Therefore, we often use assembly languages to develop some speed-sensitive portion of an application. • Thirdly, some operations have to be performed in assembly language because there is no equivalent statement in C. Consequently, assembly programs are often in the kernel codes of operating systems to implement low-level tasks, such as booting and CPU scheduling. • Finally yet importantly, understanding how to translate high-level constructs into low-level assembly instructions can help programmers to write more efficient codes in high-level languages. The audience for this book includes those who want to gain knowledge of the inner working of a System-on-Chip (SoC), and experiences of programming embedded systems at the register level. This book would serve better as text or reference material if readers have learned some basic C programming. The book covers both fundamental concepts and advanced topics, suitable for a broad range of audience. I would like to thank all the people who have helped me greatly. My colleague Prof. Duane Hanselman has offered excellent guidance and advice on book publishing. I would like to thank Kaishuang Li for encouragement and help. I would like to thank Elyse Kahl and Nolan Gagnon for proofreading and Andrew Hayford for the cover design. I appreciate all the students of ECE 271 Microcomputer Architecture and Applications class who have helped me correct many errors and gave me significant improvement suggestions in the first and second edition. Last but most importantly, I acknowledge the profound debt of gratitude I owe to my mother and father. I cannot thank them enough for all the support and love they have given to me. Yifeng Zhu June 1, 2017 Orono, Maine, USA - Table of Contents Chapter 1. See a Program Running .................................................•..................... 1 1.1 Translate a C Program into a Machine Program ....................................................... 1 1.2 Load a Machine Program into Memory .................................................................... 4 1.2.1 Harvard Architecture and Von Neumann Architecture ......................................... 4 1.2.2 Creating Runtime Memory Image .......................................................................... 7 1.3 Registers ................................................................................................................ 11 1.3.1 Reusing Registers to Improve Performance ........................................................ 12 1.3.2 Processor Registers .............................................................................................. 13 1.4 Executing a Machine Program ................................................................................ 17 1.4.1 Loading a Program ............................................................................................... 18 1.4.2 Starting the Execution .......................................................................................... 19 1.4.3 Program Completion ............................................................................................ 23 1.S Exercises ................................................................................................................ 24 Chapter 2. Data Representation .......................................................................... 27 2.1 Bit, Byte, Halfword, Word, and Double-word ......................................................... 27 2.2 Binary, Octal, Decimal, and Hexadecimal Numbers ................................................ 29 2.3 Unsigned lntegers .................................................................................................. 30 2.4 Signed lntegers ...................................................................................................... 31 2.4.1 Sign-and-Magnitude ............................................................................................. 33 2.4.2 One's Complement .............................................................................................. 34 2.4.3 Two's Complement .............................................................................................. 35 2.4.4 Carry Flag for Unsigned Addition or Subtraction ................................................. 36 2.4.5 Overflow Flag for Signed Addition or Subtraction ............................................... 39 2.4.5.l Interpreting the Carry and Overflow Flags ................................................................... 42 2.4.5.2 Two's Complement Simplifies Hardware Implementation ........................................... 45 2.S Character String ..................................................................................................... 49 2.6 Exercises ................................................................................................................ S3 Chapter 3. ARM Instruction Set Architecture ...................................................... 55 3.1 ARM Assembly Instruction Sets ............................................................................. SS 3.2 ARM Cortex-M Organization .................................................................................. 58 3.3 Going from C to Assembly ...................................................................................... 60 3.4 Assembly Instruction Format ................................................................................. 63 3.S Anatomy of an Assembly Program ......................................................................... 6S 3.6 Assembly Directives ............................................................................................... 69 3.7 Exercises ................................................................................................................ 73 Chapter 4. Arithmetic and Logic .......................................................................... 75 4.1 Program Status Register ........................................................................................ 7S 4.2 Updating Program Status Flags .............................................................................. 77 4.3 Shift and Rotate ..................................................................................................... 78 - 4.4 Arithmetic Instructions ......................................................................................... 80 4.4.l Addition and Subtraction Instructions ................................................................. 81 4.4.2 Short Multiplication and Division Instructions ..................................................... 82 4.4.3 Long Multiplication lnstructions ........................................................................... 82 4.4.4 Saturation Instructions ......................................................................................... 83 4.5 Barrel Shifter ......................................................................................................... 83 4.6 Bitwise Logic Operations ....................................................................................... 84 4.7 Reversing the Order of Bits and Bytes ................................................................... 89 4.8 Sign and Zero Extension ........................................................................................ 90 4.9 Data Comparison .................................................................................................. 91 4.10 Data Movement between Registers ...................................................................... 92 4.11 Bit Field Extract ..................................................................................................... 93 4.12 Exercises ............................................................................................................... 94 Chapter 5. Load and Store .................................................................................. 97 5.1 Load Constant into Registers ................................................................................. 97 5.1.1 Data Movement Instruction MOV and MVN ........................................................... 97 5.1.2 Pseudo Instruction LDR and ADR .......................................................................... 98 5.1.3 Comparison of LDR, ADR, and MOV ....................................................................... 99 5.2 Big and Little Endian ............................................................................................ 100 5.3 Accessing Data in Memory .................................................................................. 101 5.4 Memory Addressing ............................................................................................ 101 5.4.1 Pre-index, Post-index, and Pre-index with Update ............................................ 101 5.4.2 Load and Store lnstructions ................................................................................ 103 5.4.3 PC-relative Addressing ........................................................................................ 104 5.4.4 Example of Accessing an Array ........................................................................... 105 5.5 Loading and Storing Multiple Registers ............................................................... 106 5.6 Exercises ............................................................................................................. 108 Chapter 6. Branch and Conditional Execution ................................................... 111 6.1 Condition Testing ................................................................................................ 111 6.2 Branch Instructions ............................................................................................. 114 6.3 Conditional Execution ......................................................................................... 117 6.4 If-then Statement ................................................................................................ 118 6.5 If-then-else Statement ........................................................................................ 121 6.6 For Loop .............................................................................................................. 122 6.7 While Loop .......................................................................................................... 123 6.8 Do While Loop .................................................................................................... 124 6.9 Continue Statement ............................................................................................ 125 6.10 Break Statement ................................................................................................. 126 6.11 Switch Statement ................................................................................................ 126 6.12 Exercises ............................................................................................................. 129 Chapter 7. Structured Programming ................................................................. 133 7.1 Basic Control Structures ...................................................................................... 133 1'41• 7 .2 Register Reuse ..................................................................................................... 138 7.3 Example of Factorial Numbers ............................................................................. 141 7.4 Example of Counting Ones in a Word ................................................................... 142 7.5 Example of Finding the Maximum of an Array ..................................................... 144 7.6 Example of Counting Digits .................................................................................. 146 7.7 Example of Parity Bit ........................................................................................... 147 7.8 Example of Perfect Numbers ............................................................................... 149 7 .9 Example of Armstrong Numbers .......................................................................... 151 7.10 Example of Palindrome String .............................................................................. 152 7.11 Example of Converting String to Integer (atoi) ................................................... 154 7 .12 Example of Binary Search .................................................................................... 155 7.13 Example of Bubble Sort ....................................................................................... 157 7.14 Exercises .............................................................................................................. 159 Chapter 8. Subroutines .....................•..........•....•.....•..••.....•....•.......................... 161 8.1 Calling a Subroutine ............................................................................................. 162 8.2 Stack ................................................................................................................... 164 8.3 Implementation of Stack via STM and LDM .......................................................... 165 8.4 Preserving Runtime Environment via Stack .......................................................... 166 8.5 Passing Arguments to Subroutine via Registers .................................................... 169 8.5.1 Pass a Variable by Value and by Reference ....................................................... 170 8.5.2 Example of Passing by Value .............................................................................. 173 8.5.3 Write a Subroutine in Different Files ................................................................. 175 8.5.4 Example of Passing by Reference ...................................................................... 176 8.5.5 Example of Greatest Common Divisor ............................................................... 177 8.5.6 Example of Concatenating Two Strings ............................................................. 179 8.5.7 Example of Comparing Two Strings ................................................................... 180 8.5.8 Example of Inserting an Integer into a Sorted Array ......................................... 181 8.5.9 Example of Converting Integer to String (itoa) .................................................. 182 8.5.10 Example of Matrix Transpose ............................................................................ 184 8.5.11 Example of Removing a Character from a String ............................................... 186 8.5.12 Example of Finding Unique Numbers in an Array .............................................. 187 8.6 Passing Arguments through Stack ........................................................................ 190 8. 7 Recursive Functions ............................................................................................. 192 8.7.1 Example of Factorial Numbers ........................................................................... 194 8.7.2 Example of Reversing a String ............................................................................ 196 8.7.3 Example of String Permutation .......................................................................... 197 8.8 Exercises .............................................................................................................. 199 Chapter 9. 64-bit Data Processing ..................................................................... 203 9.1 64-bit Addition .................................................................................................... 203 9.2 64-bit Subtraction ................................................................................................ 204 9.3 64-bit Counting Leading Zeros ............................................................................. 205 9.4 64-bit Sign Extension ........................................................................................... 205 9.5 64-bit Logic Shift Left ........................................................................................... 206 9.6 64-bit Logic Shift Right ........................................................................................ 207 9.7 64-bit Multiplication ........................................................................................... 208 9.8 64-bit Unsigned Division ..................................................................................... 209 9.9 64-bit Signed Division .......................................................................................... 211 9.10 Exercises ............................................................................................................. 213 Chapter 10. Mixing C and Assembly ................................................................... 215 10.1 Data Types and Access ........................................................................................ 216 10.1.1 Signed or Unsigned Integers ............................................................................... 216 10.1.2 Data Alignment ................................................................................................... 217 10.1.3 Data Structure Padding ...................................................................................... 219 10.2 Special Variables ................................................................................................. 222 10.2.1 Static Variables ................................................................................................... 222 10.2.2 Volatile Variables ................................................................................................ 226 10.3 lnline Assembly ................................................................................................... 228 10.3.1 Assembly Functions in a C Program ................................................................... 228 10.3.2 In line Assembly Instructions in a C Program ...................................................... 229 10.4 Calling Assembly Subroutines from a C Program ................................................. 230 10.4.1 Example of Calling an Assembly Subroutine ...................................................... 230 10.4.2 Example of Accessing C Variables in Assembly .................................................. 231 10.5 Calling C Functions from Assembly Programs ...................................................... 232 10.5.1 Example of Calling a C Function ......................................................................... 232 10.5.2 Example of Accessing Assembly Data in a C Program ........................................ 233 10.6 Exercises ............................................................................................................. 234 Chapter 11. Interrupts ........................................................................................ 237 11.1 Introduction to Interrupts ................................................................................... 237 11.2 Interrupt Numbers .............................................................................................. 238 11.3 Interrupt Service Routines ................................................................................... 240 11.4 Interrupt Vector Table ......................................................................................... 241 11.5 Interrupt Stacking and Unstacking ....................................................................... 243 11.6 Nested Vectored Interrupt Controller (NVIC) ....................................................... 245 11.6.1 Enable and Disable Peripheral Interrupts .......................................................... 247 11.6.2 Interrupt Priority ................................................................................................ 249 11.6.3 Global Interrupt Enable and Disable .................................................................. 253 11.7 System Timer ...................................................................................................... 254 11.8 External Interrupt ............................................................................................... 262 11.9 Software Interrupt .............................................................................................. 266 11.10 Exercises ......................................................................................................... 267 Chapter 12. Fixed-point and Floating-point Arithmetic ...................................... 269 12.1 Fixed-point Arithmetic ........................................................................................ 270 12.1.1 Unsigned Fixed-point Representation ................................................................ 271 12.1.2 Signed Fixed-point Representation .................................................................... 272

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.