Setting up Eclipse and Code Sourcery lite for STM32 Discovery Development

Here is a step by step guide to setting up a Open Source Stm32 Arm based embedded development tool chain with the Eclipse IDE.

It is Targeted for the ST32-Discovery demo board under Windows (This was implemented under Windows 7 64bit) and uses the open source Code Sourcery version of the Gnu Arm cross assembler.

The project will be set up to use the Arm CMSIS Library which provides a common infrastructure for Arm MCU's and the Standard Peripheral Library so you should be able to run any of the STM32 Discovery example projects with this configuration.

First The Requirements.

This is what you will need to download

The Java JRE from here http://www.java.com/en/download/index.jsp  if you don't already have Java installed (This is needed to run Eclipse)

The Eclipse Helios IDE for C/C++ Developers available from here http://www.eclipse.org/downloads/packages/release/helios/sr1

The STLink Utility from here http://www.st.com/stonline/products/support/micro/files/um0892.zip

The ARM-based 32-bit MCU STM32F10x Standard Peripheral Library v3.3 from here http://www.st.com/stonline/products/support/micro/files/stm32f10x_stdperiph_lib.zip

The STM32VLDISCOVERY firmware package from here http://www.st.com/stonline/products/support/micro/files/an3268.zip

The IA32 Windows Installer of the Code Sourcery Lite Arm GNU Tool chain form http://www.codesourcery.com/sgpp/lite/arm/portal/release1294

Preparing the Workspace.

Eclipse works with workspaces which are folders on the file system containing related projects , lets set up a folder for our projects.

I created the folder D:\stm32-discovery-projects which will will call <project-base> from now on.

Now within <project-base>
  • Create a folder "workspace"
  • Unzip the Standard Peripheral Library into <project-base>
and so you should have something like ..
 

Install The Code Sourcery EABI tool chain for Windows

Use the Windows IA32 Installer and accept all the default options.


When that is installed , go to the /<codesourcery install dir>/Sourcery G++ Lite/bin folder and copy cs-rm.exe to rm.exe

Installing Eclipse

First install Java if you don't already have Java installed on your system.

Eclipse has no set-up program , you just unzip it to somewhere on your file system , you may want to give the folder another name rather then "eclipse" such as "eclipse-stm32-devel" so you can have multiple versions of Eclipse.
We will call this <eclipse-base>, also at this point it's useful to create a short cut to the "<eclipse-base>\eclipse.exe" on the desktop to run Eclipse easily.

OK Now Run the "<eclipse-base>\eclipse.exe" and you will get this dialog ..


Set the workspace to the one you created earlier <project-base>\workspace and tick "Use this as a default and do not ask again". All going well you should get the initial Eclipse screen.


Now select Help->Install new software


and click Available Software Sites


Type in "cdt" into the filter so we can narrow the list down, select the http://download.eclipse.org/tools/cdt/releases/helios Location and then click OK



Select the new download site from the Work with drop down, and then select C/C++ GCC Cross Compiler Support

Click on Next and continue on with the install Wizard and let the Cross Compiler Support install.


You will then be asked to Restart Eclipse , do that now.


Creating a Stm32 Arm based project Template

A Small point of information in the following I set up the Debug target only , afterwards I realised it would have been better to apply all the settings to the All Configurations target so both debug and release are set-up broadly the same
 
Next we create a project called Template. We will use this as a base project so it can be copied when you want to start an new project with all the following settings in place.


Select Cross-Compile Project and Cross GCC and click Next>




Put in the tool command prefix arm-none-eabi-   ( note the trailing - )  and then browse to the directory you installed the Code Sorcery Gnu Arm Tool chain and select the \bin folder then select Finish


Next Click on the Go to workbench Icon ..


And we get the C/C++ Perspective.


Right Click on the Template project in the project explorer and then select C/C++ Build->Tool Chain Editor


Click Select Tools.. and add the GCC Assembler with Add tool-->



Now we need to correct the assembler name for the cross compiler version as this is setup as the GCC assembler as there is no Cross Assembler listed.



We do this by selecting Settings->GCC Assembler and changing the Command to include the full cross compiler prefix.

Also in the General Section of GCC Assembler we need to set these Flags:-

-mthumb -mcpu=cortex-m3

And in the Miscellaneous Sections for the the Cross GCC Compiler and Cross G++ Compiler we need these flags:

-c -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd

And finally in the Miscellaneous Section for the the Linker section

-T ${ProjDirPath}\startup_src\stm32_flash.ld -mthumb -mcpu=cortex-m3  -mfix-cortex-m3-ldrd -Wl,-Map,linker.map -Wl,--gc-sections


-T "${ProjDirPath}\startup_src\stm32_flash.ld"  -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd   -Wl,-Map=linker.map -Wl,-cref  -Wl,--gc-sections

