Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
317 | f9daq | 1 | #include <windows.h> |
2 | #include <userint.h> |
||
3 | #include <utility.h> |
||
4 | //#include <userint.h> |
||
5 | #include <stdlib.h> |
||
6 | //#include <formatio.h> |
||
7 | #include <ansi_c.h> |
||
8 | #include "usb.h" |
||
9 | #include "xxusbdll.h" |
||
10 | #include "scaler.h" |
||
11 | #include <cvirte.h> /* Needed if linking in external compiler; harmless otherwise */ |
||
12 | |||
13 | extern int p4; |
||
14 | extern int daq_on; |
||
15 | |||
16 | |||
17 | #define NDIS 21 |
||
18 | #define NSCA 22 |
||
19 | #define NTGG 23 |
||
20 | |||
21 | |||
22 | |||
23 | #define C_I CAMAC_I |
||
24 | #define C_Z CAMAC_Z |
||
25 | #define C_C CAMAC_C |
||
26 | //#define C_write WUSBXX_CAMAC_write |
||
27 | //#define C_read WUSBXX_CAMAC_read |
||
28 | #define C_write CAMAC_write |
||
29 | #define C_read CAMAC_read |
||
30 | |||
31 | //******************************************************************** |
||
32 | usb_dev_handle *udev; |
||
33 | int Q; |
||
34 | int X; |
||
35 | static HINSTANCE DLLHandle; |
||
36 | |||
37 | int WUSBXX_load (char *module_path) { |
||
38 | |||
39 | |||
40 | if (module_path == NULL) |
||
41 | DLLHandle = LoadLibrary("libxxusb.dll"); |
||
42 | else |
||
43 | DLLHandle = LoadLibrary(module_path); |
||
44 | |||
45 | if (!(xxusb_register_read_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_register_read"))) return __LINE__; |
||
46 | if (!(xxusb_stack_read_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_stack_read"))) return __LINE__; |
||
47 | if (!(xxusb_stack_write_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_stack_write"))) return __LINE__; |
||
48 | if (!(xxusb_stack_execute_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_stack_execute"))) return __LINE__; |
||
49 | if (!(xxusb_register_write_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_register_write"))) return __LINE__; |
||
50 | if (!(xxusb_usbfifo_read_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_usbfifo_read"))) return __LINE__; |
||
51 | if (!(xxusb_bulk_read_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_bulk_read"))) return __LINE__; |
||
52 | if (!(xxusb_bulk_write_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_bulk_write"))) return __LINE__; |
||
53 | if (!(xxusb_reset_toggle_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_reset_toggle"))) return __LINE__; |
||
54 | |||
55 | if (!(xxusb_devices_find_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_devices_find"))) return __LINE__; |
||
56 | if (!(xxusb_device_close_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_device_close"))) return __LINE__; |
||
57 | if (!(xxusb_device_open_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_device_open"))) return __LINE__; |
||
58 | if (!(xxusb_flash_program_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_flash_program"))) return __LINE__; |
||
59 | if (!(xxusb_flashblock_program_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_flashblock_program"))) return __LINE__; |
||
60 | if (!(xxusb_serial_open_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_serial_open"))) return __LINE__; |
||
61 | |||
62 | if (!(VME_register_write_Ptr = (void *) GetProcAddress(DLLHandle,"VME_register_write"))) return __LINE__; |
||
63 | if (!(VME_register_read_Ptr = (void *) GetProcAddress(DLLHandle,"VME_register_read"))) return __LINE__; |
||
64 | if (!(VME_LED_settings_Ptr = (void *) GetProcAddress(DLLHandle,"VME_LED_settings"))) return __LINE__; |
||
65 | if (!(VME_DGG_Ptr = (void *) GetProcAddress(DLLHandle,"VME_DGG"))) return __LINE__; |
||
66 | if (!(VME_Output_settings_Ptr = (void *) GetProcAddress(DLLHandle,"VME_Output_settings"))) return __LINE__; |
||
67 | if (!(VME_read_16_Ptr = (void *) GetProcAddress(DLLHandle,"VME_read_16"))) return __LINE__; |
||
68 | if (!(VME_read_32_Ptr = (void *) GetProcAddress(DLLHandle,"VME_read_32"))) return __LINE__; |
||
69 | if (!(VME_BLT_read_32_Ptr = (void *) GetProcAddress(DLLHandle,"VME_BLT_read_32"))) return __LINE__; |
||
70 | if (!(VME_write_16_Ptr = (void *) GetProcAddress(DLLHandle,"VME_write_16"))) return __LINE__; |
||
71 | if (!(VME_write_32_Ptr = (void *) GetProcAddress(DLLHandle,"VME_write_32"))) return __LINE__; |
||
72 | |||
73 | if (!(CAMAC_DGG_Ptr = (void *) GetProcAddress(DLLHandle,"CAMAC_DGG"))) return __LINE__; |
||
74 | if (!(CAMAC_register_read_Ptr = (void *) GetProcAddress(DLLHandle,"CAMAC_register_read"))) return __LINE__; |
||
75 | if (!(CAMAC_register_write_Ptr = (void *) GetProcAddress(DLLHandle,"CAMAC_register_write"))) return __LINE__; |
||
76 | if (!(CAMAC_LED_settings_Ptr = (void *) GetProcAddress(DLLHandle,"CAMAC_LED_settings"))) return __LINE__; |
||
77 | if (!(CAMAC_Output_settings_Ptr = (void *) GetProcAddress(DLLHandle,"CAMAC_Output_settings"))) return __LINE__; |
||
78 | if (!(CAMAC_read_LAM_mask_Ptr = (void *) GetProcAddress(DLLHandle,"CAMAC_read_LAM_mask"))) return __LINE__; |
||
79 | if (!(CAMAC_write_LAM_mask_Ptr = (void *) GetProcAddress(DLLHandle,"CAMAC_write_LAM_mask"))) return __LINE__; |
||
80 | if (!(CAMAC_write_Ptr = (void *) GetProcAddress(DLLHandle,"CAMAC_write"))) return __LINE__; |
||
81 | if (!(CAMAC_read_Ptr = (void *) GetProcAddress(DLLHandle,"CAMAC_read"))) return __LINE__; |
||
82 | if (!(CAMAC_Z_Ptr = (void *) GetProcAddress(DLLHandle,"CAMAC_Z"))) return __LINE__; |
||
83 | if (!(CAMAC_C_Ptr = (void *) GetProcAddress(DLLHandle,"CAMAC_C"))) return __LINE__; |
||
84 | if (!(CAMAC_I_Ptr = (void *) GetProcAddress(DLLHandle,"CAMAC_I"))) return __LINE__; |
||
85 | return 0; |
||
86 | } |
||
87 | |||
88 | |||
89 | void WUSBXX_open (char *serial) { |
||
90 | if (serial != NULL) |
||
91 | udev = xxusb_serial_open(serial); |
||
92 | if (!udev) |
||
93 | MessagePopup ("Error", "Cannot connect to USB. ! Check the connection!"); |
||
94 | } |
||
95 | |||
96 | void WUSBXX_close (void) { |
||
97 | if (udev) xxusb_device_close(udev); |
||
98 | } |
||
99 | |||
100 | int WUSBXX_CAMAC_write( usb_dev_handle *udev, int N, int A, int F, long Data, int *Q, int *X); |
||
101 | int WUSBXX_CAMAC_read( usb_dev_handle *udev,int N, int A, int F, long *Data, int *Q, int *X); |
||
102 | int WUSBXX_CAMAC_Z( usb_dev_handle *udev ); |
||
103 | int WUSBXX_CAMAC_C( usb_dev_handle *udev ); |
||
104 | int WUSBXX_CAMAC_I(usb_dev_handle *udev, int inhibit); |
||
105 | |||
106 | int WUSBXX_CAMAC_write( usb_dev_handle *udev,int N, int A, int F, long Data, int *Q, int *X) { |
||
107 | long intbuf[4]; |
||
108 | int ret=0; |
||
109 | // CAMAC direct write function |
||
110 | intbuf[0]=1; |
||
111 | intbuf[1]=(long)(F+A*32+N*512 + 0x4000); |
||
112 | if ((F > 15) && (F < 24)) // orig controla |
||
113 | //if ((F > 15) && (F < 28)) // orig controla |
||
114 | { |
||
115 | intbuf[0]=3; |
||
116 | intbuf[2]=(Data & 0xffff); |
||
117 | intbuf[3]=((Data >>16) & 255); |
||
118 | ret = xxusb_stack_execute(udev, intbuf); |
||
119 | *Q = (intbuf[0] & 1); |
||
120 | *X = ((intbuf[0] >> 1) & 1); |
||
121 | } else { |
||
122 | printf("Error N%d A%d F%d",N,A,F); |
||
123 | } |
||
124 | return ret; |
||
125 | } |
||
126 | |||
127 | int WUSBXX_CAMAC_read( usb_dev_handle *udev,int N, int A, int F, long *Data, int *Q, int *X) { |
||
128 | long intbuf[4]; |
||
129 | int ret; |
||
130 | // CAMAC direct read function |
||
131 | intbuf[0]=1; |
||
132 | intbuf[1]=(long)(F+A*32+N*512 + 0x4000); |
||
133 | // intbuf[1]=(long)(F+A*32+N*512); |
||
134 | ret = xxusb_stack_execute(udev, intbuf); |
||
135 | // printf ("ret = %d\n",ret); |
||
136 | if (F < 16) { // orig controla |
||
137 | // *Data = intbuf[0]; //16-bit word |
||
138 | *Data = intbuf[0] + (intbuf[1] & 0xFF) * 0x10000; //24-bit word |
||
139 | *Q = ((intbuf[1] >> 8) & 1); |
||
140 | *X = ((intbuf[1] >> 9) & 1); |
||
141 | } |
||
142 | // else { |
||
143 | // printf("Error N%d A%d F%d",N,A,F); |
||
144 | // } |
||
145 | return ret; |
||
146 | } |
||
147 | |||
148 | int WUSBXX_CAMAC_Z(usb_dev_handle *udev) { |
||
149 | long intbuf[4]; |
||
150 | int ret; |
||
151 | // CAMAC Z = N(28) A(8) F(29) |
||
152 | intbuf[0]=1; |
||
153 | intbuf[1]=(long)(29+8*32+28*512 + 0x4000); |
||
154 | ret = xxusb_stack_execute(udev, intbuf); |
||
155 | return ret; |
||
156 | } |
||
157 | |||
158 | int WUSBXX_CAMAC_C(usb_dev_handle *udev) { |
||
159 | long intbuf[4]; |
||
160 | int ret; |
||
161 | // CAMAC C = N(28) A(9) F(29) |
||
162 | intbuf[0]=1; |
||
163 | intbuf[1]=(long)(29+9*32+28*512 + 0x4000); |
||
164 | ret = xxusb_stack_execute( udev, intbuf); |
||
165 | return ret; |
||
166 | } |
||
167 | |||
168 | int WUSBXX_CAMAC_I(usb_dev_handle *udev, int inhibit) { |
||
169 | long intbuf[4]; |
||
170 | int ret; |
||
171 | // Set Inhibit = N(29) A(9) F(24) |
||
172 | // Clear Inhibit = N(29) A(9) F(26) |
||
173 | intbuf[0]=1; |
||
174 | if (inhibit) intbuf[1]=(long)(24+9*32+29*512 + 0x4000); |
||
175 | else intbuf[1]=(long)(26+9*32+29*512 + 0x4000); |
||
176 | ret = xxusb_stack_execute(udev, intbuf); |
||
177 | return ret; |
||
178 | } |
||
179 | |||
180 | int init_CAMAC () { |
||
181 | |||
182 | long dum; |
||
183 | C_Z(udev); |
||
184 | C_C(udev); |
||
185 | C_I(udev,1); |
||
186 | |||
187 | C_read ( udev,NTGG, 0 , 9, &dum, &Q,&X); /* init TGG */ |
||
188 | C_write ( udev,NTGG, 0, 16, 1000, &Q,&X); /* Set preset counting value */ |
||
189 | C_write ( udev,NTGG, 0 ,17, 0x2, &Q,&X); /* Set Load&Clock Modes */ |
||
190 | C_write ( udev,NDIS, 0, 16, 0xffff, &Q,&X); /* enable all ch. */ |
||
191 | C_read ( udev,NDIS, 0, 26, &dum, &Q, &X); |
||
192 | |||
193 | C_I(udev, 0); |
||
194 | |||
195 | return 0 ; |
||
196 | } |
||
197 | |||
198 | |||
199 | |||
200 | int scaler(int min, int max, int dx, int time_set, char *filename){ |
||
201 | FILE *fp ; |
||
202 | if (filename!=NULL) { |
||
203 | fp = fopen ( filename, "w" ); |
||
204 | } else { |
||
205 | fp =stdout; |
||
206 | } |
||
207 | |||
208 | long val, dum; |
||
209 | int thr_set; |
||
210 | char str[0xFF]; |
||
211 | int n=(max-min)/dx; |
||
212 | double *x= (double *) malloc(n*sizeof(double)); |
||
213 | double *y= (double *) malloc(n*sizeof(double)); |
||
214 | |||
215 | |||
216 | |||
217 | dum=WUSBXX_load (NULL); |
||
218 | if (dum) printf("Error", "Cannot load dll!"); |
||
219 | WUSBXX_open ("CC0130"); |
||
220 | |||
221 | init_CAMAC ( ); |
||
222 | |||
223 | time_t mtime; |
||
224 | time(&mtime); |
||
225 | fprintf(fp, "#%s#gate(ms)=%d\n",ctime(&mtime),time_set); |
||
226 | for (int i=0; i<n; i++) { |
||
227 | int thr=min+i*dx; |
||
228 | C_write ( udev, NDIS, 0, 17, thr, &Q, &X); /* set the threshold */ |
||
229 | C_write ( udev, NDIS, 1, 17, 0, &Q,&X); /* */ |
||
230 | |||
231 | C_read ( udev, NSCA,0,9,&dum, &Q,&X); |
||
232 | C_write( udev, NTGG, 0, 16, time_set, &Q,&X); |
||
233 | C_read ( udev, NTGG, 0, 15, &dum, &Q,&X); |
||
234 | |||
235 | |||
236 | |||
237 | Delay(time_set*1e-3+0.01); |
||
238 | int mych=2; |
||
239 | val=0; |
||
240 | for (int ch=1;ch<mych+1;ch++){ |
||
241 | C_read ( udev, NSCA, ch, 0, &val, &Q,&X); |
||
242 | //printf("%d\t", val); |
||
243 | C_read ( udev, NSCA, ch, 0, &val, &Q,&X); |
||
244 | } |
||
245 | val--; |
||
246 | x[i]=thr; |
||
247 | y[i]=val; |
||
248 | static int plothandle=0; |
||
249 | if (plothandle) DeleteGraphPlot (p4, SCALER_GRAPH, plothandle, VAL_IMMEDIATE_DRAW); |
||
250 | plothandle = PlotXY (p4,SCALER_GRAPH, x, y, i+1, VAL_DOUBLE, VAL_DOUBLE, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, VAL_BLUE); |
||
251 | |||
252 | sprintf ( str, "%d %d", thr, val) ; |
||
253 | fprintf( fp, "%s\n", str); |
||
254 | printf("scaler %s\n", str); |
||
255 | |||
256 | C_I(udev, 0); |
||
257 | if (!daq_on) break; |
||
258 | |||
259 | |||
260 | } |
||
261 | if (fp!=stdout) fclose ( fp ) ; |
||
262 | printf("Threshold scan in the file %s\n", filename); |
||
263 | |||
264 | WUSBXX_close (); |
||
265 | return 0; |
||
266 | } |
||
267 | |||
268 | #ifdef MAIN |
||
269 | int main( int argc, char **argv){ |
||
270 | |||
271 | |||
272 | |||
273 | char filename[256]="tmp.txt" ; |
||
274 | |||
275 | |||
276 | |||
277 | //ii=80000000/i-40; |
||
278 | //if (ii<72) ii=72; |
||
279 | //CAMAC_DGG(udev,0,7,0,ii,40,0,0); |
||
280 | //CAMAC_DGG(udev,1,1,1,12,8,0,0); |
||
281 | |||
282 | |||
283 | |||
284 | |||
285 | int min=0; |
||
286 | int max=1000; |
||
287 | int dx=10; |
||
288 | int time_set=200; |
||
289 | |||
290 | if (argc>1) min = atoi(argv[1]); |
||
291 | if (argc>2) max = atoi(argv[2]); |
||
292 | if (argc>3) dx = atoi(argv[3]); |
||
293 | if (argc>4) time_set = (int)(1000*atof(argv[4])); |
||
294 | if (argc>5) sprintf(filename, "%s", argv[5]); |
||
295 | scaler(min, max, dx, time_set, filename); |
||
296 | return 0; |
||
297 | } |
||
298 | |||
299 | #endif |