This is the first part of a three part tutorial on setting up a free and fully functional GCC + Eclipse + OpenOCD (ST-LINK V/2) GDBServer environments for use with the STM32F0Discovery board. Links to the second and third parts of this tutorial are provided below:
- Part 2 – Setting up the Eclipse IDE
- Part 3 – Setting up Debugging with the Eclipse IDE (OpenOCD GDBServer)
In this blog entry I will provide instructions on how to install an arm-toolchain on a Windows 7 machine and how to use it to compile a simple blinking LED project on the STM32F0Discoveryboard from the command line.
There are a variety of arm-toolchains out there. These includearm DevKit pro, Yagarto, Mentor Graphics CodeSourcery Lite, and the gcc-arm toolchain. Of these toolchains, the two most popular ones are the codesourcery Lite (owned by mentor Graphics) and the gcc-arm toolchain. Traditionally I would use the Codesourcery Lite toolchain. However that toolchain does not provide "hardfp" (hardware floating point) support. So If I choose to use it with say the stm32F4Discovery board in the future, I will not be able to take full advantage of the floating point unit (FPU) on the stm32F4 microcontroller on board. The gcc-arm toolchain on the other hand has hardfp support and can be used with all Cortex-M0,M3 and M4 parts. This universality is why I will choose it even though I will most likely only focus on Cortex-M0 cores. And so for the purpose of this tutorial, I will be using the gcc-arm toolchain. By the way this is the same toolchain use in the Coocox IDE. Here are the things that you will need to download:
- TheGCC arm toolchain
- GNU Make for windows. You will need both the binaries and the dependencies
- TheSTM32F0Discovery Firmware package
- ST-LINK Utility to be used as a programmer
- The STM32F0Discovery Board User Manual
- The following ready to go project.
Other nice to haves are the
- First download and install the gcc-arm toolchain (.exe file) https://launchpad.net/gcc-arm-embedded. The name of the .exe file should look something like "gcc-arm-none-eabi-X_X-XXXXX... .exe"
- The installer tool is pretty intuitive. Make sure to accept the license agreement and then simply keep pressing "next" until you see "Install Wizard Complete".
- At this point you'll see three check-boxes The first two are already selected. MAKE SURE YOU SELECT THE THIRD ONE!!!! The first one allows you to see the readme file. The second automatically opens the command prompt window (DOS box) pointing to the directory containing the toolchain binaries. The last option allows you to access the compiler binaries from any directory on your computer. This allows you to call the compiler binaries from your project directory without having to specify the entire directory of the compiler binaries.
- Click finish, then close the command prompt and readme windows if they opened up.
- Now open a new command prompt window (go to Start->Accessories-> Command prompt) and enter the following command: "arm-none-eabi-gcc -v"
- you should see the following:
- The command "arm-none-eabi-gcc" invokes the arm-gcc c compiler. Whereas the command "arm-none-eabi-gcc -v" displays the version of the arm-gcc compiler and quite a bit of info about it. Getting this output implies that the compiler was installed successfully. Notice how by adding the binary directory to the "path" environment variable enables us to access the compiler binaries from anywhere on our C (Primary) drive.
- The next step is to install GNU make. We will need gnu make to be able to run makefiles. Makefiles are scripts that allow us to invoke the gcc compiler efficiently. It is very handy for building multiple source projects. Download the "GNU make for windows" binaries and the dependencies zip files from http://gnuwin32.sourceforge.net/packages/make.htm . We unzip them and locate the "make.exe" file from the binaries zip file; which should be in the bin directory of that zip file, and the "libiconv2.dll" and "libintl3.dll" files from the dependencies zip file which you will also find under the bin directory in that zipped file. Once you locate "make.exe", "libiconv2.dll" and "libintl3.dll", copy them into the same directory containing the compiler binaries. Which should be look something like: "C:\Program Files (x86)\GNU Tools ARM Embedded4.6 2012q2\bin" This way the make command can also be invoked from anywhere on your C drive.
- I have to warn you that if you have installed other programs such as WINAVR that add their own make utility to the path variable, it is very likely that a conflict may arise between that application's make utility and this make utility. A workaround is to change the name of either the WINAVR make utility or changing the name of this make utility to something like GNUmake e.t.c. Better yet, uninstall other instances of make if you can.
- To test this, open another command prompt window and type the command "make -v". You should see the following output:
- This should indicate that the make command is operational!!! and now we are ready to compile code!!!!
- But before we do this we will first install the ST Link Utility. Installation is straight forward. Just run the installer.
- Also make sure you create a directory C:\Development. In this directory unzip the contents of the "stm32f0discovery_fw.zip" directory. This should create the following directory "C:\Development\STM32F0-Discovery_FW_V1.0.0" which will contain the peripheral library and the stm32F0Discovery board specific code that we will need to run the our example.
- The next step is to create a "Workspace" directory inside the "C:\Development" such that the full path of the workspace directory ought to be "C:\Development\Workspace".
- unzip the"iotogglem0.zip" file and copy the "iotogglem0" folder contained within it into the "C:\Development\Workspace" such that the path of the iotogglem0 folder is "C:\Development\Workspace\iotogglem0"
- Note that its imperative to use these particular names, as the makefile that's included in the "iotogglem0" project relies on them.
- now open a command prompt window and go to the directory "C:\Development\Workspace\iotogglem0" using the cd command..i.e. "c:> cd C:\Development\Workspace\iotogglem0" and then type "make". If everything is in good order, then the project should successfully compile and you should get the following output:
- Note that when we type "make" the make command looks for a file called "makefile" and executes its contents thereby initiating the compilation and build process. To clean the files created by the build process, we can simply type "make clean".
- The last couple of lines in the build output (figure 4) tell us that our program will require 2900 bytes of flash memory (text) and 36 bytes of RAM (data). But I will discuss these in more detail in a future entry.
- Also the build process creates three output files in the "iotogglem0" project directory; iotogglem0.hex, iotogglem0.bin and iotogglem0.elf. Either of the first two are release based images that ought to be downloaded onto the chip when programming it. The .elf file is typically used for debugging. It contains an identical image as the one in the .bin/.hex files in addition to some debug information required by the debugger.
- To download the code to the STM32F0Discovery board, first attach the board to your computer via a USB cable. Make sure that you have already installed the STLINK utility before performing this step.
- Open the STLINK Utility program and update the firmware if necessary via the ST-LINK menu. Then click on the connect option under the target menu.
- Click on the "Program & Verify" option under the target menu. This should open an open File Dialog. Browse until you get to the directory "C:\Development\Workspace\iotogglem0" which contains the .hex/.bin files and select either one. Then click on "program".
- Now hit the reset button on the board and both LEDs on the board will blink!
- Congratulations! you just built your first program for the STM32F0Discovery Board!!!!