Revision ce5940f8

b/host/docs/dboards.rst
35 35
greater than the nyquist rate of the DAC.
36 36

  
37 37
^^^^^^^^^^^^^^^^^^^^^^^^^^^
38
DBSRX
39
^^^^^^^^^^^^^^^^^^^^^^^^^^^
40
The DBSRX board has 1 quadrature subdevice.
41

  
42
Receive Antennas: **J3**
43

  
44
The board has no user selectable antenna setting
45

  
46
Recieve Gains: 
47
    **GC1**, Range: 0-56dB
48
    **GC2**, Range: 0-24dB
49

  
50
^^^^^^^^^^^^^^^^^^^^^^^^^^^
38 51
RFX Series
39 52
^^^^^^^^^^^^^^^^^^^^^^^^^^^
40 53
Transmit Antennas: **TX/RX**
b/host/include/uhd/utils/algorithm.hpp
69 69
    }
70 70

  
71 71
    /*!
72
     * A wrapper around std::reverse that takes a range instead of an iterator.
73
     *
74
     * The elements are reversed into descending order using the less-than operator.
75
     *
76
     * \param range the range of elements to be reversed
77
     */
78
    template<typename Range> inline void reverse(Range &range){
79
        return std::reverse(boost::begin(range), boost::end(range));
80
    }
81

  
82
    /*!
83
     * A wrapper around std::reverse that takes a range instead of an iterator.
84
     *
85
     * The elements are reversed into descending order using the less-than operator.
86
     * This wrapper reverses the elements non-destructively into a new range.
87
     * Based on the builtin python function reversed(...)
88
     *
89
     * \param range the range of elements to be reversed
90
     * \return a new range with the elements reversed
91
     */
92
    template<typename Range> inline Range reversed(const Range &range){
93
        Range srange(range); std::reverse(srange); return srange;
94
    }
