Clean up annoying class structure in wbx
[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)
50   : db_base(_usrp, which)
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 int
75 wbxng_base::_refclk_divisor()
76 {
77   return 1;
78 }
79
80 struct freq_result_t
81 wbxng_base::set_freq(double freq)
82 {
83   /*
84     @returns (ok, actual_baseband_freq) where:
85     ok is True or False and indicates success or failure,
86     actual_baseband_freq is the RF frequency that corresponds to DC in the IF.
87   */
88
89   // clamp freq
90   freq_t int_freq = freq_t(std::max(freq_min(), std::min(freq, freq_max())));
91
92   bool ok = d_common->_set_freq(int_freq*2, _refclk_freq());
93   double freq_result = (double) d_common->_get_freq(_refclk_freq())/2.0;
94   struct freq_result_t args = {ok, freq_result};
95
96   /* Wait before reading Lock Detect*/
97   timespec t;
98   t.tv_sec = 0;
99   t.tv_nsec = 10000000;
100   nanosleep(&t, NULL);
101
102   //fprintf(stderr,"Setting WBXNG frequency, requested %d, obtained %f, lock_detect %d\n",
103   //        int_freq, freq_result, d_common->_get_locked());
104
105   // FIXME
106   // Offsetting the LO helps get the Tx carrier leakage out of the way.
107   // This also ensures that on Rx, we're not getting hosed by the
108   // FPGA's DC removal loop's time constant.  We were seeing a
109   // problem when running with discontinuous transmission.
110   // Offsetting the LO made the problem go away.
111   //freq += d_lo_offset;
112
113   return args;
114 }
115
116 bool
117 wbxng_base::_set_pga(float pga_gain)
118 {
119   if(d_which == 0) {
120     usrp()->set_pga(0, pga_gain);
121     usrp()->set_pga(1, pga_gain);
122   }
123   else {
124     usrp()->set_pga(2, pga_gain);
125     usrp()->set_pga(3, pga_gain);
126   }
127   return true;
128 }
129
130 bool
131 wbxng_base::is_quadrature()
132 {
133   /*
134     Return True if this board requires both I & Q analog channels.
135
136     This bit of info is useful when setting up the USRP Rx mux register.
137   */
138   return true;
139 }
140
141 double
142 wbxng_base::freq_min()
143 {
144   return (double) d_common->_get_min_freq()/2.0;
145 }
146
147 double
148 wbxng_base::freq_max()
149 {
150   return (double) d_common->_get_max_freq()/2.0;
151 }
152
153 // ----------------------------------------------------------------
154
155 db_wbxng_tx::db_wbxng_tx(usrp_basic_sptr _usrp, int which)
156   : wbxng_base(_usrp, which)
157 {
158   /*
159     @param usrp: instance of usrp.sink_c
160     @param which: 0 or 1 corresponding to side TX_A or TX_B respectively.
161   */
162
163   if(which == 0) {
164     d_spi_enable = SPI_ENABLE_TX_A;
165   }
166   else {
167     d_spi_enable = SPI_ENABLE_TX_B;
168   }
169
170   d_common = new adf4350(_usrp, d_which, d_spi_enable);
171
172   // power up the transmit side, but don't enable the mixer
173   usrp()->_write_oe(d_which,(RX_TXN|TXMOD_EN|ENABLE_33|ENABLE_5), (RX_TXN|TXMOD_EN|ENABLE_33|ENABLE_5));
174   usrp()->write_io(d_which, (RX_TXN|ENABLE_33|ENABLE_5), (RX_TXN|ENABLE_33|ENABLE_5));
175   //set_lo_offset(4e6);
176   
177   // Disable VCO/PLL
178   d_common->_enable(true);
179
180   set_gain((gain_min() + gain_max()) / 2.0);  // initialize gain
181 }
182
183 db_wbxng_tx::~db_wbxng_tx()
184 {
185   shutdown();
186 }
187
188
189 void
190 db_wbxng_tx::shutdown()
191 {
192   // fprintf(stderr, "db_wbxng_tx::shutdown  d_is_shutdown = %d\n", d_is_shutdown);
193
194   if (!d_is_shutdown){
195     d_is_shutdown = true;
196     // do whatever there is to do to shutdown
197
198     // Disable VCO/PLL
199     d_common->_enable(false);
200
201     // Power down and leave the T/R switch in the R position
202     usrp()->write_io(d_which, (RX_TXN), (RX_TXN|ENABLE_33|ENABLE_5));
203
204
205     /*
206     _write_control(_compute_control_reg());
207     */
208     _enable_refclk(false);                       // turn off refclk
209     set_auto_tr(false);
210   }
211 }
212
213 bool
214 db_wbxng_tx::set_auto_tr(bool on)
215 {
216   bool ok = true;
217   if(on) {
218     ok &= set_atr_mask (RX_TXN | TXMOD_EN);
219     ok &= set_atr_txval(0      | TXMOD_EN);
220     ok &= set_atr_rxval(RX_TXN);
221   }
222   else {
223     ok &= set_atr_mask (0);
224     ok &= set_atr_txval(0);
225     ok &= set_atr_rxval(0);
226   }
227   return ok;
228 }
229
230 bool
231 db_wbxng_tx::set_enable(bool on)
232 {
233   /*
234     Enable transmitter if on is true
235   */
236
237   int v;
238   int mask = RX_TXN | TXMOD_EN;
239   if(on) {
240     v = TXMOD_EN;
241     // Enable VCO/PLL
242     //d_common->_enable(true);
243   }
244   else {
245     v = RX_TXN;
246     // Disable VCO/PLL
247     //d_common->_enable(false);
248   }
249   return usrp()->write_io(d_which, v, mask);
250 }
251
252 float
253 db_wbxng_tx::gain_min()
254 {
255   return 0.0;
256 }
257
258 float
259 db_wbxng_tx::gain_max()
260 {
261   return 25.0;
262 }
263
264 float
265 db_wbxng_tx::gain_db_per_step()
266 {
267   return gain_max()/(1+(1.4-0.5)*4096/3.3);
268 }
269
270 bool
271 db_wbxng_tx::set_gain(float gain)
272 {
273   /*
274     Set the gain.
275
276     @param gain:  gain in decibels
277     @returns True/False
278   */
279
280   // clamp gain
281   gain = std::max(gain_min(), std::min(gain, gain_max()));
282
283   float pga_gain, agc_gain;
284   float V_maxgain, V_mingain, V_fullscale, dac_value;
285
286   float maxgain = gain_max();
287   float mingain = gain_min();
288   pga_gain = 0;
289   agc_gain = gain;
290
291   V_maxgain = 0.5;
292   V_mingain = 1.4;
293   V_fullscale = 3.3;
294   dac_value = (agc_gain*(V_maxgain-V_mingain)/(maxgain-mingain) + V_mingain)*4096/V_fullscale;
295
296   //fprintf(stderr, "TXGAIN: %f dB, Dac Code: %d, Voltage: %f\n", gain, int(dac_value), float((dac_value/4096.0)*V_fullscale));
297   assert(dac_value>=0 && dac_value<4096);
298
299   return (usrp()->write_aux_dac(d_which, 0, int(dac_value))
300      && _set_pga(usrp()->pga_max()));
301
302 }
303
304
305 /**************************************************************************/
306
307
308 db_wbxng_rx::db_wbxng_rx(usrp_basic_sptr _usrp, int which)
309   : wbxng_base(_usrp, which)
310 {
311   /*
312     @param usrp: instance of usrp.source_c
313     @param which: 0 or 1 corresponding to side RX_A or RX_B respectively.
314   */
315
316   if(which == 0) {
317     d_spi_enable = SPI_ENABLE_RX_A;
318   }
319   else {
320     d_spi_enable = SPI_ENABLE_RX_B;
321   }
322
323   d_common = new adf4350(_usrp, d_which, d_spi_enable);
324   
325   // Disable VCO/PLL
326   d_common->_enable(true);
327
328   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));
329   usrp()->write_io(d_which,  (RX2_RX1N|RXBB_EN|ENABLE_33|ENABLE_5), (RX2_RX1N|RXBB_EN|ATTN_MASK|ENABLE_33|ENABLE_5));
330   //fprintf(stderr,"Setting WBXNG RXBB on");
331
332   // set up for RX on TX/RX port
333   select_rx_antenna("TX/RX");
334
335   bypass_adc_buffers(true);
336
337   /*
338   set_lo_offset(-4e6);
339   */
340
341   set_gain((gain_min() + gain_max()) / 2.0);  // initialize gain
342 }
343
344 db_wbxng_rx::~db_wbxng_rx()
345 {
346   shutdown();
347 }
348
349 void
350 db_wbxng_rx::shutdown()
351 {
352   // fprintf(stderr, "db_wbxng_rx::shutdown  d_is_shutdown = %d\n", d_is_shutdown);
353
354   if (!d_is_shutdown){
355     d_is_shutdown = true;
356     // do whatever there is to do to shutdown
357
358     // Power down VCO/PLL
359     d_common->_enable(false);
360
361     // fprintf(stderr, "db_wbxng_rx::shutdown  before _write_control\n");
362     //_write_control(_compute_control_reg());
363
364     // fprintf(stderr, "db_wbxng_rx::shutdown  before _enable_refclk\n");
365     _enable_refclk(false);                       // turn off refclk
366
367     // fprintf(stderr, "db_wbxng_rx::shutdown  before set_auto_tr\n");
368     set_auto_tr(false);
369
370     // Power down
371     usrp()->write_io(d_which, 0, (RX2_RX1N|RXBB_EN|ATTN_MASK|ENABLE_33|ENABLE_5));
372
373     // fprintf(stderr, "db_wbxng_rx::shutdown  after set_auto_tr\n");
374   }
375 }
376
377 bool
378 db_wbxng_rx::set_auto_tr(bool on)
379 {
380   bool ok = true;
381   if(on) {
382     ok &= set_atr_mask (RXBB_EN|RX2_RX1N);
383     ok &= set_atr_txval(      0|RX2_RX1N);
384     ok &= set_atr_rxval(RXBB_EN|       0);
385   }
386   else {
387     ok &= set_atr_mask (0);
388     ok &= set_atr_txval(0);
389     ok &= set_atr_rxval(0);
390   }
391   return true;
392 }
393
394 bool
395 db_wbxng_rx::select_rx_antenna(int which_antenna)
396 {
397   /*
398     Specify which antenna port to use for reception.
399     @param which_antenna: either 'TX/RX' or 'RX2'
400   */
401
402   if(which_antenna == 0) {
403     usrp()->write_io(d_which, 0,RX2_RX1N);
404   }
405   else if(which_antenna == 1) {
406     usrp()->write_io(d_which, RX2_RX1N, RX2_RX1N);
407   }
408   else {
409     return false;
410   }
411   return true;
412 }
413
414 bool
415 db_wbxng_rx::select_rx_antenna(const std::string &which_antenna)
416 {
417   /*
418     Specify which antenna port to use for reception.
419     @param which_antenna: either 'TX/RX' or 'RX2'
420   */
421
422
423   if(which_antenna == "TX/RX") {
424     usrp()->write_io(d_which, 0, RX2_RX1N);
425   }
426   else if(which_antenna == "RX2") {
427     usrp()->write_io(d_which, RX2_RX1N, RX2_RX1N);
428   }
429   else {
430     return false;
431   }
432
433   return true;
434 }
435
436 bool
437 db_wbxng_rx::set_gain(float gain)
438 {
439   /*
440     Set the gain.
441
442     @param gain:  gain in decibels
443     @returns True/False
444   */
445
446   // clamp gain
447   gain = std::max(gain_min(), std::min(gain, gain_max()));
448
449   float pga_gain, agc_gain;
450
451   float maxgain = gain_max() - usrp()->pga_max();
452   float mingain = gain_min();
453   if(gain > maxgain) {
454     pga_gain = gain-maxgain;
455     assert(pga_gain <= usrp()->pga_max());
456     agc_gain = maxgain;
457   }
458   else {
459     pga_gain = 0;
460     agc_gain = gain;
461   }
462
463   return _set_attn(maxgain-agc_gain) && _set_pga(int(pga_gain));
464 }
465
466 bool
467 db_wbxng_rx::_set_attn(float attn)
468 {
469   int attn_code = int(floor(attn/0.5));
470   unsigned int iobits = (~attn_code) << ATTN_SHIFT;
471   //fprintf(stderr, "Attenuation: %f dB, Code: %d, IO Bits %x, Mask: %x \n", attn, attn_code, iobits & ATTN_MASK, ATTN_MASK);
472   return usrp()->write_io(d_which, iobits, ATTN_MASK);
473 }
474
475 float
476 db_wbxng_rx::gain_min()
477 {
478   return usrp()->pga_min();
479 }
480
481 float
482 db_wbxng_rx::gain_max()
483 {
484   return usrp()->pga_max()+30.5;
485 }
486
487 float
488 db_wbxng_rx::gain_db_per_step()
489 {
490   return 0.05;
491 }
492
493 bool
494 db_wbxng_rx::i_and_q_swapped()
495 {
496   return false;
497 }