From fe4ac49d7f3beb0512545ac8e4545c76da2334ec Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 4 Apr 2023 10:36:02 +1000 Subject: [PATCH] rp2/memmap_mp.ld: Allow a board to reserve memory for the C heap. Since c80e7c14e6305e50e3b39f97172d4d8fe1214d3b changed the GC heap to use all unused RAM, there is no longer any RAM available for the traditional C heap (which is not used by default in MicroPython but may be used by C extensions). This commit adds a provision for a board to reserve RAM for the C heap, by defining MICROPY_C_HEAP_SIZE. Signed-off-by: Damien George --- ports/rp2/CMakeLists.txt | 12 +++++++++++- ports/rp2/memmap_mp.ld | 15 +++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index ab09ab5148..094031c685 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.12) +cmake_minimum_required(VERSION 3.13) # Set build type to reduce firmware size if(NOT CMAKE_BUILD_TYPE) @@ -45,6 +45,12 @@ if(NOT PICO_BOARD) string(TOLOWER ${MICROPY_BOARD} PICO_BOARD) endif() +# Set the amount of C heap, if it's not already set. +# If a board uses malloc then it must set this to at least 4096. +if(NOT MICROPY_C_HEAP_SIZE) + set(MICROPY_C_HEAP_SIZE 0) +endif() + # Enable extmod components that will be configured by extmod.cmake. # A board may also have enabled additional components. set(MICROPY_SSL_MBEDTLS ON) @@ -365,6 +371,10 @@ target_compile_options(${MICROPY_TARGET} PRIVATE -Werror ) +target_link_options(${MICROPY_TARGET} PRIVATE + -Wl,--defsym=__micropy_c_heap_size__=${MICROPY_C_HEAP_SIZE} +) + set_source_files_properties( ${PICO_SDK_PATH}/src/rp2_common/pico_double/double_math.c ${PICO_SDK_PATH}/src/rp2_common/pico_float/float_math.c diff --git a/ports/rp2/memmap_mp.ld b/ports/rp2/memmap_mp.ld index 6be05b094f..9db74ce9c9 100644 --- a/ports/rp2/memmap_mp.ld +++ b/ports/rp2/memmap_mp.ld @@ -243,19 +243,19 @@ SECTIONS } > FLASH /* stack limit is poorly named, but historically is maximum heap ptr */ - __StackLimit = ORIGIN(RAM) + LENGTH(RAM); + __StackLimit = __bss_end__ + __micropy_c_heap_size__; + + /* Define start and end of GC heap */ + __GcHeapStart = __StackLimit; /* after the C heap (sbrk limit) */ + __GcHeapEnd = ORIGIN(RAM) + LENGTH(RAM); + + /* Define memory for the C stack */ __StackOneTop = ORIGIN(SCRATCH_X) + LENGTH(SCRATCH_X); __StackTop = ORIGIN(SCRATCH_Y) + LENGTH(SCRATCH_Y); __StackOneBottom = __StackOneTop - SIZEOF(.stack1_dummy); __StackBottom = __StackTop - SIZEOF(.stack_dummy); - /* Define start and end of GC heap */ - __GcHeapStart = __bss_end__; - __GcHeapEnd = __StackLimit; PROVIDE(__stack = __StackTop); - /* Check if data + heap + stack exceeds RAM limit */ - ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed") - /* Check GC heap is at least 128 KB */ /* On a RP2040 using all SRAM this should always be the case. */ ASSERT((__GcHeapEnd - __GcHeapStart) > 128*1024, "GcHeap is too small") @@ -263,4 +263,3 @@ SECTIONS ASSERT( __binary_info_header_end - __logical_binary_start <= 256, "Binary info must be in first 256 bytes of the binary") /* todo assert on extra code */ } -