Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STM32H7: Don't use DTCM memory for heap #396

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cmsis/device/rtos/TOOLCHAIN_GCC_ARM/mbed_boot_gcc_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ static osMutexId_t env_mutex_id;
static mbed_rtos_storage_mutex_t env_mutex_obj;
static osMutexAttr_t env_mutex_attr;

// Stack symbols from linker script
extern uint32_t __StackLimit;
extern uint32_t __StackTop;

// Heap symbols from linker script
#if defined(MBED_SPLIT_HEAP)
extern uint32_t __mbed_sbrk_start;
extern uint32_t __mbed_krbs_start;
extern uint32_t __mbed_sbrk_start_0;
extern uint32_t __mbed_krbs_start_0;
#else
extern uint32_t __end__;
extern uint32_t __HeapLimit;
#endif

extern void __libc_init_array(void);

Expand All @@ -45,8 +55,15 @@ void software_init_hook(void)
{
mbed_stack_isr_start = (unsigned char *) &__StackLimit;
mbed_stack_isr_size = (uint32_t) &__StackTop - (uint32_t) &__StackLimit;
#if defined(MBED_SPLIT_HEAP)
mbed_heap_start = (unsigned char *) &__mbed_sbrk_start;
mbed_heap_size = (uint32_t) &__mbed_krbs_start - (uint32_t) &__mbed_sbrk_start;
mbed_heap_start_0 = (unsigned char *) &__mbed_sbrk_start_0;
mbed_heap_size_0 = (uint32_t) &__mbed_krbs_start_0 - (uint32_t) &__mbed_sbrk_start_0;
#else
mbed_heap_start = (unsigned char *) &__end__;
mbed_heap_size = (uint32_t) &__HeapLimit - (uint32_t) &__end__;
#endif

mbed_init();
mbed_rtos_start();
Expand Down
5 changes: 5 additions & 0 deletions cmsis/device/rtos/include/mbed_boot.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ extern "C" {
extern unsigned char *mbed_heap_start;
extern uint32_t mbed_heap_size;

#if defined(MBED_SPLIT_HEAP)
extern unsigned char *mbed_heap_start_0;
extern uint32_t mbed_heap_size_0;
#endif

/* Stack limits */
extern unsigned char *mbed_stack_isr_start;
extern uint32_t mbed_stack_isr_size;
Expand Down
2 changes: 1 addition & 1 deletion cmsis/device/rtos/source/mbed_boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* - Value INITIAL_SP is ignored
*
* GCC Memory layout :
* - Heap explicitly placed in linker script (*.ld file) and heap start (__end___) and heap end (__HeapLimit) should be defined in linker script
* - Heap explicitly placed in linker script (*.ld file) and heap start (__end___) and heap end (__HeapLimit) should be defined in linker script (or __mbed_sbrk_start/__mbed_sbrk_start_0 and __mbed_krbs_start/__mbed_krbs_start_0 for split heap targets)
* - Interrupt stack placed in linker script **.ld file) and stack start (__StackTop) and stack end (__StackLimit) should be defined in linker script
*
* ARM Memory layout :
Expand Down
5 changes: 5 additions & 0 deletions platform/source/mbed_retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ extern const char __stderr_name[] = "/stderr";
unsigned char *mbed_heap_start = 0;
uint32_t mbed_heap_size = 0;

#if defined(MBED_SPLIT_HEAP)
unsigned char *mbed_heap_start_0 = 0;
uint32_t mbed_heap_size_0 = 0;
#endif

#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY

/* newlib has the filehandle field in the FILE struct as a short, so
Expand Down
21 changes: 17 additions & 4 deletions platform/source/mbed_sdk_boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "mbed_error.h"
#include "mbed_toolchain.h"
#include "mbed_boot.h"
#include <stdlib.h>
#include <stdint.h>
#include "cmsis.h"
Expand All @@ -28,10 +29,6 @@
*/
#if !defined(MBED_CONF_RTOS_PRESENT)

/* Heap limits - only used if set */
extern unsigned char *mbed_heap_start;
extern uint32_t mbed_heap_size;

/* Stack limits */
unsigned char *mbed_stack_isr_start = 0;
uint32_t mbed_stack_isr_size = 0;
Expand Down Expand Up @@ -154,17 +151,33 @@ const uint32_t os_cb_sections[] __attribute__((section(".rodata"))) = {};

extern uint32_t __StackLimit;
extern uint32_t __StackTop;

#if defined(MBED_SPLIT_HEAP)
extern uint32_t __mbed_sbrk_start;
extern uint32_t __mbed_krbs_start;
extern uint32_t __mbed_sbrk_start_0;
extern uint32_t __mbed_krbs_start_0;
#else
extern uint32_t __end__;
extern uint32_t __HeapLimit;
#endif

extern int __real_main(void);

void software_init_hook(void)
{
mbed_stack_isr_start = (unsigned char *) &__StackLimit;
mbed_stack_isr_size = (uint32_t) &__StackTop - (uint32_t) &__StackLimit;

#if defined(MBED_SPLIT_HEAP)
mbed_heap_start = (unsigned char *) &__mbed_sbrk_start;
mbed_heap_size = (uint32_t) &__mbed_krbs_start - (uint32_t) &__mbed_sbrk_start;
mbed_heap_start_0 = (unsigned char *) &__mbed_sbrk_start_0;
mbed_heap_size_0 = (uint32_t) &__mbed_krbs_start_0 - (uint32_t) &__mbed_sbrk_start_0;
#else
mbed_heap_start = (unsigned char *) &__end__;
mbed_heap_size = (uint32_t) &__HeapLimit - (uint32_t) &__end__;
#endif

mbed_init();
software_init_hook_rtos();
Expand Down
10 changes: 1 addition & 9 deletions rtos/tests/TESTS/mbed_rtos/heap_and_stack/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "greentea-client/test_env.h"
#include "utest/utest.h"
#include "unity/unity.h"
#include "mbed_boot.h"

using utest::v1::Case;

Expand All @@ -39,18 +40,9 @@ static const int test_timeout = 30;
// Malloc fill pattern
#define MALLOC_FILL 0x55

extern unsigned char *mbed_heap_start;
extern uint32_t mbed_heap_size;
extern unsigned char *mbed_stack_isr_start;
extern uint32_t mbed_stack_isr_size;

#if defined(TOOLCHAIN_GCC_ARM) && defined(MBED_SPLIT_HEAP)
extern uint32_t __mbed_sbrk_start_0;
extern uint32_t __mbed_krbs_start_0;
unsigned char *mbed_heap_start_0 = (unsigned char *) &__mbed_sbrk_start_0;;
uint32_t mbed_heap_size_0 = (uint32_t) &__mbed_krbs_start_0 - (uint32_t) &__mbed_sbrk_start_0;
#endif

struct linked_list {
linked_list *next;
uint8_t data[MALLOC_TEST_SIZE];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,14 @@ SECTIONS
_ebss = .;
} > SRAM

.heap (COPY):
/* Check if data + stack will exceed SRAM limit */
ASSERT(_ebss < ORIGIN(SRAM) + LENGTH(SRAM) - MBED_CONF_TARGET_BOOT_STACK_SIZE, "region SRAM does not have enough space for boot stack size")

.heap_0 (NOLOAD):
{
__end__ = .;
PROVIDE(end = .);
PROVIDE(__mbed_sbrk_start = .);
*(.heap*)
PROVIDE(__mbed_sbrk_start_0 = .);
. = ORIGIN(SRAM) + LENGTH(SRAM) - MBED_CONF_TARGET_BOOT_STACK_SIZE;
PROVIDE(__mbed_krbs_start = .);
__HeapLimit = .;
PROVIDE(__mbed_krbs_start_0 = .);
} > SRAM

/* .stack_dummy section doesn't contains any symbols. It is only
Expand All @@ -213,9 +212,6 @@ SECTIONS
__StackLimit = __StackTop - MBED_CONF_TARGET_BOOT_STACK_SIZE;
PROVIDE(__stack = __StackTop);

/* Check if data + heap + stack exceeds SRAM limit */
ASSERT(__StackLimit >= __HeapLimit, "region SRAM overflowed with stack")

/* Ethernet DMA descriptors should be at the start of SRAM_D2 because they need an MPU region
and because the CM4 and CM7 have to agree on their location.*/
.eth_descriptors (NOLOAD) : {
Expand All @@ -230,11 +226,11 @@ SECTIONS
*(.EthBuffers)
} >SRAM_D2

/* Use the rest of DTCM as additional heap */
.heap_0 (COPY):
/* Use SRAM_D2 as additional heap */
.heap (NOLOAD):
{
PROVIDE(__mbed_sbrk_start_0 = .);
. += (ORIGIN(SRAM_DTC) + LENGTH(SRAM_DTC) - .);
PROVIDE(__mbed_krbs_start_0 = .);
} > SRAM_DTC
PROVIDE(__mbed_sbrk_start = .);
. += (ORIGIN(SRAM_D2) + LENGTH(SRAM_D2) - .);
PROVIDE(__mbed_krbs_start = .);
} > SRAM_D2
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,15 @@ SECTIONS
_ebss = .;
} > SRAM_D2

