2489 views|0 replies

6555

Posts

0

Resources
The OP
 

C language memory allocation in STM32 [Copy link]

01Foreword

Without further ado, let’s start with the sample code

uint8_t num_byte[4];
uint32_t num_word;
const uint32_t num_word_const = 0x1234;
uint32_t *point_heap;
int main(void)
{
uint8_t num_byte_stack;
static uint8_t num_byte_static;

point_heap = (uint32_t *)malloc(4);
*point_heap = 0x 3421;
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 talk about the conclusion first:

num_byte, num_word, num_byte_static and point_heap are stored in internal RAM.

num_byte_stack is stored in the stack.

The memory requested by point_heap is in the heap.

num_word_const is in internal flash.

If there are students who know this well, they can go out and turn left. If some students are interested, they can read further.

02 big and small endian

Because the following content involves big and small endian issues, let’s talk about the big and small endian issues first.

Big-endian: The high-order byte of the data is stored at the low-end of the address and the low-order byte is stored at the high-end of the address;

Little-endian: The high-order bytes of the data are stored at the high-end of the address, and the low-order bytes are stored at the low-end of the address;

For example:

Data 0x12345678 storage format

big endian format

Low address<----0x12|0x34|0x56|0x78---->High address

little endian format

Low address<----0x78|0x56|0x34|0x12---->High address

The address is generally assigned by the compiler, or can be specified in the program. From the above table, we can clearly see that big and small endian is the way to store data in bytes. The popular understanding of big endian is that numbers are assigned from left to right; little endian is assigned from right to left.

Our commonly used X86 structure is little endian mode, while KEILC51 is big endian mode. Many ARMs and DSPs are in little-endian mode. The platform STM32F207 used in this article is in small-segment mode.

03 Step by step analysis

If there are students who are not very familiar with this part, I suggest you first read my previous tweet "Memory Allocation in C Language" to familiarize yourself with the stack, memory and other concepts of C language.

Let’s talk about the stack first. The following code can print out the stack starting position of STM32 under 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, we can use the Disassembly window to check it during IARdebug.

Regarding the stack size issue, as follows

It can be found that the end position of the stack is 0x20000720 and the end position of the heap is 0x20000920. Note: The calculation here involves the issue of big and small endianness.

via caculation:

Stack size=0x20000720-0x20000320=0x400.

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

This is the same stack configuration we have in IAR.

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

It can be seen from printing that num_byte, num_word, num_byte_static and point_heap are not on the stack, they are stored in internal RAM.

Use the Disassembly window to view the following

This also verifies the static keyword. When modifying a local variable within a function, this variable will be stored in the internal ram like the global variable.

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

For stack memory allocation, local variables, namely num_byte_stack, are stored in the scope of the stack.

num_byte_stack addr is 0x200006f8 and
its address space is in the stack. Because in the code num_byte_stack =0x11; use the Disassembly window to see that the corresponding address value is 0x11.

Regarding the stack, let me say one more thing. The stack not only saves local variables, it also saves the scene when functions are switched, interrupts occur, and saves the registers of the ARM core. These are not the focus of this article. I will dig a hole here first and wait for more later. I will write another article specifically to talk about this part.

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

point_heap is 0x20000728
*point_heap= 0x3421 in the code; in the Disassembly window, the corresponding address value is 0x3421.

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

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

This post is from MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list