Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gr-usrp / src / db_base.py
1 #
2 # Copyright 2005,2006,2007 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 version 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 import weakref
23 from usrpm import usrp_prims
24 from usrpm.usrp_fpga_regs import *
25
26 class db_base(object):
27     """
28     Abstract base class for all daughterboards.
29
30     This defines the required operations and interfaces for all d'boards.
31     """
32     def __init__(self, usrp, which):
33         """
34         Initialize daughterboard interface.
35
36         @param usrp: instance of usrp
37         @param which: which daughterboard side: A = 0, B = 1
38         @type which: int
39         """
40
41         if not (which in (0, 1)):
42             raise ValueError, "Invalid value of which: %s" % (which,)
43
44         self._u = weakref.proxy(usrp)
45
46         self._which = which
47         if hasattr(self._u, 'tx_freq'):   # is this a tx or rx daughterboard?
48             self._tx = True
49             self._slot = which * 2
50         else:
51             self._tx = False
52             self._slot = which * 2 + 1
53
54         self._refclk_reg = (FR_TX_A_REFCLK,FR_RX_A_REFCLK,FR_TX_B_REFCLK,FR_RX_B_REFCLK)[self._slot]
55
56     def dbid(self):
57         return self._u.daughterboard_id(self._which)
58
59     def name(self):
60         return usrp_prims.usrp_dbid_to_string(self.dbid())
61
62     def side_and_name(self):
63         return "AB"[self._which] + ': ' + self.name()
64
65     # Function to bypass ADC buffers.  Any board which is DC-coupled should bypass the buffers
66     def bypass_adc_buffers(self,bypass):
67         if self._tx:
68             raise RuntimeError, "TX Board has no adc buffers"
69         if self._which==0:
70             self._u.set_adc_buffer_bypass(0, bypass)
71             self._u.set_adc_buffer_bypass(1, bypass)
72         else:
73             self._u.set_adc_buffer_bypass(2, bypass)
74             self._u.set_adc_buffer_bypass(3, bypass)
75         
76     # ------------------------------------------------------------------------
77     # Reference Clock section
78
79     # Control whether a reference clock is sent to the daughterboards,
80     # and what frequency
81     #
82     # Bit 7  -- 1 turns on refclk, 0 allows IO use
83     # Bits 6:0 Divider value
84     #
85     
86     def _refclk_freq(self):
87         return self._u.fpga_master_clock_freq()/self._refclk_divisor()
88     
89     def _enable_refclk(self,enable):
90         CLOCK_OUT = 1   # Clock is on lowest bit
91         REFCLK_ENABLE = 0x80
92         REFCLK_DIVISOR_MASK = 0x7f
93         if enable:
94             self._u._write_oe(self._which, CLOCK_OUT, CLOCK_OUT)  # output enable
95             self._u._write_fpga_reg(self._refclk_reg,
96                                    ((self._refclk_divisor() & REFCLK_DIVISOR_MASK)
97                                     | REFCLK_ENABLE))
98         else:
99             self._u._write_fpga_reg(self._refclk_reg, 0)
100             
101     def _refclk_divisor(self):
102         """
103         Return value to stick in REFCLK_DIVISOR register
104         """
105         raise NotImplementedError
106
107     # ------------------------------------------------------------------------
108     # Automatic Transmit/Receive switching
109     #
110     # The presence or absence of data in the FPGA transmit fifo
111     # selects between two sets of values for each of the 4 banks of
112     # daughterboard i/o pins.
113     #
114     # Each daughterboard slot has 3 16-bit registers associated with it:
115     #   FR_ATR_MASK_*, FR_ATR_TXVAL_* and FR_ATR_RXVAL_*
116     #
117     # FR_ATR_MASK_{0,1,2,3}: 
118     #
119     #   These registers determine which of the daugherboard i/o pins are
120     #   affected by ATR switching.  If a bit in the mask is set, the
121     #   corresponding i/o bit is controlled by ATR, else it's output
122     #   value comes from the normal i/o pin output register:
123     #   FR_IO_{0,1,2,3}.
124     #
125     # FR_ATR_TXVAL_{0,1,2,3}:
126     # FR_ATR_RXVAL_{0,1,2,3}:
127     #
128     #   If the Tx fifo contains data, then the bits from TXVAL that are
129     #   selected by MASK are output.  Otherwise, the bits from RXVAL that
130     #   are selected by MASK are output.
131
132     def set_atr_mask(self, v):
133         """
134         Set Auto T/R mask.
135         """
136         return self._u._write_fpga_reg(FR_ATR_MASK_0 + 3 * self._slot, v)
137
138     def set_atr_txval(self, v):
139         """
140         Set Auto T/R register value to be used when transmitting.
141         """
142         return self._u._write_fpga_reg(FR_ATR_TXVAL_0 + 3 * self._slot, v)
143
144     def set_atr_rxval(self, v):
145         """
146         Set Auto T/R register value to be used when receiving.
147         """
148         return self._u._write_fpga_reg(FR_ATR_RXVAL_0 + 3 * self._slot, v)
149
150     def set_atr_tx_delay(self, v):
151         """
152         Set Auto T/R delay (in clock ticks) from when Tx fifo gets data to 
153         when T/R switches.
154         """
155         return self._u._write_fpga_reg(FR_ATR_TX_DELAY, v)
156         
157     def set_atr_rx_delay(self, v):
158         """
159         Set Auto T/R delay (in clock ticks) from when Tx fifo goes empty to 
160         when T/R switches.
161         """
162         return self._u._write_fpga_reg(FR_ATR_RX_DELAY, v)
163         
164     # derived classes should override the following methods
165
166     def freq_range(self):
167         """
168         Return range of frequencies in Hz that can be tuned by this d'board.
169
170         @returns (min_freq, max_freq, step_size)
171         @rtype tuple
172         """
173         raise NotImplementedError
174
175     def set_freq(self, target_freq):
176         """
177         Set the frequency.
178
179         @param freq:  target RF frequency in Hz
180         @type freq:   float
181
182         @returns (ok, actual_baseband_freq) where:
183            ok is True or False and indicates success or failure,
184            actual_baseband_freq is the RF frequency that corresponds to DC in the IF.
185         """
186         raise NotImplementedError
187
188     def gain_range(self):
189         """
190         Return range of gain that can be set by this d'board.
191
192         @returns (min_gain, max_gain, step_size)
193         Where gains are expressed in decibels (your mileage may vary)
194         """
195         raise NotImplementedError
196
197     def set_gain(self, gain):
198         """
199         Set the gain.
200
201         @param gain:  gain in decibels
202         @returns True/False
203         """
204         raise NotImplementedError
205
206     def is_quadrature(self):
207         """
208         Return True if this daughterboard does quadrature up or down conversion.
209         That is, return True if this board requires both I & Q analog channels.
210
211         This bit of info is useful when setting up the USRP Rx mux register.
212         """
213         raise NotImplementedError
214
215     def i_and_q_swapped(self):
216         """
217         Return True if this is a quadrature device and ADC 0 is Q.
218         """
219         return False
220
221     def spectrum_inverted(self):
222         """
223         Return True if the dboard gives an inverted spectrum
224         """
225         return False
226     
227     def set_enable(self, on):
228         """
229         For tx daughterboards, this controls the transmitter enable.
230         """
231         pass
232     
233     def set_auto_tr(self,on):
234         """
235         Enable automatic Transmit/Receive switching (ATR).
236
237         Should be overridden in subclasses that care.  This will typically
238         set the atr_mask, txval and rxval.
239         """
240         pass
241
242     def set_lo_offset(self, offset):
243         """
244         Set how much LO is offset from requested frequency
245         
246         Should be overriden by daughterboards that care.
247         """
248         pass
249         
250     def lo_offset(self, offset):
251         """
252         Get how much LO is offset from requested frequency
253
254         Should be overriden by daughterboards that care.
255         """
256         return 0.0