I recently bought a SAM7-P256 microcontroller kit from Olimex. It’s a nice little board but it was quite a challenge to get something running on it. All the information I could find was either a) for experienced ARM programmers, b) for completely different ARM variants or c) for Windows users.
I am an experienced C programmer but I have no ARM experience. What I really wanted was a fairly straightforward step-by-step guide to getting something simple running for this particular board using a cross-compiler on Linux. I guess I’ll have to write it myself.
So here is my simple step-by-step to get you up and running with a simple blinking LED program using free tools.
Summary
Here is what we’ll be doing:
- Build a toolchain (assembler, C compiler and linker) on a Linux PC.
- Obtain header files specific to the Atmel chip and Olimex board.
- Create the C runtime environment so we can run a C program.
- Create a simple C program and compile it.
- Download the compiled code via USB into the board’s RAM and run it.
Note that I will not be covering how to load a program into flash memory or how to load an operating system onto the ARM.
STEP 1 – Create the GCC toolchain
We’ll be using GCC as our cross-compiler. I found a good step-by-step on setting up GCC for ARM here which I will now summarise.
First you need to visit http://www.gnuarm.com/, go to the files section and download the latest source code for gcc and binutils. I am using binutils-2.17 and gcc-4.1.1 for my example.
Unpack and compile binutils first using the following commands:
tar jxvf binutils-2.17.tar.bz2
mkdir build-binutils
cd build-binutils
../binutils-2.17/configure --disable-werror --target=arm-elf --prefix=/usr/local/arm7
sudo make all install
Next set the path to include the new binutils.
export PATH=$PATH:/usr/local/arm7/bin
I also put this line into my ~/.bashrc so the tools are always available. Now we can unpack and compile:
tar jxvf gcc-4.1.1.tar.bz2
mkdir build-gcc
cd build-gcc
../gcc-4.1.1/configure --target=arm-elf --prefix=/usr/local/arm7 --enable-languages=c --with-newlib
sudo -s
make all-gcc install-gcc
exit
If all that worked, you will now have the following useful programs installed in /usr/local/arm7/bin:
| arm-elf-as |
- Assembler |
| arm-elf-gcc |
- C compiler |
| arm-elf-ld |
- Linker |
| arm-elf-objcopy |
- Utility to convert ELF binary into a raw binary or HEX image |
STEP 2 – Download example files
Download and unpack my example files ARMBlinkExample.tar.bz2, you will see the following files:
| AT91SAM7S256.h |
- Definition of the ARM chip and on-chip peripherals. |
| SAM7-P256.h |
- Definition of additional peripherals on the Olimex board. |
| Makefile |
- Makefile used to compile and link blink.c |
| ram-ln.cmd |
- Linker options. Defines the memory areas we can use. |
| ram-crt.s |
- C runtime code written in assembly language. |
| blink.c |
- The blinking LED example program written in C. |
Please take some time to read through these files or you will not learn anything! The best place to start reading is the Makefile.
I have tried to make this code as simple and easy to follow as I can.
STEP 3 – Connect the board
Now it is time to plug the board into your USB port. Once plugged in, you will need to establish communication with it. There is an excellent free program called Sam_I_Am which provides a good front end. You can download it from http://claymore.engineer.gvsu.edu/~steriana/Software/.
Once unpacked, install it with the following command:
sudo python setup.py install
You will also need a configuration file. I have included one in my example files. Copy it to your home directory:
cp <path_to_example_files>/.samiamrc ~/
Now we need to set up the USB port. Type this command:
sudo modprobe usbserial vendor=0x3eb product=0x6124
Cryptic, you bet. But you need to do this to get Linux to detect the Olimex board. If this is successful, the board will be mapped to device /dev/ttyUSB0 (unless you have other USB serial devices in which case it will have a different number and you will need to modify ~/.samiamrc appropriately).
You should now be able to run Sam_I_Am and test it by typing the version command as in this example:
$ Sam_I_Am
>version
v1.4 Nov 10 2004 14:49:33
NOTE: If your SAM-BA is not working because you have overwritten it by trying to download to the Flash memory, there are some instructions for restoring it here.
STEP 4 – Compile and download the example program
The blink example is intended to be loaded into RAM (not Flash memory). The SAM-BA bootloader has already set up the chip for us so my crt.s code is much simpler than you would find in a production application. There is also no operating system or even a C library! This is real basic stuff.
So now we can compile the example code:
make
This will spit out a file called blink.hex which is in the Intel HEX format that Sam_I_Am requires. Next we can use Sam_I_Am to download it to the board and run it. Use the send command to download the HEX file to RAM and then use the go command to run it as in this example session:
$ Sam_I_Am
v1.4 Nov 10 2004 14:49:33
>send blink.hex
0x00202000-0x00202147: 328 bytes
Start Address: 0x202000
>go 202000
>exit
You should now have some blinking LEDs, woohoo!
There is no function in this simple example to exit back to SAM-BA. You will need to press the board’s reset button. I found that when you do this, Linux will re-map the serial port to /dev/ttyUSB1 which was a bit confusing the first time. The trick is to wait a second or so and then press reset again. It will re-map back to /dev/ttyUSB0.
I hope this tutorial has been useful to you and that the example code provided can make a good starting point for your own embedded applications. If you have any comments, please post them below.
UPDATE
I now have a step-by-step on how to add on a standard C library. Click here.