Subversion Repositories f9daq

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
111 f9daq 1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <unistd.h>
5
#include <sys/mman.h>
6
#include <sys/ioctl.h>
7
#include <errno.h>
8
#include <signal.h>
9
#include <ctype.h>
10
#include <time.h>
11
#include <sys/time.h>
12
 
13
#include "wusbxx_dll.h" /* the header of the shared library */
14
#include "daq.h"
15
 
16
#define DEVICE_NAME "/dev/cc32_1"
17
#define PRRES(X) printf(">>> %s -> %d\n",#X,(X))
18
 
19
 
20
 
21
/* definiram lokacije enot*/
22
//#define NTDC 23         /* TDC LeCroy 2277*/
23
#define NTGG 23          /* CAEN C423 */
24
#define NSCA 22          /* CAEN C257 */
25
#define NDIS 21          /* Philips 7106 */
26
 
27
 
28
 
29
#define END_MARKER 0xFAF5
30
int ctrlc=0;
31
int timer_out=0;
32
 
33
void SigInt (int sig)
34
{
35
    ctrlc = 1;
36
    timer_out=1;
37
 
38
}
39
 
40
struct sigaction oact;
41
void timerast (int signumber)
42
{
43
    timer_out = 1;
44
    printf("->>> TIMEOUT !!!\n");
45
}
46
 
47
void tmlnk (int tout)
48
{
49
    timer_out = 0;
50
    struct sigaction act;
51
    struct itimerval tdelay;
52
 
53
    act.sa_handler = timerast;
54
    sigemptyset (&act.sa_mask);
55
    act.sa_flags = 0;
56
 
57
    tdelay.it_value.tv_sec = tout / 100;
58
    tdelay.it_value.tv_usec = 10000 * (tout % 100);
59
    tdelay.it_interval.tv_sec = 0;
60
    tdelay.it_interval.tv_usec = 0;
61
 
62
    if (sigaction (SIGALRM, &act, &oact) < 0)
63
{
64
        perror ("sigaction(tmlnk)");
65
        exit (EXIT_FAILURE);
66
    }
67
    if (setitimer (ITIMER_REAL, &tdelay, NULL) < 0)
68
    {
69
        perror ("setitimer(tmlnk)");
70
        exit (EXIT_FAILURE);
71
    }
72
}
73
 
74
void tmulk ()
75
{
76
    struct itimerval tdelay;
77
 
78
    tdelay.it_value.tv_sec = 0;
79
    tdelay.it_value.tv_usec = 0;
80
    tdelay.it_interval.tv_sec = 0;
81
    tdelay.it_interval.tv_usec = 0;
82
 
83
    if (setitimer (ITIMER_REAL, &tdelay, NULL) < 0)
84
    {
85
        perror ("setitimer(tmulk)");
86
        exit (EXIT_FAILURE);
87
    }
88
    if (sigaction (SIGALRM, &oact, NULL) < 0)
89
    {
90
        perror ("sigaction(tmulk)");
91
        exit (EXIT_FAILURE);
92
    }
93
}
94
 
95
 
96
 
97
 
98
void CAMAC_status(void)
99
{
100
  unsigned short id,fpga,modid,inhibit,lam;
101
  unsigned short dum;
102
 
103
 
104
  long cres;
105
  int q,x;  
106
 
107
  CSSA_RQX(0,0,0,&cres,&q,&x);
108
  dum = cres;
109
  id   = (dum & 0xF000) >> 12;
110
  fpga = (dum & 0x0F00) >> 8;  
111
  modid =  (dum & 0x00F0) >> 4;
112
  q      =  (dum & 0x0008) >> 3;
113
  x      =  (dum & 0x0004) >> 2;
114
  inhibit=  (dum & 0x0002) >> 1;
115
  lam    =  (dum & 0x0001);    
116
  printf ("CCUSB CSR ID=%x FPGA=%x modID=%x Q=%d X=%d INHIBIT=%d LAM=%d \n",id,fpga,modid, q,x,inhibit,lam);
117
}
118
 
119
int daq::connect(){
120
/*xxusb_device_type devices[100];
121
struct usb_device *dev;
122
dev = devices[0].usbdev;
123
udev = xxusb_device_open(dev);*/
124
    WUSBXX_load (NULL);
125
//    WUSBXX_open ((char *)"CC0126");
126
    WUSBXX_open ((char *)"CC0130");
127
    printf("daq::connect()\n");
128
    return 0;
129
}
130
 
131
int daq::disconnect(){
132
  /* zakljuci */
133
 
134
   printf("daq::disconnect()\n");
135
  return 0;
136
}
137
 
