Imported Upstream version 3.2.2
[debian/gnuradio] / usrp / host / lib / legacy / db_base.cc
1 //
2 // Copyright 2008 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
22 #include <db_base.h>
23 #include <db_base_impl.h>
24
25 #if 0
26 tune_result::tune_result(double baseband, double dxc, double residual, bool inv)
27   : ok(false), baseband_freq(baseband), dxc_freq(dxc), 
28     residual_freq(residual), inverted(inv)
29 {
30 }
31
32 tune_result::~tune_result()
33
34 }
35 #endif
36
37
38 /*****************************************************************************/
39
40 db_base::db_base(usrp_basic_sptr usrp, int which)
41   : d_is_shutdown(false), d_raw_usrp(usrp.get()), d_which(which), d_lo_offset(0.0)
42 {
43 }
44
45 db_base::~db_base()
46 {
47   shutdown();
48 }
49
50 void
51 db_base::shutdown()
52 {
53   if (!d_is_shutdown){
54     d_is_shutdown = true;
55     // do whatever there is to do to shutdown
56   }
57 }
58
59 int 
60 db_base::dbid()
61 {
62   return usrp()->daughterboard_id(d_which);
63 }
64
65 std::string 
66 db_base::name()
67 {
68   return usrp_dbid_to_string(dbid());
69 }
70
71 std::string 
72 db_base::side_and_name()
73 {
74   if(d_which == 0)
75     return "A: " + name();
76   else
77     return "B: " + name();
78 }
79
80 // Function to bypass ADC buffers. Any board which is DC-coupled
81 // should bypass the buffers
82
83 bool
84 db_base::bypass_adc_buffers(bool bypass)
85 {
86   //if(d_tx) {
87   //  throw  std::runtime_error("TX Board has no adc buffers\n");
88   //}
89
90   bool ok = true;
91   if(d_which==0) {
92     ok &= usrp()->set_adc_buffer_bypass(0, bypass);
93     ok &= usrp()->set_adc_buffer_bypass(1, bypass);
94   }
95   else {
96     ok &= usrp()->set_adc_buffer_bypass(2, bypass);
97     ok &= usrp()->set_adc_buffer_bypass(3, bypass);
98   }
99   return ok;
100 }
101
102 bool 
103 db_base::set_atr_mask(int v)
104 {
105   // Set Auto T/R mask.
106   return usrp()->write_atr_mask(d_which, v);
107 }
108
109 bool 
110 db_base::set_atr_txval(int v)
111 {
112   // Set Auto T/R register value to be used when transmitting.
113   return usrp()->write_atr_txval(d_which, v);
114 }
115   
116 bool 
117 db_base::set_atr_rxval(int v)
118 {
119   // Set Auto T/R register value to be used when receiving.
120   return usrp()->write_atr_rxval(d_which, v);
121 }
122   
123 bool 
124 db_base::set_atr_tx_delay(int v)
125 {
126   // Set Auto T/R delay (in clock ticks) from when Tx fifo gets data to 
127   // when T/R switches.
128   return usrp()->write_atr_tx_delay(v);
129 }
130
131 bool 
132 db_base::set_atr_rx_delay(int v)
133 {
134   // Set Auto T/R delay (in clock ticks) from when Tx fifo goes empty to 
135   // when T/R switches.
136   return usrp()->write_atr_rx_delay(v);
137 }
138
139 bool
140 db_base::i_and_q_swapped()
141 {
142   // Return True if this is a quadrature device and (for RX) ADC 0 is Q
143   // or (for TX) DAC 0 is Q
144   return false;
145 }
146
147 bool 
148 db_base::spectrum_inverted()
149 {
150   // Return True if the dboard gives an inverted spectrum
151   
152   return false;
153 }
154
155 bool
156 db_base::set_enable(bool on)
157 {
158   // For tx daughterboards, this controls the transmitter enable.
159
160   return true;  // default is nop
161 }
162
163 bool
164 db_base::set_auto_tr(bool on)
165 {
166   // Enable automatic Transmit/Receive switching (ATR).
167   // 
168   // Should be overridden in subclasses that care.  This will typically
169   // set the atr_mask, txval and rxval.
170
171   return true;
172 }
173
174 bool
175 db_base::set_lo_offset(double offset)
176 {
177   // Set how much LO is offset from requested frequency
178
179   d_lo_offset = offset;
180   return true;
181 }
182
183 bool
184 db_base::select_rx_antenna(int which_antenna)
185 {
186   // Specify which antenna port to use for reception.
187   // Should be overriden by daughterboards that care.
188
189   return which_antenna == 0;
190 }
191
192 bool
193 db_base::select_rx_antenna(const std::string &which_antenna)
194 {
195   // Specify which antenna port to use for reception.
196   // Should be overriden by daughterboards that care.
197
198   return which_antenna == "";
199 }
200
201
202 // Reference Clock section
203 //
204 // Control whether a reference clock is sent to the daughterboards,
205 // and what frequency
206 //
207 // Bit 7  -- 1 turns on refclk, 0 allows IO use
208 // Bits 6:0 Divider value
209 //
210     
211 double
212 db_base::_refclk_freq() 
213 {
214   return usrp()->fpga_master_clock_freq() / _refclk_divisor();
215 }
216
217 void 
218 db_base::_enable_refclk(bool enable)
219 {
220   int CLOCK_OUT = 1;   // Clock is on lowest bit
221   int REFCLK_ENABLE = 0x80;
222   int REFCLK_DIVISOR_MASK = 0x7f;
223
224   if(enable) {
225     usrp()->_write_oe(d_which, CLOCK_OUT, CLOCK_OUT); // output enable
226     usrp()->write_refclk(d_which, (_refclk_divisor() & REFCLK_DIVISOR_MASK) | REFCLK_ENABLE);
227   }
228   else {
229     usrp()->write_refclk(d_which, 0);
230   }
231 }
232
233 int 
234 db_base::_refclk_divisor()
235 {
236   // Return value to stick in REFCLK_DIVISOR register
237   throw std::runtime_error("_reflck_divisor() called from base class\n");;
238 }
239
240 bool
241 db_base::set_bw(float bw)
242 {
243   // Set baseband bandwidth (board specific)
244   // Should be overriden by boards that implement variable IF filtering (e.g., DBSRX)
245   return false;
246 }
247
248 std::ostream &operator<<(std::ostream &os, db_base &x)
249 {
250   os << x.side_and_name();
251   return os;
252 }