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