Subversion Repositories f9daq

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
60 f9daq 1
#include <ansi_c.h>
2
#include "CAENV673A.h"
3
#include "CAENV673A_DEF.h"
4
//#include "pcivme_dll.h"
5
#ifdef SISVME
6
#include "sisvme_dll.h"
7
#endif
8
#ifdef WIENVME
9
#include "wienvme_dll.h"
10
#endif
11
#ifdef WUSBVME
12
#include "wusbvme_dll.h"
13
#endif
14
 
15
unsigned int ModuleAddress[10];
16
static const unsigned short bufevnum[2][4]=
17
    {CAENV673A_ENR0B0, CAENV673A_ENR0B1, CAENV673A_ENR0B2, CAENV673A_ENR0B3,
18
     CAENV673A_ENR1B0, CAENV673A_ENR1B1, CAENV673A_ENR1B2, CAENV673A_ENR1B3};
19
 
20
int V673A_map (int ModuleNumber,
21
                         unsigned long ModuleOffset, int print)
22
{
23
  unsigned long vsr, mmt, fix;
24
 
25
  ModuleAddress[ModuleNumber] =  ModuleOffset;
26
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_FIX, &fix);
27
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_MMT, &mmt);
28
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_VSR, &vsr);
29
  if (print) {
30
    printf("fixed code = 0x%04x\n", fix);
31
    printf("manufacturer number = %i\n", MANUFACTURER(mmt));
32
    printf("module type = %i\n", MODULE_TYPE(mmt));
33
    printf("version = %i\n", VERSION(vsr));
34
    printf("serial no. = %i\n", SERIAL(vsr));
35
  }
36
  if ((fix != CAEN_FIX_CODE) || (mmt != CAENV673A_MMT_VALUE)) return -1;
37
  return 0;
38
}
39
 
40
int V673A_init (int ModuleNumber)
41
{
42
  unsigned long dum16;
43
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_CLRTDC, &dum16);
44
 
45
  dum16= 0xF77F;
46
  VME_A24D16_W(ModuleAddress[ModuleNumber] + CAENV673A_MCR0,dum16);
47
  dum16= 0xF77F;
48
  VME_A24D16_W(ModuleAddress[ModuleNumber] + CAENV673A_MCR1, dum16);
49
 
50
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_CLALLB, &dum16);
51
  /* range 2**(NBITS+1)*1.0416s
52
     NBITS =
53
             4         32 ns
54
             5         64
55
             6         128
56
             7         256
57
             8         512
58
             9        1024
59
             A        2048
60
             B        4096
61
             C        8200
62
             D       16400
63
             E       32000
64
   */          
65
  dum16=0x003A;// RANGE  2048 ns
66
  //dum16=0x003E; //range 32us  
67
  VME_A24D16_W(ModuleAddress[ModuleNumber] + CAENV673A_CRR0, dum16);
68
  VME_A24D16_W(ModuleAddress[ModuleNumber] + CAENV673A_CRR1, dum16);
69
 
70
  dum16=0x001A;
71
  VME_A24D16_W(ModuleAddress[ModuleNumber] + CAENV673A_CLKCR0, dum16);
72
  VME_A24D16_W(ModuleAddress[ModuleNumber] + CAENV673A_CLKCR1, dum16);
73
 
74
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_BEGDLL, &dum16);
75
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_CLALLB, &dum16);
76
 
77
  dum16=0xF35F; // both edges  + stop mode
78
  VME_A24D16_W(ModuleAddress[ModuleNumber] + CAENV673A_MCR0, dum16);
79
  VME_A24D16_W(ModuleAddress[ModuleNumber] + CAENV673A_MCR1, dum16);
80
 
81
  dum16=0xFFFF;
82
  VME_A24D16_W(ModuleAddress[ModuleNumber] + CAENV673A_MR0L, dum16);
83
  VME_A24D16_W(ModuleAddress[ModuleNumber] + CAENV673A_MR0H, dum16);
84
  VME_A24D16_W(ModuleAddress[ModuleNumber] + CAENV673A_MR1L, dum16);
85
  VME_A24D16_W(ModuleAddress[ModuleNumber] + CAENV673A_MR1H, dum16);
86
 
87
  return 0;
88
}
89
 
90
int V673A_status (int ModuleNumber)
91
{
92
  unsigned long dum16;
93
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_BSR, &dum16);
94
  return dum16;
95
}
96
 
97
int V673A_ntrig (int ModuleNumber)
98
{
99
  unsigned long ntr[2];
100
 
101
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_ENR0, &ntr[0] );
102
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_ENR1, &ntr[1] );
103
 
104
  if (ntr[0] == ntr[1]) return ntr[0];
105
  else printf ("CAENV673A [%d] ENR0=%04x ENR1=%04x\n",ModuleNumber, ntr[0],ntr[1]);
106
  return -1;
107
}
108
 
109
int V673A_read0 (int ModuleNumber, unsigned long whereto[], int size)
110
{
111
  unsigned long status, ndata;
112
  unsigned int data;
113
 
114
  ndata = 0;
115
  if (size<=0) return 0;
116
 
117
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_BSR, &status);
118
  if (!(status & (1<<5))) {
119
    //printf ("CAENV673A  BSR STATUS=%04x\n",status);
120
    return 0;
121
  }
122
  VME_A24D32_R(ModuleAddress[ModuleNumber] + CAENV673A_SEQ0, &data  );
123
  *whereto= data;
