Code::Blocks and STM32F0 (CortexM0) setup

Thank you Hertaville

I set up this tutorial using a lot of resources I found at Hertaville. Thanks, thanks, thanks!

1 – get the GCC compiler

Download toolchain from and add directory with ‘arm-none-eabi-gcc’ and others to path
Linux: get the TAR with the binaries, and extract somewhere. I put them in Documents. Yes, I know that that is not a sensible location, you could do otherwise if you’d like to. I changed the path to point to the binaries.

gedit ~/.bashrc

For me: PATH="/home/victor/Documents/gcc-arm-none-eabi-4_7-2012q4/bin:/usr/local/bin:/usr/bin:/bin:/usr/games:/opt/msp430-gcc-4.4.3/bin"

Under Windows, just run the installer, everything will be fine. At the end of the installer, let the PATH be adapted (option box selected).
2 – get OpenOCD, part of the debugging solution
Go to the OpenOCD site and download the sources. For windows: there’s a binary installer available somewhere, check Hertavilles site.
Linux: unpack,

sudo make install

3 – install Code::Blocks if you hadn’t already
Download Code::Blocks from their website, installation instructions are here.
For linux (my distro refuses to update to 11.20) you’ll have to install the archives that BerliOS provides, windows users can easily install the binaries.

4 – Download the important source files
OK; we’re getting somewhere!
From the ST website download this, and unpack in your folder-of-choice. For me: /home/victor/Dropbox/ARM/ST

From Hertaville, take the ‘toggleiom0′, I unzipped it in /home/victor/Dropbox/ARM/workspace/iotogglem0/:

Configuring Code::Blocks for compilation

  • Start Code::Blocks, create a new empty project in the iotogglem0 folder
    • Select ‘empty Project’
    • Name: iotogglem0
    • Remove extra ‘iotogglem0′ in ‘directory’ and ‘resulting filename’.

      Creation of empty project

      Creation of empty project

    • Next
    • Compiler: GNU ARM C, and leave everything else default, Finish
    • You’ll get some warnings from Code::Blocks, we’re going to fix those in a minute
  • Right-Click on the bold ‘iotogglem0′ in the left pane, and choose ‘Add files recursively’
  • Go one folder up, and select the iotogglem0 folder

    Add the files from Hertaville's project

    Add the files from Hertaville’s project

  • OK,OK
  • In the same way add the files from the ‘Libraries’ folders from the Standard Peripheral Libraries:

    Add files from Std_Peripheral_Library

    Add files from Std_Peripheral_Library

  • Close,OK
  • Do the same with the files in the …/ARM/ST/STM32F0xx_StdPeriph_Lib_V1.0.0/Libraries/CMSIS folder.

Change compiler settings

  • Go to project-> build options
  • Select ‘iotogglem0′ in the left pane
  • Click the tab ‘other options’
  • Add the following line:-mcpu=cortex-m0 -gdwarf-2 -mthumb -fomit-frame-pointer -Wall -Wstrict-prototypes -fverbose-asm -Os
  • At the tab ‘#defines’ add these lines:
  • at the tab linker settings, add this in ‘other options’:
    -mthumb -nostartfiles -Tstm32f0_linker.ld -Wl,,--cref,--no-warn-mismatch
  • Search directories, compiler:

    Compiler search directories, all as relative path

    Compiler search directories, all as relative path

  • Search directories, linker: add linker directory (~/Dropbox/ARM/workspace/iotogglem0/linker in my case)
  • Pre/post build steps: add post-build steps:arm-none-eabi-size $(TARGET_OUTPUT_FILE)
    arm-none-eabi-objcopy $(TARGET_OUTPUT_FILE) -O ihex $(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).hex
  • OK

Change compiler command

  • Settings->Compiler
  • Change drop-down to GNU ARM
  • Toolchain executables tab
  • Compiler settings

    Compiler settings

