~ubuntu-branches/ubuntu/trusty/gnuradio/trusty

« back to all changes in this revision

Viewing changes to usrp/host/lib/legacy/usrp_basic.cc

  • Committer: Bazaar Package Importer
  • Author(s): Kamal Mostafa
  • Date: 2010-03-13 07:46:01 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100313074601-zjsa893a87bozyh7
Tags: 3.2.2.dfsg-1ubuntu1
* Fix build for Ubuntu lucid (LP: #260406)
  - add binary package dep for libusrp0, libusrp2-0: adduser
  - debian/rules clean: remove pre-built Qt moc files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- c++ -*- */
 
2
/*
 
3
 * Copyright 2003,2004,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 "usrp_basic.h"
 
28
#include "usrp_prims.h"
 
29
#include "usrp_interfaces.h"
 
30
#include "fpga_regs_common.h"
 
31
#include "fpga_regs_standard.h"
 
32
#include "fusb.h"
 
33
#include "db_boards.h"
 
34
#include <usb.h>
 
35
#include <stdexcept>
 
36
#include <assert.h>
 
37
#include <math.h>
 
38
#include <ad9862.h>
 
39
#include <string.h>
 
40
#include <cstdio>
 
41
 
 
42
using namespace ad9862;
 
43
 
 
44
#define NELEM(x) (sizeof (x) / sizeof (x[0]))
 
45
 
 
46
// These set the buffer size used for each end point using the fast
 
47
// usb interface.  The kernel ends up locking down this much memory.
 
48
 
 
49
static const int FUSB_BUFFER_SIZE = fusb_sysconfig::default_buffer_size();
 
50
static const int FUSB_BLOCK_SIZE = fusb_sysconfig::max_block_size();
 
51
static const int FUSB_NBLOCKS    = FUSB_BUFFER_SIZE / FUSB_BLOCK_SIZE;
 
52
 
 
53
 
 
54
static const double POLLING_INTERVAL = 0.1;     // seconds
 
55
 
 
56
////////////////////////////////////////////////////////////////
 
57
 
 
58
static struct usb_dev_handle *
 
59
open_rx_interface (struct usb_device *dev)
 
60
{
 
61
  struct usb_dev_handle *udh = usrp_open_rx_interface (dev);
 
62
  if (udh == 0){
 
63
    fprintf (stderr, "usrp_basic_rx: can't open rx interface\n");
 
64
    usb_strerror ();
 
65
  }
 
66
  return udh;
 
67
}
 
68
 
 
69
static struct usb_dev_handle *
 
70
open_tx_interface (struct usb_device *dev)
 
71
{
 
72
  struct usb_dev_handle *udh = usrp_open_tx_interface (dev);
 
73
  if (udh == 0){
 
74
    fprintf (stderr, "usrp_basic_tx: can't open tx interface\n");
 
75
    usb_strerror ();
 
76
  }
 
77
  return udh;
 
78
}
 
79
 
 
80
 
 
81
//////////////////////////////////////////////////////////////////
 
82
//
 
83
//                      usrp_basic
 
84
//
 
85
////////////////////////////////////////////////////////////////
 
86
 
 
87
 
 
88
// Given:
 
89
//   CLKIN = 64 MHz
 
90
//   CLKSEL pin = high 
 
91
//
 
92
// These settings give us:
 
93
//   CLKOUT1 = CLKIN = 64 MHz
 
94
//   CLKOUT2 = CLKIN = 64 MHz
 
95
//   ADC is clocked at  64 MHz
 
96
//   DAC is clocked at 128 MHz
 
97
 
 
98
static unsigned char common_regs[] = {
 
99
  REG_GENERAL,          0,
 
100
  REG_DLL,              (DLL_DISABLE_INTERNAL_XTAL_OSC
 
101
                         | DLL_MULT_2X
 
102
                         | DLL_FAST),
 
103
  REG_CLKOUT,           CLKOUT2_EQ_DLL_OVER_2,
 
104
  REG_AUX_ADC_CLK,      AUX_ADC_CLK_CLK_OVER_4
 
105
};
 
106
 
 
107
 
 
108
usrp_basic::usrp_basic (int which_board, 
 
109
                        struct usb_dev_handle *
 
110
                        open_interface (struct usb_device *dev),
 
111
                        const std::string fpga_filename,
 
112
                        const std::string firmware_filename)
 
113
  : d_udh (0),
 
114
    d_usb_data_rate (16000000), // SWAG, see below
 
115
    d_bytes_per_poll ((int) (POLLING_INTERVAL * d_usb_data_rate)),
 
116
    d_verbose (false), d_fpga_master_clock_freq(64000000), d_db(2)
 
117
{
 
118
  /*
 
119
   * SWAG: Scientific Wild Ass Guess.
 
120
   *
 
121
   * d_usb_data_rate is used only to determine how often to poll for over- and under-runs.
 
122
   * We defualt it to 1/2  of our best case.  Classes derived from usrp_basic (e.g., 
 
123
   * usrp_standard_tx and usrp_standard_rx) call set_usb_data_rate() to tell us the
 
124
   * actual rate.  This doesn't change our throughput, that's determined by the signal
 
125
   * processing code in the FPGA (which we know nothing about), and the system limits
 
126
   * determined by libusb, fusb_*, and the underlying drivers.
 
127
   */
 
128
  memset (d_fpga_shadows, 0, sizeof (d_fpga_shadows));
 
129
 
 
130
  usrp_one_time_init ();
 
131
 
 
132
  if (!usrp_load_standard_bits (which_board, false, fpga_filename, firmware_filename))
 
133
    throw std::runtime_error ("usrp_basic/usrp_load_standard_bits");
 
134
 
 
135
  struct usb_device *dev = usrp_find_device (which_board);
 
136
  if (dev == 0){
 
137
    fprintf (stderr, "usrp_basic: can't find usrp[%d]\n", which_board);
 
138
    throw std::runtime_error ("usrp_basic/usrp_find_device");
 
139
  }
 
140
 
 
141
  if (!(usrp_usrp_p(dev) && usrp_hw_rev(dev) >= 1)){
 
142
    fprintf (stderr, "usrp_basic: sorry, this code only works with USRP revs >= 1\n");
 
143
    throw std::runtime_error ("usrp_basic/bad_rev");
 
144
  }
 
145
 
 
146
  if ((d_udh = open_interface (dev)) == 0)
 
147
    throw std::runtime_error ("usrp_basic/open_interface");
 
148
 
 
149
  // initialize registers that are common to rx and tx
 
150
 
 
151
  if (!usrp_9862_write_many_all (d_udh, common_regs, sizeof (common_regs))){
 
152
    fprintf (stderr, "usrp_basic: failed to init common AD9862 regs\n");
 
153
    throw std::runtime_error ("usrp_basic/init_9862");
 
154
  }
 
155
 
 
156
  _write_fpga_reg (FR_MODE, 0);         // ensure we're in normal mode
 
157
  _write_fpga_reg (FR_DEBUG_EN, 0);     // disable debug outputs
 
158
}
 
159
 
 
160
void
 
161
usrp_basic::shutdown_daughterboards()
 
162
{
 
163
  // nuke d'boards before we close down USB in ~usrp_basic
 
164
  // shutdown() will do any board shutdown while the USRP can still
 
165
  // be talked to
 
166
  for(size_t i = 0; i < d_db.size(); i++) 
 
167
    for(size_t j = 0; j < d_db[i].size(); j++) 
 
168
      d_db[i][j]->shutdown();
 
169
}
 
170
 
 
171
usrp_basic::~usrp_basic ()
 
172
{
 
173
  // shutdown_daughterboards();         // call from ~usrp_basic_{tx,rx}
 
174
 
 
175
  d_db.resize(0); // forget db shared ptrs
 
176
 
 
177
  if (d_udh)
 
178
    usb_close (d_udh);
 
179
}
 
180
 
 
181
void
 
182
usrp_basic::init_db(usrp_basic_sptr u)
 
