File: usrper.cc

package info (click to toggle)
usrp 0.8-1
  • links: PTS
  • area: contrib
  • in suites: sarge
  • size: 9,420 kB
  • ctags: 5,295
  • sloc: sh: 8,481; cpp: 7,533; ansic: 4,579; makefile: 734; python: 557; xml: 390; pascal: 348
file content (560 lines) | stat: -rw-r--r-- 13,639 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/* -*- c++ -*- */
/*
 * USRP - Universal Software Radio Peripheral
 *
 * Copyright (C) 2003,2004 Free Software Foundation, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <usb.h>			/* needed for usb functions */
#include <getopt.h>
#include <assert.h>
#include <errno.h>

#include "usrp_prims.h"
#include "spi_defs.h"

char *prog_name;


static void
set_progname (char *path)
{
  char *p = strrchr (path, '/');
  if (p != 0)
    prog_name = p+1;
  else
    prog_name = path;
}

static void
usage ()
{
  fprintf (stderr, "usage: \n");
  fprintf (stderr, "  %s [-v] [-w <which_board>] [-x] ...\n", prog_name);
  fprintf (stderr, "  %s load_standard_bits\n", prog_name);
  fprintf (stderr, "  %s load_firmware <file.ihx>\n", prog_name);
  fprintf (stderr, "  %s load_fpga <file.rbf>\n", prog_name);
  fprintf (stderr, "  %s write_fpga_reg <reg8> <value32>\n", prog_name);
  fprintf (stderr, "  %s set_fpga_reset {on|off}\n", prog_name);
  fprintf (stderr, "  %s set_fpga_tx_enable {on|off}\n", prog_name);
  fprintf (stderr, "  %s set_fpga_rx_enable {on|off}\n", prog_name);
  fprintf (stderr, "  %s set_sleep_bits <bits4> <mask4>\n", prog_name);
  fprintf (stderr, "  ----- diagnostic routines -----\n");
  fprintf (stderr, "  %s led0 {on|off}\n", prog_name);
  fprintf (stderr, "  %s led1 {on|off}\n", prog_name);
  fprintf (stderr, "  %s set_hash0 <hex-string>\n", prog_name);
  fprintf (stderr, "  %s get_hash0\n", prog_name);
  fprintf (stderr, "  %s i2c_read i2c_addr len\n", prog_name);
  fprintf (stderr, "  %s i2c_write i2c_addr <hex-string>\n", prog_name);
  fprintf (stderr, "  %s 9862a_write regno value\n", prog_name);
  fprintf (stderr, "  %s 9862b_write regno value\n", prog_name);
  fprintf (stderr, "  %s 9862a_read regno\n", prog_name);
  fprintf (stderr, "  %s 9862b_read regno\n", prog_name);
  exit (1);
}

static void
die (const char *msg)
{
  fprintf (stderr, "%s (die): %s\n", prog_name, msg);
  exit (1);
}

static int 
hexval (char ch)
{
  if ('0' <= ch && ch <= '9')
    return ch - '0';

  if ('a' <= ch && ch <= 'f')
    return ch - 'a' + 10;

  if ('A' <= ch && ch <= 'F')
    return ch - 'A' + 10;

  return -1;
}

static unsigned char *
hex_string_to_binary (const char *string, int *lenptr)
{
  int	sl = strlen (string);
  if (sl & 0x01){
    fprintf (stderr, "%s: odd number of chars in <hex-string>\n", prog_name);
    return 0;
  }

  int len = sl / 2;
  *lenptr = len;
  unsigned char *buf = new unsigned char [len];

  for (int i = 0; i < len; i++){
    int hi = hexval (string[2 * i]);
    int lo = hexval (string[2 * i + 1]);
    if (hi < 0 || lo < 0){
      fprintf (stderr, "%s: invalid char in <hex-string>\n", prog_name);
      delete [] buf;
      return 0;
    }
    buf[i] = (hi << 4) | lo;
  }
  return buf;
}

static void
print_hex (FILE *fp, unsigned char *buf, int len)
{
  for (int i = 0; i < len; i++){
    fprintf (fp, "%02x", buf[i]);
  }
  fprintf (fp, "\n");
}

static void
chk_result (bool ok)
{
  if (!ok){
    fprintf (stderr, "%s: failed\n", prog_name);
    exit (1);
  }
}

