Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / dboard / db_sbx_version4.cpp @ 09de3c07

History | View | Annotate | Download (7.32 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
    double vco_freq = target_freq;
82
    while (vco_freq < 2.2e9) {
83
        vco_freq *= 2;
84
        RFdiv *= 2;
85
    }
86

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

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

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

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

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

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

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

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

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

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

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

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

    
157
    regs.frac_12_bit = FRAC;
158
    regs.int_16_bit = N;
159
    regs.mod_12_bit = MOD;
160
    regs.clock_divider_12_bit = std::max(1, int(std::ceil(400e-6*pfd_freq/MOD)));
161
    regs.feedback_select = adf4351_regs_t::FEEDBACK_SELECT_DIVIDED;
162
    regs.clock_div_mode = adf4351_regs_t::CLOCK_DIV_MODE_RESYNC_ENABLE;
163
    regs.prescaler = prescaler;
164
    regs.r_counter_10_bit = R;
165
    regs.reference_divide_by_2 = T;
166
    regs.reference_doubler = D;
167
    regs.band_select_clock_div = BS;
168
    UHD_ASSERT_THROW(rfdivsel_to_enum.has_key(RFdiv));
169
    regs.rf_divider_select = rfdivsel_to_enum[RFdiv];
170

    
171
    //reset the N and R counter
172
    regs.counter_reset = adf4351_regs_t::COUNTER_RESET_ENABLED;
173
    self_base->get_iface()->write_spi(unit, spi_config_t::EDGE_RISE, regs.get_reg(2), 32);
174
    regs.counter_reset = adf4351_regs_t::COUNTER_RESET_DISABLED;
175

    
176
    //write the registers
177
    //correct power-up sequence to write registers (5, 4, 3, 2, 1, 0)
178
    int addr;
179

    
180
    for(addr=5; addr>=0; addr--){
181
        UHD_LOGV(often) << boost::format(
182
            "SBX SPI Reg (0x%02x): 0x%08x"
183
        ) % addr % regs.get_reg(addr) << std::endl;
184
        self_base->get_iface()->write_spi(
185
            unit, spi_config_t::EDGE_RISE,
186
            regs.get_reg(addr), 32
187
        );
188
    }
189

    
190
    //return the actual frequency
191
    UHD_LOGV(often) << boost::format(
192
        "SBX tune: actual frequency %f Mhz"
193
    ) % (actual_freq/1e6) << std::endl;
194
    return actual_freq;
195
}
196