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

13 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. jaromir says:

    I set up CB with ARM GNU tools per your tutorial and it works great. I just added “monitor halt” after “monitor reset” line in gdb commands list. Without it, CB debugger thought my target is halted after debugger started (offering continue button, as well as single step buttons – but no “break debugger” button), however it was actaully running. I had to click “continue” to get access to “break debugger” button.
    Now, my target is properly stopped after loading.

    Now, I’m curious, how can I display the values of hardware registers in watch window? Let’s say I want to observe state of GPIOA->IDR register. Is it possible?

  6. lcc says:

    well,I got something very very…different
    i haven’t use codeblocks for a long time,so ,i can now built and flash the program in command line
    but just couldn’t do it via cb
    yeah,i must admire that i just get the example from github.
    but I just didn’t know how to do something in the cb

    basically,i follow your step to set and build a project in cb
    but,it didn’t success
    something different
    1.i need a .bin file,not hex,and i don’t understand why it just build it to a hex
    2.then i found that it seems that didnt use your command,so i use the pre built command.
    but,it just tells me that ,,well,it’s hard for me to say,i think i should give you the build result
    yeah,the last sentence is ,i work it under ubuntu 12.04,and my cb is 10.05
    i didn’t found someway to get the newest 13.10

    Running target pre-build steps
    arm-none-eabi-size bin/Debug/CB
    arm-none-eabi-size: ‘bin/Debug/CB’: No such file
    Process terminated with status 1 (0 minutes, 0 seconds)
    0 errors, 0 warnings

    i,er,i just so upset as i try all the day,and the,,as that before i use almost half a year to get success to do this things under linux….
    and i search the internet many times,but just….if you can help,please.

  7. marius says:

    I have STlinkV2 which uses a configuration script file.
    I am using now the crappy impossible eclipse, and in
    debugger settings I am setting local configuration for
    openocd. Where do I configure this in CB.
    Thank you

  8. ra says:

    Thanks for the nice tutorial.
    I get the same error when trying to compile the project. This means at the last step of the tutorial.
    Additionally in the Code::Blocks log: “ToDoList: Warning: No to-do types or comment symbols selected to search for, nothing to do.”

    It helped to replace the ‘;’ assembly comment symbol in the .s file by /** and **/. However, then the compiler complains about the first line of code:
    ../../STM32F0xx_StdPeriph_Lib_V1.3.1/Libraries/CMSIS/Device/ST/STM32F0xx/Source/Templates/arm/startup_stm32f030.S:41: Error: bad instruction `stack_size EQU 0x00000400′

    It seems that the compiler? does not recognize the file as assembly.

    Do you have any idea how to fix this?
    Many thanks for your contributions :-)

  9. 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?

  10. 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 :-)

  11. 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 *