
The insider's guide to the Philips ARM7-based microcontrollers (T. Martin, 2005)
.pdfIntroduction to the LPC2000 |
5 – Tutorial With Keil Tools |
|
|
Exercise 26: Receiving CAN Data
This is the same code as in the previous example but this time it is presented from the receive point of view.
Open the project in C:\work\EX24-CANRX
In main.c add the following lines to configure the acceptance filter to receive messages
Declare an array in the filter memory
unsigned int StandardFilter[2] _at_ 0xE0038000;
Configure the standard message filter table
AFMR |
= |
0x00000001; |
StandardFilter[0] |
= |
0x20012002; |
StandardFilter[1] |
= |
0x20032004; |
SFF_sa |
= |
0x00000000; |
SFF_GRP_sa |
= |
0x00000008; |
Release the receive buffer before leaving the ISR
C1CMR = 0x00000004;
Build the code and start the debugger.
Run the code as far as initialising the CAN peripherals.
Examine the Acceptance filter memory in peripherals/CAN/Acceptance filter This view shows you which messages may be received by the CAN peripherals. Continue to run the code and receive a message.
Check that the message ID matches the Index assigned in the Acceptance filter.
180
Introduction to the LPC2000 |
5 – Tutorial With Keil Tools |
|
|
181
Introduction to the LPC2000 |
6 – Tutorial With GNU Tools |
|
|
Chapter 6: Tutorial With GNU Tools
Intoduction
The following tutorial demonstrates how to setup a project in uVISION for the GNU compiler. Exercises 1 – 6 are repeated to show the non-ANSI aspects of the GNU compiler. Once you are familiar with these exercises, you can rejoin the main tutorial but use the exercise examples in the GCC directory.
GCC Startup Code
The startup code used in the GNU project is different in that the Keil Assembler has different directives and naming conventions. However, it is performing the same operations. It is up to the programmer to edit the vector table as discussed in the section on the Keil compiler startup code. The graphical editor allows you to configure the processor stacks and system peripherals in the same way as the Keil compiler startup code.
Interworking ARM/THUMB Code
The GCC compiler also supports the ARM procedure calling standard and allows interworking between the ARM and THUMB instruction sets. However, unlike the Keil compiler, it is not possible to select individual functions as ARM or THUMB. In the GCC compiler all ARM code must be in one module or modules and the THUMB code must be in separate modules. These modules are compiled as ARM or THUMB as required and then linked together. This process is described in example 3 in this section.
Accessing Peripherals
The Keil and GNU compilers can use the same include files to access the on-chip SFR registers.
Interrupt Service Routines
The GCC compiler has a set of non-ANSI extensions which allow functions to be declared as interrupt routines. The general form of the declaration is shown below
void IRQ_Routine (void) __attribute__ ((interrupt("IRQ")));
The following keywords are available to define the exception source required:
FIQ,IRQ,SWI,UNDEF.
This function declaration is only required on the function prototype and should not be used on the main body of the function. An interrupt service routine is shown in example 5.
182
Introduction to the LPC2000 |
6 – Tutorial With GNU Tools |
|
|
Software Interrupt
There is no real software interrupt support in the GCC compiler. To generate a software interrupt you must use inline Assembler as shown below:
#define SoftwareInterrupt2 asm (" swi #02")
This will place a SWI instruction encoded with the value 2 in your code. Next it is possible to declare a pointer to a CPU register using the non-ANSI register keyword as shown below:
register unsigned * link_ptr asm ("r14");
This allows us to read the contents of the link register when we enter the ISR. When the SWI instruction is executed, the CPU will enter supervisor mode and jump to the SWI vector. The address of the SWI instruction plus four will be stored in the link register. On entry to the software interrupt ISR the following line of code is executed:
temp = *(link_ptr-1) & 0x00FFFFFF;
The address stored in the link register is rolled back by one instruction (word-wide pointer i.e. four bytes) so that it is pointing at the address of the SWI instruction which generated the exception. The top eight bits of the SWI instruction are masked off and bits 0-23 are copied into the temp variable. This in effect loads the number 2 into the temp variable. A switch statement can now be used to run the desired code. This method of handling software interrupts is shown in example 6.
Inline Functions
Within the GNU compiler functions may be declared as inline functions as follows:
inline int fast_function(char param1)
183

Introduction to the LPC2000 |
6 – Tutorial With GNU Tools |
|
|
Exercise 1: Using The Keil Toolset With The GNU Compiler
This example is based on the source code which can be found in:
C:\Exercise\Work\EX1 first program
In this first exercise we will spend some time defining a first project, building the code and downloading it into the Simulator for debugging. We will then cover the basic debugging functions of the Keil simulator.
The Keil uVISION IDE is designed to support several compilers: the GNU C compiler, the ARM development suite and the Keil ARM compiler. Before compiling, make sure you have the GNU compiler selected. This is done by activating the project workspace, rightclicking and selecting ‘manage components’. In this dialog, select the Folders/extensions tab and make sure the GNU tools box is selected.
Double-click on the Keil Uvision3 icon to start the IDE.
184

Introduction to the LPC2000 |
6 – Tutorial With GNU Tools |
|
|
From the menu bar select Project\New Project.
In the New Project dialog navigate to your desired project directory.
In the New Project dialog name the project first.uv2 and select Save.
A ‘select new device for target’ dialog will appear. Navigate through the device data base and select the Philips\LPC2129 folder and select OK.
185

Introduction to the LPC2000 |
6 – Tutorial With GNU Tools |
|
|
In the project browser highlight the ‘Target1’ root folder and select the local menu by pressing the right mouse button. In this menu select ‘Options for Target’.
In the ‘Target’ tab set the simulation frequency to 12.000 MHz.
In the Linker tab select the linker file flash.ld and tick the “Garbage collection” and do not use “standard startup files” boxes
186

Introduction to the LPC2000 |
6 – Tutorial With GNU Tools |
|
|
Note: To build the project so it will run within the on-chip RAM of the LPC2100 device, configure the Text start as select the linker file RAM.ld
In the debug tab make sure the “Use Simulator” radio button is active. Also make sure “Load Application at Startup” and “Go till main()” are checked.
Select OK to complete the target options.
In the project browser expand the ‘Target1’ root node to show the Source group 1 folder.
Highlight the ‘Source Group 1’ folder, open the local menu with a right click and select ‘Add Files to group Source Group1’.
187

Introduction to the LPC2000 |
6 – Tutorial With GNU Tools |
|
|
In the ‘Add files to Group’ dialog add the file blinky.c and serial.c.
Change the ‘Type of file’ filter to ASM and add the file startup.s
These are all the source files necessary for the project so select close.
You can view the source code contained in a file by double-clicking on the file name in the project browser window.
Once you have added all the source files the project can be built via the program menu or by the build button on the toolbar.
Once the code is built, you can start the simulator by pressing the debugger button. The use of the simulator and JTAG debugger are detailed in Exercise One in the Tutorial and are the same for the GNU compiler.
188

Introduction to the LPC2000 |
6 – Tutorial With GNU Tools |
|
|
Exercise 2: Startup Code
In this exercise we will configure the compiler startup code to configure the stack for each operating mode of the ARM7. We will also ensure that the interrupts are switched on and that our program is correctly located on the interrupt vector.
Open the project in EX2 Startup\work
Open the file Startup.s and using the graphical editor configure the operating mode stacks as follows:
Compile the code
Start the simulator and when the PC reaches main, examine the contents of each R13 register.
Each stack is allocated a space of 0x80. The user stack is 0x400 bytes so user data will start at 0x40003d80 – 0x400
Start of stack space at the top of on-chip memory
189