Imported Upstream version 3.2.2
[debian/gnuradio] / usrp / host / apps / test_usrp_standard_rx.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2003,2006,2008 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
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 3, or (at your option)
10  * any later version.
11  * 
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.
16  * 
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <usb.h>                        /* needed for usb functions */
32 #include <getopt.h>
33 #include <assert.h>
34 #include <math.h>
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"
40
41 #ifdef HAVE_SCHED_H
42 #include <sched.h>
43 #endif
44
45 char *prog_name;
46
47 static bool test_input  (usrp_standard_rx_sptr urx, int max_bytes, FILE *fp);
48
49 static void
50 set_progname (char *path)
51 {
52   char *p = strrchr (path, '/');
53   if (p != 0)
54     prog_name = p+1;
55   else
56     prog_name = path;
57 }
58
59 static void
60 usage ()
61 {
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");
72
73   exit (1);
74 }
75
76 static void
77 die (const char *msg)
78 {
79   fprintf (stderr, "die: %s: %s\n", prog_name, msg);
80   exit (1);
81 }
82
83 int
84 main (int argc, char **argv)
85 {
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);
91   int   ch;
92   char  *output_filename = 0;
93   int   which_board = 0;
94   int   decim = 8;                      // 32 MB/sec
95   double        center_freq = 0;
96   int   fusb_block_size = 0;
97   int   fusb_nblocks = 0;
98   bool  realtime_p = false;
99
100
101   set_progname (argv[0]);
102
103   while ((ch = getopt (argc, argv, "fvlco:D:F:M:8B:N:R")) != EOF){
104     switch (ch){
105     case 'f':
106       max_bytes = 0;
107       break;
108
109     case 'v':
110       verbose_p = true;
111       break;
112
113     case 'l':
114       loopback_p = true;
115       break;
116
117     case 'c':
118       counting_p = true;
119       break;
120       
121     case '8':
122       width_8_p = true;
123       break;
124       
125     case 'o':
126       output_filename = optarg;
127       break;
128       
129     case 'D':
130       decim = strtol (optarg, 0, 0);
131       break;
132
133     case 'F':
134       center_freq = strtod (optarg, 0);
135       break;
136
137     case 'M':
138       max_bytes = strtol (optarg, 0, 0) * (1L << 20);
139       if (max_bytes < 0) max_bytes = 0;
140       break;
141
142     case 'B':
143       fusb_block_size = strtol (optarg, 0, 0);
144       break;
145
146     case 'N':
147       fusb_nblocks = strtol (optarg, 0, 0);
148       break;
149
150     case 'R':
151       realtime_p = true;
152       break;
153
154     default:
155       usage ();
156     }
157   }
158
159 #ifdef HAVE_SCHED_SETSCHEDULER
160   if (realtime_p){
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
164
165     struct sched_param param;
166     memset(&param, 0, sizeof(param));
167     param.sched_priority = pri;
168     int result = sched_setscheduler(pid, policy, &param);
169     if (result != 0){
170       perror ("sched_setscheduler: failed to set real time priority");
171     }
172     else
173       printf("SCHED_FIFO enabled with priority = %d\n", pri);
174   }
175 #endif
176
177   FILE *fp = 0;
178
179   if (output_filename){
180     fp = fopen (output_filename, "wb");
181     if (fp == 0)
182       perror (output_filename);
183   }
184       
185   int mode = 0;
186   if (loopback_p)
187     mode |= usrp_standard_rx::FPGA_MODE_LOOPBACK;
188   if (counting_p)
189     mode |= usrp_standard_rx::FPGA_MODE_COUNTING;
190
191
192   usrp_standard_rx_sptr urx = 
193     usrp_standard_rx::make (which_board, decim, 1, -1, mode,
194                             fusb_block_size, fusb_nblocks);
195
196   if (urx == 0)
197     die ("usrp_standard_rx::make");
198
199   if (!urx->set_rx_freq (0, center_freq))
200     die ("urx->set_rx_freq");
201     
202   if (width_8_p){
203     int width = 8;
204     int shift = 8;
205     bool want_q = true;
206     if (!urx->set_format(usrp_standard_rx::make_format(width, shift, want_q)))
207       die("urx->set_format");
208   }
209
210   urx->start();         // start data xfers
211
212   test_input (urx, max_bytes, fp);
213
214   if (fp)
215     fclose (fp);
216
217   return 0;
218 }
219
220
221 static bool
222 test_input  (usrp_standard_rx_sptr urx, int max_bytes, FILE *fp)
223 {
224   int              fd = -1;
225   static const int BUFSIZE = urx->block_size();
226   static const int N = BUFSIZE/sizeof (short);
227   short            buf[N];
228   int              nbytes = 0;
229
230   double           start_wall_time = get_elapsed_time ();
231   double           start_cpu_time  = get_cpu_usage ();
232
233   if (fp)
234     fd = fileno (fp);
235   
236   bool overrun;
237   int noverruns = 0;
238
239   for (nbytes = 0; max_bytes == 0 || nbytes < max_bytes; nbytes += BUFSIZE){
240
241     unsigned int        ret = urx->read (buf, sizeof (buf), &overrun);
242     if (ret != sizeof (buf)){
243       fprintf (stderr, "test_input: error, ret = %d\n", ret);
244     }
245
246     if (overrun){
247       printf ("rx_overrun\n");
248       noverruns++;
249     }
250     
251     if (fd != -1){
252
253       for (unsigned int i = 0; i < sizeof (buf) / sizeof (short); i++)
254         buf[i] = usrp_to_host_short (buf[i]);
255
256       if (write (fd, buf, sizeof (buf)) == -1){
257         perror ("write");
258         fd = -1;
259       }
260     }
261   }
262
263   double stop_wall_time = get_elapsed_time ();
264   double stop_cpu_time  = get_cpu_usage ();
265
266   double delta_wall = stop_wall_time - start_wall_time;
267   double delta_cpu  = stop_cpu_time  - start_cpu_time;
268
269   printf ("xfered %.3g bytes in %.3g seconds.  %.4g bytes/sec.  cpu time = %.4g\n",
270           (double) max_bytes, delta_wall, max_bytes / delta_wall, delta_cpu);
271   printf ("noverruns = %d\n", noverruns);
272
273   return true;
274 }