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