183
{
 
184
  if (u.get() != this)
 
185
    throw std::invalid_argument("u is not this");
 
186
 
 
187
  d_db[0] = instantiate_dbs(d_dbid[0], u, 0);
 
188
  d_db[1] = instantiate_dbs(d_dbid[1], u, 1);
 
189
}
 
190
 
 
191
std::vector<db_base_sptr> 
 
192
usrp_basic::db(int which_side)
 
193
{
 
194
  which_side &= 0x1;    // clamp it to avoid any reporting any errors
 
195
  return d_db[which_side];
 
196
}
 
197
 
 
198
bool
 
199
usrp_basic::is_valid(const usrp_subdev_spec &ss)
 
200
{
 
201
  if (ss.side < 0 || ss.side > 1)
 
202
    return false;
 
203
 
 
204
  if (ss.subdev < 0 || ss.subdev >= d_db[ss.side].size())
 
205
    return false;
 
206
 
 
207
  return true;
 
208
}
 
209
 
 
210
db_base_sptr
 
211
usrp_basic::selected_subdev(const usrp_subdev_spec &ss)
 
212
{
 
213
  if (!is_valid(ss))
 
214
    throw std::invalid_argument("invalid subdev_spec");
 
215
 
 
216
  return d_db[ss.side][ss.subdev];
 
217
}
 
218
 
 
219
bool
 
220
usrp_basic::start ()
 
221
{
 
222
  return true;          // nop
 
223
}
 
224
 
 
225
bool
 
226
usrp_basic::stop ()
 
227
{
 
228
  return true;          // nop
 
229
}
 
230
 
 
231
void
 
232
usrp_basic::set_usb_data_rate (int usb_data_rate)
 
233
{
 
234
  d_usb_data_rate = usb_data_rate;
 
235
  d_bytes_per_poll = (int) (usb_data_rate * POLLING_INTERVAL);
 
236
}
 
237
 
 
238
bool
 
239
usrp_basic::_write_aux_dac (int slot, int which_dac, int value)
 
240
{
 
241
  return usrp_write_aux_dac (d_udh, slot, which_dac, value);
 
242
}
 
243
 
 
244
bool
 
245
usrp_basic::_read_aux_adc (int slot, int which_adc, int *value)
 
246
{
 
247
  return usrp_read_aux_adc (d_udh, slot, which_adc, value);
 
248
}
 
249
 
 
250
int
 
251
usrp_basic::_read_aux_adc (int slot, int which_adc)
 
252
{
 
253
  int   value;
 
254
  if (!_read_aux_adc (slot, which_adc, &value))
 
255
    return READ_FAILED;
 
256
 
 
257
  return value;
 
258
}
 
259
 
 
260
bool
 
261
usrp_basic::write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf)
 
262
{
 
263
  return usrp_eeprom_write (d_udh, i2c_addr, eeprom_offset, buf.data (), buf.size ());
 
264
}
 
265
 
 
266
std::string
 
267
usrp_basic::read_eeprom (int i2c_addr, int eeprom_offset, int len)
 
268
{
 
269
  if (len <= 0)
 
270
    return "";
 
271
 
 
272
  char buf[len];
 
273
 
 
274
  if (!usrp_eeprom_read (d_udh, i2c_addr, eeprom_offset, buf, len))
 
275
    return "";
 
276
 
 
277
  return std::string (buf, len);
 
278
}
 
279
 
 
280
bool
 
281
usrp_basic::write_i2c (int i2c_addr, const std::string buf)
 
282
{
 
283
  return usrp_i2c_write (d_udh, i2c_addr, buf.data (), buf.size ());
 
284
}
 
285
 
 
286
std::string
 
287
usrp_basic::read_i2c (int i2c_addr, int len)
 
288
{
 
289
  if (len <= 0)
 
290
    return "";
 
291
 
 
292
  char buf[len];
 
293
 
 
294
  if (!usrp_i2c_read (d_udh, i2c_addr, buf, len))
 
295
    return "";
 
296
 
 
297
  return std::string (buf, len);
 
298
}
 
299
 
 
300
std::string
 
301
usrp_basic::serial_number()
 
302
{
 
303
  return usrp_serial_number(d_udh);
 
304
}
 
305
 
 
306
// ----------------------------------------------------------------
 
307
 
 
308
bool
 
309
usrp_basic::set_adc_offset (int which_adc, int offset)
 
310
{
 
311
  if (which_adc < 0 || which_adc > 3)
 
312
    return false;
 
313
 
 
314
  return _write_fpga_reg (FR_ADC_OFFSET_0 + which_adc, offset);
 
315
}
 
316
 
 
317
bool
 
318
usrp_basic::set_dac_offset (int which_dac, int offset, int offset_pin)
 
319
{
 
320
  if (which_dac < 0 || which_dac > 3)
 
321
    return false;
 
322
 
 
323
  int which_codec = which_dac >> 1;
 
324
  int tx_a = (which_dac & 0x1) == 0;
 
325
  int lo = ((offset & 0x3) << 6) | (offset_pin & 0x1);
 
326
  int hi = (offset >> 2);
 
327
  bool ok;
 
328
 
 
329
  if (tx_a){
 
330
    ok =  _write_9862 (which_codec, REG_TX_A_OFFSET_LO, lo);
 
331
    ok &= _write_9862 (which_codec, REG_TX_A_OFFSET_HI, hi);
 
332
  }
 
333
  else {
 
334
    ok =  _write_9862 (which_codec, REG_TX_B_OFFSET_LO, lo);
 
335
    ok &= _write_9862 (which_codec, REG_TX_B_OFFSET_HI, hi);
 
336
  }
 
337
  return ok;
 
338
}
 
339
 
 
340
bool
 
341
usrp_basic::set_adc_buffer_bypass (int which_adc, bool bypass)
 
342
{
 
343
  if (which_adc < 0 || which_adc > 3)
 
344
    return false;
 
345
 
 
346
  int codec = which_adc >> 1;
 
347
  int reg = (which_adc & 1) == 0 ? REG_RX_A : REG_RX_B;
 
348
 
 
349
  unsigned char cur_rx;
 
350
  unsigned char cur_pwr_dn;
 
351
 
 
352
  // If the input buffer is bypassed, we need to power it down too.
 
353
 
 
354
  bool ok = _read_9862 (codec, reg, &cur_rx);
 
355
  ok &= _read_9862 (codec, REG_RX_PWR_DN, &cur_pwr_dn);
 
356
  if (!ok)
 
357
    return false;
 
358
 
 
359
  if (bypass){
 
360
    cur_rx |= RX_X_BYPASS_INPUT_BUFFER;
 
361
    cur_pwr_dn |= ((which_adc & 1) == 0) ? RX_PWR_DN_BUF_A : RX_PWR_DN_BUF_B;
 
362
  }
 
363
  else {
 
364
    cur_rx &= ~RX_X_BYPASS_INPUT_BUFFER;
 
365
    cur_pwr_dn &= ~(((which_adc & 1) == 0) ? RX_PWR_DN_BUF_A : RX_PWR_DN_BUF_B);
 
366
  }
 
367
 
 
368
  ok &= _write_9862 (codec, reg, cur_rx);
 
369
  ok &= _write_9862 (codec, REG_RX_PWR_DN, cur_pwr_dn);
 
370
  return ok;
 
371
}
 
372
 
 
373
bool
 
374
usrp_basic::set_dc_offset_cl_enable(int bits, int mask)
 
375
{
 
376
  return _write_fpga_reg(FR_DC_OFFSET_CL_EN, 
 
377
                         (d_fpga_shadows[FR_DC_OFFSET_CL_EN] & ~mask) | (bits & mask));
 
378
}
 
379
 
 
380
// ----------------------------------------------------------------
 
381
 
 
382
bool
 
383
usrp_basic::_write_fpga_reg (int regno, int value)
 
384
{
 
385
  if (d_verbose){
 
386
    fprintf (stdout, "_write_fpga_reg(%3d, 0x%08x)\n", regno, value);
 
387
    fflush (stdout);
 
388
  }
 
389
 
 
390
  if (regno >= 0 && regno < MAX_REGS)
 
391
    d_fpga_shadows[regno] = value;
 
392
 
 
393
  return usrp_write_fpga_reg (d_udh, regno, value);
 
394
}
 
