The most beloved setup for programming STM32 microcontrollers is project generated by CubeMX, and HAL library on board. It might look appealing - graphical MCU setup, configuration done in a few clicks, project automatically generated. My God, marvelous! Well… While this approach is convenient for many coders, it doesn't satisfy my needs. The code generated by CubeMX is totally unreadable for me, and even without the helper comments I couldn't understand what and why is happening. Although HAL simplifies complicated tasks, it complicates simple ones. I really tried to learn it, but I just couldn't, so I decided to stick with a programming method I used on 8-bit Atmels - registers.
8-bit Atmels are super easy to learn, program and boot. The minimum working ASM code for such microcontroller looks like that:
.include "tn12def.inc" .cseg .org 0x0000 rjmp reset reset: ; Set stack pointer to end of the RAM ldi r16, low(RAMEND) out SPL, r16 ; Your code begins here, have fun!
Yep, that's all. You can even remove the lines responsible for reset vector, but I believe it's a good practice to use them. The only required routine is setting stack pointer to end of RAM. STM32 microcontrollers are not that easy, they require more operations to be done before jumping to main method. Writing this every time can be pain in the ass, but thankfully ST creates these startup codes for us.
The things you need to have to start coding are headers with registers' definitions, along with startup codes. How to get them? The simplest method is to use… CubeMX! Through this tool you can download all necessary headers, linkers scripts and sources for all ST's microcontrollers.
Download, install and run CubeMX, then follow the instructions below.
Great job! Now you have to locate where these packages were downloaded. You can find location of the repository folder in Updater settings under Help menu. When you navigate to this directory, you'll find all downloaded packages there. All necessary files are here. Time to create the project.
As I promised, I'll use CMake for creating the project. I assume you have any experience with it, because this article is not about using CMake.
This article is in progress