From b3f0e8038dd700b8b6d45886bdae8472811bc18d Mon Sep 17 00:00:00 2001 From: whowechina Date: Wed, 1 Apr 2026 18:10:22 +0800 Subject: [PATCH] Double buffer for pico2 --- firmware/src/st7789.c | 23 ++++++++++++++++++----- firmware/src/st7789.h | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/firmware/src/st7789.c b/firmware/src/st7789.c index 5b1d7aa..bd274e1 100644 --- a/firmware/src/st7789.c +++ b/firmware/src/st7789.c @@ -47,7 +47,15 @@ static struct { int y; } scroll; -static uint16_t vram[HEIGHT * WIDTH]; +#ifdef PICO_RP2350 +static uint16_t vram_buf[2][HEIGHT * WIDTH]; +static int vram_index = 0; +#else +static uint16_t vram_buf[1][HEIGHT * WIDTH]; +#endif + +static uint16_t *vram = vram_buf[0]; + static void send_cmd(uint8_t cmd, const void *data, size_t len) { @@ -187,9 +195,12 @@ void st7789_dimmer(uint8_t level) void st7789_vsync() { dma_channel_wait_for_finish_blocking(ctx.spi_dma); + while (spi_is_busy(ctx.spi)) { + tight_loop_contents(); + } } -void st7789_flush(bool vsync) +void st7789_flush() { if (dma_channel_is_busy(ctx.spi_dma)) { return; @@ -203,9 +214,11 @@ void st7789_flush(bool vsync) vram, // read from crop.w * crop.h, // element count true); // start right now - if (vsync) { - st7789_vsync(); - } +#ifdef PICO_RP2350 + // flip vram buffer for next frame + vram_index = 1 - vram_index; + vram = vram_buf[vram_index]; +#endif } static void vram_dma(uint32_t offset, const void *src, bool inc, size_t pixels) diff --git a/firmware/src/st7789.h b/firmware/src/st7789.h index c625d5b..482ca5a 100644 --- a/firmware/src/st7789.h +++ b/firmware/src/st7789.h @@ -18,7 +18,7 @@ uint16_t st7789_get_crop_width(); uint16_t st7789_get_crop_height(); void st7789_dimmer(uint8_t level); void st7789_vsync(); -void st7789_flush(bool vsync); +void st7789_flush(); #define st7789_rgb32(r, g, b) ((r << 16) | (g << 8) | b) #define st7789_rgb565(rgb32) ((rgb32 >> 8) & 0xf800) | ((rgb32 >> 5) & 0x0780) | ((rgb32 >> 3) & 0x001f)