Next ,OK the settings Dialog and then Right Click on the Template project again and select New Folder. 

Type "src" and finish , do the same again for "startup_src" so we have the following folder structure..




Now copy the file <project-base>\STM32F10x_StdPeriph_Lib_V3.3.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\TrueSTUDIO\startup_stm32f10x_md_vl.s
to startup_src. Note that you can drag and drop or cut and paste the file into the folder.

Note due to a bug in Eclipse CDT , to be recognised as an assembly file the eclipse cdt needs assembly files to have an upper case S file extension, so now change the name to startup_stm32f10x_md_vl.S , also it's is likely that the file-name of the assembler has been incorrectly set-up, so you will need to right click on startup_stm32f10x_md_vl.S and choose properties then select C++ Build->settings and correct the entries for the assembler name and flags to the same as entered above.

Next right-click on startup_src and select New File, enter the filename stm32_flash.ld and Finish. Now Right click on the newly created file and select Open With -> Text Editor and paste the following text...


/*
* STM32 Discovery Linker Script
*/

_estack = 0x20002000;    /* global needed for startup_stm3210x_md_vl.S */
_minimum_stack_size = 0x100; /* 256 bytes miniumum stack size */

ENTRY(Reset_Handler)

MEMORY
{
  rom (rx)      : ORIGIN = 0x08000000, LENGTH = 128K
  ram (xrw)     : ORIGIN = 0x20000000, LENGTH = 8K
}

SECTIONS
{
  .text :
  {
    KEEP(*(.isr_vector))
    *(.text)           /* .text sections (code) */
    *(.text*)          /* .text* sections (code) */
    *(.rodata)         /* .rodata sections (constants, strings, etc.) */
    *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
    . = ALIGN(4);
    KEEP (*(.init))
    KEEP (*(.fini))
    . = ALIGN(4);
  } >rom

   /* .ARM.exidx is sorted, so has to go in its own output section.*/
   .ARM.exidx : {
        __exidx_start = .;
        *(.ARM.exidx* .gnu.linkonce.armexidx.*)
        __exidx_end = .;
    } >rom

    _sidata = .;         /* global needed for startup_stm3210x_md_vl.S */ 

  /*now start ram area with initialized data section */
  .data :
  {  
    _sdata = .;        /* global needed for startup_stm3210x_md_vl.S */
    *(.data)         
    *(.data*)        
    . = ALIGN(4);
    _edata = .;        /* global needed for startup_stm3210x_md_vl.S */
  } >ram AT>rom

  /* uninitialized data section bss*/
  .bss :
  {
      . = ALIGN(4);
    _sbss = .;         /* global needed for startup_stm3210x_md_vl.S */
    *(.bss)
    *(.bss*)
    *(COMMON)
    . = ALIGN(4);
    _ebss = .;        /* global needed for startup_stm3210x_md_vl.S */
  } >ram

 
  /* Generates an error if minimum stack size is not left in ram */
  ._check_stack_space :
  {
    . = ALIGN(4);
    . = . + _minimum_stack_size;
    . = ALIGN(4);
  } >ram

  .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) }

  /* Remove debug information from the standard libraries */
  /DISCARD/ : {
      *(.note.GNU-stack)
    libc.a ( * )
    libgcc.a ( * )
    libm.a ( * )
  }

and finally Save the file






Copying files from the The STM32VLDISCOVERY firmware package

Open up the firmware zip package an3268.zip and copy the files in \stm32vldiscovery_package\Project\Examples\GPIOToggle to your src folder



Don't copy the folders just the source files and readme.txt.

Also copy the files in \stm32vldiscovery_package\Utilities to the src folder



So your source folder should look like this...




Were not quite done yet , go back to the project properties (right click on the Template project->Properties) and select C++ General->Paths and Symbols  and select the Source Location tab

Now use Add Folder.. to add the src and startup_src directory's if they are not already there.

Then use the Link Folder.. button to add these paths (you need to set the Link to folder in the file system check box)

<project-base>\STM32F10x_StdPeriph_Lib_V3.3.0\Libraries\CMSIS\CM3\CoreSupport
<project-base>\STM32F10x_StdPeriph_Lib_V3.3.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x
<project-base>\STM32F10x_StdPeriph_Lib_V3.3.0\Libraries\STM32F10x_StdPeriph_Driver\src



When done it should look something like this:-




Now switch to the Include tab and select GCC then add the project src folder and the following paths , make sure you select GCC when you do this

Now we need to add the src folder in a special way , so that it will adjust to the project name when we make a copy of the project,


so we enter /${ProjName}/src and make sure the Is a workspace path check box is checked as above.

Now enter the following file system paths as well.

<project-base>\STM32F10x_StdPeriph_Lib_V3.3.0\Libraries\CMSIS\CM3\CoreSupport
<project-base>\STM32F10x_StdPeriph_Lib_V3.3.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x
<project-base>\STM32F10x_StdPeriph_Lib_V3.3.0\Libraries\STM32F10x_StdPeriph_Driver\inc

When done it should look something like this:-




Next we need to set some defines , go to the Properties->C++ Build->Settings->GCC Cross Compiler->Symbols and add the following  defines

STM32F10X_MD_VL

USE_STDPERIPH_DRIVER

USE_STM32_DISCOVERY







Next , Go to the Properties->C++ Build->Settings and select the Build Steps tab and enter the following in the Post-build-steps Command section and give it a description.

arm-none-eabi-objcopy  ${ProjName} ${ProjName}.bin -O binary

arm-none-eabi-objcopy -S  -O binary  "${ProjName}" "${ProjName}.bin"





This will create a binary file which we can upload with the STLink Utility.

If you haven't already done so Install the STLink Utility with its default settings , now from the main menu bar of Eclipse choose Run->External Tools->External Tools Configurations.

Use the Add New icon to add a new program and give it the name "STLink Utility" , then browse to the STM32STLinkUtility.exe in the file system and click Apply.


Finally go to the Build tab and Deselect Build Before Launch to stop the project being built every time you run the STLink Utility


You now have easy access to the utility and can now run the STLink Utility at any time from the tool-bar icon shown below ..


Ok Right Click on the Template Project and select Build


**** Build of configuration Debug for project Template ****

**** Internal Builder is used for build               ****

.....


arm-none-eabi-g++ -T D:/stm32-discovery-projects/workspace/Template\startup_src\stm32_flash.ld -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd -Wl,-Map,linker.map -oTemplate startup_src\startup_stm32f10x_md_vl.o src\stm32f10x_it.o src\main.o src\STM32vldiscovery.o StdPeriph_Lib_V3.3.0\stm32f10x_wwdg.o StdPeriph_Lib_V3.3.0\stm32f10x_usart.o StdPeriph_Lib_V3.3.0\stm32f10x_tim.o StdPeriph_Lib_V3.3.0\stm32f10x_spi.o StdPeriph_Lib_V3.3.0\stm32f10x_sdio.o StdPeriph_Lib_V3.3.0\stm32f10x_rtc.o StdPeriph_Lib_V3.3.0\stm32f10x_rcc.o StdPeriph_Lib_V3.3.0\stm32f10x_pwr.o StdPeriph_Lib_V3.3.0\stm32f10x_iwdg.o StdPeriph_Lib_V3.3.0\stm32f10x_i2c.o StdPeriph_Lib_V3.3.0\stm32f10x_gpio.o StdPeriph_Lib_V3.3.0\stm32f10x_fsmc.o StdPeriph_Lib_V3.3.0\stm32f10x_flash.o StdPeriph_Lib_V3.3.0\stm32f10x_exti.o StdPeriph_Lib_V3.3.0\stm32f10x_dma.o StdPeriph_Lib_V3.3.0\stm32f10x_dbgmcu.o StdPeriph_Lib_V3.3.0\stm32f10x_dac.o StdPeriph_Lib_V3.3.0\stm32f10x_crc.o StdPeriph_Lib_V3.3.0\stm32f10x_cec.o StdPeriph_Lib_V3.3.0\stm32f10x_can.o StdPeriph_Lib_V3.3.0\stm32f10x_bkp.o StdPeriph_Lib_V3.3.0\stm32f10x_adc.o StdPeriph_Lib_V3.3.0\misc.o STM32F10x\system_stm32f10x.o CoreSupport\core_cm3.o
arm-none-eabi-objcopy Template Template.bin -O binary
Build complete for project Template
Time consumed: 464  ms. 


All going well there will be no errors , now use the tool-bar button above to run STLink.....



With File->Open File.. browse to <project-base>\workspace\Template\Debug and select Template.bin and open , you will be asked if you want to download to the device so say OK you will then get a Program Dialog make sure the address is 0x08000000 and select Program

Next Select Target-> MCU Core from the main menu bar.


Press System Reset and then Run to Execute your program , both the Green and the Blue lights should flash together.









The End
(almost)

Some final notes

From the Project menu deselect Project->Build Automatically then Clean the project , You will also need to delete the files Template.bin and linker.map from the Debug directory this will then give you a clean Template Project folder for copying.

The above only set-up the debug target , you will need to add the folder paths and other settings for the release target in a similar way.

To test out a STM32-Discovery demo project from the firmware library you will need to copy and then paste the Template project within Eclipse and give it the same name as the STM32-Discovery Demo project , then copy all the source files from the STM32 Discovery project source folder over the files in the new Eclipse project source folder. Build and your away.

Note that some of the demo projects will only work correctly from a hard reboot so read the readme.txt (yes:) , They will not work while the STLink is still connected so you will have to power cycle the STM32 Discovery board.

Have Fun


Comments