Sunday, March 29, 2020

Start Developing STM32 on Linux

Step 1: Download Tools

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.

Step 2: Installing Software

Installing Software
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.
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.cMakefile and XXX_conf.hsystem_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.

No comments:

Post a Comment

Back to Top