/*

    This code is a QC program for PCI Industrial CARD

 

    Auther : Johnny Tseng

    Date : 1999/09/11

*/

 

#include <dos.h>

#include <stdio.h>

#include <conio.h>

 

#include "pci.h"

 

/* set default value for ioport */

unsigned int ioport = 0;

 

void testindustrialloopback (void)

{

  int loop, i, c, step = 0, errcount = 0;

  unsigned x1, x2, y1, y2, z1, z2;

  unsigned long x;

  unsigned long y;

  unsigned duration = 64;

 

  if (!ioport)

    return;

 

  gotoxy (10, 20);

  cprintf ("'+','-' : speed   space : pause  ESC : exit");

  for (loop = 0;; loop++) {

    for (i = 0; i < 32; i++) {

      x = (unsigned long)1 << i;

      x1 = x & 0x0000ffff;

      x2 = (x >> 16) & 0x0000ffff;

      y = (unsigned long)0x80000000 >> i;

      y1 = y & 0x0000ffff;

      y2 = y >> 16;

      outport (ioport, x1);

      outport (ioport + 2, x2);

      delay (1);

      outport (ioport + 4, 0xffff);

      outport (ioport + 6, 0xffff);

      delay (1);

      z1 = inport (ioport + 4);

      z2 = inport (ioport + 6);

      gotoxy (3, 18);

      cprintf ("Loop: %6d Error : %6d", loop + 1, errcount);

      gotoxy (3, 19);

      cprintf ("Testing Pin:%2d  Output: %04X%04X  Input:%04X%04X  Correct:%04X%04X",

          i, x2, x1, z2, z1, y2, y1);

      if (z1 != y1 || z2 != y2) {

    cputs (" Error\7 ");

    errcount++;

    //c = getch ();

    //if (c == 27)

    //  break;

      }

      else {

    cputs ("  OK   ");

    delay (duration);

    //c = getch ();

      }

      if (kbhit () || step) {

    c = getch ();

    if (c == 27)

      break;

    else if (c == ' ')

      step = !step;

    else if (c == '+') {

      if (duration > 1)

        duration >>= 1;

    }

    else if (c == '-') {

      if (duration < 512)

        duration <<= 1;

    }

    else if (!step)

      getch ();

      }

    }

    if (c == 27)

      break;

  }

  gotoxy (3, 17);

  cputs("Press any key when ready");

  if (kbhit ())

    getch();

}

 

void dumpbits (unsigned long x, int n)

{

  int i;

 

  for (i = n - 1; i >= 0; i--) {

    putch ((((unsigned long)1 << i) & x)? '1' : '0');

  }

}

 

void testindustrialoutput (void)

{

  unsigned long x, loop = 0L;

  int i, flag, iop, step = 0;

  unsigned x1, x2;

  char c = 0;

  unsigned duration = 64;

 

  gotoxy (10, 20);

  cprintf ("'+','-' : speed   space : pause  ESC : exit");

  outport (ioport, 0);

  outport (ioport + 2, 0);

  outport (ioport + 4, 0);

  outport (ioport + 6, 0);

  //outportb (ioport + 7, 0);

  for (flag = 0;;flag = !flag) {

    outport (iop , 0);

    outport (iop + 2, 0);

    if (flag) {

      iop = ioport + 4;

    } else {

      iop = ioport;

    }

    for (i = 0, x = (unsigned long)1; i < 32; i++, x <<= 1, loop++) {

      x1 = x & 0x0000ffff;

      x2 = (x >> 16) & 0x00000ffff;

      outport (iop, x1);

      outport (iop + 2, x2);

      gotoxy (10, 18);

      cprintf("Loop: %8ld", loop);

      gotoxy (10, 19);

      if (flag) {

    dumpbits (x, 32);

    dumpbits (0, 32);

      } else {

    dumpbits (0, 32);

    dumpbits (x, 32);

      }

      delay (duration);

      if (kbhit () || step) {

    c = getch ();

    if (c == 27)

      break;

    else if (c == ' ')

      step = !step;

    else if (c == '+') {

      if (duration > 1)

        duration >>= 1;

    }

    else if (c == '-') {

      if (duration < 512)

        duration <<= 1;

    }

    else if (!step)

      getch ();

      }

    } // for (i)

    if (c == 27)

      break;

  } // for (flag)

  if (kbhit ())

    getch ();

}

 

void testindustrialinput (void)

{

  unsigned long x, y, loop = 0L;

  unsigned char c;

 

  for (loop = 0;;loop++) {

    x = inport (ioport + 2);

    x = (x << 16) | inport (ioport);

    y = inport (ioport + 6);

    y = (y << 16) | inport (ioport + 4);

    gotoxy (10, 18);

    cprintf("Loop: %8ld", loop);

    gotoxy (10, 19);

    dumpbits (y, 32);

    dumpbits (x, 32);

    if (kbhit ()) {

      c = getch ();

      if (c == 27)

    break;

    } // for (i)

  } // for (flag)

  if (kbhit ())

    getch ();

}

 

void test8relay8photo (void)

