Setting Eclipse to work with Milos (stm32)
Introduction
Here I will show you the way we work with Milos, with Eclipse. By far, Eclipse is one of the best programming environments around. Together with some plug-ins one can get a very professional framework to develop with. In this case we will be setting up Eclipse + Codesourcery toolchain + GNU ARM Eclipse plug-ing + OpenOCD, to work with Milos (but it can be used to compile and debug general ARM code for a vast range of platforms).
I will try to not differentiate under which operating system we are installing these tools. Linux users in general have the advantage over this tutorial, but Windows user can directly download and install complete toolchains (there are many around). Anyway, this tutorial can be practical for both operating system users, and it is aimed to people who never set up a toolchain or always used proprietary software.
The toolchain
We need: an ASM and C/C++ compiler, a linker and a debugger. There are a couple of options, like Yagarto and Codesourcery, that under Windows will not need Cygwin or MinGW. For this tutorial we will be using the "lite" version of Codesourcery toolchain. Go here and download the EABI version for you operating system. By the time of writing this tutorial the 2010.09-51 is the current version.
Windows users may double-click the installer. Linux user would execute a /bin/sh ./arm-2010.09-51-arm-none-eabi.bin on the directory where the package is located (Debian/Ubuntu users may need to execute sudo dpkg-reconfigure -plow dash before executing the installer).

Follow the steps and perform a Typical installation. May sure you let the installer to configure the PATH to the binaries. This way they can be executed from any place on the system. Remember that Eclipse will be executing these tools invoking them from the command line, so it's a good idea to open a console or CMD.EXE to test if any of the binaries (like arm-none-eabi-gcc) can be reached.
Eclipse
We will need Eclipse IDE for C/C++ Developers (also known as Eclipse CDT).
NOTE: By the time of writing this tutorial, there is a bug with the latest version of Eclipse CDT (CDT 7.0.1), that breaks the GDB Hardware Debugging feature, so they recommend to download the CDT 7.0.1 Build I201009241320. To do this download Eclipse Classic (Eclipse without CDT) and CDT 7.0.1 Build I201009241320.
Remember that the JAVA runtime has to be installed, otherwise Eclipse won't run.
Once downloaded, execute Eclipse. The installation is very simple: the .zip file contains only a eclipse folder that you need to copy in a location you like. Fire up Eclipse. You will be asked to define a "Workspace" folder. Every project you make should be placed in this folder. Choose a convenient location (C:\workspace or \home\youruser\workspace should be fine).
You will need to manually install CDT 7.0.1 if the CDT + GDB Hardware debugger bug is still not fixed (mentioned above). Go to the Window >> Install New Software option and click on the Add... button. Click the Archive button and select the CDT 7.0.1 Build I201009241320 file you previously downloaded (cdt-master-7.0.1-I201009241320.zip). Click OK and then select both the checkboxes in the list below, like the following:

Click Next until finishing the installation.
GNU ARM Eclipse Plug-in
This plug-in will save you a lot of time. This means that (for now) you won't be fighting against autohell (google for it). This is very nice if you are at the very beginning of embedded development. Let's say that simply adding an extra source file won't hurt your eyes figuring out which makefile you must modify.
The GNU ARM Eclipse Plug-In is a SourceForge project that can be downloaded and installed separately, but we will use the Eclipse's "Install new software..." method. In the Eclipse main window, click the Help menu and select Install New Software... option. Paste the following in the Location textbox: http://gnuarmeclipse.sourceforge.net/updates. Click on the checkbox in the list below and press follow the installation (press Next).

