Files
esp32vga/main/display.c
T
jimmyandClaude Opus 5.5 9f84330b91 Move project to repo root and add Makefile
The Makefile wraps the build, USB and WiFi flashing, the serial monitor
and font generation, with toolchain paths that can be overridden.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 12:45:11 +13:00

273 lines
7.4 KiB
C

// VGA output for XIAO ESP32S3 Plus, 8-bit RGB332, double buffered.
//
// Wiring (resistors go from the XIAO pin to the VGA pin):
// D0 GPIO1 B0 820R -> VGA 3 D5 GPIO6 R0 2k -> VGA 1
// D1 GPIO2 B1 430R -> VGA 3 D6 GPIO43 R1 1k -> VGA 1
// D2 GPIO3 G0 2k -> VGA 2 D7 GPIO44 R2 510R -> VGA 1
// D3 GPIO4 G1 1k -> VGA 2 D8 GPIO7 HSYNC 100R -> VGA 13
// D4 GPIO5 G2 510R -> VGA 2 D9 GPIO8 VSYNC 100R -> VGA 14
// GND -> VGA 5, 6, 7, 8, 10
#include <string.h>
#include <sys/param.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_heap_caps.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lcd_panel_rgb.h"
#include "esp_log.h"
#include "display.h"
#define PIN_HSYNC 7 // D8
#define PIN_VSYNC 8 // D9
static const char *TAG = "display";
static const int data_pins[8] = {1, 2, 3, 4, 5, 6, 43, 44}; // D0..D7 = bit 0..7
uint8_t *canvas;
static esp_lcd_panel_handle_t panel;
static uint8_t *fbs[2];
static TaskHandle_t waiter;
static volatile uint32_t frames_started;
static volatile int front; // buffer currently on screen
static uint8_t *volatile snapshot_dst;
static SemaphoreHandle_t snapshot_done;
// Changed canvas area not yet copied to each buffer; empty when x1 <= x0
typedef struct {
int x0, y0, x1, y1;
} box_t;
static box_t dirty[2];
// Called from the ISR when the driver starts scanning out a frame. Any buffer
// swap requested before this point has now taken effect.
static bool IRAM_ATTR on_frame_start(esp_lcd_panel_handle_t p, const esp_lcd_rgb_panel_event_data_t *edata, void *ctx)
{
BaseType_t woken = pdFALSE;
frames_started++;
if (waiter) {
vTaskNotifyGiveFromISR(waiter, &woken);
}
return woken == pdTRUE;
}
void display_init(void)
{
esp_lcd_rgb_panel_config_t cfg = {
// Integer division of 240 MHz. A fractional divider (e.g. for
// 25.175 MHz) jitters, which blurs vertical edges on LCD monitors.
.clk_src = LCD_CLK_SRC_PLL240M,
.timings = {
.pclk_hz = PCLK_HZ,
.h_res = W,
.v_res = H,
.hsync_pulse_width = H_SYNC,
.hsync_back_porch = H_BACK,
.hsync_front_porch = H_FRONT,
.vsync_pulse_width = V_SYNC,
.vsync_back_porch = V_BACK,
.vsync_front_porch = V_FRONT,
// Active-high syncs idle low; active-low syncs idle high
.flags.hsync_idle_low = SYNC_POS,
.flags.vsync_idle_low = SYNC_POS,
},
.data_width = 8,
.bits_per_pixel = 8,
.num_fbs = 2,
.bounce_buffer_size_px = W * 10,
.dma_burst_size = 64,
.hsync_gpio_num = PIN_HSYNC,
.vsync_gpio_num = PIN_VSYNC,
.de_gpio_num = -1,
.pclk_gpio_num = -1,
.disp_gpio_num = -1,
.flags.fb_in_psram = 1,
};
for (int i = 0; i < 8; i++) {
cfg.data_gpio_nums[i] = data_pins[i];
}
waiter = xTaskGetCurrentTaskHandle();
ESP_ERROR_CHECK(esp_lcd_new_rgb_panel(&cfg, &panel));
esp_lcd_rgb_panel_event_callbacks_t cbs = {
.on_frame_buf_complete = on_frame_start,
};
ESP_ERROR_CHECK(esp_lcd_rgb_panel_register_event_callbacks(panel, &cbs, NULL));
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel));
ESP_ERROR_CHECK(esp_lcd_panel_init(panel));
ESP_ERROR_CHECK(esp_lcd_rgb_panel_get_frame_buffer(panel, 2, (void **)&fbs[0], (void **)&fbs[1]));
// Strongest drive so the pin's own resistance doesn't skew the DAC levels
for (int i = 0; i < 8; i++) {
gpio_set_drive_capability(data_pins[i], GPIO_DRIVE_CAP_3);
}
snapshot_done = xSemaphoreCreateBinary();
canvas = heap_caps_calloc(1, W * H, MALLOC_CAP_SPIRAM);
assert(canvas);
ESP_LOGI(TAG, "%dx%d @ %.1f Hz, double buffered", W, H, REFRESH_HZ);
}
uint8_t *display_fb(int buf)
{
return fbs[buf];
}
// Clip a rectangle to the screen; returns false if nothing is left
static bool clip(int *x, int *y, int *w, int *h)
{
if (*x < 0) {
*w += *x;
*x = 0;
}
if (*y < 0) {
*h += *y;
*y = 0;
}
if (*x + *w > W) {
*w = W - *x;
}
if (*y + *h > H) {
*h = H - *y;
}
return *w > 0 && *h > 0;
}
void canvas_dirty(int x, int y, int w, int h)
{
if (!clip(&x, &y, &w, &h)) {
return;
}
for (int b = 0; b < 2; b++) {
box_t *d = &dirty[b];
if (d->x1 <= d->x0) {
*d = (box_t){x, y, x + w, y + h};
} else {
d->x0 = MIN(d->x0, x);
d->y0 = MIN(d->y0, y);
d->x1 = MAX(d->x1, x + w);
d->y1 = MAX(d->y1, y + h);
}
}
}
void canvas_dirty_all(void)
{
canvas_dirty(0, 0, W, H);
}
bool display_sync(int buf)
{
box_t *d = &dirty[buf];
if (d->x1 <= d->x0) {
return false;
}
if (d->x0 == 0 && d->x1 == W) {
memcpy(fbs[buf] + d->y0 * W, canvas + d->y0 * W, (d->y1 - d->y0) * W);
} else {
gfx_restore(fbs[buf], d->x0, d->y0, d->x1 - d->x0, d->y1 - d->y0);
}
d->x1 = d->x0;
return true;
}
static void wait_frame(void)
{
uint32_t n = frames_started;
while (frames_started == n) {
ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(100));
}
}
// Runs in the display task between frames, when the front buffer can't change
static void serve_snapshot(void)
{
if (snapshot_dst) {
memcpy(snapshot_dst, fbs[front], W * H);
snapshot_dst = NULL;
xSemaphoreGive(snapshot_done);
}
}
void display_wait_frame(void)
{
wait_frame();
serve_snapshot();
}
void display_present(int buf)
{
// Ask the driver to show this buffer from the next frame, then wait for
// that frame to start before the caller draws into the other one
esp_lcd_panel_draw_bitmap(panel, 0, 0, W, H, fbs[buf]);
wait_frame();
front = buf;
serve_snapshot();
}
bool display_snapshot(uint8_t *dst)
{
if (!snapshot_done) {
return false;
}
xSemaphoreTake(snapshot_done, 0); // clear any stale give
snapshot_dst = dst;
if (xSemaphoreTake(snapshot_done, pdMS_TO_TICKS(1000)) != pdTRUE) {
snapshot_dst = NULL;
return false;
}
return true;
}
void gfx_fill(uint8_t *s, int x, int y, int w, int h, uint8_t c)
{
if (!clip(&x, &y, &w, &h)) {
return;
}
for (int j = y; j < y + h; j++) {
memset(s + j * W + x, c, w);
}
}
void gfx_restore(uint8_t *dst, int x, int y, int w, int h)
{
if (!clip(&x, &y, &w, &h)) {
return;
}
for (int j = y; j < y + h; j++) {
memcpy(dst + j * W + x, canvas + j * W + x, w);
}
}
int gfx_text_width(const font_t *f, int scale, const char *str)
{
return strlen(str) * f->w * scale;
}
int gfx_text(uint8_t *s, const font_t *f, int scale, int x, int y, const char *str, uint8_t fg, int bg)
{
for (; *str; str++, x += f->w * scale) {
int ch = (uint8_t)*str;
if (ch < FONT_FIRST || ch > FONT_LAST) {
ch = '?';
}
if (bg >= 0) {
gfx_fill(s, x, y, f->w * scale, f->h * scale, bg);
}
const uint8_t *g = f->bits + (ch - FONT_FIRST) * f->h * f->stride;
for (int row = 0; row < f->h; row++, g += f->stride) {
for (int col = 0; col < f->w; col++) {
if (g[col >> 3] & (0x80 >> (col & 7))) {
gfx_fill(s, x + col * scale, y + row * scale, scale, scale, fg);
}
}
}
}
return x;
}