Arduino Forum

Products => Arduino Due => Topic started by: Gericom on Mar 08, 2013, 07:48 pm

Title: Composite Video Generating
Post by: Gericom on Mar 08, 2013, 07:48 pm
I am working on generating composite video with the due. It uses the dac, and it works great already. I will post some photo's tomorrow.
Title: Re: Composite Video Generating
Post by: Gericom on Mar 09, 2013, 02:18 pm
Here are some pictures. (Yes, I use a camera as tv screen, since I have no tv in my room) The generated signal is 150x287, and uses 'fake' progressive: It only outputs one field.
Edit: ATM I am trying to get color working.
Title: Re: Composite Video Generating
Post by: Gericom on Mar 10, 2013, 03:21 pm
Hmm, implementing color seems to be very hard. I don't understand what I need to generate from the RGB values. Since there is a color burst + the signal itself.
Title: Re: Composite Video Generating
Post by: stimmer on Mar 10, 2013, 04:49 pm
Be careful about connecting the output of the DAC directly to a video input, as video inputs usually have a low impedance (75 ohms) and some of us have broken our DACs by connecting them directly to low impedance loads. Use a buffer amplifier or emitter follower.

Generating colour is very complicated. The colour subcarrier needs to be at a very specific frequency (3.579545 MHz for NTSC or 4.433619MHz for PAL) and I doubt the DAC can go that fast.
Title: Re: Composite Video Generating
Post by: Gericom on Mar 10, 2013, 05:00 pm

Be careful about connecting the output of the DAC directly to a video input, as video inputs usually have a low impedance (75 ohms) and some of us have broken our DACs by connecting them directly to low impedance loads. Use a buffer amplifier or emitter follower.

Generating colour is very complicated. The colour subcarrier needs to be at a very specific frequency (3.579545 MHz for NTSC or 4.433619MHz for PAL) and I doubt the DAC can go that fast.

I have it connected to my scope too (which has 1M impedance). But anyway, I could try making a R2R ladder or something. That would be much faster. But I am not sure if I have the right resistors.
Title: Re: Composite Video Generating
Post by: Markus_L811 on Mar 10, 2013, 07:56 pm

I have it connected to my scope too (which has 1M impedance).

I'm right yo you must have all parallel connected, so the smalest resistor will beat all other if you connect it and messure the impedance value you will get ~75 Ohm even with the 1M impedance. God will be an operational amplifire as voltage follower between the DAC and the rest of the connections or something else how has an high input impedance and an low output impedance.
Title: Re: Composite Video Generating
Post by: stimmer on Mar 17, 2013, 05:55 pm
The more I think about this the more I wonder if it might actually be possible - an R-2R ladder on the external memory pins (34-41) together with DMA should work reliably at 10.5MSPS and may even go as high as 21 or 28MSPS (I learned that doing the VGA library). The dealbreaker would be how close to the exact colour subcarrier frequency you have to get. In my experience, with old CRT TVs you have to be very accurate, but more recent LCD panels have more tolerance for the frequency and amplitude of the colour subcarrier being wrong.
Title: Re: Composite Video Generating
Post by: Gericom on Mar 17, 2013, 08:32 pm

The more I think about this the more I wonder if it might actually be possible - an R-2R ladder on the external memory pins (34-41) together with DMA should work reliably at 10.5MSPS and may even go as high as 21 or 28MSPS (I learned that doing the VGA library). The dealbreaker would be how close to the exact colour subcarrier frequency you have to get. In my experience, with old CRT TVs you have to be very accurate, but more recent LCD panels have more tolerance for the frequency and amplitude of the colour subcarrier being wrong.

That would be awesome, but first I need to add an op-amp to my ladder, otherwise the signal drops when connecting to my camera. Can you give me some lm#### ic numbers?

