Skip to content

Commit f551255

Browse files
committed
Add support of separate memories for heap and stack region swith the use of TWO_RAM_REGIONS define
1 parent 02b2b01 commit f551255

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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)
36+
#if defined(TOOLCHAIN_GCC_ARM) || defined(TOOLCHAIN_GCC_CR)
37+
38+
#include <errno.h>
39+
#include "stm32l4xx.h"
40+
extern uint32_t __mbed_sbrk_start;
41+
extern uint32_t __mbed_krbs_start;
42+
43+
#define STM32L4_HEAP_ALIGN 32
44+
#define STM32L4_ALIGN_UP(X, ALIGN) (((X) + (ALIGN) - 1) & ~((ALIGN) - 1))
45+
/**
46+
* The default implementation of _sbrk() (in platform/mbed_retarget.cpp) for GCC_ARM requires one-region model (heap and
47+
* stack share one region), which doesn't fit two-region model (heap and stack are two distinct regions), for example,
48+
* STM32L475xG locates heap on SRAM1 and stack on SRAM2.
49+
* Define __wrap__sbrk() to override the default _sbrk(). It is expected to get called through gcc
50+
* hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
51+
*/
52+
void *__wrap__sbrk(int incr)
53+
{
54+
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
55+
uint32_t heap_ind_old = STM32L4_ALIGN_UP(heap_ind, STM32L4_HEAP_ALIGN);
56+
uint32_t heap_ind_new = STM32L4_ALIGN_UP(heap_ind_old + incr, STM32L4_HEAP_ALIGN);
57+
58+
if (heap_ind_new > &__mbed_krbs_start) {
59+
errno = ENOMEM;
60+
return (void *) -1;
61+
}
62+
63+
heap_ind = heap_ind_new;
64+
65+
return (void *) heap_ind_old;
66+
}
67+
#endif /* GCC_ARM toolchain */
68+
#endif /* TWO_RAM_REGIONS */

0 commit comments

Comments
 (0)