95

  
96
    /*!
72 97
     * Is the value found within the elements in this range?
73 98
     *
74 99
     * Uses std::find to search the iterable for an element.
b/host/lib/ic_reg_maps/CMakeLists.txt
55 55
)
56 56

  
57 57
LIBUHD_PYTHON_GEN_SOURCE(
58
    ${CMAKE_SOURCE_DIR}/lib/ic_reg_maps/gen_max2118_regs.py
59
    ${CMAKE_BINARY_DIR}/lib/ic_reg_maps/max2118_regs.hpp
60
)
61

  
62
LIBUHD_PYTHON_GEN_SOURCE(
58 63
    ${CMAKE_SOURCE_DIR}/lib/ic_reg_maps/gen_ad9862_regs.py
59 64
    ${CMAKE_BINARY_DIR}/lib/ic_reg_maps/ad9862_regs.hpp
60 65
)
b/host/lib/ic_reg_maps/common.py
173 173
    def get_type(self):
174 174
        return 'boost::uint%d_t'%max(2**math.ceil(math.log(self.get_bit_width(), 2)), 8)
175 175

  
176
def generate(name, regs_tmpl, body_tmpl='', file=__file__):
176
def generate(name, regs_tmpl, body_tmpl='', file=__file__, append=False):
177 177
    #evaluate the regs template and parse each line into a register
178 178
    regs = list(); mregs = list()
179 179
    for entry in parse_tmpl(regs_tmpl).splitlines():
......
193 193
    )
194 194

  
195 195
    #write the generated code to file specified by argv1
196
    open(sys.argv[1], 'w').write(code)
196
    open(sys.argv[1], 'a' if append else 'w').write(code)
b/host/lib/ic_reg_maps/gen_max2118_regs.py
1
#!/usr/bin/env python
2
#
3
# Copyright 2010 Ettus Research LLC
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
#
18

  
19
########################################################################
20
# Template for raw text data describing write registers
21
# name addr[bit range inclusive] default optional enums
22
########################################################################
23
WRITE_REGS_TMPL="""\
24
########################################################################
25
## Note: offsets given from perspective of data bits (excludes address)
26
########################################################################
27
##
28
########################################################################
29
## N-Divider MSB (0) Write
30
########################################################################
31
div2                  0[7]          0       div4, div2
32
n_divider_msb         0[0:6]        3
33
########################################################################
34
## N-Divider LSB (1) Write
35
########################################################################
36
n_divider_lsb         1[0:7]        0xB6
37
~n_divider            n_divider_lsb, n_divider_msb
38
########################################################################
39
## R, Charge Pump, and VCO (2) Write
40
########################################################################
41
#set $r_divider_names = ', '.join(map(lambda x: 'div' + str(2**(x+1)), range(0,8)))
42
r_divider             2[5:7]        1       $r_divider_names 
43
#set $cp_current_bias = ', '.join(map(lambda x: 'i_cp_%dua'%(50*2**x), range(0,4)))
44
cp_current            2[3:4]        3       $cp_current_bias
45
osc_band              2[0:2]        5
46
########################################################################
47
## I/Q Filter DAC (3) Write
48
########################################################################
49
##unused              3[7]          0
50
f_dac                 3[0:6]        0x7F    ## filter tuning dac, depends on m
51
########################################################################
52
## LPF Divider DAC (4) Write
53
########################################################################
54
adl_vco_adc_latch     4[7]          0       disabled, enabled
55
ade_vco_ade_read      4[6]          0       disabled, enabled
56
dl_output_drive       4[5]          0       iq_590m_vpp, iq_1_vpp
57
m_divider             4[0:4]        2       ## filter tuning counter
58
########################################################################
59
## GC2 and Diag (5) Write
60
########################################################################
61
diag                  5[5:7]        0       normal, cp_i_source, cp_i_sink, cp_high_z, unused, n_and_filt, r_and_gc2, m_div
62
gc2                   5[0:4]        0x1F    ## Step Size: 0-1: 0dB, 2-22: 1dB, 23-31: 0.5dB
63
"""
64

  
65
########################################################################
66
# Template for raw text data describing read registers
67
# name addr[bit range inclusive] default optional enums
68
########################################################################
69
READ_REGS_TMPL="""\
70
########################################################################
71
## Status (0) Read
72
########################################################################
73
pwr                   0[6]          0       not_reset, reset
74
adc                   0[2:5]        0       ## VCO tuning voltage, Lock Status
75
########################################################################
76
## I/Q Filter DAC (1) Read
77
########################################################################
78
filter_dac            1[0:6]        0       ## I/Q Filter tuning DAC, current
79
"""
80

  
81
########################################################################
82
# Template for methods in the body of the struct
83
########################################################################
84
BODY_TMPL="""\
85
boost::uint8_t get_reg(boost::uint8_t addr){
86
    boost::uint8_t reg = 0;
87
    switch(addr){
88
    #for $addr in sorted(set(map(lambda r: r.get_addr(), $regs)))
89
    case $addr:
90
        #for $reg in filter(lambda r: r.get_addr() == addr, $regs)
91
        reg |= (boost::uint8_t($reg.get_name()) & $reg.get_mask()) << $reg.get_shift();
92
        #end for
93
        break;
94
    #end for
95
    }
96
    return boost::uint8_t(reg);
97
}
98

  
99
void set_reg(boost::uint8_t addr, boost::uint8_t reg){
100
    switch(addr){
101
    #for $addr in sorted(set(map(lambda r: r.get_addr(), $regs)))
102
    case $addr:
103
        #for $reg in filter(lambda r: r.get_addr() == addr, $regs)
104
        $reg.get_name() = $(reg.get_type())((reg >> $reg.get_shift()) & $reg.get_mask());
105
        #end for
106
        break;
107
    #end for
108
    }
109
}
110
"""
111

  
112
if __name__ == '__main__':
113
    import common; common.generate(
114
        name='max2118_write_regs',
115
        regs_tmpl=WRITE_REGS_TMPL,
116
        body_tmpl=BODY_TMPL,
117
        file=__file__,
118
    )
119

  
120
    import common; common.generate(
121
        name='max2118_read_regs',
122
        regs_tmpl=READ_REGS_TMPL,
123
        body_tmpl=BODY_TMPL,
124
        file=__file__,
125
        append=True,
126
    )
b/host/lib/usrp/dboard/CMakeLists.txt
22 22
    ${CMAKE_SOURCE_DIR}/lib/usrp/dboard/db_rfx.cpp
23 23
    ${CMAKE_SOURCE_DIR}/lib/usrp/dboard/db_xcvr2450.cpp
24 24
    ${CMAKE_SOURCE_DIR}/lib/usrp/dboard/db_wbx.cpp
25
    ${CMAKE_SOURCE_DIR}/lib/usrp/dboard/db_dbsrx.cpp
25 26
    ${CMAKE_SOURCE_DIR}/lib/usrp/dboard/db_unknown.cpp
26 27
)
27 28

  
b/host/lib/usrp/dboard/db_dbsrx.cpp
1
//
2
// Copyright 2010 Ettus Research LLC
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

  
18
// No RX IO Pins Used
19

  
20
// RX IO Functions
21

  
22
#include "max2118_regs.hpp"
23
#include <uhd/utils/static.hpp>
24
#include <uhd/utils/assert.hpp>
25
#include <uhd/utils/algorithm.hpp>
26
#include <uhd/types/ranges.hpp>
27
#include <uhd/types/dict.hpp>
28
#include <uhd/usrp/subdev_props.hpp>
29
#include <uhd/usrp/dboard_base.hpp>
30
#include <uhd/usrp/dboard_manager.hpp>
31
#include <boost/assign/list_of.hpp>
32
#include <boost/format.hpp>
33
#include <boost/thread.hpp>
34
#include <boost/math/special_functions/round.hpp>
35
#include <utility>
36

  
37
using namespace uhd;
38
using namespace uhd::usrp;
39
using namespace boost::assign;
40

  
41
/***********************************************************************
42
 * The DBSRX constants
43
 **********************************************************************/
