keil c for lpc-arm

September 15, 2017 | Author: hypernuclide | Category: Arm Architecture, Instruction Set, Central Processing Unit, Computer Architecture, Computer Engineering
Share Embed Donate


Short Description

keil c for lpc-arm...

Description

Introduction to the LPC2000

© Hitex (UK) Ltd.

Introduction

Page 1

Introduction to the LPC2000

Introduction

Published by Hitex (UK) Ltd. ISBN: 0-9549988 1 First Published February 2005 First Reprint April 2005 First Revision February 2006 Hitex (UK) Ltd. Sir William Lyons Road University Of Warwick Science Park Coventry, CV4 7EZ

Credits Author: Illustrator: Editors: Cover:

Trevor Martin Sarah Latchford Michael Beach Wolfgang Fuller

Acknowledgements The author would like to thank Kees van Seventer and Chris Davies of Philips Semiconductors for their assistance in compiling this book

© Hitex (UK) Ltd., 13/02/2006 All rights reserved. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical or photocopying, recording or otherwise without the prior written permission of the Publisher.

© Hitex (UK) Ltd.

Page 2

Introduction to the LPC2000

Introduction

1 1.1 1.2 1.3 1.4 1.5 1.6 1.6.1 1.6.2 1.6.2.1 1.6.2.2 1.7 1.8 1.9 1.10 1.11 1.12

Chapter 1: The ARM7 CPU Core 9 Outline ........................................................................................................9 The Pipeline................................................................................................9 Registers ..................................................................................................10 Current Program Status Register .............................................................11 Exception Modes ......................................................................................12 The ARM 7 Instruction Set........................................................................15 Branching .................................................................................................17 Data Processing Instructions ....................................................................18 Copying Registers ....................................................................................19 Copying Multiple Registers .......................................................................19 Swap Instruction .......................................................................................20 Modifying The Status Registers................................................................20 Software Interrupt .....................................................................................20 MAC Unit ..................................................................................................22 THUMB Instruction Set .............................................................................23 Summary ..................................................................................................25

2 2.1 2.2 2.2.1 2.2.2 2.2.3 2.3 2.4 2.5 2.6 2.7 2.7.1 2.8 2.9 2.10 2.11 2.12 2.13 2.13.1.1 2.13.1.2 2.14

Chapter 2: Software Development 26 Outline ......................................................................................................26 Which Compiler? ......................................................................................26 uVision IDE...............................................................................................27 HiTOP IDE................................................................................................27 Tutorial......................................................................................................27 Startup Code ............................................................................................28 Interworking ARM/THUMB Code ..............................................................30 STDIO Libraries ........................................................................................32 Accessing Peripherals ..............................................................................32 Interrupt Service Routines ........................................................................33 Software Interrupt .....................................................................................34 Locating Code In RAM..............................................................................34 Inline Functions ........................................................................................35 Operating System Support .......................................................................36 Fixing Objects At Absolute Locations .......................................................36 Inline Assembler .......................................................................................36 Hardware Debugging Tools ......................................................................37 Important! .................................................................................................38 Even More Important ................................................................................38 Summary ..................................................................................................38

3 3.1 3.2 3.3 3.4 3.5 3.5.1

Chapter 3: System Peripherals 39 Outline ......................................................................................................39 Bus Structure............................................................................................39 Memory Map.............................................................................................40 Register Programming..............................................................................41 Memory Accelerator Module.....................................................................41 Example MAM Configuration ....................................................................44

© Hitex (UK) Ltd.

Page 3

Introduction to the LPC2000

Introduction

3.6 3.6.1 3.6.2 3.6.3 3.6.4 3.7 3.7.1 3.7.2 3.8 3.9 3.10 3.10.1.1 3.11 3.12 3.12.1 3.12.2 3.12.3 3.12.4 3.12.5 3.12.5.1 3.12.6 3.12.7 3.12.7.1 3.12.8 3.12.9 3.12.9.1 3.12.10 3.13

