3 * Copyright 2003,2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio 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, or (at your option)
12 * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
31 #include <usb.h> /* needed for usb functions */
35 #include "time_stuff.h"
36 #include "usrp_standard.h"
37 #include "usrp_bytesex.h"
38 #include "fpga_regs_common.h"
39 #include "fpga_regs_standard.h"
47 static bool test_input (usrp_standard_rx *urx, int max_bytes, FILE *fp);
50 set_progname (char *path)
52 char *p = strrchr (path, '/');
62 fprintf (stderr, "usage: %s [-f] [-v] [-l] [-c] [-D <decim>] [-F freq] [-o output_file]\n", prog_name);
63 fprintf (stderr, " [-f] loop forever\n");
64 fprintf (stderr, " [-M] how many Megabytes to transfer (default 128)\n");
65 fprintf (stderr, " [-v] verbose\n");
66 fprintf (stderr, " [-l] digital loopback in FPGA\n");
67 fprintf (stderr, " [-c] counting in FPGA\n");
68 fprintf (stderr, " [-8] 8-bit samples across USB\n");
69 fprintf (stderr, " [-B <fusb_block_size>] set fast usb block_size\n");
70 fprintf (stderr, " [-N <fusb_nblocks>] set fast usb nblocks\n");
71 fprintf (stderr, " [-R] set real time scheduling: SCHED_FIFO; pri = midpoint\n");
79 fprintf (stderr, "die: %s: %s\n", prog_name, msg);
84 main (int argc, char **argv)
86 bool verbose_p = false;
87 bool loopback_p = false;
88 bool counting_p = false;
89 bool width_8_p = false;
90 int max_bytes = 128 * (1L << 20);
92 char *output_filename = 0;
94 int decim = 8; // 32 MB/sec
95 double center_freq = 0;
96 int fusb_block_size = 0;
98 bool realtime_p = false;
101 set_progname (argv[0]);
103 while ((ch = getopt (argc, argv, "fvlco:D:F:M:8B:N:R")) != EOF){
126 output_filename = optarg;
130 decim = strtol (optarg, 0, 0);
134 center_freq = strtod (optarg, 0);
138 max_bytes = strtol (optarg, 0, 0) * (1L << 20);
139 if (max_bytes < 0) max_bytes = 0;
143 fusb_block_size = strtol (optarg, 0, 0);
147 fusb_nblocks = strtol (optarg, 0, 0);
159 #ifdef HAVE_SCHED_SETSCHEDULER
161 int policy = SCHED_FIFO;
162 int pri = (sched_get_priority_max (policy) - sched_get_priority_min (policy)) / 2;
163 int pid = 0; // this process
165 struct sched_param param;
166 memset(¶m, 0, sizeof(param));
167 param.sched_priority = pri;
168 int result = sched_setscheduler(pid, policy, ¶m);
170 perror ("sched_setscheduler: failed to set real time priority");
173 printf("SCHED_FIFO enabled with priority = %d\n", pri);
179 if (output_filename){
180 fp = fopen (output_filename, "wb");
182 perror (output_filename);
187 mode |= usrp_standard_rx::FPGA_MODE_LOOPBACK;
189 mode |= usrp_standard_rx::FPGA_MODE_COUNTING;
192 usrp_standard_rx *urx =
193 usrp_standard_rx::make (which_board, decim, 1, -1, mode,
194 fusb_block_size, fusb_nblocks);
197 die ("usrp_standard_rx::make");
199 if (!urx->set_rx_freq (0, center_freq))
200 die ("urx->set_rx_freq");
206 if (!urx->set_format(usrp_standard_rx::make_format(width, shift, want_q)))
207 die("urx->set_format");
210 urx->start(); // start data xfers
212 test_input (urx, max_bytes, fp);
224 test_input (usrp_standard_rx *urx, int max_bytes, FILE *fp)
227 static const int BUFSIZE = urx->block_size();
228 static const int N = BUFSIZE/sizeof (short);
232 double start_wall_time = get_elapsed_time ();
233 double start_cpu_time = get_cpu_usage ();
241 for (nbytes = 0; max_bytes == 0 || nbytes < max_bytes; nbytes += BUFSIZE){
243 unsigned int ret = urx->read (buf, sizeof (buf), &overrun);
244 if (ret != sizeof (buf)){
245 fprintf (stderr, "test_input: error, ret = %d\n", ret);
249 printf ("rx_overrun\n");
255 for (unsigned int i = 0; i < sizeof (buf) / sizeof (short); i++)
256 buf[i] = usrp_to_host_short (buf[i]);
258 if (write (fd, buf, sizeof (buf)) == -1){
265 double stop_wall_time = get_elapsed_time ();
266 double stop_cpu_time = get_cpu_usage ();
268 double delta_wall = stop_wall_time - start_wall_time;
269 double delta_cpu = stop_cpu_time - start_cpu_time;
271 printf ("xfered %.3g bytes in %.3g seconds. %.4g bytes/sec. cpu time = %.4g\n",
272 (double) max_bytes, delta_wall, max_bytes / delta_wall, delta_cpu);
273 printf ("noverruns = %d\n", noverruns);