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