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