Example #1
0
File: main.c Project: libor-m/adark
int main(int argc, char *argv[])	{
	#define CHECK(expr,cod) if(!(expr))	\
		{code=(cod); goto abort;}
	enum ArchonErrors	{
		AE_SUCCESS,AE_FORMAT,AE_MEMORY,
		AE_INT,AE_SOURCE,AE_DEST,AE_NULL
	};
	int code = 0; FILE *ff=NULL;
	#ifdef VERBOSE
	clock_t t0 = clock();
	static char *errors[AE_NULL] =	{ NULL,
		"Usage: archon4r0 [e|d] <source> <dest>",
		"Memory allocation error!",
		"Internal program error - sorry!",
		"Source has no read permission!",
		"Dest has no write permission!"
	};
	#endif	//VERBOSE
	setbuf(stdout,NULL);
	printf("%s\n",IDENT);
	CHECK(argc>=4,AE_FORMAT);
	
	ff = fopen(argv[2],"rb");
	CHECK(ff,AE_SOURCE);
	fseek(ff,0,SEEK_END);
	CHECK(geninit(ff),AE_MEMORY);
	fclose(ff);
	ff = fopen(argv[argc-1],"wb");
	CHECK(ff,AE_DEST);

	if(argv[1][0] == 'e')	{
		clock_t t1;
		CHECK(gencode(),AE_MEMORY);
		printf("memory: %d meg\n",memory>>20);
		printf("Processing...\t");
		
		//Suffix Array Construction
		t1 = clock();
		CHECK(compute()>=0,AE_INT);
		printime("SAC",t1);
		
		#ifdef VERIFY
		printf("Verifying...\t");
		t1 = verify();
		printf("\nresult: %s\n",t1?"bad":"good");
		#endif	//VERIFY
		
		CHECK(encode(ff)>=0,AE_INT);
		genprint();
	}else if(argv[1][0] == 'd')	{
Example #2
0
int main(int argc, char **argv)
{  
  int verb=0; // 1=verbosed output
  int testi2c=0; // test i2c data flow 
  int ccycle=0; // count clock cycles
  int analog0=0; // read in AN0 analog voltage
  int analog1=0; // read in AN1 analog voltage
  int analog3=0; // read in AN3 analog voltage
  int fd;
  char fileName[100]="/dev/i2c-1";
  int  address=0x00;
  unsigned char buf[10];
  unsigned char send[10];
  int i=0,j=0;
  int errcnt=0; // error count
  int nrpt=1; // number of times to repeate test function
  int rtime=0; // cycle time from PIC
  int t1=0,t2=0,dt=0; // start and end time of cycle count
  int ain0=0,ain1=0,ain3=0; // analog input voltage

  int optch=0;
  while(optch!=-1)
    {
      optch=getopt(argc,argv,"a:n:i013chvV");
      if(optch=='a')
	{
	  sscanf(optarg,"%X",&address);
	}
      if(optch=='n')
	{
	  nrpt=atoi(optarg); 
	}
      if(optch=='i')
	{
          testi2c=1;          
	}
      if(optch=='c')
	{
          ccycle=1;          
	}
      if(optch=='0')
	{
          analog0=1;          
	}
      if(optch=='1')
	{
          analog1=1;          
	}
      if(optch=='3')
	{
          analog3=1;          
	}
      if(optch=='v')
	{
          verb=1;
	}
      if(optch=='h')
	{
	  printusage();
	  return 0;
	}
      if(optch=='V')
	{
	  printversion();
	  return 0;
	}
    }

  if((address<0x03)||(address>0x77))
    {
      printusage();
      return -1;
    }

// open port for reading and writing
  if(verb==1) printf("Open %s\n", fileName);
  if((fd = open(fileName, O_RDWR)) < 0) 
  {
     perror("Failed to open i2c port");
     return -1;
  }

  if(verb==1) printf("Chip address 0x%02x\n", address);
  if(ioctl(fd, I2C_SLAVE, address) < 0) 
  {
     perror("Unable to get bus access to talk to slave");
     return -1;
  }

  if((verb==1)&&(testi2c==1)) printf("                    send         received\n"); 
  if(ccycle==1)
  {
     printf("reset timer\n");
     t1=printime();
     buf[0]=0x50; // reset PIC timer
     if((write(fd, buf, 1)) != 1) 
     {
          perror("Error writing to i2c slave");
          return -1;
     }
     printf("cycles\n");
  } 

  srand((unsigned int)time(NULL));
  for(j=0;j<nrpt;j++)
  {
     if(testi2c==1)
     {
        if(verb==1) printime();
        errcnt=0;
        for(i=0;i<4;i++)
        {
           buf[i+1]=rand();
           send[i]=buf[i+1];
        }
        buf[0]=2;
        if(verb==1) 
        {
           printf("0x%02x%02x%02x%02x",buf[1],buf[2],buf[3],buf[4]);
           printf("   ");
        }
        if((write(fd, buf, 5)) != 5) 
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,4)!=4) 
        {
           perror("Unable to read from slave");
           return -1;
        }
        else 
        {
           if(verb==1)printf("0x%02x%02x%02x%02x\n",buf[0],buf[1],buf[2],buf[3]); 
           if((send[0]!=buf[0])||(send[1]!=buf[1])||(send[2]!=buf[2])||(send[3]!=buf[3])) 
           {
              printime();
              printf("0x%02x%02x%02x%02x",send[0],send[1],send[2],send[3]);
              printf(" !=");
              printf("0x%02x%02x%02x%02x\n",buf[0],buf[1],buf[2],buf[3]); 
              errcnt++;
           }
        }
     }

     if(ccycle==1)
     {
        if(verb==1) t2=printime();
        buf[0]=0x51;
        if((write(fd, buf, 1)) != 1) 
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,4)!=4) 
        {
           perror("Unable to read from slave");
           return -1;
        }
        else 
        {
           if(verb==1)printf("0x%02x%02x%02x%02x ",buf[0],buf[1],buf[2],buf[3]); 
           rtime=16777216*buf[0]+65536*buf[1]+256*buf[2]+buf[3];
           if(verb==1) printf ("%d\n",rtime);
        }
     }

     if((analog0==1)||(analog1==1)||(analog3==1)) t2=printime();
     if(analog0==1)
     {
        buf[0]=0x40;
        if((write(fd, buf, 1)) != 1) 
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,2)!=2) 
        {
           perror("Unable to read from slave");
           return -1;
        }
        else 
        {
           if(verb==1)printf("0x%02x%02x ",buf[0],buf[1]);
           ain0=256*buf[0]+buf[1];
           printf ("%d ",ain0);
        }
     }

     if(analog1==1)
     {
        buf[0]=0x41;
        if((write(fd, buf, 1)) != 1) 
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,2)!=2) 
        {
           perror("Unable to read from slave");
           return -1;
        }
        else 
        {
           if(verb==1)printf("0x%02x%02x ",buf[0],buf[1]);
           ain1=256*buf[0]+buf[1];
           printf ("%d ",ain1);
        }
     }

     if(analog3==1)
     {
        buf[0]=0x43;
        if((write(fd, buf, 1)) != 1) 
        {
           perror("Error writing to i2c slave");
           return -1;
        }
        if(read(fd, buf,2)!=2) 
        {
           perror("Unable to read from slave");
           return -1;
        }
        else 
        {
           if(verb==1)printf("0x%02x%02x ",buf[0],buf[1]);
           ain3=256*buf[0]+buf[1];
           printf ("%d ",ain3);
        }
     }

     if((analog0==1)||(analog1==1)||(analog3==1)) printf("\n");

  }

  if(ccycle==1)
  {
    t2=printime();
    dt=t2-t1;
    printf("elapsed seconds %d for %d cycles\n",dt,rtime);
    if(dt>0) printf("%f s/cycle\n",((float)dt)/((float)rtime));
  }
  
  if(testi2c==1) printf("i2c errors %d\n",errcnt);

  return 0;
}