STM32 TVout Upgrade to Work with Video Overlay - Help Needed

Post here first, or if you can't find a relevant section!
Post Reply
kevinmryan1903
Posts: 16
Joined: Tue Nov 01, 2022 12:36 pm

STM32 TVout Upgrade to Work with Video Overlay - Help Needed

Post by kevinmryan1903 »

Hi All,
I'm attempting to get the STM32 based TVOUT library (https://github.com/Tamakichi/ArduinoSTM32_TVout) to work or overlay onto a composite video images.
This is similar to the OverlayDemo sketch in (https://github.com/nootropicdesign/arduino-tvout-ve). HOWEVER this only works with ATMEGA chips, due to the differences with the register.
The OverlayDemo uses the falling edge of an external signal (from an LM1881 Video Sync Separator chip) to reset the output timer.
My code is below. Any help with this is greatly appreciated.




//STM32F103 TVOUT Mod to work with composite video overlay

#include <TTVout.h> //https://github.com/Tamakichi/ArduinoSTM32_TVout
#include <fontALL.h>

TTVout TV;

int zOff = 150;
int xOff = 0;
int yOff = 0;
int cSize = 50;
int view_plane = 64;
float angle = PI/60;
uint16_t cube2d[8][2];

void setup() {
pinMode(PB12, INPUT); //External Intrrupt Pin for reading LM1881 Video Sync Seperator chip
Serial.begin();
delay(1000);
//TV.adjust(2); // NTSC垂直同期補正(スクロールする場合)
//TV.begin(SC_448x216);
//TV.begin(SC_448x108);
//TV.begin(SC_224x216);
TV.begin(SC_224x108);
//TV.begin(SC_112x108);
TV.select_font(font6x8);

TV.println("I am the TVout\nlibrary running on a freeduino\n");
TV.delay(2500);
TV.println("I generate a PAL\nor NTSC composite video using\ninterrupts\n");
TV.delay(2500);
TV.clear_screen();
TV.println("Lets see what\nwhat I can do");
TV.delay(2000);

ExternalIntrruptSetup();
}

void loop() {

}


//Creating an Interrupt to reset or align the start of the NTSC TV overlay output with the camera composite video Vertical Sync Outpout signal
//The vertical sync signal will comes from a LM1881 Video Sync Seperator chip
void ExternalIntrruptSetup() {
attachInterrupt(digitalPinToInterrupt(PB12), StartOfNewFrame, FALLING);
}


// The code in this sub was copied from https://github.com/nootropicdesign/arduino-tvout-ve OverlayDemo sketch
// It was inteneded to initialize ATMega registers for video overlay capability.
// In order to work with STM32F103, I think I need to change this somehow for the STM registers?
void StartOfNewFrame() {
TCCR1A = 0;
// Enable timer1. ICES0 is set to 0 for falling edge detection on input capture pin.
TCCR1B = _BV(CS10);
// Enable input capture interrupt
TIMSK1 |= _BV(ICIE1);
// Enable external interrupt INT0 on pin 2 with falling edge.
EIMSK = _BV(INT0);
EICRA = _BV(ISC01);
}
// Required to reset the scan line when the vertical sync occurs
ISR(INT0_vect) {
display.scanLine = 0;
}
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: STM32 TVout Upgrade to Work with Video Overlay - Help Needed

Post by ozcar »

kevinmryan1903 wrote: Thu Dec 15, 2022 4:32 pm // The code in this sub was copied from https://github.com/nootropicdesign/arduino-tvout-ve OverlayDemo sketch
// It was inteneded to initialize ATMega registers for video overlay capability.
// In order to work with STM32F103, I think I need to change this somehow for the STM registers?
void StartOfNewFrame() {
TCCR1A = 0;
// Enable timer1. ICES0 is set to 0 for falling edge detection on input capture pin.
TCCR1B = _BV(CS10);
// Enable input capture interrupt
TIMSK1 |= _BV(ICIE1);
// Enable external interrupt INT0 on pin 2 with falling edge.
EIMSK = _BV(INT0);
EICRA = _BV(ISC01);
}
// Required to reset the scan line when the vertical sync occurs
ISR(INT0_vect) {
display.scanLine = 0;
}
I don't know why the OverlayDemo enables the timer input capture interrupt, as it does not appear to have an input capture ISR (it would be ISR(TIMER1_CAPT_vect) ). The other register flags are just what attachInterrupt(...) would set. So maybe you don't need to do anything more than the attachInterrupt() you already have. Unless there is something else there really using the input capture, but from a quick look I do not see it.
kevinmryan1903
Posts: 16
Joined: Tue Nov 01, 2022 12:36 pm

Re: STM32 TVout Upgrade to Work with Video Overlay - Help Needed

Post by kevinmryan1903 »

Wait, so if I attach a hardware interrupt falling edge to say pin PB3 (which the datasheet says is TIM2_CH2), when I pull PB3 LOW, will that automatically reset TIM2_CH2?
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: STM32 TVout Upgrade to Work with Video Overlay - Help Needed

Post by ozcar »

kevinmryan1903 wrote: Fri Dec 16, 2022 5:18 pm Wait, so if I attach a hardware interrupt falling edge to say pin PB3 (which the datasheet says is TIM2_CH2), when I pull PB3 LOW, will that automatically reset TIM2_CH2?
Have you got any of the demos that come with the STM32 TVOUT to work?

From what I can see, the OverlayDemo does not reset the timer peripheral of the chip. Rather, it detects when the vertical sync occurs, and at that time sets display.scanLine = 0. It does that by setting up an interrupt routine that is called for a falling signal on Arduino pin 2, which is evidently horizontal sync line. If you have an equivalent signal line, then attachInterrupt(...) could work. But do you have such a signal?

I'm too lazy to try to translate the STM32 TVOUT pages to understand it properly, but it looks like is uses PA1 for sync pulses, and has both vertical and horizontal sync on the same pin. If that is the case you have to be able to tell them apart and that would take more than a simple attachInterrupt().

Maybe you could use set_vbi_hook() instead.
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: STM32 TVout Upgrade to Work with Video Overlay - Help Needed

Post by ozcar »

ozcar wrote: Sat Dec 17, 2022 6:55 am
kevinmryan1903 wrote: Fri Dec 16, 2022 5:18 pm Wait, so if I attach a hardware interrupt falling edge to say pin PB3 (which the datasheet says is TIM2_CH2), when I pull PB3 LOW, will that automatically reset TIM2_CH2?
Have you got any of the demos that come with the STM32 TVOUT to work?

From what I can see, the OverlayDemo does not reset the timer peripheral of the chip. Rather, it detects when the vertical sync occurs, and at that time sets display.scanLine = 0. It does that by setting up an interrupt routine that is called for a falling signal on Arduino pin 2, which is evidently vertical sync line. If you have an equivalent signal line, then attachInterrupt(...) could work. But do you have such a signal?

I'm too lazy to try to translate the STM32 TVOUT pages to understand it properly, but it looks like is uses PA1 for sync pulses, and has both vertical and horizontal sync on the same pin. If that is the case you have to be able to tell them apart and that would take more than a simple attachInterrupt().

Maybe you could use set_vbi_hook() instead.
kevinmryan1903
Posts: 16
Joined: Tue Nov 01, 2022 12:36 pm

Re: STM32 TVout Upgrade to Work with Video Overlay - Help Needed

Post by kevinmryan1903 »

I did get the original STM32 TVOUT, "stm32_DemoNTSC" (https://github.com/Tamakichi/ArduinoSTM32_TVout) to work .
I also got the "OverlayDemo" (https://github.com/nootropicdesign/arduino-tvout-ve) to work on an ATMEGA chip.

I think you are right in that the STM32 TVOUT uses timers and then sends out a vertical sync pulse on PinA1, which tells the monitor when to start the frame.
Whereas the OverlayDemo alread has the vertical sync pulse coming from the CCD Camera composite signal (or by way of the LM1881 Video Sync Separator chip). I've confirmed this falling edge square wave on my O-Scope, lines right up with the start of the composite video signal frame.
My latest code is below. The hardware pin interrupts work. I tun on and off some leds to confirm.
I think I need to reset the timers somehow. How would I use "set_vbi_hook() " ??
Thanks



//Creating an Interrupt to reset or align the start of the NTSC TV overlay output with the camera composite video Vertical Sync Outpout signal
//The vertical sync signal will comes from a LM1881 Video Sync Seperator chip
void ExternalIntrruptsSetup() {
//PA7 is TTVOUT composite signal output pin, PA7 is also TIM3_CH2
attachInterrupt(digitalPinToInterrupt(PB13), StartOfNewFrame, FALLING); //PB13 is TIM1_CH1N //Vertical Sync //525 lines per frame
//PA1 is TTVOUT Horozontal Sync Output Pin, PA1 is also TIM2_CH2
attachInterrupt(digitalPinToInterrupt(PB12), StartOfNewLine, FALLING); // //Horozontal Sync
}


// The code in this sub was copied from https://github.com/nootropicdesign/arduino-tvout-ve OverlayDemo sketch
// It was inteneded to initialize ATMega registers for video overlay capability.
// In order to work with STM32F103, I think I need to change this somehow for the STM registers?
int cnt;
void StartOfNewFrame() {
cnt=cnt+1;
if (cnt<=30){digitalWrite(PB8, HIGH);} //Turns ON Blue LED
if (cnt>=30){digitalWrite(PB8, LOW);} //Turns OFF Blue LED
if (cnt>=60){cnt=0;}

}

int cnt2;
void StartOfNewLine() {

cnt2=cnt2+1;
if (cnt2<=30){digitalWrite(PB11, HIGH);} //Turns ON GREEN LED
if (cnt2>=30){digitalWrite(PB11, LOW);} //Turns OFF GREEN LED
if (cnt2>=60){cnt2=0;}

}
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: STM32 TVout Upgrade to Work with Video Overlay - Help Needed

Post by ozcar »

kevinmryan1903 wrote: Sat Dec 17, 2022 2:01 pm How would I use "set_vbi_hook() " ??
Sorry, way beyond my pay grade. I have not looked at TVOut before, and I only saw that function mentioned in a comment in the STM32 TTVout.cpp. For all I know, maybe the comment translates to "did not implement this" or "impossible" or something.

On checking now there seem to be about a thousand different flavours of TVOut. There is an Atmega "beta" version here that does provide that vbi hook function here: https://code.google.com/archive/p/ardui ... /downloads

But that is no use to you for STM32.

I also found a variant at:
https://github.com/bnn1044/stm32_osd

... which does not have set_vbi_hook(), but does have setBktmStartHook() and setBktmEndHook() for "blanking period start and end hooks", which sound sort-of equivalent.
User avatar
Bakisha
Posts: 139
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 5
Contact:

Re: STM32 TVout Upgrade to Work with Video Overlay - Help Needed

Post by Bakisha »

I did some experiments with TV/VGA and stm32f401/411 few years back, so i'm kind of familiar with NTSC/PAL timings. I did quick look at lm1881 datasheet, i would say that what are you trying to do is doable, but timing critical.
In short, all you need to do is to send your line data from scanline 23. That's about 1461.65 µS (63.55 * 23 ) after vertical sync output change to low. Then do it again every 63.55µS for next 200 lines (or whatever is your vertical resolution).
Hard part is to sync start of sending data every scanline that draw pixel. One way of doing that is to set timer to 63.55µS and read it's value once video sync output goes low, and use that value for CounterCompare to trigger interrupts to send pixels.
You don't need to go full DMA, one

Code: Select all

SPI.transfer(0xFE);
will be enough. if you are too early pixel will be on left side, if you are too late, pixel be on right side. If it flickers in left/right side, then you have to adjust timing depending of even/odd field (+/- 31.775µS).

IMHO, i would attach interrput on positive edge of burts output from lm1881, use digitalRead from vertical sync output and odd/even field to figure out in what scanline you are, and send data with SPI...
Post Reply

Return to “General discussion”