Merged r10071:10164 from features/cppdb-test into trunk. Implements the fully native...
[debian/gnuradio] / usrp / host / lib / legacy / usrp_standard.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 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 #include <usrp_standard.h>
24
25 #include "usrp_prims.h"
26 #include "fpga_regs_common.h"
27 #include "fpga_regs_standard.h"
28 #include <stdexcept>
29 #include <assert.h>
30 #include <math.h>
31 #include <ad9862.h>
32
33
34 static const int OLD_CAPS_VAL = 0xaa55ff77;
35 static const int DEFAULT_CAPS_VAL = ((2 << bmFR_RB_CAPS_NDUC_SHIFT)
36                                      | (2 << bmFR_RB_CAPS_NDDC_SHIFT)
37                                      | bmFR_RB_CAPS_RX_HAS_HALFBAND);
38
39 // #define USE_FPGA_TX_CORDIC
40
41
42 using namespace ad9862;
43
44 #define NELEM(x) (sizeof (x) / sizeof (x[0]))
45
46
47 void
48 usrp_standard_common::calc_dxc_freq(double target_freq, double baseband_freq, double fs,
49                                     double *dxc_freq, bool *inverted)
50 {
51   /*
52     Calculate the frequency to use for setting the digital up or down converter.
53     
54     @param target_freq: desired RF frequency (Hz)
55     @param baseband_freq: the RF frequency that corresponds to DC in the IF.
56     @param fs: converter sample rate
57     
58     @returns: 2-tuple (ddc_freq, inverted) where ddc_freq is the value
59       for the ddc and inverted is True if we're operating in an inverted
60       Nyquist zone.
61   */
62
63 #if 0
64     printf("calc_dxc_freq:\n");
65     printf("  target   = %f\n", target_freq);
66     printf("  baseband = %f\n", baseband_freq);
67     printf("  fs       = %f\n", fs);
68 #endif
69
70   double delta = target_freq - baseband_freq;
71     
72   if(delta >= 0) {
73     while(delta > fs) {
74       delta -= fs;
75     }
76     if(delta <= fs/2) {         // non-inverted region
77       *dxc_freq = -delta;       
78       *inverted = false;
79     }
80     else {                      // inverted region
81       *dxc_freq = delta - fs;
82       *inverted = true;
83     }
84   }
85   else {
86     while(delta < -fs) {
87       delta += fs;
88     }
89     if(delta >= -fs/2) {
90       *dxc_freq = -delta;       // non-inverted region
91       *inverted = false;
92     }
93     else {                      // inverted region
94       *dxc_freq = delta + fs;
95       *inverted = true;
96     }
97   }
98
99 #if 0
100     printf("  dxc_freq  = %f\n", *dxc_freq);
101     printf("  inverted  = %s\n", *inverted ? "true" : "false");
102 #endif
103 }
104
105
106 /* 
107  * Real lambda expressions would be _so_ much easier...
108  */
109 class dxc_control {
110 public:
111   virtual bool is_tx() = 0;
112   virtual bool set_dxc_freq(double dxc_freq) = 0;
113   virtual double dxc_freq() = 0;
114 };
115
116 class ddc_control : public dxc_control {
117   usrp_standard_rx     *d_u;
118   int                   d_chan;
119
120 public:
121   ddc_control(usrp_standard_rx *u, int chan)
122     : d_u(u), d_chan(chan) {}
123   
124   bool is_tx(){ return false; }
125   bool set_dxc_freq(double dxc_freq){ return d_u->set_rx_freq(d_chan, dxc_freq); }
126   double dxc_freq(){ return d_u->rx_freq(d_chan); }
127 };
128
129 class duc_control : public dxc_control {
130   usrp_standard_tx     *d_u;
131   int                   d_chan;
132
133 public:
134   duc_control(usrp_standard_tx *u, int chan)
135     : d_u(u), d_chan(chan) {}
136   
137   bool is_tx(){ return true; }
138   bool set_dxc_freq(double dxc_freq){ return d_u->set_tx_freq(d_chan, dxc_freq); }
139   double dxc_freq() { return d_u->tx_freq(d_chan); }
140 };
141
142
143 /*!
144  * \brief Tune such that target_frequency ends up at DC in the complex baseband
145  *
146  * \param db            the daughterboard to use
147  * \param target_freq   the center frequency we want at baseband (DC)
148  * \param fs            the sample rate
149  * \param dxc           DDC or DUC access and control object
150  * \param[out] result   details of what we did
151  *
152  * \returns true iff operation was successful
153  *
154  * Tuning is a two step process.  First we ask the front-end to
155  * tune as close to the desired frequency as it can.  Then we use
156  * the result of that operation and our target_frequency to
157  * determine the value for the digital down converter.
158  */
159 static bool
160 tune_a_helper(db_base_sptr db, double target_freq, double fs,
161               dxc_control &dxc, usrp_tune_result *result)
162 {
163   bool inverted = false;
164   double dxc_freq;
165   double actual_dxc_freq;
166
167   // Ask the d'board to tune as closely as it can to target_freq
168 #if 0
169   bool ok = db->set_freq(target_freq, &result->baseband_freq);
170 #else
171   bool ok;
172   {
173     freq_result_t fr = db->set_freq(target_freq);
174     ok = fr.ok;
175     result->baseband_freq = fr.baseband_freq;
176   }
177 #endif
178
179   // Calculate the DDC setting that will downconvert the baseband from the
180   // daughterboard to our target frequency.
181   usrp_standard_common::calc_dxc_freq(target_freq, result->baseband_freq, fs,
182                                       &dxc_freq, &inverted);
183
184   // If the spectrum is inverted, and the daughterboard doesn't do
185   // quadrature downconversion, we can fix the inversion by flipping the
186   // sign of the dxc_freq...  (This only happens using the basic_rx board)
187   
188   if(db->spectrum_inverted())
189     inverted = !inverted;
190   
191   if(inverted && !db->is_quadrature()){
192     dxc_freq = -dxc_freq;
193     inverted = !inverted;
194   }
195   
196   if (dxc.is_tx())              // down conversion versus up conversion
197     dxc_freq = -dxc_freq;
198
199   ok &= dxc.set_dxc_freq(dxc_freq);
200   actual_dxc_freq = dxc.dxc_freq();
201   
202   result->dxc_freq = dxc_freq;
203   result->residual_freq = dxc_freq - actual_dxc_freq;
204   result->inverted = inverted;
205   return ok;
206 }
207
208
209 static unsigned int
210 compute_freq_control_word_fpga (double master_freq, double target_freq,
211                                 double *actual_freq, bool verbose)
212 {
213   static const int NBITS = 14;
214   
215   int   v = (int) rint (target_freq / master_freq * pow (2.0, 32.0));
216
217   if (0)
218     v = (v >> (32 - NBITS)) << (32 - NBITS);    // keep only top NBITS
219
220   *actual_freq = v * master_freq / pow (2.0, 32.0);
221
222   if (verbose)
223     fprintf (stderr,
224              "compute_freq_control_word_fpga: target = %g  actual = %g  delta = %g\n",
225              target_freq, *actual_freq, *actual_freq - target_freq);
226
227   return (unsigned int) v;
228 }
229
230 // The 9862 uses an unsigned 24-bit frequency tuning word and 
231 // a separate register to control the sign.
232
233 static unsigned int
234 compute_freq_control_word_9862 (double master_freq, double target_freq,
235                                 double *actual_freq, bool verbose)
236 {
237   double sign = 1.0;
238
239   if (target_freq < 0)
240     sign = -1.0;
241
242   int   v = (int) rint (fabs (target_freq) / master_freq * pow (2.0, 24.0));
243   *actual_freq = v * master_freq / pow (2.0, 24.0) * sign;
244
245   if (verbose)
246     fprintf (stderr,
247      "compute_freq_control_word_9862: target = %g  actual = %g  delta = %g  v = %8d\n",
248      target_freq, *actual_freq, *actual_freq - target_freq, v);
249
250   return (unsigned int) v;
251 }
252
253 // ----------------------------------------------------------------
254
255 usrp_standard_common::usrp_standard_common(usrp_basic *parent)
256 {
257   // read new FPGA capability register
258   if (!parent->_read_fpga_reg(FR_RB_CAPS, &d_fpga_caps)){
259     fprintf (stderr, "usrp_standard_common: failed to read FPGA cap register.\n");
260     throw std::runtime_error ("usrp_standard_common::ctor");
261   }
262   // If we don't have the cap register, set the value to what it would
263   // have had if we did have one ;)
264   if (d_fpga_caps == OLD_CAPS_VAL)
265     d_fpga_caps = DEFAULT_CAPS_VAL;
266
267   if (0){
268     fprintf(stdout, "has_rx_halfband = %d\n", has_rx_halfband());
269     fprintf(stdout, "nddcs           = %d\n", nddcs());
270     fprintf(stdout, "has_tx_halfband = %d\n", has_tx_halfband());
271     fprintf(stdout, "nducs           = %d\n", nducs());
272   }
273 }
274
275 bool
276 usrp_standard_common::has_rx_halfband() const
277 {
278   return (d_fpga_caps & bmFR_RB_CAPS_RX_HAS_HALFBAND) ? true : false;
279 }
280
281 int
282 usrp_standard_common::nddcs() const
283 {
284   return (d_fpga_caps & bmFR_RB_CAPS_NDDC_MASK) >> bmFR_RB_CAPS_NDDC_SHIFT;
285 }
286
287 bool
288 usrp_standard_common::has_tx_halfband() const
289 {
290   return (d_fpga_caps & bmFR_RB_CAPS_TX_HAS_HALFBAND) ? true : false;
291 }
292
293 int
294 usrp_standard_common::nducs() const
295 {
296   return (d_fpga_caps & bmFR_RB_CAPS_NDUC_MASK) >> bmFR_RB_CAPS_NDUC_SHIFT;
297 }
298
299 // ----------------------------------------------------------------
300
301 static int 
302 real_rx_mux_value (int mux, int nchan)
303 {
304   if (mux != -1)
305     return mux;
306
307   return 0x32103210;
308 }
309
310 usrp_standard_rx::usrp_standard_rx (int which_board,
311                                     unsigned int decim_rate,
312                                     int nchan, int mux, int mode,
313                                     int fusb_block_size, int fusb_nblocks,
314                                     const std::string fpga_filename,
315                                     const std::string firmware_filename
316                                     )
317   : usrp_basic_rx (which_board, fusb_block_size, fusb_nblocks,
318                    fpga_filename, firmware_filename),
319     usrp_standard_common(this),
320     d_nchan (1), d_sw_mux (0x0), d_hw_mux (0x0)
321 {
322   if (!set_format(make_format())){
323     fprintf (stderr, "usrp_standard_rx: set_format failed\n");
324     throw std::runtime_error ("usrp_standard_rx::ctor");
325   }
326   if (!set_nchannels (nchan)){
327     fprintf (stderr, "usrp_standard_rx: set_nchannels failed\n");
328     throw std::runtime_error ("usrp_standard_rx::ctor");
329   }
330   if (!set_decim_rate (decim_rate)){
331     fprintf (stderr, "usrp_standard_rx: set_decim_rate failed\n");
332     throw std::runtime_error ("usrp_standard_rx::ctor");
333   }
334   if (!set_mux (real_rx_mux_value (mux, nchan))){
335     fprintf (stderr, "usrp_standard_rx: set_mux failed\n");
336     throw std::runtime_error ("usrp_standard_rx::ctor");
337   }
338   if (!set_fpga_mode (mode)){
339     fprintf (stderr, "usrp_standard_rx: set_fpga_mode failed\n");
340     throw std::runtime_error ("usrp_standard_rx::ctor");
341   }
342
343   for (int i = 0; i < MAX_CHAN; i++){
344     set_rx_freq(i, 0);
345     set_ddc_phase(i, 0);
346   }
347 }
348
349 usrp_standard_rx::~usrp_standard_rx ()
350 {
351   // fprintf(stderr, "\nusrp_standard_rx: dtor\n");
352 }
353
354 bool
355 usrp_standard_rx::start ()
356 {
357   if (!usrp_basic_rx::start ())
358     return false;
359
360   // add our code here
361
362   return true;
363 }
364
365 bool
366 usrp_standard_rx::stop ()
367 {
368   bool ok = usrp_basic_rx::stop ();
369
370   // add our code here
371
372   return ok;
373 }
374
375 usrp_standard_rx_sptr
376 usrp_standard_rx::make (int which_board,
377                         unsigned int decim_rate,
378                         int nchan, int mux, int mode,
379                         int fusb_block_size, int fusb_nblocks,
380                         const std::string fpga_filename,
381                         const std::string firmware_filename
382                         )
383 {
384   try {
385     usrp_standard_rx_sptr u = 
386       usrp_standard_rx_sptr(new usrp_standard_rx(which_board, decim_rate,
387                                                  nchan, mux, mode,
388                                                  fusb_block_size, fusb_nblocks,
389                                                  fpga_filename, firmware_filename));
390     u->init_db(u);
391     return u;
392   }
393   catch (...){
394     return usrp_standard_rx_sptr();
395   }
396 }
397
398 bool
399 usrp_standard_rx::set_decim_rate(unsigned int rate)
400 {
401   if (has_rx_halfband()){
402     if ((rate & 0x1) || rate < 4 || rate > 256){
403       fprintf (stderr, "usrp_standard_rx::set_decim_rate: rate must be EVEN and in [4, 256]\n");
404       return false;
405     }
406   }
407   else {
408     if (rate < 4 || rate > 128){
409       fprintf (stderr, "usrp_standard_rx::set_decim_rate: rate must be in [4, 128]\n");
410       return false;
411     }
412   }
413
414   d_decim_rate = rate;
415   set_usb_data_rate ((adc_rate () / rate * nchannels ())
416                      * (2 * sizeof (short)));
417
418   bool s = disable_rx ();
419   int v = has_rx_halfband() ? d_decim_rate/2 - 1 : d_decim_rate - 1;
420   bool ok = _write_fpga_reg (FR_DECIM_RATE, v);
421   restore_rx (s);
422   return ok;
423 }
424
425 bool usrp_standard_rx::set_nchannels (int nchan)
426 {
427   if (!(nchan == 1 || nchan == 2 || nchan == 4))
428     return false;
429
430   if (nchan > nddcs())
431     return false;
432
433   d_nchan = nchan;
434
435   return write_hw_mux_reg ();
436 }
437
438
439 // map software mux value to hw mux value
440 //
441 // Software mux value:
442 //
443 //    3                   2                   1                       
444 //  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
445 // +-------+-------+-------+-------+-------+-------+-------+-------+
446 // |   Q3  |   I3  |   Q2  |   I2  |   Q1  |   I1  |   Q0  |   I0  |
447 // +-------+-------+-------+-------+-------+-------+-------+-------+
448 //
449 // Each 4-bit I field is either 0,1,2,3
450 // Each 4-bit Q field is either 0,1,2,3 or 0xf (input is const zero)
451 // All Q's must be 0xf or none of them may be 0xf
452 //
453 //
454 // Hardware mux value:
455 //
456 //    3                   2                   1                       
457 //  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
458 // +-----------------------+-------+-------+-------+-------+-+-----+
459 // |      must be zero     | Q3| I3| Q2| I2| Q1| I1| Q0| I0|Z| NCH |
460 // +-----------------------+-------+-------+-------+-------+-+-----+
461
462
463 static bool
464 map_sw_mux_to_hw_mux (int sw_mux, int *hw_mux_ptr)
465 {
466   // confirm that all I's are either 0,1,2,3 
467
468   for (int i = 0; i < 8; i += 2){
469     int t = (sw_mux >> (4 * i)) & 0xf;
470     if (!(0 <= t && t <= 3))
471       return false;
472   }
473
474   // confirm that all Q's are either 0,1,2,3 or 0xf
475
476   for (int i = 1; i < 8; i += 2){
477     int t = (sw_mux >> (4 * i)) & 0xf;
478     if (!(t == 0xf || (0 <= t && t <= 3)))
479       return false;
480   }
481
482   // confirm that all Q inputs are 0xf (const zero input),
483   // or none of them are 0xf
484
485   int q_and = 1;
486   int q_or =  0;
487
488   for (int i = 0; i < 4; i++){
489     int qx_is_0xf = ((sw_mux >> (8 * i + 4)) & 0xf) == 0xf;
490     q_and &= qx_is_0xf;
491     q_or  |= qx_is_0xf;
492   }
493
494   if (q_and || !q_or){          // OK
495     int hw_mux_value = 0;
496
497     for (int i = 0; i < 8; i++){
498       int t = (sw_mux >> (4 * i)) & 0x3;
499       hw_mux_value |= t << (2 * i + 4);
500     }
501
502     if (q_and)
503       hw_mux_value |= 0x8;      // all Q's zero
504
505     *hw_mux_ptr = hw_mux_value;
506     return true;
507   }
508   else
509     return false;
510 }
511
512 bool
513 usrp_standard_rx::set_mux (int mux)
514 {
515   if (!map_sw_mux_to_hw_mux (mux, &d_hw_mux))
516     return false;
517
518   // fprintf (stderr, "sw_mux = 0x%08x  hw_mux = 0x%08x\n", mux, d_hw_mux);
519
520   d_sw_mux = mux;
521   return write_hw_mux_reg ();
522 }
523
524 bool
525 usrp_standard_rx::write_hw_mux_reg ()
526 {
527   bool s = disable_rx ();
528   bool ok = _write_fpga_reg (FR_RX_MUX, d_hw_mux | d_nchan);
529   restore_rx (s);
530   return ok;
531 }
532
533 int
534 usrp_standard_rx::determine_rx_mux_value(const usrp_subdev_spec &ss)
535 {
536   /*
537     Determine appropriate Rx mux value as a function of the subdevice choosen and the
538     characteristics of the respective daughterboard.
539     
540     @param u:           instance of USRP source
541     @param subdev_spec: return value from subdev option parser.  
542     @type  subdev_spec: (side, subdev), where side is 0 or 1 and subdev is 0 or 1
543     @returns:           the Rx mux value
544   
545     Figure out which A/D's to connect to the DDC.
546     
547     Each daughterboard consists of 1 or 2 subdevices.  (At this time,
548     all but the Basic Rx have a single subdevice.  The Basic Rx
549     has two independent channels, treated as separate subdevices).
550     subdevice 0 of a daughterboard may use 1 or 2 A/D's.  We determine this
551     by checking the is_quadrature() method.  If subdevice 0 uses only a single
552     A/D, it's possible that the daughterboard has a second subdevice, subdevice 1,
553     and it uses the second A/D.
554     
555     If the card uses only a single A/D, we wire a zero into the DDC Q input.
556     
557     (side, 0) says connect only the A/D's used by subdevice 0 to the DDC.
558     (side, 1) says connect only the A/D's used by subdevice 1 to the DDC.
559   */
560
561   struct truth_table_element
562   {
563     int          d_side;
564     int          d_uses;
565     bool         d_swap_iq;
566     unsigned int d_mux_val;
567
568     truth_table_element(int side, unsigned int uses, bool swap_iq, unsigned int mux_val=0)
569       : d_side(side), d_uses(uses), d_swap_iq(swap_iq), d_mux_val(mux_val){}
570       
571     bool operator==(const truth_table_element &in)
572     {
573       return (d_side == in.d_side && d_uses == in.d_uses && d_swap_iq == in.d_swap_iq);
574     }
575
576     unsigned int mux_val() { return d_mux_val; }
577   };
578
579
580   if (!is_valid(ss))
581     throw std::invalid_argument("subdev_spec");
582
583
584   // This is a tuple of length 1 or 2 containing the subdevice
585   // classes for the selected side.
586   std::vector<db_base_sptr> db = this->db(ss.side);
587   
588   unsigned int subdev0_uses, subdev1_uses, uses;
589
590   // compute bitmasks of used A/D's
591   
592   if(db[0]->is_quadrature())
593     subdev0_uses = 0x3;               // uses A/D 0 and 1
594   else
595     subdev0_uses = 0x1;               // uses A/D 0 only
596
597   if(db.size() > 1)                   // more than 1 subdevice?
598     subdev1_uses = 0x2;               // uses A/D 1 only
599   else
600     subdev1_uses = 0x0;               // uses no A/D (doesn't exist)
601   
602   if(ss.subdev == 0)
603     uses = subdev0_uses;
604   else if(ss.subdev == 1)
605     uses = subdev1_uses;
606   else
607     throw std::invalid_argument("subdev_spec");
608
609   if(uses == 0){
610     throw std::runtime_error("Daughterboard doesn't have a subdevice 1");
611   }
612   
613   bool swap_iq = db[0]->i_and_q_swapped();
614   
615   truth_table_element truth_table[8] = {
616     // (side, uses, swap_iq) : mux_val
617     truth_table_element(0, 0x1, false, 0xf0f0f0f0),
618     truth_table_element(0, 0x2, false, 0xf0f0f0f1),
619     truth_table_element(0, 0x3, false, 0x00000010),
620     truth_table_element(0, 0x3, true,  0x00000001),
621     truth_table_element(1, 0x1, false, 0xf0f0f0f2),
622     truth_table_element(1, 0x2, false, 0xf0f0f0f3),
623     truth_table_element(1, 0x3, false, 0x00000032),
624     truth_table_element(1, 0x3, true,  0x00000023)
625   };
626   size_t nelements = sizeof(truth_table)/sizeof(truth_table[0]);
627   
628   truth_table_element target(ss.side, uses, swap_iq, 0);
629   
630   size_t i;
631   for(i = 0; i < nelements; i++){
632     if (truth_table[i] == target)
633       return truth_table[i].mux_val();
634   }
635   throw std::runtime_error("internal error");
636 }
637
638
639
640 bool
641 usrp_standard_rx::set_rx_freq (int channel, double freq)
642 {
643   if (channel < 0 || channel > MAX_CHAN)
644     return false;
645
646   unsigned int v =
647     compute_freq_control_word_fpga (adc_rate(),
648                                     freq, &d_rx_freq[channel],
649                                     d_verbose);
650
651   return _write_fpga_reg (FR_RX_FREQ_0 + channel, v);
652 }
653
654 unsigned int
655 usrp_standard_rx::decim_rate () const { return d_decim_rate; }
656
657 int
658 usrp_standard_rx::nchannels () const { return d_nchan; }
659
660 int
661 usrp_standard_rx::mux () const { return d_sw_mux; }
662
663 double 
664 usrp_standard_rx::rx_freq (int channel) const
665 {
666   if (channel < 0 || channel >= MAX_CHAN)
667     return 0;
668
669   return d_rx_freq[channel];
670 }
671
672 bool
673 usrp_standard_rx::set_fpga_mode (int mode)
674 {
675   return _write_fpga_reg (FR_MODE, mode);
676 }
677
678 bool
679 usrp_standard_rx::set_ddc_phase(int channel, int phase)
680 {
681   if (channel < 0 || channel >= MAX_CHAN)
682     return false;
683
684   return _write_fpga_reg(FR_RX_PHASE_0 + channel, phase);
685 }
686
687
688 // To avoid quiet failures, check for things that our code cares about.
689
690 static bool
691 rx_format_is_valid(unsigned int format)
692 {
693   int width =  usrp_standard_rx::format_width(format);
694   int want_q = usrp_standard_rx::format_want_q(format);
695
696   if (!(width == 8 || width == 16))     // FIXME add other widths when valid
697     return false;
698
699   if (!want_q)          // FIXME remove check when the rest of the code can handle I only
700     return false;
701
702   return true;
703 }
704
705 bool
706 usrp_standard_rx::set_format(unsigned int format)
707 {
708   if (!rx_format_is_valid(format))
709     return false;
710
711   return _write_fpga_reg(FR_RX_FORMAT, format);
712 }
713
714 unsigned int
715 usrp_standard_rx::format() const
716 {
717   return d_fpga_shadows[FR_RX_FORMAT];
718 }
719
720 // ----------------------------------------------------------------
721
722 unsigned int 
723 usrp_standard_rx::make_format(int width, int shift, bool want_q, bool bypass_halfband)
724 {
725   unsigned int format = 
726     (((width << bmFR_RX_FORMAT_WIDTH_SHIFT) & bmFR_RX_FORMAT_WIDTH_MASK)
727      | ((shift << bmFR_RX_FORMAT_SHIFT_SHIFT) & bmFR_RX_FORMAT_SHIFT_MASK));
728
729   if (want_q)
730     format |= bmFR_RX_FORMAT_WANT_Q;
731   if (bypass_halfband)
732     format |= bmFR_RX_FORMAT_BYPASS_HB;
733
734   return format;
735 }
736
737 int
738 usrp_standard_rx::format_width(unsigned int format)
739 {
740   return (format & bmFR_RX_FORMAT_WIDTH_MASK) >> bmFR_RX_FORMAT_WIDTH_SHIFT;
741 }
742
743 int
744 usrp_standard_rx::format_shift(unsigned int format)
745 {
746   return (format & bmFR_RX_FORMAT_SHIFT_MASK) >> bmFR_RX_FORMAT_SHIFT_SHIFT;
747 }
748
749 bool
750 usrp_standard_rx::format_want_q(unsigned int format)
751 {
752   return (format & bmFR_RX_FORMAT_WANT_Q) != 0;
753 }
754
755 bool
756 usrp_standard_rx::format_bypass_halfband(unsigned int format)
757 {
758   return (format & bmFR_RX_FORMAT_BYPASS_HB) != 0;
759 }
760
761 bool
762 usrp_standard_rx::tune(int chan, db_base_sptr db, double target_freq, usrp_tune_result *result)
763 {
764   ddc_control dxc(this, chan);
765   return tune_a_helper(db, target_freq, converter_rate(), dxc, result);
766 }
767
768
769 //////////////////////////////////////////////////////////////////
770
771
772 // tx data is timed to CLKOUT1 (64 MHz)
773 // interpolate 4x
774 // fine modulator enabled
775
776
777 static unsigned char tx_regs_use_nco[] = {
778   REG_TX_IF,            (TX_IF_USE_CLKOUT1
779                          | TX_IF_I_FIRST
780                          | TX_IF_2S_COMP
781                          | TX_IF_INTERLEAVED),
782   REG_TX_DIGITAL,       (TX_DIGITAL_2_DATA_PATHS
783                          | TX_DIGITAL_INTERPOLATE_4X)
784 };
785
786
787 static int
788 real_tx_mux_value (int mux, int nchan)
789 {
790   if (mux != -1)
791     return mux;
792
793   switch (nchan){
794   case 1:
795     return 0x0098;
796   case 2:
797     return 0xba98;
798   default:
799     assert (0);
800   }
801 }
802
803 usrp_standard_tx::usrp_standard_tx (int which_board,
804                                     unsigned int interp_rate,
805                                     int nchan, int mux,
806                                     int fusb_block_size, int fusb_nblocks,
807                                     const std::string fpga_filename,
808                                     const std::string firmware_filename
809                                     )
810   : usrp_basic_tx (which_board, fusb_block_size, fusb_nblocks, fpga_filename, firmware_filename),
811     usrp_standard_common(this),
812     d_sw_mux (0x8), d_hw_mux (0x81)
813 {
814   if (!usrp_9862_write_many_all (d_udh, tx_regs_use_nco, sizeof (tx_regs_use_nco))){
815     fprintf (stderr, "usrp_standard_tx: failed to init AD9862 TX regs\n");
816     throw std::runtime_error ("usrp_standard_tx::ctor");
817   }
818   if (!set_nchannels (nchan)){
819     fprintf (stderr, "usrp_standard_tx: set_nchannels failed\n");
820     throw std::runtime_error ("usrp_standard_tx::ctor");
821   }
822   if (!set_interp_rate (interp_rate)){
823     fprintf (stderr, "usrp_standard_tx: set_interp_rate failed\n");
824     throw std::runtime_error ("usrp_standard_tx::ctor");
825   }
826   if (!set_mux (real_tx_mux_value (mux, nchan))){
827     fprintf (stderr, "usrp_standard_tx: set_mux failed\n");
828     throw std::runtime_error ("usrp_standard_tx::ctor");
829   }
830
831   for (int i = 0; i < MAX_CHAN; i++){
832     d_tx_modulator_shadow[i] = (TX_MODULATOR_DISABLE_NCO
833                                 | TX_MODULATOR_COARSE_MODULATION_NONE);
834     d_coarse_mod[i] = CM_OFF;
835     set_tx_freq (i, 0);
836   }
837 }
838
839 usrp_standard_tx::~usrp_standard_tx ()
840 {
841   // fprintf(stderr, "\nusrp_standard_tx: dtor\n");
842 }
843
844 bool
845 usrp_standard_tx::start ()
846 {
847   if (!usrp_basic_tx::start ())
848     return false;
849
850   // add our code here
851
852   return true;
853 }
854
855 bool
856 usrp_standard_tx::stop ()
857 {
858   bool ok = usrp_basic_tx::stop ();
859
860   // add our code here
861
862   return ok;
863 }
864
865 usrp_standard_tx_sptr
866 usrp_standard_tx::make (int which_board,
867                         unsigned int interp_rate,
868                         int nchan, int mux,
869                         int fusb_block_size, int fusb_nblocks,
870                         const std::string fpga_filename,
871                         const std::string firmware_filename
872                         )
873 {
874   try {
875     usrp_standard_tx_sptr u  = 
876       usrp_standard_tx_sptr(new usrp_standard_tx(which_board, interp_rate, nchan, mux,
877                                                  fusb_block_size, fusb_nblocks,
878                                                  fpga_filename, firmware_filename));
879     u->init_db(u);
880     return u;
881   }
882   catch (...){
883     return usrp_standard_tx_sptr();
884   }
885 }
886
887 bool
888 usrp_standard_tx::set_interp_rate (unsigned int rate)
889 {
890   // fprintf (stderr, "usrp_standard_tx::set_interp_rate\n");
891
892   if ((rate & 0x3) || rate < 4 || rate > 512){
893     fprintf (stderr, "usrp_standard_tx::set_interp_rate: rate must be in [4, 512] and a multiple of 4.\n");
894     return false;
895   }
896
897   d_interp_rate = rate;
898   set_usb_data_rate ((dac_rate () / rate * nchannels ())
899                      * (2 * sizeof (short)));
900
901   // We're using the interp by 4 feature of the 9862 so that we can
902   // use its fine modulator.  Thus, we reduce the FPGA's interpolation rate
903   // by a factor of 4.
904
905   bool s = disable_tx ();
906   bool ok = _write_fpga_reg (FR_INTERP_RATE, d_interp_rate/4 - 1);
907   restore_tx (s);
908   return ok;
909 }
910
911 bool
912 usrp_standard_tx::set_nchannels (int nchan)
913 {
914   if (!(nchan == 1 || nchan == 2))
915     return false;
916
917   if (nchan > nducs())
918     return false;
919
920   d_nchan = nchan;
921   return write_hw_mux_reg ();
922 }
923
924 bool
925 usrp_standard_tx::set_mux (int mux)
926 {
927   d_sw_mux = mux;
928   d_hw_mux = mux << 4;
929   return write_hw_mux_reg ();
930 }
931
932 bool
933 usrp_standard_tx::write_hw_mux_reg ()
934 {
935   bool s = disable_tx ();
936   bool ok = _write_fpga_reg (FR_TX_MUX, d_hw_mux | d_nchan);
937   restore_tx (s);
938   return ok;
939 }
940
941 int
942 usrp_standard_tx::determine_tx_mux_value(const usrp_subdev_spec &ss)
943 {
944   /*
945     Determine appropriate Tx mux value as a function of the subdevice choosen.
946
947     @param u:           instance of USRP source
948     @param subdev_spec: return value from subdev option parser.  
949     @type  subdev_spec: (side, subdev), where side is 0 or 1 and subdev is 0
950     @returns:           the Rx mux value
951   
952     This is simpler than the rx case.  Either you want to talk
953     to side A or side B.  If you want to talk to both sides at once,
954     determine the value manually.
955   */
956
957   if (!is_valid(ss))
958     throw std::invalid_argument("subdev_spec");
959
960   std::vector<db_base_sptr> db = this->db(ss.side);
961   
962   if(db[0]->i_and_q_swapped()) {
963     unsigned int mask[2] = {0x0089, 0x8900};
964     return mask[ss.side];
965   }
966   else {
967     unsigned int mask[2] = {0x0098, 0x9800};
968     return mask[ss.side];
969   }
970 }
971
972
973
974 #ifdef USE_FPGA_TX_CORDIC
975
976 bool
977 usrp_standard_tx::set_tx_freq (int channel, double freq)
978 {
979   if (channel < 0 || channel >= MAX_CHAN)
980     return false;
981
982   // This assumes we're running the 4x on-chip interpolator.
983
984   unsigned int v =
985     compute_freq_control_word_fpga (dac_rate () / 4,
986                                     freq, &d_tx_freq[channel],
987                                     d_verbose);
988
989   return _write_fpga_reg (FR_TX_FREQ_0 + channel, v);
990 }
991
992
993 #else
994
995 bool
996 usrp_standard_tx::set_tx_freq (int channel, double freq)
997 {
998   if (channel < 0 || channel >= MAX_CHAN)
999     return false;
1000
1001   // split freq into fine and coarse components
1002
1003   coarse_mod_t  cm;
1004   double        coarse;
1005
1006   assert (dac_rate () == 128000000);
1007
1008   if (freq < -44e6)             // too low
1009     return false;
1010   else if (freq < -24e6){       // [-44, -24)
1011     cm = CM_NEG_FDAC_OVER_4;
1012     coarse = -dac_rate () / 4;
1013   }
1014   else if (freq < -8e6){        // [-24, -8)
1015     cm = CM_NEG_FDAC_OVER_8;
1016     coarse = -dac_rate () / 8;
1017   }
1018   else if (freq < 8e6){         // [-8, 8)
1019     cm = CM_OFF;
1020     coarse = 0;
1021   }
1022   else if (freq < 24e6){        // [8, 24)
1023     cm = CM_POS_FDAC_OVER_8;
1024     coarse = dac_rate () / 8;
1025   }
1026   else if (freq <= 44e6){       // [24, 44]
1027     cm = CM_POS_FDAC_OVER_4;
1028     coarse = dac_rate () / 4;
1029   }
1030   else                          // too high
1031     return false;
1032
1033
1034   set_coarse_modulator (channel, cm);   // set bits in d_tx_modulator_shadow
1035
1036   double fine = freq - coarse;
1037
1038
1039   // Compute fine tuning word...
1040   // This assumes we're running the 4x on-chip interpolator.
1041   // (This is required to use the fine modulator.)
1042
1043   unsigned int v =
1044     compute_freq_control_word_9862 (dac_rate () / 4,
1045                                     fine, &d_tx_freq[channel], d_verbose);
1046
1047   d_tx_freq[channel] += coarse;         // adjust actual
1048   
1049   unsigned char high, mid, low;
1050
1051   high = (v >> 16) & 0xff;
1052   mid =  (v >>  8) & 0xff;
1053   low =  (v >>  0) & 0xff;
1054
1055   bool ok = true;
1056
1057   // write the fine tuning word
1058   ok &= _write_9862 (channel, REG_TX_NCO_FTW_23_16, high);
1059   ok &= _write_9862 (channel, REG_TX_NCO_FTW_15_8,  mid);
1060   ok &= _write_9862 (channel, REG_TX_NCO_FTW_7_0,   low);
1061
1062
1063   d_tx_modulator_shadow[channel] |= TX_MODULATOR_ENABLE_NCO;
1064
1065   if (fine < 0)
1066     d_tx_modulator_shadow[channel] |= TX_MODULATOR_NEG_FINE_TUNE;
1067   else
1068     d_tx_modulator_shadow[channel] &= ~TX_MODULATOR_NEG_FINE_TUNE;
1069
1070   ok &=_write_9862 (channel, REG_TX_MODULATOR, d_tx_modulator_shadow[channel]);
1071
1072   return ok;
1073 }
1074 #endif
1075
1076 bool
1077 usrp_standard_tx::set_coarse_modulator (int channel, coarse_mod_t cm)
1078 {
1079   if (channel < 0 || channel >= MAX_CHAN)
1080     return false;
1081
1082   switch (cm){
1083   case CM_NEG_FDAC_OVER_4:
1084     d_tx_modulator_shadow[channel] &= ~TX_MODULATOR_CM_MASK;
1085     d_tx_modulator_shadow[channel] |= TX_MODULATOR_COARSE_MODULATION_F_OVER_4;
1086     d_tx_modulator_shadow[channel] |= TX_MODULATOR_NEG_COARSE_TUNE;
1087     break;
1088
1089   case CM_NEG_FDAC_OVER_8:
1090     d_tx_modulator_shadow[channel] &= ~TX_MODULATOR_CM_MASK;
1091     d_tx_modulator_shadow[channel] |= TX_MODULATOR_COARSE_MODULATION_F_OVER_8;
1092     d_tx_modulator_shadow[channel] |= TX_MODULATOR_NEG_COARSE_TUNE;
1093     break;
1094
1095   case CM_OFF:
1096     d_tx_modulator_shadow[channel] &= ~TX_MODULATOR_CM_MASK;
1097     break;
1098
1099   case CM_POS_FDAC_OVER_8:
1100     d_tx_modulator_shadow[channel] &= ~TX_MODULATOR_CM_MASK;
1101     d_tx_modulator_shadow[channel] |= TX_MODULATOR_COARSE_MODULATION_F_OVER_8;
1102     break;
1103
1104   case CM_POS_FDAC_OVER_4:
1105     d_tx_modulator_shadow[channel] &= ~TX_MODULATOR_CM_MASK;
1106     d_tx_modulator_shadow[channel] |= TX_MODULATOR_COARSE_MODULATION_F_OVER_4;
1107     break;
1108
1109   default:
1110     return false;
1111   }
1112
1113   d_coarse_mod[channel] = cm;
1114   return true;
1115 }
1116
1117 unsigned int
1118 usrp_standard_tx::interp_rate () const { return d_interp_rate; }
1119
1120 int
1121 usrp_standard_tx::nchannels () const { return d_nchan; }
1122
1123 int
1124 usrp_standard_tx::mux () const { return d_sw_mux; }
1125
1126 double
1127 usrp_standard_tx::tx_freq (int channel) const
1128 {
1129   if (channel < 0 || channel >= MAX_CHAN)
1130     return 0;
1131
1132   return d_tx_freq[channel];
1133 }
1134
1135 usrp_standard_tx::coarse_mod_t
1136 usrp_standard_tx::coarse_modulator (int channel) const
1137 {
1138   if (channel < 0 || channel >= MAX_CHAN)
1139     return CM_OFF;
1140
1141   return d_coarse_mod[channel];
1142 }
1143
1144 bool
1145 usrp_standard_tx::tune(int chan, db_base_sptr db, double target_freq, usrp_tune_result *result)
1146 {
1147   duc_control dxc(this, chan);
1148   return tune_a_helper(db, target_freq, converter_rate(), dxc, result);
1149 }