Stack and heap

Whether one needs to initialize the stack on the MCU that one is programming for depends on how low-level one wishes to go. When using the C-runtime (on AVR: avr-libc), the runtime will handle initializing the stack and other details by letting the linker place naked code into init sections, for example specified by:

__attribute__ ((naked, used, section (".init3")))

Preceding the execution of any of our own application code.

The standard RAM layout on AVR is to start with the .data variables at the beginning of the RAM, followed by .bss. The stack is started from the opposite site of the RAM, growing towards the beginning. There will be room left between the end of the .bss section and the end of the stack illustrated as follows:

Since the stack grows depending on the depth of the function calls in the application being run, it is hard to say how much space is available. Some MCUs allow one to use external RAM as well, which would be a possible location for the heap as follows:

The AVR Libc library implements a malloc() memory allocator routine, optimized for the AVR architecture. Using it, one can implement one's own new and delete functionality as well—if one so desiressince the AVR toolchain does not implement either.

In order to use external memory with an AVR MCU for heap storage, one would have to make sure that the external memory has been initialized, after which the address space becomes available to malloc(). The start and end of the heap space is hereby defined by these global variables:

char * __malloc_heap_start 
char * __malloc_heap_end 

The AVR documentation has the following advice regarding adjusting the heap:

If the heap is going to be moved to external RAM, __malloc_heap_end must be adjusted accordingly. This can either be done at runtime, by writing directly to this variable, or it can be done automatically at link-time, by adjusting the value of the symbol __heap_end.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.2.122