Transitioning from Arduino to STM32? There Are Some Caveats You Should Know
You've learned Arduino and feel confident with your basic embedded skills. Now you want to upgrade yourself by learning STM32. It should be the next logical step, right?
First of all, I want to say congratulations to you because you've proved yourself that you enjoy embedded systems. Not many people can go through that phase.
Now, this is the new phase. Learning something more "serious" like STM32. (though you can seriously use Arduino as well, with some tips).
I promise you, learning STM32 will be a delightful experience, and you won't regret a single bit (pardon the pun). But to ensure the smoothest transition, I will offer you some of my wisdom.
There are several things you need to know to ensure a smooth transition (not as smooth as butter though, you still have to learn a lot):
Let's dive into each point.
Which STM32 board should you choose?
If you just want to pick one board. Choose the blue pill. That's it. It's good enough for you to get started. Blue pill means the STM32 board's form-factor is like a giant pill and the color is blue.
Usually blue pills contain STM32F103 microcontroller which is pretty good for various basic projects.
Of course you can pick other boards. For example, if you want to focus on low-power project (aka battery-powered), you can explore STM32L series. Personally I'd go with L0 series (e.g. STM32L072) if I want to squeeze battery life, but with the cons being slower and not suitable for high-computing projects.
If I need more performance, I'd go with the L4 series (e.g. STM32L476). It has Floating-Point Unit (FPU) and larger memory. Great for projects like smartwatch. I have a project where I need to perform 1000 float/double calculations per second on the fly. L0 won't cut it. L4 series will crush this requirement.
You probably won't need more powerful boards right now. But if you do need some inspiration, you could use H5 or H7 series if you're doing crazy stuff like high-speed DSP, or AI/ML stuff.
But again, if you're just starting out the Blue Pill will do just fine.
To maximize learning, don't use Arduino framework
Yes, you read that right. Though I have no issue if you use Arduino framework professionally, I think you should ditch Arduino framework this time.
Wait, you could use Arduino framework with Blue Pill?
Yes. Search "STM32duino".
Because you have used Arduino board like UNO or Pro or Micro before, you definitely have used Arduino framework (you know, if you used Serial.begin(115200)
before, it's Arduino framework).
STM32 microcontroller is way too powerful compared to basic ATMega328 chip.
Arduino, in your case, will hinder your learning progress and will hide too many interesting features an STM32 microcontroller has to offer.
CMSIS, HAL, mbed, Keil, IAR, CubeMX. What the hell are they?
They are tools and libraries in STM32 ecosystem.
ARM (the company) created ARM Cortex-M (the processor) and wrote libraries for it and call it CMSIS.
ST Microelectronics created microcontrollers (like STM32F103) and used Cortex-M as the processor. ST then wrote libraries for STM32F103 and called it HAL (on top of the CMSIS).
mbed is ARM (the company) version of Arduino framework, it's far more powerful than Arduino. You probably want to get experience on HAL first, then if you're interested, you could try mbed.
Now, your firmware source code needs to be compiled so you can get the binary and flash it into the microcontroller. There are many options you can take:
- GCC Compiler. Free, but not too optimized for STM32 microcontroller.
- IAR Compiler. Optimized binary result, but paid or limited feature in free version.
- Keil Compiler. Also optimized and limited feature in free version.
Pick GCC, it should enough for now. As you gain more experience and knowledge, you probably want to switch to IAR or Keil offering.
ST Microelectronics offers a tool called STM32CubeMX to generate a boilerplate (template project for you to get started).
Why would you need a boilerplate? Because STM32 microcontrollers have tons of peripherals, and each of them has tons of configuration. It's tedious and painful to write code manually over and over again, so ST created a tool to assist you generating the basic code.
Different way to configure peripherals
I'd say it again, STM32 microcontrollers have tons of peripherals, and each of them has tons of configuration. Let me give you an example:
In STM32, you will find something called Clock Tree.
Unlike your classic ATMega328 which uses a single clock for the entire chip, STM32 can have different clock speeds for each of its peripherals. Why you might ask? In summary it's for flexibility.
Now, configuring clock tree is error-prone when it's done manually, that's why we use CubeMX to help us generate correct settings for the entire system.
This is drastically different in Arduino, where we simply use Serial.begin()
for UART peripheral, SPI.begin()
for the SPI, and so on. Because the configuration is simple, we can do it in a single line. No need to have CubeMX for Arduino.
This is something you need to be aware of when exploring STM32. You will need CubeMX almost all the time.
I said this so you won't be surprised with the tooling.
Different build system (and flags)
In Arduino, usually we just click a button to compile the source code and flash binary to the board. We don't really care about the process. What we expect is to just get the board flashed.
Although we can automate the build process in STM32, we will have to make effort to understand the build system. For example, you need to understand linker script, CMake script, what files to compile, etc.
In Arduino you can't set the compile and linking flags. Heck, we don't even understand what is compiler "flags". Yes, it's an important topic but Arduino hides them from us.
Actually this topic alone is worth a book chapter, and I won't go deeper here. I'm just giving you a caution so you won't discouraged when things are getting harder (because it will be harder).
Different library structure
This is probably the most difficult thing to accept as someone transitioning from Arduino to STM32.
Arduino libraries have their own structure and way of operating. And they are, in general, not compatible with STM32 ecosystem (except someone has ported the libs).
For example, this OLED display library cannot directly be used for STM32 HAL. You need to port it manually (which requires some skills).
Some libraries are platform-independent, this is the kind of library I love. Some examples are TinyGPS (with minor modifications), ArduinoJSON, and Bosch BMI160 driver (not beginner-friendly though). So I can use them pretty much without much changes to use for my STM32 HAL projects.
So embrace the fact you will find yourself writing (or porting) driver for anything not Arduino compatible.
More debugging feature
In STM32 you will get more advanced debugging options: JTAG, SWD, as well as the classic printf().
The cool thing about advanced debugging is you can gain much deeper information about the system state. It's just a fancy word to describe current memory condition. You can see every variable's content, what functions are being called, what caused crash, how much memory a task consumes, etc.
You cannot do that using Arduino.
But for every advanced technology, there is always an additional effort to understand and use it.
So, make the effort and learn them.
There you have it. Happy learning STM32!
Whenever you're ready, there are 2 ways I can help you:
1. FREE Firmware Development Email Course. A free 7-Day Email Course to teach you the foundational concepts you should know to start firmware development. Full of practical tips and industrial insights.
2. FREE Embedded Freelancing Email Course. Another free Email Course to help you grasp the basics of embedded freelancing and make more money as an embedded engineer.