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