#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include "lpc.h"

int bela[][2]={

	{ LPC_MODE_pRTS, LPC_MODE_pDTR },
	{ LPC_MODE_pRTS, LPC_MODE_nDTR },
	{ LPC_MODE_nRTS, LPC_MODE_pDTR },
	{ LPC_MODE_nRTS, LPC_MODE_nDTR },

	{ LPC_MODE_pDTR, LPC_MODE_pRTS },
	{ LPC_MODE_pDTR, LPC_MODE_nRTS },
	{ LPC_MODE_nDTR, LPC_MODE_pRTS },
	{ LPC_MODE_nDTR, LPC_MODE_nRTS }
};

static int	tt[ 10000 ];

int main( int argc, char *argv[] )
{
LPC	lpc;
int	i;
struct timeval	beg, end;

	memset( tt, 0x0, 10000 );
	printf( "%s start\n", argv[ argc - argc ] );
	lpc = LPC_Open();
	LPC_LogEnable( lpc, LPC_LOG_SERIAL );
	LPC_LogEnable( lpc, LPC_LOG_TARGET );

	printf( "LOGOPEN %d\n", LPC_LogOpen( lpc, "logfajl", 0 ) );
	printf( "SEROPEN %d\n", LPC_SerialOpen( lpc, "/dev/ttyS0", NULL ) );
	LPC_TargetCrystal( lpc, 10000000 );
	LPC_TargetBaud( lpc, 9600, 38400 );
/*
	for ( i = 0 ; i < 8 ; i++ ) {

		LPC_TargetControl( lpc, bela[i][0], bela[i][1] );
		printf( "TGTCONN %d :  %d\n", i, LPC_TargetConnect( lpc ) );
		getchar();
	}
*/

i = 3;
i = 7;
	LPC_TargetControl( lpc, bela[i][0], bela[i][1] );
	printf( "TGTCONN %d :  %d\n", i, LPC_TargetConnect( lpc ) );

gettimeofday( &beg, NULL );
/*
	for ( i = 0 ; i < 4096 ; i++ ) tt[ i ] = rand();
	LPC_TargetMemWrite( lpc, 0x40001600, tt, 4096 );
	LPC_TargetMemRead( lpc, 0x40001600, tt+4096, 4096 );
*/
gettimeofday( &end, NULL );
printf( "DELTA=%f ms\n", end.tv_sec * 1E3 - beg.tv_sec * 1E3 + end.tv_usec / 1E3 - beg.tv_usec / 1E3 );
printf( "ERROR? %d\n", memcmp( tt, tt+4096, 4096 ) );

	LPC_TargetMemRead( lpc, 0x0, tt, 256 );
for ( i = 0 ; i < 64 ; i++ ) printf( "%02x %08x\n", 4*i, tt[i] );

	LPC_TargetErase( lpc, 0, 27 );

	LPC_LogClose( lpc );
	LPC_Close( lpc );
	printf( "%s done\n", argv[ argc - argc ] );
	return( 0 );
}

