Introduction: Start Developing STM32 on Linux

About: Hi, I'm Matej and I'm a student of Electrical Engineering. I have been working with electronics for more than 7 years and have lots of knowledge, projects and lots to share along my path, so this page is just …

In this Instructable, I'm going to show you how easy it is to start developing STM32 programs on Linux. I started using Linux as my main machine 2 years ago and haven't been let down. Everything works faster and better than windows. Of course is less convenient from time to time, but it forces you to learn things deeper so you can use it.

Anyway, in this instructable, part of a series I'm starting here AND on youtube is about how to start it. Please make sure to watch my youtube video as well where I explain everything as one segment and you can code along side me.

In this series I will show you how you can develop using only a text editor like notepad, SublimeText or Atom, so you don't need any proprietary software or IDE. This is as bare bones as it gets and it is surprisingly easy.

Step 1: Download Everything That You Need

You need to download three parts for everything to work:

Compiler is the main piece of software that compiles our C code with all other library files into machine language that our stm32 controller can understand. Download the latest pre-compiled version of this compiler.

Folder containing STM32 firmware is the one that holds all the startup and core files needed for the operation of the main processor. We will be using Standard Peripheral Library that has been surpassed by HAL. I like StPeriphLibrary more as companies that work on this processors use them because it is robust and older and supported. It is also more rugged. It doesn't cut the work that you have to do to initialize a peripheral or turn on an LED but, so it forces you to learn how these processors work. With that you have more knowledge of inner workings thus making sense of programming any task.

Last piece of software to download is st-link utility. It is maintained on github and is used to transfer compiled binary files to the processor using stlink IC on the board serving as a SWD / JTAG programmer/debugger.

Also I have provided a sample project folder that I talk about later and you can download it. It is inside the first VIDEO1 folder.

Step 2: Installing Software

After you have downloaded all the files I suggest you put them inside a common folder as they are all used together for the same purpose. I put all the folders inside a folder called "Embedded" in my HOME directory.

We will start with with the easiest, the STM32 libraries. The folder that you have downloaded can be just left there. Just make sure to dig around to see where the appropriate files are stored. Therefore you can change and edit the main MakeFile so it will work with your platform.

Second easiest is the compiler. You also don't need to do anything to it, but we will make the compiler a globally accessible function so you can call the compiler from any folder regardless of the path. All the steps can be done in terminal or in gui, but I like to use terminal as when you get experienced it gets faster and easier and I encourage you to use terminal more, if you are afraid of it. Here are the steps:

  1. Go into your home folder "/home/YOURUSERNAME/" or "~/" or type cd into terminal
  2. open file ".bashrc" by typing: nano .bashrc
  3. scroll down to the end of the file and add this line:
    export PATH=$PATH:~/Embedded/gcc-arm-none-eabi-8-2018-q4/bin
  4. exit by saving: CTRL+X, click Y, ENTER
  5. run command: source .bashrc to refresh terminal sources
  6. check if everything is working by typing: arm-none-eabi-gcc --version, it should display the latest version of the compiler

To install st-link, extract the archive that you have downloaded into the Embedded folder. Then follow these steps:

  1. Run: make
  2. Go into folder "build/Release": cd build/Release
  3. Type ls and you will see two executables called "st-flash" and "st-util"
  4. Move those two into the parent directory stlink: mv st-flash st-util ../../
  5. You can, if you would like to use these two functions globally edit ".bashrc" file again by adding:

    export PATH=$PATH:~/Embedded/stlink/

That's all! You have everything you need. Now go grab yourself your favourite text editor. Use just a standard one, a smarter one like SublimeText or Atom, it is what I use.

Step 3: Setting Up a Sample Project​

We will now create a sample project that you can use to start every project. It is like a template with all the main setting already handled.

You can download it on my MEGA, the link is on the first step of this instructable and under every youtube video of mine. Inside is the empty main.c file along with some startup files for this processor and the Makefile. Makefile is the one that tells the C compiler where to find the arm compiler, how to compile and where are all the libraries. To get these appropriate files for your project, you can go into the STM32 library folder and check for a "project" or "examples" folders. Inside you will see and copy these files: main.c, Makefile and XXX_conf.h, system_XXX.c.
Also you will need stm32_flash.ld linker file that can be found in folder :