138
int daq::start(int thr_set,int time_set){
139
      int q,x;
140
      // printf("Nastavljam  threshold na %d in cas meritve na %d ms\n",thr_set,time_set);
141
      long dum;
142
      /* postavimo zeljeni prag prozenja */
143
      CSSA_WQX(NDIS,0,17,thr_set,&q,&x);
144
      CSSA_WQX(NDIS,1,17,0,&q,&x);
145
 
146
      /* stevec postavim na nic */
147
      CSSA_RQX ( NSCA, 0, 9,&dum, &q,&x);
148
 
149
      /* nastavim dolzino signala */
150
      CSSA_WQX(NTGG,0,16,time_set,&q,&x);
151
      CSSA_RQX(NTGG,0,15,&dum,&q,&x);
152
 
153
      usleep(1000*time_set);
154
      return 0;
155
}
156
 
157
 
158
int daq::count(){
159
    int q,x;    
160
    /* prestejem sunke na posameznem kanalu */    
161
      for ( int ch=0 ; ch<16 ; ch ++ ) {
162
        long data=0;
163
        CSSA_RQX( NSCA, ch, 0, &data, &q,&x);
164
        CSSA_RQX( NSCA, ch, 0, &data, &q,&x);
165
                // CC USB ne precita v redu CAEN C257 scalerja, 
166
                // kanali so zamaknjeni za 1, ce je klic samo en
167
                // vrednost kanala 0 se pristeje h kanalom 8..16, zato ga ne uporabljamo. 
168
                // vajo izvedemo le s kanali 1..15.
169
        gData[ch]=data;
170
//        printf("Kanal:%d Stevilo sunkov: %d \n",ch,data );
171
      }
172
      for ( int ch=0 ; ch<16 ; ch ++ ) if (gData[ch]>0) gData[ch]--;
173
 
174
return 0;
175
}    
176
 
177
 
178
int daq::save(){
179
    /* shrani */
180
    FILE *fp;
181
     int ch;
182
     if ( ( fp = fopen ( "scaler.txt", "w+" ) ) == NULL ) printf ( "ERROR" ) ;
183
      for ( ch=0; ch<16 ; ch++ ) {  
184
        fprintf ( fp, "%d\t%d\n", ch, gData[ch] ) ;
185
      }
186
    fclose ( fp ) ;
187
   return 0;
188
}
189
 
190
 
191
int daq::append(char *filename){       
192
        /* zapisi v tekstovno datoteko */                        
193
 
194
      FILE *fp=fopen ( filename, "a" );
195
       if ( fp!= NULL ){
196
         for ( int ch=0; ch<16 ; ch++ ) fprintf ( fp, "%d\t", gData[ch] ) ;
197
         fprintf ( fp, "\n" ) ;
198
         fclose ( fp ) ;
199
           printf("Rezultati meritve so dodani v datoteko %s\n",filename);
200
       } else {
201
         printf ( "ERROR" ) ;
202
       }
203
 
204
     return 0;
205
}
206
 
207
int daq::init(){
208
  printf("DAQ init at 0x%x\n",(unsigned int *) udev);
209
  if (udev == NULL) connect();
210
  if (udev == NULL) printf("udev == NULL\n");
211
  CAMAC_status();
212
  CCCZ;
213
  CCCC;
214
 
215
  CSET_I;
216
 
217
  int q,x;  
218
  long dum;  
219
  printf("Pazi na cas meritve. Kaksen je obseg stevca?\n");
220
  CSSA_RQX( NTGG, 0, 9, &dum,&q, &x);         /* init TGG */
221
  CSSA_WQX( NTGG, 0,16, 1000,&q, &x);  /* Set preset counting value   */
222
  CSSA_WQX( NTGG, 0,17, 0x2 ,&q, &x );  /* Set Load&Clock Modes */
223
  CSSA_WQX( NDIS, 0,16, 0xffff,&q, &x);/* enable all ch. */
224
  CSSA_RQX( NDIS, 0,26, &dum,&q, &x);
225
 
226
  CREM_I;
227
 
228
  return 0;
229
}
230
 
231
 
232
 
233
daq::daq(){
234
// intercept routine
235
  if (signal (SIGINT, SigInt) == SIG_ERR) perror ("sigignore");
236
 
237
connect();
238
init();
239
 
240
}
241
 
242
daq::~daq(){
243
 
244
disconnect();
245
}
246
 
247
#ifdef MAIN
248
int main (int argc, char **argv){
249
  int threshold=250;
250
  int time_set =1000; /* in ms */
251
  char filename[1024];
252
  sprintf(filename,"output.txt");
253
  if (argc>1) threshold = atoi(argv[1]);
254
  if (argc>2) sprintf(filename ,"%s",argv[2]);
255
  if (argc>3) time_set  = atoi(argv[3]);
256
  daq *d= new daq();
257
  d->init();
258
  d->start(threshold, time_set); // threshold
259
 
260
  d->count();
261
  d->append(filename);
262
  delete d;
263
 
264
return 0;
265
}
266
#endif