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
|
/* -*- c++ -*- */
/*
* Copyright 2005,2008 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio 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 3, or (at your option)
* any later version.
*
* GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#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 <math.h>
#include <boost/scoped_ptr.hpp>
#include "usrp_local_sighandler.h"
#include "usrp_standard.h"
#include "usrp_bytesex.h"
char *prog_name;
static void
run_cal(usrp_standard_rx_sptr u, int which_side, int decim, bool verbose_p)
{
static const int BUFSIZE = u->block_size();
static const int N = BUFSIZE/sizeof (short);
short buf[N];
bool cal_done = false;
bool overrun;
int noverruns = 0;
static const double K = 1e-4;
long integrator[2];
int offset[2];
integrator[0] = 0;
integrator[1] = 0;
offset[0] = 0;
offset[1] = 0;
u->start(); // start data xfers
while(!cal_done){
int ret = u->read (buf, sizeof (buf), &overrun);
if (ret != (int) sizeof (buf)){
fprintf (stderr, "usrp_cal_dc_offset: error, ret = %d\n", ret);
continue;
}
if (overrun){
fprintf (stderr, "O");
noverruns++;
}
else {
// fputc('.', stderr);
}
static const int MAX = (1L << 30); // 1G
for (int i = 0; i < N/2; i++){
for (int n = 0; n < 2; n++){
integrator[n] = integrator[n] + buf[2*i + n];
if (integrator[n] > MAX)
integrator[n] = MAX;
else if (integrator[n] < -MAX)
integrator[n] = -MAX;
}
}
#if 1
for (int n = 0; n < 2; n++){
offset[n] = (int) rint(integrator[n] * K);
if (offset[n] > 32767)
offset[n] = 32767;
else if (offset[n] < -32767)
offset[n] = -32767;
u->set_adc_offset(which_side * 2 + n, offset[n]);
}
#else
offset[0] = (int) rint(integrator[0] * K);
if (offset[0] > 32767)
offset[0] = 32767;
else if (offset[0] < -32767)
offset[0] = -32767;
u->set_adc_offset(which_side * 2 + 0, offset[0]);
u->set_adc_offset(which_side * 2 + 1, offset[0]);
#endif
printf ("%9ld : %6d\t\t%9ld : %6d\n",
integrator[0], offset[0], integrator[1], offset[1]);
}
u->stop();
}
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: %s [-v] [-w which_side] [-D decim] [-c ddc_freq] [-g gain]\n", prog_name);
fprintf(stderr, " [-S fusb_block_size] [-N fusb_nblocks]\n");
exit (1);
}
static void
die (const char *msg)
{
fprintf (stderr, "die: %s: %s\n", prog_name, msg);
exit (1);
}
int
main (int argc, char **argv)
{
int ch;
int decim = 128; // 500 kS/sec
bool verbose_p = false;
int which_board = 0;
int which_side = 0;
double ddc_freq = 0;
int fusb_block_size = 1024;
int fusb_nblocks = 4;
double pga_gain = 0.0;
set_progname(argv[0]);
while ((ch = getopt (argc, argv, "vw:D:c:S:N:g:")) != EOF){
switch (ch){
case 'w':
which_side = strtol (optarg, 0, 0);
if (which_side < 0 || which_side > 1)
usage();
break;
case 'D':
decim = strtol (optarg, 0, 0);
if (decim < 1)
usage();
break;
case 'c':
ddc_freq = strtod (optarg, 0);
break;
case 'v':
verbose_p = true;
break;
case 'S':
fusb_block_size = strtol(optarg, 0, 0);
break;
case 'N':
fusb_nblocks = strtol(optarg, 0, 0);
break;
case 'g':
pga_gain = strtod (optarg, 0);
break;
default:
usage ();
}
}
int nchannels = 1;
int mode = usrp_standard_rx::FPGA_MODE_NORMAL;
int mux;
if (which_side == 0)
mux = 0x00000010;
else
mux = 0x00000032;
#ifdef SIGINT
usrp_local_sighandler sigint (SIGINT, usrp_local_sighandler::throw_signal);
#endif
#ifdef SIGQUIT
usrp_local_sighandler sigquit (SIGQUIT, usrp_local_sighandler::throw_signal);
#endif
usrp_standard_rx_sptr urx =
usrp_standard_rx::make(which_board, decim,
nchannels, mux, mode,
fusb_block_size, fusb_nblocks);
if (!urx)
die("usrp_standard_rx::make");
try {
if (!urx->set_rx_freq(0, ddc_freq))
die("urx->set_rx_freq");
urx->set_pga(2 * which_side + 0, pga_gain);
urx->set_pga(2 * which_side + 1, pga_gain);
run_cal(urx, which_side, decim, verbose_p);
}
catch (usrp_signal &sig){
fprintf (stderr, "usrp_cal_dc_offset: caught %s\n", sig.name().c_str());
}
catch(...){
fprintf (stderr, "usrp_cal_dc_offset: caught something\n");
}
}
|