3 * Copyright 2005 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 3, 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 <boost/scoped_ptr.hpp>
36 #include "usrp_local_sighandler.h"
37 #include "usrp_standard.h"
38 #include "usrp_bytesex.h"
46 run_cal(usrp_standard_rx *u, int which_side, int decim, bool verbose_p)
48 static const int BUFSIZE = u->block_size();
49 static const int N = BUFSIZE/sizeof (short);
51 bool cal_done = false;
55 static const double K = 1e-4;
64 u->start(); // start data xfers
67 int ret = u->read (buf, sizeof (buf), &overrun);
68 if (ret != (int) sizeof (buf)){
69 fprintf (stderr, "usrp_cal_dc_offset: error, ret = %d\n", ret);
73 fprintf (stderr, "O");
77 // fputc('.', stderr);
80 static const int MAX = (1L << 30); // 1G
82 for (int i = 0; i < N/2; i++){
83 for (int n = 0; n < 2; n++){
84 integrator[n] = integrator[n] + buf[2*i + n];
85 if (integrator[n] > MAX)
87 else if (integrator[n] < -MAX)
93 for (int n = 0; n < 2; n++){
94 offset[n] = (int) rint(integrator[n] * K);
95 if (offset[n] > 32767)
97 else if (offset[n] < -32767)
99 u->set_adc_offset(which_side * 2 + n, offset[n]);
102 offset[0] = (int) rint(integrator[0] * K);
103 if (offset[0] > 32767)
105 else if (offset[0] < -32767)
107 u->set_adc_offset(which_side * 2 + 0, offset[0]);
108 u->set_adc_offset(which_side * 2 + 1, offset[0]);
112 printf ("%9ld : %6d\t\t%9ld : %6d\n",
113 integrator[0], offset[0], integrator[1], offset[1]);
121 set_progname (char *path)
123 char *p = strrchr (path, '/');
133 fprintf(stderr, "usage: %s [-v] [-w which_side] [-D decim] [-c ddc_freq] [-g gain]\n", prog_name);
134 fprintf(stderr, " [-S fusb_block_size] [-N fusb_nblocks]\n");
139 die (const char *msg)
141 fprintf (stderr, "die: %s: %s\n", prog_name, msg);
146 main (int argc, char **argv)
149 int decim = 128; // 500 kS/sec
150 bool verbose_p = false;
154 int fusb_block_size = 1024;
155 int fusb_nblocks = 4;
156 double pga_gain = 0.0;
158 set_progname(argv[0]);
160 while ((ch = getopt (argc, argv, "vw:D:c:S:N:g:")) != EOF){
164 which_side = strtol (optarg, 0, 0);
165 if (which_side < 0 || which_side > 1)
170 decim = strtol (optarg, 0, 0);
176 ddc_freq = strtod (optarg, 0);
184 fusb_block_size = strtol(optarg, 0, 0);
188 fusb_nblocks = strtol(optarg, 0, 0);
192 pga_gain = strtod (optarg, 0);
201 int mode = usrp_standard_rx::FPGA_MODE_NORMAL;
210 usrp_local_sighandler sigint (SIGINT, usrp_local_sighandler::throw_signal);
213 usrp_local_sighandler sigquit (SIGQUIT, usrp_local_sighandler::throw_signal);
216 usrp_standard_rx *urx =
217 usrp_standard_rx::make(which_board, decim,
218 nchannels, mux, mode,
219 fusb_block_size, fusb_nblocks);
221 die("usrp_standard_rx::make");
225 if (!urx->set_rx_freq(0, ddc_freq))
226 die("urx->set_rx_freq");
228 urx->set_pga(2 * which_side + 0, pga_gain);
229 urx->set_pga(2 * which_side + 1, pga_gain);
231 run_cal(urx, which_side, decim, verbose_p);
233 catch (usrp_signal &sig){
234 fprintf (stderr, "usrp_cal_dc_offset: caught %s\n", sig.name().c_str());
237 fprintf (stderr, "usrp_cal_dc_offset: caught something\n");