Rev 12 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 12 | Rev 15 | ||
---|---|---|---|
1 | // ./pts -a 0x1000000 -v 1 -w 2 turn LED ON |
1 | // ./pts -a 0x1000000 -v 1 -w 2 turn LED ON |
2 | // ./pts -a 0x1000000 -v 1 -w 1 turn LED OFF |
2 | // ./pts -a 0x1000000 -v 1 -w 1 turn LED OFF |
3 | #include <stdlib.h> |
3 | #include <stdlib.h> |
4 | #include <stdio.h> |
4 | #include <stdio.h> |
5 | #include <stdint.h> |
5 | #include <stdint.h> |
6 | #include <ctype.h> |
6 | #include <ctype.h> |
7 | #include <unistd.h> |
7 | #include <unistd.h> |
8 | #include <string.h> |
8 | #include <string.h> |
9 | #include "PtsModule.h" |
9 | #include "PtsModule.h" |
- | 10 | #include <getopt.h> |
|
10 | 11 | ||
11 | void Delay(double t){ |
12 | void Delay(double t){ |
12 | usleep(t*1e6); |
13 | usleep(t*1e6); |
13 | } |
14 | } |
14 | 15 | ||
15 | 16 | ||
16 | uint32_t ptaddr=0; |
17 | uint32_t ptaddr=0; |
17 | 18 | ||
18 | int Pts_write(uint32_t addr, uint32_t data ){ |
19 | int Pts_write(uint32_t addr, uint32_t data ){ |
19 | VME_A32D32_W(ptaddr+addr,&data); |
20 | VME_A32D32_W(ptaddr+addr,&data); |
20 | return 0; |
21 | return 0; |
21 | } |
22 | } |
22 | 23 | ||
23 | int Pts_read(uint32_t addr, uint32_t *data ){ |
24 | int Pts_read(uint32_t addr, uint32_t *data ){ |
24 | VME_A32D32_R(ptaddr+addr,data); |
25 | VME_A32D32_R(ptaddr+addr,data); |
25 | return 0; |
26 | return 0; |
26 | } |
27 | } |
27 | 28 | ||
28 | 29 | ||
29 | int Pts_erase( int verbose ) { |
30 | int Pts_erase( int verbose ) { |
30 | uint32_t dum; |
31 | uint32_t dum; |
31 | Pts_write( ADR_CSR1, 0 ); |
32 | Pts_write( ADR_CSR1, 0 ); |
32 | Delay(0.1); |
33 | Delay(0.1); |
33 | Pts_read( ADR_CSR1, &dum); |
34 | Pts_read( ADR_CSR1, &dum); |
34 | if( verbose ) printf( "CSR1(0x%02x) = 0x%04x\n", ADR_CSR1, |
35 | if( verbose ) printf( "CSR1(0x%02x) = 0x%04x\n", ADR_CSR1, |
35 | dum & 0xffff ); |
36 | dum & 0xffff ); |
36 | Pts_write( ADR_CSR1, CSR1_PROGRAM_ ); |
37 | Pts_write( ADR_CSR1, CSR1_PROGRAM_ ); |
37 | Delay(0.1); |
38 | Delay(0.1); |
38 | Pts_read( ADR_CSR1, &dum); |
39 | Pts_read( ADR_CSR1, &dum); |
39 | if( verbose ) printf( "CSR1(0x%02x) = 0x%04x\n", ADR_CSR1, |
40 | if( verbose ) printf( "CSR1(0x%02x) = 0x%04x\n", ADR_CSR1, |
40 | dum & 0xffff ); |
41 | dum & 0xffff ); |
41 | return 1; |
42 | return 1; |
42 | } |
43 | } |
43 | /* |
44 | /* |
44 | int Pts_configure( const char *filename, int mode, int verbose ) { |
45 | int Pts_configure( const char *filename, int mode, int verbose ) { |
45 | int len = strlen( filename ); |
46 | int len = strlen( filename ); |
46 | if( len > 4 ) { |
47 | if( len > 4 ) { |
47 | const char *p = filename+len-4; |
48 | const char *p = filename+len-4; |
48 | if( strcmp(p,".bit")==0 ) return Pts_configure_bit( filename, mode, verbose ); |
49 | if( strcmp(p,".bit")==0 ) return Pts_configure_bit( filename, mode, verbose ); |
49 | if( strcmp(p,".rbt")==0 ) return Pts_configure_rbt( filename, mode, verbose ); |
50 | if( strcmp(p,".rbt")==0 ) return Pts_configure_rbt( filename, mode, verbose ); |
50 | } |
51 | } |
51 | if( verbose ) fprintf( stderr, "Bad filename: \"%s\" .\n" ); |
52 | if( verbose ) fprintf( stderr, "Bad filename: \"%s\" .\n" ); |
52 | return -1; |
53 | return -1; |
53 | } |
54 | } |
54 | 55 | ||
55 | int PtsModule::configure_rbt( const char *filename, int mode, int verbose ) { |
56 | int PtsModule::configure_rbt( const char *filename, int mode, int verbose ) { |
56 | long n, pos; |
57 | long n, pos; |
57 | char cs[1024]; |
58 | char cs[1024]; |
58 | FILE *fp; |
59 | FILE *fp; |
59 | if( ( fp = fopen( filename, "r" ) ) == NULL ) { |
60 | if( ( fp = fopen( filename, "r" ) ) == NULL ) { |
60 | if( verbose ) fprintf( stderr, "cannot open \"%s\"\n", filename ); |
61 | if( verbose ) fprintf( stderr, "cannot open \"%s\"\n", filename ); |
61 | return -1; |
62 | return -1; |
62 | } |
63 | } |
63 | if( verbose ) printf( "file \"%s\" opened.\n", filename ); |
64 | if( verbose ) printf( "file \"%s\" opened.\n", filename ); |
64 | |
65 | |
65 | do{ |
66 | do{ |
66 | pos = ftell(fp); |
67 | pos = ftell(fp); |
67 | fgets( cs, sizeof(cs), fp ); |
68 | fgets( cs, sizeof(cs), fp ); |
68 | puts(cs); |
69 | puts(cs); |
69 | n=strlen(cs); |
70 | n=strlen(cs); |
70 | } while( n==0 || strspn( cs, " \t\n01" )!=n ); |
71 | } while( n==0 || strspn( cs, " \t\n01" )!=n ); |
71 | fseek( fp, pos, SEEK_SET ); |
72 | fseek( fp, pos, SEEK_SET ); |
72 | 73 | ||
73 | long nchar = 0; |
74 | long nchar = 0; |
74 | if( mode == SLAVESERIAL_MODE ) { |
75 | if( mode == SLAVESERIAL_MODE ) { |
75 | write( ADR_MODE, SLAVESERIAL_MODE ); |
76 | write( ADR_MODE, SLAVESERIAL_MODE ); |
76 | erase( verbose ); |
77 | erase( verbose ); |
77 | int c; |
78 | int c; |
78 | const long bit_per_dot = BYTE_PER_DOT*8; |
79 | const long bit_per_dot = BYTE_PER_DOT*8; |
79 | while( (c=getc(fp))!=EOF ){ |
80 | while( (c=getc(fp))!=EOF ){ |
80 | if( c=='0' || c=='1' ) { |
81 | if( c=='0' || c=='1' ) { |
81 | write( ADR_CFG, c=='0' ? 0 : 1 ); |
82 | write( ADR_CFG, c=='0' ? 0 : 1 ); |
82 | nchar++; |
83 | nchar++; |
83 | if( verbose && ++nchar%bit_per_dot==0 ) putchar('.'); |
84 | if( verbose && ++nchar%bit_per_dot==0 ) putchar('.'); |
84 | } |
85 | } |
85 | else if( isspace(c) ) continue; |
86 | else if( isspace(c) ) continue; |
86 | else { |
87 | else { |
87 | fprintf( stderr, "(%ld) illegal character : %c\n", nchar, c ); |
88 | fprintf( stderr, "(%ld) illegal character : %c\n", nchar, c ); |
88 | fclose(fp); |
89 | fclose(fp); |
89 | return -1; |
90 | return -1; |
90 | } |
91 | } |
91 | } |
92 | } |
92 | } else if(mode == SELECTMAP_MODE) { |
93 | } else if(mode == SELECTMAP_MODE) { |
93 | if(verbose) fprintf(stderr, "\nselectmap mode (rbt file) not supported\n"); |
94 | if(verbose) fprintf(stderr, "\nselectmap mode (rbt file) not supported\n"); |
94 | return -1; |
95 | return -1; |
95 | } else { |
96 | } else { |
96 | if(verbose) fprintf(stderr, "\nIllegal mode\n"); |
97 | if(verbose) fprintf(stderr, "\nIllegal mode\n"); |
97 | return -1; |
98 | return -1; |
98 | } |
99 | } |
99 | if(verbose) printf("\ntotal %ld bits\n", nchar); |
100 | if(verbose) printf("\ntotal %ld bits\n", nchar); |
100 | fclose(fp); |
101 | fclose(fp); |
101 | return check_configure( verbose ); |
102 | return check_configure( verbose ); |
102 | } |
103 | } |
103 | */ |
104 | */ |
104 | int Pts_configure_bit( const char *filename, int mode, int verbose ) { |
105 | int Pts_configure_bit( const char *filename, int mode, int verbose ) { |
105 | int c,j; |
106 | int c,j; |
106 | int dummyword; |
107 | int dummyword; |
107 | FILE *fp; |
108 | FILE *fp; |
108 | const long byte_per_dot = BYTE_PER_DOT; |
109 | const long byte_per_dot = BYTE_PER_DOT; |
109 | unsigned long nchar = 0; |
110 | unsigned long nchar = 0; |
110 | 111 | ||
111 | if( ( fp = fopen( filename, "rb" ) ) == NULL ) { |
112 | if( ( fp = fopen( filename, "rb" ) ) == NULL ) { |
112 | if( verbose ) fprintf( stderr, "cannot open \"%s\"\n", filename ); |
113 | if( verbose ) fprintf( stderr, "cannot open \"%s\"\n", filename ); |
113 | return -1; |
114 | return -1; |
114 | } |
115 | } |
115 | if(verbose) printf( "file \"%s\" opened.\n", filename ); |
116 | if(verbose) printf( "file \"%s\" opened.\n", filename ); |
116 | 117 | ||
117 | /* ------------------------------------------------------------ *\ |
118 | /* ------------------------------------------------------------ *\ |
118 | The data for the configuration start from 0xffff_ffff_aa99_aa66 |
119 | The data for the configuration start from 0xffff_ffff_aa99_aa66 |
119 | ( cf. xapp138; we don't know the definition of the BIT file ) |
120 | ( cf. xapp138; we don't know the definition of the BIT file ) |
120 | \* ------------------------------------------------------------ */ |
121 | \* ------------------------------------------------------------ */ |
121 | dummyword = 0; |
122 | dummyword = 0; |
122 | do{ |
123 | do{ |
123 | if( (c = getc( fp )) == EOF ) { |
124 | if( (c = getc( fp )) == EOF ) { |
124 | if(verbose) fprintf(stderr, "EOF detected. Exit.\n"); |
125 | if(verbose) fprintf(stderr, "EOF detected. Exit.\n"); |
125 | return -1; |
126 | return -1; |
126 | } |
127 | } |
127 | (c == 0xff) ? dummyword++ : (dummyword=0); |
128 | (c == 0xff) ? dummyword++ : (dummyword=0); |
128 | } while( dummyword < 4 ); |
129 | } while( dummyword < 4 ); |
129 | 130 | ||
130 | // const long byte_per_dot = BYTE_PER_DOT; |
131 | // const long byte_per_dot = BYTE_PER_DOT; |
131 | // unsigned long nchar = 0; |
132 | // unsigned long nchar = 0; |
132 | if( mode == SLAVESERIAL_MODE ) { |
133 | if( mode == SLAVESERIAL_MODE ) { |
133 | if(verbose) puts("slave serial mode"); |
134 | if(verbose) puts("slave serial mode"); |
134 | Pts_write( ADR_MODE, mode ); |
135 | Pts_write( ADR_MODE, mode ); |
135 | Pts_erase( verbose ); |
136 | Pts_erase( verbose ); |
136 | for( j=0; j<32; j++ ) Pts_write( ADR_CFG, 0x1 ); |
137 | for( j=0; j<32; j++ ) Pts_write( ADR_CFG, 0x1 ); |
137 | while( (c=getc(fp))!=EOF ){ |
138 | while( (c=getc(fp))!=EOF ){ |
138 | for( j=0; j<8; j++ ) Pts_write( ADR_CFG, (c>>(7-j))&0x1 ); |
139 | for( j=0; j<8; j++ ) Pts_write( ADR_CFG, (c>>(7-j))&0x1 ); |
139 | nchar++; |
140 | nchar++; |
140 | if( verbose && nchar%byte_per_dot==0 ) putchar('.'); |
141 | if( verbose && nchar%byte_per_dot==0 ) putchar('.'); |
141 | } |
142 | } |
142 | } else if( mode == SELECTMAP_MODE ) { |
143 | } else if( mode == SELECTMAP_MODE ) { |
143 | if( verbose ) puts("select map mode"); |
144 | if( verbose ) puts("select map mode"); |
144 | Pts_write( ADR_MODE, SELECTMAP_MODE ); |
145 | Pts_write( ADR_MODE, SELECTMAP_MODE ); |
145 | Pts_erase( verbose ); |
146 | Pts_erase( verbose ); |
146 | for( j=0; j<4; j++ ) Pts_write( ADR_CFG, 0xff ); |
147 | for( j=0; j<4; j++ ) Pts_write( ADR_CFG, 0xff ); |
147 | while( (c=getc(fp))!=EOF ){ |
148 | while( (c=getc(fp))!=EOF ){ |
148 | int cc = 0; |
149 | int cc = 0; |
149 | for(j=0; j<8; j++) cc |= ((c&(1<<j))>>j)<<(7-j); |
150 | for(j=0; j<8; j++) cc |= ((c&(1<<j))>>j)<<(7-j); |
150 | Pts_write( ADR_CFG, cc ); |
151 | Pts_write( ADR_CFG, cc ); |
151 | nchar++; |
152 | nchar++; |
152 | if( verbose && nchar%byte_per_dot==0 ) putchar('.'); |
153 | if( verbose && nchar%byte_per_dot==0 ) putchar('.'); |
153 | } |
154 | } |
154 | } else { |
155 | } else { |
155 | if(verbose) fprintf(stderr, "\nIllegal mode\n"); |
156 | if(verbose) fprintf(stderr, "\nIllegal mode\n"); |
156 | return -1; |
157 | return -1; |
157 | } |
158 | } |
158 | if(verbose) printf("\ntotal %ld bits\n", nchar); |
159 | if(verbose) printf("\ntotal %ld bits\n", nchar); |
159 | fclose(fp); |
160 | fclose(fp); |
160 | return Pts_check_configure( verbose ); |
161 | return Pts_check_configure( verbose ); |
161 | } |
162 | } |
162 | 163 | ||
163 | int Pts_check_configure( int verbose ) { |
164 | int Pts_check_configure( int verbose ) { |
164 | uint32_t csr1_value; |
165 | uint32_t csr1_value; |
165 | Pts_read(ADR_CSR1,&csr1_value); |
166 | Pts_read(ADR_CSR1,&csr1_value); |
166 | if(verbose) printf("CSR1(0x%02x)=0x%04x\n",ADR_CSR1,csr1_value&0xffff); |
167 | if(verbose) printf("CSR1(0x%02x)=0x%04x\n",ADR_CSR1,csr1_value&0xffff); |
167 | if(csr1_value&CSR1_DONE) { |
168 | if(csr1_value&CSR1_DONE) { |
168 | if(verbose) puts("configure complete."); |
169 | if(verbose) puts("configure complete."); |
169 | return 1; |
170 | return 1; |
170 | } else { |
171 | } else { |
171 | if(verbose) puts("configure not complete."); |
172 | if(verbose) puts("configure not complete."); |
172 | return -1; |
173 | return -1; |
173 | } |
174 | } |
174 | } |
175 | } |
175 | 176 | ||
176 | int Pts_reset( int verbose ) { |
177 | int Pts_reset( int verbose ) { |
177 | Pts_write(ADR_CSR0,1); |
178 | Pts_write(ADR_CSR0,1); |
178 | if( verbose ) printf( "CSR0(0x%02x) = 0x01\n", ADR_CSR0 ); |
179 | if( verbose ) printf( "CSR0(0x%02x) = 0x01\n", ADR_CSR0 ); |
179 | return 1; |
180 | return 1; |
180 | } |
181 | } |
181 | 182 | ||
182 | int Pts_write_csr( int verbose, uint32_t value ) { |
183 | int Pts_write_csr( int verbose, uint32_t value ) { |
183 | Pts_write(ADR_CSR0,value); |
184 | Pts_write(ADR_CSR0,value); |
184 | if( verbose ) printf( "Pts_write_csr 0x%08x\n", value ); |
185 | if( verbose ) printf( "Pts_write_csr 0x%08x\n", value ); |
185 | return 1; |
186 | return 1; |
186 | } |
187 | } |
187 | 188 | ||
188 | 189 | ||
189 | void help(char *argv){ |
190 | void help(char *argv){ |
190 | printf("Usage: %s -a <ptsaddr> -v <verbose> -c .... Pts_check_configure\n", argv); |
191 | printf("Usage: %s -a <ptsaddr> -v <verbose> -c .... Pts_check_configure\n", argv); |
191 | printf("Usage: %s -a <ptsaddr> -v <verbose> -i <filename> -b mode ... Pts_configure_bit\n", argv); |
192 | printf("Usage: %s -a <ptsaddr> -v <verbose> -i <filename> -b mode (2) ... Pts_configure_bit\n", argv); |
192 | printf("Usage: %s -a <ptsaddr> -v <verbose> -e .... Pts_erase\n", argv); |
193 | printf("Usage: %s -a <ptsaddr> -v <verbose> -e .... Pts_erase\n", argv); |
193 | printf("Usage: %s -a <ptsaddr> -v <verbose> - |
194 | printf("Usage: %s -a <ptsaddr> -v <verbose> -x .... Pts_reset\n", argv); |
- | 195 | printf("Usage: %s -a <ptsaddr> -v <verbose> -c .... Pts_check_configure\n", argv); |
|
- | 196 | printf("Usage: %s -a <ptsaddr> -v <verbose> -r .... vme read\n", argv); |
|
- | 197 | printf("Usage: %s -a <ptsaddr> -v <verbose> -w <value> .... vme write\n", argv); |
|
194 | printf("Usage: %s -a <ptsaddr> -v <verbose> - |
198 | printf("Usage: %s -a <ptsaddr> -v <verbose> -s <value> .... Pts_write_csr\n", argv); |
- | 199 | printf("Example: %s --address 0x1000000 --verbose 1 --erase\n", argv); |
|
- | 200 | printf("Example: %s --address 0x1000000 --verbose 1 --input pts_scaler.bit --load-bit 2\n", argv); |
|
- | 201 | printf("Example: %s --address 0x1000004 --write-csr 0x7600\n", argv); |
|
195 | } |
202 | } |
- | 203 | ||
196 | 204 | ||
- | 205 | ||
- | 206 | static const char *optString = "v:rxea:cs:w:i:b:h"; |
|
- | 207 | static const struct option longOpts[] = { |
|
- | 208 | { "verbose", required_argument, NULL, 'v' }, |
|
- | 209 | { "reset" , no_argument, NULL, 'x' }, |
|
- | 210 | { "erase" , no_argument, NULL, 'e' }, |
|
- | 211 | { "address", required_argument, NULL, 'a' }, |
|
- | 212 | { "check-configure", no_argument, NULL, 'c' }, |
|
- | 213 | { "write-csr", required_argument , NULL, 's' }, |
|
- | 214 | { "input", required_argument , NULL, 'i' }, |
|
- | 215 | { "load-bit", required_argument , NULL, 'b' }, |
|
- | 216 | { "read", no_argument , NULL, 'r' }, |
|
- | 217 | { "write",required_argument , NULL, 'w' }, |
|
- | 218 | { "help", no_argument, NULL, 'h' }, |
|
- | 219 | { NULL, no_argument, NULL, 0 } |
|
- | 220 | }; |
|
- | 221 | ||
197 | int main (int argc, char ** argv){ |
222 | int main (int argc, char ** argv){ |
- | 223 | ||
198 |
|
224 | uint32_t value; |
199 | 225 | ||
200 | int verbose=0; |
226 | int verbose=0; |
201 | char filename[256]="file.bit"; |
227 | char filename[256]="file.bit"; |
202 | int c; |
228 | int c; |
203 | opterr = 0; |
229 | opterr = 0; |
- | 230 | int longIndex = 0; |
|
204 | 231 | ||
205 | if (argc<2) { |
232 | if (argc<2) { |
206 | help(argv[0]); |
233 | help(argv[0]); |
207 | exit(-1); |
234 | exit(-1); |
208 | } |
235 | } |
209 | 236 | ||
210 | VME_START(NULL); |
237 | VME_START(NULL); |
- | 238 | ||
- | 239 | c = getopt_long( argc, argv, optString, longOpts, &longIndex ); |
|
- | 240 | while( c != -1 ) |
|
- | 241 | ||
211 |
|
242 | // while ((c = getopt (argc, argv, "v:xrea:csw:i:b:h")) != -1) |
- | 243 | { |
|
212 | switch (c) |
244 | switch (c) |
213 | { |
245 | { |
214 | case 'v': |
246 | case 'v': |
215 | verbose = atoi(optarg); |
247 | verbose = atoi(optarg); |
216 | break; |
248 | break; |
217 | case |
249 | case 'x': |
218 | Pts_reset(verbose); |
250 | Pts_reset(verbose); |
219 | break; // reset |
251 | break; // reset |
220 | case 'e': |
252 | case 'e': |
221 | Pts_erase(verbose); |
253 | Pts_erase(verbose); |
222 | break; // erase |
254 | break; // erase |
223 | case 'a': |
255 | case 'a': |
224 | ptaddr = strtoul (optarg,NULL,0); |
256 | ptaddr = strtoul (optarg,NULL,0); |
225 | break; // address |
257 | break; // address |
226 | case 'c': |
258 | case 'c': |
227 | Pts_check_configure( verbose ); |
259 | Pts_check_configure( verbose ); |
228 | break; |
260 | break; |
229 | case 'w': |
261 | case 'w': |
- | 262 | value = strtoul (optarg,NULL,0); |
|
- | 263 | Pts_write( 0 , value ); |
|
- | 264 | break; |
|
- | 265 | case 'r': |
|
- | 266 | value = 0; |
|
- | 267 | Pts_read( 0 , &value ); |
|
- | 268 | break; |
|
- | 269 | case 's': |
|
230 | value = strtoul (optarg,NULL,0); |
270 | value = strtoul (optarg,NULL,0); |
231 | Pts_write_csr( verbose, value ); |
271 | Pts_write_csr( verbose, value ); |
232 | break; |
272 | break; |
233 | case 'i': |
273 | case 'i': |
234 | sprintf(filename,"%s",optarg); |
274 | sprintf(filename,"%s",optarg); |
235 | break; // data width |
275 | break; // data width |
236 | case 'b': |
276 | case 'b': |
237 | //#define SELECTMAP_MODE 2 |
277 | //#define SELECTMAP_MODE 2 |
238 | //#define SLAVESERIAL_MODE 3 |
278 | //#define SLAVESERIAL_MODE 3 |
239 | value = strtoul (optarg,NULL,0); |
279 | value = strtoul (optarg,NULL,0); |
240 | Pts_configure_bit( filename, value, verbose ); |
280 | Pts_configure_bit( filename, value, verbose ); |
241 | break; |
281 | break; |
242 | case 'h': |
282 | case 'h': |
243 | help(argv[0]); |
283 | help(argv[0]); |
244 | break; |
284 | break; |
245 | default: |
285 | default: |
246 | printf("Unknown command %c\n",c); |
286 | printf("Unknown command %c\n",c); |
247 | } |
287 | } |
- | 288 | c = getopt_long( argc, argv, optString, longOpts, &longIndex ); |
|
- | 289 | } |
|
248 | VME_STOP(); |
290 | VME_STOP(); |
249 | } |
291 | } |
250 | 292 | ||
251 | 293 |