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