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