/* ----------------------------------------------------------- */

/* demo 5 : test RS-232 port (single VXC-182I + PC-COM1)       */

/* step 1 : RS-232 port of vxc182I connect to COM1 of PC       */

/* step 2 : run DEMO5.EXE				       */

/* NOTE   : PC_COM1=send=polling, receive=polling	       */

/*	    Vxc182I=send=polling, receive=interrupt	       */

/* ----------------------------------------------------------- */



#include "..\PIO.H"

#include "..\VxCard.h"



#define COM1	  0x3f8

#define COM2	  0x2f8

#define COM3	  0x3e8

#define COM4	  0x2e8



#define A1_8259   0x20

#define A2_8259   0xA0

#define EOI	      0x20



WORD init_low();

WORD restore_int();

void interrupt (*oldfunc) ();

static void interrupt irq_service();

int  rs232_val, rs232_status, irqmask, now_int_state;



WORD wBase, wIrq, wBase0, wBase1, wA0, wA1;

WORD wT=32000;



int main()

{

    int   t,tt,i,j,ii,jj,k;

    char  c, bSpeedMode;

    char  cCmd[50];

    int   iRet,iComPort;

    DWORD dd;

    WORD  wSubVendor,wSubDevice,wSubAux,wSlotBus,wSlotDevice;

    WORD  wBoards,wRetVal,t1,t2,t3,t4,t5,t0,tt0,tt1;



    clrscr();

    /* step 1: find address-mapping of PIO/PISO cards		  */

    wRetVal=PIO_DriverInit(&wBoards,0x161,0x01,0x00);  /* for VXC-182I */

	printf("\nThrer are %d VXC-182I Cards in this PC; Ret=%d",wBoards, wRetVal);

	if (wBoards==0) return 1;



    printf("\n--------------- The Configuration Space ---------------");

    for(i=0; i<wBoards; i++)

    {

        PIO_GetConfigAddressSpace(i,&wBase,&wIrq,&wSubVendor,&wSubDevice,

                    &wSubAux,&wSlotBus,&wSlotDevice);

        printf("\nCard_%d: wBase=%x,wIrq=%x,subID=[%x,%x],SlotID=[%x,%x]",i,wBase,wIrq,wSubVendor,wSubDevice,wSlotBus,wSlotDevice);

        printf(" --> ");

        ShowPioPiso(wSubVendor,wSubDevice,wSubAux);

    }



    PIO_GetConfigAddressSpace(0,&wBase,&wIrq,&t1,&t2,&t3,&t4,&t5);



    /* step 2: enable all D/I/O port				   */

    outportb(wBase,1);				          /* enable D/I/O  */

    Vxc_SetConfig(wBase, VXC_182i_CONFIG);    /* for VXC-142/142i/182i */

    Vxc_SetLed(wBase, VXC_182i_LED_ON); 	  /* led on	   */

    Vxc_SetSpeedMode(wBase,1,VXC_LOW_SPEED);  /* low speed	   */



    printf("\ninit 1");

    init_low();

    printf("\ninit 2");



    /* step 3: select RS-485 or RS-232 port 			   */

    wA1=wBase + VXC_PORT2;



    iRet=Vxc_OpenCom(wA1,115200L, "8N1");   /* RS-232 of VXC-182I=115200	   */

    if (iRet==0) printf("\n\n(1) Open card_0 OK");

    else         printf("\niRET=%d",iRet);



    iRet= Vxc_OpenCom(COM1, 115200L, "8N1");   /* RS_232 of PC-COM1=115200	   */

    if (iRet==0) printf("\n\n(1) Open PC COM OK");

	else         printf("\niRET=%d",iRet);

	

	outportb(wA1 + VXC_IER, 1); // Enable UART Interrupt



    i=0x55; j=0xaa; rs232_status=0;

    for (;;)

    {

        t0=0; t1=0; tt0=0; tt1=0;

        for (;;)

        {

            if (t0<32)

            {

                if ( 0 == Vxc_SendChar(COM1, i, 2) )  /* pc-send=polling */

                    t0++;

            }



            if (t1<32)

            {

                if ( 0 == Vxc_SendChar(wA1, j, 2) ) /* vxc-182i-send=polling */

                    t1++;

            }



            if ( 0 == Vxc_ReceiveChar(COM1, &k, 2) ) /* pc-receive=polling */

            {

				if (k!=j) {

                    printf("[Err0,%2x,%2x]",k,j); 

                    return 1; 

                }

                tt0++;

            }



            if (rs232_status==1)		/* vxc-182i-receive=interrupt */

            {

                k=rs232_val;

                rs232_status=0;

				if (k!=i) {

                    printf("[Err1,%2x,%2x]",k,i); 

                    return 1; 

                }

				tt1++;

            }



			if ((tt0>=32) && (tt1>=32))

				break;

        }

        i++; i=i&0xff;

        j++; j=j&0xff;

        if (kbhit()!=0) {getch(); break;}

        printf("\n[%2x, %2x]",i,j);

    }



    restore_int();

    PIO_DriverClose();

	return 0;

}



/* -------------------------------------------------------------- */

WORD init_low()

{

    DWORD dwVal;



    disable();

    outportb(wBase+5,0);			 /* disable all interrupt */



    if (wIrq<8)

    {

        oldfunc=getvect(wIrq+8);

        irqmask=inportb(A1_8259+1);

        outportb(A1_8259+1,irqmask & (0xff ^ (1 << wIrq)));

        setvect(wIrq+8, irq_service);

    } else {

        oldfunc=getvect(wIrq-8+0x70);

        irqmask=inportb(A1_8259+1);

        outportb(A1_8259+1,irqmask & 0xfb);			  /* IRQ2 */

        irqmask=inportb(A2_8259+1);

        outportb(A2_8259+1,irqmask & (0xff ^ (1 << (wIrq-8))));

        setvect(wIrq-8+0x70, irq_service);

    }



    outportb(wBase+0x2a,2); 	        /* non-invert COM1	  */

    now_int_state=0x0;		            /* now COM1 is low	  */

    outportb(wBase+5,2);		        /* enable DI1 interrupt	  */

	enable();

	return 0;

}



/* -------------------------------------------------------------- */



WORD restore_int()

{

    disable();



    outportb(wBase+5,0);			 /* disable all interrupt */

    if (wIrq<8)

    {

        setvect(wIrq+8,oldfunc);

    } else {

		setvect(wIrq-8+0x70,oldfunc);

	}



	return 0;

}



/* -------------------------------------------------------------- */



void interrupt irq_service()

{

    Vxc_SetLed(wBase, VXC_LED_OFF); 	/* led OFF	 */

    rs232_status=1;

    rs232_val=inportb(wA1);

    if (wIrq>=8) outportb(A2_8259,0x20);

    outportb(A1_8259,0x20);

    Vxc_SetLed(wBase, VXC_182i_LED_ON); /* led ON	 */

}