44
static const bool dbsrx_debug = true;
45

  
46
static const freq_range_t dbsrx_freq_range(0.8e9, 2.4e9);
47

  
48
static const freq_range_t dbsrx_pfd_freq_range(0.15e6, 2.01e6);
49

  
50
static const prop_names_t dbsrx_antennas = list_of("J3");
51

  
52
static const uhd::dict<std::string, gain_range_t> dbsrx_gain_ranges = map_list_of
53
    ("GC1", gain_range_t(0, 56, 0.5))
54
    ("GC2", gain_range_t(0, 24, 1))
55
;
56

  
57
/***********************************************************************
58
 * The DBSRX dboard class
59
 **********************************************************************/
60
class dbsrx : public rx_dboard_base{
61
public:
62
    dbsrx(ctor_args_t args, boost::uint8_t max2118_addr);
63
    ~dbsrx(void);
64

  
65
    void rx_get(const wax::obj &key, wax::obj &val);
66
    void rx_set(const wax::obj &key, const wax::obj &val);
67

  
68
private:
69
    double _lo_freq;
70
    float _bandwidth;
71
    uhd::dict<std::string, float> _gains;
72
    max2118_write_regs_t _max2118_write_regs;
73
    max2118_read_regs_t _max2118_read_regs;
74
    boost::uint8_t _max2118_addr; //0x67 or 0x65 depending on which side
75

  
76
    void set_lo_freq(double target_freq);
77
    void set_gain(float gain, const std::string &name);
78
    void set_bandwidth(float bandwidth);
79

  
80
    void send_reg(boost::uint8_t start_reg, boost::uint8_t stop_reg){
81
        start_reg = boost::uint8_t(std::clip(int(start_reg), 0x0, 0x5));
82
        stop_reg = boost::uint8_t(std::clip(int(stop_reg), 0x0, 0x5));
83

  
84
        for(boost::uint8_t start_addr=start_reg; start_addr <= stop_reg; start_addr += sizeof(boost::uint32_t) - 1){
85
            int num_bytes = int(stop_reg - start_addr + 1) > int(sizeof(boost::uint32_t)) - 1 ? sizeof(boost::uint32_t) - 1 : stop_reg - start_addr + 1;
86

  
87
            //create buffer for register data (+1 for start address)
88
            byte_vector_t regs_vector(num_bytes + 1);
89

  
90
            //first byte is the address of first register
91
            regs_vector[0] = start_addr;
92

  
93
            //get the register data
94
            for(int i=0; i<num_bytes; i++){
95
                regs_vector[1+i] = _max2118_write_regs.get_reg(start_addr+i);
96
                if(dbsrx_debug) std::cerr << boost::format(
97
                    "DBSRX: send reg 0x%02x, value 0x%04x, start_addr = 0x%04x, num_bytes %d"
98
                ) % int(start_addr+i) % int(regs_vector[1+i]) % int(start_addr) % num_bytes << std::endl;
99
            }
100

  
101
            //send the data
102
            this->get_iface()->write_i2c(
103
                _max2118_addr, regs_vector
104
            );
105
        }
106
    }
107

  
108
    void read_reg(boost::uint8_t start_reg, boost::uint8_t stop_reg){
109
        static const boost::uint8_t status_addr = 0x0;
110
        start_reg = boost::uint8_t(std::clip(int(start_reg), 0x0, 0x1));
111
        stop_reg = boost::uint8_t(std::clip(int(stop_reg), 0x0, 0x1));
112

  
113
        for(boost::uint8_t start_addr=start_reg; start_addr <= stop_reg; start_addr += sizeof(boost::uint32_t)){
114
            int num_bytes = int(stop_reg - start_addr + 1) > int(sizeof(boost::uint32_t)) ? sizeof(boost::uint32_t) : stop_reg - start_addr + 1;
115

  
116
            //create address to start reading register data
117
            byte_vector_t address_vector(1);
118
            address_vector[0] = start_addr;
119

  
120
            /*
121
            //send the address
122
            this->get_iface()->write_i2c(
123
                _max2118_addr, address_vector
124
            );
125
            */
126

  
127
            //create buffer for register data
128
            byte_vector_t regs_vector(num_bytes);
129

  
130
            //read from i2c
131
            regs_vector = this->get_iface()->read_i2c(
132
                _max2118_addr, num_bytes
133
            );
134

  
135
            for(boost::uint8_t i=0; i < num_bytes; i++){
136
                if (i + start_addr >= status_addr){
137
                    _max2118_read_regs.set_reg(i + start_addr, regs_vector[i]);
138
                    if(dbsrx_debug) std::cerr << boost::format(
139
                        "DBSRX: set reg 0x%02x, value 0x%04x"
140
                    ) % int(i + start_addr) % int(_max2118_read_regs.get_reg(i + start_addr)) << std::endl;
141
                }
142
                if(dbsrx_debug) std::cerr << boost::format(
143
                    "DBSRX: read reg 0x%02x, value 0x%04x, start_addr = 0x%04x, num_bytes %d"
144
                ) % int(start_addr+i) % int(regs_vector[i]) % int(start_addr) % num_bytes << std::endl;
145
            }
146
        }
147
    }
148

  
149
    /*!
150
     * Is the LO locked?
151
     * \return true for locked
152
     */
153
    bool get_locked(void){
154
        read_reg(0x0, 0x1);
155

  
156
        //mask and return lock detect
157
        bool locked = 5 >= _max2118_read_regs.adc && _max2118_read_regs.adc >= 2;
158

  
159
        if(dbsrx_debug) std::cerr << boost::format(
160
            "DBSRX: locked %d"
161
        ) % locked << std::endl;
162

  
163
        return locked;
164
    }
165

  
166
};
167

  
168
/***********************************************************************
169
 * Register the DBSRX dboard
170
 **********************************************************************/
