summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ports/zephyr/dts/bindings/micropython/micropython,heap.yaml23
-rw-r--r--ports/zephyr/main.c25
-rw-r--r--ports/zephyr/mpconfigport.h4
3 files changed, 52 insertions, 0 deletions
diff --git a/ports/zephyr/dts/bindings/micropython/micropython,heap.yaml b/ports/zephyr/dts/bindings/micropython/micropython,heap.yaml
new file mode 100644
index 000000000..333baa966
--- /dev/null
+++ b/ports/zephyr/dts/bindings/micropython/micropython,heap.yaml
@@ -0,0 +1,23 @@
+# Copyright (c) 2025 MASSDRIVER EI (massdriver.space)
+# SPDX-License-Identifier: Apache-2.0
+
+description: |
+ MicroPython Heap
+ Used to define micropython heap areas.
+
+compatible: "micropython,heap"
+
+include: [base.yaml]
+
+properties:
+ size:
+ type: int
+ required: true
+ description: |
+ Size of the heap
+
+ memory-region:
+ type: phandle
+ required: true
+ description: |
+ Memory region to place the heap in
diff --git a/ports/zephyr/main.c b/ports/zephyr/main.c
index 0fa4e97b2..c82814e04 100644
--- a/ports/zephyr/main.c
+++ b/ports/zephyr/main.c
@@ -58,6 +58,28 @@
#include "extmod/vfs.h"
#endif
+#if MICROPY_ENABLE_GC
+#if MICROPY_GC_SPLIT_HEAP && DT_HAS_COMPAT_STATUS_OKAY(micropython_heap)
+
+#include <zephyr/linker/devicetree_regions.h>
+
+#define MICROPY_HEAP_NAME(node) CONCAT(DT_NODE_FULL_NAME_TOKEN(node), _heap)
+
+#define MICROPY_HEAP_DEFINE(node) \
+ static char MICROPY_HEAP_NAME(node)[DT_PROP(node, size)] \
+ Z_GENERIC_SECTION(LINKER_DT_NODE_REGION_NAME(DT_PROP(node, memory_region)));
+
+DT_FOREACH_STATUS_OKAY(micropython_heap, MICROPY_HEAP_DEFINE)
+
+#define MICROPY_HEAP_ADD(node) \
+ gc_add((void *)&MICROPY_HEAP_NAME(node), \
+ (void *)&(MICROPY_HEAP_NAME(node)[DT_PROP(node, size) - 1]));
+
+#elif DT_HAS_COMPAT_STATUS_OKAY(micropython_heap)
+#error Has additional Heap but split heap is not enabled
+#endif
+#endif
+
#include "modmachine.h"
#include "modzephyr.h"
@@ -107,6 +129,9 @@ soft_reset:
mp_cstack_init_with_sp_here(CONFIG_MAIN_STACK_SIZE);
#if MICROPY_ENABLE_GC
gc_init(heap, heap + sizeof(heap));
+ #if MICROPY_GC_SPLIT_HEAP && DT_HAS_COMPAT_STATUS_OKAY(micropython_heap)
+ DT_FOREACH_STATUS_OKAY(micropython_heap, MICROPY_HEAP_ADD)
+ #endif
#endif
mp_init();
diff --git a/ports/zephyr/mpconfigport.h b/ports/zephyr/mpconfigport.h
index 7f8497348..56a442c26 100644
--- a/ports/zephyr/mpconfigport.h
+++ b/ports/zephyr/mpconfigport.h
@@ -151,6 +151,10 @@ void mp_hal_signal_event(void);
#define MICROPY_PY_MACHINE_ADC_READ_UV (1)
#endif
+#if DT_HAS_COMPAT_STATUS_OKAY(micropython_heap)
+#define MICROPY_GC_SPLIT_HEAP (1)
+#endif
+
typedef long mp_off_t;
#define MP_STATE_PORT MP_STATE_VM