395
 
 
396
bool
 
397
usrp_basic::_write_fpga_reg_masked (int regno, int value, int mask)
 
398
{
 
399
  //Only use this for registers who actually use a mask in the verilog firmware, like FR_RX_MASTER_SLAVE
 
400
  //value is a 16 bits value and mask is a 16 bits mask
 
401
  if (d_verbose){
 
402
    fprintf (stdout, "_write_fpga_reg_masked(%3d, 0x%04x,0x%04x)\n", regno, value, mask);
 
403
    fflush (stdout);
 
404
  }
 
405
 
 
406
  if (regno >= 0 && regno < MAX_REGS)
 
407
    d_fpga_shadows[regno] = value;
 
408
 
 
409
  return usrp_write_fpga_reg (d_udh, regno, (value & 0xffff) | ((mask & 0xffff)<<16));
 
410
}
 
411
 
 
412
 
 
413
bool
 
414
usrp_basic::_read_fpga_reg (int regno, int *value)
 
415
{
 
416
  return usrp_read_fpga_reg (d_udh, regno, value);
 
417
}
 
418
 
 
419
int
 
420
usrp_basic::_read_fpga_reg (int regno)
 
421
{
 
422
  int value;
 
423
  if (!_read_fpga_reg (regno, &value))
 
424
    return READ_FAILED;
 
425
  return value;
 
426
}
 
427
 
 
428
bool
 
429
usrp_basic::_write_9862 (int which_codec, int regno, unsigned char value)
 
430
{
 
431
  if (0 && d_verbose){
 
432
    // FIXME really want to enable logging in usrp_prims:usrp_9862_write
 
433
    fprintf(stdout, "_write_9862(codec = %d, regno = %2d, val = 0x%02x)\n", which_codec, regno, value);
 
434
    fflush(stdout);
 
435
  }
 
436
 
 
437
  return usrp_9862_write (d_udh, which_codec, regno, value);
 
438
}
 
439
 
 
440
 
 
441
bool
 
442
usrp_basic::_read_9862 (int which_codec, int regno, unsigned char *value) const
 
443
{
 
444
  return usrp_9862_read (d_udh, which_codec, regno, value);
 
445
}
 
446
 
 
447
int
 
448
usrp_basic::_read_9862 (int which_codec, int regno) const
 
449
{
 
450
  unsigned char value;
 
451
  if (!_read_9862 (which_codec, regno, &value))
 
452
    return READ_FAILED;
 
453
  return value;
 
454
}
 
455
 
 
456
bool
 
457
usrp_basic::_write_spi (int optional_header, int enables, int format, std::string buf)
 
458
{
 
459
  return usrp_spi_write (d_udh, optional_header, enables, format,
 
460
                         buf.data(), buf.size());
 
461
}
 
462
 
 
463
std::string
 
464
usrp_basic::_read_spi (int optional_header, int enables, int format, int len)
 
465
{
 
466
  if (len <= 0)
 
467
    return "";
 
468
  
 
469
  char buf[len];
 
470
 
 
471
  if (!usrp_spi_read (d_udh, optional_header, enables, format, buf, len))
 
472
    return "";
 
473
 
 
474
  return std::string (buf, len);
 
475
}
 
476
 
 
477
 
 
478
bool
 
479
usrp_basic::_set_led (int which_led, bool on)
 
480
{
 
481
  return usrp_set_led (d_udh, which_led, on);
 
482
}
 
483
 
 
484
bool
 
485
usrp_basic::write_atr_tx_delay(int value)
 
486
{
 
487
  return _write_fpga_reg(FR_ATR_TX_DELAY, value);
 
488
}
 
489
 
 
490
bool
 
491
usrp_basic::write_atr_rx_delay(int value)
 
492
{
 
493
  return _write_fpga_reg(FR_ATR_RX_DELAY, value);
 
494
}
 
495
 
 
496
/*
 
497
 * ----------------------------------------------------------------
 
498
 * Routines to access and control daughterboard specific i/o
 
499
 * ----------------------------------------------------------------
 
500
 */
 
501
static int
 
502
slot_id_to_oe_reg (int slot_id)
 
503
{
 
504
  static int reg[4]  = { FR_OE_0, FR_OE_1, FR_OE_2, FR_OE_3 };
 
505
  assert (0 <= slot_id && slot_id < 4);
 
506
  return reg[slot_id];
 
507
}
 
508
 
 
509
static int
 
510
slot_id_to_io_reg (int slot_id)
 
511
{
 
512
  static int reg[4]  = { FR_IO_0, FR_IO_1, FR_IO_2, FR_IO_3 };
 
513
  assert (0 <= slot_id && slot_id < 4);
 
514
  return reg[slot_id];
 
515
}
 
516
 
 
517
static int
 
518
slot_id_to_refclk_reg(int slot_id)
 
519
{
 
520
  static int reg[4]  = { FR_TX_A_REFCLK, FR_RX_A_REFCLK, FR_TX_B_REFCLK, FR_RX_B_REFCLK };
 
521
  assert (0 <= slot_id && slot_id < 4);
 
522
  return reg[slot_id];
 
523
}
 
524
 
 
525
static int
 
526
slot_id_to_atr_mask_reg(int slot_id)
 
527
{
 
528
  static int reg[4]  = { FR_ATR_MASK_0, FR_ATR_MASK_1, FR_ATR_MASK_2, FR_ATR_MASK_3 };
 
529
  assert (0 <= slot_id && slot_id < 4);
 
530
  return reg[slot_id];
 
531
}
 
532
 
 
533
static int
 
534
slot_id_to_atr_txval_reg(int slot_id)
 
535
{
 
536
  static int reg[4]  = { FR_ATR_TXVAL_0, FR_ATR_TXVAL_1, FR_ATR_TXVAL_2, FR_ATR_TXVAL_3 };
 
537
  assert (0 <= slot_id && slot_id < 4);
 
538
  return reg[slot_id];
 
539
}
 
540
 
 
541
static int
 
542
slot_id_to_atr_rxval_reg(int slot_id)
 
543
{
 
544
  static int reg[4]  = { FR_ATR_RXVAL_0, FR_ATR_RXVAL_1, FR_ATR_RXVAL_2, FR_ATR_RXVAL_3 };
 
545
  assert (0 <= slot_id && slot_id < 4);
 
546
  return reg[slot_id];
 
547
}
 
548
 
 
549
static int
 
550
to_slot(txrx_t txrx, int which_side)
 
551
{
 
552
  // TX_A = 0
 
553
  // RX_A = 1
 
554
  // TX_B = 2
 
555
  // RX_B = 3
 
556
  return ((which_side & 0x1) << 1) | ((txrx & 0x1) == C_RX);
 
557
}
 
558
 
 
559
bool
 
560
usrp_basic::common_set_pga(txrx_t txrx, int which_amp, double gain)
 
561
{
 
562
  if (which_amp < 0 || which_amp > 3)
 
563
    return false;
 
564
 
 
565
  gain = std::min(common_pga_max(txrx),
 
566
                  std::max(common_pga_min(txrx), gain));
 
567
 
 
568
  int codec = which_amp >> 1;   
 
569
  int int_gain = (int) rint((gain - common_pga_min(txrx)) / common_pga_db_per_step(txrx));
 
570
 
 
571
  if (txrx == C_TX){            // 0 and 1 are same, as are 2 and 3
 
572
    return _write_9862(codec, REG_TX_PGA, int_gain);
 
573
  }
 
574
  else {
 
575
    int reg = (which_amp & 1) == 0 ? REG_RX_A : REG_RX_B;
 
576
 
 
577
    // read current value to get input buffer bypass flag.
 
578
    unsigned char cur_rx;
 
579
    if (!_read_9862(codec, reg, &cur_rx))
 
580
      return false;
 
581
 
 
582
    cur_rx = (cur_rx & RX_X_BYPASS_INPUT_BUFFER) | (int_gain & 0x7f);
 
583
    return _write_9862(codec, reg, cur_rx);
 
584
  }
 
585
}
 
