Subversion Repositories f9daq

Rev

Rev 111 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>

#include "wusbxx_dll.h" /* the header of the shared library */
#include "daq.h"

#define PRRES(X) printf(">>> %s -> %d\n",#X,(X))



/* definiram lokacije enot*/
//#define NTDC 23         /* TDC LeCroy 2277*/
#define NTGG 23          /* CAEN C423 */
#define NSCA 22          /* CAEN C257 */
#define NDIS 21          /* Philips 7106 */



#define END_MARKER 0xFAF5
int ctrlc=0;
int timer_out=0;

void SigInt (int sig)
{
    ctrlc = 1;
    timer_out=1;
   
}

struct sigaction oact;
void timerast (int signumber)
{
    timer_out = 1;
    printf("->>> TIMEOUT !!!\n");
}

void tmlnk (int tout)
{
    timer_out = 0;
    struct sigaction act;
    struct itimerval tdelay;

    act.sa_handler = timerast;
    sigemptyset (&act.sa_mask);
    act.sa_flags = 0;

    tdelay.it_value.tv_sec = tout / 100;
    tdelay.it_value.tv_usec = 10000 * (tout % 100);
    tdelay.it_interval.tv_sec = 0;
    tdelay.it_interval.tv_usec = 0;

    if (sigaction (SIGALRM, &act, &oact) < 0)
{
        perror ("sigaction(tmlnk)");
        exit (EXIT_FAILURE);
    }
    if (setitimer (ITIMER_REAL, &tdelay, NULL) < 0)
    {
        perror ("setitimer(tmlnk)");
        exit (EXIT_FAILURE);
    }
}

void tmulk ()
{
    struct itimerval tdelay;

    tdelay.it_value.tv_sec = 0;
    tdelay.it_value.tv_usec = 0;
    tdelay.it_interval.tv_sec = 0;
    tdelay.it_interval.tv_usec = 0;

    if (setitimer (ITIMER_REAL, &tdelay, NULL) < 0)
    {
        perror ("setitimer(tmulk)");
        exit (EXIT_FAILURE);
    }
    if (sigaction (SIGALRM, &oact, NULL) < 0)
    {
        perror ("sigaction(tmulk)");
        exit (EXIT_FAILURE);
    }
}




void CAMAC_status(void)
{
  unsigned short id,fpga,modid,inhibit,lam;
  unsigned short dum;
  

  long cres;
  int q,x;  
 
  short retval = CSSA_RQX(0,0,0,&cres,&q,&x);
  dum = cres;
  id   = (dum & 0xF000) >> 12;
  fpga = (dum & 0x0F00) >> 8;   
  modid =  (dum & 0x00F0) >> 4; 
  q      =  (dum & 0x0008) >> 3;
  x      =  (dum & 0x0004) >> 2;
  inhibit=  (dum & 0x0002) >> 1;
  lam    =  (dum & 0x0001);     
  printf ("[%d] CCUSB CSR ID=%x FPGA=%x modID=%x Q=%d X=%d INHIBIT=%d LAM=%d \n",retval, id,fpga,modid, q,x,inhibit,lam);
}

int daq::connect(){
    WUSBXX_load (NULL);
//    WUSBXX_open ((char *)"CC0126");
    WUSBXX_open ((char *)"CC0130");
    printf("daq::connect()\n");
    return 0;
}
  
int daq::disconnect(){ 
   /* zakljuci */
   if (udev == NULL) ;    WUSBXX_close ();
   printf("daq::disconnect()\n");
  return 0;
}

int daq::start(int thr_set,int time_set){
      int q,x;
      // printf("Nastavljam  threshold na %d in cas meritve na %d ms\n",thr_set,time_set);
      long dum;
      short retval;
      /* postavimo zeljeni prag prozenja */ 
      retval = CSSA_WQX(NDIS,0,17,thr_set,&q,&x);
      if (retval<0) printf ("USB error %d line %d\n",retval,__LINE__);

      retval = CSSA_WQX(NDIS,1,17,0,&q,&x);
      if (retval<0) printf ("USB error %d line %d\n",retval,__LINE__);
        
      /* stevec postavim na nic */
      retval = CSSA_RQX ( NSCA, 0, 9,&dum, &q,&x);
      if (retval<0) printf ("USB error %d line %d\n",retval,__LINE__);
   
      /* nastavim dolzino signala */
      retval = CSSA_WQX(NTGG,0,16,time_set,&q,&x);
      if (retval<0) printf ("USB error %d line %d\n",retval,__LINE__);

      retval = CSSA_RQX(NTGG,0,15,&dum,&q,&x);
      if (retval<0) printf ("USB error %d line %d\n",retval,__LINE__);
        
      usleep(time_set);
      return 0;
}


