Skip to content

Commit 3593444

Browse files
author
Deepika
committed
Add support of heap memory split between 2-RAM banks.
Please note the heap address of the both the banks must not be contigious else GCC considers it to be single memory bank and does allocation across the banks, which might result into hard-fault
1 parent 719d0fb commit 3593444

File tree

3 files changed

+39
-80
lines changed

3 files changed

+39
-80
lines changed

platform/mbed_retarget.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,41 @@ extern "C" WEAK void __cxa_pure_virtual(void)
12401240
// SP. This make it compatible with RTX RTOS thread stacks.
12411241
#if defined(TOOLCHAIN_GCC_ARM)
12421242

1243+
#if defined(MBED_SPLIT_HEAP)
1244+
1245+
extern uint32_t __mbed_sbrk_start;
1246+
extern uint32_t __mbed_krbs_start;
1247+
extern uint32_t __mbed_sbrk_start_0;
1248+
extern uint32_t __mbed_krbs_start_0;
1249+
1250+
extern "C" WEAK caddr_t _sbrk(int incr)
1251+
{
1252+
static uint32_t heap = (uint32_t) &__mbed_sbrk_start_0;
1253+
static bool once = true;
1254+
uint32_t prev_heap = heap;
1255+
uint32_t new_heap = heap + incr;
1256+
1257+
/**
1258+
* If the new address is outside the first region, start allocating from the second region.
1259+
*/
1260+
if (once && (new_heap > (uint32_t) &__mbed_krbs_start_0)) {
1261+
once = false;
1262+
prev_heap = (uint32_t) &__mbed_sbrk_start;
1263+
new_heap = prev_heap + incr;
1264+
} else if (new_heap > (uint32_t) &__mbed_krbs_start) {
1265+
/**
1266+
* If the new address is outside the second region, return out-of-memory.
1267+
*/
1268+
errno = ENOMEM;
1269+
return (caddr_t) - 1;
1270+
}
1271+
1272+
heap = new_heap;
1273+
return (caddr_t) prev_heap;
1274+
}
1275+
1276+
#else
1277+
12431278
extern "C" uint32_t __end__;
12441279
extern "C" uint32_t __HeapLimit;
12451280

@@ -1264,6 +1299,7 @@ extern "C" WEAK caddr_t _sbrk(int incr)
12641299
return (caddr_t) prev_heap;
12651300
}
12661301
#endif
1302+
#endif
12671303

12681304
#if defined(TOOLCHAIN_GCC_ARM)
12691305
extern "C" void _exit(int return_code)

targets/TARGET_STM/TARGET_STM32L4/l4_retarget.c

Lines changed: 0 additions & 78 deletions
This file was deleted.

targets/targets.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4147,7 +4147,8 @@
41474147
"detect_code": ["0764"],
41484148
"macros_add": [
41494149
"MBED_TICKLESS",
4150-
"USBHOST_OTHER"
4150+
"USBHOST_OTHER",
4151+
"MBED_SPLIT_HEAP"
41514152
],
41524153
"device_has_add": [
41534154
"ANALOGOUT",
@@ -4178,7 +4179,7 @@
41784179
}
41794180
},
41804181
"detect_code": ["0468"],
4181-
"macros_add": ["USBHOST_OTHER", "TWO_RAM_REGIONS"],
4182+
"macros_add": ["USBHOST_OTHER", "MBED_SPLIT_HEAP"],
41824183
"device_has_add": [
41834184
"ANALOGOUT",
41844185
"CAN",

0 commit comments

Comments
 (0)