Compare commits

...

13 Commits

5 changed files with 184 additions and 74 deletions

View File

@ -10,12 +10,6 @@ set(CMAKE_CXX_STANDARD 17)
# Initialize SDK
pico_sdk_init()
# include(example_auto_set_url.cmake)
# Add blink example
# add_subdirectory(blink)
# Add hello world example
# add_subdirectory(hello_world)
add_compile_options(-Wall
-Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
-Wno-unused-function # we have some for the docs that aren't called
@ -41,6 +35,8 @@ pico_add_extra_outputs(ceda2vga)
target_link_libraries(ceda2vga
pico_stdlib
hardware_pio
hardware_dma
hardware_irq
)
# add url via pico_set_program_url

View File

@ -5,10 +5,10 @@ RUN useradd -m -G sudo builder
USER builder
WORKDIR /home/builder
RUN git clone -b master https://github.com/raspberrypi/pico-sdk.git && cd pico-sdk && git submodule update --init
# RUN git clone -b master https://github.com/raspberrypi/pico-sdk.git && cd pico-sdk && git submodule update --init
# RUN git clone -b master https://github.com/raspberrypi/pico-examples.git
# RUN git clone -b master https://github.com/raspberrypi/pico-extras.git
# RUN git clone -b master https://github.com/raspberrypi/pico-playground.git
ENV PICO_SDK_PATH=/home/builder/pico-sdk
ENV PICO_SDK_PATH=/home/builder/workspace/pico-sdk

14
README.md Normal file
View File

@ -0,0 +1,14 @@
# ceda2vga
A video adapter for Sanco 8000 series computers, to use your handy flat-screen VGA display.
## Setup
```
./setup.sh
```
## Development container
```
./work.sh
./build.sh
```

View File

@ -8,17 +8,18 @@
*/
#include "hardware/clocks.h"
#include "hardware/dma.h"
#include "hardware/pio.h"
#include "hardware/pll.h"
#include "hardware/structs/pio.h"
#include "pico/stdlib.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "vga.pio.h"
#include <stdio.h>
#define FRAMES 2
#define HPIXEL 640
@ -35,21 +36,18 @@ static Frame frames[FRAMES] = {0};
static void vga_pixel_program_init(PIO pio, uint sm, uint offset)
{
// magic definition of the called function?
pio_sm_config c = vga_free_run_program_get_default_config(offset);
pio_sm_config config = vga_free_run_program_get_default_config(offset);
// destination pins for OUT instructions
sm_config_set_out_pins(&c, 22, 1);
sm_config_set_out_pins(&config, 22, 1);
sm_config_set_out_shift(&config, true, true, 0);
pio_gpio_init(pio, 22);
pio_sm_set_consecutive_pindirs(pio, sm, 22, 1, true);
sm_config_set_wrap(&config, offset + vga_free_run_wrap_target, offset + vga_free_run_wrap);
pio_sm_init(pio, sm, offset, &c);
pio_sm_init(pio, sm, offset, &config);
pio_sm_set_clkdiv_int_frac(pio, sm, 1, 0);
pio->sm->shiftctrl = (1 << PIO_SM0_SHIFTCTRL_FJOIN_TX_LSB) |
(1 << PIO_SM0_SHIFTCTRL_OUT_SHIFTDIR_LSB) |
(1 << PIO_SM0_SHIFTCTRL_OUT_SHIFTDIR_LSB) |
(1 << PIO_SM0_SHIFTCTRL_AUTOPULL_LSB);
pio_sm_set_enabled(pio, sm, true);
}
@ -60,6 +58,7 @@ static void vga_hsync_program_init(PIO pio, uint sm, uint offset)
sm_config_set_set_pins(&config, 20, 1);
pio_gpio_init(pio, 20);
pio_sm_set_consecutive_pindirs(pio, sm, 20, 1, true);
sm_config_set_wrap(&config, offset + vga_hsync_wrap_target, offset + vga_hsync_wrap);
pio_sm_init(pio, sm, offset, &config);
@ -77,6 +76,7 @@ static void vga_vsync_program_init(PIO pio, uint sm, uint offset)
sm_config_set_set_pins(&config, 21, 1);
pio_gpio_init(pio, 21);
pio_sm_set_consecutive_pindirs(pio, sm, 21, 1, true);
sm_config_set_wrap(&config, offset + vga_vsync_wrap_target, offset + vga_vsync_wrap);
pio_sm_init(pio, sm, offset, &config);
@ -87,63 +87,154 @@ static void vga_vsync_program_init(PIO pio, uint sm, uint offset)
pio_sm_set_enabled(pio, sm, true);
}
void setup_clocks(void)
{
// disable resuscitation clock
clocks_hw->resus.ctrl = 0;
// before changing PLL, switch sys and ref cleanly away from their aux sources
hw_clear_bits(&clocks_hw->clk[clk_sys].ctrl, CLOCKS_CLK_SYS_CTRL_SRC_BITS);
while (clocks_hw->clk[clk_sys].selected != 0x1)
tight_loop_contents();
hw_clear_bits(&clocks_hw->clk[clk_ref].ctrl, CLOCKS_CLK_REF_CTRL_SRC_BITS);
while (clocks_hw->clk[clk_ref].selected != 0x1)
tight_loop_contents();
// set PLL at 126 MHz, and wait for it to stabilize
pll_init(pll_sys, 1, 1512 * MHZ, 6, 2);
// re-configure sys_clk to use PLL
clock_configure(clk_sys, CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX,
CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS, 126 * MHZ, 126 * MHZ);
// re-configure CLK PERI = clk_sys
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 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;
}
void set_pixel(uint16_t x, uint16_t y)
{
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()
{
// main clock = VCO / divider1 / divider2 = 126MHz
pll_init(pll_sys, 1, 1500 * MHZ, 6, 2);
setup_clocks();
stdio_init_all();
sleep_ms(1000);
// "draw" an horizontal dotted line (?)
for (unsigned int c = 0; c < 20; c += 3)
// clear frame buffer
memset(&frames[0].data[0], 0, sizeof(frames[0].data));
// draw a cross
// horizontal lines
for (uint16_t x = 0; x < 639; ++x) // last pixel high -> bad
{
frames[0].data[80 * 64 + 20 + c] = 0x55;
set_pixel(x, 0);
set_pixel(x, 239);
set_pixel(x, 479);
}
for (unsigned int c = 0; c < 20; c += 3)
// vertical lines
for (uint16_t y = 0; y < 480; ++y)
{
frames[0].data[80 * 128 + 20 + c] = 0x99;
set_pixel(0, y);
set_pixel(239, y);
set_pixel(638, y);
}
// PIO and state machines
PIO pio = pio0;
// Running programs on PIO
PIORun vga_hsync, vga_vsync, vga_pixel;
vga_hsync.pio = vga_vsync.pio = vga_pixel.pio = pio0;
sleep_ms(5000);
// VGA pixel program
uint offset0 = pio_add_program(pio, &vga_free_run_program);
uint sm0 = pio_claim_unused_sm(pio, true);
printf("Starting VGA pixel machine on %u...\n", sm0);
vga_pixel_program_init(pio, sm0, offset0);
// Prepare frame interrupt
// Frame interrupt is asserted on PIO0 interrupt 1 by SM1
pio_set_irq1_source_enabled(vga_vsync.pio, pis_interrupt1,
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
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);
vga_hsync_program_init(pio, sm1, offset1);
printf("Feeding horizontal sync...\n");
printf("Starting VGA hsync machine... ");
if (!pio_can_add_program(vga_hsync.pio, &vga_hsync_program))
panic("cannot add program");
vga_hsync.offset = pio_add_program(vga_hsync.pio, &vga_hsync_program);
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
pio_sm_put_blocking(pio, sm1, 64 * 4);
pio_sm_put_blocking(pio, sm1, 765 * 4);
pio_sm_put_blocking(vga_hsync.pio, vga_hsync.sm, (64 - 1));
pio_sm_put_blocking(vga_hsync.pio, vga_hsync.sm, (640 - 1));
printf("OK\n");
// VGA VSYNC program
uint offset2 = pio_add_program(pio, &vga_vsync_program);
uint sm2 = pio_claim_unused_sm(pio, true);
printf("Starting VGA vsync machine on %u...\n", sm2);
vga_vsync_program_init(pio, sm2, offset2);
printf("Feeding vertical sync...\n");
printf("Starting VGA vsync machine... ");
if (!pio_can_add_program(vga_vsync.pio, &vga_vsync_program))
panic("cannot add program");
vga_vsync.offset = pio_add_program(vga_vsync.pio, &vga_vsync_program);
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
pio_sm_put_blocking(pio, sm2, 3 - 1);
pio_sm_put_blocking(pio, sm2, 500 - 3 - 1);
pio_sm_put_blocking(vga_vsync.pio, vga_vsync.sm, 3 - 1);
pio_sm_put_blocking(vga_vsync.pio, vga_vsync.sm, 500 - 3 - 1);
printf("OK\n");
printf("Start!\n");
// 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");
while (true)
{
for (size_t i = 0; i < sizeof(frames[0].data);)
{
// feed pixel machine
if (!pio_sm_is_tx_fifo_full(pio, sm0))
pio_sm_put_blocking(pio, sm0, frames[0].data[i++]);
}
}
printf("Setting up DMA... ");
dma_channel = dma_claim_unused_channel(true);
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_read_increment(&dma_config, true);
channel_config_set_write_increment(&dma_config, false);
channel_config_set_dreq(&dma_config, DREQ_PIO0_TX2);
dma_channel_set_irq0_enabled(dma_channel, true);
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], 9600,
false);
dma_ready = true;
printf("OK\n");
printf("Now running.\n");
for (;;)
tight_loop_contents();
}

View File

@ -2,9 +2,15 @@
entrypoint_vga_free_run:
pull
mov y, osr
.wrap_target
mov x, y
wait irq 7
loop:
out pins, 1 [2]
jmp loop
out pins, 1 [3]
jmp x-- loop
.wrap
.program vga_hsync
@ -13,21 +19,24 @@ entrypoint_vga_hsync:
mov isr, osr
pull
loop:
nop
.wrap_target
set pins, 0 [1]
irq set 0
mov x, isr
set pins, 0
hsync_pulse:
jmp x-- hsync_pulse
jmp x-- hsync_pulse [3]
mov x, osr
set pins, 1
set pins, 1 [1]
set y, 24
hsync_back_porch:
jmp y-- hsync_back_porch [19]
irq set 7
mov x, osr [3]
hsync_idle:
jmp x-- hsync_idle
nop
jmp loop
jmp x-- hsync_idle [3]
.wrap
.program vga_vsync
@ -37,8 +46,7 @@ entrypoint_vga_vsync:
mov isr, osr
pull
loop:
.wrap_target
mov x, isr
vsync_pulse:
wait irq 0
@ -51,4 +59,5 @@ vsync_idle:
set pins, 1
jmp x-- vsync_idle
jmp loop
irq set 1
.wrap