3 * USRP - Universal Software Radio Peripheral
5 * Copyright (C) 2003,2004,2009 Free Software Foundation, Inc.
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.
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.
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
34 #include "usrp/usrp_prims.h"
35 #include "usrp_spi_defs.h"
39 #include <libusb-1.0/libusb.h>
47 set_progname (char *path)
49 char *p = strrchr (path, '/');
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);
86 fprintf (stderr, "%s (die): %s\n", prog_name, msg);
94 if ('0' <= ch && ch <= '9')
97 if ('a' <= ch && ch <= 'f')
100 if ('A' <= ch && ch <= 'F')
101 return ch - 'A' + 10;
106 static unsigned char *
107 hex_string_to_binary (const char *string, int *lenptr)
109 int sl = strlen (string);
111 fprintf (stderr, "%s: odd number of chars in <hex-string>\n", prog_name);
117 unsigned char *buf = new unsigned char [len];
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);
127 buf[i] = (hi << 4) | lo;
133 print_hex (FILE *fp, unsigned char *buf, int len)
135 for (int i = 0; i < len; i++){
136 fprintf (fp, "%02x", buf[i]);
145 fprintf (stderr, "%s: failed\n", prog_name);
151 get_on_off (const char *s)
153 if (strcmp (s, "on") == 0)
156 if (strcmp (s, "off") == 0)
159 usage (); // no return
165 main (int argc, char **argv)
168 bool verbose = false;
170 bool fx2_ok_p = false;
172 set_progname (argv[0]);
174 while ((ch = getopt (argc, argv, "vw:x")) != EOF){
182 which_board = strtol (optarg, 0, 0);
194 int nopts = argc - optind;
199 const char *cmd = argv[optind++];
202 usrp_one_time_init ();
205 libusb_device *udev = usrp_find_device (which_board, fx2_ok_p);
207 fprintf (stderr, "%s: failed to find usrp[%d]\n", prog_name, which_board);
211 if (usrp_unconfigured_usrp_p (udev)){
212 fprintf (stderr, "%s: found unconfigured usrp; needs firmware.\n", prog_name);
215 if (usrp_fx2_p (udev)){
216 fprintf (stderr, "%s: found unconfigured FX2; needs firmware.\n", prog_name);
219 libusb_device_handle *udh = usrp_open_cmd_interface (udev);
221 fprintf (stderr, "%s: failed to open_cmd_interface\n", prog_name);
225 #define CHKARGS(n) if (nopts != n) usage (); else
227 if (strcmp (cmd, "led0") == 0){
229 bool on = get_on_off (argv[optind]);
230 chk_result (usrp_set_led (udh, 0, on));
232 else if (strcmp (cmd, "led1") == 0){
234 bool on = get_on_off (argv[optind]);
235 chk_result (usrp_set_led (udh, 1, on));
237 else if (strcmp (cmd, "led2") == 0){
239 bool on = get_on_off (argv[optind]);
240 chk_result (usrp_set_led (udh, 2, on));
242 else if (strcmp (cmd, "set_hash0") == 0){
244 char *p = argv[optind];
245 unsigned char buf[16];
247 memset (buf, ' ', 16);
248 for (int i = 0; i < 16 && *p; i++)
251 chk_result (usrp_set_hash (udh, 0, buf));
253 else if (strcmp (cmd, "get_hash0") == 0){
255 unsigned char buf[17];
257 bool r = usrp_get_hash (udh, 0, buf);
259 printf ("hash: %s\n", buf);
262 else if (strcmp (cmd, "load_fpga") == 0){
264 char *filename = argv[optind];
265 chk_result (usrp_load_fpga (udh, filename, true));
267 else if (strcmp (cmd, "load_firmware") == 0){
269 char *filename = argv[optind];
270 chk_result (usrp_load_firmware (udh, filename, true));
272 else if (strcmp (cmd, "write_fpga_reg") == 0){
274 chk_result (usrp_write_fpga_reg (udh, strtoul (argv[optind], 0, 0),
275 strtoul(argv[optind+1], 0, 0)));
277 else if (strcmp (cmd, "set_fpga_reset") == 0){
279 chk_result (usrp_set_fpga_reset (udh, get_on_off (argv[optind])));
281 else if (strcmp (cmd, "set_fpga_tx_enable") == 0){
283 chk_result (usrp_set_fpga_tx_enable (udh, get_on_off (argv[optind])));
285 else if (strcmp (cmd, "set_fpga_rx_enable") == 0){
287 chk_result (usrp_set_fpga_rx_enable (udh, get_on_off (argv[optind])));
289 else if (strcmp (cmd, "load_standard_bits") == 0){
291 usrp_close_interface (udh);
293 chk_result (usrp_load_standard_bits (which_board, true));
295 else if (strcmp (cmd, "i2c_read") == 0){
297 int i2c_addr = strtol (argv[optind], 0, 0);
298 int len = strtol (argv[optind + 1], 0, 0);
302 unsigned char *buf = new unsigned char [len];
303 bool result = usrp_i2c_read (udh, i2c_addr, buf, len);
307 print_hex (stdout, buf, len);
309 else if (strcmp (cmd, "i2c_write") == 0){
311 int i2c_addr = strtol (argv[optind], 0, 0);
313 char *hex_string = argv[optind + 1];
314 unsigned char *buf = hex_string_to_binary (hex_string, &len);
318 bool result = usrp_i2c_write (udh, i2c_addr, buf, len);
321 else if (strcmp (cmd, "9862a_write") == 0){
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));
327 else if (strcmp (cmd, "9862b_write") == 0){
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));
333 else if (strcmp (cmd, "9862a_read") == 0){
335 int regno = strtol (argv[optind], 0, 0);
337 bool result = usrp_9862_read (udh, 0, regno, &value);
341 fprintf (stdout, "reg[%d] = 0x%02x\n", regno, value);
343 else if (strcmp (cmd, "9862b_read") == 0){
345 int regno = strtol (argv[optind], 0, 0);
347 bool result = usrp_9862_read (udh, 1, regno, &value);
351 fprintf (stdout, "reg[%d] = 0x%02x\n", regno, value);
358 usrp_close_interface (udh);