The LM1881 chips have arrived, so I was able to continue with this project.
I started with wiring LM1881 directly to RaspberryPi and it turned out that with gpiots I get quite stable timings so I think I will abandon the idea of separate AVR microcontroller for preprocessing.
This is how the prototype looks like:
I have used the circuit taken directly from the datasheet:
There is one 680K resistor, two 0.1uF capacitors. I needed also one RCA plug and one socket to tie into Raspberry Pi video signal.
Here is the complete ciruit diagram prepared in PaperCAD
As mentioned earlier I'm using GPIO27 for the lightpen button and GPIO17 for the phototransistor.
New connections are VSYNC (pin 3) from LM1881 that goes to GPIO22 and ODD/EVEN (pin 7) that goes to GPIO23.
We have also a passthrough (plug and socket) to tie into composite video signal coming out of Raspberry Pi's A/V port.
We need to enable timestamping of interrupts on GPIO22 and GPIO17:
sudo modprobe gpiots gpios=17,22
We want to calculate the time difference between interrupt on GPIO22 (VSYNC) and interrupt on GPIO17 (light pen phototransistor) - when electron beam scanning the screen will reach the position we're point at.
Here is what I got:
Can you see the issue? The timings appear stable but there is a difference between every second interrupt.
Of course I forgot that the image is interlaced (even with all the flickering going on). The C64 outputs non-interlaced image so it just didn't occur to me.
This is the proper timing diagram:
I have used information from ODD/EVEN line from LM1881.
I tried to add some 77us offset to get result like this:
In the end it wasn't stable over whole screen area and I decided that I will just take only even or only odd frames anyway.
This reduces the problem to 25 readings per second and I can get at most about 300 lines resolution. That's plenty enough for a lightpen.