Programming ARM Cortex-M based Microcontrollers with Eclipse and GCC

Keywords: Embedded systems, Eclipse, Toolchain, GNU, make, GCC, ARM Cortex-M

In the previous tutorial we explained how to configure Eclipse, ARM GCC Toolchain, and OpenOCD for Programming ARM Cortex-M based Microcontrollers. While the previous tutorial was about development environment setup this tutorial is about actual programming the ARM Cortex-M based Microcontrollers.

1. New Project Steps:

1.1. Open eclipse (configured in previous tutorial) and click on New → Project → C Project. Click Next – Figure-1.

Figure-1: Eclipse – New C Project

1.2. Enter the name of the Project in our case STM_Blinky – Figure-2. Now based on GNU ARM Eclipse Plug-in we installed in the previous tutorial, we will have the following options to choose from:

  • Hello World ARM Cortex-M C/C++ Project: Choose this option for developing Project for any ARM Cortex-M based MCU. This is a generic option for ARM Cortex-M Series. If you have good knowledge of ARM Cortex-M Processors and GNU ARM assembly, you should choose this option. This option creates project for ARM Cortex-M CORE only with flexible configuration to extend the project for any Cortex-M based MCU. Anyhow though this is my favourite option, I will recommend to choose this option only if GNU ARM Eclipse Plug-in doesn’t provide template for your MCU (see bellow options).
  • Freescale Kinetis KLxx C/C++ Project: This Option provides pre-configured project template for Freescale Kinetis K and L series MCU based on ARM Cortex-M processors. Choose this option if your target MCU is Freescale Kinetis.
  • STM32Fxxx C/C++ Project: GNU ARM Eclipse Covers wide range of STM MCU based on ARM Cortex-M processors. This Option provides pre-configured project template for STM32F0xx – STM32F7xx series MCU. Choose this option if your target MCU blongs to any STM family shown in Figure-2.
Figure-2: Eclipse – Choosing ARM Cortex-M Profile

Note: For this tutorial we will be using STM32f4-Discovery board (STM32F407VG MCU). Thus we will choose STM32F4xx C/C++ Project – Figure-2.

1.3. Click Next and leave the settings as default except trace output option. If you don’t wanna route any I/O to and from MCU via debugger, just set it to None (no trace output). Let’s keep it for now. we may need it – Figure-3. Click Next

Figure-3: Eclipse – STM32F4xx Project

1.4. Leave folder structure as default as shown in Figure-4 and Click Next.

Figure-4: Eclipse – STM32F4xx Project folder structure

1.5. Check both Debug and Release check boxes and Next – Figure-5.

Figure-5: Eclipse – STM32F4xx Project Debug/Release Configuration

1.6. If you have completed the previous tutorial, the fields in Figure-6 will be automatically filled for you, if not all you have to do is to enter GNU ARM GCC toolchain executables directory as shown bellow.

Figure-6: Eclipse – STM32F4xx Project Toolchain Path

1.7. Finally Click Finish. Congratuations you have successfully created you first ARM GCC toolchain for STM32F407VG MCU.

Note: By default the GNU ARM Eclipse Plug-in will add some .c file for blinking LED. The Project is ready to be Build. We will build it in step-3.

2. Debug Configuration Steps:

Now that we have created eclipse project, its time to configure debugger for downloading and debugging code. Eclipse can be configured to have access to External tools (like OpenOCD) that are not part of eclipse itself via External Tools Configurations… as OpenOCD is not a part of eclipse, we will configure it via External Tools so that eclipse can access and run it. Following are the steps to configure OpenOCD in Eclipse.

2.1. To configure OpenOCD, click Run → External Tools → External Tools Configurations from the main menu – Figure-6. Click on  icon, In the Name Field enter appropriate name, in Location field enter the openocd.exe absolute path. In the Workspace Directory enter the script directory path inside OpenOCD installation directory.

In the Arguments directory enter the interface script (JimTcl) used by OpenOCD to communicate with the target Processors via the interface defined by this script. The OpenOCD installation, contains script folder that contains scripts for various boards and target SoCs. You can write your own one as well. OpenOCD already has script for the evaluation board we are using for this tutorial i.e. STM32F4-Discovery with the name stm32f4discovery.cfg inside Installation_Directory/script/board.

Figure-7: OpenOCD – Interface Configuration

2.2. In the Build tab uncheck the Build before launch check box – Figure-7.

Figure-8: OpenOCD – Disable build before launch

2.3. In the Common tab check the External Tools check box. This will add OpenOCD shortcut to standard toolbar. Click Apply and Close.

Figure-9: OpenOCD – Add shortcut to standard toolbar

2.4. Right click on the project and click Debug As → Debug Configurations… . Click on icon. In the Main tab the Project and C/C++ Application fields will be automatically filled for you. Nothing special here – Figure-10.

Figure-10: Project Debug Configurations – Binaries selection

2.5. In the Debugger tab uncheck the Start OpenOCD Locally checkbox. We will be starting OpenOCD manually once once. Checking this option will cause the debugger to take longer time to start and gain control of SoC.

Figure-11: Project Debug Configurations – Debugger

2.6. In the Common tab check the Debug checkbox.

Figure-12: Project Debug Configurations – Add config to shortcut

2.7. In the Startup tab leave everything at the default.

Figure-13: OpenOCD – Target commands (GUI based)

Now we are all set to build and debug the project. As we mentioned earlier by default code for LED blinky is added to the project.

3. Building the Project:

Right click on Project and click Build Project – Figure-14.

Figure-14: Eclipse Build Project

If build was successful, various sizes of exectable will be shown in the console window – Figure-15.

Figure-15: Eclipse Build Project – Section sizes

4. Debugging the Project:

Finally we have reached the point to download and debug our Blinky project. Connect target board (in our case STM32F4-Discovery) to host PC.

Click on External Tools Configuration icon from standard toolbar and click on OpenOCD_Config configuration that we created in Step-2.

Figure-16: OpenOCD Launch

OpenOCD will try to connect to target board (in our case STM32F4-Discovery) based on configuration script i.e. stm32f4discovery.cfg we provided in Step-2. On successful connection to target board, the OpenOCD will display the target debug informations like hardware has 6 breakpoints, 4 watchpoints – Figure-17.

Figure-17: OpenOCD successful connection to target SoC

Click on Debug icon from standard toolbar and click on Project debug configuration (STM_Blinky Debug in our case) we created in Step-2.

Figure-18: Project debug Launch

The OpenOCD will download code to target board and will enter debugging mode as shown in Figure-18.

Figure-19: Eclipse debug perspective

Use the Debug icons to debug the project. Click on Run icon in debug icons and you should see LED blinking on STM32F4-Discovery board.

Video Demonstration:

Click the full screen button for more clear view.


[1] – Mastering Embedded Linux Programming by Chris Simmonds

[2] – LLVM Project

[2] – GCC ARM Options

4,641 thoughts on “Programming ARM Cortex-M based Microcontrollers with Eclipse and GCC”