124
  if ((*whereto) & (1<<23)) {
125
    ndata++;
126
    do {  
127
    VME_A24D32_R(ModuleAddress[ModuleNumber] + CAENV673A_SEQ0, &data);
128
      *(++whereto) = data;
129
      ndata++;
130
    } while (!((*whereto) & ((1<<30) | (1<<31))) && ndata<size);
131
    if (ndata>=size) {
132
 
133
       printf("V673A_read0 Increase buffer size ndata=%d\ndata=0x%x\n", ndata,data);  
134
       return ndata;
135
    }
136
    if (!((*whereto) & (1<<31))) return ndata;
137
  }
138
 
139
  return 0;  
140
 
141
}
142
 
143
int V673A_read1 (int ModuleNumber, unsigned long whereto[], int size)
144
{
145
  unsigned long  status, ndata;
146
  unsigned int data;
147
 
148
  ndata = 0;
149
  if (size<=0) return 0;
150
 
151
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_BSR, &status);
152
  if (!(status & (1<<7))) return 0;
153
 
154
 
155
  VME_A24D32_R(ModuleAddress[ModuleNumber] + CAENV673A_SEQ1, &data);
156
  *whereto = data;    
157
  if ((*whereto) & (1<<23)) {
158
    ndata++;
159
    do {
160
    VME_A24D32_R(ModuleAddress[ModuleNumber] + CAENV673A_SEQ1, &data);  
161
      *(++whereto) =  data;
162
      ndata++;
163
    } while (!((*whereto) & ((1<<30) | (1<<31))) && ndata<size);
164
    if (ndata>=size) {
165
       printf("V673A_read1 Increase buffer size ndata=%d\ndata=0x%x\n", ndata,data);
166
       return ndata;
167
    }
168
    if (!((*whereto) & (1<<31))) return ndata;
169
  }
170
 
171
  return 0;  
172
 
173
}
174
 
175
int V673A_firstevnt (int ModuleNumber)
176
{
177
  unsigned long status, ptr;
178
  int evn[2];
179
 
180
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_BSR, &status);
181
  if (!(status & 0x8000)) return -1;
182
 
183
  if (status & 0x0020) {
184
    VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_PTR0, &ptr);
185
    ptr &= 0x3;
186
    evn[0]=0;
187
    VME_A24D16_R(ModuleAddress[ModuleNumber] + bufevnum[0][ptr], &evn[0]);
188
   } else {
189
    evn[0]=0x10000;
190
  }
191
  if (status & 0x0080) {
192
    VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_PTR1, &ptr);
193
    ptr &= 0x3;
194
    evn[1]=0;
195
    VME_A24D16_R(ModuleAddress[ModuleNumber] + bufevnum[1][ptr], &evn[1]);
196
   } else {
197
    evn[1]=0x10000;
198
  }
199
  //printf ("first event [%d] evn: %d %d \n",ModuleNumber ,evn[0]&0xffff,evn[1]&0xffff);
200
  if (evn[0] < evn[1]) {
201
    return evn[0];
202
   } else {
203
    return evn[1];
204
  }
205
}
206
 
207
int V673A_getevnt (int ModuleNumber, int evn, unsigned long whereto[], int size)
208
{
209
  unsigned long dum16;
210
  unsigned long status, bptr, bevn, ndata;
211
  int msize=size;
212
  ndata=0;
213
 
214
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_BSR, &status);
215
  if (!(status & 0x8000)) return -1;
216
 
217
  while (1) {
218
    if (status & 0x0020) {
219
      VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_PTR0, &bptr);
220
      bptr &= 0x3;
221
      VME_A24D16_R(ModuleAddress[ModuleNumber] + bufevnum[0][bptr], &bevn);
222
      if (bevn == evn) {
223
        ndata += V673A_read0(ModuleNumber, whereto, msize);
224
        msize-=ndata;
225
        whereto += ndata;
226
        break;
227
       } else if (bevn < evn) {
228
        VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_CLRCB0, &dum16);
229
        VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_BSR, &status);
230
        if (!(status & 0x8000)) return ndata;
231
       } else {
232
        break;
233
      }
234
     } else {
235
      break;
236
    }
237
  }
238
  while (1) {
239
    if (status & 0x0080) {
240
      VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_PTR1, &bptr);
241
      bptr &= 0x3;
242
      VME_A24D16_R(ModuleAddress[ModuleNumber] + bufevnum[1][bptr], &bevn);
243
      if (bevn == evn) {
244
        ndata += V673A_read1(ModuleNumber, whereto, msize);
245
        msize-=ndata;
246
        break;
247
       } else if (bevn < evn) {
248
        VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_CLRCB1, &dum16);
249
        VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_BSR, &status);
250
        if (!(status & 0x8000)) return ndata;
251
       } else {
252
        break;
253
      }
254
     } else {
255
      break;
256
    }
257
  }
258
  return ndata;  
259
}
260
 
261
 
262
 
263
int V673A_clallb (int ModuleNumber){
264
  unsigned long dum16;  
265
  VME_A24D16_R(ModuleAddress[ModuleNumber] + CAENV673A_CLALLB, &dum16);
266
  return 0;
267
}
268
 
269
int V673A_getreg (int ModuleNumber, int reg){
270
  unsigned long dum16;
271
  VME_A24D16_R(ModuleAddress[ModuleNumber] + reg, &dum16);
272
  return dum16;
273
}
274
 
275
void V673A_setreg (int ModuleNumber, int reg, int set){
276
  unsigned long dum16=set;
277
  VME_A24D16_W(ModuleAddress[ModuleNumber] + reg, dum16);
278
  return;
279
}