171
// FIXME 0x67 is the default i2c address on USRP2
172
//       need to handle which side for USRP1 with different address
173
static dboard_base::sptr make_dbsrx(dboard_base::ctor_args_t args){
174
    return dboard_base::sptr(new dbsrx(args, 0x67));
175
}
176

  
177
//FIXME different dbid for USRP1 also
178
UHD_STATIC_BLOCK(reg_dbsrx_dboard){
179
    //register the factory function for the rx dbid
180
    dboard_manager::register_dboard(0x000D, &make_dbsrx, "DBSRX");
181
}
182

  
183
/***********************************************************************
184
 * Structors
185
 **********************************************************************/
186
dbsrx::dbsrx(ctor_args_t args, boost::uint8_t max2118_addr) : rx_dboard_base(args){
187
    //enable only the clocks we need
188
    this->get_iface()->set_clock_enabled(dboard_iface::UNIT_RX, true);
189

  
190
    //set the gpio directions and atr controls (identically)
191
    this->get_iface()->set_pin_ctrl(dboard_iface::UNIT_RX, 0x0); // All unused in atr
192
    this->get_iface()->set_gpio_ddr(dboard_iface::UNIT_RX, 0x0); // All Inputs
193

  
194
    //set the i2c address for the max2118
195
    _max2118_addr = max2118_addr;
196

  
197
    //send initial register settings
198
    this->send_reg(0x0, 0x5);
199

  
200
    //set defaults for LO, gains
201
    set_lo_freq(dbsrx_freq_range.min);
202
    BOOST_FOREACH(const std::string &name, dbsrx_gain_ranges.keys()){
203
        set_gain(dbsrx_gain_ranges[name].min, name);
204
    }
205

  
206
    set_bandwidth(22.27e6); // default bandwidth from datasheet
207

  
208
    get_locked();
209
}
210

  
211
dbsrx::~dbsrx(void){
212
}
213

  
214

  
215
/***********************************************************************
216
 * Tuning
217
 **********************************************************************/
