Skip to content

Commit 126c261

Browse files
committed
ports/stm32: Use dynamic heap with proper .noinit section handling.
Replace the 80KB static heap with dynamic heap allocation by properly handling the .noinit section in the linker script. Zephyr kernel stacks are placed in .noinit, and the heap now starts after this section. This reduces BSS from 179KB to 97KB when threading is enabled, and has zero overhead when threading is disabled (.noinit is empty). Changes: - Add .noinit section to common_bss_heap_stack.ld with _enoinit symbol - Update stm32f429.ld to use _enoinit for _heap_start - Remove static 80KB heap array from main.c, use MICROPY_HEAP_START/END
1 parent c4cc5d2 commit 126c261

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

ports/stm32/boards/common_bss_heap_stack.ld

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
_ebss = .;
1212
} >RAM
1313

14+
/* Non-initialized data section (Zephyr kernel stacks when threading enabled) */
15+
.noinit (NOLOAD) :
16+
{
17+
. = ALIGN(4);
18+
*(.noinit*)
19+
. = ALIGN(4);
20+
_enoinit = .;
21+
} >RAM
22+
1423
/* This is to define the start of the heap, and make sure there is a minimum size */
1524
.heap :
1625
{

ports/stm32/boards/stm32f429.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ _sstack = _estack - 16K; /* tunable */
2626
/* RAM extents for the garbage collector */
2727
_ram_start = ORIGIN(RAM);
2828
_ram_end = ORIGIN(RAM) + LENGTH(RAM);
29-
_heap_start = _ebss; /* heap starts just after statically allocated memory */
29+
_heap_start = _enoinit; /* heap starts after .bss and .noinit sections */
3030
_heap_end = _sstack;
3131

3232
/* Filesystem cache in RAM, and storage in flash */

ports/stm32/main.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,6 @@ static bool init_sdcard_fs(void) {
312312
#endif
313313

314314
#if MICROPY_ZEPHYR_THREADING
315-
// Static heap for Zephyr threading builds
316-
// Thread stacks are now dynamically allocated from this heap (not pre-allocated in BSS)
317-
// Heap size increased to 80KB to accommodate both Python objects and thread stacks
318-
// NUCLEO_F429ZI has 192KB RAM total.
319-
// Previous: 42KB heap + 40KB pre-allocated stacks in BSS = 82KB for MicroPython use
320-
// Current: 80KB heap with dynamic stack allocation = same effective capacity
321-
#define MICROPY_HEAP_SIZE (80 * 1024)
322-
static char heap[MICROPY_HEAP_SIZE];
323-
324315
// Zephyr threading entry point - called by Zephyr kernel after z_cstart()
325316
// This function runs in z_main_thread context after kernel initialization
326317
void micropython_main_thread_entry(void *p1, void *p2, void *p3) {
@@ -362,8 +353,8 @@ void micropython_main_thread_entry(void *p1, void *p2, void *p3) {
362353
size_t stack_size = z_main_thread.stack_info.size;
363354
mp_cstack_init_with_top(stack_top, stack_size);
364355

365-
// GC init with static heap (avoids overlap with thread stacks in .noinit section)
366-
gc_init(heap, heap + sizeof(heap));
356+
// GC init (linker script places .noinit after .bss, so _heap_start is safe)
357+
gc_init(MICROPY_HEAP_START, MICROPY_HEAP_END);
367358

368359
// Threading init - Phase 2 (allocate main thread on GC heap)
369360
// Requires GC to be initialized for m_new_obj() heap allocation

0 commit comments

Comments
 (0)