Skip to content

Commit

Permalink
rp2/memmap_mp.ld: Allow a board to reserve memory for the C heap.
Browse files Browse the repository at this point in the history
Since c80e7c1 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 <[email protected]>
  • Loading branch information
dpgeorge committed Apr 26, 2023
1 parent 7ea06a3 commit fe4ac49
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
12 changes: 11 additions & 1 deletion ports/rp2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions ports/rp2/memmap_mp.ld
Original file line number Diff line number Diff line change
Expand Up @@ -243,24 +243,23 @@ 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")

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 */
}

0 comments on commit fe4ac49

Please sign in to comment.