"/FLASH_Program/TrueSTUDIO/FLASH_Program/" that is inside the example folder or just search for the file.

Makefile can be found online or copied from my folder, but you will need to change a few things. Let's tale a look at my make file and what you could change.

# Path to stlink folder for uploading code to board
STLINK=~/Embedded/stlink

# Put your source files here (*.c)
SRCS=main.c system_stm32f4xx.c

# Libraries source files
#SRCS += stm32f4xx_rcc.c
#SRCS += stm32f4xx_gpio.c

# Binaries will be generated with this name (.elf, .bin, .hex)
PROJ_NAME=test

# Put your STM32F4 library code directory here, change YOURUSERNAME to yours
STM_COMMON=/home/matej/Embedded/STM32F4-Discovery_FW_V1.1.0

# Compiler settings. Only edit CFLAGS to include other header files.

CC=arm-none-eabi-gcc
OBJCOPY=arm-none-eabi-objcopy

# Compiler flags
CFLAGS  = -g -O2 -Wall -Tstm32_flash.ld
CFLAGS += -DUSE_STDPERIPH_DRIVER
CFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m4 -mthumb-interwork
CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
CFLAGS += -I.

# Include files from STM libraries
CFLAGS += -I$(STM_COMMON)/Libraries/CMSIS/Include
CFLAGS += -I$(STM_COMMON)/Libraries/CMSIS/ST/STM32F4xx/Include
CFLAGS += -I$(STM_COMMON)/Libraries/STM32F4xx_StdPeriph_Driver/inc
CFLAGS += -I$(STM_COMMON)/Utilities/STM32F4-Discovery

# add startup file to build
SRCS += $(STM_COMMON)/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/TrueSTUDIO/startup_stm32f4xx.s
OBJS = $(SRCS:.c=.o)

vpath %.c $(STM_COMMON)/Libraries/STM32F4xx_StdPeriph_Driver/src \

.PHONY: proj

all: proj

proj: $(PROJ_NAME).elf

$(PROJ_NAME).elf: $(SRCS)
	$(CC) $(CFLAGS) $^ -o $@
	$(OBJCOPY) -O ihex $(PROJ_NAME).elf $(PROJ_NAME).hex
	$(OBJCOPY) -O binary $(PROJ_NAME).elf $(PROJ_NAME).bin

clean:
	rm -f *.o $(PROJ_NAME).elf $(PROJ_NAME).hex $(PROJ_NAME).bin

# Flash the STM32F4
burn: proj
	$(STLINK)/st-flash write $(PROJ_NAME).bin 0x80000000
  • You could edit first line to change path to the your stlink utility folder
  • You can change line to the destination of your folder with libraries and YOURUSERNAME

    STM_COMMON=/home/YOURUSERNAME/Embedded/STM32F4-Discovery_FW_V1.1.0<br>

  • Also check out section where all the libraries are linked. This can change depending on the platform that you are using so please check for changes in the file tree. Everithing else that includes any paths to certain files, like with next line with startup file can be changed.

After you have edited all these things inside the Makefile you can check if it is working by opening a terminal inside your directory and typing: make. If it compiles every thing with no problem, then you are set. If not, look at the compiler errors and edit the Makefile.

Also, when I use Atom, I put two pieces of code side by side. Usually main.c and Makefile on left as you only need to edit Makefile once and libraries on the right. You can see on the picture that I have opened the folder containing the .c and .h files for each library. You can see all of this in the videos.

Step 4: Finished!

Now that you have you Makefile configured and compiler working, you can use this folder for all projects as a template, so make sure you save a copy of this folder.

Also you can test the st-flash and st-info programs by plugging in your development board and typing into terminal: st-info --probe<br>

You can see the platform that the stlink software recognizes and the IC family along with cache and other stuff. You can type in:st-info to see all available parameters.

Now you can start programming. In the next instructable and video, I will show you the basics of GPIO and clocks. These two are the basics for everything else as almost everything that the board interacts with is over GPIO and everything works on clock and you will see the pattern to programming these processors.

Until then, thank you for checking out my instructable and my youtube video, if you haven't done so yet.