Subversion Repositories f9daq

Rev

Rev 109 | 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 DEVICE_NAME "/dev/cc32_1"
#define PRRES(X) printf(">>> %s -> %d\n",#X,(X))



/* definiram lokacije enot*/
//#define NTDC 23         /* TDC LeCroy 2277*/
#define NTDC 16         /* TDC LeCroy 2277*/
#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;  
 
  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 ("CC32 CSR ID=%x FPGA=%x modID=%x Q=%d X=%d INHIBIT=%d LAM=%d \n",id,fpga,modid, q,x,inhibit,lam);
}

int daq::init(){

  printf("DAQ init at 0x%x\n",(unsigned int *) udev);
  if (udev == NULL) connect();
  if (udev == NULL) printf("udev == NULL\n");
  xxusb_register_write(udev,1,0x0); // Stop DAQ mode
  while (xxusb_usbfifo_read(udev, (int*) stackdump,BUFF_L,100)>0); 
  
  printf("DAQ init finished\n");
  CCCZ;
  CCCC;

  CREM_I;
  long cres;
  int q,x;  
 
  // set status registerdevices_find
  //int tdcreg2277=0x8C00; // status register common start (green LED),Front panel timeout ,leading+trailing
  //  tdcreg2277= 0xC00;   //  status register common stop (red LED)                        ,leading + trailing edge recording
  int  tdcreg2277= 0xCC00; //  status register common start (green LED),8192 ns timeout     ,leading+trailing
  
 CSSA_WQX(NTDC,0,17,tdcreg2277,&q,&x);
 CSSA_RQX(NTDC,0,17,&cres,&q,&x);
  
  printf("LC2277 SetRegister 0x%04x GetRegister 0x%04x\n",tdcreg2277,(unsigned int)cres);
  
  CAMAC_status();

  // listmode data -----------------------------------------------

    long k=1;
    

  /*
    stackwrite[k++]=(NAF(NTDC,0,8))| 0x8000 ; // test lam
    stackwrite[k++]=0x80 ; // wait for lam 


    stackwrite[k++]=(NAF(NTDC,1,27))| 0x8000;  // test buffering
    stackwrite[k++]=0x10 | 0x8000 ; //until Q=0
    stackwrite[k++]=0xF ; //max repetitions

    stackwrite[k++]=NAFS(0,0,16);
    stackwrite[k++]=0xbeef;

    stackwrite[k++]=(NAF(NTDC,9,27))| 0x8000;  // test buffering
    stackwrite[k++]=0x10 | 0x8000 ; //until Q=0
    stackwrite[k++]=0xFF ; //max repetitions

    stackwrite[k++]=NAFS(0,0,16);
    stackwrite[k++]=0xbeef;
*/


    stackwrite[k++]=(NAF(NTDC,0,0))| 0x8000;  // read TDC
    stackwrite[k++]=0x10 | 0x8000 ; //until Q=0
    stackwrite[k++]=0xF ; //max repetitions
   
    stackwrite[k++]=NAF(NTDC,0,9) ; // clear tdc

    stackwrite[k++]=NAF(NTDC,0,10) ; // clear lam

    
//    stackwrite[k++]=NAF(NTDC,0,1);  // read status register
    stackwrite[k++]=NAFS(0,0,16);
    stackwrite[k++]=END_MARKER;
    stackwrite[0]=k-1;

    for(unsigned int i=0;i<stackwrite[0];i++) printf("0x%04lx\n",stackwrite[i+1]);
    
    CAMAC_LED_settings(udev, 1,1,0,0);
    xxusb_stack_write(udev,0x2,(long int *)stackwrite);
    PRRES(xxusb_stack_read(udev,0x2,(long int *) stackdata));
    for(int i=0;i<13;i++) printf("0x%04lx\n",stackdata[i]);

    PRRES(CAMAC_register_read(udev,0,&k));
    char tbline[300];
    sprintf(tbline,"Firmware ID -> 0x%08lX",k);
    printf("%s\n",tbline);
   
    CAMAC_register_read(udev,1,&k);
    sprintf(tbline,"Global Mode -> 0x%08lX",k);
    printf("%s\n",tbline);
    k=(k&0xF000)|0x0006;
    //                    k=0x0002;
    k=7;// Start DAQ mode Buffopt=7 single event
    CAMAC_register_write(udev,1,k);
    CAMAC_register_write(udev,2,0x80); // wait 0x80 us
    CAMAC_register_write(udev,3,0x0);
    CAMAC_register_write(udev,9,0x0);
    CAMAC_register_write(udev,14,0x0);

    return 0;
}

