Subversion Repositories f9daq

Rev

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