static bool
get_on_off (const char *s)
{
  if (strcmp (s, "on") == 0)
    return true;

  if (strcmp (s, "off") == 0)
    return false;

  usage ();			// no return
  return false;
}


int
main (int argc, char **argv)
{
  int		ch;
  bool		verbose = false;
  int		which_board = 0;
  bool		fx2_ok_p = false;
  
  set_progname (argv[0]);
  
  while ((ch = getopt (argc, argv, "vw:x")) != EOF){
    switch (ch){

    case 'v':
      verbose = true;
      break;
      
    case 'w':
      which_board = strtol (optarg, 0, 0);
      break;
      
    case 'x':
      fx2_ok_p = true;
      break;
      
    default:
      usage ();
    }
  }

  int nopts = argc - optind;

  if (nopts < 1)
    usage ();

  const char *cmd = argv[optind++];
  nopts--;

  usrp_one_time_init ();

  
  struct usb_device *udev = usrp_find_device (which_board, fx2_ok_p);
  if (udev == 0){
    fprintf (stderr, "%s: failed to find usrp[%d]\n", prog_name, which_board);
    exit (1);
  }

  if (usrp_unconfigured_usrp_p (udev)){
    fprintf (stderr, "%s: found unconfigured usrp; needs firmware.\n", prog_name);
  }

  if (usrp_fx2_p (udev)){
    fprintf (stderr, "%s: found unconfigured FX2; needs firmware.\n", prog_name);
  }

  struct usb_dev_handle *udh = usrp_open_cmd_interface (udev);
  if (udh == 0){
    fprintf (stderr, "%s: failed to open_cmd_interface\n", prog_name);
    exit (1);
  }

#define CHKARGS(n) if (nopts != n) usage (); else

  if (strcmp (cmd, "led0") == 0){
    CHKARGS (1);
    bool on = get_on_off (argv[optind]);
    chk_result (usrp_set_led (udh, 0, on));
  }
  else if (strcmp (cmd, "led1") == 0){
    CHKARGS (1);
    bool on = get_on_off (argv[optind]);
    chk_result (usrp_set_led (udh, 1, on));
  }
  else if (strcmp (cmd, "led2") == 0){
    CHKARGS (1);
    bool on = get_on_off (argv[optind]);
    chk_result (usrp_set_led (udh, 2, on));
  }
  else if (strcmp (cmd, "set_hash0") == 0){
    CHKARGS (1);
    char *p = argv[optind];
    unsigned char buf[16];

    memset (buf, ' ', 16);
    for (int i = 0; i < 16 && *p; i++)
      buf[i] = *p++;
    
    chk_result (usrp_set_hash (udh, 0, buf));
  }
  else if (strcmp (cmd, "get_hash0") == 0){
    CHKARGS (0);
    unsigned char buf[17];
    memset (buf, 0, 17);
    bool r = usrp_get_hash (udh, 0, buf);
    if (r)
      printf ("hash: %s\n", buf);
    chk_result (r);
  }
  else if (strcmp (cmd, "load_fpga") == 0){
    CHKARGS (1);
    char *filename = argv[optind];
    chk_result (usrp_load_fpga (udh, filename, true));
  }
  else if (strcmp (cmd, "load_firmware") == 0){
    CHKARGS (1);
    char *filename = argv[optind];
    chk_result (usrp_load_firmware (udh, filename, true));
  }
  else if (strcmp (cmd, "write_fpga_reg") == 0){
    CHKARGS (2);
    chk_result (usrp_write_fpga_reg (udh, strtoul (argv[optind], 0, 0),
				     strtoul(argv[optind+1], 0, 0)));
  }
  else if (strcmp (cmd, "set_fpga_reset") == 0){
    CHKARGS (1);
    chk_result (usrp_set_fpga_reset (udh, get_on_off (argv[optind])));
  }
  else if (strcmp (cmd, "set_fpga_tx_enable") == 0){
    CHKARGS (1);
    chk_result (usrp_set_fpga_tx_enable (udh, get_on_off (argv[optind])));
  }
  else if (strcmp (cmd, "set_fpga_rx_enable") == 0){
    CHKARGS (1);
    chk_result (usrp_set_fpga_rx_enable (udh, get_on_off (argv[optind])));
  }
  else if (strcmp (cmd, "set_sleep_bits") == 0){
    CHKARGS (2);
    chk_result (usrp_set_sleep_bits (udh,
				     strtol (argv[optind], 0, 0),
				     strtol (argv[optind+1], 0, 0)));
  }
  else if (strcmp (cmd, "load_standard_bits") == 0){
    CHKARGS (0);
    usrp_close_interface (udh);
    udh = 0;
    chk_result (usrp_load_standard_bits (which_board, true));
  }
  else if (strcmp (cmd, "i2c_read") == 0){
    CHKARGS (2);
    int	i2c_addr = strtol (argv[optind], 0, 0);
    int len = strtol (argv[optind + 1], 0, 0);
    if (len < 0)
      chk_result (0);

    unsigned char *buf = new unsigned char [len];
    bool result = usrp_i2c_read (udh, i2c_addr, buf, len);
    if (!result){
      chk_result (0);
    }
    print_hex (stdout, buf, len);
  }
  else if (strcmp (cmd, "i2c_write") == 0){
    CHKARGS (2);
    int	i2c_addr = strtol (argv[optind], 0, 0);
    int	len;
    char *hex_string  = argv[optind + 1];
    unsigned char *buf = hex_string_to_binary (hex_string, &len);
    if (buf == 0)
      chk_result (0);

    bool result = usrp_i2c_write (udh, i2c_addr, buf, len);
    chk_result (result);
  }
  else if (strcmp (cmd, "9862a_write") == 0){
    CHKARGS (2);
    int regno = strtol (argv[optind], 0, 0);
    int value = strtol (argv[optind+1], 0, 0);
    chk_result (usrp_9862_write (udh, 0, regno, value));
  }
  else if (strcmp (cmd, "9862b_write") == 0){
    CHKARGS (2);
    int regno = strtol (argv[optind], 0, 0);
    int value = strtol (argv[optind+1], 0, 0);
    chk_result (usrp_9862_write (udh, 1, regno, value));
  }
  else if (strcmp (cmd, "9862a_read") == 0){
    CHKARGS (1);
    int regno = strtol (argv[optind], 0, 0);
    unsigned char value;
    bool result = usrp_9862_read (udh, 0, regno, &value);
    if (!result){
      chk_result (0);
    }
    fprintf (stdout, "reg[%d] = 0x%02x\n", regno, value);
  }
  else if (strcmp (cmd, "9862b_read") == 0){
    CHKARGS (1);
    int regno = strtol (argv[optind], 0, 0);
    unsigned char value;
    bool result = usrp_9862_read (udh, 1, regno, &value);
    if (!result){
      chk_result (0);
    }
    fprintf (stdout, "reg[%d] = 0x%02x\n", regno, value);
  }
#if 0
  else if (strcmp (cmd, "one_shot") == 0){
    CHKARGS (0);
    chk_result (usrp_one_shot (udh));
  }
  else if (strcmp (cmd, "test_busif") == 0){
    CHKARGS (0);
    chk_result (usrp_test_busif (udh));
  }
  else if (strcmp (cmd, "test_output") == 0){
    CHKARGS (0);
    chk_result (usrp_test_output (udh));
  }
  else if (strcmp (cmd, "test_input") == 0){
    CHKARGS (0);
    chk_result (usrp_test_input (udh, verbose));
  }
#endif
  else {
    usage ();
  }

  if (udh){
    usrp_close_interface (udh);
    udh = 0;
  }

  return 0;
}


