summaryrefslogtreecommitdiff
path: root/gr-usrp/src/usrp1_source_base.cc
blob: ced49e03b418c951dcb940c3ede02619544a399b (plain)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* -*- c++ -*- */
/*
 * Copyright 2004 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 2, 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 <usrp1_source_base.h>
#include <gr_io_signature.h>
#include <usrp_standard.h>
#include <assert.h>

static const int OUTPUT_MULTIPLE_BYTES = 4 * 1024;

usrp1_source_base::usrp1_source_base (const std::string &name,
				      gr_io_signature_sptr output_signature,
				      int which_board,
				      unsigned int decim_rate,
				      int nchan,
				      int mux,
				      int mode,
				      int fusb_block_size,
				      int fusb_nblocks,
				      const std::string fpga_filename,
				      const std::string firmware_filename
				      ) throw (std::runtime_error)
  : gr_sync_block (name,
		   gr_make_io_signature (0, 0, 0),
		   output_signature),
    d_noverruns (0)
{
  d_usrp = usrp_standard_rx::make (which_board, decim_rate,
				   nchan, mux, mode,
				   fusb_block_size,
				   fusb_nblocks,
				   fpga_filename,
				   firmware_filename);
  if (d_usrp == 0)
    throw std::runtime_error ("can't open usrp1");

  // All calls to d_usrp->read must be multiples of 512 bytes.
  // We jack this up to 4k to reduce overhead.

  set_output_multiple (OUTPUT_MULTIPLE_BYTES / output_signature->sizeof_stream_item (0));
}

usrp1_source_base::~usrp1_source_base ()
{
  delete d_usrp;
}

unsigned int
usrp1_source_base::sizeof_basic_sample() const
{
  return usrp_standard_rx::format_width(d_usrp->format()) / 8;
}

bool
usrp1_source_base::start()
{
  return d_usrp->start();
}

bool
usrp1_source_base::stop()
{
  return d_usrp->stop();
}

int
usrp1_source_base::work (int noutput_items,
			 gr_vector_const_void_star &input_items,
			 gr_vector_void_star &output_items)
{
  static const int BUFSIZE = 4 * OUTPUT_MULTIPLE_BYTES;
  unsigned char buf[BUFSIZE];
  int output_index = 0;
  int output_items_produced;
  int bytes_read;
  bool overrun;

  while (output_index < noutput_items){
    int nbytes = ninput_bytes_reqd_for_noutput_items (noutput_items - output_index);
    nbytes = std::min (nbytes, BUFSIZE);

    int result_nbytes = d_usrp->read (buf, nbytes, &overrun);
    if (overrun){
      // fprintf (stderr, "usrp1_source: overrun\n");
      fputs ("uO", stderr);
      d_noverruns++;
    }

    if (result_nbytes < 0)	// We've got a problem.  Usually board unplugged or powered down.
      return -1;		// Indicate we're done.

    if (result_nbytes != nbytes){	// not really an error, but unexpected
      fprintf (stderr, "usrp1_source: short read.  Expected %d, got %d\n",
	       nbytes, result_nbytes);
    }

    copy_from_usrp_buffer (output_items,
			   output_index,
			   noutput_items - output_index,   // output_items_available
			   output_items_produced,	   // [out]
			   buf,				   // usrp_buffer
			   result_nbytes,		   // usrp_buffer_length
			   bytes_read);			   // [out]

    assert (output_index + output_items_produced <= noutput_items);
    assert (bytes_read == result_nbytes);

    output_index += output_items_produced;
  }

  return noutput_items;
}


bool
usrp1_source_base::set_decim_rate (unsigned int rate)
{
  return d_usrp->set_decim_rate (rate);
}

bool
usrp1_source_base::set_nchannels (int nchan)
{
  return d_usrp->set_nchannels (nchan);
}

bool
usrp1_source_base::set_mux (int mux)
{
  return d_usrp->set_mux (mux);
}

bool
usrp1_source_base::set_rx_freq (int channel, double freq)
{
  return d_usrp->set_rx_freq (channel, freq);
}

long
usrp1_source_base::fpga_master_clock_freq() const
{
  return d_usrp->fpga_master_clock_freq();
}

long
usrp1_source_base::converter_rate() const
{
  return d_usrp->converter_rate();
}

unsigned int
usrp1_source_base::decim_rate () const
{
  return d_usrp->decim_rate ();
}

int
usrp1_source_base::nchannels () const
{
  return d_usrp->nchannels ();
}

int
usrp1_source_base::mux () const
{
  return d_usrp->mux ();
}

double
usrp1_source_base::rx_freq (int channel) const
{
  return d_usrp->rx_freq (channel);
}

bool
usrp1_source_base::set_fpga_mode (int mode)
{
  return d_usrp->set_fpga_mode (mode);
}

bool
usrp1_source_base::set_ddc_phase (int channel, int phase)
{
  return d_usrp->set_ddc_phase(channel, phase);
}

bool
usrp1_source_base::set_dc_offset_cl_enable(int bits, int mask)
{
  return d_usrp->set_dc_offset_cl_enable(bits, mask);
}

void
usrp1_source_base::set_verbose (bool verbose)
{  
  d_usrp->set_verbose (verbose);
}

bool
usrp1_source_base::write_aux_dac (int which_dboard, int which_dac, int value)
{
  return d_usrp->write_aux_dac (which_dboard, which_dac, value);
}

int
usrp1_source_base::read_aux_adc (int which_dboard, int which_adc)
{
  return d_usrp->read_aux_adc (which_dboard, which_adc);
}

bool
usrp1_source_base::write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf)
{
  return d_usrp->write_eeprom (i2c_addr, eeprom_offset, buf);
}

std::string
usrp1_source_base::read_eeprom (int i2c_addr, int eeprom_offset, int len)
{
  return d_usrp->read_eeprom (i2c_addr, eeprom_offset, len);
}

bool
usrp1_source_base::write_i2c (int i2c_addr, const std::string buf)
{
  return d_usrp->write_i2c (i2c_addr, buf);
}

std::string
usrp1_source_base::read_i2c (int i2c_addr, int len)
{
  return d_usrp->read_i2c (i2c_addr, len);
}

bool
usrp1_source_base::set_pga (int which, double gain)
{
  return d_usrp->set_pga (which, gain);
}

double
usrp1_source_base::pga (int which) const
{
  return d_usrp->pga (which);
}

double
usrp1_source_base::pga_min () const
{
  return d_usrp->pga_min ();
}

double
usrp1_source_base::pga_max () const
{
  return d_usrp->pga_max ();
}

double
usrp1_source_base::pga_db_per_step () const
{
  return d_usrp->pga_db_per_step ();
}

int
usrp1_source_base::daughterboard_id (int which) const
{
  return d_usrp->daughterboard_id (which);
}


bool
usrp1_source_base::set_adc_offset (int which, int offset)
{
  return d_usrp->set_adc_offset (which, offset);
}

bool
usrp1_source_base::set_dac_offset (int which, int offset, int offset_pin)
{
  return d_usrp->set_dac_offset (which, offset, offset_pin);
}

bool
usrp1_source_base::set_adc_buffer_bypass (int which, bool bypass)
{
  return d_usrp->set_adc_buffer_bypass (which, bypass);
}

std::string
usrp1_source_base::serial_number()
{
  return d_usrp->serial_number();
}

bool
usrp1_source_base::_write_oe (int which_dboard, int value, int mask)
{
  return d_usrp->_write_oe (which_dboard, value, mask);
}

bool
usrp1_source_base::write_io (int which_dboard, int value, int mask)
{
  return d_usrp->write_io (which_dboard, value, mask);
}

int
usrp1_source_base::read_io (int which_dboard)
{
  return d_usrp->read_io (which_dboard);
}




// internal routines...

bool
usrp1_source_base::_write_fpga_reg (int regno, int value)
{
  return d_usrp->_write_fpga_reg (regno, value);
}

bool
usrp1_source_base::_write_fpga_reg_masked (int regno, int value, int mask)
{
  return d_usrp->_write_fpga_reg_masked (regno, value, mask);
}

int
usrp1_source_base::_read_fpga_reg (int regno)
{
  return d_usrp->_read_fpga_reg (regno);
}

bool
usrp1_source_base::_write_9862 (int which_codec, int regno, unsigned char value)
{
  return d_usrp->_write_9862 (which_codec, regno, value);
}

int
usrp1_source_base::_read_9862 (int which_codec, int regno) const
{
  return d_usrp->_read_9862 (which_codec, regno);
}

bool
usrp1_source_base::_write_spi (int optional_header, int enables,
			       int format, std::string buf)
{
  return d_usrp->_write_spi (optional_header, enables, format, buf);
}

std::string
usrp1_source_base::_read_spi (int optional_header, int enables, int format, int len)
{
  return d_usrp->_read_spi (optional_header, enables, format, len);
}

bool
usrp1_source_base::set_format(unsigned int format)
{
  return d_usrp->set_format(format);
}

unsigned int
usrp1_source_base::format() const
{
  return d_usrp->format();
}

unsigned int
usrp1_source_base::make_format(int width, int shift, bool want_q, bool bypass_halfband)
{
  return usrp_standard_rx::make_format(width, shift, want_q, bypass_halfband);
}

int
usrp1_source_base::format_width(unsigned int format)
{
  return usrp_standard_rx::format_width(format);
}

int
usrp1_source_base::format_shift(unsigned int format)
{
  return usrp_standard_rx::format_shift(format);
}

bool
usrp1_source_base::format_want_q(unsigned int format)
{
  return usrp_standard_rx::format_want_q(format);
}

bool
usrp1_source_base::format_bypass_halfband(unsigned int format)
{
  return usrp_standard_rx::format_bypass_halfband(format);
}