int daq::connect(){
/*xxusb_device_type devices[100];
struct usb_device *dev;
dev = devices[0].usbdev;
udev = xxusb_device_open(dev);*/
    WUSBXX_load (NULL);
//    WUSBXX_open ((char *)"CC0126");
    WUSBXX_open ((char *)"CC0130");
    printf("daq::connect()\n");
    return 0;
}
  
int daq::disconnect(){ 
  /* zakljuci */
  
   printf("daq::disconnect()\n");
  return 0;
}


int daq::event(unsigned int *data, int maxn){
  
  //int events=0;  
 
  short ret;

  
 
  //while ((events==0)&&(!fStop)){ 
  int tout=100; /* 1/100 of a second */
  tmlnk (tout);
   
  ret=xxusb_usbfifo_read(udev,(int *) data,BUFF_L,500);
  tmulk(); 
   

  // printf("ret=%d,events=0x%08x\n",ret,data[0]);
  //}
  
  
  return ret*2;
}



daq::daq(){
// intercept routine
  if (signal (SIGINT, SigInt) == SIG_ERR) perror ("sigignore");
   
fStop=0;
connect();
init();
 xxusb_register_write(udev,1,0x1); // Start DAQ mode
}

daq::~daq(){
 xxusb_register_write(udev,1,0x0); // Stop DAQ mode
  while (xxusb_usbfifo_read(udev,(int *)stackdump,BUFF_L,30)>0);

disconnect();
}

#ifdef MAIN



int main (int argc, char **argv){
  
  int debug=0;
  int neve=1000000;
  char fname[0xFF];
  sprintf(fname, "test.dat");
  if (argc==1) {
    printf("Uporaba: %s stevilo_dogodkov ime_datoteke\n",argv[0]);
    printf("Meritev prekini s Ctrl-C, ce je nabranih dogodkov ze dovolj\n");
    exit(0);
  } 
  if (argc>1) neve  = atoi(argv[1]);
  if (argc>2) sprintf(fname,"%s", argv[2]);
  if (argc>3) debug = atoi(argv[3]);
  
  

  // odpremo datoteko za pisanje
  FILE *fp=fopen(fname,"w");
  
  int hdr[4]={1};
  
  #define BSIZE 10000
  unsigned int data[BSIZE];
  daq *d= new daq();
  
  for (int ieve=0;ieve!=neve && !ctrlc ;ieve++){
    //int tout=100; /* 1/100 of a second */
    //tmlnk (tout);
    int nb=d->event(data,BSIZE);
    //tmulk();
    // zapis v datoteko   
    if (nb<0){
      ieve--; 
      continue;
    }
    hdr[1]=nb+4*sizeof(int);
    hdr[2]=time(NULL);
    hdr[3]=ieve;
   
    fwrite(hdr,   sizeof(int),4 , fp); 
    fwrite(data,   sizeof(int),nb, fp);
//*****************
    int evsize=0;
    int events=0;  
    int ib=1,count=0;
    events = data[0];
    evsize = data[ib++]&0xffff;
    if (ieve %100 == 0) printf("nb=%d Event:%d events=%d EvSize:%d\n",nb, ieve,  events, evsize);
    if (evsize<2) {
      ieve--;
      continue;
    }
    for (int i=0;i<evsize;i++)  {
       //if (debug) printf("%d\t%08x\n", ib, data[ib]);
       if (data[ib]== END_MARKER) break; 
       if (ib%2==0) {
          unsigned short word1 =data[ib  ]&0xFFFF;
          unsigned short word2 =data[ib+1]&0xFFFF;
          unsigned short tdc =  word1;                 
          unsigned short ch  =  (word2 >> 1 ) &0x1F;
          unsigned short edge =  word2   & 0x1;
          unsigned short q    = (word2 >> 8)  &0x1;
          unsigned short x    = (word2 >> 9)  &0x1;
          if (debug) printf("%d. [ch=%2d] edge=%d data=%d q=%d x=%d\n",count,ch,edge,tdc, q, x);
          count++;
       }
       ib++;
    }
    if (data[evsize+1]!=END_MARKER) printf("Error! END_MARKER not found\n");
//*****************   

 
  }
  fclose(fp);
  printf("Podatki so v datoteki %s\n", fname);

  
  delete d;

  return 0;
}
#endif