diff options
| author | Vdragon <mail@massdriver.space> | 2025-10-25 23:39:33 +0200 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2025-11-26 23:40:39 +1100 |
| commit | 3d9a3e89cf4a32fdc6fca2a8edd91714b5b67665 (patch) | |
| tree | 8fff35607ed4fe8291367325fdc67c591ec57415 | |
| parent | bd111ccd4b4932ea94ba7c66007840cb88e3b4a7 (diff) | |
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>
| -rw-r--r-- | ports/zephyr/dts/bindings/micropython/micropython,heap.yaml | 23 | ||||
| -rw-r--r-- | ports/zephyr/main.c | 25 | ||||
| -rw-r--r-- | ports/zephyr/mpconfigport.h | 4 |
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 |