218
void dbsrx::set_lo_freq(double target_freq){
219
    target_freq = std::clip(target_freq, dbsrx_freq_range.min, dbsrx_freq_range.max);
220

  
221
    double actual_freq=0.0, pfd_freq=0.0, ref_clock=0.0;
222
    int R=0, N=0, r=0;
223

  
224
    //choose refclock
225
    std::vector<double> clock_rates = this->get_iface()->get_clock_rates(dboard_iface::UNIT_RX);
226
    BOOST_FOREACH(ref_clock, std::reversed(std::sorted(clock_rates))){
227
        if (ref_clock != 4e6) continue;
228

  
229
        //choose R
230
        for(r = 0; r <= 6; r += 1) {
231
            //compute divider from setting
232
            R = pow(2, r+1);
233
            if (dbsrx_debug) std::cerr << boost::format("DBSRX R:%d\n") % R << std::endl;
234

  
235
            //compute PFD compare frequency = ref_clock/R
236
            pfd_freq = ref_clock / R;
237

  
238
            //constrain the PFD frequency to specified range
239
            if ((pfd_freq < dbsrx_pfd_freq_range.min) or (pfd_freq > dbsrx_pfd_freq_range.max)) continue;
240

  
241
            //compute N
242
            N = int(std::floor(target_freq/pfd_freq));
243

  
244
            //constrain N to specified range
245
            if ((N < 256) or (N > 32768)) continue;
246

  
247
            goto done_loop;
248
        }
249
    } 
250

  
251
    //Assert because we failed to find a suitable combination of ref_clock, R and N 
252
    UHD_ASSERT_THROW((pfd_freq < dbsrx_pfd_freq_range.min) or (pfd_freq > dbsrx_pfd_freq_range.max));
253
    UHD_ASSERT_THROW((N < 256) or (N > 32768));
254
    done_loop:
255

  
256
    //compute resulting output frequency
257
    actual_freq = pfd_freq * N;
258

  
259
    //apply ref_clock, R, and N settings
260
    this->get_iface()->set_clock_rate(dboard_iface::UNIT_RX, ref_clock);
261
    ref_clock = this->get_iface()->get_clock_rate(dboard_iface::UNIT_RX);
262
    _max2118_write_regs.r_divider = (max2118_write_regs_t::r_divider_t) r;
263
    _max2118_write_regs.set_n_divider(N);
264
    _max2118_write_regs.ade_vco_ade_read = max2118_write_regs_t::ADE_VCO_ADE_READ_ENABLED;
265
    send_reg(0x4,0x4);
266
    send_reg(0x0,0x2);
267
    
268
    //compute prescaler variables
269
    int scaler = actual_freq > 1125e6 ? 2 : 4;
270
    _max2118_write_regs.div2 = scaler == 4 ? max2118_write_regs_t::DIV2_DIV4 : max2118_write_regs_t::DIV2_DIV2;
271

  
272
    //compute vco frequency and select vco
273
    double vco_freq = actual_freq * scaler;
274
    int vco;
275
    if (vco_freq < 2433e6)
276
        vco = 0;
277
    else if (vco_freq < 2711e6)
278
        vco=1;
279
    else if (vco_freq < 3025e6)
280
        vco=2;
281
    else if (vco_freq < 3341e6)
282
        vco=3;
283
    else if (vco_freq < 3727e6)
284
        vco=4;
285
    else if (vco_freq < 4143e6)
286
        vco=5;
287
    else if (vco_freq < 4493e6)
288
        vco=6;
289
    else
290
        vco=7;
291

  
292
    //apply vco selection
293
    _max2118_write_regs.osc_band = vco;
294
    send_reg(0x2, 0x2);
295

  
296
    //check vtune for lock condition
297
    read_reg(0x0, 0x1);
298

  
299
    //if we are out of lock for chosen vco, change vco
300
    while ((_max2118_read_regs.adc == 0) or (_max2118_read_regs.adc == 7)){
301
        //vtune is too low, try lower frequency vco
302
        if (_max2118_read_regs.adc == 0){
303
            UHD_ASSERT_THROW(_max2118_write_regs.osc_band <= 0);
304
            _max2118_write_regs.osc_band -= 1;
305
        }
306

  
307
        //vtune is too high, try higher frequency vco
308
        if (_max2118_read_regs.adc == 7){
309
            UHD_ASSERT_THROW(_max2118_write_regs.osc_band >= 7);
310
            _max2118_write_regs.osc_band += 1;
311
        }
312

  
313
        //update vco selection and check vtune
314
        send_reg(0x2, 0x2);
315
        read_reg(0x0, 0x0);
316
    }
317
      
318
    //select charge pump bias current
319
    if (_max2118_read_regs.adc <= 2) _max2118_write_regs.cp_current = max2118_write_regs_t::CP_CURRENT_I_CP_100UA;
320
    else if (_max2118_read_regs.adc >= 5) _max2118_write_regs.cp_current = max2118_write_regs_t::CP_CURRENT_I_CP_400UA;
321
    else _max2118_write_regs.cp_current = max2118_write_regs_t::CP_CURRENT_I_CP_200UA;
322
    
323
    //update charge pump bias current setting
324
    send_reg(0x2, 0x2);
325

  
326
    //compute actual tuned frequency
327
    _lo_freq = this->get_iface()->get_clock_rate(dboard_iface::UNIT_RX) / pow(2,(1 + _max2118_write_regs.r_divider)) * _max2118_write_regs.get_n_divider();
328

  
329
    //debug output of calculated variables
330
    if (dbsrx_debug) std::cerr
331
        << boost::format("DBSRX tune:\n")
332
        << boost::format("    VCO=%d, CP=%d, PFD Freq=%fMHz\n") % int(_max2118_write_regs.osc_band) % _max2118_write_regs.cp_current % (pfd_freq/1e6)
333
        << boost::format("    R=%d, N=%f, scaler=%d, div2=%d\n") % R % N % scaler % int(_max2118_write_regs.div2)
334
        << boost::format("    Ref    Freq=%fMHz\n") % (ref_clock/1e6)
335
        << boost::format("    Target Freq=%fMHz\n") % (target_freq/1e6)
336
        << boost::format("    Actual Freq=%fMHz\n") % (_lo_freq/1e6)
337
        << std::endl;
338
}
339

  
340
/***********************************************************************
341
 * Gain Handling
342
 **********************************************************************/
