STM32 is a uC family developed by STMicroelectronics based on the 32-bit ARM architecture. Overall a very interesting platform for embedded development with multiple choices regarding features and power. The community support is very good I would say and tools for development are widely available. Free and open source is always in scope for hobby and prototyping!
This is still a work in progress... so there are just pieces of information and not a complete article. I do plan to write more as I progress with my project. First of all I bought 2 STM32 boards from Aliexpress:
The STM32F103RCT6-Generic-Board - due to some of the nice features that the STM32 chip offers (2 DAC's).
The Blue Pill because is cheap and I got a good deal for both ST Link and the board (about 3.5 euro).
The first challenge is always to test that everything works as expected and to set up a comfortable development environment. There are 2 variants that I would like to cover in this article. The first one is based on Arduino IDE with no support for debugging and the second one base on VS Code where one can take advantage of the debugging features and extensive intelisense. The second one is where I would go for bigger projects, where actually teams can work and do more thing together.
Officially the STCubeIDE is provided and is in any case a must as it comes with a few tools that make life a hole lot easier. Coding experience in the IDE is not perfect but it does work well. Debugging is also quite good. The fact is that STCubeIDE will hide a lot of the low level stuff by having the HAL as a middle layer.
Another topic that I would like to cover is the base that is used. Here are the libraries on which the user application is based. From the Arduino framework to a fully featured Realtime OS there are a lot of options to choose. I would like to cover here the 2 that I think have the most community support and are, of course, Open Source: Arduino and FreeRTOS.
For interfacing with the two STM32 boards I have two solutions at my disposal: the FTDI and the ST-Link v2.
Arduino IDE
After downloading the IDE one needs to install the toolchain and adaptation for the STM32. This can be installed from here: https://github.com/stm32duino/Arduino_Core_STM32
After this is done there are a couple of changes needed in order to be able to use the ST Link or the FTDI chip for flashing:
- OpenOCD: usually the default version that comes is a little out of date or missing scripts. The best way is to get one from github and compile-it yourself. A couple of scripts are necessary (basically copy paste some of the existing ones and change the FTDI vendor id, etc).
- Change the boards/platforms definition from the Arduino STM32 core.
With tease changes a new program compiled with Arduino IDE can be flashed on the chip.
Debugging connections can be made in 2 ways: SWJ or JTAG. Here I use my ft4232h adapter.
RS232 JTAG SWJ
---------------------------------------------------------
ADBUS0 | TXD TCK SWDCLK
ADBUS1 | RXD TDO/DI SWDIO
ADBUS2 | RTS TDI/DO SWDIO (470 Ohm)
ADBUS3 | CTS TMS N/A
ADBUS4 | DTR (GPIOL0) /nSRST optional module reset
ADBUS5 | DSR (GPIOL1) /nTRST optional target reset
Another way to flash a program using the FTDI is to use the serial communication features. The STM32 comes with a prebuild serial bootloader:
Visual Studio Code
Visual studio code is my preferred development environment: light but still packet with features and extensible. A few extensions will be required: C/C++, Cortex Debug, ARM and QML.
To get the full control of the compilation process I chosen to use qbs and I have created my own rules for compiling the code. As the compilation process I used a similar one as the Arduino where every project is compiles as a lib and in the final step everything is linked into the application. The compiler of choice is gcc as provided by my package manager. Using qbs proves to be very intuitive and easy and I was able to create full build framework vor various ST uC in a couple of lines of code.
A very helpful starting point is the STM32-Base web site. From here I used the project layout (startup + linker) as it looked very easy to understand and integrate. basically they provide the linker + startup files in a very structured way. I only used the F3 and F4 uC for the moment but in theory it can be extended to the other STM32 families as well.
My project example can be found in github: https://github.com/bdmihai/blink-stm32f103rb
Tips and Tricks
Reseting the FTDI kernel module. It hapens when you remove the FTDI but the driver still remains. After pluging again the ttyUSB ports cannot be opened anymore. The reset command is the following:
sudo modprobe -r ftdi_sio && sleep 5 && sudo modprobe ftdi_sio