From 9f8f896c1d0406d882e292999146c322e14551ef Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Fri, 13 Nov 2026 16:30:51 -0800 Subject: [PATCH 1/4] espressif: write back the I2S buffer cache after each fill On the ESP32-P4 internal RAM is reached through the L1 cache. IDF writes the DMA buffer back right after the on_sent callback, but audiobusio fills the buffer later from a background callback, so the new samples can sit in the cache while the DMA plays the buffer's old contents. MP3 playback through I2SOut came out garbled. Write the buffer back after each fill. Only chips that cache internal RAM (SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE) compile it in. Measured on a Metro ESP32-P4 with an on-chip loopback (I2S1 RX reading the TX pins), slow.mp3, three runs: 0, 392, 0 stale blocks with the write-back on, off, on. Fixes #11873 Co-Authored-By: Claude Opus 5.5 --- ports/espressif/common-hal/audiobusio/__init__.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ports/espressif/common-hal/audiobusio/__init__.c b/ports/espressif/common-hal/audiobusio/__init__.c index dae711100d3..4843e457f1a 100644 --- a/ports/espressif/common-hal/audiobusio/__init__.c +++ b/ports/espressif/common-hal/audiobusio/__init__.c @@ -12,6 +12,10 @@ #include "bindings/espidf/__init__.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "soc/soc_caps.h" +#if defined(SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE) && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE +#include "esp_cache.h" +#endif #include "shared-module/audiocore/__init__.h" @@ -114,7 +118,18 @@ static void i2s_fill_buffer(i2s_t *self) { static void i2s_callback_fun(void *self_in) { i2s_t *self = self_in; + #if defined(SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE) && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE + void *buffer = self->next_buffer; + size_t buffer_size = self->next_buffer_size; + #endif i2s_fill_buffer(self); + #if defined(SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE) && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE + // Internal RAM is cached. IDF writes the buffer back right after on_sent, before + // we fill it here, so write it back again or the DMA plays stale data. + if (buffer != NULL) { + esp_cache_msync(buffer, buffer_size, ESP_CACHE_MSYNC_FLAG_DIR_C2M); + } + #endif } static bool i2s_event_interrupt(i2s_chan_handle_t handle, i2s_event_data_t *event, void *self_in) { From 5ce2597f11b18661d3047ce720ed8cc9c0deb878 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Mon, 16 Nov 2026 09:52:38 -0800 Subject: [PATCH 2/4] supervisor: let a nested background run reach queued callbacks background_callback_run_all() detached the whole queue before running it. A callback still waiting in the detached list could not be re-queued or run by a nested call, so a long callback blocked everything behind it. Take each callback off the live queue just before it runs instead, and stop at the tail seen on entry, so callbacks added meanwhile still wait for the next call. Co-Authored-By: Claude Opus 5.5 --- supervisor/shared/background_callback.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/supervisor/shared/background_callback.c b/supervisor/shared/background_callback.c index ffeb78bbb83..65a0bcbf4c0 100644 --- a/supervisor/shared/background_callback.c +++ b/supervisor/shared/background_callback.c @@ -69,11 +69,19 @@ void PLACE_IN_ITCM(background_callback_run_all)(void) { return; } ++background_prevention_count; - background_callback_t *cb = (background_callback_t *)callback_head; - callback_head = NULL; - callback_tail = NULL; - while (cb) { - background_callback_t *next = cb->next; + // Run the callbacks queued now, taking each one off the live queue just before it + // runs. A callback that allows background tasks (like a display auto-refresh) can + // then run the rest in a nested call. Stop at the current tail so callbacks added + // while running wait for the next call. + background_callback_t *last = (background_callback_t *)callback_tail; + while (callback_head) { + background_callback_t *cb = (background_callback_t *)callback_head; + callback_head = cb->next; + if (callback_head) { + callback_head->prev = NULL; + } else { + callback_tail = NULL; + } cb->next = cb->prev = NULL; background_callback_fun fun = cb->fun; void *data = cb->data; @@ -83,7 +91,9 @@ void PLACE_IN_ITCM(background_callback_run_all)(void) { fun(data); } CALLBACK_CRITICAL_BEGIN; - cb = next; + if (cb == last) { + break; + } } --background_prevention_count; CALLBACK_CRITICAL_END; From 38e026022c276aada5534398289f0dc5a539040f Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Mon, 16 Nov 2026 09:52:38 -0800 Subject: [PATCH 3/4] framebufferio: run background tasks during auto-refresh A full auto-refresh of a 720x1280 MIPI DSI panel takes about 700 ms. It runs inside a background callback, so the I2S refill callback could not run and audio skipped on every screen update. Allow background callbacks around the auto-refresh so the existing RUN_BACKGROUND_TASKS in _refresh_area() runs them. refresh_in_progress already stops a nested refresh of the same display. Co-Authored-By: Claude Opus 5.5 --- shared-module/framebufferio/FramebufferDisplay.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/shared-module/framebufferio/FramebufferDisplay.c b/shared-module/framebufferio/FramebufferDisplay.c index 72f0dd65311..d0d31faeace 100644 --- a/shared-module/framebufferio/FramebufferDisplay.c +++ b/shared-module/framebufferio/FramebufferDisplay.c @@ -13,6 +13,7 @@ #include "shared-bindings/time/__init__.h" #include "shared-module/displayio/__init__.h" #include "shared-module/displayio/display_core.h" +#include "supervisor/background_callback.h" #include "supervisor/shared/display.h" #include "supervisor/shared/tick.h" @@ -226,8 +227,8 @@ static bool _refresh_area(framebufferio_framebufferdisplay_obj_t *self, const di #if CIRCUITPY_WEB_WORKFLOW display_stream_area_updated(&self->core, &subrectangle); #endif - // Run background tasks so they can run during an explicit refresh. - // Auto-refresh won't run background tasks here because it is a background task itself. + // Run background tasks so they can run during a refresh. Auto-refresh allows + // them in framebufferio_framebufferdisplay_background(). RUN_BACKGROUND_TASKS; // Run USB background tasks so they can run during an implicit refresh. @@ -338,7 +339,12 @@ void framebufferio_framebufferdisplay_background(framebufferio_framebufferdispla _update_backlight(self); if (self->auto_refresh && (supervisor_ticks_ms64() - self->core.last_refresh) > self->native_ms_per_frame) { + // Auto-refresh runs inside a background callback, which blocks other callbacks. + // Allow callbacks so RUN_BACKGROUND_TASKS in _refresh_area() can run them. + // refresh_in_progress stops a nested refresh of this display. + background_callback_allow(); _refresh_display(self); + background_callback_prevent(); } } From f9327531c5838cfb771636a43ee2ecc3b6594063 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Mon, 16 Nov 2026 12:46:10 -0800 Subject: [PATCH 4/4] trinket_m0_haxpress: turn off paralleldisplaybus The de_DE build had 36 bytes of flash left and no longer fits. A parallel display bus needs more pins than the Trinket M0 has. Co-Authored-By: Claude Opus 5.5 --- ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk index 29b5865d419..4da33b82e21 100644 --- a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk +++ b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk @@ -11,3 +11,4 @@ EXTERNAL_FLASH_DEVICES = W25Q32BV LONGINT_IMPL = MPZ CIRCUITPY_CODEOP = 0 +CIRCUITPY_PARALLELDISPLAYBUS = 0