343
/*!
344
 * Convert a requested gain for the GC2 vga into the integer register value.
345
 * The gain passed into the function will be set to the actual value.
346
 * \param gain the requested gain in dB
347
 * \return 5 bit the register value
348
 */
349
static int gain_to_gc2_vga_reg(float &gain){
350
    int reg = 0;
351
    gain = std::clip<float>(boost::math::iround(gain), dbsrx_gain_ranges["GC2"].min, dbsrx_gain_ranges["GC2"].max);
352

  
353
    // Half dB steps from 0-5dB, 1dB steps from 5-24dB
354
    if (gain < 5) {
355
        reg = boost::math::iround(31.0 - gain/0.5);
356
        gain = float(boost::math::iround(gain)) * 0.5;
357
    } else {
358
        reg = boost::math::iround(22.0 - (gain - 4.0));
359
        gain = float(boost::math::iround(gain));
360
    }
361

  
362
    if (dbsrx_debug) std::cerr << boost::format(
363
        "DBSRX GC2 Gain: %f dB, reg: %d"
364
    ) % gain % reg << std::endl;
365

  
366
    return reg;
367
}
368

  
369
/*!
370
 * Convert a requested gain for the GC1 rf vga into the dac_volts value.
371
 * The gain passed into the function will be set to the actual value.
372
 * \param gain the requested gain in dB
373
 * \return dac voltage value
374
 */