FLASH Memory Programming..................................................................45 Memory Map Control ................................................................................45 Bootloader ................................................................................................46 Philips ISP Utility ......................................................................................46 In-Application Programming .....................................................................48 External Bus Interface ..............................................................................49 External Memory Interface........................................................................49 Using The External Bus Interface .............................................................52 Booting From ROM...................................................................................54 Phase Locked Loop ..................................................................................56 VLSI Peripheral Bus Divider .....................................................................58 Example Code: PLL And VPB Configuration ............................................58 Power Control ...........................................................................................59 LPC2000 Interrupt System .......................................................................61 Pin Connect Block ....................................................................................61 External Interrupt Pins ..............................................................................61 Interrupt Structure.....................................................................................62 FIQ interrupt .............................................................................................63 Leaving An FIQ Interrupt ..........................................................................63 Example Program: FIQ Interrupt..............................................................64 Vectored IRQ ............................................................................................65 Leaving An IRQ Interrupt ..........................................................................66 Example Program: IRQ interrupt ..............................................................67 Non-Vectored Interrupts ...........................................................................67 Leaving A Non-Vectored IRQ Interrupt .....................................................68 Example Program: Non-Vectored Interrupt...............................................68 Nested Interrupts ......................................................................................69 Summary ..................................................................................................70

4 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 4.11 4.12 4.12.1.1 4.12.2 4.12.3 4.12.4 4.12.5 4.12.6 4.12.7

Chapter 4: User Peripherals 72 Outline ......................................................................................................72 General Purpose I/O.................................................................................72 General Purpose Timers...........................................................................74 PWM Modulator ........................................................................................78 Real Time Clock .......................................................................................81 Watchdog .................................................................................................84 UART........................................................................................................86 I2C Interface .............................................................................................90 SPI Interface.............................................................................................95 Analog To Digital Converter......................................................................97 Digital To Analog Converter....................................................................100 CAN Controller .......................................................................................101 ISO 7 Layer Model..................................................................................101 CAN Node Design ..................................................................................101 CAN Message Objects ...........................................................................103 CAN Bus Arbitration................................................................................105 Bit Timing................................................................................................106 CAN Message Transmission ..................................................................108 CAN Error Containment..........................................................................110

© Hitex (UK) Ltd.

Page 4

Introduction to the LPC2000

Introduction

4.12.8 4.12.9 4.12.9.1 4.13

CAN Message Reception .......................................................................113 Acceptance Filtering ...............................................................................114 Configuring The Acceptance Filter .........................................................115 Summary ................................................................................................116

5 5.1 5.1.1 5.2 5.3 5.4 5.4.1 5.5 5.6 5.7 5.8 5.9 5.10 5.11 5.12 5.13 5.14 5.15 5.16 5.17 5.18 5.19 5.20 5.21 5.22 5.23 5.24 5.25 5.26 5.27 5.28 5.29

Chapter 5: Keil Tutorial 118 Installation ..............................................................................................118 Using the Keil UVISION IDE...................................................................119 Exercise 1: Using the Keil Toolset ..........................................................120 Using The Debugger...............................................................................126 Using The ULINK Hardware Debugger...................................................130 Setting up the ULINK JTAG hardware debugger: ...................................130 Exercise 2: Startup Code........................................................................133 Exercise 3: Using THUMB Code ............................................................134 Exercise 4: Using STDIO Libraries ........................................................136 Exercise 5: Simple Interrupt....................................................................138 Exercise 6: Software Interrupt ................................................................140 Exercise 7: Memory Accelerator Module ................................................141 Exercise 8: In-Application Programming.................................................143 Exercise 9: External Bus Interface..........................................................144 Exercise 10: Phase Locked Loop ...........................................................148 Exercise 11: Fast Interrupt......................................................................150 Exercise 12: Vectored Interrupt ..............................................................151 Exercise 13 : Non Vectored Interrupt......................................................152 Exercise 14: Nested Interrupts ..............................................................153 Exercise 15: General Purpose IO Pins ...................................................154 Exercise 16: Timer Capture ....................................................................155 Exercise 17: Timer Match .......................................................................157 Exercise 18: Dual-Edge (Symmetrical) PWM Generation.......................160 Exercise 19: Real Time Clock.................................................................162 Exercise 20: UART .................................................................................163 Exercise 21: I2C interface.......................................................................164 Exercise 22: SPI .....................................................................................166 Exercise 23: Analog To Digital Converter ...............................................167 Exercise 24: Digital to Analogue Converter ...........................................168 Exercise 25: Transmitting CAN Data ......................................................169 Exercise 26: Receiving CAN Data ..........................................................170