586
 
 
587
double
 
588
usrp_basic::common_pga(txrx_t txrx, int which_amp) const
 
589
{
 
590
  if (which_amp < 0 || which_amp > 3)
 
591
    return READ_FAILED;
 
592
 
 
593
  if (txrx == C_TX){
 
594
    int codec = which_amp >> 1;
 
595
    unsigned char v;
 
596
    bool ok = _read_9862 (codec, REG_TX_PGA, &v);
 
597
    if (!ok)
 
598
      return READ_FAILED;
 
599
 
 
600
    return (pga_db_per_step() * v) + pga_min();
 
601
  }
 
602
  else {
 
603
    int codec = which_amp >> 1;
 
604
    int reg = (which_amp & 1) == 0 ? REG_RX_A : REG_RX_B;
 
605
    unsigned char v;
 
606
    bool ok = _read_9862 (codec, reg, &v);
 
607
    if (!ok)
 
608
      return READ_FAILED;
 
609
 
 
610
    return (pga_db_per_step() * (v & 0x1f)) + pga_min();
 
611
  }
 
612
}
 
613
 
 
614
double
 
615
usrp_basic::common_pga_min(txrx_t txrx) const
 
616
{
 
617
  if (txrx == C_TX)
 
618
    return -20.0;
 
619
  else
 
620
    return   0.0;
 
621
}
 
622
 
 
623
double
 
624
usrp_basic::common_pga_max(txrx_t txrx) const
 
625
{
 
626
  if (txrx == C_TX)
 
627
    return   0.0;
 
628
  else
 
629
    return  20.0;
 
630
}
 
631
 
 
632
double
 
633
usrp_basic::common_pga_db_per_step(txrx_t txrx) const
 
634
{
 
635
  if (txrx == C_TX)
 
636
    return  20.0 / 255;
 
637
  else
 
638
    return  20.0 / 20;
 
639
}
 
640
 
 
641
bool
 
642
usrp_basic::_common_write_oe(txrx_t txrx, int which_side, int value, int mask)
 
643
{
 
644
  if (! (0 <= which_side && which_side <= 1))
 
645
    return false;
 
646
 
 
647
  return _write_fpga_reg(slot_id_to_oe_reg(to_slot(txrx, which_side)),
 
648
                         (mask << 16) | (value & 0xffff));
 
649
}
 
650
 
 
651
bool
 
652
usrp_basic::common_write_io(txrx_t txrx, int which_side, int value, int mask)
 
653
{
 
654
  if (! (0 <= which_side && which_side <= 1))
 
655
    return false;
 
656
 
 
657
  return _write_fpga_reg(slot_id_to_io_reg(to_slot(txrx, which_side)),
 
658
                         (mask << 16) | (value & 0xffff));
 
659
}
 
660
 
 
661
bool
 
662
usrp_basic::common_read_io(txrx_t txrx, int which_side, int *value)
 
663
{
 
664
  if (! (0 <= which_side && which_side <= 1))
 
665
    return false;
 
666
 
 
667
  int t;
 
668
  int reg = which_side + 1;     // FIXME, *very* magic number (fix in serial_io.v)
 
669
  bool ok = _read_fpga_reg(reg, &t);
 
670
  if (!ok)
 
671
    return false;
 
672
 
 
673
  if (txrx == C_TX){
 
674
    *value = t & 0xffff;                // FIXME, more magic
 
675
    return true;
 
676
  }
 
677
  else {
 
678
    *value = (t >> 16) & 0xffff;        // FIXME, more magic
 
679
    return true;
 
680
  }
 
681
}
 
682
 
 
683
int
 
684
usrp_basic::common_read_io(txrx_t txrx, int which_side)
 
685
{
 
686
  int   value;
 
687
  if (!common_read_io(txrx, which_side, &value))
 
688
    return READ_FAILED;
 
689
  return value;
 
690
}
 
691
 
 
692
bool
 
693
usrp_basic::common_write_refclk(txrx_t txrx, int which_side, int value)
 
694
{
 
695
  if (! (0 <= which_side && which_side <= 1))
 
696
    return false;
 
697
 
 
698
  return _write_fpga_reg(slot_id_to_refclk_reg(to_slot(txrx, which_side)),
 
699
                         value);
 
700
}
 
701
 
 
702
bool
 
703
usrp_basic::common_write_atr_mask(txrx_t txrx, int which_side, int value)
 
704
{
 
705
  if (! (0 <= which_side && which_side <= 1))
 
706
    return false;
 
707
 
 
708
  return _write_fpga_reg(slot_id_to_atr_mask_reg(to_slot(txrx, which_side)),
 
709
                         value);
 
710
}
 
711
 
 
712
bool
 
713
usrp_basic::common_write_atr_txval(txrx_t txrx, int which_side, int value)
 
714
{
 
715
  if (! (0 <= which_side && which_side <= 1))
 
716
    return false;
 
717
 
 
718
  return _write_fpga_reg(slot_id_to_atr_txval_reg(to_slot(txrx, which_side)),
 
719
                         value);
 
720
}
 
721
 
 
722
bool
 
723
usrp_basic::common_write_atr_rxval(txrx_t txrx, int which_side, int value)
 
724
{
 
725
  if (! (0 <= which_side && which_side <= 1))
 
726
    return false;
 
727
 
 
728
  return _write_fpga_reg(slot_id_to_atr_rxval_reg(to_slot(txrx, which_side)),
 
729
                         value);
 
730
}
 
731
 
 
732
bool
 
733
usrp_basic::common_write_aux_dac(txrx_t txrx, int which_side, int which_dac, int value)
 
734
{
 
735
  return _write_aux_dac(to_slot(txrx, which_side), which_dac, value);
 
736
}
 
737
 
 
738
bool
 
739
usrp_basic::common_read_aux_adc(txrx_t txrx, int which_side, int which_adc, int *value)
 
740
{
 
741
  return _read_aux_adc(to_slot(txrx, which_side), which_adc, value);
 
742
}
 
743
 
 
744
int
 
745
usrp_basic::common_read_aux_adc(txrx_t txrx, int which_side, int which_adc)
 
746
{
 
747
  return _read_aux_adc(to_slot(txrx, which_side), which_adc);
 
748
}
 
749
 
 
750
 
 
751
////////////////////////////////////////////////////////////////
 
752
//
 
753
//                         usrp_basic_rx
 
754
//
 
755
////////////////////////////////////////////////////////////////
 
756
 
 
757
static unsigned char rx_init_regs[] = {
 
758
  REG_RX_PWR_DN,        0,
 
759
  REG_RX_A,             0,      // minimum gain = 0x00 (max gain = 0x14)
 
760
  REG_RX_B,             0,      // minimum gain = 0x00 (max gain = 0x14)
 
761
  REG_RX_MISC,          (RX_MISC_HS_DUTY_CYCLE | RX_MISC_CLK_DUTY),
 
762
  REG_RX_IF,            (RX_IF_USE_CLKOUT1
 
763
                         | RX_IF_2S_COMP),
 
764
  REG_RX_DIGITAL,       (RX_DIGITAL_2_CHAN)
 
765
};
 
766
 
 
767
 
 
768
usrp_basic_rx::usrp_basic_rx (int which_board, int fusb_block_size, int fusb_nblocks,
 
769
                              const std::string fpga_filename,
 
770
                              const std::string firmware_filename
 
771
                              )
 
772
  : usrp_basic (which_board, open_rx_interface, fpga_filename, firmware_filename),
 
773
    d_devhandle (0), d_ephandle (0),
 
774
    d_bytes_seen (0), d_first_read (true),
 
775
    d_rx_enable (false)
 
