[STM32] Set up tools and library

Previous post I told you how I chose to learn STM32 next. This post will be about how to set up tools like C Compiler and library, ST-LINK software.

1. GCC for ARM

Download this free C compiler for ARM here GCC-arm-embedded
Currently the latest version is 5-2016-q1-update. Choose a packet that compatible with your system, either Windows or Linux.
Extract it to a folder. In my case, it is in ~/STM32/gcc-arm-none-eabi-5_3-2016q1/bin
Next step is add GCC binary folder into your PATH environment. I’m using Linux Mint.

$ export PATH=$PATH:~/STM32/gcc-arm-none-eabi-5_3-2016q1/bin

Next install automake for the MAKE files to work

$ sudo apt-get install automake

To check if the compiler is ‘installed’ correctly, let’s see its version

$ arm-none-eabi-gcc --version
 arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 5.3.1 20160307 (release) [ARM/embedded-5-branch revision 234589]
 Copyright (C) 2015 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions. There is NO

That means GCC is good to go.

2. STLINK and USB permission

Plug your STM32F407 Discovery board in one of your PC USB ports. PWR LED should lit up. And normally COM LED should lit up but not blinking because there’s no communication yet.

Check if PC recognize the board (actually, the STLINK V2 part):

$ lsusb | grep ST-LINK
 Bus 002 Device 003: ID 0483:3748 STMicroelectronics ST-LINK/V2

This mean the PC recognize the ST-LINK with VendorID 0483 and ProductID 3748
On Windows, you should be able to communicate with ST-LINK now.
On Linux, you have to set its permission:

Create a new rules file /etc/udev/rules/45-usb-stlink-v2.rules and add the following line:

SUBSYSTEM==”usb”, ATTR{idVendor}==”0483″, ATTR{idProduct}==”3748″, MODE=”0666″

Reboot or restart udev

$ sudo service udev restart

Now you should have ST-LINK board up and running. You still need to have software to run it though. On Windows you should be fine because ST has a very capable software used for ST-LINK. But ST has not public its protocol for the open source guru yet. Texane has reverse engineer this protocol and made it possible for us Linux nerd to use.
Install these packages:

$ sudo apt-get install autoconf pkg-config libusb-1.0 git

(Most Linux system now already has libusb-1.0 installed. And if you’re a nerd, you probably have git installed.)

Infact you don’t have to install git if you have no intend to use it later. You can go to texane’s github folder and download it to your PC, extract it to your home folder: https://github.com/texane/stlink
If you have git, issue these:

$ cd ~
$ git clone https://github.com/texane/stlink.git

Next build stlink with these:

$ cd ~/stlink
$ ./autogen.sh
$ ./configure
$ make

You now should have stlink ready to work. You should read stlink README file. It contains a lot of good information on how to use stlink to program the chip, or gdb server to debug it.

3. Peripheral Library

A Peripheral Library is something you will definitely need to program STM32. STM32F4 has too many functions and registers and way to combine those register to work. Yes you can program bit by bit, register by register to get a certain module to work. But a library will make the code self-explainable, easy for human to read, reusable and portable (from STM32F407 to STM32F427 for example).
ST has HAL (Hardware Abstraction Layer) but it’s way too complicated for me. Maybe I’ll try figure it out later.
I chose libopencm3 at www.libopencm3.org. I quote from their site:

The libopencm3 project (previously known as libopenstm32) aims to create a free/libre/open-source (LGPL v3, or later) firmware library for various ARM Cortex-M0(+)/M3/M4 microcontrollers, including ST STM32, Ti Tiva and Stellaris, NXP LPC 11xx, 13xx, 15xx, 17xx parts, Atmel SAM3, Energy Micro EFM32 and others.

This project has started on 2009 and been developed constantly for new chips to come. I like it for its clarity. I guess it’s a matter of taste in code.
Again, you can download the library from their site, extract it to your home folder. Or if you have git installed, you can do these:

$ git clone git://github.com/libopencm3/libopencm3.git

To build libopencm3:

$ cd libopencm3
$ make

They also have example projects for you to get started.

$ git clone https://github.com/libopencm3/libopencm3-examples.git
$ cd libopencm3-examples
$ make

This should build the examples.

Let’s blink some LEDs. Go check the main code in this example:

$ cd ~/STM32/libopencm3-examples/examples/stm32/f4/stm32f4-discovery/fancyblink
$ make hex

This should make fancyblink.hex. To flash it to the board, issue:

$ make fancyblink.stlink-flash

Press reset button on the board (the black one), Red LED and Green LED should be blinking alternatively. That means all system is good.

To understand what this libopencm3 does and use it correctly, please read the documents online here: http://libopencm3.github.io/docs/latest/html/
You can build these documents to read offline, in html or in pdf. The direction on how to do that has been covered in their README file. The library is pretty complicated at first glance. But to me, it still is better than ST HAL (or CUBE).

Everything’s now ready for your coding…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s