#if 0
/*
 * write infinite amount of test data to FPGA
 */

bool 
usrp_test_output (struct usb_dev_handle *udh)
{
  static const int MAX_N = 512 / sizeof (short) * 32;
  unsigned short buf[MAX_N];
  
  // FIXME reset FPGA, enable TX

  int	n = MAX_N;		// if we enumerate as full speed, this should be 64/2
  //int	counter = ~0;
  int	counter = 0;
  int	loop_counter = 0;
  
  while (1){
    loop_counter++;
    
    // fill in buffer with sequence of shorts
    for (int i = 0; i < n; i++){
      buf[i] = counter;
      //counter--;
      counter++;
    }

    int r = usb_bulk_write (udh, USRP_DATAOUT_ENDPOINT, (char *) buf, n * sizeof (short), 1000);
    if (r < 0){
      fprintf (stderr, "usb_bulk_write failed (loop %d): %s\n", loop_counter, usb_strerror ());
      return false;
    }
  }

  return true;			// not reached
}
  
/*
 * read infinite amount of data from FPGA
 */

bool 
usrp_test_input (struct usb_dev_handle *udh, bool write_file)
{
  static const int MAX_N = 512 / sizeof (short) * 32;
  unsigned short buf[MAX_N];
  
  FILE *fp = 0;
  char *filename = "test_input.dat";
  
  if (write_file){
    fp = fopen (filename, "wb");
    if (fp == 0)
      perror (filename);
  }

  usrp_set_fpga_rx_enable (udh, false);
  usrp_set_fpga_tx_enable (udh, false);
  usrp_set_fpga_reset (udh, true);
  usrp_set_fpga_reset (udh, false);

  unsigned int adc_clk_div =   2;
  unsigned int ext_clk_div =  12;
  unsigned int interp_rate = 255;
  unsigned int decim_rate  =   1;
  
  unsigned int	v = ((adc_clk_div << 24) | (ext_clk_div << 16)
		     | (interp_rate << 8) | (decim_rate));

  usrp_write_fpga_reg (udh, 8, v);
  usrp_set_fpga_rx_enable (udh, true);
  

  int	n = MAX_N;		// if we enumerate as full speed, this should be 64/2
  int	loop_counter = 0;
  
  while (1){
    loop_counter++;
    
    int r = usb_bulk_read (udh, USRP_DATAIN_ENDPOINT, (char *) buf,
			   n * sizeof (short), 1000);
    if (r < 0){
      fprintf (stderr, "usb_bulk_read failed (loop %d): %s\n",
	       loop_counter, usb_strerror ());
      return false;
    }

    if (fp)
      fwrite (buf, 1, n * sizeof (short), fp);
  }

  return true;			// not reached
}
#endif

