Subversion Repositories f9daq

Rev

Rev 111 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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