Skip to content

Commit 37bfc9e

Browse files
author
Deepika
committed
Reposition heap at the end of RAM to be 4K aligned
HEAP memory should be 4K aligned for GCC newlib, with ISR stack at the end of RAM memory we loose 3K of RAM memory. This fix is for device with <16K RAM to use RAM entirely.
1 parent f24ac50 commit 37bfc9e

File tree

5 files changed

+160
-34
lines changed

5 files changed

+160
-34
lines changed

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F070RB/device/TOOLCHAIN_GCC_ARM/STM32F070XB.ld

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* Linker script to configure memory regions. */
2+
StackSize = 0x400;
23
MEMORY
34
{
45
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128k
@@ -113,6 +114,20 @@ SECTIONS
113114

114115
} > RAM
115116

117+
/* .stack section doesn't contains any symbols. It is only
118+
* used for linker to reserve space for the main stack section
119+
*/
120+
.stack (NOLOAD):
121+
{
122+
__StackLimit = .;
123+
*(.stack*);
124+
. += StackSize - (. - __StackLimit);
125+
} > RAM
126+
__StackTop = ADDR(.stack) + SIZEOF(.stack);
127+
_estack = __StackTop;
128+
__StackLimit = ADDR(.stack);
129+
PROVIDE(__stack = __StackTop);
130+
116131
.bss :
117132
{
118133
. = ALIGN(4);
@@ -129,25 +144,13 @@ SECTIONS
129144
{
130145
__end__ = .;
131146
end = __end__;
132-
*(.heap*)
147+
*(.heap*);
148+
. += (ORIGIN(RAM) + LENGTH(RAM) - .);
133149
__HeapLimit = .;
134150
} > RAM
135151

136-
/* .stack_dummy section doesn't contains any symbols. It is only
137-
* used for linker to calculate size of stack sections, and assign
138-
* values to stack symbols later */
139-
.stack_dummy (COPY):
140-
{
141-
*(.stack*)
142-
} > RAM
143-
144-
/* Set stack top to end of RAM, and stack limit move down by
145-
* size of stack_dummy section */
146-
__StackTop = ORIGIN(RAM) + LENGTH(RAM);
147-
_estack = __StackTop;
148-
__StackLimit = __StackTop - SIZEOF(.stack_dummy);
149-
PROVIDE(__stack = __StackTop);
152+
PROVIDE(__heap_size = SIZEOF(.heap));
153+
PROVIDE(__mbed_sbrk_start = ADDR(.heap));
154+
PROVIDE(__mbed_krbs_start = ADDR(.heap) + SIZEOF(.heap));
150155

151-
/* Check if data + heap + stack exceeds RAM limit */
152-
ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack")
153156
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018, STMicroelectronics
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
14+
* may be used to endorse or promote products derived from this software
15+
* without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
#include <errno.h>
29+
#include "stm32f0xx.h"
30+
extern uint32_t __mbed_sbrk_start;
31+
extern uint32_t __mbed_krbs_start;
32+
33+
/* Support heap with two-region model
34+
*
35+
* The default implementation of _sbrk() (in mbed_retarget.cpp) for GCC_ARM requires one-region
36+
* model (heap and stack share one region), which doesn't fit two-region model (heap and stack
37+
* are two distinct regions)
38+
* Hence, override _sbrk() here to support heap with two-region model.
39+
*/
40+
void *_sbrk(int incr)
41+
{
42+
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
43+
uint32_t heap_ind_old = heap_ind;
44+
uint32_t heap_ind_new = heap_ind_old + incr;
45+
46+
if (heap_ind_new > (uint32_t) &__mbed_krbs_start) {
47+
errno = ENOMEM;
48+
return (void *) -1;
49+
}
50+
51+
heap_ind = heap_ind_new;
52+
53+
return (void *) heap_ind_old;
54+
}

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F072RB/device/TOOLCHAIN_GCC_ARM/STM32F072XB.ld

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* Linker script to configure memory regions. */
2+
StackSize = 0x400;
23
MEMORY
34
{
45
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128k
@@ -113,6 +114,20 @@ SECTIONS
113114

114115
} > RAM
115116

117+
/* .stack section doesn't contains any symbols. It is only
118+
* used for linker to reserve space for the main stack section
119+
*/
120+
.stack (NOLOAD):
121+
{
122+
__StackLimit = .;
123+
*(.stack*);
124+
. += StackSize - (. - __StackLimit);
125+
} > RAM
126+
__StackTop = ADDR(.stack) + SIZEOF(.stack);
127+
_estack = __StackTop;
128+
__StackLimit = ADDR(.stack);
129+
PROVIDE(__stack = __StackTop);
130+
116131
.bss :
117132
{
118133
. = ALIGN(4);
@@ -129,25 +144,13 @@ SECTIONS
129144
{
130145
__end__ = .;
131146
end = __end__;
132-
*(.heap*)
147+
*(.heap*);
148+
. += (ORIGIN(RAM) + LENGTH(RAM) - .);
133149
__HeapLimit = .;
134150
} > RAM
135151

136-
/* .stack_dummy section doesn't contains any symbols. It is only
137-
* used for linker to calculate size of stack sections, and assign
138-
* values to stack symbols later */
139-
.stack_dummy (COPY):
140-
{
141-
*(.stack*)
142-
} > RAM
143-
144-
/* Set stack top to end of RAM, and stack limit move down by
145-
* size of stack_dummy section */
146-
__StackTop = ORIGIN(RAM) + LENGTH(RAM);
147-
_estack = __StackTop;
148-
__StackLimit = __StackTop - SIZEOF(.stack_dummy);
149-
PROVIDE(__stack = __StackTop);
152+
PROVIDE(__heap_size = SIZEOF(.heap));
153+
PROVIDE(__mbed_sbrk_start = ADDR(.heap));
154+
PROVIDE(__mbed_krbs_start = ADDR(.heap) + SIZEOF(.heap));
150155

151-
/* Check if data + heap + stack exceeds RAM limit */
152-
ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack")
153156
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018, STMicroelectronics
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
14+
* may be used to endorse or promote products derived from this software
15+
* without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
#include <errno.h>
29+
#include "stm32f0xx.h"
30+
extern uint32_t __mbed_sbrk_start;
31+
extern uint32_t __mbed_krbs_start;
32+
33+
/* Support heap with two-region model
34+
*
35+
* The default implementation of _sbrk() (in mbed_retarget.cpp) for GCC_ARM requires one-region
36+
* model (heap and stack share one region), which doesn't fit two-region model (heap and stack
37+
* are two distinct regions)
38+
* Hence, override _sbrk() here to support heap with two-region model.
39+
*/
40+
void *_sbrk(int incr)
41+
{
42+
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
43+
uint32_t heap_ind_old = heap_ind;
44+
uint32_t heap_ind_new = heap_ind_old + incr;
45+
46+
if (heap_ind_new > (uint32_t) &__mbed_krbs_start) {
47+
errno = ENOMEM;
48+
return (void *) -1;
49+
}
50+
51+
heap_ind = heap_ind_new;
52+
53+
return (void *) heap_ind_old;
54+
}

targets/TARGET_STM/mbed_rtx.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,22 @@ extern uint32_t __HeapLimit[];
137137
#endif
138138

139139
#if (defined(TARGET_STM32F070RB) || defined(TARGET_STM32F072RB))
140+
#if (defined(__GNUC__) && !defined(__CC_ARM) && !defined(__ARMCC_VERSION))
141+
extern uint32_t __StackLimit;
142+
extern uint32_t __StackTop;
143+
extern uint32_t __end__;
144+
extern uint32_t __HeapLimit;
145+
#define HEAP_START ((unsigned char*) &__end__)
146+
#define HEAP_SIZE ((uint32_t)((uint32_t) &__HeapLimit - (uint32_t) HEAP_START))
147+
#define ISR_STACK_START ((unsigned char*) &__StackLimit)
148+
#define ISR_STACK_SIZE ((uint32_t)((uint32_t) &__StackTop - (uint32_t) &__StackLimit))
149+
#endif
150+
140151
#ifdef MBED_CONF_RTOS_MAIN_THREAD_STACK_SIZE
141152
#undef MBED_CONF_RTOS_MAIN_THREAD_STACK_SIZE
142153
#endif
143154
#define MBED_CONF_RTOS_MAIN_THREAD_STACK_SIZE 3072
155+
144156
#endif
145157

146158
#endif // MBED_MBED_RTX_H

0 commit comments

Comments
 (0)