STM32 Overview
What is STM32 ?
STM32 is a family of 32-bit Microcontrollers and development boards manufactured by ST Microelectronics which offer very high performance for a relatively low price. All STM32 microcontrollers are based on ARM Cortex-M processor which make them much better in terms of computing power than most of the common microcontrollers/microcontroller development boards which are available in the market. These microcontrollers also have been provided with software support by ST for developers for free.
If you have experience with microcontrollers like Arduino, this is a great step forward in understanding microcontrollers in depth. The STM32 framework allows you to control all peripherals and allows you to tweak them as per your wish, whereas in Arduino IDE, when you program in C++, it comes with in-built functions and libraries which don’t allow you to understand the backend working behind everything. This is very crucial, as it allows you to use the microcontroller to its full potential.
STM32 FAMILY
The STM32 family of microcontrollers is vast and expansive, offering a variety of options for the consumer to choose from. The family of microcontrollers is broadly classified into 4 categories-
High performance
ST offers a series of microcontrollers which have the fastest code execution time, high speed data transfer and relatively higher flash memory. Along with this, the feature the most advance peripherals that ST offers. This makes these controllers very useful for real-time applications such as running an RTOS.
The F4 series is broadly divided into 3 segments-
Wireless
The main focus of the STM32Wx microcontroller family is wireless connectivity. They cover sub-GHz as well as 2.4Ghz frequency range operation
The STM32 wireless MCUs are further divided into 2 categories–
STM32WB
These MCUs have a dual core processor unit, Cortex M4 & Cortex M0+, which allows for processing distribution, making it a great candidate for real-time processing and execution. The MCU series is based on STM32L4 architecture, which offers dynamic voltage scaling, reducing the power consumption of the device. They also support BLE 5.3, Open Thread and Zigbee protocols, making them a great choice for IOT applications, providing a lot of functionality onboard the chip itself.
STM32WL
These MCUs also come with a dual core processor unit like the STM32WB series, and some variants also come with a single ARM Cortex M4 as their processing unit. The key highlight of this series of microcontrollers is that not only do they work as a general purpose microcontroller, but also come with an integrated sub GHz radio supporting LoRa WAN which makes it a great choice for smart home applications, utilities, logistics, Industrial IOT etc.
Ultra-Low Power –
STM32 ultra-low-power microcontrollers offer designers of energy-efficient embedded systems and applications a balance between performance, power, security and cost effectiveness. The portfolio includes the STM8L (8-bit proprietary core), the STM32L4 (Arm® Cortex®-M4), the STM32L0 (Arm® Cortex®-M0+) and the STM32L1 (Arm® Cortex®-M3).
The STM32L5 MCU (Arm® Cortex®-M33) with its enhancedsecurity features is the latest addition to this rich portfolio.
- The new STM32U5 series combines the latest and most efficient Arm Cortex-M33 core with an innovative 40 nm platform that reduces energy consumption to the bone, while increasing performance. The series also adds the state-of-the-art features which are required in today’s applications,including advanced cyber security with hardware-based protection, and graphics accelerators for rich graphical user interfaces.
- The STM32L5 series enhanced security features leverage Arm® Cortex®-M33 and its TrustZone® for Armv8-M. Thanks to this new core and a new ST ART Accelerator™ (also supporting external memory), the STM32L5 reaches an 443 CoreMark.
- The STM32L4 series offers the excellence of ST’s ultra-low power platform with an additional performance dimension by providing 100 DMIPS with DSP instructions and Floating-Point Unit (FPU), more memory (up to 1 Mbyte of Flash memory) and innovative features.
- The STM32L4+ series extends STM32L4 technology by offering higher performance (120 MHz/409 CoreMark executing from internal Flash memory), larger embedded memories (up to 2 Mbytes of Flash memory and 640 Kbytes of SRAM), and rendering advanced graphics without compromising ultra-low-power consumption.
- The STM32L0 series offers a genuine energy-saving solution for entry-level applications. Available in tiny packages down to 14 pins and with a wide range of Flash memory densities from 8 to 192 Kbytes, the STM32L0 features ultra-low power consumption in a competitive portfolio.
Mainstream–
The mainstream microcontrollers are more general-purpose boards which offer a large variety of applications and do not have proclivity towards only a single application. These can be considered well balanced devices in terms of performance vs power consumption and come at affordable prices meant for robust applications.
- STM32F0 series is an attractive 32-bit MCU alternative to 8- and 16-bit microcontrollers for engineers looking at future-proofing their product platform and standardizing their development around the industry-standard ARM cortex M0 cores.
- STM32G0 series is a new generation of efficient microcontrollers based on ARM cortex M0+ cores delivering an upgrade in BOM cost reduction, performance, consumption and integration. The STM32G0 Series supports a wider portfolio in packages and memories while featuring the essentials of the STM32 family and is particularly well suited for cost-sensitive applications.
- STM32F1 series, the pioneer of the STM32 family of 32-bit Arm® Cortex MCUs, offers high performance with first-class peripherals paired with a high level of integration at accessible prices with a simple architecture and easy-to-use tools.
- STM32F3 series comes with its rich choice of advanced analog peripherals enables the most demanding real-time control for consumer and industrial applications.
Getting Started-
To get started with STM32, it is common practice to get an evaluation board / development board. Evaluation boards are useful when you need to test the peripherals onboard the microcontroller and see whether the selected MCU is the best choice for your project / device or not. These boards are generally breadboard friendly or sometimes come with sensors and some output devices like displays onboard, which eliminates the need for a breadboard. You can imagine these boards as the equivalent of an Arduino UNO in the STM32 world.
STM32
Blue Pill (STM32F103C8T6)-
The Blue pill is based around the STM32F103C8T6 microcontroller chip. It is based on ARM Cortex M3 and comes with many features and rich peripherals like-
This board is also very well documented online by the community. To program this board, you can use Keil µ Vision, STM32 Cube IDE (explained here) and even Arduino IDE, as some developers have made it possible to work with it using the Arduino bootloader, which would allow you to directly program it via micro-USB. To just get started with the STM32 environment, the Blue Pill is a great choice.
To work with any STM32 microcontroller, ST Microelectronics provides a Software Development Kit (SDK), which is a collection of software tools to program and debug the device.
The STM32 Cube IDE is a development environment is based on Eclipse®/CDT™ framework and GCC toolchain for the development. It is basically a software in which you can write your code and compile it to build your Hex and binary files to upload on the microcontroller. You can use this to analyse your code and test it out.
You can download the software package from the official website – https://www.st.com/en/development-tools/stm32cubeide.html
Here’s a look at the same-
Click on ‘Open file’-
Once you click on ‘open file’, you would need to select the file you want to upload to your device. Here, you have to pick the ELF or Hex file that you created when completing your code in STM32 Cube IDE.
Once you
have written your code and compiled it, it’s time to upload your code to your
microcontroller. For this purpose, ST provides the STM32 Cube Programmer to
upload your files to the flash memory of the microcontroller. You can download
the software officially from-
https://www.st.com/en/development-tools/stm32cubeprog.html
To upload the code, you first have to connect your device to ST-link V2 as described here. Once you make the connections, follow these steps to upload your code to the board-
Select your ST-link device-
Upon refreshing, you would see the serial number of your ST-link. If you do not see your ST-link’s serial number, then you can try to use an older version of STM32 Cube Programmer, or you can try to upgrade its firmware by clicking on ‘Update Firmware’. In case you are still unable to see your device, you can try using the ‘ST-link Utility’ software, which sometimes is able to interface with older ST-link devices.
If connected, you would be able to see which microcontroller you are using under ‘Target information’. This should match the microcontroller you have connected. Keep the other settings as they are and click on connect. Once you are able to connect, you can see the data that is in all the registers along with their addresses. This can come in handy while debugging.
Click on ‘Open file’-
Once you click on ‘open file’, you would need to select the file you want to upload to your device. Here, you have to pick the ELF or Hex file that you created when completing your code in STM32 Cube IDE.
Select
‘Download’
Right next
to the ST-link configuration menu, you would be able to see a drop-down to
select from the options shown in this image. Select download for dumping your
code into the microcontroller’s Flash memory
If everything is done correctly, your code should be up and running!
STM32 Cube Programmer also offers features like reading the memory to get the existing data in your flash memory (the last program onboard the controller) into a file. Another useful feature in this software is ‘Verify’ using which you can compare the existing flash memory code to a file on your PC to verify whether both of them are the same or not. This feature is very useful to check if you have the right code running on your microcontroller.
The STM32 framework allows users to use the full potential of a microcontroller, but it comes it’s own inconveniences. If you are new to the STM landscape, it would be a very tedious task to read through the reference manuals, find the right registers to manipulate for just setting up your controller in the first place. This includes setting up pins as digital Input/Output, selecting whether to use the internal or external clock (that too high speed or low speed), setting the desired pre-scalers for your peripherals etc. To overcome this problem and reduce the amount of time spent on setting up your device and then writing the actual code, ST Microelectronics provides a software called STM32 CubeMX. This software is a GUI, which shows you all the possible settings for your pre-scalers, I/O pins, clock, timers etc. This makes it much easier to set up your device and quickly implement your project.
You can download STM32 CubeMX from the official link –
https://www.st.com/en/development-tools/stm32cubemx.html
Here is a look at the same-
As you can see, after you create your project by selecting your microcontroller, you get a GUI where you can select each pin individually and decide it’s function as per your project requirements. Once you set up your pins, you can generate the code for the configuration you have made for each pin.
Not only that, you can also set up your clock configuration to save time in the initialization process. Under the ‘Clock configuration’ menu, you can see all the clocks present on the microcontroller, select your main clock, set the pre-scalers for all the different peripherals as per your need and the software will generate the code needed to set up the desired clock configuration by manipulation of clock related registers.
STM32 HAL (HARDWARE ABSTRACTION LAYER)
Let’s say you are very new to programming in general and also very new to the STM32 environment, or you are just interested in developing a project using this class of microcontrollers to get an end result. You most likely are not bothered with going down to the register level and changing values for reading/writing, some basic GPIO functionality, using PWM, or working with communication buses like UART, SPI, I2C etc. For the ease of programming for the user (i.e., you) ST provides what is called a Hardware Abstraction Layer.
The Hardware abstraction layer is an API (Application programming Interface) which allows you to simply use the functions provided in it’s files to work with the hardware in your microcontroller. Here, we can draw a very good comparison between HAL and the Arduino IDE. When you program in the Arduino IDE, you can use certain functions such as pinMode( ) digitalWrite( ) Serial.print() etc. which require the microcontroller to do some register manipulation in order to work with the pins on your Arduino. But have you ever noticed that you never have to include any library for this functionality?
The Arduino IDE by default includes some files while you write your code to make it easier to work with it. That is the reason why Arduino sketches are on average larger in size than they always need to be. But it comes with the benefit of ease for the user to program and implement ideas.
STM32 HAL is very similar to this. This API is provided for all microcontroller series manufactured by ST Microelectronics, making it simpler to use the hardware peripherals on your microcontroller and saving much of your time. HAL provides these drivers to operate certain things on your microcontroller such as Timers, Counters, Interrupts, GPIO, UART, SPI, I2C etc.
For further reading, you can refer to the official documentation done by ST – https://www.st.com/resource/en/user_manual/um1725-description-of-stm32f4-hal-and-lowlayer-drivers-stmicroelectronics.pdf
STM32 Debuggers
The STM32 environment wouldn’t be complete without debuggers. These are devices provided by STM officially and also unofficially by some vendors which are popular. Debuggers allow you to interface with your STM32 microcontroller for uploading code and also debugging your code.
They come in 2 versions, an official one provided by STM, and one unofficial which people have developed and is cheaper to buy.
STlink-V2 USB dongle (unofficial) –
This is an unofficial version which is very popular and used by many people working with STM32 microcontrollers. The device has header pins at the back to which you can attach jumper wires and attach to your STM32 microcontroller by connecting the corresponding pins. When you do this and attach this dongle to your PC, you can debug and dump programs into your flash memory. After connecting, you can open STM32 Cube Programmer and follow the steps described below to upload your code and debug it.
For connecting your STM32 board to the STlink V2 Dongle , here is a schematic-
Note that this device maybe cheap to buy, but the product quality depends on the vendor that you pick. Some of these devices either work flawlessly without any problems or can be an absolute nightmare to get working. Sometimes they straight up don’t even work. Some manufacturers provide it with older firmware and sometimes you might need to even use older versions of STM32 Cube Programmer or even use the very old ‘ST link utility’ software to get these devices to work. You can use it to program your Microcontroller, but it is advisable to read product reviews before purchasing this device.
ST-link V2 (official)-
The official ST-link V2 is provided by ST Microelectronics and accomplishes the same tasks that ST-link V2 dongle does. It provides something that the unofficial version doesn’t, that is the JTAG debugging feature. Apart from that you can use the trace feature for debugging as well. Best part about this device is that it is reliable and probably won’t require you to troubleshoot much once you connect it to your PC.
Author