776
{
 
777
  // initialize rx specific registers
 
778
 
 
779
  if (!usrp_9862_write_many_all (d_udh, rx_init_regs, sizeof (rx_init_regs))){
 
780
    fprintf (stderr, "usrp_basic_rx: failed to init AD9862 RX regs\n");
 
781
    throw std::runtime_error ("usrp_basic_rx/init_9862");
 
782
  }
 
783
 
 
784
  if (0){
 
785
    // FIXME power down 2nd codec rx path
 
786
    usrp_9862_write (d_udh, 1, REG_RX_PWR_DN, 0x1);     // power down everything
 
787
  }
 
788
 
 
789
  // Reset the rx path and leave it disabled.
 
790
  set_rx_enable (false);
 
791
  usrp_set_fpga_rx_reset (d_udh, true);
 
792
  usrp_set_fpga_rx_reset (d_udh, false);
 
793
 
 
794
  set_fpga_rx_sample_rate_divisor (2);  // usually correct
 
795
 
 
796
  set_dc_offset_cl_enable(0xf, 0xf);    // enable DC offset removal control loops
 
797
 
 
798
  probe_rx_slots (false);
 
799
 
 
800
  //d_db[0] = instantiate_dbs(d_dbid[0], this, 0);
 
801
  //d_db[1] = instantiate_dbs(d_dbid[1], this, 1);
 
802
 
 
803
  // check fusb buffering parameters
 
804
 
 
805
  if (fusb_block_size < 0 || fusb_block_size > FUSB_BLOCK_SIZE)
 
806
    throw std::out_of_range ("usrp_basic_rx: invalid fusb_block_size");
 
807
 
 
808
  if (fusb_nblocks < 0)
 
809
    throw std::out_of_range ("usrp_basic_rx: invalid fusb_nblocks");
 
810
  
 
811
  if (fusb_block_size == 0)
 
812
    fusb_block_size = fusb_sysconfig::default_block_size();
 
813
 
 
814
  if (fusb_nblocks == 0)
 
815
    fusb_nblocks = std::max (1, FUSB_BUFFER_SIZE / fusb_block_size);
 
816
 
 
817
  d_devhandle = fusb_sysconfig::make_devhandle (d_udh);
 
818
  d_ephandle = d_devhandle->make_ephandle (USRP_RX_ENDPOINT, true,
 
819
                                           fusb_block_size, fusb_nblocks);
 
820
 
 
821
  write_atr_mask(0, 0);         // zero Rx A Auto Transmit/Receive regs
 
822
  write_atr_txval(0, 0);
 
823
  write_atr_rxval(0, 0);
 
824
  write_atr_mask(1, 0);         // zero Rx B Auto Transmit/Receive regs
 
825
  write_atr_txval(1, 0);
 
826
  write_atr_rxval(1, 0);
 
827
}
 
828
 
 
829
static unsigned char rx_fini_regs[] = {
 
830
  REG_RX_PWR_DN,        0x1                             // power down everything
 
831
};
 
832
 
 
833
usrp_basic_rx::~usrp_basic_rx ()
 
834
{
 
835
  if (!set_rx_enable (false)){
 
836
    fprintf (stderr, "usrp_basic_rx: set_fpga_rx_enable failed\n");
 
837
    usb_strerror ();
 
838
  }
 
839
 
 
840
  d_ephandle->stop ();
 
841
  delete d_ephandle;
 
842
  delete d_devhandle;
 
843
 
 
844
  if (!usrp_9862_write_many_all (d_udh, rx_fini_regs, sizeof (rx_fini_regs))){
 
845
    fprintf (stderr, "usrp_basic_rx: failed to fini AD9862 RX regs\n");
 
846
  }
 
847
 
 
848
  shutdown_daughterboards();
 
849
}
 
850
 
 
851
 
 
852
bool
 
853
usrp_basic_rx::start ()
 
854
{
 
855
  if (!usrp_basic::start ())    // invoke parent's method
 
856
    return false;
 
857
 
 
858
  // fire off reads before asserting rx_enable
 
859
 
 
860
  if (!d_ephandle->start ()){
 
861
    fprintf (stderr, "usrp_basic_rx: failed to start end point streaming");
 
862
    usb_strerror ();
 
863
    return false;
 
864
  }
 
865
 
 
866
  if (!set_rx_enable (true)){
 
867
    fprintf (stderr, "usrp_basic_rx: set_rx_enable failed\n");
 
868
    usb_strerror ();
 
869
    return false;
 
870
  }
 
871
  
 
872
  return true;
 
873
}
 
874
 
 
875
bool
 
876
usrp_basic_rx::stop ()
 
877
{
 
878
  bool ok = usrp_basic::stop();
 
879
 
 
880
  if (!set_rx_enable(false)){
 
881
    fprintf (stderr, "usrp_basic_rx: set_rx_enable(false) failed\n");
 
882
    usb_strerror ();
 
883
    ok = false;
 
884
  }
 
885
 
 
886
  if (!d_ephandle->stop()){
 
887
    fprintf (stderr, "usrp_basic_rx: failed to stop end point streaming");
 
888
    usb_strerror ();
 
889
    ok = false;
 
890
  }
 
891
 
 
892
  return ok;
 
893
}
 
894
 
 
895
usrp_basic_rx *
 
896
usrp_basic_rx::make (int which_board, int fusb_block_size, int fusb_nblocks,
 
897
                     const std::string fpga_filename,
 
898
                     const std::string firmware_filename)
 
899
{
 
900
  usrp_basic_rx *u = 0;
 
901
  
 
902
  try {
 
903
    u = new usrp_basic_rx (which_board, fusb_block_size, fusb_nblocks,
 
904
                           fpga_filename, firmware_filename);
 
905
    return u;
 
906
  }
 
907
  catch (...){
 
908
    delete u;
 
909
    return 0;
 
910
  }
 
911
 
 
912
  return u;
 
913
}
 
914
 
 
915
bool
 
916
usrp_basic_rx::set_fpga_rx_sample_rate_divisor (unsigned int div)
 
917
{
 
918
  return _write_fpga_reg (FR_RX_SAMPLE_RATE_DIV, div - 1);
 
919
}
 
920
 
 
921
 
 
922
/*
 
923
 * \brief read data from the D/A's via the FPGA.
 
924
 * \p len must be a multiple of 512 bytes.
 
925
 *
 
926
 * \returns the number of bytes read, or -1 on error.
 
927
 *
 
928
 * If overrun is non-NULL it will be set true iff an RX overrun is detected.
 
929
 */
 
930
int
 
931
usrp_basic_rx::read (void *buf, int len, bool *overrun)
 
932
{
 
933
  int   r;
 
934
  
 
935
  if (overrun)
 
936
    *overrun = false;
 
937
  
 
938
  if (len < 0 || (len % 512) != 0){
 
939
    fprintf (stderr, "usrp_basic_rx::read: invalid length = %d\n", len);
 
940
    return -1;
 
941
  }
 
942
 
 
943
  r = d_ephandle->read (buf, len);
 
944
  if (r > 0)
 
945
    d_bytes_seen += r;
 
946
 
 
947
  /*
 
948
   * In many cases, the FPGA reports an rx overrun right after we
 
949
   * enable the Rx path.  If this is our first read, check for the
 
950
   * overrun to clear the condition, then ignore the result.
 
951
   */
 
952
  if (0 && d_first_read){       // FIXME
 
953
    d_first_read = false;
 
954
    bool bogus_overrun;
 
955
    usrp_check_rx_overrun (d_udh, &bogus_overrun);
 
956
  }
 
957
 
 
958
  if (overrun != 0 && d_bytes_seen >= d_bytes_per_poll){
 
959
    d_bytes_seen = 0;
 
960
    if (!usrp_check_rx_overrun (d_udh, overrun)){
 
961
      fprintf (stderr, "usrp_basic_rx: usrp_check_rx_overrun failed\n");
 
962
      usb_strerror ();
 
963
    }
 
964
  }
 
965
    
 
966
  return r;
 
967
}
 
968
 
 
969
bool
 
970
usrp_basic_rx::set_rx_enable (bool on)
 
971
{
 
972
  d_rx_enable = on;
 
973
  return usrp_set_fpga_rx_enable (d_udh, on);
 
974
}
 
