Skip to content

Commit c80e7c1

Browse files
cpottle9dpgeorge
authored andcommitted
rp2: Allocate GC heap from unused RAM.
Borrowing an idea from the mimxrt port (also stm32 port): in the loader input file memmap_mp.ld calculate __GcHeapStart and __GcHeapEnd as the unused RAM. Then in main.c use these addresses as arguments to gc_init(). The benefits of this change are: 1) When libraries are added or removed in the future changing BSS usage, main.c's sizing of the GC heap does not need to be changed. 2) Currently these changes make the GC area about 30 KBytes larger, eg on PICO_W the GC heap increases from 166016 to 192448 bytes. Without that change this RAM would never get used. 3) If someone wants to disable one or more SRAM blocks on the RP2040 to reduce power consumption it will be easy: just change the MEMORY section in memmap_mp.ld. For instance to not use SRAM2 and SRAM3 change it to: MEMORY { FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 2048k RAM(rwx) : ORIGIN = 0x21000000, LENGTH = 128k SCRATCH_X(rwx) : ORIGIN = 0x20040000, LENGTH = 4k SCRATCH_Y(rwx) : ORIGIN = 0x20041000, LENGTH = 4k } Then to turn off clocks for SRAM2 and SRAM3 from MicroPython, set the appropriate bits in WAKE_EN0 and SLEEP_EN0. Tested by running the firmware.uf2 file on PICO_W and displaying micropython.mem_info(). Confirmed GC total size approximately matched the size calculated by the loader. Signed-off-by: cpottle9 <cpottle9@outlook.com>
1 parent f80d040 commit c80e7c1

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

ports/rp2/main.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,8 @@
5959
#include "lib/cyw43-driver/src/cyw43.h"
6060
#endif
6161

62-
#ifndef MICROPY_GC_HEAP_SIZE
63-
#if MICROPY_PY_LWIP
64-
#define MICROPY_GC_HEAP_SIZE 166 * 1024
65-
#else
66-
#define MICROPY_GC_HEAP_SIZE 192 * 1024
67-
#endif
68-
#endif
69-
7062
extern uint8_t __StackTop, __StackBottom;
71-
__attribute__((section(".uninitialized_bss"))) static char gc_heap[MICROPY_GC_HEAP_SIZE];
63+
extern uint8_t __GcHeapStart, __GcHeapEnd;
7264

7365
// Embed version info in the binary in machine readable form
7466
bi_decl(bi_program_version_string(MICROPY_GIT_TAG));
@@ -118,7 +110,7 @@ int main(int argc, char **argv) {
118110
// Initialise stack extents and GC heap.
119111
mp_stack_set_top(&__StackTop);
120112
mp_stack_set_limit(&__StackTop - &__StackBottom - 256);
121-
gc_init(&gc_heap[0], &gc_heap[MP_ARRAY_SIZE(gc_heap)]);
113+
gc_init(&__GcHeapStart, &__GcHeapEnd);
122114

123115
#if MICROPY_PY_LWIP
124116
// lwIP doesn't allow to reinitialise itself by subsequent calls to this function

ports/rp2/memmap_mp.ld

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,18 @@ SECTIONS
248248
__StackTop = ORIGIN(SCRATCH_Y) + LENGTH(SCRATCH_Y);
249249
__StackOneBottom = __StackOneTop - SIZEOF(.stack1_dummy);
250250
__StackBottom = __StackTop - SIZEOF(.stack_dummy);
251+
/* Define start and end of GC heap */
252+
__GcHeapStart = __bss_end__;
253+
__GcHeapEnd = __StackLimit;
251254
PROVIDE(__stack = __StackTop);
252255

253256
/* Check if data + heap + stack exceeds RAM limit */
254257
ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed")
255258

259+
/* Check GC heap is at least 128 KB */
260+
/* On a RP2040 using all SRAM this should always be the case. */
261+
ASSERT((__GcHeapEnd - __GcHeapStart) > 128*1024, "GcHeap is too small")
262+
256263
ASSERT( __binary_info_header_end - __logical_binary_start <= 256, "Binary info must be in first 256 bytes of the binary")
257264
/* todo assert on extra code */
258265
}

0 commit comments

Comments
 (0)