Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / dboard / db_sbx_version4.cpp @ 40576b23

History | View | Annotate | Download (6.91 KB)

1
//
2
// Copyright 2011-2012 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

    
19
#include "adf4351_regs.hpp"
20
#include "db_sbx_common.hpp"
21

    
22

    
23
using namespace uhd;
24
using namespace uhd::usrp;
25
using namespace boost::assign;
26

    
27
/***********************************************************************
28
 * Structors
29
 **********************************************************************/
30
sbx_xcvr::sbx_version4::sbx_version4(sbx_xcvr *_self_sbx_xcvr) {
31
    //register the handle to our base SBX class
32
    self_base = _self_sbx_xcvr;
33
}
34

    
35

    
36
sbx_xcvr::sbx_version4::~sbx_version4(void){
37
    /* NOP */
38
}
39

    
40

    
41
/***********************************************************************
42
 * Tuning
43
 **********************************************************************/
44
double sbx_xcvr::sbx_version4::set_lo_freq(dboard_iface::unit_t unit, double target_freq) {
45
    UHD_LOGV(often) << boost::format(
46
        "SBX tune: target frequency %f Mhz"
47
    ) % (target_freq/1e6) << std::endl;
48

    
49
    //clip the input
50
    target_freq = sbx_freq_range.clip(target_freq);
51

    
52
    //map prescaler setting to mininmum integer divider (N) values (pg.18 prescaler)
53
    static const uhd::dict<int, int> prescaler_to_min_int_div = map_list_of
54
        (0,23) //adf4351_regs_t::PRESCALER_4_5
55
        (1,75) //adf4351_regs_t::PRESCALER_8_9
56
    ;
57

    
58
    //map rf divider select output dividers to enums
59
    static const uhd::dict<int, adf4351_regs_t::rf_divider_select_t> rfdivsel_to_enum = map_list_of
60
        (1,  adf4351_regs_t::RF_DIVIDER_SELECT_DIV1)
61
        (2,  adf4351_regs_t::RF_DIVIDER_SELECT_DIV2)
62
        (4,  adf4351_regs_t::RF_DIVIDER_SELECT_DIV4)
63
        (8,  adf4351_regs_t::RF_DIVIDER_SELECT_DIV8)
64
        (16, adf4351_regs_t::RF_DIVIDER_SELECT_DIV16)
65
        (32, adf4351_regs_t::RF_DIVIDER_SELECT_DIV32)
66
        (64, adf4351_regs_t::RF_DIVIDER_SELECT_DIV64)
67
    ;
68

    
69
    double actual_freq, pfd_freq;
70
    double ref_freq = self_base->get_iface()->get_clock_rate(unit);
71
    int R=0, BS=0, N=0, FRAC=0, MOD=0;
72
    int RFdiv = 1;
73
    adf4351_regs_t::reference_divide_by_2_t T     = adf4351_regs_t::REFERENCE_DIVIDE_BY_2_DISABLED;
74
    adf4351_regs_t::reference_doubler_t     D     = adf4351_regs_t::REFERENCE_DOUBLER_DISABLED;    
75

    
76
    //Reference doubler for 50% duty cycle
77
    // if ref_freq < 12.5MHz enable regs.reference_divide_by_2
78
    if(ref_freq <= 12.5e6) D = adf4351_regs_t::REFERENCE_DOUBLER_ENABLED;
79

    
80
    //increase RF divider until acceptable VCO frequency
81
    //start with target_freq*2 because mixer has divide by 2
82
    double vco_freq = target_freq;
83
    while (vco_freq < 2.2e9) {
84
        vco_freq *= 2;
85
        RFdiv *= 2;
86
    }
87

    
88
    //use 8/9 prescaler for vco_freq > 3 GHz (pg.18 prescaler)
89
    adf4351_regs_t::prescaler_t prescaler = vco_freq > 3e9 ? adf4351_regs_t::PRESCALER_8_9 : adf4351_regs_t::PRESCALER_4_5;
90

    
91
    /*
92
     * The goal here is to loop though possible R dividers,
93
     * band select clock dividers, N (int) dividers, and FRAC 
94
     * (frac) dividers.
95
     *
96
     * Calculate the N and F dividers for each set of values.
97
     * The loop exists when it meets all of the constraints.
98
     * The resulting loop values are loaded into the registers.
99
     *
100
     * from pg.21
101
     *
102
     * f_pfd = f_ref*(1+D)/(R*(1+T))
103
     * f_vco = (N + (FRAC/MOD))*f_pfd
104
     *    N = f_vco/f_pfd - FRAC/MOD = f_vco*((R*(T+1))/(f_ref*(1+D))) - FRAC/MOD
105
     * f_rf = f_vco/RFdiv)
106
     * f_actual = f_rf/2
107
     */
108
    for(R = 1; R <= 1023; R+=1){
109
        //PFD input frequency = f_ref/R ... ignoring Reference doubler/divide-by-2 (D & T)
110
        pfd_freq = ref_freq*(1+D)/(R*(1+T));
111

    
112
        //keep the PFD frequency at or below 25MHz (Loop Filter Bandwidth)
113
        if (pfd_freq > 25e6) continue;
114

    
115
        //ignore fractional part of tuning
116
        N = int(std::floor(vco_freq/pfd_freq));
117

    
118
        //keep N > minimum int divider requirement
119
        if (N < prescaler_to_min_int_div[prescaler]) continue;
120

    
121
        for(BS=1; BS <= 255; BS+=1){
122
            //keep the band select frequency at or below 100KHz
123
            //constraint on band select clock
124
            if (pfd_freq/BS > 100e3) continue;
125
            goto done_loop;
126
        }
127
    } done_loop:
128

    
129
    //Fractional-N calculation
130
    MOD = 4095; //max fractional accuracy
131
    FRAC = int((vco_freq/pfd_freq - N)*MOD);
132

    
133
    //Reference divide-by-2 for 50% duty cycle
134
    // if R even, move one divide by 2 to to regs.reference_divide_by_2
135
    if(R % 2 == 0){
136
        T = adf4351_regs_t::REFERENCE_DIVIDE_BY_2_ENABLED;
137
        R /= 2;
138
    }
139

    
140
    //actual frequency calculation
141
    actual_freq = double((N + (double(FRAC)/double(MOD)))*ref_freq*(1+int(D))/(R*(1+int(T)))/RFdiv);
142

    
143
    UHD_LOGV(often)
144
        << boost::format("SBX Intermediates: ref=%0.2f, outdiv=%f, fbdiv=%f") % (ref_freq*(1+int(D))/(R*(1+int(T)))) % double(RFdiv*2) % double(N + double(FRAC)/double(MOD)) << std::endl
145
        << boost::format("SBX tune: R=%d, BS=%d, N=%d, FRAC=%d, MOD=%d, T=%d, D=%d, RFdiv=%d"
146
            ) % R % BS % N % FRAC % MOD % T % D % RFdiv << std::endl
147
        << boost::format("SBX Frequencies (MHz): REQ=%0.2f, ACT=%0.2f, VCO=%0.2f, PFD=%0.2f, BAND=%0.2f"
148
            ) % (target_freq/1e6) % (actual_freq/1e6) % (vco_freq/1e6) % (pfd_freq/1e6) % (pfd_freq/BS/1e6) << std::endl;
149

    
150
    //load the register values
151
    adf4351_regs_t regs;
152

    
153
    if ((unit == dboard_iface::UNIT_TX) and (actual_freq == sbx_tx_lo_2dbm.clip(actual_freq))) 
154
        regs.output_power = adf4351_regs_t::OUTPUT_POWER_2DBM;
155
    else
156
        regs.output_power = adf4351_regs_t::OUTPUT_POWER_5DBM;
157

    
158
    regs.frac_12_bit = FRAC;
159
    regs.int_16_bit = N;
160
    regs.mod_12_bit = MOD;
161
    regs.prescaler = prescaler;
162
    regs.r_counter_10_bit = R;
163
    regs.reference_divide_by_2 = T;
164
    regs.reference_doubler = D;
165
    regs.band_select_clock_div = BS;
166
    UHD_ASSERT_THROW(rfdivsel_to_enum.has_key(RFdiv));
167
    regs.rf_divider_select = rfdivsel_to_enum[RFdiv];
168

    
169
    //write the registers
170
    //correct power-up sequence to write registers (5, 4, 3, 2, 1, 0)
171
    int addr;
172

    
173
    for(addr=5; addr>=0; addr--){
174
        UHD_LOGV(often) << boost::format(
175
            "SBX SPI Reg (0x%02x): 0x%08x"
176
        ) % addr % regs.get_reg(addr) << std::endl;
177
        self_base->get_iface()->write_spi(
178
            unit, spi_config_t::EDGE_RISE,
179
            regs.get_reg(addr), 32
180
        );
181
    }
182

    
183
    //return the actual frequency
184
    UHD_LOGV(often) << boost::format(
185
        "SBX tune: actual frequency %f Mhz"
186
    ) % (actual_freq/1e6) << std::endl;
187
    return actual_freq;
188
}
189