975
 
 
976
// conditional disable, return prev state
 
977
bool
 
978
usrp_basic_rx::disable_rx ()
 
979
{
 
980
  bool enabled = rx_enable ();
 
981
  if (enabled)
 
982
    set_rx_enable (false);
 
983
  return enabled;
 
984
}
 
985
 
 
986
// conditional set
 
987
void
 
988
usrp_basic_rx::restore_rx (bool on)
 
989
{
 
990
  if (on != rx_enable ())
 
991
    set_rx_enable (on);
 
992
}
 
993
 
 
994
void
 
995
usrp_basic_rx::probe_rx_slots (bool verbose)
 
996
{
 
997
  struct usrp_dboard_eeprom     eeprom;
 
998
  static int slot_id_map[2] = { SLOT_RX_A, SLOT_RX_B };
 
999
  static const char *slot_name[2] = { "RX d'board A", "RX d'board B" };
 
1000
 
 
1001
  for (int i = 0; i < 2; i++){
 
1002
    int slot_id = slot_id_map [i];
 
1003
    const char *msg = 0;
 
1004
    usrp_dbeeprom_status_t s = usrp_read_dboard_eeprom (d_udh, slot_id, &eeprom);
 
1005
 
 
1006
    switch (s){
 
1007
    case UDBE_OK:
 
1008
      d_dbid[i] = eeprom.id;
 
1009
      msg = usrp_dbid_to_string (eeprom.id).c_str ();
 
1010
      set_adc_offset (2*i+0, eeprom.offset[0]);
 
1011
      set_adc_offset (2*i+1, eeprom.offset[1]);
 
1012
      _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | eeprom.oe);
 
1013
      _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
 
1014
      break;
 
1015
      
 
1016
    case UDBE_NO_EEPROM:
 
1017
      d_dbid[i] = -1;
 
1018
      msg = "<none>";
 
1019
      _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
 
1020
      _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
 
1021
      break;
 
1022
      
 
1023
    case UDBE_INVALID_EEPROM:
 
1024
      d_dbid[i] = -2;
 
1025
      msg = "Invalid EEPROM contents";
 
1026
      _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
 
1027
      _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
 
1028
      break;
 
1029
      
 
1030
    case UDBE_BAD_SLOT:
 
1031
    default:
 
1032
      assert (0);
 
1033
    }
 
1034
 
 
1035
    if (verbose){
 
1036
      fflush (stdout);
 
1037
      fprintf (stderr, "%s: %s\n", slot_name[i], msg);
 
1038
    }
 
1039
  }
 
1040
}
 
1041
 
 
1042
bool
 
1043
usrp_basic_rx::set_pga (int which_amp, double gain)
 
1044
{
 
1045
  return common_set_pga(C_RX, which_amp, gain);
 
1046
}
 
1047
 
 
1048
double
 
1049
usrp_basic_rx::pga(int which_amp) const
 
1050
{
 
1051
  return common_pga(C_RX, which_amp);
 
1052
}
 
1053
 
 
1054
double
 
1055
usrp_basic_rx::pga_min() const
 
1056
{
 
1057
  return common_pga_min(C_RX);
 
1058
}
 
1059
 
 
1060
double
 
1061
usrp_basic_rx::pga_max() const
 
1062
{
 
1063
  return common_pga_max(C_RX);
 
1064
}
 
1065
 
 
1066
double
 
1067
usrp_basic_rx::pga_db_per_step() const
 
1068
{
 
1069
  return common_pga_db_per_step(C_RX);
 
1070
}
 
1071
 
 
1072
bool
 
1073
usrp_basic_rx::_write_oe (int which_side, int value, int mask)
 
1074
{
 
1075
  return _common_write_oe(C_RX, which_side, value, mask);
 
1076
}
 
1077
 
 
1078
bool
 
1079
usrp_basic_rx::write_io (int which_side, int value, int mask)
 
1080
{
 
1081
  return common_write_io(C_RX, which_side, value, mask);
 
1082
}
 
1083
 
 
1084
bool
 
1085
usrp_basic_rx::read_io (int which_side, int *value)
 
1086
{
 
1087
  return common_read_io(C_RX, which_side, value);
 
1088
}
 
1089
 
 
1090
int
 
1091
usrp_basic_rx::read_io (int which_side)
 
1092
{
 
1093
  return common_read_io(C_RX, which_side);
 
1094
}
 
1095
 
 
1096
bool
 
1097
usrp_basic_rx::write_refclk(int which_side, int value)
 
1098
{
 
1099
  return common_write_refclk(C_RX, which_side, value);
 
1100
}
 
1101
 
 
1102
bool
 
1103
usrp_basic_rx::write_atr_mask(int which_side, int value)
 
1104
{
 
1105
  return common_write_atr_mask(C_RX, which_side, value);
 
1106
}
 
1107
 
 
1108
bool
 
1109
usrp_basic_rx::write_atr_txval(int which_side, int value)
 
1110
{
 
1111
  return common_write_atr_txval(C_RX, which_side, value);
 
1112
}
 
1113
 
 
1114
bool
 
1115
usrp_basic_rx::write_atr_rxval(int which_side, int value)
 
1116
{
 
1117
  return common_write_atr_rxval(C_RX, which_side, value);
 
1118
}
 
1119
 
 
1120
bool
 
1121
usrp_basic_rx::write_aux_dac (int which_side, int which_dac, int value)
 
1122
{
 
1123
  return common_write_aux_dac(C_RX, which_side, which_dac, value);
 
1124
}
 
1125
 
 
1126
bool
 
1127
usrp_basic_rx::read_aux_adc (int which_side, int which_adc, int *value)
 
1128
{
 
1129
  return common_read_aux_adc(C_RX, which_side, which_adc, value);
 
1130
}
 
1131
 
 
1132
int
 
1133
usrp_basic_rx::read_aux_adc (int which_side, int which_adc)
 
1134
{
 
1135
  return common_read_aux_adc(C_RX, which_side, which_adc);
 
1136
}
 
1137
 
 
1138
int
 
1139
usrp_basic_rx::block_size () const { return d_ephandle->block_size(); }
 
1140
 
 
1141
////////////////////////////////////////////////////////////////
 
1142
//
 
1143
//                         usrp_basic_tx
 
1144
//
 
1145
////////////////////////////////////////////////////////////////
 
1146
 
 
1147
 
 
1148
//
 
1149
// DAC input rate 64 MHz interleaved for a total input rate of 128 MHz
 
1150
// DAC input is latched on rising edge of CLKOUT2
 
1151
// NCO is disabled
 
1152
// interpolate 2x
 
1153
// coarse modulator disabled
 
1154
//
 
1155
 
 
1156
static unsigned char tx_init_regs[] = {
 
1157
  REG_TX_PWR_DN,        0,
 
1158
  REG_TX_A_OFFSET_LO,   0,
 
1159
  REG_TX_A_OFFSET_HI,   0,
 
1160
  REG_TX_B_OFFSET_LO,   0,
 
1161
  REG_TX_B_OFFSET_HI,   0,
 
1162
  REG_TX_A_GAIN,        (TX_X_GAIN_COARSE_FULL | 0),
 
1163
  REG_TX_B_GAIN,        (TX_X_GAIN_COARSE_FULL | 0),
 
1164
  REG_TX_PGA,           0xff,                   // maximum gain (0 dB)
 
1165
  REG_TX_MISC,          0,
 
1166
  REG_TX_IF,            (TX_IF_USE_CLKOUT1
 
1167
                         | TX_IF_I_FIRST
 
1168
                         | TX_IF_INV_TX_SYNC
 
1169
                         | TX_IF_2S_COMP
 
1170
                         | TX_IF_INTERLEAVED),
 
1171
  REG_TX_DIGITAL,       (TX_DIGITAL_2_DATA_PATHS
 
1172
                         | TX_DIGITAL_INTERPOLATE_4X),
 
1173
  REG_TX_MODULATOR,     (TX_MODULATOR_DISABLE_NCO
 
1174
                         | TX_MODULATOR_COARSE_MODULATION_NONE),
 
1175
  REG_TX_NCO_FTW_7_0,   0,
 
1176
  REG_TX_NCO_FTW_15_8,  0,
 
1177
  REG_TX_NCO_FTW_23_16, 0
 
1178
};
 