/* Check if data + stack will exceed SRAM limit */
ASSERT(_ebss < ORIGIN(SRAM_D2) + LENGTH(SRAM_D2) - MBED_CONF_TARGET_BOOT_STACK_SIZE, "region SRAM does not have enough space for boot stack size")

.heap (COPY):
{
__end__ = .;
PROVIDE(end = .);
PROVIDE(__mbed_sbrk_start = .);
*(.heap*)
. = ORIGIN(SRAM_D2) + LENGTH(SRAM_D2) - MBED_CONF_TARGET_BOOT_STACK_SIZE;
PROVIDE(__mbed_krbs_start = .);
__HeapLimit = .;
} > SRAM_D2
/* .stack_dummy section doesn't contains any symbols. It is only
Expand All @@ -207,9 +208,6 @@ SECTIONS
__StackLimit = __StackTop - MBED_CONF_TARGET_BOOT_STACK_SIZE;
PROVIDE(__stack = __StackTop);

/* Check if data + heap + stack exceeds SRAM_D2 limit */
ASSERT(__StackLimit >= __HeapLimit, "region SRAM_D2 overflowed with stack")

/* Put crash data in the otherwise unused D3 SRAM */
.crash_data_ram :
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ SECTIONS
_ebss = .;
} > SRAM

