Add gruel::thread as synonym to boost::thread for convenience
[debian/gnuradio] / usrp / host / lib / db_wbxng.cc
1 //
2 // Copyright 2008,2009 Free Software Foundation, Inc.
3 //
4 // This file is part of GNU Radio
5 //
6 // GNU Radio is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either asversion 3, or (at your option)
9 // any later version.
10 //
11 // GNU Radio is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with GNU Radio; see the file COPYING.  If not, write to
18 // the Free Software Foundation, Inc., 51 Franklin Street,
19 // Boston, MA 02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <usrp/db_wbxng.h>
26 #include "db_wbxng_adf4350.h"
27 #include <db_base_impl.h>
28 #include <stdio.h>
29
30 // d'board i/o pin defs
31 // Tx and Rx have shared defs, but different i/o regs
32 #define ENABLE_5        (1 << 7)         // enables 5.0V power supply
33 #define ENABLE_33       (1 << 6)         // enables 3.3V supply
34 //#define RX_TXN          (1 << 15)         // Tx only: T/R antenna switch for TX/RX port
35 //#define RX2_RX1N        (1 << 15)         // Rx only: antenna switch between RX2 and TX/RX port
36 #define RX_TXN          ((1 << 5)|(1 << 15))         // Tx only: T/R antenna switch for TX/RX port
37 #define RX2_RX1N        ((1 << 5)|(1 << 15))         // Rx only: antenna switch between RX2 and TX/RX port
38 #define RXBB_EN         (1 << 4)
39 #define TXMOD_EN        (1 << 4)
40 #define PLL_CE          (1 << 3)
41 #define PLL_PDBRF       (1 << 2)
42 #define PLL_MUXOUT      (1 << 1)
43 #define PLL_LOCK_DETECT (1 << 0)
44
45 // RX Attenuator constants
46 #define ATTN_SHIFT      8
47 #define ATTN_MASK       (63 << ATTN_SHIFT)
48
49 wbxng_base::wbxng_base(usrp_basic_sptr _usrp, int which, int _power_on)
50   : db_base(_usrp, which), d_power_on(_power_on)
51 {
52   /*
53     @param usrp: instance of usrp.source_c
54     @param which: which side: 0 or 1 corresponding to side A or B respectively
55     @type which: int
56   */
57
58   usrp()->_write_oe(d_which, 0, 0xffff);   // turn off all outputs
59
60   d_first = true;
61   d_spi_format = SPI_FMT_MSB | SPI_FMT_HDR_0;
62
63   _enable_refclk(false);                // disable refclk
64
65   set_auto_tr(false);
66 }
67
68 wbxng_base::~wbxng_base()
69 {
70   if (d_common)
71     delete d_common;
72 }
73
74 struct freq_result_t
75 wbxng_base::set_freq(double freq)
76 {
77   /*
78     @returns (ok, actual_baseband_freq) where:
79     ok is True or False and indicates success or failure,
80     actual_baseband_freq is the RF frequency that corresponds to DC in the IF.
81   */
82
83   freq_t int_freq = freq_t(freq);
84   bool ok = d_common->_set_freq(int_freq*2);
85   double freq_result = (double) d_common->_get_freq()/2.0;
86   struct freq_result_t args = {ok, freq_result};
87
88   /* Wait before reading Lock Detect*/
89   timespec t;
90   t.tv_sec = 0;
91   t.tv_nsec = 10000000;
92   nanosleep(&t, NULL);
93
94   //fprintf(stderr,"Setting WBXNG frequency, requested %d, obtained %f, lock_detect %d\n",
95   //        int_freq, freq_result, d_common->_get_locked());
96
97   // FIXME
98   // Offsetting the LO helps get the Tx carrier leakage out of the way.
99   // This also ensures that on Rx, we're not getting hosed by the
100   // FPGA's DC removal loop's time constant.  We were seeing a
101   // problem when running with discontinuous transmission.
102   // Offsetting the LO made the problem go away.
103   //freq += d_lo_offset;
104
105   return args;
106 }
107
108 bool
109 wbxng_base::_set_pga(float pga_gain)
110 {
111   if(d_which == 0) {
112     usrp()->set_pga(0, pga_gain);
113     usrp()->set_pga(1, pga_gain);
114   }
115   else {
116     usrp()->set_pga(2, pga_gain);
117     usrp()->set_pga(3, pga_gain);
118   }
119   return true;
120 }
121
122 bool
123 wbxng_base::is_quadrature()
124 {
125   /*
126     Return True if this board requires both I & Q analog channels.
127
128     This bit of info is useful when setting up the USRP Rx mux register.
129   */
130   return true;
131 }
132
133 double
134 wbxng_base::freq_min()
135 {
136   return (double) d_common->_get_min_freq()/2.0;
137 }
138
139 double
140 wbxng_base::freq_max()
141 {
142   return (double) d_common->_get_max_freq()/2.0;
143 }
144
145 // ----------------------------------------------------------------
146
147 wbxng_base_tx::wbxng_base_tx(usrp_basic_sptr _usrp, int which, int _power_on)
148   : wbxng_base(_usrp, which, _power_on)
149 {
150   /*
151     @param usrp: instance of usrp.sink_c
152     @param which: 0 or 1 corresponding to side TX_A or TX_B respectively.
153   */
154
155   if(which == 0) {
156     d_spi_enable = SPI_ENABLE_TX_A;
157   }
158   else {
159     d_spi_enable = SPI_ENABLE_TX_B;
160   }
161
162   d_common = new adf4350(_usrp, d_which, d_spi_enable);
163
164   // power up the transmit side, but don't enable the mixer
165   usrp()->_write_oe(d_which,(RX_TXN|TXMOD_EN|ENABLE_33|ENABLE_5), (RX_TXN|TXMOD_EN|ENABLE_33|ENABLE_5));
166   usrp()->write_io(d_which, (power_on()|RX_TXN|ENABLE_33|ENABLE_5), (RX_TXN|ENABLE_33|ENABLE_5));
167   //set_lo_offset(4e6);
168   
169   // Disable VCO/PLL
170   d_common->_enable(true);
171
172   set_gain((gain_min() + gain_max()) / 2.0);  // initialize gain
173 }
174
175 wbxng_base_tx::~wbxng_base_tx()
176 {
177   shutdown();
178 }
179
180
181 void
182 wbxng_base_tx::shutdown()
183 {
184   // fprintf(stderr, "wbxng_base_tx::shutdown  d_is_shutdown = %d\n", d_is_shutdown);
185
186   if (!d_is_shutdown){
187     d_is_shutdown = true;
188     // do whatever there is to do to shutdown
189
190     // Disable VCO/PLL
191     d_common->_enable(false);
192
193     // Power down and leave the T/R switch in the R position
194     usrp()->write_io(d_which, (power_off()|RX_TXN), (RX_TXN|ENABLE_33|ENABLE_5));
195
196
197     /*
198     _write_control(_compute_control_reg());
199     */
200     _enable_refclk(false);                       // turn off refclk
201     set_auto_tr(false);
202   }
203 }
204
205 bool
206 wbxng_base_tx::set_auto_tr(bool on)
207 {
208   bool ok = true;
209   if(on) {
210     ok &= set_atr_mask (RX_TXN | TXMOD_EN);
211     ok &= set_atr_txval(0      | TXMOD_EN);
212     ok &= set_atr_rxval(RX_TXN);
213   }
214   else {
215     ok &= set_atr_mask (0);
216     ok &= set_atr_txval(0);
217     ok &= set_atr_rxval(0);
218   }
219   return ok;
220 }
221
222 bool
223 wbxng_base_tx::set_enable(bool on)
224 {
225   /*
226     Enable transmitter if on is true
227   */
228
229   int v;
230   int mask = RX_TXN | TXMOD_EN;
231   if(on) {
232     v = TXMOD_EN;
233     // Enable VCO/PLL
234     //d_common->_enable(true);
235   }
236   else {
237     v = RX_TXN;
238     // Disable VCO/PLL
239     //d_common->_enable(false);
240   }
241   return usrp()->write_io(d_which, v, mask);
242 }
243
244 float
245 wbxng_base_tx::gain_min()
246 {
247   return 0.0;
248 }
249
250 float
251 wbxng_base_tx::gain_max()
252 {
253   return 25.0;
254 }
255
256 float
257 wbxng_base_tx::gain_db_per_step()
258 {
259   return gain_max()/(1+(1.4-0.5)*4096/3.3);
260 }
261
262 bool
263 wbxng_base_tx::set_gain(float gain)
264 {
265   /*
266     Set the gain.
267
268     @param gain:  gain in decibels
269     @returns True/False
270   */
271
272   // clamp gain
273   gain = std::max(gain_min(), std::min(gain, gain_max()));
274
275   float pga_gain, agc_gain;
276   float V_maxgain, V_mingain, V_fullscale, dac_value;
277
278   float maxgain = gain_max();
279   float mingain = gain_min();
280   pga_gain = 0;
281   agc_gain = gain;
282
283   V_maxgain = 0.5;
284   V_mingain = 1.4;
285   V_fullscale = 3.3;
286   dac_value = (agc_gain*(V_maxgain-V_mingain)/(maxgain-mingain) + V_mingain)*4096/V_fullscale;
287
288   //fprintf(stderr, "TXGAIN: %f dB, Dac Code: %d, Voltage: %f\n", gain, int(dac_value), float((dac_value/4096.0)*V_fullscale));
289   assert(dac_value>=0 && dac_value<4096);
290
291   return (usrp()->write_aux_dac(d_which, 0, int(dac_value))
292      && _set_pga(usrp()->pga_max()));
293
294 }
295
296
297 /**************************************************************************/
298
299
300 wbxng_base_rx::wbxng_base_rx(usrp_basic_sptr _usrp, int which, int _power_on)
301   : wbxng_base(_usrp, which, _power_on)
302 {
303   /*
304     @param usrp: instance of usrp.source_c
305     @param which: 0 or 1 corresponding to side RX_A or RX_B respectively.
306   */
307
308   if(which == 0) {
309     d_spi_enable = SPI_ENABLE_RX_A;
310   }
311   else {
312     d_spi_enable = SPI_ENABLE_RX_B;
313   }
314
315   d_common = new adf4350(_usrp, d_which, d_spi_enable);
316   
317   // Disable VCO/PLL
318   d_common->_enable(true);
319
320   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));
321   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));
322   //fprintf(stderr,"Setting WBXNG RXBB on");
323
324   // set up for RX on TX/RX port
325   select_rx_antenna("TX/RX");
326
327   bypass_adc_buffers(true);
328
329   /*
330   set_lo_offset(-4e6);
331   */
332 }
333
334 wbxng_base_rx::~wbxng_base_rx()
335 {
336   shutdown();
337 }
338
339 void
340 wbxng_base_rx::shutdown()
341 {
342   // fprintf(stderr, "wbxng_base_rx::shutdown  d_is_shutdown = %d\n", d_is_shutdown);
343
344   if (!d_is_shutdown){
345     d_is_shutdown = true;
346     // do whatever there is to do to shutdown
347
348     // Power down VCO/PLL
349     d_common->_enable(false);
350
351     // fprintf(stderr, "wbxng_base_rx::shutdown  before _write_control\n");
352     //_write_control(_compute_control_reg());
353
354     // fprintf(stderr, "wbxng_base_rx::shutdown  before _enable_refclk\n");
355     _enable_refclk(false);                       // turn off refclk
356
357     // fprintf(stderr, "wbxng_base_rx::shutdown  before set_auto_tr\n");
358     set_auto_tr(false);
359
360     // Power down
361     usrp()->write_io(d_which, power_off(), (RX2_RX1N|RXBB_EN|ATTN_MASK|ENABLE_33|ENABLE_5));
362
363     // fprintf(stderr, "wbxng_base_rx::shutdown  after set_auto_tr\n");
364   }
365 }
366
367 bool
368 wbxng_base_rx::set_auto_tr(bool on)
369 {
370   bool ok = true;
371   if(on) {
372     ok &= set_atr_mask (RXBB_EN|RX2_RX1N);
373     ok &= set_atr_txval(      0|RX2_RX1N);
374     ok &= set_atr_rxval(RXBB_EN|       0);
375   }
376   else {
377     ok &= set_atr_mask (0);
378     ok &= set_atr_txval(0);
379     ok &= set_atr_rxval(0);
380   }
381   return true;
382 }
383
384 bool
385 wbxng_base_rx::select_rx_antenna(int which_antenna)
386 {
387   /*
388     Specify which antenna port to use for reception.
389     @param which_antenna: either 'TX/RX' or 'RX2'
390   */
391
392   if(which_antenna == 0) {
393     usrp()->write_io(d_which, 0,RX2_RX1N);
394   }
395   else if(which_antenna == 1) {
396     usrp()->write_io(d_which, RX2_RX1N, RX2_RX1N);
397   }
398   else {
399     return false;
400   }
401   return true;
402 }
403
404 bool
405 wbxng_base_rx::select_rx_antenna(const std::string &which_antenna)
406 {
407   /*
408     Specify which antenna port to use for reception.
409     @param which_antenna: either 'TX/RX' or 'RX2'
410   */
411
412
413   if(which_antenna == "TX/RX") {
414     usrp()->write_io(d_which, 0, RX2_RX1N);
415   }
416   else if(which_antenna == "RX2") {
417     usrp()->write_io(d_which, RX2_RX1N, RX2_RX1N);
418   }
419   else {
420     return false;
421   }
422
423   return true;
424 }
425
426 bool
427 wbxng_base_rx::set_gain(float gain)
428 {
429   /*
430     Set the gain.
431
432     @param gain:  gain in decibels
433     @returns True/False
434   */
435
436   // clamp gain
437   gain = std::max(gain_min(), std::min(gain, gain_max()));
438
439   float pga_gain, agc_gain;
440
441   float maxgain = gain_max() - usrp()->pga_max();
442   float mingain = gain_min();
443   if(gain > maxgain) {
444     pga_gain = gain-maxgain;
445     assert(pga_gain <= usrp()->pga_max());
446     agc_gain = maxgain;
447   }
448   else {
449     pga_gain = 0;
450     agc_gain = gain;
451   }
452
453   return _set_attn(maxgain-agc_gain) && _set_pga(int(pga_gain));
454 }
455
456 bool
457 wbxng_base_rx::_set_attn(float attn)
458 {
459   int attn_code = int(floor(attn/0.5));
460   unsigned int iobits = (~attn_code) << ATTN_SHIFT;
461   //fprintf(stderr, "Attenuation: %f dB, Code: %d, IO Bits %x, Mask: %x \n", attn, attn_code, iobits & ATTN_MASK, ATTN_MASK);
462   return usrp()->write_io(d_which, iobits, ATTN_MASK);
463 }
464
465 // ----------------------------------------------------------------
466
467 db_wbxng_tx::db_wbxng_tx(usrp_basic_sptr usrp, int which)
468   : wbxng_base_tx(usrp, which)
469 {
470 }
471
472 db_wbxng_tx::~db_wbxng_tx()
473 {
474 }
475
476 db_wbxng_rx::db_wbxng_rx(usrp_basic_sptr usrp, int which)
477   : wbxng_base_rx(usrp, which)
478 {
479   set_gain((gain_min() + gain_max()) / 2.0);  // initialize gain
480 }
481
482 db_wbxng_rx::~db_wbxng_rx()
483 {
484 }
485
486 float
487 db_wbxng_rx::gain_min()
488 {
489   return usrp()->pga_min();
490 }
491
492 float
493 db_wbxng_rx::gain_max()
494 {
495   return usrp()->pga_max()+30.5;
496 }
497
498 float
499 db_wbxng_rx::gain_db_per_step()
500 {
501   return 0.05;
502 }
503
504
505 bool
506 db_wbxng_rx::i_and_q_swapped()
507 {
508   return false;
509 }