Subversion Repositories f9daq

Rev

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

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/mman.h>
  6. #include <errno.h>
  7. #include <signal.h>
  8. #include <ctype.h>
  9. #include <time.h>
  10. //#include "../include/wusbxx_dll.h" /* the header of the shared library */
  11. #include "../include/daq.h"
  12.  
  13. //#define DEBUG /* vkljuci dodatni izpis */
  14. #ifdef DEBUG
  15.   #define DBG(X) X
  16.   #define DBGFUNI(X) printf(">>> %s -> %d\n",#X,(X))
  17. #else
  18.   #define DBG(X)
  19.   #define DBGFUNI(X) X
  20. #endif
  21.  
  22. /* definiram lokacije enot*/
  23. //#define NTDC 1         /* TDC */
  24. //#define NTDCCH 8
  25. //#define NADC 2         /* ADC */
  26. //#define NADCCH 8
  27. int ctrlc=0;
  28. char *ccserial=(char*)"CC0126";
  29. int devDetect;  // variable to tell if we detect any devices
  30.  
  31. int daq::connect(){
  32. // odpri daq
  33. /*  xxusb_device_type devices[100];
  34.   //struct usb_device *dev;
  35.   devDetect = xxusb_devices_find(devices);
  36. //  printf("Detected devices: %d\n", devDetect);
  37.   //dev = devices[0].usbdev;
  38.   //udev = xxusb_device_open(dev);
  39.  
  40.   if(devDetect > 0)
  41.   {
  42.     WUSBXX_load(NULL);
  43.     WUSBXX_open(ccserial);
  44.     printf("daq::connect()\n");
  45.   }
  46.   else
  47. */    printf("daq::connect() - No devices were detected!\n");
  48.   return 0;
  49. }
  50.  
  51. int daq::init(int chan = 0){
  52.  
  53. //    int i;  
  54. //    long k;
  55.  
  56. /*    DBGFUNI(xxusb_register_write(udev,1,0x0)); // Stop DAQ mode
  57.     while (xxusb_usbfifo_read(udev, (int*) stackdump,BUFF_L,100)>0);
  58.  
  59.     CCCZ;
  60.     CCCC;
  61.     CREM_I;
  62.  
  63.     NTDC = 1;
  64.     NADC = 2;
  65.     if(chan != 0)
  66.     {
  67.       NTDCCH = chan/2;
  68.       NADCCH = chan/2;
  69.     }
  70.     else
  71.     {
  72.       NTDCCH = 1;
  73.       NADCCH = 1;
  74.     }
  75.     printf("after: NTDCCH = %d, NADCCH = %d\n", NTDCCH, NADCCH);
  76.  
  77. // create command stack for the TDC and ADC
  78.     k=1;
  79.     for(i=0;(i<NTDCCH)&&(i<NADCCH);i++) { stackwrite[k++]=NAF(NTDC,i,0); stackwrite[k++]=NAF(NADC,i,0); }
  80. //    for(i=0;i<NADCCH;i++) stackwrite[k++]=NAF(NADC,i,0);
  81.     stackwrite[k++]=NAF(NTDC,0,9);
  82.     stackwrite[k++]=NAF(NADC,0,9);
  83.     stackwrite[k++]=NAFS(0,0,16); // insert next word to data
  84.     stackwrite[k++]=0xfafb; // event termination word
  85.     stackwrite[0]=k-1;
  86. // upload stack #2
  87.     xxusb_stack_write(udev,0x2,(long int *)stackwrite);
  88.     xxusb_stack_read(udev,0x2,(long int *) stackdata);
  89.     DBG(for(i=0;i<k;i++) printf("0x%04x\n",stackdata[i]);)
  90.  
  91.     int ret[10];
  92.    
  93.     CAMAC_LED_settings(udev, 1,1,0,0);
  94.     ret[0] = CAMAC_register_read(udev,0,&k);
  95.     printf("Firmware ID (return %d) -> 0x%08lX\n",ret[0],k);            // GKM: Firmware ID (i.e. 0x72000001 = 0111 0010 0000 0000 0000 0000 0000 0001)
  96.     ret[1] = CAMAC_register_read(udev,1,&k);
  97.     printf("Global Mode (return %d) -> 0x%08lX\n",ret[1],k);
  98.     k=(k&0xF000)|0x0005; // set buffer length: n=0..6 -> 0x10000 >> n, n=7 -> single event
  99.     ret[0] = CAMAC_register_write(udev,1,k);                            // GKM: sets the buffer length (i.e. k=5 -> buf length=128 words)
  100.     ret[1] = CAMAC_register_write(udev,2,0x80); // wait 0x80 us after trigger           // GKM: delay settings in microseconds
  101.     ret[2] = CAMAC_register_write(udev,3,0x0); // Scaler Readout Control Register               // GKM: scaler readout settings - sets the frequency of readout (if 0, it is disabled)
  102.     ret[3] = CAMAC_register_write(udev,9,0x0); // Lam Mask Register             // GKM: When 0, readout is triggered by the signal on NIM input
  103.     ret[4] = CAMAC_register_write(udev,14,0x0); // USB Bulk Transfer Setup Register
  104.  
  105.     //    CAMAC_DGG(udev,1,2,3,0,200,0,0);
  106.     //    CAMAC_DGG(udev,0,0,0,0,100,0,0);
  107.     ret[5] = CAMAC_register_write(udev,5,(0x06<<16)+(0x04<<8)+0x00); // output          // GKM: NIM outputs (i.e. 0x060400 = 00 0110 0000 0100 0000 0000 -> NIM O2=DGG_B, NIM O3=DGG_A)
  108.     ret[6] = CAMAC_register_write(udev,6,(0x01<<24)+(0x01<<16)+(0x0<<8)+0x0); // SCLR & DGG     // GKM: device source selector (i.e. 0x01010000 = 00 0000 0001 0000 0001 0000 0000 0000 0000 -> DGG_A=NIM I1, DGG_B=NIM I1, SCLR=disabled)
  109.     ret[7] = CAMAC_register_write(udev,7,(100<<16)+0); // output                // GKM: Delay and Gate Generator A registers (i.e. 0x00640000 = 0000 0000 0110 0100 0000 0000 0000 0000 -> DDG_A [gate=100, delay=0])
  110.     ret[8] = CAMAC_register_write(udev,8,(10000<<16)+0); // output      // GKM: Delay and Gate Generator B registers (i.e. 0x27100000 = 0010 0111 0001 0000 0000 0000 0000 0000 -> DDG_B [gate=10000, delay=0])
  111.     ret[9] = CAMAC_register_write(udev,13,0); // output         // GKM: Extended (course) delay (i.e. 0x00000000 = 0 -> DDG_A ext=0, DDG_B ext=0)
  112.  
  113. //    for(i = 0; i < 10; i++) printf("Setting %d? -> return = %d\n",i,ret[i]);
  114.  
  115. //    ret[0] = CAMAC_register_read(udev,1,&k);
  116. //    printf("k (return %d) -> 0x%08lX\n",ret[0],k);
  117.    
  118. */    printf("daq::init()\n");
  119.     return 0;
  120. }
  121.  
  122. int daq::start(){
  123. //    xxusb_register_write(udev,1,0x1); // Start DAQ mode
  124.     printf("daq::start()\n");
  125.     return 0;
  126. }
  127.  
  128. int daq::stop(){
  129. //    xxusb_register_write(udev,1,0x0); // Stop DAQ mode
  130. //    while (xxusb_usbfifo_read(udev,(int *)stackdump,BUFF_L,30)>0);
  131.     printf("daq::stop()\n");
  132.     return 0;
  133. }
  134.  
  135. int daq::event(unsigned int *data, int maxn){
  136. //    int ib,count;
  137.     int count;
  138. /*    int events,evsize;
  139.     short ret;
  140.  
  141.     ib=0;
  142.     ret=xxusb_usbfifo_read(udev,(int *) stackdata,BUFF_L,500);
  143.     events=stackdata[ib++];
  144.     DBG(printf("ret=%d,events=0x%08x\n",ret,events);)
  145.       if ((ret<0)||(ret!=(((NTDCCH+NADCCH)*4+4)*events+4))) return 0;
  146.  
  147.     count=0;
  148.     while (ib<(ret/2-1)){
  149.       evsize = stackdata[ib++]&0xffff;
  150.       DBG(printf("Event:%d EvSize:%d\n", events, evsize);)
  151.       for (int i=0;i<(NTDCCH+NADCCH);i++,ib++)  data[count++] =stackdata[ib++]&0xffff;
  152.       if (stackdata[ib++]!=0xfafb){
  153.         printf("Error!\n");,
  154.         return 0;
  155.       }
  156.       events--;
  157.       if (fStop) return 0;
  158.     }
  159.     if (stackdata[ib++]!=0xffff){
  160.       printf("Error!\n");
  161.       return 0;
  162.     }
  163. */
  164.     count = 1;
  165.     return count;
  166. }
  167.  
  168. int daq::disconnect(){
  169. // zapri daq
  170. //    WUSBXX_close();
  171.     printf("daq::disconnect()\n");
  172.     return 0;
  173. }
  174.  
  175. daq::daq(){
  176.   fStop=0;
  177.   connect();
  178. //  if(devDetect > 0)
  179. //    init();
  180. }
  181.  
  182. daq::~daq(){
  183. disconnect();
  184. }
  185.  
  186. #ifdef MAIN
  187. void CatchSig (int signumber)
  188. {
  189.    ctrlc = 1;
  190. }
  191.  
  192.  
  193. int main (int argc, char **argv){
  194.  
  195.   int neve=1000000;
  196.   char *fname="test.dat";
  197.   if (argc==1) {
  198.     printf("Uporaba: %s stevilo_dogodkov ime_datoteke\n",argv[0]);
  199.     printf("Meritev prekini s Ctrl-C, ce je nabranih dogodkov ze dovolj\n");
  200.     exit(0);
  201.   }
  202.   if (argc>1) neve  = atoi(argv[1]);
  203.   if (argc>2) fname = argv[2];
  204.  
  205.  
  206. // intercept routine
  207.   if (signal (SIGINT, CatchSig) == SIG_E,RR) perror ("sigignore");
  208.    
  209. #define BSIZE 10000
  210.   int i,ieve,nc,nb;
  211. //  int hdr[4]={1,(NTDCCH+4)*sizeof(int)};      // hdr[0]=1, hdr[1]=(NTDCCH+4)*4
  212.   int hdr[4]={1,(NTDCCH+NADCCH+4)*sizeof(int)};
  213.   unsigned short adc;
  214.   unsigned int data[BSIZE];
  215.   daq *d= new daq();,
  216.   time_t time_check;,
  217.  
  218. // odpremo datoteko za pisanje
  219.   FILE *fp=fopen(fname,"w");
  220.  
  221.   d->start();
  222.   ieve=0;
  223.  
  224.   while((ieve<neve)&&(!ctrlc)){
  225.     nc=d->event(data,BSIZE);
  226.     nb=0;
  227.     while((nc>0)&&(ieve++<neve)&&(!ctrlc)){
  228.       // zapis v datoteko  
  229.       hdr[2]=time(NULL);
  230.       hdr[3]=ieve;
  231.       fwrite(hdr,sizeof(int),4 ,fp);
  232.       fwrite(&data[nb],sizeof(int),(NTDCCH+NADCCH),fp);
  233. //      DBG(
  234.         for(i=0;i<(NTDCCH+NADCCH);i++){
  235.           adc=data[nb+i]&0xFFFF;       
  236.           if(i % 2 == 0)
  237. printf(/*"nev=%4d %d. TDC data=%d\n"*/"%d\t"/*,ieve,i*//*/2*/,adc);
  238. //            printf("nev=%4d %d. TDC data=%d\n",ieve,i/2,adc);
  239.           else if(i % 2 == 1)
  240. printf(/*"nev=%4d %d. TDC data=%d\n"*/"%d\t"/*,ieve,i*//*/2*/,adc);
  241. //            printf("nev=%4d %d. ADC data=%d\n",ieve,i/2,adc);
  242.         }
  243. printf("\n");
  244. //      )
  245.       nb+=(NTDCCH+NADCCH);
  246.       nc-=(NTDCCH+NADCCH);
  247.       if (!(ieve%1000)) printf("event no. -> %d\n",ieve);
  248.     };
  249.   };
  250.    
  251.   d->stop();
  252.   fclose(fp);
  253.   printf("Podatki so v datoteki %s\n", fname);
  254.   delete d;
  255.  
  256.   return 0;
  257. }
  258. #endif
  259.