Improve code readability.

This commit is contained in:
giomba 2022-10-16 20:44:19 +02:00
parent 7a79fdd6d8
commit fc3ed64e9e

View File

@ -131,6 +131,17 @@ void set_pixel(uint16_t x, uint16_t y)
frames[0].data[y * 80 + x / 8] |= (1 << x % 8); frames[0].data[y * 80 + x / 8] |= (1 << x % 8);
} }
/**
* @brief Represents a program running on the SM of a PIO.
*
*/
typedef struct PIORun
{
PIO pio; //< executing PIO
uint offset; //< PIO memory offset for program
uint sm; //< executing SM
} PIORun;
int main() int main()
{ {
setup_clocks(); setup_clocks();
@ -139,6 +150,7 @@ int main()
// clear frame buffer // clear frame buffer
memset(&frames[0].data[0], 0, sizeof(frames[0].data)); memset(&frames[0].data[0], 0, sizeof(frames[0].data));
// draw a cross
// horizontal lines // horizontal lines
for (uint16_t x = 0; x < 640; ++x) for (uint16_t x = 0; x < 640; ++x)
{ {
@ -154,47 +166,52 @@ int main()
set_pixel(640, y); set_pixel(640, y);
} }
// PIO and state machines // Running programs on PIO
PIO pio = pio0; PIORun vga_hsync, vga_vsync, vga_pixel;
vga_hsync.pio = vga_vsync.pio = vga_pixel.pio = pio0;
// Prepare frame interrupt // Prepare frame interrupt
pio_set_irq1_source_enabled(pio, pis_interrupt1, true); // Frame interrupt is asserted on PIO0 interrupt 1 by SM1
irq_set_exclusive_handler(PIO0_IRQ_1, new_frame_handler); pio_set_irq1_source_enabled(vga_vsync.pio, pis_interrupt1,
irq_set_enabled(PIO0_IRQ_1, true); true); // allow SM1 of PIO to fire the interrupt
irq_set_exclusive_handler(PIO0_IRQ_1, new_frame_handler); // set handler for IRQ1 of PIO
irq_set_enabled(PIO0_IRQ_1, true); // enable interrupt
// VGA HSYNC program // VGA HSYNC program
uint offset1 = pio_add_program(pio, &vga_hsync_program); printf("Starting VGA hsync machine... ");
uint sm1 = pio_claim_unused_sm(pio, true); if (!pio_can_add_program(vga_hsync.pio, &vga_hsync_program))
printf("Starting VGA hsync machine on %u...\n", sm1); panic("cannot add program");
vga_hsync_program_init(pio, sm1, offset1); vga_hsync.offset = pio_add_program(vga_hsync.pio, &vga_hsync_program);
printf("Feeding horizontal sync...\n"); vga_hsync.sm = pio_claim_unused_sm(vga_hsync.pio, true);
vga_hsync_program_init(vga_hsync.pio, vga_hsync.sm, vga_hsync.offset);
// low pulse is X pixels long, and SM runs at 4x of pixel clock // low pulse is X pixels long, and SM runs at 4x of pixel clock
pio_sm_put_blocking(pio, sm1, (64 - 1) * 4); pio_sm_put_blocking(vga_hsync.pio, vga_hsync.sm, (64 - 1) * 4);
pio_sm_put_blocking(pio, sm1, (776 - 1) * 4); pio_sm_put_blocking(vga_hsync.pio, vga_hsync.sm, (776 - 1) * 4);
printf("OK\n");
// VGA VSYNC program // VGA VSYNC program
uint offset2 = pio_add_program(pio, &vga_vsync_program); printf("Starting VGA vsync machine... ");
uint sm2 = pio_claim_unused_sm(pio, true); if (!pio_can_add_program(vga_vsync.pio, &vga_vsync_program))
printf("Starting VGA vsync machine on %u...\n", sm2); panic("cannot add program");
vga_vsync_program_init(pio, sm2, offset2); vga_vsync.offset = pio_add_program(vga_vsync.pio, &vga_vsync_program);
printf("Feeding vertical sync...\n"); vga_vsync.sm = pio_claim_unused_sm(vga_vsync.pio, true);
vga_vsync_program_init(vga_vsync.pio, vga_vsync.sm, vga_vsync.offset);
// low pulse assert lasts for 3 horizontal lines, then adjust by one // low pulse assert lasts for 3 horizontal lines, then adjust by one
pio_sm_put_blocking(pio, sm2, 3 - 1); pio_sm_put_blocking(vga_vsync.pio, vga_vsync.sm, 3 - 1);
pio_sm_put_blocking(pio, sm2, 500 - 3 - 1); pio_sm_put_blocking(vga_vsync.pio, vga_vsync.sm, 500 - 3 - 1);
printf("OK\n"); printf("OK\n");
// VGA pixel program // VGA pixel program
printf("Starting VGA pixel machine... ");
if (!pio_can_add_program(vga_pixel.pio, &vga_free_run_program))
panic("cannot add program");
vga_pixel.offset = pio_add_program(vga_pixel.pio, &vga_free_run_program);
vga_pixel.sm = pio_claim_unused_sm(vga_pixel.pio, false);
vga_pixel_program_init(vga_pixel.pio, vga_pixel.sm, vga_pixel.offset);
pio_sm_put_blocking(vga_pixel.pio, vga_pixel.sm, 640 - 1);
printf("OK\n");
printf("pio_add_program...\n"); printf("Setting up DMA... ");
uint offset0 = pio_add_program(pio, &vga_free_run_program);
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);
printf("Setting up DMA...\n");
dma_channel = dma_claim_unused_channel(true); dma_channel = dma_claim_unused_channel(true);
dma_channel_config dma_config = dma_channel_get_default_config(dma_channel); dma_channel_config dma_config = dma_channel_get_default_config(dma_channel);
channel_config_set_transfer_data_size(&dma_config, DMA_SIZE_32); channel_config_set_transfer_data_size(&dma_config, DMA_SIZE_32);
@ -211,8 +228,9 @@ int main()
false); false);
dma_ready = true; dma_ready = true;
printf("OK\n");
printf("Start!\n"); printf("Now running.\n");
for (;;) for (;;)
tight_loop_contents(); tight_loop_contents();