6 6.1 6.2 6.3 6.4 6.5 6.5.1 6.6 6.7 6.8 6.9

Chapter 6: Keil Tutorial With GNU Tools 172 Intoduction ..............................................................................................172 GCC Startup Code..................................................................................172 Interworking ARM/THUMB Code ............................................................172 Accessing Peripherals ............................................................................172 Interrupt Service Routines ......................................................................172 Software Interrupt ...................................................................................173 Inline Functions ......................................................................................173 Exercise 1: Using The Keil Toolset With The GNU Compiler .................174 Exercise 2: Startup Code........................................................................179 Exercise 3: Using THUMB Code ............................................................179

© Hitex (UK) Ltd.

Page 5

Introduction to the LPC2000

Introduction

6.10 6.11 6.12

Exercise 4: Using The GNU Libraries .....................................................182 Exercise 5: Simple Interrupt....................................................................183 Exercise 6: Software Interrupt ................................................................185

7 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8

Chapter 7: Hitex Tutorial (With Keil Or GNU Compiler) 187 Installation ..............................................................................................187 Creating The First Project.......................................................................188 Exercise 1: Creating The First Project ....................................................189 Using HiTOP...........................................................................................191 Exercise 2: Startup Code........................................................................194 Exercise 3: Using THUMB code .............................................................195 Using The Tantino Hardware Debugger .................................................197 Setting Up The Tantino JTAG hardware Debugger ................................197

8 8.1 8.2 8.3 8.4 8.5

Chapter 8: Extended Debugging With ETM Trace 199 Outline ....................................................................................................199 Using The Tanto With Trace...................................................................199 Recording Execution Trace ....................................................................200 A Data Trace ..........................................................................................206 Trace Examples......................................................................................208

9 9.1 9.1.1 9.1.2 9.1.2.1 9.1.3 9.2

Appendices 212 Appendix A .............................................................................................212 Bibliography............................................................................................212 Webliography..........................................................................................212 Reference Sites ......................................................................................212 Tools and Software Development...........................................................212 Evaluation Boards And Modules.............................................................212

© Hitex (UK) Ltd.

Page 6

Introduction to the LPC2000

Introduction

Introduction This book is intended as a hands-on guide for anyone planning to use the Philips LPC2000 family of microcontrollers in a new design. It is laid out both as a reference book and as a tutorial. It is assumed that you have some experience in programming microcontrollers for embedded systems and are familiar with the C language. The bulk of technical information is spread over the first four chapters, which should be read in order if you are completely new to the LPC2000 and the ARM7 CPU. The first chapter gives an introduction to the major features of the ARM7 CPU. Reading this chapter will give you enough understanding to be able to program any ARM7 device. If you want to develop your knowledge further, there are a number of excellent books which describe this architecture and some of these are listed in the bibliography. Chapter Two is a description of how to write C programs to run on an ARM7 processor and, as such, describes specific extensions to the ISO C standard which are necessary for embedded programming. In this book a commercial compiler is used in the main text, however the GCC tools have also been ported to ARM. Appendix A details the ARM-specific features of the GCC tools. Having read the first two chapters you should understand the processor and its development tools. Chapter Three then introduces the LPC2000 system peripherals. This chapter describes the system architecture of the LPC2000 family and how to set the chip up for its best performance. In Chapter Four we look at the on-chip user peripherals and how to configure them for our application code. Throughout these chapters various exercises are listed. Each of these exercises are described in detail in Chapter Five, the Tutorial section. The Tutorial contains a worksheet for each exercise which steps you through an important aspect of the LPC2000. All of the exercises can be done with the evaluation compiler and simulator which come on the CD provided with this book. A low-cost starter kit is also available which allows you to download the example code on to some real hardware and “prove” that it does in fact work. It is hoped that by reading the book and doing the exercises you will quickly become familiar with the LPC2000.

© Hitex (UK) Ltd.

Page 7

Introduction to the LPC2000

© Hitex (UK) Ltd.

Introduction

Page 8

Introduction to the LPC2000

Introduction