Edit: I ordered some samples of the AD811 (http://www.analog.com/en/all-operational-amplifiers-op-amps/operational-amplifiers-op-amps/ad811/products/product.html). It seems to be a good one. I hope I will get them in a week or 2.
Title: Re: Composite Video Generating
Post by: Gericom on Mar 19, 2013, 03:49 pm
I got it working on my r2r ladder without an op-amp now (I mean the signal b/w not color yet). I only needed to change the pin numbers.

Can someone tell me how to add dma?
Title: Re: Composite Video Generating
Post by: stimmer on Mar 19, 2013, 05:54 pm
I've just found some resistors for an R-2R ladder but they are only 1% tolerance. I was going to measure them more accurately and select some for the ladder, otherwise things start going wrong after 5 bits. Even then I think there will be too much noise on the digital outputs for full 8 bit accuracy. I'll see how well I can do.

DMA for the external memory port is quite hard to set up as it is AHB DMA - I use it in the VGA library but the code is impossible to follow  :smiley-mr-green: I'll try and do a simpler example once I've got the ladder done.
Title: Re: Composite Video Generating
Post by: Gericom on Mar 19, 2013, 06:07 pm

I've just found some resistors for an R-2R ladder but they are only 1% tolerance. I was going to measure them more accurately and select some for the ladder, otherwise things start going wrong after 5 bits. Even then I think there will be too much noise on the digital outputs for full 8 bit accuracy. I'll see how well I can do.

DMA for the external memory port is quite hard to set up as it is AHB DMA - I use it in the VGA library but the code is impossible to follow  :smiley-mr-green: I'll try and do a simpler example once I've got the ladder done.

I have 8 bit now. With 5% tolerance. (R is 10K each. I use 2 of them for the 2R. I know that this resistor is a bit too high)

Hmm, too bad. Isn't dma just setting a source and a destination pointer? On the ds for example there are functions like these:
Code: [Select]
void MI_DmaCopy32(u32 dmaNo, const void *src, void *dest, u32 size);
Which you use like this:
Code: [Select]
MI_DmaCopy32(3, &px[0], VramPixels,512 * 192);
Aren't there such functions on the due?
Title: Re: Composite Video Generating
Post by: Gericom on Mar 19, 2013, 06:56 pm
Here is an example of a picture generated with the due. (It is captured in jpeg using the camera I use for viewing it, so not the best quality)
Title: Re: Composite Video Generating
Post by: stimmer on Mar 19, 2013, 07:36 pm
With 5% resistors the R-2R ladder may lose too much accuracy. 5% of 255 is almost 13 which is a 4-bit number, so you could lose 4 bits of accuracy out of the 8, which is too much. Try connecting the analog output of the ladder to an analog input on the Due, then write a sketch which outputs 0-255 onto the ladder and reads the value on the analog input. You might find that the ladder isn't monotonic (sometimes when the input goes up by 1 the output voltage goes down instead of up).

THe DMA needs to be continuous (a bit like an audio buffer would be, just a lot faster)  which is why it's more complicated than just DMAing blocks of memory.
Title: Re: Composite Video Generating
Post by: Gericom on Mar 19, 2013, 07:56 pm

With 5% resistors the R-2R ladder may lose too much accuracy. 5% of 255 is almost 13 which is a 4-bit number, so you could lose 4 bits of accuracy out of the 8, which is too much. Try connecting the analog output of the ladder to an analog input on the Due, then write a sketch which outputs 0-255 onto the ladder and reads the value on the analog input. You might find that the ladder isn't monotonic (sometimes when the input goes up by 1 the output voltage goes down instead of up).

THe DMA needs to be continuous (a bit like an audio buffer would be, just a lot faster)  which is why it's more complicated than just DMAing blocks of memory.

Yea, but it works good enough for now. I will use other resistors (1%) later on.

Oh, okay. I realize how it need to work now. I will try to understand your code, since I only need to change the frequency and the source pointer.
Title: Re: Composite Video Generating
Post by: stimmer on Mar 20, 2013, 12:31 am
Here's the sketch I used to check my ladder:
Code: [Select]

char pins[]={34,35,36,37,38,39,40,41};

void writeval(int v){
  for(int i=0;i<sizeof(pins);i++){
    digitalWrite(pins[i],v&1);
    v>>=1;
  }
}

void setup() {
  Serial.begin(115200);
  analogReadResolution(12);
  for(int i=0;i<sizeof(pins);i++)
    pinMode(pins[i],OUTPUT);
}

void loop() {
  int l=0;
  for(int i=0;i<(1<<(sizeof(pins)));i++){
    writeval(i);
    delay(10);
    int m=analogRead(0);
    printf("%d : %d  d=%d\n",i,m,m-l);
    if(m<l)printf("!!!!!!ERROR!!!!!!!!!! non-monotonic\n");
    l=m;
  }
  delay(2000);
}


If you get errors then just swap the resistors around until they go away.
Title: Re: Composite Video Generating
Post by: Gericom on Mar 20, 2013, 03:16 pm
I've tried some things with dma, but now my arduino doesn't work anymore. I have seriously no idea what's the problem.

Edit: It works again. I pressed the erease and the reset button at the same time, and were able to upload a sketch again. After that everything worked well again.
Title: Re: Composite Video Generating
Post by: Markus_L811 on Mar 20, 2013, 04:35 pm

I've tried some things with dma, but now my arduino doesn't work anymore. I have seriously no idea what's the problem.

Edit: It works again. I pressed the erease and the reset button at the same time, and were able to upload a sketch again. After that everything worked well again.

Lucky men, I know this kind of happiness...
Title: Re: Composite Video Generating
Post by: Gericom on Mar 20, 2013, 06:32 pm


I've tried some things with dma, but now my arduino doesn't work anymore. I have seriously no idea what's the problem.

Edit: It works again. I pressed the erease and the reset button at the same time, and were able to upload a sketch again. After that everything worked well again.

Lucky men, I know this kind of happiness...

Yea, first I thought it has broken. But then I just tried some random things, and then by purpose the bossac com port connected to my pc.

By the way, I changed the loop (with asm code) and now it is 330 px width.
Title: Re: Composite Video Generating
Post by: stimmer on Mar 20, 2013, 07:32 pm
Success!  8) :smiley-mr-green: XD
Title: Re: Composite Video Generating
Post by: Gericom on Mar 20, 2013, 07:49 pm

