From 32df89a50c3ffd9e5b11a3a77a7ba3fc50a1effd Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Thu, 5 Nov 2026 21:02:07 +0000 Subject: [PATCH 1/9] picodvi: compute a display's RAM layout in one place The framebuffer length and the DMA command count move out of the constructor into a helper, and picodvi_framebuffer_internal_ram() returns the bytes a display of a given size takes. No change in behaviour. --- .../common-hal/picodvi/Framebuffer_RP2350.c | 70 ++++++++++++------- .../common-hal/picodvi/Framebuffer_RP2350.h | 4 ++ 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c index 590ed894fb3..73f4bb6f8b1 100644 --- a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c +++ b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c @@ -176,6 +176,46 @@ bool common_hal_picodvi_framebuffer_preflight( return false; } +// Where a display of this size lives in RAM: the output mode, the words per row, +// and the lengths in words of the framebuffer and of the DMA command list for one +// frame. +static void picodvi_framebuffer_layout(mp_uint_t width, mp_uint_t height, mp_uint_t color_depth, + mp_uint_t *output_width, size_t *pitch, size_t *framebuffer_len, size_t *dma_commands_len) { + if (width % 160 == 0) { + *output_width = 640; + } else { + *output_width = 720; + } + size_t output_scaling = *output_width / width; + + // Pitch is number of 32-bit words per line. We round up pitch_bytes to the nearest word + // so that each scanline begins on a natural 32-bit word boundary. + size_t pitch_bytes = (width * color_depth) / 8; + *pitch = (pitch_bytes + sizeof(uint32_t) - 1) / sizeof(uint32_t); + *framebuffer_len = *pitch * height; + + // We compute all DMA transfers needed for a single frame. This ensure we don't have any super + // quick interrupts that we need to respond to. Each transfer takes two words, trans_count and + // read_addr. Active pixel lines need two transfers due to different read addresses. When pixel + // doubling, then we must also set transfer size. + size_t dma_command_size = 2; + if (output_scaling > 1) { + dma_command_size = 4; + } + if (*output_width == 640) { + *dma_commands_len = (MODE_640_V_FRONT_PORCH + MODE_640_V_SYNC_WIDTH + MODE_640_V_BACK_PORCH + 2 * MODE_640_V_ACTIVE_LINES + 1) * dma_command_size; + } else { + *dma_commands_len = (MODE_720_V_FRONT_PORCH + MODE_720_V_SYNC_WIDTH + MODE_720_V_BACK_PORCH + 2 * MODE_720_V_ACTIVE_LINES + 1) * dma_command_size; + } +} + +size_t picodvi_framebuffer_internal_ram(mp_uint_t width, mp_uint_t height, mp_uint_t color_depth) { + mp_uint_t output_width; + size_t pitch, framebuffer_len, dma_commands_len; + picodvi_framebuffer_layout(width, height, color_depth, &output_width, &pitch, &framebuffer_len, &dma_commands_len); + return (framebuffer_len + dma_commands_len) * sizeof(uint32_t); +} + void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, mp_uint_t width, mp_uint_t height, const mcu_pin_obj_t *clk_dp, const mcu_pin_obj_t *clk_dn, @@ -196,11 +236,10 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, self->dma_command_channel = -1; self->dma_pixel_channel = -1; - if (width % 160 == 0) { - self->output_width = 640; - } else { - self->output_width = 720; - } + size_t pitch; + size_t framebuffer_size; + size_t dma_commands_len; + picodvi_framebuffer_layout(width, height, color_depth, &self->output_width, &pitch, &framebuffer_size, &dma_commands_len); size_t output_scaling = self->output_width / width; size_t all_allocated = 0; @@ -231,11 +270,8 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, self->width = width; self->height = height; self->color_depth = color_depth; - // Pitch is number of 32-bit words per line. We round up pitch_bytes to the nearest word - // so that each scanline begins on a natural 32-bit word boundary. - size_t pitch_bytes = (self->width * color_depth) / 8; - self->pitch = (pitch_bytes + sizeof(uint32_t) - 1) / sizeof(uint32_t); - size_t framebuffer_size = self->pitch * self->height; + self->pitch = pitch; + self->dma_commands_len = dma_commands_len; // We check that allocations aren't in PSRAM because we haven't added XIP // streaming support. @@ -247,20 +283,6 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, } memset(self->framebuffer, 0, framebuffer_size * sizeof(uint32_t)); - // We compute all DMA transfers needed for a single frame. This ensure we don't have any super - // quick interrupts that we need to respond to. Each transfer takes two words, trans_count and - // read_addr. Active pixel lines need two transfers due to different read addresses. When pixel - // doubling, then we must also set transfer size. - size_t dma_command_size = 2; - if (output_scaling > 1) { - dma_command_size = 4; - } - - if (self->output_width == 640) { - self->dma_commands_len = (MODE_640_V_FRONT_PORCH + MODE_640_V_SYNC_WIDTH + MODE_640_V_BACK_PORCH + 2 * MODE_640_V_ACTIVE_LINES + 1) * dma_command_size; - } else { - self->dma_commands_len = (MODE_720_V_FRONT_PORCH + MODE_720_V_SYNC_WIDTH + MODE_720_V_BACK_PORCH + 2 * MODE_720_V_ACTIVE_LINES + 1) * dma_command_size; - } self->dma_commands = (uint32_t *)port_malloc(self->dma_commands_len * sizeof(uint32_t), true); if (self->dma_commands == NULL || ((size_t)self->dma_commands & 0xf0000000) == 0x10000000) { common_hal_picodvi_framebuffer_deinit(self); diff --git a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.h b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.h index 904e69d83c9..2ffe9eaf43a 100644 --- a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.h +++ b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.h @@ -103,3 +103,7 @@ typedef struct { // The framebuffer the frame interrupt is driving, or NULL. extern picodvi_framebuffer_obj_t *active_picodvi; + +// The internal RAM a display of this size takes: its framebuffer and the DMA +// command list for one frame, in bytes. +size_t picodvi_framebuffer_internal_ram(mp_uint_t width, mp_uint_t height, mp_uint_t color_depth); From 48371301ee09573e7723830dbdcae9eca3b7d980 Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Thu, 5 Nov 2026 21:02:49 +0000 Subject: [PATCH 2/9] picodvi: allocate the boot display's buffers even when it is not built Since every supervisor allocation starts in internal RAM (#11176), the USB descriptors, the pystack and the heap's segments land right above the display's buffers at boot, and after release_displays() the heap grows into the freed space. A program that builds the display later then has no room for a 640x480 framebuffer. On boards with PSRAM, picodvi now allocates the buffers of the display it would build at boot even when it does not build it, and a Framebuffer that fits in them uses them instead of allocating. Fixes #11641. --- docs/environment.rst | 6 +++ .../common-hal/picodvi/Framebuffer_RP2350.c | 43 +++++++++------ .../raspberrypi/common-hal/picodvi/__init__.c | 53 ++++++++++++------- .../raspberrypi/common-hal/picodvi/__init__.h | 11 ++++ 4 files changed, 78 insertions(+), 35 deletions(-) diff --git a/docs/environment.rst b/docs/environment.rst index 7da361b5fb3..594f2883168 100644 --- a/docs/environment.rst +++ b/docs/environment.rst @@ -219,6 +219,12 @@ A display configured in this manner is available at ``supervisor.runtime.display until it is released by ``displayio.release_displays()``. It does not appear at ``board.DISPLAY``. +On boards with PSRAM, the buffers of the configured display (the size from +``CIRCUITPY_DISPLAY_WIDTH`` and the related variables, or 640x480 by default) are +allocated at boot even when no display is built, so ``picodvi.Framebuffer`` can +still be constructed at that size, or a smaller one, after the heap has grown. +``never`` allocates nothing. + `Adafruit Feather RP2350 `_ `Adafruit Metro RP2350 `_ diff --git a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c index 73f4bb6f8b1..859ac19d6f3 100644 --- a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c +++ b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c @@ -25,6 +25,7 @@ */ #include "bindings/picodvi/Framebuffer.h" +#include "common-hal/picodvi/__init__.h" #include "py/gc.h" #include "py/runtime.h" @@ -273,23 +274,29 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, self->pitch = pitch; self->dma_commands_len = dma_commands_len; - // We check that allocations aren't in PSRAM because we haven't added XIP - // streaming support. - self->framebuffer = (uint32_t *)port_malloc(framebuffer_size * sizeof(uint32_t), true); - if (self->framebuffer == NULL || ((size_t)self->framebuffer & 0xf0000000) == 0x10000000) { - common_hal_picodvi_framebuffer_deinit(self); - m_malloc_fail(framebuffer_size * sizeof(uint32_t)); - return; + // The boot display's buffers, when this display fits in them. + uint32_t *boot_ram = picodvi_boot_ram_take((framebuffer_size + dma_commands_len) * sizeof(uint32_t)); + if (boot_ram != NULL) { + self->framebuffer = boot_ram; + self->dma_commands = boot_ram + framebuffer_size; + } else { + // We check that allocations aren't in PSRAM because we haven't added XIP + // streaming support. + self->framebuffer = (uint32_t *)port_malloc(framebuffer_size * sizeof(uint32_t), true); + if (self->framebuffer == NULL || ((size_t)self->framebuffer & 0xf0000000) == 0x10000000) { + common_hal_picodvi_framebuffer_deinit(self); + m_malloc_fail(framebuffer_size * sizeof(uint32_t)); + return; + } + self->dma_commands = (uint32_t *)port_malloc(self->dma_commands_len * sizeof(uint32_t), true); + if (self->dma_commands == NULL || ((size_t)self->dma_commands & 0xf0000000) == 0x10000000) { + common_hal_picodvi_framebuffer_deinit(self); + m_malloc_fail(self->dma_commands_len * sizeof(uint32_t)); + return; + } } memset(self->framebuffer, 0, framebuffer_size * sizeof(uint32_t)); - self->dma_commands = (uint32_t *)port_malloc(self->dma_commands_len * sizeof(uint32_t), true); - if (self->dma_commands == NULL || ((size_t)self->dma_commands & 0xf0000000) == 0x10000000) { - common_hal_picodvi_framebuffer_deinit(self); - m_malloc_fail(self->dma_commands_len * sizeof(uint32_t)); - return; - } - // The command channel and the pixel channel form a pipeline that feeds combined HSTX // commands and pixel data to the HSTX FIFO. The command channel reads a pre-computed // list of control/status words from the dma_commands buffer and writes them to the @@ -611,10 +618,12 @@ void common_hal_picodvi_framebuffer_deinit(picodvi_framebuffer_obj_t *self) { picodvi_audioout_framebuffer_deinit(self); - port_free(self->framebuffer); + // The boot display's buffers stay allocated for the next Framebuffer. + if (!picodvi_is_boot_ram(self->framebuffer)) { + port_free(self->framebuffer); + port_free(self->dma_commands); + } self->framebuffer = NULL; - - port_free(self->dma_commands); self->dma_commands = NULL; self->base.type = &mp_type_NoneType; diff --git a/ports/raspberrypi/common-hal/picodvi/__init__.c b/ports/raspberrypi/common-hal/picodvi/__init__.c index a9276096529..2dfbe500a75 100644 --- a/ports/raspberrypi/common-hal/picodvi/__init__.c +++ b/ports/raspberrypi/common-hal/picodvi/__init__.c @@ -20,23 +20,23 @@ #include "common-hal/picodvi/AudioOut.h" #endif -#if defined(DEFAULT_DVI_BUS_CLK_DP) -static bool picodvi_autoconstruct_enabled(mp_int_t *default_width, mp_int_t *default_height) { - char buf[sizeof("detect")]; - buf[0] = 0; - - // (any failure leaves the content of buf untouched: an empty nul-terminated string - (void)settings_get_str("CIRCUITPY_PICODVI_ENABLE", buf, sizeof(buf)); - - if (!strcasecmp(buf, "never")) { - return false; - } - if (!strcasecmp(buf, "always")) { - return true; - } +// Every supervisor allocation starts in internal RAM, so without this the heap +// takes the space of a display that a program builds only later. +static uint32_t *_boot_ram; +static size_t _boot_ram_size; // bytes + +uint32_t *picodvi_boot_ram_take(size_t bytes) { + // NULL when the boot allocation failed, or there is none. + return bytes <= _boot_ram_size ? _boot_ram : NULL; +} - // It's "detect" or else an invalid value which is treated the same as "detect". +bool picodvi_is_boot_ram(const void *ptr) { + return ptr != NULL && ptr == _boot_ram; +} +#if defined(DEFAULT_DVI_BUS_CLK_DP) +// True when a display answers at the EDID address and supports one of our modes. +static bool picodvi_display_detected(mp_int_t *default_width, mp_int_t *default_height) { // check if address 0x50 is live on the I2C bus busio_i2c_obj_t *i2c = common_hal_board_create_i2c(0); if (!i2c) { @@ -99,11 +99,18 @@ void picodvi_autoconstruct(void) { return; } - mp_int_t default_width = 640; - mp_int_t default_height = 480; - if (!picodvi_autoconstruct_enabled(&default_width, &default_height)) { + char buf[sizeof("detect")]; + buf[0] = 0; + // (any failure leaves the content of buf untouched: an empty nul-terminated string + (void)settings_get_str("CIRCUITPY_PICODVI_ENABLE", buf, sizeof(buf)); + if (!strcasecmp(buf, "never")) { return; } + // It's "always", "detect" or else an invalid value which is treated the same as "detect". + mp_int_t default_width = 640; + mp_int_t default_height = 480; + bool enabled = !strcasecmp(buf, "always") || + picodvi_display_detected(&default_width, &default_height); #if CIRCUITPY_PICODVI_AUDIOOUT bool audio = true; @@ -149,6 +156,16 @@ void picodvi_autoconstruct(void) { color_depth = 8; } + #ifdef CIRCUITPY_PSRAM_CHIP_SELECT + // Taken now, before anything else lands in internal RAM, whether or not the + // display is built. The heap has PSRAM to grow into. + _boot_ram_size = picodvi_framebuffer_internal_ram(width, height, color_depth); + _boot_ram = port_malloc(_boot_ram_size, true); + #endif + if (!enabled) { + return; + } + // construct framebuffer and display picodvi_framebuffer_obj_t *fb = &allocate_display_bus_or_raise()->picodvi; fb->base.type = &picodvi_framebuffer_type; diff --git a/ports/raspberrypi/common-hal/picodvi/__init__.h b/ports/raspberrypi/common-hal/picodvi/__init__.h index 41f3656339a..a78b88ef70a 100644 --- a/ports/raspberrypi/common-hal/picodvi/__init__.h +++ b/ports/raspberrypi/common-hal/picodvi/__init__.h @@ -6,4 +6,15 @@ #pragma once +#include +#include +#include + extern void picodvi_autoconstruct(void); + +// The buffers of the display built at boot. On boards with PSRAM they are +// allocated at boot even when that display is not built, see +// picodvi_autoconstruct. A Framebuffer whose framebuffer and DMA command list +// fit in them uses them instead of allocating, and leaves them allocated. +uint32_t *picodvi_boot_ram_take(size_t bytes); +bool picodvi_is_boot_ram(const void *ptr); From 0a2636e45314a463c00bdb6eb4e88fad4cd66753 Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Fri, 6 Nov 2026 15:58:53 +0000 Subject: [PATCH 3/9] picodvi: size the boot buffers by what is known at boot With a 1080p display the boot mode is 720x400, 20 KB less than 640x480x8 needs, and Fruit Jam OS switches modes at run time. A display configured with CIRCUITPY_DISPLAY_WIDTH keeps its own size, so the rest of internal RAM stays with the heap. A display built at boot without one gets the largest mode, 320x240 at 32 bits, so the program can switch modes later. With neither, nothing is held. --- docs/environment.rst | 13 ++++++++----- .../raspberrypi/common-hal/picodvi/__init__.c | 19 ++++++++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/docs/environment.rst b/docs/environment.rst index 594f2883168..501f9bbabdb 100644 --- a/docs/environment.rst +++ b/docs/environment.rst @@ -219,11 +219,14 @@ A display configured in this manner is available at ``supervisor.runtime.display until it is released by ``displayio.release_displays()``. It does not appear at ``board.DISPLAY``. -On boards with PSRAM, the buffers of the configured display (the size from -``CIRCUITPY_DISPLAY_WIDTH`` and the related variables, or 640x480 by default) are -allocated at boot even when no display is built, so ``picodvi.Framebuffer`` can -still be constructed at that size, or a smaller one, after the heap has grown. -``never`` allocates nothing. +On boards with PSRAM, the buffers of the display configured with +``CIRCUITPY_DISPLAY_WIDTH`` and the related variables are allocated at boot even +when no display is built, so ``picodvi.Framebuffer`` can still be constructed at +that size after the heap has grown into internal RAM. Without a configured size, +a display built at boot (``always``, or ``detect`` with a display attached) gets +the buffers of the largest mode, 320x240 at 32 bits (about 325 KB), so a program +can switch modes later. Otherwise nothing is allocated and the heap can use all +of internal RAM. `Adafruit Feather RP2350 `_ `Adafruit Metro RP2350 `_ diff --git a/ports/raspberrypi/common-hal/picodvi/__init__.c b/ports/raspberrypi/common-hal/picodvi/__init__.c index 2dfbe500a75..cdb1fbc2b05 100644 --- a/ports/raspberrypi/common-hal/picodvi/__init__.c +++ b/ports/raspberrypi/common-hal/picodvi/__init__.c @@ -122,7 +122,7 @@ void picodvi_autoconstruct(void) { mp_int_t color_depth = 8; mp_int_t rotation = 0; - (void)settings_get_int("CIRCUITPY_DISPLAY_WIDTH", &width); + bool configured = settings_get_int("CIRCUITPY_DISPLAY_WIDTH", &width) == SETTINGS_OK; (void)settings_get_int("CIRCUITPY_DISPLAY_HEIGHT", &height); (void)settings_get_int("CIRCUITPY_DISPLAY_COLOR_DEPTH", &color_depth); (void)settings_get_int("CIRCUITPY_DISPLAY_ROTATION", &rotation); @@ -154,13 +154,22 @@ void picodvi_autoconstruct(void) { width = default_width; height = default_height; color_depth = 8; + configured = false; } #ifdef CIRCUITPY_PSRAM_CHIP_SELECT - // Taken now, before anything else lands in internal RAM, whether or not the - // display is built. The heap has PSRAM to grow into. - _boot_ram_size = picodvi_framebuffer_internal_ram(width, height, color_depth); - _boot_ram = port_malloc(_boot_ram_size, true); + // Taken now, before anything else lands in internal RAM; the heap has PSRAM to + // grow into. A configured display gets its own size. A display built without + // one may be switched for another later, so it gets the largest mode, 320x240 + // at 32 bits. With neither, nothing is held and the heap takes the RAM. + if (configured) { + _boot_ram_size = picodvi_framebuffer_internal_ram(width, height, color_depth); + } else if (enabled) { + _boot_ram_size = picodvi_framebuffer_internal_ram(320, 240, 32); + } + if (_boot_ram_size > 0) { + _boot_ram = port_malloc(_boot_ram_size, true); + } #endif if (!enabled) { return; From b675b484550833ddc3df3a41abc0abbac680cdff Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Fri, 6 Nov 2026 18:05:48 +0000 Subject: [PATCH 4/9] picodvi: allocate the boot buffers once board_init() runs again when waking from a fake deep sleep, which would allocate a second block and leak the first. --- ports/raspberrypi/common-hal/picodvi/__init__.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/ports/raspberrypi/common-hal/picodvi/__init__.c b/ports/raspberrypi/common-hal/picodvi/__init__.c index cdb1fbc2b05..0a39a1180f0 100644 --- a/ports/raspberrypi/common-hal/picodvi/__init__.c +++ b/ports/raspberrypi/common-hal/picodvi/__init__.c @@ -162,13 +162,16 @@ void picodvi_autoconstruct(void) { // grow into. A configured display gets its own size. A display built without // one may be switched for another later, so it gets the largest mode, 320x240 // at 32 bits. With neither, nothing is held and the heap takes the RAM. - if (configured) { - _boot_ram_size = picodvi_framebuffer_internal_ram(width, height, color_depth); - } else if (enabled) { - _boot_ram_size = picodvi_framebuffer_internal_ram(320, 240, 32); - } - if (_boot_ram_size > 0) { - _boot_ram = port_malloc(_boot_ram_size, true); + // Once only: board_init() runs again after a fake deep sleep. + if (_boot_ram == NULL) { + if (configured) { + _boot_ram_size = picodvi_framebuffer_internal_ram(width, height, color_depth); + } else if (enabled) { + _boot_ram_size = picodvi_framebuffer_internal_ram(320, 240, 32); + } + if (_boot_ram_size > 0) { + _boot_ram = port_malloc(_boot_ram_size, true); + } } #endif if (!enabled) { From 264c92d608afd3861c486b61d33f5e6fd33622c0 Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Fri, 13 Nov 2026 06:49:42 +0000 Subject: [PATCH 5/9] picodvi: take the boot buffers out again The display's buffers are allocated when the display is built, as before this branch. The four commits that kept them from boot are undone. --- docs/environment.rst | 9 -- .../common-hal/picodvi/Framebuffer_RP2350.c | 115 +++++++----------- .../common-hal/picodvi/Framebuffer_RP2350.h | 4 - .../raspberrypi/common-hal/picodvi/__init__.c | 67 +++------- .../raspberrypi/common-hal/picodvi/__init__.h | 11 -- 5 files changed, 61 insertions(+), 145 deletions(-) diff --git a/docs/environment.rst b/docs/environment.rst index 501f9bbabdb..7da361b5fb3 100644 --- a/docs/environment.rst +++ b/docs/environment.rst @@ -219,15 +219,6 @@ A display configured in this manner is available at ``supervisor.runtime.display until it is released by ``displayio.release_displays()``. It does not appear at ``board.DISPLAY``. -On boards with PSRAM, the buffers of the display configured with -``CIRCUITPY_DISPLAY_WIDTH`` and the related variables are allocated at boot even -when no display is built, so ``picodvi.Framebuffer`` can still be constructed at -that size after the heap has grown into internal RAM. Without a configured size, -a display built at boot (``always``, or ``detect`` with a display attached) gets -the buffers of the largest mode, 320x240 at 32 bits (about 325 KB), so a program -can switch modes later. Otherwise nothing is allocated and the heap can use all -of internal RAM. - `Adafruit Feather RP2350 `_ `Adafruit Metro RP2350 `_ diff --git a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c index 859ac19d6f3..590ed894fb3 100644 --- a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c +++ b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c @@ -25,7 +25,6 @@ */ #include "bindings/picodvi/Framebuffer.h" -#include "common-hal/picodvi/__init__.h" #include "py/gc.h" #include "py/runtime.h" @@ -177,46 +176,6 @@ bool common_hal_picodvi_framebuffer_preflight( return false; } -// Where a display of this size lives in RAM: the output mode, the words per row, -// and the lengths in words of the framebuffer and of the DMA command list for one -// frame. -static void picodvi_framebuffer_layout(mp_uint_t width, mp_uint_t height, mp_uint_t color_depth, - mp_uint_t *output_width, size_t *pitch, size_t *framebuffer_len, size_t *dma_commands_len) { - if (width % 160 == 0) { - *output_width = 640; - } else { - *output_width = 720; - } - size_t output_scaling = *output_width / width; - - // Pitch is number of 32-bit words per line. We round up pitch_bytes to the nearest word - // so that each scanline begins on a natural 32-bit word boundary. - size_t pitch_bytes = (width * color_depth) / 8; - *pitch = (pitch_bytes + sizeof(uint32_t) - 1) / sizeof(uint32_t); - *framebuffer_len = *pitch * height; - - // We compute all DMA transfers needed for a single frame. This ensure we don't have any super - // quick interrupts that we need to respond to. Each transfer takes two words, trans_count and - // read_addr. Active pixel lines need two transfers due to different read addresses. When pixel - // doubling, then we must also set transfer size. - size_t dma_command_size = 2; - if (output_scaling > 1) { - dma_command_size = 4; - } - if (*output_width == 640) { - *dma_commands_len = (MODE_640_V_FRONT_PORCH + MODE_640_V_SYNC_WIDTH + MODE_640_V_BACK_PORCH + 2 * MODE_640_V_ACTIVE_LINES + 1) * dma_command_size; - } else { - *dma_commands_len = (MODE_720_V_FRONT_PORCH + MODE_720_V_SYNC_WIDTH + MODE_720_V_BACK_PORCH + 2 * MODE_720_V_ACTIVE_LINES + 1) * dma_command_size; - } -} - -size_t picodvi_framebuffer_internal_ram(mp_uint_t width, mp_uint_t height, mp_uint_t color_depth) { - mp_uint_t output_width; - size_t pitch, framebuffer_len, dma_commands_len; - picodvi_framebuffer_layout(width, height, color_depth, &output_width, &pitch, &framebuffer_len, &dma_commands_len); - return (framebuffer_len + dma_commands_len) * sizeof(uint32_t); -} - void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, mp_uint_t width, mp_uint_t height, const mcu_pin_obj_t *clk_dp, const mcu_pin_obj_t *clk_dn, @@ -237,10 +196,11 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, self->dma_command_channel = -1; self->dma_pixel_channel = -1; - size_t pitch; - size_t framebuffer_size; - size_t dma_commands_len; - picodvi_framebuffer_layout(width, height, color_depth, &self->output_width, &pitch, &framebuffer_size, &dma_commands_len); + if (width % 160 == 0) { + self->output_width = 640; + } else { + self->output_width = 720; + } size_t output_scaling = self->output_width / width; size_t all_allocated = 0; @@ -271,32 +231,43 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, self->width = width; self->height = height; self->color_depth = color_depth; - self->pitch = pitch; - self->dma_commands_len = dma_commands_len; - - // The boot display's buffers, when this display fits in them. - uint32_t *boot_ram = picodvi_boot_ram_take((framebuffer_size + dma_commands_len) * sizeof(uint32_t)); - if (boot_ram != NULL) { - self->framebuffer = boot_ram; - self->dma_commands = boot_ram + framebuffer_size; - } else { - // We check that allocations aren't in PSRAM because we haven't added XIP - // streaming support. - self->framebuffer = (uint32_t *)port_malloc(framebuffer_size * sizeof(uint32_t), true); - if (self->framebuffer == NULL || ((size_t)self->framebuffer & 0xf0000000) == 0x10000000) { - common_hal_picodvi_framebuffer_deinit(self); - m_malloc_fail(framebuffer_size * sizeof(uint32_t)); - return; - } - self->dma_commands = (uint32_t *)port_malloc(self->dma_commands_len * sizeof(uint32_t), true); - if (self->dma_commands == NULL || ((size_t)self->dma_commands & 0xf0000000) == 0x10000000) { - common_hal_picodvi_framebuffer_deinit(self); - m_malloc_fail(self->dma_commands_len * sizeof(uint32_t)); - return; - } + // Pitch is number of 32-bit words per line. We round up pitch_bytes to the nearest word + // so that each scanline begins on a natural 32-bit word boundary. + size_t pitch_bytes = (self->width * color_depth) / 8; + self->pitch = (pitch_bytes + sizeof(uint32_t) - 1) / sizeof(uint32_t); + size_t framebuffer_size = self->pitch * self->height; + + // We check that allocations aren't in PSRAM because we haven't added XIP + // streaming support. + self->framebuffer = (uint32_t *)port_malloc(framebuffer_size * sizeof(uint32_t), true); + if (self->framebuffer == NULL || ((size_t)self->framebuffer & 0xf0000000) == 0x10000000) { + common_hal_picodvi_framebuffer_deinit(self); + m_malloc_fail(framebuffer_size * sizeof(uint32_t)); + return; } memset(self->framebuffer, 0, framebuffer_size * sizeof(uint32_t)); + // We compute all DMA transfers needed for a single frame. This ensure we don't have any super + // quick interrupts that we need to respond to. Each transfer takes two words, trans_count and + // read_addr. Active pixel lines need two transfers due to different read addresses. When pixel + // doubling, then we must also set transfer size. + size_t dma_command_size = 2; + if (output_scaling > 1) { + dma_command_size = 4; + } + + if (self->output_width == 640) { + self->dma_commands_len = (MODE_640_V_FRONT_PORCH + MODE_640_V_SYNC_WIDTH + MODE_640_V_BACK_PORCH + 2 * MODE_640_V_ACTIVE_LINES + 1) * dma_command_size; + } else { + self->dma_commands_len = (MODE_720_V_FRONT_PORCH + MODE_720_V_SYNC_WIDTH + MODE_720_V_BACK_PORCH + 2 * MODE_720_V_ACTIVE_LINES + 1) * dma_command_size; + } + self->dma_commands = (uint32_t *)port_malloc(self->dma_commands_len * sizeof(uint32_t), true); + if (self->dma_commands == NULL || ((size_t)self->dma_commands & 0xf0000000) == 0x10000000) { + common_hal_picodvi_framebuffer_deinit(self); + m_malloc_fail(self->dma_commands_len * sizeof(uint32_t)); + return; + } + // The command channel and the pixel channel form a pipeline that feeds combined HSTX // commands and pixel data to the HSTX FIFO. The command channel reads a pre-computed // list of control/status words from the dma_commands buffer and writes them to the @@ -618,12 +589,10 @@ void common_hal_picodvi_framebuffer_deinit(picodvi_framebuffer_obj_t *self) { picodvi_audioout_framebuffer_deinit(self); - // The boot display's buffers stay allocated for the next Framebuffer. - if (!picodvi_is_boot_ram(self->framebuffer)) { - port_free(self->framebuffer); - port_free(self->dma_commands); - } + port_free(self->framebuffer); self->framebuffer = NULL; + + port_free(self->dma_commands); self->dma_commands = NULL; self->base.type = &mp_type_NoneType; diff --git a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.h b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.h index 2ffe9eaf43a..904e69d83c9 100644 --- a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.h +++ b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.h @@ -103,7 +103,3 @@ typedef struct { // The framebuffer the frame interrupt is driving, or NULL. extern picodvi_framebuffer_obj_t *active_picodvi; - -// The internal RAM a display of this size takes: its framebuffer and the DMA -// command list for one frame, in bytes. -size_t picodvi_framebuffer_internal_ram(mp_uint_t width, mp_uint_t height, mp_uint_t color_depth); diff --git a/ports/raspberrypi/common-hal/picodvi/__init__.c b/ports/raspberrypi/common-hal/picodvi/__init__.c index 0a39a1180f0..a9276096529 100644 --- a/ports/raspberrypi/common-hal/picodvi/__init__.c +++ b/ports/raspberrypi/common-hal/picodvi/__init__.c @@ -20,23 +20,23 @@ #include "common-hal/picodvi/AudioOut.h" #endif -// Every supervisor allocation starts in internal RAM, so without this the heap -// takes the space of a display that a program builds only later. -static uint32_t *_boot_ram; -static size_t _boot_ram_size; // bytes - -uint32_t *picodvi_boot_ram_take(size_t bytes) { - // NULL when the boot allocation failed, or there is none. - return bytes <= _boot_ram_size ? _boot_ram : NULL; -} +#if defined(DEFAULT_DVI_BUS_CLK_DP) +static bool picodvi_autoconstruct_enabled(mp_int_t *default_width, mp_int_t *default_height) { + char buf[sizeof("detect")]; + buf[0] = 0; -bool picodvi_is_boot_ram(const void *ptr) { - return ptr != NULL && ptr == _boot_ram; -} + // (any failure leaves the content of buf untouched: an empty nul-terminated string + (void)settings_get_str("CIRCUITPY_PICODVI_ENABLE", buf, sizeof(buf)); + + if (!strcasecmp(buf, "never")) { + return false; + } + if (!strcasecmp(buf, "always")) { + return true; + } + + // It's "detect" or else an invalid value which is treated the same as "detect". -#if defined(DEFAULT_DVI_BUS_CLK_DP) -// True when a display answers at the EDID address and supports one of our modes. -static bool picodvi_display_detected(mp_int_t *default_width, mp_int_t *default_height) { // check if address 0x50 is live on the I2C bus busio_i2c_obj_t *i2c = common_hal_board_create_i2c(0); if (!i2c) { @@ -99,18 +99,11 @@ void picodvi_autoconstruct(void) { return; } - char buf[sizeof("detect")]; - buf[0] = 0; - // (any failure leaves the content of buf untouched: an empty nul-terminated string - (void)settings_get_str("CIRCUITPY_PICODVI_ENABLE", buf, sizeof(buf)); - if (!strcasecmp(buf, "never")) { - return; - } - // It's "always", "detect" or else an invalid value which is treated the same as "detect". mp_int_t default_width = 640; mp_int_t default_height = 480; - bool enabled = !strcasecmp(buf, "always") || - picodvi_display_detected(&default_width, &default_height); + if (!picodvi_autoconstruct_enabled(&default_width, &default_height)) { + return; + } #if CIRCUITPY_PICODVI_AUDIOOUT bool audio = true; @@ -122,7 +115,7 @@ void picodvi_autoconstruct(void) { mp_int_t color_depth = 8; mp_int_t rotation = 0; - bool configured = settings_get_int("CIRCUITPY_DISPLAY_WIDTH", &width) == SETTINGS_OK; + (void)settings_get_int("CIRCUITPY_DISPLAY_WIDTH", &width); (void)settings_get_int("CIRCUITPY_DISPLAY_HEIGHT", &height); (void)settings_get_int("CIRCUITPY_DISPLAY_COLOR_DEPTH", &color_depth); (void)settings_get_int("CIRCUITPY_DISPLAY_ROTATION", &rotation); @@ -154,28 +147,6 @@ void picodvi_autoconstruct(void) { width = default_width; height = default_height; color_depth = 8; - configured = false; - } - - #ifdef CIRCUITPY_PSRAM_CHIP_SELECT - // Taken now, before anything else lands in internal RAM; the heap has PSRAM to - // grow into. A configured display gets its own size. A display built without - // one may be switched for another later, so it gets the largest mode, 320x240 - // at 32 bits. With neither, nothing is held and the heap takes the RAM. - // Once only: board_init() runs again after a fake deep sleep. - if (_boot_ram == NULL) { - if (configured) { - _boot_ram_size = picodvi_framebuffer_internal_ram(width, height, color_depth); - } else if (enabled) { - _boot_ram_size = picodvi_framebuffer_internal_ram(320, 240, 32); - } - if (_boot_ram_size > 0) { - _boot_ram = port_malloc(_boot_ram_size, true); - } - } - #endif - if (!enabled) { - return; } // construct framebuffer and display diff --git a/ports/raspberrypi/common-hal/picodvi/__init__.h b/ports/raspberrypi/common-hal/picodvi/__init__.h index a78b88ef70a..41f3656339a 100644 --- a/ports/raspberrypi/common-hal/picodvi/__init__.h +++ b/ports/raspberrypi/common-hal/picodvi/__init__.h @@ -6,15 +6,4 @@ #pragma once -#include -#include -#include - extern void picodvi_autoconstruct(void); - -// The buffers of the display built at boot. On boards with PSRAM they are -// allocated at boot even when that display is not built, see -// picodvi_autoconstruct. A Framebuffer whose framebuffer and DMA command list -// fit in them uses them instead of allocating, and leaves them allocated. -uint32_t *picodvi_boot_ram_take(size_t bytes); -bool picodvi_is_boot_ram(const void *ptr); From 5a86562c0bec42015c284c85b6db234a150528a0 Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Fri, 13 Nov 2026 06:49:54 +0000 Subject: [PATCH 6/9] raspberrypi: allocate in PSRAM first again unless DMA is needed Since #11176 every allocation started in internal RAM, so the heap grew into the space a DMA buffer needs later. A picodvi framebuffer built after start-up then failed with MemoryError while PSRAM was free. Allocations that don't need DMA go to PSRAM first again, as in 10.2.1. Fixes #11641. --- ports/raspberrypi/supervisor/port.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/ports/raspberrypi/supervisor/port.c b/ports/raspberrypi/supervisor/port.c index ff3ddb4cf91..6ff7fef9bf7 100644 --- a/ports/raspberrypi/supervisor/port.c +++ b/ports/raspberrypi/supervisor/port.c @@ -310,17 +310,17 @@ void port_heap_init(void) { } void *port_malloc(size_t size, bool dma_capable) { - // Prefer internal RAM for everything: it is much faster than PSRAM - // (data there skips the external bus and the shared XIP cache). PSRAM, when - // present, serves as spillover capacity for allocations that don't need DMA. - common_hal_mcu_disable_interrupts(); - void *block = tlsf_malloc(_heap, size); - common_hal_mcu_enable_interrupts(); - if (block == NULL && !dma_capable && _psram_size > 0) { + if (!dma_capable && _psram_size > 0) { common_hal_mcu_disable_interrupts(); - block = tlsf_malloc(_psram_heap, size); + void *block = tlsf_malloc(_psram_heap, size); common_hal_mcu_enable_interrupts(); + if (block) { + return block; + } } + common_hal_mcu_disable_interrupts(); + void *block = tlsf_malloc(_heap, size); + common_hal_mcu_enable_interrupts(); return block; } @@ -335,11 +335,7 @@ void port_free(void *ptr) { } void *port_realloc(void *ptr, size_t size, bool dma_capable) { - if (ptr == NULL) { - // Fresh allocation: same internal-first policy as port_malloc. - return port_malloc(size, dma_capable); - } - if (_psram_size > 0 && ((size_t)ptr) < SRAM_BASE) { + if (_psram_size > 0 && ((ptr != NULL && ((size_t)ptr) < SRAM_BASE) || (ptr == NULL && !dma_capable))) { common_hal_mcu_disable_interrupts(); void *block = tlsf_realloc(_psram_heap, ptr, size); common_hal_mcu_enable_interrupts(); From e250ee6a1c4f2ef3f373046ce347a78c807a017b Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Fri, 13 Nov 2026 16:38:52 +0000 Subject: [PATCH 7/9] Add supervisor.runtime.fast_heap With the heap back in PSRAM, a program can ask for internal RAM again once its display and audio exist. While the property is set, new chunks of the VM heap are taken from DMA-capable memory when it has room. When the chunk asked for does not fit there, the largest one that does is added next to the usual one, so that it is used up first. The property is cleared when the next program starts. Not built where all memory is DMA-capable; the attribute is a read-only False there. --- main.c | 3 +++ py/circuitpy_mpconfig.h | 2 +- shared-bindings/supervisor/Runtime.c | 31 ++++++++++++++++++++++++ supervisor/port_heap.h | 10 ++++++++ supervisor/shared/port.c | 36 ++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index a80391af6df..0a9da2173bf 100644 --- a/main.c +++ b/main.c @@ -203,6 +203,9 @@ static void start_mp(safe_mode_t safe_mode) { #endif #if MICROPY_ENABLE_GC + #if !CIRCUITPY_ALL_MEMORY_DMA_CAPABLE + port_vm_heap_prefers_dma_capable = false; + #endif size_t heap_size = 0; _heap = _allocate_memory(safe_mode, "CIRCUITPY_HEAP_START_SIZE", CIRCUITPY_HEAP_START_SIZE, &heap_size); gc_init(_heap, _heap + heap_size); diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 8649b3e65a2..b3d527a66c3 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -101,7 +101,7 @@ extern void common_hal_mcu_enable_interrupts(void); #define MICROPY_GC_ALLOC_THRESHOLD (0) #define MICROPY_GC_SPLIT_HEAP (1) #define MICROPY_GC_SPLIT_HEAP_AUTO (1) -#define MP_PLAT_ALLOC_HEAP(size) port_malloc(size, false) +#define MP_PLAT_ALLOC_HEAP(size) port_vm_heap_grow(size) #define MP_PLAT_FREE_HEAP(ptr) port_free(ptr) #include "supervisor/port_heap.h" #define MICROPY_HELPER_LEXER_UNIX (0) diff --git a/shared-bindings/supervisor/Runtime.c b/shared-bindings/supervisor/Runtime.c index 3f75dad09a9..dbfa4ec4305 100644 --- a/shared-bindings/supervisor/Runtime.c +++ b/shared-bindings/supervisor/Runtime.c @@ -14,6 +14,7 @@ #include "shared-bindings/supervisor/Runtime.h" #include "shared-bindings/supervisor/SafeModeReason.h" +#include "supervisor/port_heap.h" #include "supervisor/shared/reload.h" #include "supervisor/shared/serial.h" #include "supervisor/shared/stack.h" @@ -266,6 +267,31 @@ MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_audio_obj, supervisor_runtime_g MP_PROPERTY_GETTER(supervisor_runtime_audio_obj, (mp_obj_t)&supervisor_runtime_get_audio_obj); +//| fast_heap: bool +//| """Whether the heap grows into internal RAM first on a board that also has PSRAM. +//| Internal RAM is faster, but displays, audio and other drivers need it for their +//| buffers. Create those first, then set this. Memory the heap already has stays where +//| it is, so set it early. It is ``False`` again when the next program starts. +//| +//| Read-only ``False`` on boards without this choice.""" +//| +#if !CIRCUITPY_ALL_MEMORY_DMA_CAPABLE +static mp_obj_t supervisor_runtime_get_fast_heap(mp_obj_t self) { + return mp_obj_new_bool(port_vm_heap_prefers_dma_capable); +} +MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_fast_heap_obj, supervisor_runtime_get_fast_heap); + +static mp_obj_t supervisor_runtime_set_fast_heap(mp_obj_t self, mp_obj_t state_in) { + port_vm_heap_prefers_dma_capable = mp_obj_is_true(state_in); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(supervisor_runtime_set_fast_heap_obj, supervisor_runtime_set_fast_heap); + +MP_PROPERTY_GETSET(supervisor_runtime_fast_heap_obj, + (mp_obj_t)&supervisor_runtime_get_fast_heap_obj, + (mp_obj_t)&supervisor_runtime_set_fast_heap_obj); +#endif + static const mp_rom_map_elem_t supervisor_runtime_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_usb_connected), MP_ROM_PTR(&supervisor_runtime_usb_connected_obj) }, { MP_ROM_QSTR(MP_QSTR_serial_connected), MP_ROM_PTR(&supervisor_runtime_serial_connected_obj) }, @@ -281,6 +307,11 @@ static const mp_rom_map_elem_t supervisor_runtime_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_display), MP_ROM_NONE }, #endif { MP_ROM_QSTR(MP_QSTR_audio), MP_ROM_PTR(&supervisor_runtime_audio_obj) }, + #if !CIRCUITPY_ALL_MEMORY_DMA_CAPABLE + { MP_ROM_QSTR(MP_QSTR_fast_heap), MP_ROM_PTR(&supervisor_runtime_fast_heap_obj) }, + #else + { MP_ROM_QSTR(MP_QSTR_fast_heap), MP_ROM_FALSE }, + #endif }; static MP_DEFINE_CONST_DICT(supervisor_runtime_locals_dict, supervisor_runtime_locals_dict_table); diff --git a/supervisor/port_heap.h b/supervisor/port_heap.h index ff37d02700d..72cdfb2a195 100644 --- a/supervisor/port_heap.h +++ b/supervisor/port_heap.h @@ -31,6 +31,16 @@ void *port_realloc(void *ptr, size_t size, bool dma_capable); // Check if the full buffer is in DMA-capable memory and meets cache alignment // requirements. DMA-capable memory is also accessible during flash operations. bool port_buffer_is_dma_capable(const void *ptr, size_t len); + +// While set, the VM heap grows into DMA-capable memory when that has room. A program sets it +// with supervisor.runtime.fast_heap. Cleared when the VM starts. +extern bool port_vm_heap_prefers_dma_capable; + +// Returns the next chunk of the VM heap. With the flag set it may also add a DMA-capable chunk +// to the heap itself, with gc_add(), so only the heap's own growth code may call it. +void *port_vm_heap_grow(size_t size); +#else +#define port_vm_heap_grow(size) port_malloc(size, false) #endif size_t port_heap_get_largest_free_size(void); diff --git a/supervisor/shared/port.c b/supervisor/shared/port.c index 757c03b2d39..6c0dc9dc3db 100644 --- a/supervisor/shared/port.c +++ b/supervisor/shared/port.c @@ -65,6 +65,42 @@ MP_WEAK void port_free(void *ptr) { tlsf_free(heap, ptr); } +#if !CIRCUITPY_ALL_MEMORY_DMA_CAPABLE +bool port_vm_heap_prefers_dma_capable = false; + +void *port_vm_heap_grow(size_t size) { + if (!port_vm_heap_prefers_dma_capable) { + return port_malloc(size, false); + } + void *chunk = port_malloc(size, true); + if (chunk != NULL) { + return chunk; + } + chunk = port_malloc(size, false); + if (chunk == NULL) { + return NULL; + } + // DMA-capable memory has no room for a chunk this big. Add the largest one it does have + // room for as well, found to within 1 kB, so that it fills up before `chunk` is used. + size_t largest = 0; + for (size_t step = size / 2; step >= 1024; step /= 2) { + void *probe = port_malloc(largest + step, true); + if (probe != NULL) { + port_free(probe); + largest += step; + } + } + // Anything smaller than the first chunk of the heap is not worth a chunk of its own. + if (largest >= CIRCUITPY_HEAP_START_SIZE) { + void *extra = port_malloc(largest, true); + if (extra != NULL) { + gc_add(extra, (uint8_t *)extra + largest); + } + } + return chunk; +} +#endif + // Safely free an object that may have been allocated from either the GC heap or port heap. // If the pointer is on the GC heap, it will be freed by the GC automatically, so we do nothing. // If the pointer is not on the GC heap (i.e., allocated with port_malloc), we free it with port_free. From 0b4059cebfe229b332d6ac8f8737d080903c5be7 Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Sat, 14 Nov 2026 14:21:46 +0000 Subject: [PATCH 8/9] Leave fast_heap out where it does not apply As a constant the attribute cost 40 to 52 bytes on every board, and qtpy_m0_haxpress and trinket_m0_haxpress overflowed by 8 bytes in de_DE. --- shared-bindings/supervisor/Runtime.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/shared-bindings/supervisor/Runtime.c b/shared-bindings/supervisor/Runtime.c index dbfa4ec4305..a3aa052966f 100644 --- a/shared-bindings/supervisor/Runtime.c +++ b/shared-bindings/supervisor/Runtime.c @@ -273,7 +273,7 @@ MP_PROPERTY_GETTER(supervisor_runtime_audio_obj, //| buffers. Create those first, then set this. Memory the heap already has stays where //| it is, so set it early. It is ``False`` again when the next program starts. //| -//| Read-only ``False`` on boards without this choice.""" +//| Boards without this choice do not have this property.""" //| #if !CIRCUITPY_ALL_MEMORY_DMA_CAPABLE static mp_obj_t supervisor_runtime_get_fast_heap(mp_obj_t self) { @@ -309,8 +309,6 @@ static const mp_rom_map_elem_t supervisor_runtime_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_audio), MP_ROM_PTR(&supervisor_runtime_audio_obj) }, #if !CIRCUITPY_ALL_MEMORY_DMA_CAPABLE { MP_ROM_QSTR(MP_QSTR_fast_heap), MP_ROM_PTR(&supervisor_runtime_fast_heap_obj) }, - #else - { MP_ROM_QSTR(MP_QSTR_fast_heap), MP_ROM_FALSE }, #endif }; From 268da446cc092b4381751793bbe00a7916df3bce Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Sat, 14 Nov 2026 14:59:55 +0000 Subject: [PATCH 9/9] Give fast_heap a build flag, on for RP2350 only It was tied to CIRCUITPY_ALL_MEMORY_DMA_CAPABLE. That also built it for ESP32-P4 and ESP32-S31, where it is untested, and left out ESP32-S3 boards with PSRAM. CIRCUITPY_FAST_HEAP lets a port turn it on once it has been tried there. The docs now say that not every board has the property and that it changes nothing without PSRAM. --- main.c | 2 +- ports/raspberrypi/mpconfigport.mk | 1 + py/circuitpy_mpconfig.mk | 4 ++++ shared-bindings/supervisor/Runtime.c | 6 +++--- supervisor/port_heap.h | 2 ++ supervisor/shared/port.c | 2 +- 6 files changed, 12 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 0a9da2173bf..14aa2945737 100644 --- a/main.c +++ b/main.c @@ -203,7 +203,7 @@ static void start_mp(safe_mode_t safe_mode) { #endif #if MICROPY_ENABLE_GC - #if !CIRCUITPY_ALL_MEMORY_DMA_CAPABLE + #if CIRCUITPY_FAST_HEAP port_vm_heap_prefers_dma_capable = false; #endif size_t heap_size = 0; diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index efb5f77e9ee..8979b49c9f2 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -76,6 +76,7 @@ endif ifeq ($(CHIP_VARIANT),RP2350) # RP2350 has PSRAM that is not DMA-capable CIRCUITPY_ALL_MEMORY_DMA_CAPABLE = 0 +CIRCUITPY_FAST_HEAP ?= 1 # Default PICODVI on because it doesn't require much code in RAM to talk to HSTX. CIRCUITPY_PICODVI ?= 1 diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index e3ec3618e00..1e4e81e9d2f 100755 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -377,6 +377,10 @@ CFLAGS += -DCIRCUITPY_ESPCAMERA=$(CIRCUITPY_ESPCAMERA) CIRCUITPY__EVE ?= 0 CFLAGS += -DCIRCUITPY__EVE=$(CIRCUITPY__EVE) +# supervisor.runtime.fast_heap: a program can ask for the VM heap in DMA-capable RAM. +CIRCUITPY_FAST_HEAP ?= 0 +CFLAGS += -DCIRCUITPY_FAST_HEAP=$(CIRCUITPY_FAST_HEAP) + CIRCUITPY_FLOPPYIO ?= 0 CFLAGS += -DCIRCUITPY_FLOPPYIO=$(CIRCUITPY_FLOPPYIO) diff --git a/shared-bindings/supervisor/Runtime.c b/shared-bindings/supervisor/Runtime.c index a3aa052966f..9903202b573 100644 --- a/shared-bindings/supervisor/Runtime.c +++ b/shared-bindings/supervisor/Runtime.c @@ -273,9 +273,9 @@ MP_PROPERTY_GETTER(supervisor_runtime_audio_obj, //| buffers. Create those first, then set this. Memory the heap already has stays where //| it is, so set it early. It is ``False`` again when the next program starts. //| -//| Boards without this choice do not have this property.""" +//| Not every board has this property, and it changes nothing on a board without PSRAM.""" //| -#if !CIRCUITPY_ALL_MEMORY_DMA_CAPABLE +#if CIRCUITPY_FAST_HEAP static mp_obj_t supervisor_runtime_get_fast_heap(mp_obj_t self) { return mp_obj_new_bool(port_vm_heap_prefers_dma_capable); } @@ -307,7 +307,7 @@ static const mp_rom_map_elem_t supervisor_runtime_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_display), MP_ROM_NONE }, #endif { MP_ROM_QSTR(MP_QSTR_audio), MP_ROM_PTR(&supervisor_runtime_audio_obj) }, - #if !CIRCUITPY_ALL_MEMORY_DMA_CAPABLE + #if CIRCUITPY_FAST_HEAP { MP_ROM_QSTR(MP_QSTR_fast_heap), MP_ROM_PTR(&supervisor_runtime_fast_heap_obj) }, #endif }; diff --git a/supervisor/port_heap.h b/supervisor/port_heap.h index 72cdfb2a195..689c90afbf4 100644 --- a/supervisor/port_heap.h +++ b/supervisor/port_heap.h @@ -31,7 +31,9 @@ void *port_realloc(void *ptr, size_t size, bool dma_capable); // Check if the full buffer is in DMA-capable memory and meets cache alignment // requirements. DMA-capable memory is also accessible during flash operations. bool port_buffer_is_dma_capable(const void *ptr, size_t len); +#endif +#if CIRCUITPY_FAST_HEAP // While set, the VM heap grows into DMA-capable memory when that has room. A program sets it // with supervisor.runtime.fast_heap. Cleared when the VM starts. extern bool port_vm_heap_prefers_dma_capable; diff --git a/supervisor/shared/port.c b/supervisor/shared/port.c index 6c0dc9dc3db..8ab61e27e5b 100644 --- a/supervisor/shared/port.c +++ b/supervisor/shared/port.c @@ -65,7 +65,7 @@ MP_WEAK void port_free(void *ptr) { tlsf_free(heap, ptr); } -#if !CIRCUITPY_ALL_MEMORY_DMA_CAPABLE +#if CIRCUITPY_FAST_HEAP bool port_vm_heap_prefers_dma_capable = false; void *port_vm_heap_grow(size_t size) {