{

  unsigned long loop = 0L;

  unsigned int x;

  unsigned int y;

  int i, ofs = 0, step = 0;

  char c = 0;

  unsigned duration = 64;

 

  gotoxy (5, 20);

  cprintf ("1-4 : channel number   '+','-' : speed   space : pause  ESC : exit");

 

  outport (ioport, 0);

  outport (ioport + 2, 0);

  outport (ioport + 4, 0);

  outport (ioport + 6, 0);

  x = 0x0100;

  for (;;) {

    outport (ioport + ofs, x | 0x00ff);

    y = inport (ioport + ofs);

    gotoxy (10, 18);

    cprintf("Loop: %8ld ", loop++);

    dumpbits (y, 8);

    cprintf ("   ");

    dumpbits (x >> 8, 8);

    if (x == 0x8000)

      x = 0x0100;

    else

      x <<= 1;

    delay (duration);

    if (kbhit () || step) {

      c = getch ();

      if (c == 27)

    break;

      else if (c == '1')

    ofs = 0;

      else if (c == '2')

    ofs = 2;

      else if (c == '3')

    ofs = 4;

      else if (c == '4')

    ofs = 6;

      else if (c == ' ')

    step = !step;

      else if (c == '+') {

    if (duration > 1)

      duration >>= 1;

      }

      else if (c == '-') {

    if (duration < 512)

      duration <<= 1;

      }

      else if (!step)

    getch ();

    }

    if (c == 27)

      break;

  } // for (;;)

  if (kbhit ())

    getch ();

}

 

void test16relay (void)

{

  unsigned long loop = 0L;

  unsigned int x;

  int i, ofs = 0, step = 0;

  char c = 0;

  unsigned duration = 64;

 

  gotoxy (5, 20);

  cprintf ("1-4 : channel number   '+','-' : speed   space : pause  ESC : exit");

 

  outport (ioport, 0);

  outport (ioport + 2, 0);

  outport (ioport + 4, 0);

  outport (ioport + 6, 0);

  x = 0x0001;

  for (;;) {

    outport (ioport + ofs, x);

    gotoxy (10, 18);

    cprintf("Loop: %8ld ", loop++);

    dumpbits (x, 16);

    if (x == 0x8000)

      x = 0x0001;

    else

      x <<= 1;

    delay (duration);

    if (kbhit () || step) {

      c = getch ();

      if (c == 27)

    break;

      else if (c == '1')

    ofs = 0;

      else if (c == '2')

    ofs = 2;

      else if (c == '3')

    ofs = 4;

      else if (c == '4')

    ofs = 6;

      else if (c == ' ')

    step = !step;

      else if (c == '+') {

    if (duration > 1)

      duration >>= 1;

      }

      else if (c == '-') {

    if (duration < 512)

      duration <<= 1;

      }

      else if (!step)

    getch ();

    }

    if (c == 27)

      break;

  } // for (;;)

  if (kbhit ())

    getch ();

}

 

void test16photo (void)

{

  unsigned long loop = 0L;

  unsigned int x;

  int i, ofs = 0, step = 0;

  char c = 0;

  unsigned duration = 64;

 

  gotoxy (5, 20);

  cprintf ("1-4 : channel number   '+','-' : speed   space : pause  ESC : exit");

 

  outport (ioport, 0);

  outport (ioport + 2, 0);

  outport (ioport + 4, 0);

  outport (ioport + 6, 0);

  for (loop = 0;;loop++) {

    outport (ioport + ofs, 0xffff);

    x = inport (ioport + ofs);

    gotoxy (10, 18);

    cprintf("Loop: %8ld ", loop++);

    dumpbits (x, 16);

    delay (duration);

    if (kbhit () || step) {

      c = getch ();

      if (c == 27)

    break;

      else if (c == '1')

    ofs = 0;

      else if (c == '2')

    ofs = 2;

      else if (c == '3')

    ofs = 4;

      else if (c == '4')

    ofs = 6;

      else if (c == ' ')

    step = !step;

      else if (c == '+') {

    if (duration > 1)

      duration >>= 1;

      }

      else if (c == '-') {

    if (duration < 512)

      duration <<= 1;

      }

      else if (!step)

    getch ();

    }

    if (c == 27)

      break;

  } // for (;;)

  if (kbhit ())

    getch ();

}

 

 

int main()

{

  int r;

  int flag = 1;

  char ch;

 

  while (flag) {

    clrscr();

    r =  GetPCICardInfo (PCI_DEVICE_ID_INDUSTRIAL, &ioport);

    if (r)

      r =  GetPCICardInfo (PCI_DEVICE_ID_INDUSTRIAL_OLD1, &ioport);

    if (r)

      r =  GetPCICardInfo (PCI_DEVICE_ID_INDUSTRIAL_OLD2, &ioport);

    if (r) {

      printf ("Can not find PCI-Industrial card.\n");

      return 1;

    }

    cputs ("\n\n\n\r");

    cputs ("                    PCI-Industrial CARD Q.C. Program V1.0\n\r");

    cputs ("                    =====================================\n\r");

    cputs ("\n\r");

    cprintf ("                     I/O Address: %04X\n\r", ioport);

    cputs ("\n\r");

    cputs ("                     (1): Loopback test\n\r");

    cputs ("                     (2): Output Test\n\r");

    cputs ("                     (3): Input Test\n\r");

    cputs ("                     (4): Test 8 relay 8 photo terminal board\n\r");

    cputs ("                     (5): Test 8 SSR 8 logic output terminal board\n\r");

    cputs ("                     (6): Test 16 relay terminal board\n\r");

    cputs ("                     (7): Test 16 photo terminal board\n\r");

    cputs ("                     ESC: EXIT\n\r");

    cputs ("                     Choose:");

    do {

      ch = getch();

    } while (ch != '1' && ch != '2' && ch != '3'

      && ch != '4' && ch != '5' && ch != '6'

      && ch != '7' && ch != 27);

    switch (ch) {

      case 27:

    flag = 0;

    break;

      case '1':

    testindustrialloopback ();

    break;

      case '2':

    testindustrialoutput ();

    break;

      case '3':

    testindustrialinput ();

    break;

      case '4':

    test8relay8photo ();

    break;

      case '5':

      case '6':

    test16relay ();

    break;

      case '7':

    test16photo ();

    break;

    }

  }

  return 0;

} /* end of main */