Success!  8) :smiley-mr-green: XD

:smiley-eek: Cool! May I have your code?
Title: Re: Composite Video Generating
Post by: stimmer on Mar 20, 2013, 11:25 pm
The code needs tidying and commenting and debugging before I can share it (and before I forget how it works). I have got colour out of 3 monitors so far so it is looking good. This is using NTSC timings, I will try PAL another day. Resolution is  640x200 (or about 700x220 with overscan), but practically the Arduino won't be fast enough or have the ram to make use of all that. I may aim for a 160x200 mode.

Grayscale, colour bars and dot crawl :)
Title: Re: Composite Video Generating
Post by: Gericom on Mar 21, 2013, 07:17 am

The code needs tidying and commenting and debugging before I can share it (and before I forget how it works). I have got colour out of 3 monitors so far so it is looking good. This is using NTSC timings, I will try PAL another day. Resolution is  640x200 (or about 700x220 with overscan), but practically the Arduino won't be fast enough or have the ram to make use of all that. I may aim for a 160x200 mode.

Grayscale, colour bars and dot crawl :)


Sorry, but I wanted to make such library. I really appreciate your help, but I want to do things myself. Could you give me the dma stuff at least?
Title: Re: Composite Video Generating
Post by: stimmer on Mar 21, 2013, 02:50 pm
This is just the DMA section of the code:
Code: [Select]
// Continuous DMA -> SMC port (pins 34-41)
// by stimmer

// set up 2 DMA buffers and a linked list
// 888 and 222 is NTSC timings (~15750 buffers/sec)
uint8_t dmabuf[2][888];
uint32_t dmall[10]={(uint32_t)dmabuf[0],0x60000000,0x22060000+222,0x20000000,(uint32_t)(dmall+5),
                    (uint32_t)dmabuf[1],0x60000000,0x22060000+222,0x20000000,(uint32_t)dmall};
int cbuf=0; // current buffer index

void  DMAC_Handler()
{
    uint32_t dummy=REG_DMAC_EBCISR; // clear DMA interrupt flag
    dmall[2+5*cbuf]=0x22060000+222; // may not be needed

  // write to dmabuf[cbuf] here   
  // you only have 6 clock cycles per byte! 
  // write words and longs where you can for speed
 
    cbuf=1-cbuf;
}

void setup() {
 
  // set up memory controller
  REG_PMC_PCER0= 1<<9;
  REG_SMC_SETUP0=0; 
  REG_PIOC_PDR=0b1111111100;
  REG_PIOC_ABSR&=~0b1111111100; 
  REG_SMC_WPCR=0x534d4300;
  REG_SMC_SETUP0=0x00000000;
  REG_SMC_PULSE0=0X00000101;
  REG_SMC_CYCLE0=0X00000006; // this sets the DMA rate -  84/6=14 MByte/sec
  REG_SMC_TIMINGS0=0;
  REG_SMC_MODE0=0x00000000;
 
  // this code puts DMA priority above CPU.
  MATRIX->MATRIX_WPMR=0x4d415400;
  for(int i=0;i<6;i++)MATRIX->MATRIX_MCFG[i]=1;
  MATRIX->MATRIX_MCFG[4]=4;
  for(int i=0;i<8;i++)MATRIX->MATRIX_SCFG[i]=0x01000008;
  MATRIX->MATRIX_SCFG[6]=0x011200ff;
  MATRIX->MATRIX_PRAS0=MATRIX->MATRIX_PRAS1=MATRIX->MATRIX_PRAS2=0x00000000;
  MATRIX->MATRIX_PRAS3=MATRIX->MATRIX_PRAS4=MATRIX->MATRIX_PRAS5=0x00000000;
  MATRIX->MATRIX_PRAS6=0x00030000;
  MATRIX->MATRIX_PRAS7=MATRIX->MATRIX_PRAS8=0x00000000;

  // set up DMA
  REG_PMC_PCER1= 1<<7; 
  REG_DMAC_WPMR=DMAC_WPMR_WPKEY(0x444d4143);
  REG_DMAC_EN=1;
  REG_DMAC_GCFG=0x00;
  REG_DMAC_CFG5=0x10702200;
  REG_DMAC_DSCR5=(uint32_t)dmall;
  REG_DMAC_EBCIER=1<<5;
  NVIC_EnableIRQ(DMAC_IRQn);
 
  // start DMA
  REG_DMAC_CHER=1<<5;

}

