Tuesday 26 July 2016

Getting started on STM32F10x with *buntu Linux

I've struggled every time I need to do an installation for developing STM32 microcontrollers under any *buntu Linux distribution. There are quite a few websites with information, at my first attempt I downloaded and compiled everything per hand. But if you just want to get started as quickly as possible and spend your time getting that LED to blink and not looking for compilers and libraries and installing program after program, I might be able to help. I'm installing and setting up a simple yet good enough development environment on a Linux Xubuntu 14.04, and it has worked on higher versions too.

My system runs using the arm-none-eabi GCC compiler for bare metal, that is, you program everything per hand. printf won't work this way, I haven't bothered to install the newlib and generating the correct library. To be honest, I'm not even sure how that works.

These instructions will allow you to program pretty much every STM32F10x controller around, and later on, we can proceed to the STM32F2xx family, but let's not get ahead of ourselves.



Another tool you might want to consider is the ST-Link V2 programming tool. I started programming using the serial port (more on that later) but as the programs got larger it got painfully slow. The ST-Link V2 is connected to your PC via USB, and there are a few Linux programs that support it.


When I started writing this, I was using Code::Blocks as my development environment. Since I'm using makefiles, I find Code:Blocks as well as Eclipse a bit bloated. So now I'm testing Geany, and, unless anything changes, it's the development suite I will write about.

Finally, you'll need an appropriate STM32F1xx development board, I guess the Discovery board should do. If you own a Discovery board, you don't need the external programmer, which is and advantage. Just plug it via USB and off you go. Olimex does some nice kits as well. None of them fit my purposes though, so I built my own.

Downloading and preparing


Ok, first of all, open your terminal and install the compiler:

sudo apt-get install gcc-arm-none-eabi

Install the environment (Geany)

sudo apt-get install geany 

Now we'll download the Standard Peripheral Library from the ST website. While I don't use it extensively, it provides a few very handy files and startup functions. There's a few header files that you don't want to miss either. One way of downloading the library is:
  • Go to Google and search "STM32F1 standard peripheral library". You'll come across a link to the STM32 website and something like "STSW-STM32054". Follow the link, register to download, and store the .zip file somewhere.
The other way... download directly from here.

While it's downloading, you might set up a folder where you plan to store all your projects. Mine's located at the home folder, called ARMProjects. Unzip the newly downloaded lib and copy the complete STM32F10x_StdPeriph_Lib_V3.x.x  folder under ARMProjects. For our next projects, we'll reference this folder using relative paths.

Download the following files here (zip file), and copy them into ARMProjects/stm32f107.
  • Makefile.common - A common makefile template for all projects
  • Makefile - This makefile is specific for every project
  • startup.c - A startup file for the microcontroller. It contains the interrupt vectors table as well as the startup code after a reset
  • main.c - Your main program (a very simple blinky in this case)
  • linker.ld - The linker file
  • stm32f107.geany - Template file for Geany project. More on that a bit further down
I'll be very honest, I downloaded all files from different sources and tweaked them until they worked for me. They are far from being the optimal files, but they work. If anyone feels that I've breached some copyright issues, please contact me and I'll remove the files in question.

After unzipping the file, you should have then 3 folders (inc, src, obj) and 4 files (linker.ld, Makefile.common, Makefile, stm32f107.geany). The source files are in the src folder.

Interlude One


At this point, you're ready to compile. You could start using any code editor you like and you'll generate main.elf, main.hex and main.bin file. Just run the command

make

on this folder and it should compile and link nicely. Want to delete all files? Run

make clean

and you're done.

Note that you can modify Makefile and Makefile.common as you wish, as long as you include all necessary folder in case you want to start larger projects. You can also put source and header files in different subfolders, but remember to change the Makefile accordingly.

Install the programming tools


