Tuesday, September 2, 2008

lpc 2148 blinker 1

Blinking the led on the LPC-H2148 board is actually pretty simple. With this example you will only need to build binutils (dont need gcc).

If you are afraid to write to flash (why?) you can compile this for sram only and load and start it from jtag.

To build for flash, use this linker script and these commands:


arm-thumb-elf-as vectors.s -o vectors.o
arm-thumb-elf-ld vectors.o flashmap -o blink.elf
arm-thumb-elf-objdump -D blink.elf > blink.list


To load using JTAG, power cycle the board, press reset a time or few, then:

openocd -f filename.cfg

In another console:

telnet localhost 4444
reset halt
flash write_image erase blink.elf
reset


If done right then when you power on the board it will start running this program.


/* flashmap */
MEMORY
{
rom(RX) : ORIGIN = 0x00000000, LENGTH = 0x1000
ram(WAIL) : ORIGIN = 0x40000000, LENGTH = 0x1000
}
SECTIONS
{
.text : { *(.text*) } > rom
}


To build for sram only, use this linker script and these commands:


arm-thumb-elf-as vectors.s -o vectors.o
arm-thumb-elf-ld vectors.o srammap -o blink.elf
arm-thumb-elf-objdump -D blink.elf > blink.list


To load using JTAG, power cycle the board, press reset a time or few, then:

openocd -f filename.cfg

In another console:

telnet localhost 4444
reset halt
load_image blink.elf
soft_reset_halt
resume 0x40000000


Your program will be lost if you power cycle the board.


/* srammap */
MEMORY
{
rom(RXWAIL) : ORIGIN = 0x40000000, LENGTH = 0x1000
}
SECTIONS
{
.text : { *(.text*) } > rom
}


The program itself. Not much to it. Enable pin P1.24 as an output pin. Clearing the pin (setting the output low, ground) will ground the led and turn it on. Setting the pin high will turn off the led. A simple count down from N timing loop is used to delay the state change. Flash does not power up at full speed, and this program does not make any changes to the clock or flash speed.


/* blink.s */

.TEXT

.ALIGN
.CODE 32

b _start /* reset vector */
b .
b .
b .
b .
nop
b . /* IRQ */
b . /* FIQ */

.GLOBAL _start
_start:
ldr r0,=0xE0028018 /* LPC_2148_IO1DIR */
mov r1,#0x01000000 /* P1.24 */
ldr r2,[r0]
orr r2,r2,r1
str r2,[r0]

ldr r0,=0xE002801C /* LPC_2148_IO1CLR */
ldr r1,=0xE0028014 /* LPC_2148_IO1SET */
mov r2,#0x01000000 /* P1.24 */

mainloop:
str r2,[r0]
mov r3,#0x20000
delay1:
subs r3,r3,#1
bne delay1
str r2,[r1]
mov r3,#0x20000
delay2:
subs r3,r3,#1
bne delay2
b mainloop

.END

Openocd and JTAG

Finally got jtag working for the LPC-H2148, the config file with the openocd sources didnt quite work. The config below works with the Olimex parallel port wiggler and the usb ocd tiny, using the current openocd source (svn) built against the current libftdi, but NOT libft2xx. (dont need either of course for the parallel port). Uncomment the relevant section depending on the interface.

Changing the 14765 to 12000 (starting from the openocd config file) and adding the calc_checksum helped greatly. I dont know what the board/chip is doing but it helps greatly (if you cant connect) to power cycle the board, press the reset button a time or two, and run openocd (openocd -f filename.cfg). Telnet into openocd telnet localhost 4444

If you compiled for flash then:

reset halt
flash write_image erase blink.elf
reset


If you compiled to run out of ram only:

reset halt
load_image blink.elf
resume 0x40000000




#daemon configuration
telnet_port 4444
gdb_port 3333

#Parallel Port Interface
#interface parport
#parport_port 0
#parport_cable wiggler
#jtag_speed 0

# OpenOCD JTAG TINY
#interface ft2232
#ft2232_device_desc "Olimex OpenOCD JTAG TINY"
#ft2232_layout "olimex-jtag"
#ft2232_vid_pid 0x15BA 0x0004
#jtag_speed 20

#delays on reset lines
jtag_nsrst_delay 200
jtag_ntrst_delay 200

