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