August 5, 2013

mbed GCC with eclipse KL25Z (Part 1)

I have published my ongoing work, adding GCC to freedom board KL25Z. I have been using eclipse (offline compiling, debugging) with JLink (could be used with Jlink firmware for openSDA, not tested yet due to my prototype board which refuses to flash Jlink firmware). I am writing this on a laptop using windows, therefore paths included here are for windows, blame on me :-) The first part is dedicated to be able to compile an application and get a binary output.

For those who hate eclipse or don't need to debug their code, just skip steps with the eclipse. This should be your step by step routine:
install gcc toolchain, download mbed sources, build mbed library, download makefile, build an application and the last step - copy bin file to mbed file disk.

There's already cookbook for eclipse debugging on mbed eclipse for building and debugging [mbed.org]. I will refer to this guide as mbed cookbook, don't need to repeat what is trivial and usually you set an enviroment only once anyway.
We will use a different approach, clone mbed git repository, set everything in eclipse, use a makefile and set a debugger. Using this approach, you use always newest version of mbed.

1. step - Install eclipse, make and GNU toolchain
Follow Install Eclipse with C/C++ and GNU Support in the mbed cookbook. It provides information how to install eclipse and make.

2. step - Download GNU toolchain
I use GCC ARM, download it from here GCC ARM embedded [launchpad.net]

3. step - Clone mbed git repo and build mbed sources for debugging
mbed git repository is here mbed git repository [github.com]. If you don't use git, neither github, download repo as zip file. This one step is crucial for offline debugging.
As soon as you have mbed source files, there's one important link to visit Mbed Tools [mbed.org/handbook]. I'll describe what you have to do in order to get mbed library. Thus you don't need to visit mbed tools link.

Create a new file named private_settings.py in mbed/workspace_tools. Here's mine set up for ARM GCC 4.7 and also KEIL which is not our concern right now. I use debug info option as default to be able to debug mbed sources.

from os.path import join

ARM_PATH = "C:/Program Files/Keil/TAD/ARM"
ARM_BIN = join(ARM_PATH,"ARMCC","bin")
ARM_INC = join(ARM_PATH,"RV31","INC")
ARM_LIB    = join(ARM_PATH,"ARMCC","lib")
ARM_CPPLIB = join(ARM_PATH, "ARMCC","lib","cpplib")

GCC_ARM_PATH = "C:/Program Files/GNU Tools ARM Embedded/4.7 2013q1/bin"

BUILD_OPTIONS = ["debug-info"]

Time to build our own mbed library, type python build.py -m KL25Z -t GCC_ARM in the command line from workspace_tool directory. This should be an output:


At the end of the output is Build successes:   * GCC_ARM::KL25Z. There was created a directory one folder up, named build. This directory should be added to your project, that's mbed library. There's library libmbed.a and many more files which I don't need to explain here.


4. step - create new project in eclipse
File -> New -> C++ Project
Select Makefile Project -> Empty Project in Project Type, Toolchains, choose Cross GCC.



Go to the project properties as on the picture below, the compiler invocation command should be arm-none-eabi-gcc for GCC and arm-none-eabi-g++ for G++ compiler.



Click on C++ Build and uncheck Use default build command,  type there
C:\PROGRA~1\GnuWin32\bin\make.exe . Note here, that I had a problem with spaces in paths, therefore I changed PATH variable in Environment which is under C/C++ Build options. Please verify there's also folder where you installed GCC ARM (an example C:\PROGRA~1\GNU Tools ARM Embedded\4.7 2013q1\bin).



5. step - get a makefile or create a new one
This step is optional. I wrote the makefile to build an application with mbed. Download it from mbed_gcc_kl25z [https://github.com/0xc0170].

The file structure in mbed changes seldomly, therefore I include here what to check in my makefile and probably there will dwell a problem.
A variable MBED_OBJ contains object files from the mbed source. The dirs variables (INC_DIRS and SRC_DIRS) define directory structure as it is at the moment.


6. step - copy the mbed library and an application
Copy the mbed folder which is inside build directory. It's output from the step 3. I have selected to start with Hello World (KL25z examples [mbed.org/handbook]).

Here is the project tree structure.

mbed folder is folder which is copied from sources build which we did in the 3d step. main.cpp contains hello world application, makefile is my own. Files starting with . (project and cproject) are eclipse project files.


7. step - build an application
If you set everything according to this tutorial, you should be able to build an application.
Here's the output from the console:

Finished building target: out/mbed.elf

Printing size
arm-none-eabi-size --totals out/mbed.elf
   text       data        bss        dec        hex    filename
  26932        216        316      27464       6b48    out/mbed.elf
  26932        216        316      27464       6b48    (TOTALS)
arm-none-eabi-objcopy -O srec out/mbed.elf out/mbed.s19
arm-none-eabi-objcopy -O binary -v out/mbed.elf out/mbed.bin
copy from `out/mbed.elf' [elf32-littlearm] to `out/mbed.bin' [binary]
arm-none-eabi-objdump -D out/mbed.elf > out/mbed.lst
arm-none-eabi-nm out/mbed.elf > out/mbed-symbol-table.txt


The output size is huge (text section), thus we will need to tune our makefile in order to squeeze it.


One last note, I use KL25Z, to set it to any other platform should be trivial. The binary file was generated in the output folder. That one is ready to be copied to mbed.

That's compiling part. I'll continue in the next post with debugging part.