#if 0
/*
 * diagnostic routine.  This requires that the
 * passthru bitsteam is loaded into the fpga.
 *
 * We reset the fpga, disable tx, then send a single packet
 * with known contents.  After waiting for user input,
 * we enable tx, which should send the know data to the dac port
 * which we can watch with the logic analyzer.
 */

bool 
usrp_one_shot (struct usb_dev_handle *udh)
{
  static const int PKTSIZE = 256 * 4;
  short buf[PKTSIZE];
  
  usrp_set_fpga_tx_enable (udh, 0);	// disable tx
  usrp_set_fpga_reset (udh, 1);		// reset fpga, emptying internal fifos
  usrp_set_fpga_reset (udh, 0);		// re-enable fpga

  
  int	n = 0;
  for (int i = 0; i < PKTSIZE / 2; i++){
    int		v;

    v = (i + 0x200) << 2;

    buf[n+0] = v;
    buf[n+1] = 0;
    n += 2;
  }

  int r = usb_bulk_write (udh, USRP_DATAOUT_ENDPOINT,
			  (char *) buf, sizeof (buf), 1000);
  if (r < 0){
    fprintf (stderr, "usb_bulk_write failed: %s\n", usb_strerror ());
    return false;
  }

#if 0
  fprintf (stdout, "Press enter to send packet: ");
  fflush (stdout);
  fgetc (stdin);
#endif

  usrp_set_fpga_tx_enable (udh, 1);

  return true;
}

bool 
usrp_test_busif (struct usb_dev_handle *udh)
{
  static const int PKTSIZE = 256 * 4;
  short buf[PKTSIZE];
  
  int	n = 0;
  for (int i = 0; i < PKTSIZE / 2; i++){
    int		v;

    v = (i + 0x200) << 2;

    buf[n+0] = v;
    buf[n+1] = 0;
    n += 2;
  }


  usrp_one_shot (udh);	// launch first burst

  while (1){

    int r = usb_bulk_write (udh, USRP_DATAOUT_ENDPOINT,
			    (char *) buf, sizeof (buf), 1000);
    if (r < 0){
      fprintf (stderr, "usb_bulk_write failed: %s\n", usb_strerror ());
      return false;
    }
  }

  return true;
}

#endif