/* Example of Digital I/O for PCI-D64HU

   Author: Moki

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

/* File level history (record changes for this file here.)

   v 0.0.0 12 Aug 2016 by Moki
     create, blah blah... */

#define _10MHz_BASE_CLOCK_

#include "PlxApi.h"

#if defined(PLX_LINUX)
    #include "ConsFunc.h"
    #include "PlxInit.h"
#endif

#include "PLX_Inc/Reg9054.h"
#include "D64HDLL.h"
#include "d64h_private.h"
#include "d64h_cpld.h"

int main(void)
{
	PLX_STATUS		rc;
	U16			u16Val;
	U32			u32Val;
	short			wCardNum, nErrCode;
	BYTE			bCardID;

	nErrCode = initLib();
	if( nErrCode != SUCCESS_NO_ERROR )
	{
		exit(0);
	}

	nErrCode = d64h_scan( &wCardNum, NULL );
	if( nErrCode == SUCCESS_NO_ERROR )
	{
		Cons_printf("found %d cards, ", wCardNum);
	}

	nErrCode = d64h_cardinfo_get( 0, &bCardID );
	if( nErrCode == SUCCESS_NO_ERROR )
	{
		Cons_printf("first card id = %d\n\n", bCardID);
	} else {
		Cons_printf("\n\n");
		exit(0);
	}
	rc = d64h_open( bCardID );

	if (rc != ApiSuccess)
	{
		Cons_printf("\n   ERROR: Unable to find or select a PLX device\n");
		PlxSdkErrorDisplay(rc);
		exit(-1);
	}

	Cons_printf("Demo dio : Direct DIO\n");
	nErrCode = d64h_do_writeline(bCardID, 0, 1);
	nErrCode = d64h_di_readline(bCardID, 0, &u16Val);
	Cons_printf("di line read =0x%08x\n", u16Val);
	nErrCode = d64h_do_readline(bCardID, 0, &u16Val);
	Cons_printf("do line readback =0x%08x\n", u16Val);

	nErrCode = d64h_do_writeline(bCardID, 0, 0);
	nErrCode = d64h_di_readline(bCardID, 0, &u16Val);
	Cons_printf("di line read =0x%08x\n", u16Val);
	nErrCode = d64h_do_readline(bCardID, 0, &u16Val);
	Cons_printf("do line readback =0x%08x\n", u16Val);

	nErrCode = d64h_do_writeport(bCardID, 0x55);
	nErrCode = d64h_di_readport(bCardID, &u32Val);
	Cons_printf("di read =0x%08x\n", u32Val);
	nErrCode = d64h_do_readport(bCardID, &u32Val);
	Cons_printf("do readback =0x%08x\n", u32Val);

	nErrCode = d64h_do_writeport(bCardID, 0xaa);
	nErrCode = d64h_di_readport(bCardID, &u32Val);
	Cons_printf("di read =0x%08x\n", u32Val);
	nErrCode = d64h_do_readport(bCardID, &u32Val);
	Cons_printf("do readback =0x%08x\n", u32Val);

	d64h_close(bCardID);

	return 0;
}