1 Chapter 1: The ARM7 CPU Core 1.1 Outline The CPU at the heart of the LPC2000 family is an ARM7. You do not need to be an expert in ARM7 programming to use the LPC2000, as many of the complexities are taken care of by the C compiler. You do need to have a basic understanding of how the CPU is working and its unique features in order to produce a reliable design. In this chapter we will look at the key features of the ARM7 core along with its programmers’ model and we will also discuss the instruction set used to program it. This is intended to give you a good feel for the CPU used in the LPC2000 family. For a more detailed discussion of the ARM processors, please refer to the books listed in the bibliography. The key philosophy behind the ARM design is simplicity. The ARM7 is a RISC computer with a small instruction set and consequently a small gate count. This makes it ideal for embedded systems. It has high performance, low power consumption and it takes a small amount of the available silicon die area.

1.2 The Pipeline At the heart of the ARM7 CPU is the instruction pipeline. The pipeline is used to process instructions taken from the program store. On the ARM 7 a three-stage pipeline is used.

The ARM7 three-stage pipeline has independent fetch, decode and execute stages

A three-stage pipeline is the simplest form of pipeline and does not suffer from the kind of hazards such as read-before-write seen in pipelines with more stages. The pipeline has hardware independent stages that execute one instruction while decoding a second and fetching a third. The pipeline speeds up the throughput of CPU instructions so effectively that most ARM instructions can be executed in a single cycle. The pipeline works most efficiently on linear code. As soon as a branch is encountered, the pipeline is flushed and must be refilled before full execution speed can be resumed. As we shall see, the ARM instruction set has some interesting features which help smooth out small jumps in your code in order to get the best flow of code through the pipeline. As the pipeline is part of the CPU, the programmer does not have any exposure to it. However, it is important to remember that the PC is running eight bytes ahead of the current instruction being executed, so care must be taken when calculating offsets used in PC relative addressing. For example, the instruction:

0x4000

