Our Latest News

The difference between the stack in ordinary microcontrollers and STM32 microcontrollers

When learning STM32 microcontroller, you always come across the concept of “stack”. I hope this article will help you to understand the stack.

For those who know a little bit about assembly programming, you can know that the stack is a continuous storage area in memory, which is used to store some temporary data. The stack operation is done by two instructions, PUSH and POP. And the program memory can be divided into several zones.

This image has an empty alt attribute; its file name is image-137.png

Stack area (stack)

Heap area (Heap)

Global area (staTIc)

Text constant light area program code area

After the program is compiled, global variables and static variables have been allocated memory space. When the function is running, the program needs to allocate stack space for local variables, and when the interrupt comes, it also needs to put the function pointer into the stack to protect the site so that it can return to the previously executed function after the interrupt is processed.
The stack is allocated from high to low, and the heap is allocated from low to high.

The difference between stack in ordinary microcontroller and STM32 microcontroller

But STM32 microcontroller needs it.

Here we can first look at the process of microcontroller program execution, microcontroller execution in three steps.

Fetch instructions

Analyze the instruction

Execute the instruction

According to the value of the PC read out instructions from program memory to the instruction register. Then analyze and execute the execution. In this way the microcontroller goes to code instructions from the internal program memory and accesses the relevant data from RAM.

The speed of RAM fetching is much higher than ROM, but the slow fetching of instructions from ROM does not affect the ordinary microcontroller because it does not run at a high frequency itself.

The STM32 CPU runs at a high frequency, which is much faster than the speed of reading and writing from ROM. So you need to use bootloader to move the code from ROM to RAM.

Using a stack is like going to a restaurant, just order (send request), pay, and eat (use), and leave when you’re full, not caring about the preparation work such as chopping and washing vegetables and the cleanup work such as washing dishes and brushing pots. Using the heap is like making your own favorite dishes, more trouble, but more in line with your taste, and the degree of freedom.

In fact, the stack is a number of storage units in the microcontroller, these storage units are designated to save some special information, such as address (to protect the breakpoint) and data (to protect the site).

If I had to add a few features to him it would be.

The contents of these memory cells are some parameters related to the accident scene when the execution of the program is interrupted in the process. If these parameters are not saved, the microcontroller can not return to the main program after the execution of the interrupt function to continue execution.

The addresses of these memory cells are recorded in a place called the stack pointer (SP).

Stack in the context of STM32 development

From the above description, you can see how the heap and stack are occupied in the code. Many people may still not be able to understand it, so here we will talk about it in the context of the STM32 development process related to the stack.

How to set the STM32 stack size?

At the beginning of the MDK-based startup file, there is a section of assembly code that allocates the stack size.

f0aae06a-9fb7-11ed-bfe3-dac502259ad0.png

The focus here is just to know the size of the stack values. There is also a paragraph AREA (area), which indicates the allocation of a segment of stack data. The value size can be modified by yourself, or you can use the STM32CubeMX value size configuration, as shown below.

The STM32F1 default setting is 0x400, which is the 1K size.

Stack_Size EQU 0x400

Local variables within the function body.

void Fun(void){ char i; int Tmp[256]; //…}

The local variables occupy a total of 256*4 + 1 byte of stack space. So, when there are more local variables inside the function, you need to be careful if you exceed the stack size we configured.

Function parameters.

void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)

Here to emphasize a point: pass pointer only occupies 4 bytes, if pass structure, it will occupy structure size space. Tip: When nesting functions, recursive, the system will still occupy the stack space.

The default setting for Heap is 0x200 (512) bytes.

Heap_Size EQU 0x200

Most people should rarely use malloc to allocate heap space. Although the data on the heap can always be accessed as long as the programmer does not free the space, forgetting to free the heap memory will cause memory leaks and even fatal potential errors.

Analysis of RAM Occupancy Size in MDK

Those who often debug online may analyze some of the underlying content. Here is a combination of MDK-ARM to analyze the RAM occupancy size. After MDK is compiled, there will be a RAM size information.

f0c81752-9fb7-11ed-bfe3-dac502259ad0.png

Here 4 + 6 = 1640, converted to hexadecimal is 0x668, when conducting in debugging, there will be.

f0d40b84-9fb7-11ed-bfe3-dac502259ad0.png

This MSP is the main stack pointer, generally we reset after pointing to the location, the reset points to actually the top of the stack: the

f0df2a78-9fb7-11ed-bfe3-dac502259ad0.png

The MSP points to address 0x20000668, which is 0x20000000 offset by 0x668. For details on where the RAM is used, see the map file at [Image Symbol Table].

f0ecf77a-9fb7-11ed-bfe3-dac502259ad0.png

    GET A FREE QUOTE

    FPGA IC & FULL BOM LIST

    We'd love to

    hear from you

    Highlight multiple sections with this eye-catching call to action style.

      Contact Us

      Exhibition Bay South Squre, Fuhai Bao’an Shenzhen China

      • Sales@ebics.com
      • +86.755.27389663