You can load the firmware into your microcontroller using the internal USB bootloader (won't go into that now), a serial port (RS232), your ST-Link V2 progamming adapter or directly into a discovery board.

  • Using the serial port
You will need at least a level converter (as the Max232) between your microcontroller and your PC, and power the microcontroller separately. I have one RS232 interface on my board, and used it quite extensively before I bought the ST-Link V2 programmer. The software to load the firmware is stm32flash, it's a terminal software, pretty easy to use. you'll have to compile it yourself. Not one of the very complex programs thanks goodness, a simple

make

will do. Either copy the newly generated executable stm32flash to /usr/bin or create a symlink to it, and you're ready to upload your microcontroller with main.bin.


  • Using the ST-Link V2 programming adapter or integrated discovery board programmer
*buntu recognizes the programming adapter natively, but we need to create a set of USB rules (udev rules) so that you can use the adapter as a regular user and not as root. This file (49-stlinkv2.rules) is compatible with both the external programmer or the discovery board. Copy this file to /etc/udev/rules.d/ and restart the udev service

sudo service udev restart

You still need a program to upload the firmware to the controller, and QStlink2 was at the beginning my tool of choice. Very easy to use, pretty much self explanatory, but you'll have to build it on your own (At the time of this writing I had trouble installing QStlink2 from the repositores, you might want to try that first. There's instructions on how to do that on the QStlink2 website). There are some dependencies you'll have to install:

sudo apt-get install libusb-1.0-0 libusb-1.0-0-dev qt4-qmake qt4-dev-tools libqt4-gui libqt4-xml

Download the code for QStlink2 from here. (As of May 10th 2016, I also had to download the QtUsb folder separately, since the folder inside the zip file was empty. Just copy the contents from QtUsb.zip into the empty QStlink2-master/QtUsb). Unpack it in your preferred location an run following commands from the QStlink2-master folder:

qmake-qt4
make
sudo make install

QSstink2 is installed, now you can upload the main.bin file into the controller.

Alternative: OpenOCD


As an alternative, you could use OpenOCD to flash the controller, and it's an alternative I've been favouring more of late. OpenOCD is pretty easy to compile as well, and since I've had trouble installing it from the repositories, well, why not, let's download and compile the latest version.

You can get the latest version from here.

Unzip it in a destination of your choice, and navigate into it with the terminal. I had some trouble getting OpenOCD to work, so, a good idea is to read the installation instructions located here. Or you can do as I did, which is make sure that the following three packages were installed on my system:

sudo apt-get install make
sudo apt-get install libtool
sudo apt-get pkg-config
sudo apt-get install libusb-1.0-0 libusb-1.0-0-dev

After all this has been checked, enter the following three commands

./configure
make
sudo make install

Now it's installed. Let's see how we can use it for simply programming our controller. OpenOCD needs to know which interface you are using to program the controller as well as the target controller. Create an empty file named openocd.cfg on your home folder (or any folder where you'd run openOCD from) and add the following two lines

# Include config files found under /scripts.
source [find interface/stlink-v2.cfg]
source [find target/stm32f1x.cfg]

(I always leave a blank line at the end and that seems to work fine).

Navigate to the folder where your main.bin file is located. For more information on getting started using OpenOCD, have a look at this useful link.

Use Geany as development environment


We don't necessarily need this, we have our makefile and we have our programmer, but as projects get larger and larger, it's nice to have everything tidied up in one place. Plus, if you use Geany, setting up a project takes very little. On the zip you downloaded previously you'll find the template I created to make my life easier as stm32f107.geany.

I used to use Eclipse and Code::Blocks for developing, but I found them a bit heavy for the kind of development I'm doing. They are great tools, and feel free to play around with them, but both got a bit too complex for what I needed. Geany is smaller, but it takes some time to get around to use it. Still, let's give it a go.


Open the template project using "Project -> Open". That should open two files, main.c and startup.c. To build the complete project, go under "Build -> Make" or simply click "Shift + F9". The build messages should appear in a log below the main window. You can customize the build commands by selecting "Build -> Set Build Commands", as I did, to match your makefile

Final thoughts


I think that covers it more or less. I won't go into more detail, the information here should get you started in an afternoon or a long evening I suppose. I guess if I remember something that I've missed I'll update it soon-ish, but it took me a while to write this... so... don't hold your breath. I'll still update it with information on how to do the same for the STM32F2xx series, but it's basically the same, except the linker file and the lib have to be changed, other than that, it stays consistent.

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Note: only a member of this blog may post a comment.