void loop() {
 
}
Title: Re: Composite Video Generating
Post by: Gericom on Mar 21, 2013, 03:04 pm

This is just the DMA section of the code:
Code: [Select]
// Continuous DMA -> SMC port (pins 34-41)
// by stimmer

// set up 2 DMA buffers and a linked list
// 888 and 222 is NTSC timings (~15750 buffers/sec)
uint8_t dmabuf[2][888];
uint32_t dmall[10]={(uint32_t)dmabuf[0],0x60000000,0x22060000+222,0x20000000,(uint32_t)(dmall+5),
                   (uint32_t)dmabuf[1],0x60000000,0x22060000+222,0x20000000,(uint32_t)dmall};
int cbuf=0; // current buffer index

void  DMAC_Handler()
{
   uint32_t dummy=REG_DMAC_EBCISR; // clear DMA interrupt flag
   dmall[2+5*cbuf]=0x22060000+222; // may not be needed

 // write to dmabuf[cbuf] here  
 // you only have 6 clock cycles per byte!  
 // write words and longs where you can for speed
 
   cbuf=1-cbuf;
}

void setup() {
 
 // set up memory controller
 REG_PMC_PCER0= 1<<9;
 REG_SMC_SETUP0=0;  
 REG_PIOC_PDR=0b1111111100;
 REG_PIOC_ABSR&=~0b1111111100;  
 REG_SMC_WPCR=0x534d4300;
 REG_SMC_SETUP0=0x00000000;
 REG_SMC_PULSE0=0X00000101;
 REG_SMC_CYCLE0=0X00000006; // this sets the DMA rate -  84/6=14 MByte/sec
 REG_SMC_TIMINGS0=0;
 REG_SMC_MODE0=0x00000000;
 
 // this code puts DMA priority above CPU.
 MATRIX->MATRIX_WPMR=0x4d415400;
 for(int i=0;i<6;i++)MATRIX->MATRIX_MCFG[i]=1;
 MATRIX->MATRIX_MCFG[4]=4;
 for(int i=0;i<8;i++)MATRIX->MATRIX_SCFG[i]=0x01000008;
 MATRIX->MATRIX_SCFG[6]=0x011200ff;
 MATRIX->MATRIX_PRAS0=MATRIX->MATRIX_PRAS1=MATRIX->MATRIX_PRAS2=0x00000000;
 MATRIX->MATRIX_PRAS3=MATRIX->MATRIX_PRAS4=MATRIX->MATRIX_PRAS5=0x00000000;
 MATRIX->MATRIX_PRAS6=0x00030000;
 MATRIX->MATRIX_PRAS7=MATRIX->MATRIX_PRAS8=0x00000000;

 // set up DMA
 REG_PMC_PCER1= 1<<7;  
 REG_DMAC_WPMR=DMAC_WPMR_WPKEY(0x444d4143);
 REG_DMAC_EN=1;
 REG_DMAC_GCFG=0x00;
 REG_DMAC_CFG5=0x10702200;
 REG_DMAC_DSCR5=(uint32_t)dmall;
 REG_DMAC_EBCIER=1<<5;
 NVIC_EnableIRQ(DMAC_IRQn);
 
 // start DMA
 REG_DMAC_CHER=1<<5;

}

void loop() {
 
}


