Rev 52 | Rev 83 | Go to most recent revision | Details | Compare with Previous | 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 | |||
81 | f9daq | 13 | // sudo ./pts -a 0x1000000 -v 1 -w 2 turn LED ON |
14 | // sudo ./pts -a 0x1000000 -v 1 -w 1 turn LED OFF |
||
15 | // sudo ./ptsvmusb -a 0x02501000 -v 2 -i a.bit -b 2 |
||
52 | f9daq | 16 | #include <stdlib.h> |
17 | #include <stdio.h> |
||
18 | |||
19 | #include <ctype.h> |
||
20 | |||
21 | #include <string.h> |
||
22 | |||
81 | f9daq | 23 | #include "vme.h" |
52 | f9daq | 24 | |
25 | |||
26 | |||
27 | |||
81 | f9daq | 28 | |
29 | |||
52 | f9daq | 30 | int verbose; |
31 | uint32_t ptaddr=0; |
||
32 | char str[255]; |
||
33 | |||
34 | int Pts_write(uint32_t addr, uint32_t data ){ |
||
35 | int i; |
||
81 | f9daq | 36 | VME_A32D32_W(ptaddr+addr,data); |
52 | f9daq | 37 | return 0; |
38 | } |
||
39 | |||
40 | int Pts_Mwrite(uint32_t addr, uint32_t data ){ |
||
41 | int i; |
||
81 | f9daq | 42 | VME_MW(VME_A32, VME_D32, ptaddr+addr,data); |
52 | f9daq | 43 | return 0; |
44 | } |
||
45 | |||
46 | int Pts_read(uint32_t addr, uint32_t *data ){ |
||
47 | VME_A32D32_R(ptaddr+addr,data); |
||
48 | return 0; |
||
49 | } |
||
50 | |||
51 | |||
52 | int Pts_erase( int verbose ) { |
||
53 | uint32_t dum; |
||
54 | |||
55 | Pts_write( ADR_CSR1, 0 ); |
||
56 | Delay(0.1); |
||
57 | Pts_read( ADR_CSR1, &dum); |
||
58 | if( verbose ) { |
||
59 | sprintf(str, "CSR1(0x%02x) = 0x%04x\n", ADR_CSR1,dum & 0xffff ); |
||
60 | SetCtrlVal (p1, P1_RESPONSE, str); |
||
61 | } |
||
62 | Pts_write( ADR_CSR1, CSR1_PROGRAM_ ); |
||
63 | Delay(0.1); |
||
64 | Pts_read( ADR_CSR1, &dum); |
||
65 | if( verbose ) { |
||
66 | sprintf( str, "CSR1(0x%02x) = 0x%04x\n", ADR_CSR1, dum & 0xffff ); |
||
67 | SetCtrlVal (p1, P1_RESPONSE, str); |
||
68 | } |
||
69 | return 1; |
||
70 | } |
||
71 | |||
72 | int Pts_configure_bit( const char *filename, int mode, int verbose ) { |
||
73 | int c,j; |
||
74 | int dummyword; |
||
75 | FILE *fp; |
||
76 | const long byte_per_dot = BYTE_PER_DOT; |
||
77 | unsigned long nchar = 0; |
||
78 | |||
79 | if( ( fp = fopen( filename, "rb" ) ) == NULL ) { |
||
80 | if( verbose ) { |
||
81 | sprintf( str, "cannot open \"%s\"\n", filename ); |
||
82 | SetCtrlVal (p1, P1_RESPONSE, str); |
||
83 | } |
||
84 | return -1; |
||
85 | } |
||
86 | if(verbose) sprintf( str, "file \"%s\" opened.\n", filename ); |
||
87 | |||
88 | /* ------------------------------------------------------------ *\ |
||
89 | The data for the configuration start from 0xffff_ffff_aa99_aa66 |
||
90 | ( cf. xapp138; we don't know the definition of the BIT file ) |
||
91 | \* ------------------------------------------------------------ */ |
||
92 | dummyword = 0; |
||
93 | do{ |
||
94 | if( (c = getc( fp )) == EOF ) { |
||
95 | if(verbose) { |
||
96 | sprintf(str, "EOF detected. Exit.\n"); |
||
97 | |||
98 | SetCtrlVal (p1, P1_RESPONSE, str); |
||
99 | } |
||
100 | return -1; |
||
101 | } |
||
102 | (c == 0xff) ? dummyword++ : (dummyword=0); |
||
103 | } while( dummyword < 4 ); |
||
104 | |||
105 | if( mode == SLAVESERIAL_MODE ) { |
||
106 | if(verbose) SetCtrlVal(p1,P1_RESPONSE,"slave serial mode"); |
||
107 | Pts_write( ADR_MODE, mode ); |
||
108 | Pts_erase( verbose ); |
||
109 | for( j=0; j<32; j++ ) Pts_write( ADR_CFG, 0x1 ); |
||
110 | while( (c=getc(fp))!=EOF ){ |
||
111 | for( j=0; j<8; j++ ) Pts_write( ADR_CFG, (c>>(7-j))&0x1 ); |
||
112 | nchar++; |
||
113 | if( verbose && nchar%byte_per_dot==0 ) { |
||
114 | sprintf( str,"#"); |
||
115 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
116 | } |
||
117 | } |
||
118 | } else if( mode == SELECTMAP_MODE ) { |
||
119 | if( verbose ) SetCtrlVal(p1,P1_RESPONSE,"select map mode\n"); |
||
120 | Pts_write( ADR_MODE, SELECTMAP_MODE ); |
||
121 | Pts_erase( verbose ); |
||
81 | f9daq | 122 | VME_MWRST(); |
123 | for( j=0; j<4; j++ ) Pts_Mwrite( ADR_CFG, 0xff ); |
||
124 | VME_MWEXEC(); |
||
125 | |||
52 | f9daq | 126 | VME_MWRST(); |
127 | while( (c=getc(fp))!=EOF ){ |
||
128 | int cc = 0; |
||
129 | for(j=0; j<8; j++) cc |= ((c&(1<<j))>>j)<<(7-j); |
||
130 | Pts_Mwrite( ADR_CFG, cc ); |
||
131 | nchar++; |
||
132 | if( verbose && nchar%byte_per_dot==0 ){ |
||
133 | VME_MWEXEC(); |
||
134 | VME_MWRST(); |
||
135 | sprintf( str,"#"); |
||
136 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
137 | } |
||
138 | } |
||
139 | VME_MWEXEC(); |
||
140 | } else { |
||
141 | if(verbose) { |
||
142 | sprintf(str, "\nIllegal mode\n"); |
||
143 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
144 | } |
||
145 | return -1; |
||
146 | } |
||
147 | if(verbose) { |
||
148 | sprintf(str,"\ntotal %ld bits\n", nchar); |
||
149 | |||
150 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
151 | } |
||
152 | fclose(fp); |
||
153 | return Pts_check_configure( verbose ); |
||
154 | } |
||
155 | |||
156 | int Pts_check_configure( int verbose ) { |
||
157 | uint32_t csr1_value; |
||
158 | Pts_read(ADR_CSR1,&csr1_value); |
||
159 | if(verbose) { |
||
160 | sprintf( str,"CSR1(0x%02x)=0x%04x\n",ADR_CSR1,csr1_value&0xffff); |
||
161 | |||
162 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
163 | } |
||
164 | if(csr1_value&CSR1_DONE) { |
||
165 | if(verbose) SetCtrlVal(p1,P1_RESPONSE, "configure complete.\n"); |
||
166 | return 1; |
||
167 | } else { |
||
168 | if(verbose) SetCtrlVal(p1,P1_RESPONSE,"configure not complete."); |
||
169 | return -1; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | int Pts_reset( int verbose ) { |
||
174 | Pts_write(ADR_CSR0,1); |
||
175 | if( verbose ) { |
||
176 | sprintf( str, "CSR0(0x%02x) = 0x01\n", ADR_CSR0 ); |
||
177 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
178 | } |
||
179 | return 1; |
||
180 | } |
||
181 | |||
182 | int Pts_write_csr( int verbose, uint32_t value ) { |
||
183 | Pts_write(ADR_CSR0,value); |
||
184 | if( verbose ) { |
||
185 | sprintf( str,"Pts_write_csr 0x%08x\n", value ); |
||
186 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
187 | } |
||
188 | return 1; |
||
189 | } |
||
190 | |||
191 | |||
192 | void help(char *argv){ |
||
193 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -c .... Pts_check_configure\n", argv); |
||
194 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
195 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -i filename -b mode (2) ... Pts_configure_bit\n", argv); |
||
196 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
197 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -e .... Pts_erase\n", argv); |
||
198 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
199 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -x .... Pts_reset\n", argv); |
||
200 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
201 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -c .... Pts_check_configure\n", argv); |
||
202 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
203 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -r .... vme read\n", argv); |
||
204 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
205 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -w value .... vme write\n", argv); |
||
206 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
207 | sprintf(str,"Usage: %s -a ptsaddr -v verbose -s value .... Pts_write_csr\n", argv); |
||
208 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
209 | sprintf(str,"Example: %s --address 0x1000000 --verbose 1 --erase\n", argv); |
||
210 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
211 | sprintf(str,"Example: %s --address 0x1000000 --verbose 1 --input pts_scaler.bit --load-bit 2\n", argv); |
||
212 | SetCtrlVal(p1,P1_RESPONSE,str); |
||
213 | sprintf(str,"Example: %s --address 0x1000004 --write-csr 0x7600\n", argv);SetCtrlVal(p1,P1_RESPONSE,str); |
||
214 | } |
||
215 | |||
216 | |||
217 | |||
218 | int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, |
||
219 | LPSTR lpszCmdLine, int nCmdShow) |
||
220 | { |
||
221 | char saddr[255]; |
||
222 | if (InitCVIRTE (hInstance, 0, 0) == 0) |
||
223 | return -1; /* out of memory */ |
||
224 | if ((p1 = LoadPanel (0, "PtsModule_CVI.uir", P1)) < 0) |
||
225 | return -1; |
||
226 | DisplayPanel (p1); |
||
81 | f9daq | 227 | |
52 | f9daq | 228 | GetCtrlVal(p1,P1_VERBOSE,&verbose); |
229 | GetCtrlVal(p1,P1_ADDRESS,saddr); |
||
230 | ptaddr = strtoul (saddr,NULL,0); |
||
231 | RunUserInterface (); |
||
232 | DiscardPanel (p1); |
||
233 | VME_STOP(); |
||
234 | return 0; |
||
235 | } |
||
236 | |||
237 | int CVICALLBACK Reset (int panel, int control, int event, |
||
238 | void *callbackData, int eventData1, int eventData2) |
||
239 | { |
||
240 | switch (event) |
||
241 | { |
||
242 | case EVENT_COMMIT: |
||
243 | Pts_reset(verbose); |
||
244 | break; |
||
245 | } |
||
246 | return 0; |
||
247 | } |
||
248 | |||
249 | int CVICALLBACK Erase (int panel, int control, int event, |
||
250 | void *callbackData, int eventData1, int eventData2) |
||
251 | { |
||
252 | switch (event) |
||
253 | { |
||
254 | case EVENT_COMMIT: |
||
255 | Pts_erase(verbose); |
||
256 | break; |
||
257 | } |
||
258 | return 0; |
||
259 | } |
||
260 | |||
261 | int CVICALLBACK Download (int panel, int control, int event, |
||
262 | void *callbackData, int eventData1, int eventData2) |
||
263 | { |
||
264 | char filename[254]; |
||
265 | int mode=0; |
||
266 | switch (event) |
||
267 | { |
||
268 | case EVENT_COMMIT: |
||
269 | GetCtrlVal(p1,P1_FIRMWARE,filename); |
||
270 | GetCtrlVal(p1,P1_MODE,&mode); |
||
81 | f9daq | 271 | if(VME_CONNECTED() >=0 ) { |
272 | Pts_configure_bit( filename, mode, verbose ); |
||
273 | } else { |
||
274 | MessagePopup ("Warning", "Connect VME!!"); |
||
275 | } |
||
52 | f9daq | 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 | } |
||
81 | f9daq | 474 | |
475 | int CVICALLBACK Connect (int panel, int control, int event, |
||
476 | void *callbackData, int eventData1, int eventData2) |
||
477 | { |
||
478 | int interface = 0; |
||
479 | switch (event) |
||
480 | { |
||
481 | |||
482 | case EVENT_COMMIT: |
||
483 | GetCtrlVal (p1, P1_VMEINTERFACE, &interface); |
||
484 | VME_START(interface); |
||
485 | break; |
||
486 | } |
||
487 | return 0; |
||
488 | } |
||
489 | |||
490 | int CVICALLBACK Disconnect (int panel, int control, int event, |
||
491 | void *callbackData, int eventData1, int eventData2) |
||
492 | { |
||
493 | switch (event) |
||
494 | { |
||
495 | case EVENT_COMMIT: |
||
496 | VME_STOP(); |
||
497 | break; |
||
498 | } |
||
499 | return 0; |
||
500 | } |