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