Thanks, but what if I want to transfer only one buffer at a time? (After calling  REG_DMAC_CHER=1<<5;)
Title: Re: Composite Video Generating
Post by: Gericom on Mar 29, 2013, 04:06 pm
The last few days, I worked on writing a wss. It was not easy, but now widescreen is possible. I have also made some progress on the color stuff. But not on the arduino yet. I first made an application on the pc to generate composite video signals from pictures. It works great. (I got another tool to get it colored again) I will work soon on color for the arduino.
Title: Re: Composite Video Generating
Post by: Gericom on Mar 30, 2013, 12:39 pm
Some progress on coloring is made, but I can't get those colors to be smooth.
On the picture you can see from top to bottom Red, Green and Blue. (The green is the most bad one. But you can see some green spots)
Title: Re: Composite Video Generating
Post by: Gericom on Apr 02, 2013, 04:44 pm
This is much better already. (Same picture as above):
Title: Re: Composite Video Generating
Post by: Gericom on Apr 03, 2013, 10:42 am
Light blue (#00c0c0):
Title: Re: Composite Video Generating
Post by: Gericom on Apr 03, 2013, 02:15 pm
Arduino logo:
Title: Re: Composite Video Generating
Post by: Gericom on Apr 03, 2013, 03:18 pm
I got the colors almost perfect now.   :smiley-mr-green:
The Arduino logo, the SNES logo, and an Itembox of mkdd:
Title: Re: Composite Video Generating
Post by: Gericom on Apr 03, 2013, 03:44 pm
Colors:
Title: Re: Composite Video Generating
Post by: Markus_L811 on Apr 03, 2013, 04:11 pm
Yes really impressiv work.
Title: Re: Composite Video Generating
Post by: Gericom on Apr 03, 2013, 04:56 pm

Yes really impressiv work.

Thanks! It will even become better when I change the resistors of my R2R ladder. They're a bit too high, so that's why the pictures are a bit dark. Here is another picture:
Title: Re: Composite Video Generating
Post by: Gericom on Apr 03, 2013, 06:39 pm
NSMBU worldmap (aka little details):
Title: Re: Composite Video Generating
Post by: Gericom on Apr 03, 2013, 07:39 pm
I just tested on a real tv, and it looks very nice! No color errors and such. Keep in mind that it works with a framebuffer, so you can draw on it in runtime. And you can use every color you want!
Title: Re: Composite Video Generating
Post by: Gericom on Apr 04, 2013, 03:03 pm
I've added a video op-amp, and the brightness is much better now.
Title: Re: Composite Video Generating
Post by: Gericom on Apr 04, 2013, 04:55 pm
I made the wss working again:
Title: Re: Composite Video Generating
Post by: Gericom on Apr 04, 2013, 05:23 pm
Another widescreen picture in the right proportions:
Title: Re: Composite Video Generating
Post by: Gericom on May 17, 2013, 06:15 pm
Proof of concept video:
http://www.youtube.com/watch?v=ecU-Zi--jNw
(please subscribe)
Title: Re: Composite Video Generating
Post by: brendanwilding on Jun 12, 2013, 02:56 am
This is so cool, well done!

I would love to see some more information about wiring etc.
Title: Re: Composite Video Generating
Post by: Gericom on Jun 12, 2013, 12:59 pm

This is so cool, well done!

I would love to see some more information about wiring etc.

Thanks!
It is a r2r ladder (kind of) and a video op-amp (which you can get for free). That's it.
Title: Re: Composite Video Generating
Post by: Gericom on Dec 13, 2013, 07:05 pm
I have started working on this a few days ago, and the video op-amp is not needed anymore now. It turned out that the the voltage ratios were not correct. I fixed it, and in combination with a new and better r2r ladder, it works much better. I also switched from 8 to 5 bits, cause that's just more suitable for a 1% r2r ladder.

There are still some color phase errors, but since pal uses phase alternation, those errors are not much noticable.
Title: Re: Composite Video Generating
Post by: BlackSnake on Dec 19, 2013, 04:55 am
Hi There.
I just got myself an Arduino due today and thought what better way to try it out then with your cool graphics :)
Do you have a library ? or any code I can run. Possibly a circuit diagram if it's not to much trouble of course.
You did a fantastic job on your project, well done
Thanks
BlackSnake
Title: Re: Composite Video Generating
Post by: Gericom on Dec 19, 2013, 08:05 am

Hi There.
I just got myself an Arduino due today and thought what better way to try it out then with your cool graphics :)
Do you have a library ? or any code I can run. Possibly a circuit diagram if it's not to much trouble of course.
You did a fantastic job on your project, well done
Thanks
BlackSnake

Thanks, I have a sketch you can run (it consists out of multiple files) and I will make a schematic. I will do this when I have time to do so.
Title: Re: Composite Video Generating
Post by: mano1979 on Mar 19, 2014, 03:30 am
Any update on your code and wiring? I'd love to try this!

Gr. Uit Amsterdam