Enable debugging from Code::Blocks

  • From the openocd source directory copy the file stm32f0discovery.cfg (in ./tcl/board/) to your output directory (~/Dropbox/ARM/workspace/iotogglem0/bin/Debug in my case)
  • In Code::Blocks, change the settings for debugging by making an ARM-GDB setting:
    • Settings->Debugger: turn on ‘full-debug-log’ to get more info of what is happening, if that’s what you like (I do).
    • In the left pane, select GDB/CDB Debugger, and press the button ‘Create Config’. Type in the new config name, I chose ‘ARM OpenOCD’
    • Click ‘ARM OpenOCD’ in the left pane, change settings as shown below
      Debugger settings, Code::Blocks

      Debugger settings, Code::Blocks

      Changed items are: executable, ‘do not run’

    • Close
  • Click ‘Project->Properties’
  • Go to the Debugger tab
  • Choose ”, and apply presets as below:

    Debugger connection tab

    Debugger connection tab

  • The following options are set to load the correct output file in the microcontroller:

    GDB command settings

    GDB command settings

  • actual settings should be:
    monitor halt
    load ./bin/Debug/iotogglem0
    file ./bin/Debug/iotogglem0
    monitor sleep 1000
    monitor reset
  • OK
  • Choose Debug->Active Debuggers->GDB/CDB Debugger: ARM OpenOCD
  • Open a terminal and go to your output directory (for me: /home/victor/Dropbox/ARM/workspace/iotogglem0/bin/Debug) and type openocd -f stm32f0discovery.cfg
  • Start debugging by pressing the red arrow (Run / continue) in the debugging toolbar

9 thoughts on “Code::Blocks and STM32F0 (CortexM0) setup

  1. vps says:

    I really love your blog. You write about very interesting things. Thanks for all your tips and information.

  2. Dabbede says:

    Dear Van De Dam,

    thank you for this nice tutorial. Unfortunately I am a newbie with ARM compiling and Code::Blocks too.

    I’ve tried to follow your instructions, but when I compile I receive the following message “../../ST/STM32F0xx_StdPeriph_Lib_V1.0.0/Libraries/CMSIS/Device/ST/STM32F0xx/Source/Templates/arm/startup_stm32f0xx.s|1|Error: junk at end of line, first unrecognized character is `*’| ”
    There ara several of these errors! I don’t know how to avoid this!

    Any help will be very appreciated :-)

    A side note: in your tutorial, you wrote nothing under “at the tab linker settings:”… is this normal?

    • EVS says:


      I have the same problem. lots of errors: -Error: junk at end of line, first unrecognized character is `*’|

      Can you please tell how to solve this.


  3. Benzol says:

    I have a problem. Which are the linker libraries? It’s not stipulated. => “Search directories, linker: add linker directory”. Please clarify. Thanks.


    • admin says:

      Hello Benzol,
      Thanks for the comment. I’ve updated it, it’s the linker directory in the iotogglem0 folder (~/Dropbox/ARM/workspace/iotogglem0/linker in my case).

  4. ra says:

    We encountered some problems related to divisions after using this tutorial to set up the toolchain and codeblocks.
    The execution got stuck in the HardFault_Handler.
    “You must also include -mcpu=cortex-m0 in your linker command line, to let it link libraris for cortex-m0.”

    In our case, in Codeblocks
    -mthumb -mcpu=cortex-m0 -nostartfiles -Tstm32f0_linker.ld -Wl,,–cref,–no-warn-mismatch
    instead of:
    -mthumb -nostartfiles -Tstm32f0_linker.ld -Wl,,–cref,–no-warn-mismatch

  5. ra says:

    Removed the iar and arm folder from ../../STM32F0xx_StdPeriph_Lib_V1.3.1/Libraries/CMSIS/Device/ST/STM32F0xx/Source/Templates as they use another assembly syntax.

    now I get:
    ../../STM32F0xx_StdPeriph_Lib_V1.3.1/Libraries/CMSIS/Device/ST/STM32F0xx/Source/Templates/system_stm32f0xx.c|99|fatal error: stm32f0xx.h: No such file or directory|

    stm32f0xx.h is in the source file tree.
    Any idea how to make the compiler see it?

  6. ra says:

    Ok, adding the paths explicitly helped.
    Then some redundant .s files had to be removed as well.
    Other toolchains I used in the past managed to discover which .s files to use for a specific processor-core. But this is not a problem.

    Thanks again for the tutorial :-)

  7. admin says:

    Thanks for finding the error yourself! Hope this also helps others!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>