Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the memory allocation of C language in STM32

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to analyze the memory allocation of C language in STM32, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

01, preface

Don't talk nonsense, go to the sample code first.

Uint8_t num_byte [4]; uint32_t num_word;const uint32_t num_word_const = 0x1234 / uint32folt * point_heap;int main (void) {uint8_t num_byte_stack; static uint8_t num_byte_static; point_heap = (uint32_t *) malloc (4); * point_heap = 0x3421; free (point_heap); num_byte_stack = 0x11; # pragma section = "CSTACK" char * pbeginstk = _ _ section_begin ("CSTACK") # pragma section = "HEAP" char * pbeginheap = _ _ section_begin ("HEAP"); printf ("CSTACK addr is 0x%x\ r\ n", pbeginstk); printf ("HEAP addr is 0x%x\ r\ n", pbeginheap); printf ("num_byte addr is 0x%x\ r\ n", & num_byte); printf ("num_word addr is 0x%x\ r\ n", & num_word) Printf ("num_word_const addr is 0x%x\ r\ n", & num_word_const); printf ("point_heap addr is 0x%x\ r\ n", & point_heap); printf ("point_heap is 0x%x\ r\ n", point_heap); printf ("num_byte_stack addr is 0x%x\ r\ n", & num_byte_stack); printf ("num_byte_static addr is 0x%x\ r\ n", & num_byte_static);}

Print as follows

STACK addr is 0x20000320

HEAP addr is 0x20000720

Num_byte addr is 0x20000308

Num_word addr is 0x2000030c

Num_word_const addr is 0x8002a44

Point_heap addr is 0x20000310

Point_heap is 0x20000728

Num_byte_stack addr is 0x200006f8

Num_byte_static addr is 0x20000318

Let's start with the conclusion:

Num_byte, num_word, num_byte_static, and point_heap are stored in the internal RAM.

Num_byte_stack is stored in the stack.

Memory heap applied for by point_heap.

Num_word_const is in the internal flash.

If some students know this very well, they can go out and turn left. If some students are interested, they can look down further.

02. Large and small end

Because the following content is related to the size end, let's talk about the size end first.

Big-endian: the high-order bytes of the data are stored at the low end of the address, and the low-order bytes are stored at the high end of the address.

Small end (Little-endian): the high-order bytes of the data are stored in the high-end and low-order bytes of the address, and the low-end of the address.

For example:

Data 0x12345678 storage format

Big-end format

Low address and high address

Small end format

Low address and high address

The address is generally assigned by the compiler or can be specified in the program. From the above table, you can clearly see that the size end is the way to store data in bytes. The popular understanding of the big end is that the assignment number is from left to right; the small end is from right to left.

Our common X86 architecture is small-end mode, while KEILC51 is large-end mode. Many ARM,DSP are small-end mode, and the platform STM32F207 used in this article is the small-segment mode.

03. Step by step analysis

If some students are not familiar with this part, I suggest you take a look at my previous tweet "memory allocation in C language" and familiarize yourself with the concepts of C language stack, memory and so on.

First of all, about the stack, the following code can print the stack start position of STM32 on the IAR platform.

# pragma section = "CSTACK" char * pbeginstk = _ _ section_begin ("CSTACK"); # pragma section = "HEAP" char * pbeginheap = _ _ section_begin ("HEAP")

The printed result is as follows

STACK addr is 0x20000320

HEAP addr is 0x20000720

Whether this address is correct or not can be checked in the Disassembly window when we IARdebug.

With regard to the stack size, the following

You can find that the stack terminates at 0x20000720 and the heap terminates at 0x20000920. Note: the calculation here involves the size of the problem.

By calculation:

Size of the stack = 0x20000720-0x20000320=0x400.

Heap size = 0x20000920-0x20000720=0x200.

This is the same as our stack configuration in IAR.

Next, let's talk about the variables allocated in memory.

As you can see from the print, num_byte, num_word, num_byte_static, and point_heap are not on the stack, they are stored in the internal RAM.

Use the Disassembly window to view the following

This also validates the static keyword, which is stored in the same internal ram as the global variable when decorating the local variable within the function.

It also shows that when STM32 allocates memory internally, it first allocates global variables (and local variables modified by static), then allocates stack, and finally allocates heap.

For the memory allocation of the stack, the local variable, that is, num_byte_stack, is stored in the scope of the stack.

Num_byte_stack addr is 0x200006f8

Its address space is on the stack. Because in the code num_byte_stack = 0x11; use the Disassembly window to see that the corresponding address value is 0x11.

With regard to the stack, another word is that the stack not only holds local variables, but also saves the scene in the event of a function switch, the interrupt occurs, and the registers of the ARM kernel. These are not the focus of this article. Dig a hole here, and then write an article dedicated to this part later.

Heap problem, to put it simply: the memory requested by malloc is in the heap. The memory address pointed to by the point_heap pointer is within the range of the heap.

Point_heap is 0x20000728

In the code, * point_heap= 0x3421; check in the Disassembly window and see that the corresponding address value is 0x3421.

The last num_word_const,const-decorated variable is stored in the internal flash and its address is in the internal flash range.

There is also a corresponding assignment in the code, constuint32_t num_word_const = 0x1234; in the Disassembly window, you can see that the corresponding address value is 0x1234.

After reading the above, do you have any further understanding of how to analyze the memory allocation of C language in STM32? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report