Adventures in STM32 - part 1

Recently I wrote a short article about setting up environment for programming STM32 microcontrollers with libopencm3 library. You can read it here.

I wanted to write the second part as soon as possible but I found out that even though I could, I didn’t understand everything well enough. I just didn’t feel comfortable with the subject. That’s why I started this series. I want to explore what actually happens when you program STM32. Only then I can say I really understand the whole process.

Processor architecture

STM32 is a family of 32-bit microcontrollers based on ARM Cortex-M architecture. The fact that they are ARM is really important and a lot of concepts stem from that fact. What does it actually mean for you? The architecture defines how the processor is built and in consequence defines a set of instructions the processor understands.

Programming languages (like C ) allow developers to write code which can be compiled for different platforms. Compilers take the burden of translating the code to a platform specific instructions on themselves. This platform specific instructions are written in Assembly language. You might have heard it in a sentence like:

No one programs in Assembly anymore!

Which is mostly true. It isn’t easy or fast to write in Assembly, because it deals with the operations on the specific registers of the processor - it describes what the processor actually does. Therefore It makes sense that there are multiple flavours of Assembly.

Reading and learning about Assembly for a specific architecture can be really beneficial to understand what actually happens inside the processor, and I strongly encourage you to do so. One of the more interesting articles on this topic can be found here.

Lets get back to ARM. Saying something uses ARM architecture doesn’t give you a lot of information. If you talk about specific processor you should also know if it’s Cortex-R, Cortex-A or Cortex-M. Those are 3 groups of processors using ARM architecture. Since we are here to program microcontrollers, only Cortex-M cores are of interest for us. Cortex-R are used for hard real-time applications. Cortex-A are for application use (lets say small Linux computers). Cortex-M? Well… Microcontrollers!

And you still can exactly say what subset of Assembly instructions the processor uses… There are versions of Cortex-M. There are specific floating point math instructions the processor may or may not support. There are SIMD instructions…

But don’t worry. You won’t be programming in Assembly (unless you really want to). You just have to understand that the architecture of the processor matters. It tells you what the processor can do and with what it might struggle.

Everything written above is important because it influences the way the STM32’s libraries are written and distributed. Before going into the libraries I will delve into the memory of a typical STM32 microcontroller in part 2.