Rev 81 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
52 | f9daq | 1 | |
2 | |||
3 | |||
4 | #include <cvirte.h> |
||
5 | |||
6 | #include "PtsModule.h" |
||
7 | #include <userint.h> |
||
8 | #include "PtsModule_CVI.h" |
||
9 | #include <utility.h> |
||
10 | #include <formatio.h> |
||
11 | static int p1; |
||
12 | |||
13 | // ./pts -a 0x1000000 -v 1 -w 2 turn LED ON |
||
14 | // ./pts -a 0x1000000 -v 1 -w 1 turn LED OFF |
||
15 | #include <stdlib.h> |
||
16 | #include <stdio.h> |
||
17 | |||
18 | #include <ctype.h> |
||
19 | |||
20 | #include <string.h> |
||
21 | |||
22 | |||
23 | #ifndef CAEN_V1718 |
||
24 | //typedef unsigned long uint32_t; |
||
25 | #endif |
||
26 | /* |
||
27 | void Delay(double t){ |
||
28 | usleep(t*1e6); |
||
29 | } |
||
30 | */ |
||
31 | |||
32 | |||
33 | |||
34 | int verbose; |
||
35 | uint32_t ptaddr=0; |
||
36 | char str[255]; |
||
37 | |||
38 | int Pts_write(uint32_t addr, uint32_t data ){ |
||
39 | int i; |
||
40 | VME_A32D32_W(ptaddr+addr,&data); |
||
41 | return 0; |
||
42 | } |
||
43 | |||
44 | int Pts_Mwrite(uint32_t addr, uint32_t data ){ |
||
45 | int i; |
||
46 | VME_A32D32_MW(ptaddr+addr,&data); |
||
47 | return 0; |
||
48 | } |
||
49 | |||
50 | int Pts_read(uint32_t addr, uint32_t *data ){ |
||
51 | VME_A32D32_R(ptaddr+addr,data); |
||
52 | return 0; |
||
53 | } |
||
54 | |||
55 | |||
56 | int Pts_erase( int verbose ) { |
||
57 | uint32_t dum; |
||
58 | |||
59 | Pts_write( ADR_CSR1, 0 ); |
||
60 | Delay(0.1); |
||
61 | Pts_read( ADR_CSR1, &dum); |
||
62 | if( verbose ) { |
||
63 | sprintf(str, "CSR1(0x%02x) = 0x%04x\n", ADR_CSR1,dum & 0xffff ); |
||
64 | SetCtrlVal (p1, P1_RESPONSE, str); |
||
65 | } |
||
66 | Pts_write( ADR_CSR1, CSR1_PROGRAM_ ); |
||
67 | Delay(0.1); |
||
68 | Pts_read( ADR_CSR1, &dum); |
||
69 | if( verbose ) { |
||
70 | sprintf( str, "CSR1(0x%02x) = 0x%04x\n", ADR_CSR1, dum & 0xffff ); |
||
71 | SetCtrlVal (p1, P1_RESPONSE, str); |
||
72 | } |
||
73 | return 1; |
||
74 | } |
||
75 | |||
76 | int Pts_configure_bit( const char *filename, int mode, int verbose ) { |
||
77 | int c,j; |
||
78 | int dummyword; |
||
79 | FILE *fp; |
||
80 | const long byte_per_dot = BYTE_PER_DOT; |
||
81 | unsigned long nchar = 0; |
||
82 | |||
83 | if( ( fp = fopen( filename, "rb" ) ) == NULL ) { |
||
84 | if( verbose ) { |
||
85 | sprintf( str, "cannot open \"%s\"\n", filename ); |
||
86 | SetCtrlVal (p1, P1_RESPONSE, str); |
||
87 | } |
||
88 | return -1; |
||
89 | } |
||
90 | if(verbose) sprintf( str, "file \"%s\" opened.\n", filename ); |
||
91 | |||
92 | /* ------------------------------------------------------------ *\ |
||
93 | The data for the configuration start from 0xffff_ffff_aa99_aa66 |
||
94 | ( cf. xapp138; we don't know the definition of the BIT file ) |
||
95 | \* ------------------------------------------------------------ */ |
||
96 | dummyword = 0; |
||
97 | do{ |
||
98 | if( (c = getc( fp )) == EOF ) { |
||
99 | if(verbose) { |
||
100 | sprintf(str, "EOF detected. Exit.\n"); |
||
101 | |||
102 | SetCtrlVal (p1, P1_RESPONSE, str); |
||
103 | } |
||
104 | return -1; |
||
105 | } |
||
106 | (c == 0xff) ? dummyword++ : (dummyword=0); |
||
107 | } while( dummyword < 4 ); |
||
108 | |||
109 | // const long byte_per_dot = BYTE_PER_DOT; |
||
110 | // unsigned long nchar = 0; |
||
111 | if( mode == SLAVESERIAL_MODE ) { |
||
112 | if(verbose) SetCtrlVal(p1,P1_RESPONSE,"slave serial mode"); |
||
113 | Pts_write( ADR_MODE, mode ); |
||
114 | Pts_erase( verbose ); |
||
115 | for( j=0; j<32; j++ ) Pts_write( ADR_CFG, 0x1 ); |
||
116 | while( (c=getc(fp))!=EOF ){ |
||
117 | for( j=0; j<8; j++ ) Pts_write( ADR_CFG, (c>>(7-j))&0x1 ); |
||
118 | nchar++; |
||
119 | if( verbose && nchar%byte_per_dot==0 ) { |
||
120 | sprintf( str,"#"); |
||
121 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
122 | } |
||
123 | } |
||
124 | } else if( mode == SELECTMAP_MODE ) { |
||
125 | if( verbose ) SetCtrlVal(p1,P1_RESPONSE,"select map mode\n"); |
||
126 | Pts_write( ADR_MODE, SELECTMAP_MODE ); |
||
127 | Pts_erase( verbose ); |
||
128 | for( j=0; j<4; j++ ) Pts_write( ADR_CFG, 0xff ); |
||
129 | VME_MWRST(); |
||
130 | while( (c=getc(fp))!=EOF ){ |
||
131 | int cc = 0; |
||
132 | for(j=0; j<8; j++) cc |= ((c&(1<<j))>>j)<<(7-j); |
||
133 | Pts_Mwrite( ADR_CFG, cc ); |
||
134 | nchar++; |
||
135 | if( verbose && nchar%byte_per_dot==0 ){ |
||
136 | VME_MWEXEC(); |
||
137 | VME_MWRST(); |
||
138 | sprintf( str,"#"); |
||
139 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
140 | } |
||
141 | } |
||
142 | VME_MWEXEC(); |
||
143 | } else { |
||
144 | if(verbose) { |
||
145 | sprintf(str, "\nIllegal mode\n"); |
||
146 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
147 | } |
||
148 | return -1; |
||
149 | } |
||
150 | if(verbose) { |
||
151 | sprintf(str,"\ntotal %ld bits\n", nchar); |
||
152 | |||
153 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
154 | } |
||
155 | fclose(fp); |
||
156 | return Pts_check_configure( verbose ); |
||
157 | } |
||
158 | |||
159 | int Pts_check_configure( int verbose ) { |
||
160 | uint32_t csr1_value; |
||
161 | Pts_read(ADR_CSR1,&csr1_value); |
||
162 | if(verbose) { |
||
163 | sprintf( str,"CSR1(0x%02x)=0x%04x\n",ADR_CSR1,csr1_value&0xffff); |
||
164 | |||
165 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
166 | } |
||
167 | if(csr1_value&CSR1_DONE) { |
||
168 | if(verbose) SetCtrlVal(p1,P1_RESPONSE, "configure complete.\n"); |
||
169 | return 1; |
||
170 | } else { |
||
171 | if(verbose) SetCtrlVal(p1,P1_RESPONSE,"configure not complete."); |
||
172 | return -1; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | int Pts_reset( int verbose ) { |
||
177 | Pts_write(ADR_CSR0,1); |
||
178 | if( verbose ) { |
||
179 | sprintf( str, "CSR0(0x%02x) = 0x01\n", ADR_CSR0 ); |
||
180 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
181 | } |
||
182 | return 1; |
||
183 | } |
||
184 | |||
185 | int Pts_write_csr( int verbose, uint32_t value ) { |
||
186 | Pts_write(ADR_CSR0,value); |
||
187 | if( verbose ) { |
||
188 | sprintf( str,"Pts_write_csr 0x%08x\n", value ); |
||
189 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
190 | } |
||
191 | return 1; |
||
192 | } |
||
193 | |||
194 | |||
195 | void help(char *argv){ |
||
196 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -c .... Pts_check_configure\n", argv); |
||
197 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
198 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -i filename -b mode (2) ... Pts_configure_bit\n", argv); |
||
199 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
200 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -e .... Pts_erase\n", argv); |
||
201 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
202 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -x .... Pts_reset\n", argv); |
||
203 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
204 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -c .... Pts_check_configure\n", argv); |
||
205 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
206 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -r .... vme read\n", argv); |
||
207 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
208 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -w value .... vme write\n", argv); |
||
209 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
210 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -s value .... Pts_write_csr\n", argv); |
||
211 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
212 | sprintf(str,"Example: %s --address 0x1000000 --verbose 1 --erase\n", argv); |
||
213 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
214 | sprintf(str,"Example: %s --address 0x1000000 --verbose 1 --input pts_scaler.bit --load-bit 2\n", argv); |
||
215 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
216 | sprintf(str,"Example: %s --address 0x1000004 --write-csr 0x7600\n", argv);SetCtrlVal(p1,P1_RESPONSE,str); |
||
217 | } |
||
218 | |||
219 | |||
220 | |||
221 | int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, |
||
222 | LPSTR lpszCmdLine, int nCmdShow) |
||
223 | { |
||
224 | char saddr[255]; |
||
225 | if (InitCVIRTE (hInstance, 0, 0) == 0) |
||
226 | return -1; /* out of memory */ |
||
227 | if ((p1 = LoadPanel (0, "PtsModule_CVI.uir", P1)) < 0) |
||
228 | return -1; |
||
229 | DisplayPanel (p1); |
||
230 | VME_START(NULL); |
||
231 | GetCtrlVal(p1,P1_VERBOSE,&verbose); |
||
232 | GetCtrlVal(p1,P1_ADDRESS,saddr); |
||
233 | ptaddr = strtoul (saddr,NULL,0); |
||
234 | RunUserInterface (); |
||
235 | DiscardPanel (p1); |
||
236 | VME_STOP(); |
||
237 | return 0; |
||
238 | } |
||
239 | |||
240 | int CVICALLBACK Reset (int panel, int control, int event, |
||
241 | void *callbackData, int eventData1, int eventData2) |
||
242 | { |
||
243 | switch (event) |
||
244 | { |
||
245 | case EVENT_COMMIT: |
||
246 | Pts_reset(verbose); |
||
247 | break; |
||
248 | } |
||
249 | return 0; |
||
250 | } |
||
251 | |||
252 | int CVICALLBACK Erase (int panel, int control, int event, |
||
253 | void *callbackData, int eventData1, int eventData2) |
||
254 | { |
||
255 | switch (event) |
||
256 | { |
||
257 | case EVENT_COMMIT: |
||
258 | Pts_erase(verbose); |
||
259 | break; |
||
260 | } |
||
261 | return 0; |
||
262 | } |
||
263 | |||
264 | int CVICALLBACK Download (int panel, int control, int event, |
||
265 | void *callbackData, int eventData1, int eventData2) |
||
266 | { |
||
267 | char filename[254]; |
||
268 | int mode=0; |
||
269 | switch (event) |
||
270 | { |
||
271 | case EVENT_COMMIT: |
||
272 | GetCtrlVal(p1,P1_FIRMWARE,filename); |
||
273 | GetCtrlVal(p1,P1_MODE,&mode); |
||
274 | |||
275 | Pts_configure_bit( filename, mode, verbose ); |
||
276 | break; |
||
277 | } |
||
278 | return 0; |
||
279 | } |
||
280 | |||
281 | int CVICALLBACK Check (int panel, int control, int event, |
||
282 | void *callbackData, int eventData1, int eventData2) |
||
283 | { |
||
284 | switch (event) |
||
285 | { |
||
286 | case EVENT_COMMIT: |
||
287 | Pts_check_configure( verbose ); |
||
288 | break; |
||
289 | } |
||
290 | return 0; |
||
291 | } |
||
292 | |||
293 | int CVICALLBACK Read (int panel, int control, int event, |
||
294 | void *callbackData, int eventData1, int eventData2) |
||
295 | { |
||
296 | uint32_t value=0; |
||
297 | char str[0xFF]; |
||
298 | char saddr[256]; |
||
299 | int offset; |
||
300 | switch (event) |
||
301 | { |
||
302 | case EVENT_COMMIT: |
||
303 | GetCtrlVal(p1,P1_OFFSET,saddr); |
||
304 | offset = strtoul (saddr,NULL,0); |
||
305 | Pts_read( offset , &value ); |
||
306 | if( verbose ) { |
||
307 | sprintf(str, "VME Read addr 0x%0x+0x%0x response 0x%0x\n", ptaddr, offset, value ); |
||
308 | SetCtrlVal (p1, P1_RESPONSE, str); |
||
309 | } |
||
310 | break; |
||
311 | } |
||
312 | return 0; |
||
313 | } |
||
314 | |||
315 | int CVICALLBACK Write (int panel, int control, int event, |
||
316 | void *callbackData, int eventData1, int eventData2) |
||
317 | { |
||
318 | int value; |
||
319 | int offset; |
||
320 | char svalue[256]; |
||
321 | char saddr[256]; |
||
322 | switch (event) |
||
323 | { |
||
324 | case EVENT_COMMIT: |
||
325 | GetCtrlVal(p1,P1_DATA,svalue); |
||
326 | GetCtrlVal(p1,P1_OFFSET,saddr); |
||
327 | value = strtoul (svalue,NULL,0); |
||
328 | offset = strtoul (saddr,NULL,0); |
||
329 | Pts_write( offset , value ); |
||
330 | if( verbose ) { |
||
331 | sprintf(str, "VME Write addr 0x%0x+0x%0x data 0x%0x\n", ptaddr, offset, value ); |
||
332 | SetCtrlVal (p1, P1_RESPONSE, str); |
||
333 | } |
||
334 | break; |
||
335 | } |
||
336 | return 0; |
||
337 | } |
||
338 | |||
339 | int CVICALLBACK WriteCSR (int panel, int control, int event, |
||
340 | void *callbackData, int eventData1, int eventData2) |
||
341 | { |
||
342 | int value; |
||
343 | char svalue[256]; |
||
344 | switch (event) |
||
345 | { |
||
346 | case EVENT_COMMIT: |
||
347 | GetCtrlVal(p1,P1_DATA,svalue); |
||
348 | value = strtoul (svalue,NULL,0); |
||
349 | Pts_write_csr( verbose, value ); |
||
350 | break; |
||
351 | } |
||
352 | return 0; |
||
353 | } |
||
354 | |||
355 | int CVICALLBACK Verbose (int panel, int control, int event, |
||
356 | void *callbackData, int eventData1, int eventData2) |
||
357 | { |
||
358 | int * data; |
||
359 | switch (event) |
||
360 | { |
||
361 | case EVENT_COMMIT: |
||
362 | GetCtrlVal(panel,control,&verbose); |
||
363 | |||
364 | break; |
||
365 | } |
||
366 | return 0; |
||
367 | } |
||
368 | |||
369 | int CVICALLBACK Help (int panel, int control, int event, |
||
370 | void *callbackData, int eventData1, int eventData2) |
||
371 | { |
||
372 | switch (event) |
||
373 | { |
||
374 | case EVENT_COMMIT: |
||
375 | help("PtsModule"); |
||
376 | break; |
||
377 | } |
||
378 | return 0; |
||
379 | } |
||
380 | |||
381 | int CVICALLBACK Address (int panel, int control, int event, |
||
382 | void *callbackData, int eventData1, int eventData2) |
||
383 | { |
||
384 | char saddr[100]; |
||
385 | switch (event) |
||
386 | { |
||
387 | case EVENT_COMMIT: |
||
388 | GetCtrlVal(p1,P1_ADDRESS,saddr); |
||
389 | ptaddr = strtoul (saddr,NULL,0); |
||
390 | break; |
||
391 | } |
||
392 | return 0; |
||
393 | } |
||
394 | |||
395 | int CVICALLBACK Exit (int panel, int control, int event, |
||
396 | void *callbackData, int eventData1, int eventData2) |
||
397 | { |
||
398 | switch (event) |
||
399 | { |
||
400 | case EVENT_COMMIT: |
||
401 | QuitUserInterface(0); |
||
402 | break; |
||
403 | } |
||
404 | return 0; |
||
405 | } |
||
406 | |||
407 | int CVICALLBACK FSelect (int panel, int control, int event, |
||
408 | void *callbackData, int eventData1, int eventData2) |
||
409 | { |
||
410 | int status; |
||
411 | char pathName[MAX_PATHNAME_LEN]; |
||
412 | switch (event) |
||
413 | { |
||
414 | case EVENT_COMMIT: |
||
415 | status = FileSelectPopup ("", "*.bit", "*.bit", "Select Firmware file", |
||
416 | VAL_SELECT_BUTTON, 0, 0, 1, 0, pathName); |
||
417 | if (status>0) SetCtrlVal(p1,P1_FIRMWARE,pathName); |
||
418 | break; |
||
419 | } |
||
420 | return 0; |
||
421 | } |
||
422 | |||
423 | int CVICALLBACK LedCB (int panel, int control, int event, |
||
424 | void *callbackData, int eventData1, int eventData2) |
||
425 | { |
||
426 | switch (event) |
||
427 | { |
||
428 | int value=0; |
||
429 | case EVENT_COMMIT: |
||
430 | GetCtrlVal(p1,P1_LED,&value); |
||
431 | |||
432 | Pts_write( 0 , value+1 ); |
||
433 | |||
434 | break; |
||
435 | } |
||
436 | return 0; |
||
437 | } |
||
438 | |||
439 | int CVICALLBACK Execute (int panel, int control, int event, |
||
440 | void *callbackData, int eventData1, int eventData2) |
||
441 | { |
||
442 | switch (event) |
||
443 | { |
||
444 | case EVENT_COMMIT:{ |
||
445 | int n,i,nb; |
||
446 | char buf[400]; |
||
447 | unsigned int data, addr; |
||
448 | char sdata[255], saddr[255], rw[255]; |
||
449 | GetNumTextBoxLines(panel, P1_COMMANDS,&n); |
||
450 | for (i=0;i<n;i++){ |
||
451 | GetTextBoxLine(panel, P1_COMMANDS, i, buf); |
||
452 | nb = sscanf(buf,"%s%s%s",rw, saddr,sdata); |
||
453 | addr = strtoul (saddr,NULL,0); |
||
454 | data = strtoul (sdata,NULL,0); |
||
455 | if (strstr(rw,"W")!=NULL){ |
||
456 | Pts_write( addr , data ); |
||
457 | if( verbose ) { |
||
458 | sprintf(str, "VME Write addr 0x%0x+0x%0x data 0x%0x\n", ptaddr, addr, data ); |
||
459 | SetCtrlVal (p1, P1_RESPONSE, str); |
||
460 | } |
||
461 | } else { |
||
462 | Pts_read( addr , &data ); |
||
463 | if( verbose ) { |
||
464 | sprintf(str, "VME Read addr 0x%0x+0x%0x response 0x%0x\n", ptaddr, addr, data); |
||
465 | SetCtrlVal (p1, P1_RESPONSE, str); |
||
466 | } |
||
467 | } |
||
468 | } |
||
469 | break; |
||
470 | } |
||
471 | } |
||
472 | return 0; |
||
473 | } |