# NOTE!!! LPCs need reset pulled while RTCK is low. 0 to activate
# JTAG, power-on reset is not enough, i.e. you need to perform a
# reset before being able to talk to the LPC2148, attach is not
# possible.

#use combined on interfaces or targets that can't set TRST/SRST separately
reset_config trst_and_srst srst_pulls_trst

#jtag scan chain
jtag_device 4 0x1 0xf 0xe

target arm7tdmi little 0 arm7tdmi-s_r4

working_area 0 0x40000000 0x4000 nobackup

#flash bank lpc2000 0x0 0x7d000 0 0 0 lpc2000_v2 14765
flash bank lpc2000 0x0 0x40000 0 0 0 lpc2000_v2 12000 calc_checksum

Roll your own gcc

gcc is not the best, but widely used, works on most platforms, and you are far more likely to find information or help on the net than any other compiler. So why not use it right?

A good place for a pre-built is code sourcery. But if you want to build your own, there is really not much to it.


This works for 4.2.x not 4.3.x, just use 4.2.x




#IF WINDOWS
Go to http://www.mingw.org, find and download

MinGW=5.1.3.exe
MSYS-1.0.11-2004.04.30-1.exe

Install both
Run the Msys shell/prompt to continue as if on a native Linux system.
#ENDIF

#IF LINUX
Make sure you have texinfo, bison, and flex installed, as well as gcc, make and other typical programs
On ubuntu this would be

sudo apt-get install build-essential bison flex texinfo

Note the makeinfo error I am trying to avoid here is part of the texinfo package
#ENDIF

Most of this section is simply a list of the commands to type.

#IF LINUX
You probably dont run as root all the time so you may need to use a directory other than /arm or
as root create /arm and chown to your username so that you can finish the build as the user.
It wants to run from the path used to create it so think about where you want it and who you want
to use it. If you do all of this as root and keep it in /arm (and make it readable by everyone) everyone
can use it.
#ENDIF

Download http://ftp.gnu.org/gnu/gcc/gcc-4.2.2/gcc-core-4.2.2.tar.bz2
Download http://ftp.gnu.org/gnu/binutils/binutils-2.18.tar.gz

ZZZ='--target=arm-thumb-elf --prefix=/arm'
mkdir /arm
cd /arm
tar xzvf /path/to/binutils-2.18.tar.gz
cd binutils-2.18
mkdir build
cd build
../configure $ZZZ

#IF WINDOWS AND SOMETIMES LINUX
edit the Makefile in this directory
Find and change the line from

MAKEINFO = /arm/binutils-2.18/missing makeinfo

to

MAKEINFO = /bin/makeinfo

#ENDIF

make all install

(this will take a while)

/arm/bin/arm-thumb-elf-as --version

GNU assembler (GNU Binutils) 2.18
...

Now binutils, the assembler, linker and other utilties is complete. If you only want to use assembly language this is all you need

To clean up the binutils build files

cd /arm
rm -rf binutils-2.18

On to gcc

cd /arm
tar xjvf /path/to/gcc-core-4.2.2.tar.bz2
cd gcc-4.2.2
mkdir build
cd build
../configure $ZZZ --disable-libssp
make all install

#IF WINDOWS
This will fail at some point with errors that look like

In file included from ../../gcc/libgcc2.c:35:
./tm.h:6:28: error: config/dbxelf.h: No such file or directory
...

It doesnt like the -I by itself in the xgcc command line
The culprit is in gcc/Makefile, a line that starts with

INCLUDES = -I. -I$(@D) -I$(srcdir) ...

Just get rid of that second include

INCLUDES = -I. -I$(srcdir) ...

Then start the build again

make all install

and it will complete this time
#ENDIF

/arm/arm-thumb-elf-gcc --version

arm-thumb-elf-gcc.exe (GCC) 4.2.2

To clean up the gcc build files

cd /arm
rm -rf gcc-4.2.2

#IF WINDOWS
You can exit msys and then copy the arm directory tree from
z:\msys\1.0\arm to z:\arm
(so that your binaries are in z:\arm\bin\)
z:\ is of course whatever directory you happen to be working with C:\ for example
#ENDIF

Welcome to lpcstuff

Welcome, I thought I would keep track of my Philips LPC2000 microcontroller development board experiments.

Currently I have a few boards from Olimex like the LPC-H2148, LPC-H2124, and LPC-H2129.