Skip to content

Commit 7c07141

Browse files
Marcus ChangDeepika
authored andcommitted
Expand sbrk to allocate memory from two regions
1 parent 6dbc00d commit 7c07141

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/device/TOOLCHAIN_GCC_ARM/STM32L475XX.ld

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ M_CRASH_DATA_RAM_SIZE = 0x100;
1616

1717
/* Linker script to configure memory regions. */
1818
MEMORY
19-
{
19+
{
2020
FLASH (rx) : ORIGIN = MBED_APP_START, LENGTH = MBED_APP_SIZE
2121
SRAM2 (rwx) : ORIGIN = 0x10000188, LENGTH = 32k - 0x188
2222
SRAM1 (rwx) : ORIGIN = 0x20000000, LENGTH = 96k
@@ -26,7 +26,7 @@ MEMORY
2626
* with other linker script that defines memory regions FLASH and RAM.
2727
* It references following symbols, which must be defined in code:
2828
* Reset_Handler : Entry of reset handler
29-
*
29+
*
3030
* It defines following symbols, which code can use without definition:
3131
* __exidx_start
3232
* __exidx_end
@@ -93,7 +93,7 @@ SECTIONS
9393

9494
__etext = .;
9595
_sidata = .;
96-
96+
9797
.crash_data_ram :
9898
{
9999
. = ALIGN(8);
@@ -173,6 +173,10 @@ SECTIONS
173173
.stack_dummy (COPY):
174174
{
175175
*(.stack*)
176+
. += (ORIGIN(SRAM2) + STACK_SIZE - .);
177+
__mbed_sbrk_start_0 = .;
178+
. += (ORIGIN(SRAM2) + LENGTH(SRAM2) - .);
179+
__mbed_krbs_start_0 = .;
176180
} > SRAM2
177181

178182
/* Set stack top to end of RAM, and stack limit move down by
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
******************************************************************************
3+
* @file l4_retarget.c
4+
* @author MCD Application Team
5+
* @brief CMSIS Cortex-M4 Core Peripheral Access Layer Source File for STM32L475xG
6+
******************************************************************************
7+
* @attention
8+
*
9+
* <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
10+
*
11+
* Redistribution and use in source and binary forms, with or without modification,
12+
* are permitted provided that the following conditions are met:
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
19+
* may be used to endorse or promote products derived from this software
20+
* without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
*
33+
******************************************************************************
34+
*/
35+
#if (defined(TWO_RAM_REGIONS) && defined(__GNUC__) && !defined(__CC_ARM) && !defined(__ARMCC_VERSION))
36+
#include <stdbool.h>
37+
#include <errno.h>
38+
#include "stm32l4xx.h"
39+
extern uint32_t __mbed_sbrk_start;
40+
extern uint32_t __mbed_krbs_start;
41+
extern uint32_t __mbed_sbrk_start_0;
42+
extern uint32_t __mbed_krbs_start_0;
43+
44+
/**
45+
* The default implementation of _sbrk() (in platform/mbed_retarget.cpp) for GCC_ARM only supports one region.
46+
* This implementation supports two regions by continuing the allocation to the second region after the first
47+
* one is full.
48+
* Define __wrap__sbrk() to override the default _sbrk(). It is expected to get called through gcc
49+
* hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
50+
*/
51+
void *__wrap__sbrk(int incr)
52+
{
53+
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start_0;
54+
static bool once = true;
55+
uint32_t heap_ind_old = heap_ind;
56+
uint32_t heap_ind_new = heap_ind_old + incr;
57+
58+
/**
59+
* If the new address is outside the first region, start allocating from the second region.
60+
*/
61+
if (once && (heap_ind_new >= (uint32_t)&__mbed_krbs_start_0)) {
62+
once = false;
63+
heap_ind_old = (uint32_t)&__mbed_sbrk_start;
64+
heap_ind_new = heap_ind_old + incr;
65+
/**
66+
* If the new address is outside the second region, return out-of-memory.
67+
*/
68+
} else if (heap_ind_new >= (uint32_t)&__mbed_krbs_start) {
69+
errno = ENOMEM;
70+
return (void *) - 1;
71+
}
72+
73+
heap_ind = heap_ind_new;
74+
75+
return (void *) heap_ind_old;
76+
}
77+
#endif /* GCC_ARM toolchain && TWO_RAM_REGIONS*/
78+

0 commit comments

Comments
 (0)