Imported Upstream version 3.2.2
[debian/gnuradio] / usrp / host / apps / usrper.cc
1 /* -*- c++ -*- */
2 /*
3  * USRP - Universal Software Radio Peripheral
4  *
5  * Copyright (C) 2003,2004 Free Software Foundation, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Boston, MA  02110-1301  USA
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <usb.h>                        /* needed for usb functions */
27 #include <getopt.h>
28 #include <assert.h>
29 #include <errno.h>
30
31 #include "usrp_prims.h"
32 #include "usrp_spi_defs.h"
33 #include <string.h>
34
35 char *prog_name;
36
37
38 static void
39 set_progname (char *path)
40 {
41   char *p = strrchr (path, '/');
42   if (p != 0)
43     prog_name = p+1;
44   else
45     prog_name = path;
46 }
47
48 static void
49 usage ()
50 {
51   fprintf (stderr, "usage: \n");
52   fprintf (stderr, "  %s [-v] [-w <which_board>] [-x] ...\n", prog_name);
53   fprintf (stderr, "  %s load_standard_bits\n", prog_name);
54   fprintf (stderr, "  %s load_firmware <file.ihx>\n", prog_name);
55   fprintf (stderr, "  %s load_fpga <file.rbf>\n", prog_name);
56   fprintf (stderr, "  %s write_fpga_reg <reg8> <value32>\n", prog_name);
57   fprintf (stderr, "  %s set_fpga_reset {on|off}\n", prog_name);
58   fprintf (stderr, "  %s set_fpga_tx_enable {on|off}\n", prog_name);
59   fprintf (stderr, "  %s set_fpga_rx_enable {on|off}\n", prog_name);
60   fprintf (stderr, "  ----- diagnostic routines -----\n");
61   fprintf (stderr, "  %s led0 {on|off}\n", prog_name);
62   fprintf (stderr, "  %s led1 {on|off}\n", prog_name);
63   fprintf (stderr, "  %s set_hash0 <hex-string>\n", prog_name);
64   fprintf (stderr, "  %s get_hash0\n", prog_name);
65   fprintf (stderr, "  %s i2c_read i2c_addr len\n", prog_name);
66   fprintf (stderr, "  %s i2c_write i2c_addr <hex-string>\n", prog_name);
67   fprintf (stderr, "  %s 9862a_write regno value\n", prog_name);
68   fprintf (stderr, "  %s 9862b_write regno value\n", prog_name);
69   fprintf (stderr, "  %s 9862a_read regno\n", prog_name);
70   fprintf (stderr, "  %s 9862b_read regno\n", prog_name);
71   exit (1);
72 }
73
74 #if 0
75 static void
76 die (const char *msg)
77 {
78   fprintf (stderr, "%s (die): %s\n", prog_name, msg);
79   exit (1);
80 }
81 #endif
82
83 static int 
84 hexval (char ch)
85 {
86   if ('0' <= ch && ch <= '9')
87     return ch - '0';
88
89   if ('a' <= ch && ch <= 'f')
90     return ch - 'a' + 10;
91
92   if ('A' <= ch && ch <= 'F')
93     return ch - 'A' + 10;
94
95   return -1;
96 }
97
98 static unsigned char *
99 hex_string_to_binary (const char *string, int *lenptr)
100 {
101   int   sl = strlen (string);
102   if (sl & 0x01){
103     fprintf (stderr, "%s: odd number of chars in <hex-string>\n", prog_name);
104     return 0;
105   }
106
107   int len = sl / 2;
108   *lenptr = len;
109   unsigned char *buf = new unsigned char [len];
110
111   for (int i = 0; i < len; i++){
112     int hi = hexval (string[2 * i]);
113     int lo = hexval (string[2 * i + 1]);
114     if (hi < 0 || lo < 0){
115       fprintf (stderr, "%s: invalid char in <hex-string>\n", prog_name);
116       delete [] buf;
117       return 0;
118     }
119     buf[i] = (hi << 4) | lo;
120   }
121   return buf;
122 }
123
124 static void
125 print_hex (FILE *fp, unsigned char *buf, int len)
126 {
127   for (int i = 0; i < len; i++){
128     fprintf (fp, "%02x", buf[i]);
129   }
130   fprintf (fp, "\n");
131 }
132
133 static void
134 chk_result (bool ok)
135 {
136   if (!ok){
137     fprintf (stderr, "%s: failed\n", prog_name);
138     exit (1);
139   }
140 }
141
142 static bool
143 get_on_off (const char *s)
144 {
145   if (strcmp (s, "on") == 0)
146     return true;
147
148   if (strcmp (s, "off") == 0)
149     return false;
150
151   usage ();                     // no return
152   return false;
153 }
154
155
156 int
157 main (int argc, char **argv)
158 {
159   int           ch;
160   bool          verbose = false;
161   int           which_board = 0;
162   bool          fx2_ok_p = false;
163   
164   set_progname (argv[0]);
165   
166   while ((ch = getopt (argc, argv, "vw:x")) != EOF){
167     switch (ch){
168
169     case 'v':
170       verbose = true;
171       break;
172       
173     case 'w':
174       which_board = strtol (optarg, 0, 0);
175       break;
176       
177     case 'x':
178       fx2_ok_p = true;
179       break;
180       
181     default:
182       usage ();
183     }
184   }
185
186   int nopts = argc - optind;
187
188   if (nopts < 1)
189     usage ();
190
191   const char *cmd = argv[optind++];
192   nopts--;
193
194   usrp_one_time_init ();
195
196   
197   struct usb_device *udev = usrp_find_device (which_board, fx2_ok_p);
198   if (udev == 0){
199     fprintf (stderr, "%s: failed to find usrp[%d]\n", prog_name, which_board);
200     exit (1);
201   }
202
203   if (usrp_unconfigured_usrp_p (udev)){
204     fprintf (stderr, "%s: found unconfigured usrp; needs firmware.\n", prog_name);
205   }
206
207   if (usrp_fx2_p (udev)){
208     fprintf (stderr, "%s: found unconfigured FX2; needs firmware.\n", prog_name);
209   }
210
211   struct usb_dev_handle *udh = usrp_open_cmd_interface (udev);
212   if (udh == 0){
213     fprintf (stderr, "%s: failed to open_cmd_interface\n", prog_name);
214     exit (1);
215   }
216
217 #define CHKARGS(n) if (nopts != n) usage (); else
218
219   if (strcmp (cmd, "led0") == 0){
220     CHKARGS (1);
221     bool on = get_on_off (argv[optind]);
222     chk_result (usrp_set_led (udh, 0, on));
223   }
224   else if (strcmp (cmd, "led1") == 0){
225     CHKARGS (1);
226     bool on = get_on_off (argv[optind]);
227     chk_result (usrp_set_led (udh, 1, on));
228   }
229   else if (strcmp (cmd, "led2") == 0){
230     CHKARGS (1);
231     bool on = get_on_off (argv[optind]);
232     chk_result (usrp_set_led (udh, 2, on));
233   }
234   else if (strcmp (cmd, "set_hash0") == 0){
235     CHKARGS (1);
236     char *p = argv[optind];
237     unsigned char buf[16];
238
239     memset (buf, ' ', 16);
240     for (int i = 0; i < 16 && *p; i++)
241       buf[i] = *p++;
242     
243     chk_result (usrp_set_hash (udh, 0, buf));
244   }
245   else if (strcmp (cmd, "get_hash0") == 0){
246     CHKARGS (0);
247     unsigned char buf[17];
248     memset (buf, 0, 17);
249     bool r = usrp_get_hash (udh, 0, buf);
250     if (r)
251       printf ("hash: %s\n", buf);
252     chk_result (r);
253   }
254   else if (strcmp (cmd, "load_fpga") == 0){
255     CHKARGS (1);
256     char *filename = argv[optind];
257     chk_result (usrp_load_fpga (udh, filename, true));
258   }
259   else if (strcmp (cmd, "load_firmware") == 0){
260     CHKARGS (1);
261     char *filename = argv[optind];
262     chk_result (usrp_load_firmware (udh, filename, true));
263   }
264   else if (strcmp (cmd, "write_fpga_reg") == 0){
265     CHKARGS (2);
266     chk_result (usrp_write_fpga_reg (udh, strtoul (argv[optind], 0, 0),
267                                      strtoul(argv[optind+1], 0, 0)));
268   }
269   else if (strcmp (cmd, "set_fpga_reset") == 0){
270     CHKARGS (1);
271     chk_result (usrp_set_fpga_reset (udh, get_on_off (argv[optind])));
272   }
273   else if (strcmp (cmd, "set_fpga_tx_enable") == 0){
274     CHKARGS (1);
275     chk_result (usrp_set_fpga_tx_enable (udh, get_on_off (argv[optind])));
276   }
277   else if (strcmp (cmd, "set_fpga_rx_enable") == 0){
278     CHKARGS (1);
279     chk_result (usrp_set_fpga_rx_enable (udh, get_on_off (argv[optind])));
280   }
281   else if (strcmp (cmd, "load_standard_bits") == 0){
282     CHKARGS (0);
283     usrp_close_interface (udh);
284     udh = 0;
285     chk_result (usrp_load_standard_bits (which_board, true));
286   }
287   else if (strcmp (cmd, "i2c_read") == 0){
288     CHKARGS (2);
289     int i2c_addr = strtol (argv[optind], 0, 0);
290     int len = strtol (argv[optind + 1], 0, 0);
291     if (len < 0)
292       chk_result (0);
293
294     unsigned char *buf = new unsigned char [len];
295     bool result = usrp_i2c_read (udh, i2c_addr, buf, len);
296     if (!result){
297       chk_result (0);
298     }
299     print_hex (stdout, buf, len);
300   }
301   else if (strcmp (cmd, "i2c_write") == 0){
302     CHKARGS (2);
303     int i2c_addr = strtol (argv[optind], 0, 0);
304     int len = 0;
305     char *hex_string  = argv[optind + 1];
306     unsigned char *buf = hex_string_to_binary (hex_string, &len);
307     if (buf == 0)
308       chk_result (0);
309
310     bool result = usrp_i2c_write (udh, i2c_addr, buf, len);
311     chk_result (result);
312   }
313   else if (strcmp (cmd, "9862a_write") == 0){
314     CHKARGS (2);
315     int regno = strtol (argv[optind], 0, 0);
316     int value = strtol (argv[optind+1], 0, 0);
317     chk_result (usrp_9862_write (udh, 0, regno, value));
318   }
319   else if (strcmp (cmd, "9862b_write") == 0){
320     CHKARGS (2);
321     int regno = strtol (argv[optind], 0, 0);
322     int value = strtol (argv[optind+1], 0, 0);
323     chk_result (usrp_9862_write (udh, 1, regno, value));
324   }
325   else if (strcmp (cmd, "9862a_read") == 0){
326     CHKARGS (1);
327     int regno = strtol (argv[optind], 0, 0);
328     unsigned char value;
329     bool result = usrp_9862_read (udh, 0, regno, &value);
330     if (!result){
331       chk_result (0);
332     }
333     fprintf (stdout, "reg[%d] = 0x%02x\n", regno, value);
334   }
335   else if (strcmp (cmd, "9862b_read") == 0){
336     CHKARGS (1);
337     int regno = strtol (argv[optind], 0, 0);
338     unsigned char value;
339     bool result = usrp_9862_read (udh, 1, regno, &value);
340     if (!result){
341       chk_result (0);
342     }
343     fprintf (stdout, "reg[%d] = 0x%02x\n", regno, value);
344   }
345   else {
346     usage ();
347   }
348
349   if (udh){
350     usrp_close_interface (udh);
351     udh = 0;
352   }
353
354   return 0;
355 }