int daq::count(){
    int q,x;
    short retval=0;    
    /* prestejem sunke na posameznem kanalu */    
      for ( int ch=0 ; ch<16 ; ch ++ ) {
        long data=0;
        retval = CSSA_RQX( NSCA, ch, 0, &data, &q,&x);
        retval = CSSA_RQX( NSCA, ch, 0, &data, &q,&x);
        if (retval<0) printf ("USB error %d line %d\n",retval,__LINE__);
        

                // CC USB ne precita v redu CAEN C257 scalerja, 
                // kanali so zamaknjeni za 1, ce je klic samo en
                // vrednost kanala 0 se pristeje h kanalom 8..16, zato ga ne uporabljamo. 
                // vajo izvedemo le s kanali 1..15.
        gData[ch]=data;
        printf("[%d] Kanal:%d Stevilo sunkov: %ld \n",retval,ch,data );
      }
      for ( int ch=0 ; ch<16 ; ch ++ ) if (gData[ch]>0) gData[ch]--; 
     
return 0;
}    


int daq::save(){
    /* shrani */
    FILE *fp;
     int ch;
     if ( ( fp = fopen ( "scaler.txt", "w+" ) ) == NULL ) printf ( "ERROR" ) ;
      for ( ch=0; ch<16 ; ch++ ) {  
        fprintf ( fp, "%d\t%d\n", ch, gData[ch] ) ;
      }
    fclose ( fp ) ;
   return 0;
}


int daq::append(char *filename){        
        /* zapisi v tekstovno datoteko */                        

      FILE *fp=fopen ( filename, "a" );
       if ( fp!= NULL ){
         for ( int ch=0; ch<16 ; ch++ ) fprintf ( fp, "%d\t", gData[ch] ) ;
         fprintf ( fp, "\n" ) ;
         fclose ( fp ) ;
           printf("Rezultati meritve so dodani v datoteko %s\n",filename);
       } else {
         printf ( "ERROR" ) ;
       }
      
     return 0;
}
    
int daq::init(){
  printf("DAQ init at 0x%x\n",(unsigned int *) udev);
  if (udev == NULL) connect();
  if (udev == NULL) printf("udev == NULL\n");
  CAMAC_status();
  CCCZ;
  CCCC;

  CSET_I;

  int q,x;  
  long dum;  
  printf("Pazi na cas meritve. Kaksen je obseg stevca?\n");
  CSSA_RQX( NTGG, 0, 9, &dum,&q, &x);         /* init TGG */
  CSSA_WQX( NTGG, 0,16, 1000,&q, &x);  /* Set preset counting value   */
  CSSA_WQX( NTGG, 0,17, 0x2 ,&q, &x );  /* Set Load&Clock Modes */
  CSSA_WQX( NDIS, 0,16, 0xffff,&q, &x);/* enable all ch. */
  CSSA_RQX( NDIS, 0,26, &dum,&q, &x);

  CREM_I;
  printf("Initialized\n");

  return 0;
}



daq::daq(){
// intercept routine
  if (signal (SIGINT, SigInt) == SIG_ERR) perror ("sigignore");
   
connect();
init();
 
}

daq::~daq(){
 
disconnect();
}

#ifdef MAIN
int main (int argc, char **argv){
  int threshold=250;
  int time_set =1000; /* in ms */
  char filename[1024];
  sprintf(filename,"output.txt");
  if (argc>1) threshold = atoi(argv[1]);
  if (argc>2) sprintf(filename ,"%s",argv[2]);
  if (argc>3) time_set  = atoi(argv[3]);
  daq *d= new daq();
  d->init();
  d->start(threshold, time_set); // threshold
  usleep(time_set*.1);
  d->count();
  d->append(filename);
  delete d;

return 0;
}
#endif