/* Check if data + stack will exceed SRAM limit */
ASSERT(_ebss < ORIGIN(SRAM) + LENGTH(SRAM) - MBED_CONF_TARGET_BOOT_STACK_SIZE, "region SRAM does not have enough space for boot stack size")

.heap (COPY):
{
__end__ = .;
PROVIDE(end = .);
PROVIDE(__mbed_sbrk_start = .);
*(.heap*)
. = ORIGIN(SRAM) + LENGTH(SRAM) - MBED_CONF_TARGET_BOOT_STACK_SIZE;
PROVIDE(__mbed_krbs_start = .);
__HeapLimit = .;
} > SRAM

Expand Down Expand Up @@ -230,12 +230,4 @@ SECTIONS
. = ALIGN(8);
__CRASH_DATA_RAM_END__ = .; /* Define a global symbol at data end */
} > SRAM_D3

/* Use the rest of DTCM as additional heap */
.heap_0 (COPY):
{
PROVIDE(__mbed_sbrk_start_0 = .);
. += (ORIGIN(SRAM_DTC) + LENGTH(SRAM_DTC) - .);
PROVIDE(__mbed_krbs_start_0 = .);
} > SRAM_DTC
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,14 @@ SECTIONS
_ebss = .;
} > SRAM

.heap (COPY):
/* Check if data + stack will exceed SRAM limit */
ASSERT(_ebss < ORIGIN(SRAM) + LENGTH(SRAM) - MBED_CONF_TARGET_BOOT_STACK_SIZE, "region SRAM does not have enough space for boot stack size")

.heap_0 (NOLOAD):
{
__end__ = .;
PROVIDE(end = .);
PROVIDE(__mbed_sbrk_start = .);
*(.heap*)
PROVIDE(__mbed_sbrk_start_0 = .);
. = ORIGIN(SRAM) + LENGTH(SRAM) - MBED_CONF_TARGET_BOOT_STACK_SIZE;
PROVIDE(__mbed_krbs_start = .);
__HeapLimit = .;
PROVIDE(__mbed_krbs_start_0 = .);
} > SRAM

/* .stack_dummy section doesn't contains any symbols. It is only
Expand All @@ -200,9 +199,6 @@ SECTIONS
__StackLimit = __StackTop - MBED_CONF_TARGET_BOOT_STACK_SIZE;
PROVIDE(__stack = __StackTop);

/* Check if data + heap + stack exceeds SRAM limit */
ASSERT(__StackLimit >= __HeapLimit, "region SRAM overflowed with stack")

/* Put crash data in the otherwise unused D3 SRAM */
.crash_data_ram :
{
Expand All @@ -229,11 +225,11 @@ SECTIONS
*(.EthBuffers)
} >SRAM_AXI

/* Use the rest of DTCM as additional heap */
.heap_0 (COPY):
/* Use SRAM_AXI as additional heap */
.heap (NOLOAD):
{
PROVIDE(__mbed_sbrk_start_0 = .);
. += (ORIGIN(SRAM_DTC) + LENGTH(SRAM_DTC) - .);
PROVIDE(__mbed_krbs_start_0 = .);
} > SRAM_DTC
PROVIDE(__mbed_sbrk_start = .);
. += (ORIGIN(SRAM_AXI) + LENGTH(SRAM_AXI) - .);
PROVIDE(__mbed_krbs_start = .);
} > SRAM_AXI
}
6 changes: 2 additions & 4 deletions targets/targets.json5
Original file line number Diff line number Diff line change
Expand Up @@ -3455,8 +3455,7 @@
}
},
"macros_add": [
"CORE_CM7",
"MBED_SPLIT_HEAP"
"CORE_CM7"
]
},
"NUCLEO_H745ZI_Q": {
Expand Down Expand Up @@ -3522,8 +3521,7 @@
}
},
"macros_add": [
"CORE_CM7",
"MBED_SPLIT_HEAP"
"CORE_CM7"
]
},
"MCU_STM32H747xI_CM4": {
Expand Down