From 1d68748f504772aa532179a4fa8141d0500c6369 Mon Sep 17 00:00:00 2001 From: giomba Date: Sun, 16 Oct 2022 20:00:26 +0200 Subject: [PATCH] A cross. Sort of. Also, remember that program is 32 words per PIO, not SM. --- src/main.c | 59 ++++++++++++++++++++++++++++++++++++++--------------- src/vga.pio | 8 ++++---- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/main.c b/src/main.c index d040039..102b03e 100644 --- a/src/main.c +++ b/src/main.c @@ -15,11 +15,11 @@ #include "pico/stdlib.h" #include +#include +#include #include "vga.pio.h" -#include - #define FRAMES 2 #define HPIXEL 640 @@ -108,34 +108,56 @@ void setup_clocks(void) clock_configure(clk_peri, 0, CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLK_SYS, 126 * MHZ, 126 * MHZ); } +static bool dma_ready = false; static int dma_channel; -void dma_handler() +void new_frame_handler(void) { + // restart DMA + if (dma_ready) + dma_channel_set_read_addr(dma_channel, &frames[0].data[0], true); + + pio_interrupt_clear(pio0_hw, 1); +} + +void dma_handler(void) +{ + // acknoweledge DMA dma_hw->ints0 = 1 << dma_channel; - dma_channel_set_read_addr(dma_channel, &frames[0].data[5120], true); - // printf("."); +} + +void set_pixel(uint16_t x, uint16_t y) +{ + frames[0].data[y * 80 + x / 8] |= (1 << x % 8); } int main() { setup_clocks(); stdio_init_all(); - sleep_ms(5000); + sleep_ms(1000); - // "draw" an horizontal dotted line (?) - for (unsigned int c = 0; c < 5; c++) - { - frames[0].data[80 * 64 + 60 + c] = 0x55; - } + // clear frame buffer + memset(&frames[0].data[0], 0, sizeof(frames[0].data)); + // horizontal line + for (uint16_t x = 400; x < 620; ++x) + set_pixel(x, 64); + // vertical line + for (uint16_t y = 20; y < 460; ++y) + set_pixel(400, y); // PIO and state machines PIO pio = pio0; + // Prepare frame interrupt + pio_set_irq1_source_enabled(pio, pis_interrupt1, true); + irq_set_exclusive_handler(PIO0_IRQ_1, new_frame_handler); + irq_set_enabled(PIO0_IRQ_1, true); + // VGA HSYNC program uint offset1 = pio_add_program(pio, &vga_hsync_program); uint sm1 = pio_claim_unused_sm(pio, true); - printf("Starting VGA sync machine on %u...\n", sm1); + printf("Starting VGA hsync machine on %u...\n", sm1); vga_hsync_program_init(pio, sm1, offset1); printf("Feeding horizontal sync...\n"); // low pulse is X pixels long, and SM runs at 4x of pixel clock @@ -154,8 +176,12 @@ int main() printf("OK\n"); // VGA pixel program + + printf("pio_add_program...\n"); uint offset0 = pio_add_program(pio, &vga_free_run_program); - uint sm0 = pio_claim_unused_sm(pio, true); + printf("pio_claim_unused_sm...\n"); + uint sm0 = pio_claim_unused_sm(pio, false); + printf("sm0 = %u\n", sm0); printf("Starting VGA pixel machine on %u...\n", sm0); vga_pixel_program_init(pio, sm0, offset0); pio_sm_put_blocking(pio, sm0, 640 - 1); @@ -173,12 +199,13 @@ int main() irq_set_exclusive_handler(DMA_IRQ_0, dma_handler); irq_set_enabled(DMA_IRQ_0, true); - dma_channel_configure(dma_channel, &dma_config, &pio0_hw->txf[2], &frames[0].data[0], 80, true); + dma_channel_configure(dma_channel, &dma_config, &pio0_hw->txf[2], &frames[0].data[0], 38400, + false); - // dma_handler(); + dma_ready = true; printf("Start!\n"); for (;;) - ; + tight_loop_contents(); } diff --git a/src/vga.pio b/src/vga.pio index fc423eb..dbc8882 100644 --- a/src/vga.pio +++ b/src/vga.pio @@ -19,8 +19,7 @@ entrypoint_vga_hsync: pull loop: - nop - irq set 0 + irq set 0 [1] mov x, isr set pins, 0 @@ -31,8 +30,7 @@ hsync_pulse: set pins, 1 hsync_idle: jmp x-- hsync_idle - nop - jmp loop + jmp loop [1] .program vga_vsync @@ -56,4 +54,6 @@ vsync_idle: set pins, 1 jmp x-- vsync_idle + irq set 1 + jmp loop