Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.81 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 "adf4350_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_version3::sbx_version3(sbx_xcvr *_self_sbx_xcvr) {
31
    //register the handle to our base SBX class
32
    self_base = _self_sbx_xcvr;
33
}
34

    
35
sbx_xcvr::sbx_version3::~sbx_version3(void){
36
    /* NOP */
37
}
38

    
39

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

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

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

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

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

    
73
    //Reference doubler for 50% duty cycle
74
    // if ref_freq < 12.5MHz enable regs.reference_divide_by_2
75
    if(ref_freq <= 12.5e6) D = adf4350_regs_t::REFERENCE_DOUBLER_ENABLED;
76

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

    
85
    //use 8/9 prescaler for vco_freq > 3 GHz (pg.18 prescaler)
86
    adf4350_regs_t::prescaler_t prescaler = vco_freq > 3e9 ? adf4350_regs_t::PRESCALER_8_9 : adf4350_regs_t::PRESCALER_4_5;
87

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

    
109
        //keep the PFD frequency at or below 25MHz (Loop Filter Bandwidth)
110
        if (pfd_freq > 25e6) continue;
111

    
112
        //ignore fractional part of tuning
113
        N = int(std::floor(vco_freq/pfd_freq));
114

    
115
        //keep N > minimum int divider requirement
116
        if (N < prescaler_to_min_int_div[prescaler]) continue;
117

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

    
126
    //Fractional-N calculation
127
    MOD = 4095; //max fractional accuracy
128
    FRAC = int((vco_freq/pfd_freq - N)*MOD);
129

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

    
137
    //actual frequency calculation
138
    actual_freq = double((N + (double(FRAC)/double(MOD)))*ref_freq*(1+int(D))/(R*(1+int(T)))/RFdiv);
139

    
140
    UHD_LOGV(often)
141
        << 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
142
        << boost::format("SBX tune: R=%d, BS=%d, N=%d, FRAC=%d, MOD=%d, T=%d, D=%d, RFdiv=%d"
143
            ) % R % BS % N % FRAC % MOD % T % D % RFdiv << std::endl
144
        << boost::format("SBX Frequencies (MHz): REQ=%0.2f, ACT=%0.2f, VCO=%0.2f, PFD=%0.2f, BAND=%0.2f"
145
            ) % (target_freq/1e6) % (actual_freq/1e6) % (vco_freq/1e6) % (pfd_freq/1e6) % (pfd_freq/BS/1e6) << std::endl;
146

    
147
    //load the register values
148
    adf4350_regs_t regs;
149

    
150
    if ((unit == dboard_iface::UNIT_TX) and (actual_freq == sbx_tx_lo_2dbm.clip(actual_freq))) 
151
        regs.output_power = adf4350_regs_t::OUTPUT_POWER_2DBM;
152
    else
153
        regs.output_power = adf4350_regs_t::OUTPUT_POWER_5DBM;
154

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

    
166
    //write the registers
167
    //correct power-up sequence to write registers (5, 4, 3, 2, 1, 0)
168
    int addr;
169

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

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