summaryrefslogtreecommitdiff
path: root/usrp/host/lib/db_wbxng.cc
blob: 3dafe458bcd3c03b5d35a13cbb9860a041a8f8a2 (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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//
// Copyright 2008,2009 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 asversion 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 <usrp/db_wbxng.h>
#include "db_wbxng_adf4350.h"
#include <db_base_impl.h>
#include <stdio.h>

// d'board i/o pin defs
// Tx and Rx have shared defs, but different i/o regs
#define ENABLE_5        (1 << 7)         // enables 5.0V power supply
#define ENABLE_33       (1 << 6)         // enables 3.3V supply
//#define RX_TXN          (1 << 15)         // Tx only: T/R antenna switch for TX/RX port
//#define RX2_RX1N        (1 << 15)         // Rx only: antenna switch between RX2 and TX/RX port
#define RX_TXN          ((1 << 5)|(1 << 15))         // Tx only: T/R antenna switch for TX/RX port
#define RX2_RX1N        ((1 << 5)|(1 << 15))         // Rx only: antenna switch between RX2 and TX/RX port
#define RXBB_EN         (1 << 4)
#define TXMOD_EN        (1 << 4)
#define PLL_CE          (1 << 3)
#define PLL_PDBRF       (1 << 2)
#define PLL_MUXOUT      (1 << 1)
#define PLL_LOCK_DETECT (1 << 0)

// RX Attenuator constants
#define ATTN_SHIFT	8
#define ATTN_MASK	(63 << ATTN_SHIFT)

wbxng_base::wbxng_base(usrp_basic_sptr _usrp, int which, int _power_on)
  : db_base(_usrp, which), d_power_on(_power_on)
{
  /*
    @param usrp: instance of usrp.source_c
    @param which: which side: 0 or 1 corresponding to side A or B respectively
    @type which: int
  */

  usrp()->_write_oe(d_which, 0, 0xffff);   // turn off all outputs

  d_first = true;
  d_spi_format = SPI_FMT_MSB | SPI_FMT_HDR_0;

  _enable_refclk(false);                // disable refclk

  set_auto_tr(false);
}

wbxng_base::~wbxng_base()
{
  if (d_common)
    delete d_common;
}

struct freq_result_t
wbxng_base::set_freq(double freq)
{
  /*
    @returns (ok, actual_baseband_freq) where:
    ok is True or False and indicates success or failure,
    actual_baseband_freq is the RF frequency that corresponds to DC in the IF.
  */

  freq_t int_freq = freq_t(freq);
  bool ok = d_common->_set_freq(int_freq*2);
  double freq_result = (double) d_common->_get_freq()/2.0;
  struct freq_result_t args = {ok, freq_result};

  /* Wait before reading Lock Detect*/
  timespec t;
  t.tv_sec = 0;
  t.tv_nsec = 10000000;
  nanosleep(&t, NULL);

  fprintf(stderr,"Setting WBXNG frequency, requested %d, obtained %f, lock_detect %d\n",
          int_freq, freq_result, d_common->_get_locked());

  // FIXME
  // Offsetting the LO helps get the Tx carrier leakage out of the way.
  // This also ensures that on Rx, we're not getting hosed by the
  // FPGA's DC removal loop's time constant.  We were seeing a
  // problem when running with discontinuous transmission.
  // Offsetting the LO made the problem go away.
  //freq += d_lo_offset;

  return args;
}

bool
wbxng_base::_set_pga(float pga_gain)
{
  if(d_which == 0) {
    usrp()->set_pga(0, pga_gain);
    usrp()->set_pga(1, pga_gain);
  }
  else {
    usrp()->set_pga(2, pga_gain);
    usrp()->set_pga(3, pga_gain);
  }
  return true;
}

bool
wbxng_base::is_quadrature()
{
  /*
    Return True if this board requires both I & Q analog channels.

    This bit of info is useful when setting up the USRP Rx mux register.
  */
  return true;
}

double
wbxng_base::freq_min()
{
  return (double) d_common->_get_min_freq()/2.0;
}

double
wbxng_base::freq_max()
{
  return (double) d_common->_get_max_freq()/2.0;
}

// ----------------------------------------------------------------

wbxng_base_tx::wbxng_base_tx(usrp_basic_sptr _usrp, int which, int _power_on)
  : wbxng_base(_usrp, which, _power_on)
{
  /*
    @param usrp: instance of usrp.sink_c
    @param which: 0 or 1 corresponding to side TX_A or TX_B respectively.
  */

  if(which == 0) {
    d_spi_enable = SPI_ENABLE_TX_A;
  }
  else {
    d_spi_enable = SPI_ENABLE_TX_B;
  }

  d_common = new adf4350(_usrp, d_which, d_spi_enable);

  // power up the transmit side, but don't enable the mixer
  usrp()->_write_oe(d_which,(RX_TXN|TXMOD_EN|ENABLE_33|ENABLE_5), (RX_TXN|TXMOD_EN|ENABLE_33|ENABLE_5));
  usrp()->write_io(d_which, (power_on()|RX_TXN|ENABLE_33|ENABLE_5), (RX_TXN|ENABLE_33|ENABLE_5));
  //set_lo_offset(4e6);
  
  // Disable VCO/PLL
  d_common->_enable(true);

  set_gain((gain_min() + gain_max()) / 2.0);  // initialize gain
}

wbxng_base_tx::~wbxng_base_tx()
{
  shutdown();
}


void
wbxng_base_tx::shutdown()
{
  // fprintf(stderr, "wbxng_base_tx::shutdown  d_is_shutdown = %d\n", d_is_shutdown);

  if (!d_is_shutdown){
    d_is_shutdown = true;
    // do whatever there is to do to shutdown

    // Disable VCO/PLL
    d_common->_enable(false);

    // Power down and leave the T/R switch in the R position
    usrp()->write_io(d_which, (power_off()|RX_TXN), (RX_TXN|ENABLE_33|ENABLE_5));


    /*
    _write_control(_compute_control_reg());
    */
    _enable_refclk(false);                       // turn off refclk
    set_auto_tr(false);
  }
}

bool
wbxng_base_tx::set_auto_tr(bool on)
{
  bool ok = true;
  if(on) {
    ok &= set_atr_mask (RX_TXN | TXMOD_EN);
    ok &= set_atr_txval(0      | TXMOD_EN);
    ok &= set_atr_rxval(RX_TXN);
  }
  else {
    ok &= set_atr_mask (0);
    ok &= set_atr_txval(0);
    ok &= set_atr_rxval(0);
  }
  return ok;
}

bool
wbxng_base_tx::set_enable(bool on)
{
  /*
    Enable transmitter if on is true
  */

  int v;
  int mask = RX_TXN | TXMOD_EN;
  if(on) {
    v = TXMOD_EN;
    // Enable VCO/PLL
    //d_common->_enable(true);
  }
  else {
    v = RX_TXN;
    // Disable VCO/PLL
    //d_common->_enable(false);
  }
  return usrp()->write_io(d_which, v, mask);
}

float
wbxng_base_tx::gain_min()
{
  return usrp()->pga_max();
}

float
wbxng_base_tx::gain_max()
{
  return usrp()->pga_max() + 25.0;
}

float
wbxng_base_tx::gain_db_per_step()
{
  return 1;
}

bool
wbxng_base_tx::set_gain(float gain)
{
  /*
    Set the gain.

    @param gain:  gain in decibels
    @returns True/False
  */

  // clamp gain
  gain = std::max(gain_min(), std::min(gain, gain_max()));

  float pga_gain, agc_gain;
  float V_maxgain, V_mingain, V_fullscale, dac_value;

  float maxgain = gain_max() - usrp()->pga_max();
  float mingain = gain_min();
  if(gain > maxgain) {
    pga_gain = gain-maxgain;
    assert(pga_gain <= usrp()->pga_max());
    agc_gain = maxgain;
  }
  else {
    pga_gain = 0;
    agc_gain = gain;
  }

  V_maxgain = 0.7;
  V_mingain = 1.4;
  V_fullscale = 3.3;
  dac_value = (agc_gain*(V_maxgain-V_mingain)/(maxgain-mingain) + V_mingain)*4096/V_fullscale;

  fprintf(stderr, "TXGAIN: %f dB, Dac Code: %d, Voltage: %f\n", gain, int(dac_value), float((dac_value/4096.0)*V_fullscale));
  assert(dac_value>=0 && dac_value<4096);

  return (usrp()->write_aux_dac(d_which, 0, int(dac_value))
	  && _set_pga(int(pga_gain)));
}


/**************************************************************************/


wbxng_base_rx::wbxng_base_rx(usrp_basic_sptr _usrp, int which, int _power_on)
  : wbxng_base(_usrp, which, _power_on)
{
  /*
    @param usrp: instance of usrp.source_c
    @param which: 0 or 1 corresponding to side RX_A or RX_B respectively.
  */

  if(which == 0) {
    d_spi_enable = SPI_ENABLE_RX_A;
  }
  else {
    d_spi_enable = SPI_ENABLE_RX_B;
  }

  d_common = new adf4350(_usrp, d_which, d_spi_enable);
  
  // Disable VCO/PLL
  d_common->_enable(true);

  usrp()->_write_oe(d_which, (RX2_RX1N|RXBB_EN|ATTN_MASK|ENABLE_33|ENABLE_5), (RX2_RX1N|RXBB_EN|ATTN_MASK|ENABLE_33|ENABLE_5));
  usrp()->write_io(d_which,  (power_on()|RX2_RX1N|RXBB_EN|ENABLE_33|ENABLE_5), (RX2_RX1N|RXBB_EN|ATTN_MASK|ENABLE_33|ENABLE_5));
  fprintf(stderr,"Setting WBXNG RXBB on");

  // set up for RX on TX/RX port
  select_rx_antenna("TX/RX");

  bypass_adc_buffers(true);

  /*
  set_lo_offset(-4e6);
  */
}

wbxng_base_rx::~wbxng_base_rx()
{
  shutdown();
}

void
wbxng_base_rx::shutdown()
{
  // fprintf(stderr, "wbxng_base_rx::shutdown  d_is_shutdown = %d\n", d_is_shutdown);

  if (!d_is_shutdown){
    d_is_shutdown = true;
    // do whatever there is to do to shutdown

    // Power down VCO/PLL
    d_common->_enable(false);

    // fprintf(stderr, "wbxng_base_rx::shutdown  before _write_control\n");
    //_write_control(_compute_control_reg());

    // fprintf(stderr, "wbxng_base_rx::shutdown  before _enable_refclk\n");
    _enable_refclk(false);                       // turn off refclk

    // fprintf(stderr, "wbxng_base_rx::shutdown  before set_auto_tr\n");
    set_auto_tr(false);

    // Power down
    usrp()->write_io(d_which, power_off(), (RX2_RX1N|RXBB_EN|ATTN_MASK|ENABLE_33|ENABLE_5));

    // fprintf(stderr, "wbxng_base_rx::shutdown  after set_auto_tr\n");
  }
}

bool
wbxng_base_rx::set_auto_tr(bool on)
{
  bool ok = true;
  if(on) {
    ok &= set_atr_mask (RXBB_EN|RX2_RX1N);
    ok &= set_atr_txval(      0|RX2_RX1N);
    ok &= set_atr_rxval(RXBB_EN|       0);
  }
  else {
    ok &= set_atr_mask (0);
    ok &= set_atr_txval(0);
    ok &= set_atr_rxval(0);
  }
  return true;
}

bool
wbxng_base_rx::select_rx_antenna(int which_antenna)
{
  /*
    Specify which antenna port to use for reception.
    @param which_antenna: either 'TX/RX' or 'RX2'
  */

  if(which_antenna == 0) {
    usrp()->write_io(d_which, 0,RX2_RX1N);
  }
  else if(which_antenna == 1) {
    usrp()->write_io(d_which, RX2_RX1N, RX2_RX1N);
  }
  else {
    return false;
  }
  return true;
}

bool
wbxng_base_rx::select_rx_antenna(const std::string &which_antenna)
{
  /*
    Specify which antenna port to use for reception.
    @param which_antenna: either 'TX/RX' or 'RX2'
  */


  if(which_antenna == "TX/RX") {
    usrp()->write_io(d_which, 0, RX2_RX1N);
  }
  else if(which_antenna == "RX2") {
    usrp()->write_io(d_which, RX2_RX1N, RX2_RX1N);
  }
  else {
    return false;
  }

  return true;
}

bool
wbxng_base_rx::set_gain(float gain)
{
  /*
    Set the gain.

    @param gain:  gain in decibels
    @returns True/False
  */

  // clamp gain
  gain = std::max(gain_min(), std::min(gain, gain_max()));

  float pga_gain, agc_gain;

  float maxgain = gain_max() - usrp()->pga_max();
  float mingain = gain_min();
  if(gain > maxgain) {
    pga_gain = gain-maxgain;
    assert(pga_gain <= usrp()->pga_max());
    agc_gain = maxgain;
  }
  else {
    pga_gain = 0;
    agc_gain = gain;
  }

  return _set_attn(maxgain-agc_gain) && _set_pga(int(pga_gain));
}

bool
wbxng_base_rx::_set_attn(float attn)
{
  int attn_code = int(floor(attn/0.5));
  unsigned int iobits = (~attn_code) << ATTN_SHIFT;
  fprintf(stderr, "Attenuation: %f dB, Code: %d, IO Bits %x, Mask: %x \n", attn, attn_code, iobits & ATTN_MASK, ATTN_MASK);
  return usrp()->write_io(d_which, iobits, ATTN_MASK);
}

// ----------------------------------------------------------------

db_wbxng_tx::db_wbxng_tx(usrp_basic_sptr usrp, int which)
  : wbxng_base_tx(usrp, which)
{
}

db_wbxng_tx::~db_wbxng_tx()
{
}

db_wbxng_rx::db_wbxng_rx(usrp_basic_sptr usrp, int which)
  : wbxng_base_rx(usrp, which)
{
  set_gain((gain_min() + gain_max()) / 2.0);  // initialize gain
}

db_wbxng_rx::~db_wbxng_rx()
{
}

float
db_wbxng_rx::gain_min()
{
  return usrp()->pga_min();
}

float
db_wbxng_rx::gain_max()
{
  return usrp()->pga_max()+30.5;
}

float
db_wbxng_rx::gain_db_per_step()
{
  return 0.05;
}


bool
db_wbxng_rx::i_and_q_swapped()
{
  return false;
}