1179
 
 
1180
usrp_basic_tx::usrp_basic_tx (int which_board, int fusb_block_size, int fusb_nblocks,
 
1181
                              const std::string fpga_filename,
 
1182
                              const std::string firmware_filename)
 
1183
  : usrp_basic (which_board, open_tx_interface, fpga_filename, firmware_filename),
 
1184
    d_devhandle (0), d_ephandle (0),
 
1185
    d_bytes_seen (0), d_first_write (true),
 
1186
    d_tx_enable (false)
 
1187
{
 
1188
  if (!usrp_9862_write_many_all (d_udh, tx_init_regs, sizeof (tx_init_regs))){
 
1189
    fprintf (stderr, "usrp_basic_tx: failed to init AD9862 TX regs\n");
 
1190
    throw std::runtime_error ("usrp_basic_tx/init_9862");
 
1191
  }
 
1192
 
 
1193
  if (0){
 
1194
    // FIXME power down 2nd codec tx path
 
1195
    usrp_9862_write (d_udh, 1, REG_TX_PWR_DN,
 
1196
                     (TX_PWR_DN_TX_DIGITAL
 
1197
                      | TX_PWR_DN_TX_ANALOG_BOTH));
 
1198
  }
 
1199
 
 
1200
  // Reset the tx path and leave it disabled.
 
1201
  set_tx_enable (false);
 
1202
  usrp_set_fpga_tx_reset (d_udh, true);
 
1203
  usrp_set_fpga_tx_reset (d_udh, false);
 
1204
 
 
1205
  set_fpga_tx_sample_rate_divisor (4);  // we're using interp x4
 
1206
 
 
1207
  probe_tx_slots (false);
 
1208
 
 
1209
  //d_db[0] = instantiate_dbs(d_dbid[0], this, 0);
 
1210
  //d_db[1] = instantiate_dbs(d_dbid[1], this, 1);
 
1211
 
 
1212
  // check fusb buffering parameters
 
1213
 
 
1214
  if (fusb_block_size < 0 || fusb_block_size > FUSB_BLOCK_SIZE)
 
1215
    throw std::out_of_range ("usrp_basic_rx: invalid fusb_block_size");
 
1216
 
 
1217
  if (fusb_nblocks < 0)
 
1218
    throw std::out_of_range ("usrp_basic_rx: invalid fusb_nblocks");
 
1219
  
 
1220
  if (fusb_block_size == 0)
 
1221
    fusb_block_size = FUSB_BLOCK_SIZE;
 
1222
 
 
1223
  if (fusb_nblocks == 0)
 
1224
    fusb_nblocks = std::max (1, FUSB_BUFFER_SIZE / fusb_block_size);
 
1225
 
 
1226
  d_devhandle = fusb_sysconfig::make_devhandle (d_udh);
 
1227
  d_ephandle = d_devhandle->make_ephandle (USRP_TX_ENDPOINT, false,
 
1228
                                           fusb_block_size, fusb_nblocks);
 
1229
 
 
1230
  write_atr_mask(0, 0);         // zero Tx A Auto Transmit/Receive regs
 
1231
  write_atr_txval(0, 0);
 
1232
  write_atr_rxval(0, 0);
 
1233
  write_atr_mask(1, 0);         // zero Tx B Auto Transmit/Receive regs
 
1234
  write_atr_txval(1, 0);
 
1235
  write_atr_rxval(1, 0);
 
1236
}
 
1237
 
 
1238
 
 
1239
static unsigned char tx_fini_regs[] = {
 
1240
  REG_TX_PWR_DN,        (TX_PWR_DN_TX_DIGITAL
 
1241
                         | TX_PWR_DN_TX_ANALOG_BOTH),
 
1242
  REG_TX_MODULATOR,     (TX_MODULATOR_DISABLE_NCO
 
1243
                         | TX_MODULATOR_COARSE_MODULATION_NONE)
 
1244
};
 
1245
 
 
1246
usrp_basic_tx::~usrp_basic_tx ()
 
1247
{
 
1248
  d_ephandle->stop ();
 
1249
  delete d_ephandle;
 
1250
  delete d_devhandle;
 
1251
 
 
1252
  if (!usrp_9862_write_many_all (d_udh, tx_fini_regs, sizeof (tx_fini_regs))){
 
1253
    fprintf (stderr, "usrp_basic_tx: failed to fini AD9862 TX regs\n");
 
1254
  }
 
1255
 
 
1256
  shutdown_daughterboards();
 
1257
}
 
1258
 
 
1259
bool
 
1260
usrp_basic_tx::start ()
 
1261
{
 
1262
  if (!usrp_basic::start ())
 
1263
    return false;
 
1264
 
 
1265
  if (!set_tx_enable (true)){
 
1266
    fprintf (stderr, "usrp_basic_tx: set_tx_enable failed\n");
 
1267
    usb_strerror ();
 
1268
    return false;
 
1269
  }
 
1270
  
 
1271
  if (!d_ephandle->start ()){
 
1272
    fprintf (stderr, "usrp_basic_tx: failed to start end point streaming");
 
1273
    usb_strerror ();
 
1274
    return false;
 
1275
  }
 
1276
 
 
1277
  return true;
 
1278
}
 
1279
 
 
1280
bool
 
1281
usrp_basic_tx::stop ()
 
1282
{
 
1283
  bool ok = usrp_basic::stop ();
 
1284
 
 
1285
  if (!d_ephandle->stop ()){
 
1286
    fprintf (stderr, "usrp_basic_tx: failed to stop end point streaming");
 
1287
    usb_strerror ();
 
1288
    ok = false;
 
1289
  }
 
1290
 
 
1291
  if (!set_tx_enable (false)){
 
1292
    fprintf (stderr, "usrp_basic_tx: set_tx_enable(false) failed\n");
 
1293
    usb_strerror ();
 
1294
    ok = false;
 
1295
  }
 
1296
 
 
1297
  return ok;
 
1298
}
 
1299
 
 
1300
usrp_basic_tx *
 
1301
usrp_basic_tx::make (int which_board, int fusb_block_size, int fusb_nblocks,
 
1302
                     const std::string fpga_filename,
 
1303
                     const std::string firmware_filename)
 
1304
{
 
1305
  usrp_basic_tx *u = 0;
 
1306
  
 
1307
  try {
 
1308
    u = new usrp_basic_tx (which_board, fusb_block_size, fusb_nblocks,
 
1309
                           fpga_filename, firmware_filename);
 
1310
    return u;
 
1311
  }
 
1312
  catch (...){
 
1313
    delete u;
 
1314
    return 0;
 
1315
  }
 
1316
 
 
1317
  return u;
 
1318
}
 
1319
 
 
1320
bool
 
1321
usrp_basic_tx::set_fpga_tx_sample_rate_divisor (unsigned int div)
 
1322
{
 
1323
  return _write_fpga_reg (FR_TX_SAMPLE_RATE_DIV, div - 1);
 
1324
}
 
1325
 
 
1326
/*!
 
1327
 * \brief Write data to the A/D's via the FPGA.
 
1328
 *
 
1329
 * \p len must be a multiple of 512 bytes.
 
1330
 * \returns number of bytes written or -1 on error.
 
1331
 *
 
1332
 * if \p underrun is non-NULL, it will be set to true iff
 
1333
 * a transmit underrun condition is detected.
 
1334
 */
 
1335
int
 
1336
usrp_basic_tx::write (const void *buf, int len, bool *underrun)
 
