Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use VGA setup for another project #21

Open
Martius108 opened this issue Dec 5, 2024 · 3 comments
Open

Use VGA setup for another project #21

Martius108 opened this issue Dec 5, 2024 · 3 comments

Comments

@Martius108
Copy link

Martius108 commented Dec 5, 2024

Hello,

I successfully created this project and wanted to use this Pico - VGA hardware configuration (because the VGA cable is soldered to my Pico W) for another project (Pong game). I tried to code this in MicroPython but could't get a VGA output. So I started to create a test file at first which looks like this - but still no signal on the screen. This code compiles without problems in Thonny and was saved to the Pico W as main.py so that it should run on boot. Could you please tell me what I'm doing wrong?

`from machine import Pin
import rp2
import time

VGA timing constants for 640x480@60Hz

H_ACTIVE = 640
H_FRONT_PORCH = 16
H_SYNC = 96
H_BACK_PORCH = 48
V_ACTIVE = 480
V_FRONT_PORCH = 10
V_SYNC = 2
V_BACK_PORCH = 33

@rp2.asm_pio(
out_init=rp2.PIO.OUT_LOW,
sideset_init=(rp2.PIO.OUT_HIGH,) * 2,
autopull=True,
pull_thresh=32,
fifo_join=rp2.PIO.JOIN_TX
)
def vga_program():
# Active video area
pull(block) .side(0b11)
out(x, 32) .side(0b11)
pull(block) .side(0b11)

label("active")
out(pins, 1)            .side(0b11)
jmp(x_dec, "active")    .side(0b11)

# Horizontal sync (simplified timing)
nop()                   .side(0b11)  # Front porch
nop()                   .side(0b01)  # HSYNC pulse
nop()                   .side(0b11)  # Back porch

Create a simple test pattern (white screen)

line_words = H_ACTIVE // 32
test_line = [0xFFFFFFFF] * line_words

Initialize pins

video_pin = Pin(18, Pin.OUT) # GPIO18 -> Physical Pin 24
vsync_pin = Pin(19, Pin.OUT) # GPIO19 -> Physical Pin 25
hsync_pin = Pin(21, Pin.OUT) # GPIO21 -> Physical Pin 27

Initialize state machine

sm = rp2.StateMachine(
0,
vga_program,
freq=125_000_000, # Run at maximum frequency
sideset_base=Pin(19),
out_base=Pin(18),
set_base=Pin(21)
)

print("Starting VGA output...")
sm.active(1)

Main loop with VSYNC handling

vsync_counter = 0
try:
while True:
# Output one complete frame
for line in range(V_ACTIVE):
# Send line data
for word in test_line:
sm.put(word)

    # Handle VSYNC
    vsync_counter += 1
    if vsync_counter >= V_ACTIVE + V_FRONT_PORCH + V_SYNC + V_BACK_PORCH:
        vsync_counter = 0
    
    # Set VSYNC signal
    if V_ACTIVE + V_FRONT_PORCH <= vsync_counter < V_ACTIVE + V_FRONT_PORCH + V_SYNC:
        vsync_pin.value(0)  # VSYNC active (low)
    else:
        vsync_pin.value(1)  # VSYNC inactive (high)
    
    time.sleep_us(31)  # Line timing

except KeyboardInterrupt:
sm.active(0)
print("Stopped")`

@evansm7
Copy link
Owner

evansm7 commented Dec 13, 2024

No idea! But it looks like you're doing timing-sensitive stuff in Python -- I'd be impressed if it was anywhere near fast/predictable enough to do that. Even though C, it would be less frustrating to try this experiment in C I think... Good luck.

@Martius108
Copy link
Author

Thanks. That was a stupid idea :) I didn't know that this kind of project can only be done with C.

@evansm7
Copy link
Owner

evansm7 commented Dec 21, 2024

Ah but you don’t know until you try, experimenting is learning, and all that! (Who knows, maybe it’ll work :) but maybe worth trying it second :) :) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants