3 * Copyright 2005,2008,2009 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.
34 #include <boost/scoped_ptr.hpp>
35 #include <usrp/usrp_local_sighandler.h>
36 #include <usrp/usrp_standard.h>
37 #include <usrp/usrp_bytesex.h>
45 run_cal(usrp_standard_rx_sptr u, int which_side, int decim, bool verbose_p)
47 static const int BUFSIZE = u->block_size();
48 static const int N = BUFSIZE/sizeof (short);
50 bool cal_done = false;
54 static const double K = 1e-4;
63 u->start(); // start data xfers
66 int ret = u->read (buf, sizeof (buf), &overrun);
67 if (ret != (int) sizeof (buf)){
68 fprintf (stderr, "usrp_cal_dc_offset: error, ret = %d\n", ret);
72 fprintf (stderr, "O");
76 // fputc('.', stderr);
79 static const int MAX = (1L << 30); // 1G
81 for (int i = 0; i < N/2; i++){
82 for (int n = 0; n < 2; n++){
83 integrator[n] = integrator[n] + buf[2*i + n];
84 if (integrator[n] > MAX)
86 else if (integrator[n] < -MAX)
92 for (int n = 0; n < 2; n++){
93 offset[n] = (int) rint(integrator[n] * K);
94 if (offset[n] > 32767)
96 else if (offset[n] < -32767)
98 u->set_adc_offset(which_side * 2 + n, offset[n]);
101 offset[0] = (int) rint(integrator[0] * K);
102 if (offset[0] > 32767)
104 else if (offset[0] < -32767)
106 u->set_adc_offset(which_side * 2 + 0, offset[0]);
107 u->set_adc_offset(which_side * 2 + 1, offset[0]);
111 printf ("%9ld : %6d\t\t%9ld : %6d\n",
112 integrator[0], offset[0], integrator[1], offset[1]);
120 set_progname (char *path)
122 char *p = strrchr (path, '/');
132 fprintf(stderr, "usage: %s [-v] [-w which_side] [-D decim] [-c ddc_freq] [-g gain]\n", prog_name);
133 fprintf(stderr, " [-S fusb_block_size] [-N fusb_nblocks]\n");
138 die (const char *msg)
140 fprintf (stderr, "die: %s: %s\n", prog_name, msg);
145 main (int argc, char **argv)
148 int decim = 128; // 500 kS/sec
149 bool verbose_p = false;
153 int fusb_block_size = 1024;
154 int fusb_nblocks = 4;
155 double pga_gain = 0.0;
157 set_progname(argv[0]);
159 while ((ch = getopt (argc, argv, "vw:D:c:S:N:g:")) != EOF){
163 which_side = strtol (optarg, 0, 0);
164 if (which_side < 0 || which_side > 1)
169 decim = strtol (optarg, 0, 0);
175 ddc_freq = strtod (optarg, 0);
183 fusb_block_size = strtol(optarg, 0, 0);
187 fusb_nblocks = strtol(optarg, 0, 0);
191 pga_gain = strtod (optarg, 0);
200 int mode = usrp_standard_rx::FPGA_MODE_NORMAL;
209 usrp_local_sighandler sigint (SIGINT, usrp_local_sighandler::throw_signal);
212 usrp_local_sighandler sigquit (SIGQUIT, usrp_local_sighandler::throw_signal);
215 usrp_standard_rx_sptr urx =
216 usrp_standard_rx::make(which_board, decim,
217 nchannels, mux, mode,
218 fusb_block_size, fusb_nblocks);
220 die("usrp_standard_rx::make");
224 if (!urx->set_rx_freq(0, ddc_freq))
225 die("urx->set_rx_freq");
227 urx->set_pga(2 * which_side + 0, pga_gain);
228 urx->set_pga(2 * which_side + 1, pga_gain);
230 run_cal(urx, which_side, decim, verbose_p);
232 catch (usrp_signal &sig){
233 fprintf (stderr, "usrp_cal_dc_offset: caught %s\n", sig.name().c_str());
236 fprintf (stderr, "usrp_cal_dc_offset: caught something\n");