31 January 2014

Easy Development for STM32 Devices without an IDE

I've posted it before finishing as an easy way to grab the links if you already know what you're doing

Sometimes IDEs can drive you mad. Or maybe you happen to have three of them installed from separate device vendors, each resulting in a separate installation of Eclipse.. grrr! Or sometimes... you just want to go back to basics!


"But I don't mind using an IDE" - The real point of this tutorial is to show how an IDE runs behind the scenes. All that console text you see flying past when you hit Build is full of useful information that beginners take for granted. It might all look like witchcraft if you're new, but it's must more straightforward than it seems. In fact if you get frustrated with these large, clunky IDE installations, you'll love this tutorial!
If you've ever programmed in plain C, you likely created a simple HelloWorld PC application. The compiler you probably used (even if you weren't aware of it) was GCC or the GNU C compiler (though there are also others). Assuming the GCC compiler was installed correctly on your PC, you could simply go into a command prompt/console and type: 

gcc main.c -o MyApp

This would compile your main.c program into an executable called MyApp. Similarly, we can apply this principal to building STM32 projects with GCC (granted with a little more detail).


Firstly you need a way of writing your code, and I don't mean using notepad! A while back I came across a rather nice code editor. Sublime Text 2 is a lightweight editor designed with many useful coding features similar to EMACS. It also has a large user base with an ever growing number of addons. It can also be set up to run build scripts with a push of a button, we will use this to drive our GCC toolchain, debugger and ST-link utility. Of course you can use any editor you like, the steps related to the actual building and debugging won't be any different.

Once you've found a text editor, you'll need a way of compiling code for an STM32 device. As these chips use ARM cores, we must use a version of GCC specifically suited for ARM. You must download and install the ARM Embedded GCC Toolchain for your specific platform.

Next you'll need a means of connecting to your target platform. The STM32F4 Discovery Board is a cheap and cheerful ARM Cortex M4 based platform that is cheaper than an Arduino! Once you get used to it, it's almost as straightforward to code for as well. Go ahead and grab the ST Link v2 software from the ST website and install it. You should find a GUI application that lets you connect to the board and upload firmware. We'll however use the command line application (also included) to drive the entire thing from a couple lines in a command prompt window.

Initially you need to check your PCs environment variables are correctly set up. These are what the command prompt/console recognises as valid commands. For windows, go into: Control Panel > System > Advanced System Settings > Advanced. Now under System variables find the PATH entry. Hit Edit... and at the end of the text, add ;C:\Program Files\ST-Linkv2\ (or wherever your STlink application is installed). Also check the location for the ARM GCC toolchain is in PATH as well. Don't forget that there should be a semicolon between all the entries.

You can check the tools work by going into a command prompt and typing:

arm-none-eabi-gcc --version


You should see the version of the installed GCC come up. Also check the ST-Link command line application by typing:


You should see some colorful instructions pop up.


Now you should have everything you need to write, compile and upload a bit of code to your STM32 device (We'll do this soon). What if we want to debug it? Well, GCC features a debugger called GDB. We will use GDB (GNU debugger) to connect to the running application and have real time control over the device. Currently, GDB doesn't know how to run the code on a target other than its host machine (i.e. the PC). For this to work, we'll need a server that bridges the GNU debugger to the STM32 device. Go ahead and download the server from here.

Finally go ahead and download my Template Code with build script (Windows). Now we have everything we need to get started.




No comments:

Post a Comment