Tuesday, May 11, 2010

ARM LPC-2148 On Linux

There are zillions of micro-controller enthusiasts around the globe. Everyone has a different approach to experiment and learn micro-controllers. We have lots of choices too. Some 8-bit options are: 8051, PIC and AVR. While the most common 32-bit option is ARM. I have worked with 8051 and felt that its time to go for a 32-micro-controller.

For working with micro-controllers, all we need is:

1. An Editor

2. A Cross compiler, assembler, linker and some other tools(like debugger) collectively called a tool-chain.

3. A utility to download the executable binary to the target development kit or an emulator to give you a virtual environment to give you a feel of how your program runs on real hardware.

4. A JTAG debugger setup. The is some thing that one has to use one day or the other if he is working with higher architecture micro-controllers

While 1 and 2 are a must, a choice can be made on 3 and 4. The options are to go with a development kit and get a hands-on experience of the hardware or to go with a simulated environment.

1) My laptop has Ubuntu-8.10. Though there are options for opensource IDEs, I chose to build one for myself. I did what was given here.

Some other choices are Yagarto and Codesoucery

Some references for building the toolchain are:-
frank.harvard.edu and hermann-uwe.de

The references are myriad. I was not able to download the newlib package and so went with GCC 3.4 toolchain at gnuarm.com. I am using GNU-EMACS as my editor(should I say my IDE rather!).

2) I chose to go with a development kit and get a feel of real hardware. That's how I came across Blueboard-LPC-2148 development kit. I chose Blueboard because it was economical, at first place, had a code library for reference besides other documents and was ISP UART programmble. So all I need is an Null modem serial (UART) cable to program the board and I can postpone buying the debugger setup for now. But I know I'll need it soon.

Here are some links for those who want to work with ARM simulators:
qemu-ARM and qemu home.

Some other LPC series development kit vendors are Robokits, olimex, Rhydolabz, NSK electronics

I think olimex has most users and support. Most of the discussions and documentation I came across were based on it.

3) Now I wanted a tool to flash the executable to the target through serial port. I had choices here too:- LPC ISP and LPC 2000 Programmer

While lpcisp is a windows executable, it can run on wine. I went with lpc2k_pgm. It is available as a pre-compiled binary and as a source distro. I built the binary from the source.

Now that I have a development environment on my linux machine with:
* development kit (Blueboard-LPC-2148)
* An IDE (EMACS + GNU toolchain) and
* A flashing utility(lpc2k_pgm)
[Perhaps debugger is missing, but I'll get it soon!], I am a few steps away from writing my first program.

For a C-program to execute:
1 a stack needs to be initialized,
2 Hardware settings ,like disabling interrupts, need to be made.
3 Variables need to be loaded to memory and different object modules have to be linked with source[main()]
4 call main() to execute the source code.

A startupfile does the first two and also calls main() after all hardware initlization. I have taken the startups given by Blueboard. I made a few changes as they were giving some link and assembler problems.
The startups are software_init.s and hardware_init.s

A linker script does task 3 and I have taken the linker script from Blueboard's library. You can find it here.

Aah! I am ready to write my first program. Unlike "hello world" this is a program to blink the led on the target board. The source directory is here.

To summerize:
assemble the startup files as shown here:

arm-elf-gcc -mcpu=arm7tdmi -mlittle-endian -Wall -O0 -gdwarf-2 -std=c99 -march=armv4t -mlittle-endian -fno-builtin -c <ASSEMBLY_SOURCE_NAME>

Compile(only) the C source modules as follows:

arm-elf-gcc -mcpu=arm7tdmi -mlittle-endian -Wall -O0 -gdwarf-2 -std=c99 -march=armv4t -mlittle-endian -fno-builtin -c

Link all objects with the main program as follows:

arm-elf-gcc -mcpu=arm7tdmi -mlittle-endian -Wall -O0 -gdwarf-2 -std=c99 -march=armv4t -mlittle-endian -fno-builtin -nostartfiles -Wl,-Map=main.map,--cref -lc -lm -lc -lgcc -T lpc2148_gcc.ld main.c led.o sw_delay.o tnkernel_startup_gcc.o startup_hardware_init.o -o main.elf

Gernerate the hex file with objdump:

arm-elf-objcopy -I ihex main.elf main.hex

Two drawbacks I have with my setup is that the toolchain I am using is old and second is that it is a command line setup and not GUI setup.

Here are some reference links:
wriggler
LPC2000_Archive
USB JTAG schematic
An article on Embedded ARM design and development on Opensource platform
Building Toolchain
LPC2000 Disscussion Forum
ARM Architecture Reference Manual(ARM ARM)