# External SRAM problem

**URL:** <https://forums.freertos.org/t/external-sram-problem/4069>\
**Category:** Kernel\
**Created:** [March 2, 2014, 7:42am UTC](https://forums.freertos.org/t/external-sram-problem/4069 "2014-03-02T07:42:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 2, 2014, 7:42am UTC](https://forums.freertos.org/t/external-sram-problem/4069/1 "2014-03-02T07:42:00Z")

</div>

**[pefo77](https://sourceforge.net/u/pefo77/)** wrote on [Sunday, March 02, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/b35f0426/#28d5):

Hello,

i have problem with running RTOS and external SRAM.

If i use only internal SRAM all works fine. When i configure external  
SRAM (i can send my ld file) then program don’t work.  
I use Atmel Studio 6.1, ASF 3.14.0 and freertos 7.3.0, gcc 4.7.3

[sram.ld]  
/\* Memory Spaces Definitions \*/  
MEMORY  
{  
rom (rx) : ORIGIN = 0x00400000, LENGTH = 0x00100000  
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00020000  
ram\_ext (rwxa) : ORIGIN = 0x60000000, LENGTH = 0x00100000   
}

/\* The stack size used by the application. NOTE: you need to adjust according to your application. \*/  
**stack\_size** = DEFINED( **stack\_size** ) ? **stack\_size** : 0x019000;  
**ram\_end** = ORIGIN(ram\_ext) + LENGTH(ram\_ext) - 4;

SECTIONS  
{  
.text :  
{  
// deleted lines  
// same in SRAM config  
} \> rom

```
PROVIDE_HIDDEN (__exidx_start = .);
.ARM.exidx :
{
  *(.ARM.exidx* .gnu.linkonce.armexidx.*)
} > rom
PROVIDE_HIDDEN (__exidx_end = .);

. = ALIGN(4);
_etext = .;

/* stack section */
.stack (NOLOAD):
{
    . = ALIGN(8);
    _sstack = .;
    . = . + __stack_size__ ;
    . = ALIGN(8);
    _estack = .;
} > ram

/* free ram section (for dma buffers, etc) */
.free_ram ():
{
    . = ALIGN(4);
    _sfree_ram = .;
} > ram

.relocate : AT (_etext)
{
    . = ALIGN(4);
    _srelocate = .;
    *(.ramfunc .ramfunc.*);
    *(.data .data.*);
    . = ALIGN(4);
    _erelocate = .;
} > ram_ext

/* .bss section which is used for uninitialized data */
.bss (NOLOAD) :
{
    . = ALIGN(4);
    _sbss = . ;
    _szero = .;
    *(.bss .bss.*)
    *(COMMON)
    . = ALIGN(4);
    _ebss = . ;
    _ezero = .;
} > ram_ext

. = ALIGN(4);
_end = . ;

```

}

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 2, 2014, 10:23am UTC](https://forums.freertos.org/t/external-sram-problem/4069/2 "2014-03-02T10:23:40Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Sunday, March 02, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/b35f0426/#3998):

What does “not work” mean? You can’t program the chip, or main() is not called, or main() is called but the scheduler does not start, or the scheduler does start but context switches don’t happen, or one of many other possibilities?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 2, 2014, 11:19am UTC](https://forums.freertos.org/t/external-sram-problem/4069/3 "2014-03-02T11:19:38Z")

</div>

**[pefo77](https://sourceforge.net/u/pefo77/)** wrote on [Sunday, March 02, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/b35f0426/#8991):

Hello,

scheduler start, but don’t run any task. When i move xHeap (heap4.c) to internal SRAM, tasks is working.

same behavior is described here: [http://www.freertos.org/FreeRTOS\_Support\_Forum\_Archive/May\_2012/freertos\_Put\_heap\_in\_external\_memory\_for\_STM32\_5243508.html](http://www.freertos.org/FreeRTOS_Support_Forum_Archive/May_2012/freertos_Put_heap_in_external_memory_for_STM32_5243508.html)

Peter.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [March 2, 2014, 1:50pm UTC](https://forums.freertos.org/t/external-sram-problem/4069/4 "2014-03-02T13:50:28Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Sunday, March 02, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/b35f0426/#8991/8433):

Does the chip allow the stack to be allocated in external RAM?

If so, what happens if you allocate and use external SRAM before the  
scheduler is started? For example:

```

void main( void )
{
char *c;
long l, lError = pdFALSE;

    /* Allocate a buffer. */
    c = pvPortMalloc( 1000 );

    /* Fill the buffer with known values. */
    for( l = 0; l < 1000; l++ )
    {
        c[l] = ( char ) l;
    }

    /* Check the value was actually written. */
    for( l = 0; l < 1000; l++ )
    {
        if( c[l] != ( char ) l )
        {
            lError = pdTRUE;
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 2, 2014, 2:09pm UTC](https://forums.freertos.org/t/external-sram-problem/4069/5 "2014-03-02T14:09:01Z")

</div>

**[pefo77](https://sourceforge.net/u/pefo77/)** wrote on [Sunday, March 02, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/b35f0426/#eb10):

Hi,

no error i make selft-test after initialization SRAM (2 passes),

sysclk\_get\_main\_hz: 240000000  
sysclk\_get\_cpu\_hz: 120000000  
sysclk\_get\_peripheral\_hz: 120000000  
SystemCoreClock: 120000000  
SRAM _sram\_init\_status\_flag_: 0  
size of unsigned long: 4  
size of double: 8  
size of float: 4  
size of int: 4  
size of long: 4  
size of ptrdiff\_t: 4

lError: 0

If the xHeap is in ext SRAM , program hang up in HardFault\_Handler - randomly (one or two task calls) - i think there is problem with timing SMC & EBI, and context switching. I have fully funcitonal program using ext. SRAM (without FreeRTOS).

Task: 1

Task: 2

Error: RTOS is down! \<---- HardFault\_Handler called here

Peter.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [March 2, 2014, 2:18pm UTC](https://forums.freertos.org/t/external-sram-problem/4069/6 "2014-03-02T14:18:55Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Sunday, March 02, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/b35f0426/#eb10/7273):

> If the xHeap is in ext SRAM , program hang up in HardFault\_Handler -

I’m afraid I don’t know why that would be. It could be a timing issue.  
If the crash happens in the context switch code then it might be that  
some additional barrier instructions would fix it.

> I have fully funcitonal program using ext. SRAM (without FreeRTOS).

Is that with the stack in SRAM too?

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 2, 2014, 3:04pm UTC](https://forums.freertos.org/t/external-sram-problem/4069/7 "2014-03-02T15:04:27Z")

</div>

**[pefo77](https://sourceforge.net/u/pefo77/)** wrote on [Sunday, March 02, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/b35f0426/#eb10/7273/4c2a):

Memory map is same, stack + some dma buffers is in internal sram. Relocation sector + heap is in ext. Sram.

Peter.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 2, 2014, 3:13pm UTC](https://forums.freertos.org/t/external-sram-problem/4069/8 "2014-03-02T15:13:47Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Sunday, March 02, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/b35f0426/#c8f0):

> stack + some dma buffers is in internal sram

The two cases are not the same then.

If you put the FreeRTOS heap in external RAM then the stacks used by the tasks will also be in external RAM.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 2, 2014, 3:34pm UTC](https://forums.freertos.org/t/external-sram-problem/4069/9 "2014-03-02T15:34:57Z")

</div>

**[pefo77](https://sourceforge.net/u/pefo77/)** wrote on [Sunday, March 02, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/b35f0426/#c8f0/b3e4):

It’s true, because freertos have own stack in heap, but main stack is internal sram (see ld file). I must try to force freertos to use main stack in internal sram and heap in ext sram (for malloc and pvPortMalloc).

Peter

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 2, 2014, 8:05pm UTC](https://forums.freertos.org/t/external-sram-problem/4069/10 "2014-03-02T20:05:17Z")

</div>

**[pefo77](https://sourceforge.net/u/pefo77/)** wrote on [Sunday, March 02, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/b35f0426/#eb10/7273/a10e):

Some dmb, dsb and isb helps, but not solve it (task’s is executed more times before hangs in HardFault), i going to try v8.0.0

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 2, 2014, 8:51pm UTC](https://forums.freertos.org/t/external-sram-problem/4069/11 "2014-03-02T20:51:05Z")

</div>

**[pefo77](https://sourceforge.net/u/pefo77/)** wrote on [Sunday, March 02, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/b35f0426/#5508):

After many tests i solved some hanging issue, i use 50ns ext. SRAM, timing waves is set precisly to fit it (measured), SRAM woking well without erros, but if i start use FreeRTOS with heap mapped to ext. SRAM, programs hangs in HardFault\_Handler. When i set the SMC\_CYCLE\_NWE\_CYCLE, SMC\_CYCLE\_NRD\_CYCLE two ticks longer, then stuff start working in FreeRTOS, maybe MATRIX arbitration settings helps too.

Peter.
