cancel
Showing results for 
Search instead for 
Did you mean: 

Configuring linker to use external SRAM for stack and heap space

g mcclean
Associate II
Posted on February 01, 2017 at 15:32

I am new to the STM32 and struggling to configure external SRAM for stack and heap space.

I have a custom board based on an STMF103 which is defined using STM32CubeMX with external SRAM.

Using sw4stm32 I can access SRAM as memory mapped memory starting at 0x68000000 from my application so the FSMC and memory timings appear to be fine. 

Looking at the application notes I should be able to use the SRAM for heap and stack space and the notes give examples for all toolchains except sw4stm32. Generally, the application note suggest that the toolchain will included a custom linker file for this type of configuration. This file does not seem to exist within sw4stm32.

I understand I should be able to modify my exiting STM32F103ZETx_FLASH.ld but my attempts don't seem to have any effect.

Does anyone know how to achieve this or have I missed an application note somewhere?

#linker #heap #sram #stack #stm-32
7 REPLIES 7
Posted on February 01, 2017 at 15:43

I don't Eclipse/sw4stm32 but unless it messes with the linker script it should be similar to any other gcc-based example. Have example for atollic?

Alternatively, post your existing linker script.

JW

g mcclean
Associate II
Posted on February 01, 2017 at 16:18

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6mN&d=%2Fa%2F0X0000000bvF%2F6La8YxwlLF8.dlIWml1oixBAqhSv54kG.bEe7lMWlGk&asPdf=false
Posted on February 01, 2017 at 17:17

Stack is usually initialized in startup code (.s file, you should be able to locate it and find how it';s done), heap in the standard C library (this is tricky as there are variants around, toolchain dependent). Both these initializations expect to be provided the needed values (through variables declared as extern) by the linker.

Thus, in linker you need to define the required symbols (names of said extern variables) and assign them appropriate values. In the linker script you gave above it appears that

_estack = 0x20010000;    /* end of RAM */

provides the value to initialize stack and

   . = ALIGN(8);

    PROVIDE ( end = . );

    PROVIDE ( _end = . );

value(s) to initialize heap.

The changes you've made appear to be roughly what's needed. Note that F(S)MC has to be properly initialized (including respective initialization of GPIO) before you use external stack. This may prove tricky if not written entirely in asm, so you may want to leave stack in internal memory and change SP manually only after the initialization.

Btw., stack in external memory is generally a bad idea performance-wise; if you feel you run out of internal memory due to excessive stack size, you should consider changing strategy and using heap-allocated variables or manually-generated-data-stack instead of using large non-static local variables.

JW

Posted on February 01, 2017 at 19:13

Agreed you don't want the stack in external memory, it is dog slow, and will impact everything you run, negatively.

>>

This is only my second week with the STM32 so please excuse any stupid comments on my part.

Not sure the use of external memory is unique to STM32 parts.

The key here is that you must have code to initialize the external interface (controller, pins, etc) before it can be used. This is typically done in code called by SystemInit(). The code *must* match your own hardware design, so don't assume code written for other boards will get the job done. Review and test the external memory before trying to use it via the linker. ie use pointers to access it after you have configured it correctly. Once you have that done you can use load regions or sections within the linker script to direct content into it. The startup code will be expected to zero or copy initial data into the memory, you might have to add additional code to achieve this.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 02, 2017 at 11:04

Thank you, you answered many of the questions that were at the back of my mind and more specifically what STM32CubeMX does and does not do for you.

I had not considered the performance implications of using SRAM for the stack and I don't have any application reason to actually relocate the stack it had just put into my mind by something I read and stuck there :)

Since I am pushed for time on this project and only have large not complex data structures to manage I will probably just do it directly and look at implementing the heap as a side project at a later date.

Again thank you for your help.

Posted on February 02, 2017 at 11:20

I guess I am biased by my background which is working on systems that either have full speed memory access, don't support using external stack/heap memory or run at such a low a speed memory performance is never an issue.

But you are right I should have given it more thought :)

Between your comments and those of JW I think I have enough to work it out.

Thank you.

zhao pengpeng
Associate
Posted on April 07, 2017 at 09:39

Hi , do you had get it done? I have a problem like this. In my stm32f7 lcd app I defined one big array near 2M: u16 ltdc_lcd_framebuf[1280][800] __attribute__((at(0xC0000000)));//Buf in SDRAM     And ld get error:`.bss' will not fi

 

t in region `RAM'   I don't know why it not fit in region RAM I just defined it in SDRAM, could you give me some suggestion about modify linker script just like 'Set memory bank area and size if external memory is used.' .  THANK YOU!