Arduino Due libraries (official and 3rd party)

Hi Guys,

I'm trying to implement the awesome TVOut library on the arduino due....

my results so far... Arduino DUE TVOut Library almost succeeded - YouTube

when I have the library working I'll upload it, for now, I think I'm getting somewhere...

by the way... this is the Sync Generation.... you may say it's like Grug from The Croods... but if you saw the video, it's working nice so far :wink:

volatile int syncON, syncOFF;
volatile int LineCount;
char v1;
int i, r;
int vLines;

#define HRes 744
#define VRes 240
#define ScreenRatioCorrection (VRes * 53 / 18) / HRes
const float AspectRatioY = 4301075 / 10000000;
#define LinesToWait 10
#define ArraySize int(HRes/8 * VRes)
#define ScreenTop 0
#define ScreenBot VRes - 1

#define NOP __asm__("nop\n\t")
#define NOP10 NOP; NOP; NOP; NOP; NOP; NOP; NOP; NOP; NOP; NOP
#define NOP100 NOP10;NOP10;NOP10;NOP10;NOP10;NOP10;NOP10;NOP10;NOP10;NOP10

#define NOPLineSync NOP100;NOP100;NOP10;NOP10;NOP10;NOP10;NOP10;NOP10;NOP;NOP;NOP;NOP;NOP;NOP;NOP//;NOP//NOP10
#define NOPPrePixels NOP100;NOP100;NOP100;NOP100;NOP//;NOP//;NOP//;NOP;NOP//;NOP//;NOP//;NOP  //NOPLineSync
#define NOPPixel400 NOP;NOP;NOP;NOP;NOP;
#define NOPPixel640 NOP//;NOP

void setup() {
  Serial.begin(115200);
  pinMode(2, OUTPUT);
  pinMode(23, OUTPUT);
  pinMode(3, OUTPUT);   // port B pin 25
  analogWrite(3, 255);  // sets up some other registers I haven't worked out yet
  REG_TC2_WPMR = 0x54494D00; // enable write to registers
  //               33222222222211111111110000000000
  //               10987654321098765432109876543210
  REG_TC2_CMR1 = 0b00000000000010011100010000000000;
  // 1/50/625*84000000 - 1 = 2688: 64uS PER LINE /PAL STD
  // 1/60/525*84000000 - 1 = 2669.5: 63.55uS PER LINE /NTSC STD
  REG_TC2_RC1 = 2670;
  REG_TC2_RA1 = 0;  // PWM value 4.7uS
  REG_TC2_CCR1 = 0b101;  // start counter
  REG_TC2_IER1 = 0b00010000; // enable interrupt on counter=rc
  REG_TC2_IDR1 = 0b11101111; // disable other interrupts
  NVIC_EnableIRQ(TC7_IRQn); // enable TC7 interrupts
  NVIC_SetPriority(TC7_IRQn, 0); //sets Interrupt Priority to top

  LineCount = 0;
  syncON = ~1 << 25; // PIOB.25-->Pin D2 on the board
  syncOFF = 1 << 25;

  draw_line(0, 0, 0, VRes - 1, 1);
  draw_line(0, VRes - 1, HRes - 1, VRes - 1, 1);
  draw_line(HRes - 1, VRes - 1, HRes - 1, 0, 1);
  draw_line(HRes - 1, 0, 0, 0, 1);

  draw_line(HRes / 2 - 1, 0, HRes / 2 - 1, VRes - 1, 1);
  draw_line(0, VRes / 2 - 1, HRes - 1, VRes / 2 - 1, 1);

  draw_circle(HRes / 2 - 1, VRes / 2 - 1, 240, 1, 0);
  DrawEllipse(HRes / 2 - 1, VRes / 2 - 1, 100, 50, 1, 0);
  draw_circle(HRes / 2 - 1, VRes / 2 - 1, 50, 2, 1);
  randomSeed(analogRead(0));

}

void TC7_Handler()  // Interrupt every 64.07uS (PALSTD)
{
  long dummy = REG_TC2_SR1; // vital - reading this clears some flag otherwise you get infinite interrupts start the Horizontal sync pulse
  
  // **************************
  // ***** Sync Generation ****
  // **************************
  PIOB->PIO_ODSR = syncON;
  if (LineCount <= ScreenBot) NOPLineSync; // Generates the 4.7uS 0V sync pulse
  if (LineCount == 248) {   syncON = 1 << 25;    syncOFF = ~1 << 25;    NOPLineSync;  }
  if (LineCount == 251) {   syncON = ~1 << 25;    syncOFF = 1 << 25;    NOPLineSync;  }
  if (LineCount == 263) {    LineCount = 0;    NOPLineSync;  }
  if (LineCount <= (ScreenBot) && LineCount >= (ScreenTop + LinesToWait)) {    i = (LineCount - LinesToWait) * HRes / 8;  }
  PIOB->PIO_ODSR = syncOFF;
  // **************************
  // ***** Sync Generation ****
  // **************************

  if ((LineCount) <= (ScreenBot) && (LineCount) >= (ScreenTop + LinesToWait))
  {
    NOPPrePixels;
    for (int j = 0; j < int(HRes / 8); j++) {
      v1 = screen[i + j] ;
      PIOA->PIO_ODSR = (v1 & 128) <<  7;//NOPPixel640;  // PIOA.14-->Pin D23 on the board
      PIOA->PIO_ODSR = (v1 &  64) <<  8;//NOPPixel640;
      PIOA->PIO_ODSR = (v1 &  32) <<  9;//NOPPixel640;
      PIOA->PIO_ODSR = (v1 &  16) << 10;//NOPPixel640;
      PIOA->PIO_ODSR = (v1 &   8) << 11;//NOPPixel640;
      PIOA->PIO_ODSR = (v1 &   4) << 12;//NOPPixel640;
      PIOA->PIO_ODSR = (v1 &   2) << 13;//NOPPixel640;
      PIOA->PIO_ODSR = (v1 &   1) << 14;//NOPPixel640;
    }  
    PIOA->PIO_ODSR = ~1 << 14;
  }
  LineCount++;

}

I thank Stimmer for the Due Timer and the TVOut library designer for this project, they were the inspiration...

Next... NTSC Video overlay (Video Experimenter @ Video Experimenter: Arduino shield that lets you do all kinds of experiments with video) :wink:

this is the Arduino MEGA overlay using Video Experimenter concept :Hud arduino mega - YouTube
cheers