Skip to content

Commit 3d9a3e8

Browse files
VynDragondpgeorge
authored andcommitted
zephyr: Add support for GC split-heap.
Adds the ability to use PSRAM and non-contiguous memory. Example usage, add this to dts overlay: / { heap_psram { compatible = "micropython,heap"; size = <DT_SIZE_M(4)>; memory-region = <&psram>; }; heap_sram1 { compatible = "micropython,heap"; size = <DT_SIZE_K(140)>; memory-region = <&sram1>; }; }; Signed-off-by: Vdragon <mail@massdriver.space>
1 parent bd111cc commit 3d9a3e8

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) 2025 MASSDRIVER EI (massdriver.space)
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
MicroPython Heap
6+
Used to define micropython heap areas.
7+
8+
compatible: "micropython,heap"
9+
10+
include: [base.yaml]
11+
12+
properties:
13+
size:
14+
type: int
15+
required: true
16+
description: |
17+
Size of the heap
18+
19+
memory-region:
20+
type: phandle
21+
required: true
22+
description: |
23+
Memory region to place the heap in

ports/zephyr/main.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@
5858
#include "extmod/vfs.h"
5959
#endif
6060

61+
#if MICROPY_ENABLE_GC
62+
#if MICROPY_GC_SPLIT_HEAP && DT_HAS_COMPAT_STATUS_OKAY(micropython_heap)
63+
64+
#include <zephyr/linker/devicetree_regions.h>
65+
66+
#define MICROPY_HEAP_NAME(node) CONCAT(DT_NODE_FULL_NAME_TOKEN(node), _heap)
67+
68+
#define MICROPY_HEAP_DEFINE(node) \
69+
static char MICROPY_HEAP_NAME(node)[DT_PROP(node, size)] \
70+
Z_GENERIC_SECTION(LINKER_DT_NODE_REGION_NAME(DT_PROP(node, memory_region)));
71+
72+
DT_FOREACH_STATUS_OKAY(micropython_heap, MICROPY_HEAP_DEFINE)
73+
74+
#define MICROPY_HEAP_ADD(node) \
75+
gc_add((void *)&MICROPY_HEAP_NAME(node), \
76+
(void *)&(MICROPY_HEAP_NAME(node)[DT_PROP(node, size) - 1]));
77+
78+
#elif DT_HAS_COMPAT_STATUS_OKAY(micropython_heap)
79+
#error Has additional Heap but split heap is not enabled
80+
#endif
81+
#endif
82+
6183
#include "modmachine.h"
6284
#include "modzephyr.h"
6385

@@ -107,6 +129,9 @@ int real_main(void) {
107129
mp_cstack_init_with_sp_here(CONFIG_MAIN_STACK_SIZE);
108130
#if MICROPY_ENABLE_GC
109131
gc_init(heap, heap + sizeof(heap));
132+
#if MICROPY_GC_SPLIT_HEAP && DT_HAS_COMPAT_STATUS_OKAY(micropython_heap)
133+
DT_FOREACH_STATUS_OKAY(micropython_heap, MICROPY_HEAP_ADD)
134+
#endif
110135
#endif
111136
mp_init();
112137

ports/zephyr/mpconfigport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ void mp_hal_signal_event(void);
151151
#define MICROPY_PY_MACHINE_ADC_READ_UV (1)
152152
#endif
153153

154+
#if DT_HAS_COMPAT_STATUS_OKAY(micropython_heap)
155+
#define MICROPY_GC_SPLIT_HEAP (1)
156+
#endif
157+
154158
typedef long mp_off_t;
155159

156160
#define MP_STATE_PORT MP_STATE_VM

0 commit comments

Comments
 (0)