1337
{
 
1338
  int   r;
 
1339
  
 
1340
  if (underrun)
 
1341
    *underrun = false;
 
1342
  
 
1343
  if (len < 0 || (len % 512) != 0){
 
1344
    fprintf (stderr, "usrp_basic_tx::write: invalid length = %d\n", len);
 
1345
    return -1;
 
1346
  }
 
1347
 
 
1348
  r = d_ephandle->write (buf, len);
 
1349
  if (r > 0)
 
1350
    d_bytes_seen += r;
 
1351
    
 
1352
  /*
 
1353
   * In many cases, the FPGA reports an tx underrun right after we
 
1354
   * enable the Tx path.  If this is our first write, check for the
 
1355
   * underrun to clear the condition, then ignore the result.
 
1356
   */
 
1357
  if (d_first_write && d_bytes_seen >= 4 * FUSB_BLOCK_SIZE){
 
1358
    d_first_write = false;
 
1359
    bool bogus_underrun;
 
1360
    usrp_check_tx_underrun (d_udh, &bogus_underrun);
 
1361
  }
 
1362
 
 
1363
  if (underrun != 0 && d_bytes_seen >= d_bytes_per_poll){
 
1364
    d_bytes_seen = 0;
 
1365
    if (!usrp_check_tx_underrun (d_udh, underrun)){
 
1366
      fprintf (stderr, "usrp_basic_tx: usrp_check_tx_underrun failed\n");
 
1367
      usb_strerror ();
 
1368
    }
 
1369
  }
 
1370
 
 
1371
  return r;
 
1372
}
 
1373
 
 
1374
void
 
1375
usrp_basic_tx::wait_for_completion ()
 
1376
{
 
1377
  d_ephandle->wait_for_completion ();
 
1378
}
 
1379
 
 
1380
bool
 
1381
usrp_basic_tx::set_tx_enable (bool on)
 
1382
{
 
1383
  d_tx_enable = on;
 
1384
  // fprintf (stderr, "set_tx_enable %d\n", on);
 
1385
  return usrp_set_fpga_tx_enable (d_udh, on);
 
1386
}
 
1387
 
 
1388
// conditional disable, return prev state
 
1389
bool
 
1390
usrp_basic_tx::disable_tx ()
 
1391
{
 
1392
  bool enabled = tx_enable ();
 
1393
  if (enabled)
 
1394
    set_tx_enable (false);
 
1395
  return enabled;
 
1396
}
 
1397
 
 
1398
// conditional set
 
1399
void
 
1400
usrp_basic_tx::restore_tx (bool on)
 
1401
{
 
1402
  if (on != tx_enable ())
 
1403
    set_tx_enable (on);
 
1404
}
 
1405
 
 
1406
void
 
1407
usrp_basic_tx::probe_tx_slots (bool verbose)
 
1408
{
 
1409
  struct usrp_dboard_eeprom     eeprom;
 
1410
  static int slot_id_map[2] = { SLOT_TX_A, SLOT_TX_B };
 
1411
  static const char *slot_name[2] = { "TX d'board A", "TX d'board B" };
 
1412
 
 
1413
  for (int i = 0; i < 2; i++){
 
1414
    int slot_id = slot_id_map [i];
 
1415
    const char *msg = 0;
 
1416
    usrp_dbeeprom_status_t s = usrp_read_dboard_eeprom (d_udh, slot_id, &eeprom);
 
1417
 
 
1418
    switch (s){
 
1419
    case UDBE_OK:
 
1420
      d_dbid[i] = eeprom.id;
 
1421
      msg = usrp_dbid_to_string (eeprom.id).c_str ();
 
1422
      // FIXME, figure out interpretation of dc offset for TX d'boards
 
1423
      // offset = (eeprom.offset[1] << 16) | (eeprom.offset[0] & 0xffff);
 
1424
      _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | eeprom.oe);
 
1425
      _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
 
1426
      break;
 
1427
      
 
1428
    case UDBE_NO_EEPROM:
 
1429
      d_dbid[i] = -1;
 
1430
      msg = "<none>";
 
1431
      _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
 
1432
      _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
 
1433
      break;
 
1434
      
 
1435
    case UDBE_INVALID_EEPROM:
 
1436
      d_dbid[i] = -2;
 
1437
      msg = "Invalid EEPROM contents";
 
1438
      _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
 
1439
      _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
 
1440
      break;
 
1441
      
 
1442
    case UDBE_BAD_SLOT:
 
1443
    default:
 
1444
      assert (0);
 
1445
    }
 
1446
 
 
1447
    if (verbose){
 
1448
      fflush (stdout);
 
1449
      fprintf (stderr, "%s: %s\n", slot_name[i], msg);
 
1450
    }
 
1451
  }
 
1452
}
 
1453
 
 
1454
bool
 
1455
usrp_basic_tx::set_pga (int which_amp, double gain)
 
1456
{
 
1457
  return common_set_pga(C_TX, which_amp, gain);
 
1458
}
 
1459
 
 
1460
double
 
1461
usrp_basic_tx::pga (int which_amp) const
 
1462
{
 
1463
  return common_pga(C_TX, which_amp);
 
1464
}
 
1465
 
 
1466
double
 
1467
usrp_basic_tx::pga_min() const
 
1468
{
 
1469
  return common_pga_min(C_TX);
 
1470
}
 
1471
 
 
1472
double
 
1473
usrp_basic_tx::pga_max() const
 
1474
{
 
1475
  return common_pga_max(C_TX);
 
1476
}
 
1477
 
 
1478
double
 
1479
usrp_basic_tx::pga_db_per_step() const
 
1480
{
 
1481
  return common_pga_db_per_step(C_TX);
 
1482
}
 
1483
 
 
1484
bool
 
1485
usrp_basic_tx::_write_oe (int which_side, int value, int mask)
 
1486
{
 
1487
  return _common_write_oe(C_TX, which_side, value, mask);
 
1488
}
 
1489
 
 
1490
bool
 
1491
usrp_basic_tx::write_io (int which_side, int value, int mask)
 
1492
{
 
1493
  return common_write_io(C_TX, which_side, value, mask);
 
1494
}
 
1495
 
 
1496
bool
 
1497
usrp_basic_tx::read_io (int which_side, int *value)
 
1498
{
 
1499
  return common_read_io(C_TX, which_side, value);
 
1500
}
 
1501
 
 
1502
int
 
1503
usrp_basic_tx::read_io (int which_side)
 
1504
{
 
1505
  return common_read_io(C_TX, which_side);
 
1506
}
 
1507
 
 
1508
bool
 
1509
usrp_basic_tx::write_refclk(int which_side, int value)
 
1510
{
 
1511
  return common_write_refclk(C_TX, which_side, value);
 
1512
}
 
1513
 
 
1514
bool
 
1515
usrp_basic_tx::write_atr_mask(int which_side, int value)
 
1516
{
 
1517
  return common_write_atr_mask(C_TX, which_side, value);
 
1518
}
 
1519
 
 
1520
bool
 
1521
usrp_basic_tx::write_atr_txval(int which_side, int value)
 
1522
{
 
1523
  return common_write_atr_txval(C_TX, which_side, value);
 
1524
}
 
1525
 
 
1526
bool
 
1527
usrp_basic_tx::write_atr_rxval(int which_side, int value)
 
1528
{
 
1529
  return common_write_atr_rxval(C_TX, which_side, value);
 
1530
}
 
1531
 
 
1532
bool
 
1533
usrp_basic_tx::write_aux_dac (int which_side, int which_dac, int value)
 
1534
{
 
1535
  return common_write_aux_dac(C_TX, which_side, which_dac, value);
 
1536
}
 
1537
 
 
1538
bool
 
1539
usrp_basic_tx::read_aux_adc (int which_side, int which_adc, int *value)
 
1540
{
 
1541
  return common_read_aux_adc(C_TX, which_side, which_adc, value);
 
1542
}
 
1543
 
 
1544
int
 
1545
usrp_basic_tx::read_aux_adc (int which_side, int which_adc)
 
1546
{
 
1547
  return common_read_aux_adc(C_TX, which_side, which_adc);
 
1548
}
 
1549
 
 
1550
int
 
1551
usrp_basic_tx::block_size () const { return d_ephandle->block_size(); }
 
1552