LDR PC,[PC,#4]

will load the contents of the address PC+4 into the PC. As the PC is running eight bytes ahead then the contents of address 0x400C will be loaded into the PC and not 0x4004 as you might expect on first inspection.

© Hitex (UK) Ltd.

Page 9

1 - The ARM7 CPU Core

1.3 Registers The ARM7 is a load-and-store architecture, so in order to perform any data processing instructions the data has first to be moved from the memory store into a central set of registers, the data processing instruction has to be executed and then the data is stored back into memory.

The ARM7 CPU is a load-andstore architecture. All data processing instructions may only be carried out on a central register file

The central set of registers are a bank of 16 user registers R0 – R15. Each of these registers is 32 bits wide and R0 – R12 are user registers in that they do not have any specific other function. The Registers R13 – R15 do have special functions in the CPU. R13 is used as the stack pointer (SP). R14 is called the link register (LR). When a call is made to a function the return address is automatically stored in the link register and is immediately available on return from the function. This allows quick entry and return into a ‘leaf’ function (a function that is not going to call further functions). If the function is part of a branch (i.e. it is going to call other functions) then the link register must be preserved on the stack (R13). Finally R15 is the program counter (PC). Interestingly, many instructions can be performed on R13 - R15 as if they were standard user registers.

The central register file has 16 word wide registers plus an additional CPU register called the current program status register. R0 – R12 are user registers R13 – R15 have special functions.

© Hitex (UK) Ltd.

Page 10

1 - The ARM7 CPU Core

1.4 Current Program Status Register In addition to the register bank there is an additional 32 bit wide register called the ‘current program status register’ (CPSR). The CPSR contains a number of flags which report and control the operation of the ARM7 CPU.

The Current Program Status Register contains condition code flags which indicate the result of data processing operations and User flags which set the operating mode and enable interrupts. The T bit is for reference only

The top four bits of the CPSR contain the condition codes which are set by the CPU. The condition codes report the result status of a data processing operation. From the condition codes you can tell if a data processing instruction generated a negative, zero, carry or overflow result. The lowest eight bits in the CPSR contain flags which may be set or cleared by the application code. Bits 7 and 8 are the I and F bits. These bits are used to enable and disable the two interrupt sources which are external to the ARM7 CPU. All of the LPC2000 peripherals are connected to these two interrupt lines as we shall see later. You should be careful when programming these two bits because in order to disable either interrupt source the bit must be set to ‘1’ not ‘0’ as you might expect. Bit 5 is the THUMB bit. The ARM7 CPU is capable of executing two instruction sets; the ARM instruction set which is 32 bits wide and the THUMB instruction set which is 16 bits wide. Consequently the T bit reports which instruction set is being executed. Your code should not try to set or clear this bit to switch between instruction sets. We will see the correct entry mechanism a bit later. The last five bits are the mode bits. The ARM7 has seven different operating modes. Your application code will normally run in the user mode with access to the register bank R0 – R15 and the CPSR as already discussed. However in response to an exception such as an interrupt, memory error or software interrupt instruction the processor will change modes. When this happens the registers R0 – R12 and R15 remain the same but R13 (LR ) and R14 (SP) are replaced by a new pair of registers unique to that mode. This means that each mode has its own stack and link register. In addition the fast interrupt mode (FIQ) has duplicate registers for R7 – R12. This means that you can make a fast entry into an FIQ interrupt without the need to preserve registers onto the stack.

© Hitex (UK) Ltd.

Page 11

1 - The ARM7 CPU Core

Each of the modes except user mode has an additional register called the “saved program status register”. If your application is running in user mode when an exception occurs the mode will change and the current contents of the CPSR will be saved into the SPSR. The exception code will run and on return from the exception the context of the CPSR will be restored from the SPSR allowing the application code to resume execution. The operating modes are listed below.

The ARM7 CPU has six operating modes which are used to process exceptions. The shaded registers are banked memory that is “switched in” when the operating mode changes. The SPSR register is used to save a copy of the CPSR when the switch occurs

1.5 Exception Modes When an exception occurs, the CPU will change modes and the PC be forced to an exception vector. The vector table starts from address zero with the reset vector and then has an exception vector every four bytes.

Each operating mode has an associated interrupt vector. When the processor changes mode the PC will jump to the associated vector. NB. there is a missing vector at 0x00000014

© Hitex (UK) Ltd.

Page 12

1 - The ARM7 CPU Core NB: There is a gap in the vector table because there is a missing vector at 0x00000014. This location was used on an earlier ARM architecture and has been preserved on ARM7 to ensure software compatibility between different ARM architectures. However in the LPC2000 family these four bytes are used for a very special purpose as we shall see later.

Each of the exception sources has a fixed priority. The on chip peripherals are served by FIQ and IRQ interrupts. Each peripheral’s priority may be assigned within these groups

If multiple exceptions occur then there is a fixed priority as shown below. When an exception occurs, for example an IRQ exception, the following actions are taken: First the address of the next instruction to be executed (PC + 4) is saved into the link register. Then the CPSR is copied into the SPSR of the exception mode that is about to be entered (i.e. SPSR_irq). The PC is then filled with the address of the exception mode interrupt vector. In the case of the IRQ mode this is 0x00000018. At the same time the mode is changed to IRQ mode, which causes R13 and R14 to be replaced by the IRQ R13 and R14 registers. On entry to the IRQ mode, the I bit in the CPSR is set, causing the IRQ interrupt line to be disabled. If you need to have nested IRQ interrupts, your code must manually re-enable the IRQ interrupt and push the link register onto the stack in order to preserve the original return address. From the exception interrupt vector your code will jump to the exception ISR. The first thing your code must do is to preserve any of the registers R0-R12 that the ISR will use by pushing them onto the IRQ stack. Once this is done you can begin processing the exception.

When an exception occurs the CPU will change modes and jump to the associated interrupt vector

Once your code has finished processing the exception it must return back to the user mode and continue where it left off. However the ARM instruction set does not contain a “return” or “return from interrupt” instruction so manipulating the PC must be done by regular instructions. The situation is further complicated by there being a number of different return cases. First of all, consider the SWI instruction. In this case the SWI instruction is executed, the address of the next instruction to be executed is stored in the Link register and the exception is processed. In order to return from the exception all that is necessary is to move the contents of the link register into the PC and processing can continue. However in order to make the CPU switch modes back to user mode, a modified version of the move instruction is used and this is called MOVS (more about this later). Hence for a software interrupt the return instruction is

MOVS

© Hitex (UK) Ltd.

R15,R14

; Move Link register into the PC and switch modes.

Page 13

1 - The ARM7 CPU Core However, in the case of the FIQ and IRQ instructions, when an exception occurs the current instruction being executed is discarded and the exception is entered. When the code returns from the exception the link register contains the address of the discarded instruction plus four. In order to resume processing at the correct point we need to roll back the value in the Link register by four. In this case we use the subtract instruction to deduct four from the link register and store the results in the PC. As with the move instruction, there is a form of the subtract instruction which will also restore the operating mode. For an IRQ, FIQ or Prog Abort, the return instruction is:

SUBS

R15, R14,#4

In the case of a data abort instruction, the exception will occur one instruction after execution of the instruction which caused the exception. In this case we will ideally enter the data abort ISR, sort out the problem with the memory and return to reprocess the instruction that caused the exception. In this case we have to roll back the PC by two instructions i.e. the discarded instruction and the instruction that caused the exception. In other words subtract eight from the link register and store the result in the PC. For a data abort exception the return instruction is

SUBS

R15, R14,#8

Once the return instruction has been executed, the modified contents of the link register are moved into the PC, the user mode is restored and the SPSR is restored to the CPSR. Also, in the case of the FIQ or IRQ exceptions, the relevant interrupt is enabled. This exits the privileged mode and returns to the user code ready to continue processing.

At the end of the exception the CPU returns to user mode and the context is restored by moving the SPSR to the CPSR

© Hitex (UK) Ltd.

Page 14

1 - The ARM7 CPU Core

1.6 The ARM 7 Instruction Set Now that we have an idea of the ARM7 architecture, programmers model and operating modes we need to take a look at its instruction set or rather sets. Since all our programming examples are written in C there is no need to be an expert ARM7 assembly programmer. However an understanding of the underlying machine code is very important in developing efficient programs. Before we start our overview of the ARM7 instructions it is important to set out a few technicalities. The ARM7 CPU has two instruction sets: the ARM instruction set which has 32-bit wide instructions and the THUMB instruction set which has 16-bit wide instructions. In the following section the use of the word ARM means the 32-bit instruction set and ARM7 refers to the CPU. The ARM7 is designed to operate as a big-endian or little-endian processor. That is, the MSB is located at the high order bit or the low order bit. You may be pleased to hear that the LPC2000 family fixes the endianess of the processor as little endian (i.e. MSB at highest bit address), which does make it a lot easier to work with. However the ARM7 compiler you are working with will be able to compile code as little endian or big endian. You must be sure you have it set correctly or the compiled code will be back to front.

The ARM7 CPU is designed to support code compiler in big endian or little endian format. The Philips silicon is fixed as little endian.

One of the most interesting features of the ARM instruction set is that every instruction may be conditionally executed. In a more traditional microcontroller the only conditional instructions are conditional branches and maybe a few others like bit test and set. However in the ARM instruction set the top four bits of the operand are compared to the condition codes in the CPSR. If they do not match then the instruction is not executed and passes through the pipeline as a NOP (no operation).

Every ARM ( 32 bit) instruction is conditionally executed. The top four bits are ANDed with the CPSR condition codes. If they do not match the instruction is executed as a NOP

© Hitex (UK) Ltd.

Page 15

1 - The ARM7 CPU Core So it is possible to perform a data processing instruction, which affects the condition codes in the CPSR. Then depending on this result, the following instructions may or may not be carried out. The basic assembler instructions such as MOV or ADD can be prefixed with sixteen conditional mnemonics, which define the condition code states to be tested for.

Each ARM (32- bit) instruction can be prefixed by one of 16 condition codes. Hence each instruction has 16 different variants.

So for example:

EQMOV R1, #0x00800000 will only move 0x00800000 into the R1 if the last result of the last data processing instruction was equal and consequently set the Z flag in the CPSR. The aim of this conditional execution of instructions is to keep a smooth flow of instructions through the pipeline. Every time there is a branch or jump the pipeline is flushed and must be refilled and this causes a dip in overall performance. In practice there is a break-even point between effectively forcing NOP instructions through the pipeline and a traditional conditional branch and refill of the pipeline. This break-even point is three instructions, so a small branch such as:

if( x
View more...

Comments

Copyright © 2017 KUPDF Inc.
SUPPORT KUPDF