375
static float gain_to_gc1_rfvga_dac(float &gain){
376
    //clip the input
377
    gain = std::clip<float>(gain, dbsrx_gain_ranges["GC1"].min, dbsrx_gain_ranges["GC1"].max);
378

  
379
    //voltage level constants
380
    static const float max_volts = float(2.7), min_volts = float(1.2);
381
    static const float slope = (max_volts-min_volts)/dbsrx_gain_ranges["GC1"].max;
382

  
383
    //calculate the voltage for the aux dac
384
    float dac_volts = gain*slope + min_volts;
385

  
386
    if (dbsrx_debug) std::cerr << boost::format(
387
        "DBSRX GC1 Gain: %f dB, dac_volts: %f V"
388
    ) % gain % dac_volts << std::endl;
389

  
390
    //the actual gain setting
391
    gain = (dac_volts - min_volts)/slope;
392

  
393
    return dac_volts;
394
}
395

  
396
void dbsrx::set_gain(float gain, const std::string &name){
397
    assert_has(dbsrx_gain_ranges.keys(), name, "dbsrx gain name");
398
    if (name == "GC2"){
399
        _max2118_write_regs.gc2 = gain_to_gc2_vga_reg(gain);
400
        send_reg(0x5, 0x5);
401
    }
402
    else if(name == "GC1"){
403
        //write the new voltage to the aux dac
404
        this->get_iface()->write_aux_dac(dboard_iface::UNIT_RX, dboard_iface::AUX_DAC_A, gain_to_gc1_rfvga_dac(gain));
405
    }
406
    else UHD_THROW_INVALID_CODE_PATH();
407
    _gains[name] = gain;
408
}
409

  
410
/***********************************************************************
411
 * Bandwidth Handling
412
 **********************************************************************/
413
void dbsrx::set_bandwidth(float bandwidth){
414
    //clip the input
415
    bandwidth = std::clip<float>(bandwidth, 4e6, 33e6);
416
    
417
    //calculate ref_freq
418
    float ref_freq = this->get_iface()->get_clock_rate(dboard_iface::UNIT_RX);
419

  
420
    //FIXME this contraint needs to be in the set_freq and needs to assert if it can't hit the range
421
    //calculate acceptable m_divider for filter tuning
422
    int m = 1;
423
    while (ref_freq/m < 1e6 or ref_freq/m > 2.5e6){ m++; }
424
    _max2118_write_regs.m_divider = m;
425

  
426
    _bandwidth = float((ref_freq/1e6/_max2118_write_regs.m_divider)*(4+0.145*_max2118_write_regs.f_dac)*1e6);
427

  
428
    _max2118_write_regs.f_dac = int(((bandwidth*_max2118_write_regs.m_divider/ref_freq) - 4)/0.145);
429

  
430
    if (dbsrx_debug) std::cerr << boost::format(
431
        "DBSRX Filter Bandwidth: %f MHz, m: %d, f_dac: %d\n"
432
    ) % (_bandwidth/1e6) % m % int(_max2118_write_regs.f_dac) << std::endl;
433

  
434
    this->send_reg(0x3, 0x5);
435
}
436

  
437
/***********************************************************************
438
 * RX Get and Set
439
 **********************************************************************/