If everything went OK you can try to set up a new C Project from the File menu >> New >> C Project. On a successful installation you will see the ARM Cross Target Application project type option on the left side list, and a list of possible toolchains to use on the right side list (here, the plug-in will list some toolchains that are not installed; the plug-in doesn't "scan" for installed toolchains).
Final steps to compile
If everything went OK it's time to download Milos and to unpack it on the Eclipse's Workspace folder you choose. Open Eclipse and the Milos Workspace should automatically load.
This may not be needed if the Milos project loaded fine: Milos needs some paths configured in order to compile (a small price has to be paid in order to not use autotools, yet).
Before continuing setting these paths it's better explain how Milos is organized. The main Milos directory contains the following structure:
\Milos \app (the directory where you put your application code. You must create a new directory and put your main.c file in there) \core (where the main OS files reside) \drivers (here we define the different drivers the OS supports, each one in an individual directory) \platform (platform-dependent code. Each microcontroller architecture has a dedicated directory in there, and contains code that only applies to it)\common (common files, string and memory management)
\fs (file systems directory. Each file system type should have its own directory)
So what we need is to define include paths in order to use the features we want.
We need to define:
- the path to the main Milos folder,
- the path the platform we want to use,
- and, if needed by the platform, paths to include directories it may need.
This is done using the GNU ARM Eclipse Plug-In. From the Project Explorer, right-click on the Milos project and select Properties. Under the C/C++ Build option, select Settings. After that go to the ARM Sourcery Linux GCC C Compiler options and select Directories. On the right side there is a list, and by clicking the + button you can set Include paths. It's always a good thing to set the paths relative to the workspace.
For this tutorial set the include paths like the following:

Another thing to set is the linker script. This is done under the ARM Sourcery Linux GCC C Linker options, General sub-option: on the right there is the Script file (-T) text box. Set it to "${workspace_loc:/Milos}/platform/stm32/gcc/plat_comp_dep.ld" and click OK.
We are currently working in a plug-in for Eclipse to set these parameters in an automated way.
If everything went OK, you should start compiling Milos with ctrl+b. If you find multiple definition-like linker errors, that's because Milos could be shipped with various examples under the \app directory, and the linker has found that functions like main() are defined multiple times (among all examples). Just ctrl+right click the \app\"examples-you-don't-want-to-compile" directories and select Exclude from build option.
Debug with OpenOCD
We will use OpenOCD for debugging code. OpenOCD (Open On-Chip Debugger) is an open source project (a very active one) created by Dominic Rath as part of a diploma thesis for the University of Applied Sciences, FH-Augsburg. Practically, this wonderful piece of software has an embedded GDB server, that receives GDB commands from Eclipse and translates them into JTAG signals. This way you can debug your code from the very development environment, setting breakpoints and watching memory/variables. A lot of JTAG cables are supported and the project is in constant development.
You will find that many of the JTAG cables around are based on a chip from FTDI Chip. In order to compile (and use) OpenOCD with these JTAG cables you need to link against proprietary libraries from FTDI Chip. Distributing the compiled OpenOCD binaries with these libraries violates the OpenOCD GNU GPL license. There are alternatives like compiling and distributing OpenOCD with libFTDI, an open source version of the FTDI Chip libraries. You can download a ready-to-use precompiled OpenOCD version from Freddie Chopin's website.
Of course, you can always compile OpenOCD by yourself using FTDI Chip proprietary libraries. This is an issue we will not discuss here. Again, Linux users have the advantage here (compiling OpenOCD on Windows will require extra tools that Linux already has). Just be sure you include the JTAG cable you own when executing ./configure command. The rest of the settings can be used as default.
To start debugging, spawn OpenOCD. The way you launch OpenOCD may vary: for example on linux you would execute openocd -f interface/jlink.cfg -f target/stm32.cfg to use OpenOCD with a J-Link JTAG on a STM32.
We need to configure how Eclipse will communicate with OpenOCD. That's done under the Run >> Debug configurations menu. On the left side list, right-click the GDB Hardware debugging option and select New. On the right-side of the window click the Search Project... button and select Milos.elf. Now go to the Debugger tab and change the GDB Command from the default value gdb to arm-none-eabi-gdb. This will tell Eclipse to launch our Codesourcery GDB debugger. After that go to the Startup tab and paste the following into the Initialization commands text box:
monitor set remotetimeout 10000monitor reset halt
monitor poll
monitor stm32x mass_erase 0
monitor flash write_image "${workspace_loc:/Milos/stm32-Debug/Milos.elf}" file "${workspace_loc:/Milos/stm32-Debug/Milos.elf}"
break main
That's it. Click Apply button and the Debug button to start debugging. Each time you need to debug click the bug button 
Have fun !!!
