Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp2 / codec_ctrl.cpp @ 476afe68

History | View | Annotate | Download (5.76 KB)

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
#include "codec_ctrl.hpp"
19
#include "ad9777_regs.hpp"
20
#include "ads62p44_regs.hpp"
21
#include "usrp2_regs.hpp"
22
#include <boost/cstdint.hpp>
23
#include <boost/foreach.hpp>
24
#include <iostream>
25
#include <uhd/utils/exception.hpp>
26

    
27
static const bool codec_ctrl_debug = false;
28

    
29
using namespace uhd;
30

    
31
/*!
32
 * A usrp2 codec control specific to the ad9777 ic.
33
 */
34
class usrp2_codec_ctrl_impl : public usrp2_codec_ctrl{
35
public:
36
    usrp2_codec_ctrl_impl(usrp2_iface::sptr iface){
37
        _iface = iface;
38

    
39
        //setup the ad9777 dac
40
        _ad9777_regs.x_1r_2r_mode = ad9777_regs_t::X_1R_2R_MODE_1R;
41
        _ad9777_regs.filter_interp_rate = ad9777_regs_t::FILTER_INTERP_RATE_4X;
42
        _ad9777_regs.mix_mode = ad9777_regs_t::MIX_MODE_REAL;
43
        _ad9777_regs.pll_divide_ratio = ad9777_regs_t::PLL_DIVIDE_RATIO_DIV1;
44
        _ad9777_regs.pll_state = ad9777_regs_t::PLL_STATE_ON;
45
        _ad9777_regs.auto_cp_control = ad9777_regs_t::AUTO_CP_CONTROL_AUTO;
46
        //I dac values
47
        _ad9777_regs.idac_fine_gain_adjust = 0;
48
        _ad9777_regs.idac_coarse_gain_adjust = 0xf;
49
        _ad9777_regs.idac_offset_adjust_lsb = 0;
50
        _ad9777_regs.idac_offset_adjust_msb = 0;
51
        //Q dac values
52
        _ad9777_regs.qdac_fine_gain_adjust = 0;
53
        _ad9777_regs.qdac_coarse_gain_adjust = 0xf;
54
        _ad9777_regs.qdac_offset_adjust_lsb = 0;
55
        _ad9777_regs.qdac_offset_adjust_msb = 0;
56
        //write all regs
57
        for(boost::uint8_t addr = 0; addr <= 0xC; addr++){
58
            this->send_ad9777_reg(addr);
59
        }
60

    
61
        //power-up adc
62
        switch(_iface->get_rev()){
63
        case usrp2_iface::USRP2_REV3:
64
        case usrp2_iface::USRP2_REV4:
65
            _iface->poke32(_iface->regs.misc_ctrl_adc, U2_FLAG_MISC_CTRL_ADC_ON);
66
            break;
67

    
68
        case usrp2_iface::USRP_N200:
69
        case usrp2_iface::USRP_N210:
70
            _ads62p44_regs.reset = 1;
71
            this->send_ads62p44_reg(0x00); //issue a reset to the ADC
72
            //everything else should be pretty much default, i think
73
            //_ads62p44_regs.decimation = DECIMATION_DECIMATE_1;
74
            _ads62p44_regs.power_down = ads62p44_regs_t::POWER_DOWN_NORMAL;
75
            this->send_ads62p44_reg(0x14);
76
            break;
77

    
78
        case usrp2_iface::USRP_NXXX: break;
79
        }
80
    }
81

    
82
    ~usrp2_codec_ctrl_impl(void){
83
        //power-down dac
84
        _ad9777_regs.power_down_mode = 1;
85
        this->send_ad9777_reg(0);
86

    
87
        //power-down adc
88
        switch(_iface->get_rev()){
89
        case usrp2_iface::USRP2_REV3:
90
        case usrp2_iface::USRP2_REV4:
91
            _iface->poke32(_iface->regs.misc_ctrl_adc, U2_FLAG_MISC_CTRL_ADC_OFF);
92
            break;
93

    
94
        case usrp2_iface::USRP_N200:
95
        case usrp2_iface::USRP_N210:
96
            //send a global power-down to the ADC here... it will get lifted on reset
97
            _ads62p44_regs.power_down = ads62p44_regs_t::POWER_DOWN_GLOBAL_PD;
98
            this->send_ads62p44_reg(0x14);
99
            break;
100

    
101
        case usrp2_iface::USRP_NXXX: break;
102
        }
103
    }
104

    
105
    void set_rx_digital_gain(float gain) {  //fine digital gain
106
        switch(_iface->get_rev()){
107
        case usrp2_iface::USRP_N200:
108
        case usrp2_iface::USRP_N210:
109
            _ads62p44_regs.fine_gain = int(gain/0.5);
110
            this->send_ads62p44_reg(0x17);
111
            break;
112

    
113
        default: UHD_THROW_INVALID_CODE_PATH();
114
        }
115
    }
116

    
117
    void set_rx_digital_fine_gain(float gain) { //gain correction      
118
        switch(_iface->get_rev()){
119
        case usrp2_iface::USRP_N200:
120
        case usrp2_iface::USRP_N210:
121
            _ads62p44_regs.gain_correction = int(gain / 0.05);
122
            this->send_ads62p44_reg(0x1A);
123
            break;
124

    
125
        default: UHD_THROW_INVALID_CODE_PATH();
126
        }
127
    }
128

    
129
    void set_rx_analog_gain(bool gain) { //turns on/off analog 3.5dB preamp
130
        switch(_iface->get_rev()){
131
        case usrp2_iface::USRP_N200:
132
        case usrp2_iface::USRP_N210:
133
            _ads62p44_regs.coarse_gain = gain ? ads62p44_regs_t::COARSE_GAIN_3_5DB : ads62p44_regs_t::COARSE_GAIN_0DB;
134
            this->send_ads62p44_reg(0x14);
135
            break;
136

    
137
        default: UHD_THROW_INVALID_CODE_PATH();
138
        }
139
    }
140

    
141
private:
142
    ad9777_regs_t _ad9777_regs;
143
    ads62p44_regs_t _ads62p44_regs;
144
    usrp2_iface::sptr _iface;
145

    
146
    void send_ad9777_reg(boost::uint8_t addr){
147
        boost::uint16_t reg = _ad9777_regs.get_write_reg(addr);
148
        if (codec_ctrl_debug) std::cout << "send_ad9777_reg: " << std::hex << reg << std::endl;
149
        _iface->transact_spi(
150
            SPI_SS_AD9777, spi_config_t::EDGE_RISE,
151
            reg, 16, false /*no rb*/
152
        );
153
    }
154

    
155
    void send_ads62p44_reg(boost::uint8_t addr) {
156
        boost::uint16_t reg = _ads62p44_regs.get_write_reg(addr);
157
        _iface->transact_spi(
158
            SPI_SS_ADS62P44, spi_config_t::EDGE_FALL,
159
            reg, 16, false /*no rb*/
160
        );
161
    }
162
};
163

    
164
/***********************************************************************
165
 * Public make function for the usrp2 codec control
166
 **********************************************************************/
167
usrp2_codec_ctrl::sptr usrp2_codec_ctrl::make(usrp2_iface::sptr iface){
168
    return sptr(new usrp2_codec_ctrl_impl(iface));
169
}