440
void dbsrx::rx_get(const wax::obj &key_, wax::obj &val){
441
    wax::obj key; std::string name;
442
    boost::tie(key, name) = extract_named_prop(key_);
443

  
444
    //handle the get request conditioned on the key
445
    switch(key.as<subdev_prop_t>()){
446
    case SUBDEV_PROP_NAME:
447
        val = get_rx_id().to_pp_string();
448
        return;
449

  
450
    case SUBDEV_PROP_OTHERS:
451
        val = prop_names_t(); //empty
452
        return;
453

  
454
    case SUBDEV_PROP_GAIN:
455
        assert_has(_gains.keys(), name, "dbsrx gain name");
456
        val = _gains[name];
457
        return;
458

  
459
    case SUBDEV_PROP_GAIN_RANGE:
460
        assert_has(dbsrx_gain_ranges.keys(), name, "dbsrx gain name");
461
        val = dbsrx_gain_ranges[name];
462
        return;
463

  
464
    case SUBDEV_PROP_GAIN_NAMES:
465
        val = prop_names_t(dbsrx_gain_ranges.keys());
466
        return;
467

  
468
    case SUBDEV_PROP_FREQ:
469
        val = _lo_freq;
470
        return;
471

  
472
    case SUBDEV_PROP_FREQ_RANGE:
473
        val = dbsrx_freq_range;
474
        return;
475

  
476
    case SUBDEV_PROP_ANTENNA:
477
        val = std::string("J3");
478
        return;
479

  
480
    case SUBDEV_PROP_ANTENNA_NAMES:
481
        val = dbsrx_antennas;
482
        return;
483

  
484
/*
485
    case SUBDEV_PROP_QUADRATURE:
486
        val = true;
487
        return;
488

  
489
    case SUBDEV_PROP_IQ_SWAPPED:
490
        val = false;
491
        return;
492

  
493
    case SUBDEV_PROP_SPECTRUM_INVERTED:
494
        val = false;
495
        return;
496
*/
497
    case SUBDEV_PROP_CONNECTION:
498
        val = SUBDEV_CONN_COMPLEX_IQ;
499
        return;
500

  
501
    case SUBDEV_PROP_USE_LO_OFFSET:
502
        val = false;
503
        return;
504

  
505
    case SUBDEV_PROP_LO_LOCKED:
506
        val = this->get_locked();
507
        return;
508

  
509
/*
510
    case SUBDEV_PROP_RSSI:
511
        val = this->get_rssi();
512
        return;
513
*/
514

  
515
    case SUBDEV_PROP_BANDWIDTH:
516
        val = _bandwidth;
517
        return;
518

  
519
    default: UHD_THROW_PROP_GET_ERROR();
520
    }
521
}
522

  
523
void dbsrx::rx_set(const wax::obj &key_, const wax::obj &val){
524
    wax::obj key; std::string name;
525
    boost::tie(key, name) = extract_named_prop(key_);
526

  
527
    //handle the get request conditioned on the key
528
    switch(key.as<subdev_prop_t>()){
529

  
530
    case SUBDEV_PROP_FREQ:
531
        this->set_lo_freq(val.as<double>());
532
        return;
533

  
534
    case SUBDEV_PROP_GAIN:
535
        this->set_gain(val.as<float>(), name);
536
        return;
537

  
538
    case SUBDEV_PROP_BANDWIDTH:
539
        this->set_bandwidth(val.as<float>());
540
        return;
541

  
542
    default: UHD_THROW_PROP_SET_ERROR();
543
    }
544
}
545

  
b/host/lib/usrp/usrp2/clock_ctrl.cpp
86 86

  
87 87
    void set_rate_rx_dboard_clock(double rate){
88 88
        assert_has(get_rates_rx_dboard_clock(), rate, "rx dboard clock rate");
89
        size_t divider = size_t(rate/get_master_clock_rate());
89
        size_t divider = size_t(get_master_clock_rate()/rate);
90 90
        //bypass when the divider ratio is one
91 91
        _ad9510_regs.bypass_divider_out7 = (divider == 1)? 1 : 0;
92 92
        //calculate the low and high dividers
......
118 118

  
119 119
    void set_rate_tx_dboard_clock(double rate){
120 120
        assert_has(get_rates_tx_dboard_clock(), rate, "tx dboard clock rate");
121
        size_t divider = size_t(rate/get_master_clock_rate());
121
        size_t divider = size_t(get_master_clock_rate()/rate);
122 122
        //bypass when the divider ratio is one
123 123
        _ad9510_regs.bypass_divider_out6 = (divider == 1)? 1 : 0;
124 124
        //calculate the low and high dividers

Also available in: Unified diff