aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/usrp/common/ad9361_driver/ad9361_device.h
blob: 0c7a7e4f7873b58d0dba54005e9a533301ebeb8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// Copyright 2014 Ettus Research LLC
//

#ifndef INCLUDED_AD9361_DEVICE_H
#define INCLUDED_AD9361_DEVICE_H

#include <ad9361_client.h>
#include <boost/noncopyable.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include <uhd/types/filters.hpp>
#include <uhd/types/sensors.hpp>
#include <complex>
#include <vector>
#include <map>
#include "boost/assign.hpp"
#include "boost/bind.hpp"
#include "boost/function.hpp"

namespace uhd { namespace usrp {

class ad9361_device_t : public boost::noncopyable
{
public:
    enum direction_t { RX, TX };
    enum gain_mode_t {GAIN_MODE_MANUAL, GAIN_MODE_SLOW_AGC, GAIN_MODE_FAST_AGC};
    enum chain_t { CHAIN_1, CHAIN_2, CHAIN_BOTH };

    ad9361_device_t(ad9361_params::sptr client, ad9361_io::sptr io_iface) :
        _client_params(client), _io_iface(io_iface) {

        _rx_filters = boost::assign::map_list_of("LPF_TIA", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_lp_tia_sec, this, _1),
                                                    boost::bind(&ad9361_device_t::_set_filter_lp_tia_sec, this, _1, _3)))
                                            ("LPF_BB", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_lp_bb, this, _1),
                                                    boost::bind(&ad9361_device_t::_set_filter_lp_bb, this, _1, _3)))
                                            ("HB_3", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_hb_3, this, _1), 0))
                                            ("DEC_3", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_dec_int_3, this, _1), 0))
                                            ("HB_2", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_hb_2, this, _1), 0))
                                            ("HB_1", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_hb_1, this, _1), 0))
                                            ("FIR_1", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_fir, this, _1, _2),
                                                    boost::bind(&ad9361_device_t::_set_filter_fir, this, _1, _2, _3)));

        _tx_filters = boost::assign::map_list_of("LPF_SECONDARY", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_lp_tia_sec, this, _1),
                                                    boost::bind(&ad9361_device_t::_set_filter_lp_tia_sec, this, _1, _3)))
                                            ("LPF_BB", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_lp_bb, this, _1),
                                                    boost::bind(&ad9361_device_t::_set_filter_lp_bb, this, _1, _3)))
                                            ("HB_3", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_hb_3, this, _1), 0))
                                            ("INT_3", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_dec_int_3, this, _1), 0))
                                            ("HB_2", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_hb_2, this, _1), 0))
                                            ("HB_1", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_hb_1, this, _1), 0))
                                            ("FIR_1", filter_query_helper(boost::bind(&ad9361_device_t::_get_filter_fir, this, _1, _2),
                                                    boost::bind(&ad9361_device_t::_set_filter_fir, this, _1, _2, _3)));
    }

    /* Initialize the AD9361 codec. */
    void initialize();

    /* This function sets the RX / TX rate between AD9361 and the FPGA, and
     * thus determines the interpolation / decimation required in the FPGA to
     * achieve the user's requested rate.
     */
    double set_clock_rate(const double req_rate);

    /* Set which of the four TX / RX chains provided by AD9361 are active.
     *
     * AD9361 provides two sets of chains, Side A and Side B. Each side
     * provides one TX antenna, and one RX antenna. The B200 maintains the USRP
     * standard of providing one antenna connection that is both TX & RX, and
     * one that is RX-only - for each chain. Thus, the possible antenna and
     * chain selections are:
     *
     */
    void set_active_chains(bool tx1, bool tx2, bool rx1, bool rx2);

    /* Tune the RX or TX frequency.
     *
     * This is the publicly-accessible tune function. It makes sure the tune
     * isn't a redundant request, and if not, passes it on to the class's
     * internal tune function.
     *
     * After tuning, it runs any appropriate calibrations. */
    double tune(direction_t direction, const double value);

    /* Set the gain of RX1, RX2, TX1, or TX2.
     *
     * Note that the 'value' passed to this function is the actual gain value,
     * _not_ the gain index. This is the opposite of the eval software's GUI!
     * Also note that the RX chains are done in terms of gain, and the TX chains
     * are done in terms of attenuation. */
    double set_gain(direction_t direction, chain_t chain, const double value);

    /* Make AD9361 output its test tone. */
    void output_test_tone();

    /* Turn on/off AD9361's TX port --> RX port loopback. */
    void data_port_loopback(const bool loopback_enabled);

    /* Read back the internal RSSI measurement data. */
    double get_rssi(chain_t chain);

    /*! Read the internal temperature sensor
     *\param calibrate return raw sensor readings or apply calibration factor.
     *\param num_samples number of measurements to average over
     */
    double get_average_temperature(const double cal_offset = -30.0, const size_t num_samples = 3);

    /* Turn on/off AD9361's RX DC offset correction */
    void set_dc_offset_auto(direction_t direction, const bool on);

    /* Turn on/off AD9361's RX IQ imbalance correction */
    void set_iq_balance_auto(direction_t direction, const bool on);

    /* Configure AD9361's AGC module to use either fast or slow AGC mode. */
    void set_agc_mode(chain_t chain, gain_mode_t gain_mode);

    /* Enable AD9361's AGC gain mode. */
    void set_agc(chain_t chain, bool enable);

    /* Set bandwidth of AD9361's analog LP filters.
     * Bandwidth should be RF bandwidth */
    double set_bw_filter(direction_t direction, const double rf_bw);

    /*
     * Filter API implementation
     * */
    filter_info_base::sptr get_filter(direction_t direction, chain_t chain, const std::string &name);

    void set_filter(direction_t direction, chain_t chain, const std::string &name, filter_info_base::sptr filter);

    std::vector<std::string> get_filter_names(direction_t direction);

    //Constants
    static const double AD9361_MAX_GAIN;
    static const double AD9361_MAX_CLOCK_RATE;
    static const double AD9361_CAL_VALID_WINDOW;
    static const double AD9361_RECOMMENDED_MAX_BANDWIDTH;

private:    //Methods
    void _program_fir_filter(direction_t direction, int num_taps, boost::uint16_t *coeffs);
    void _setup_tx_fir(size_t num_taps, boost::int32_t interpolation);
    void _setup_rx_fir(size_t num_taps, boost::int32_t decimation);
    void _program_fir_filter(direction_t direction, chain_t chain, int num_taps, boost::uint16_t *coeffs);
    void _setup_tx_fir(size_t num_taps);
    void _setup_rx_fir(size_t num_taps);
    void _calibrate_lock_bbpll();
    void _calibrate_synth_charge_pumps();
    double _calibrate_baseband_rx_analog_filter(double rfbw);
    double _calibrate_baseband_tx_analog_filter(double rfbw);
    double _calibrate_secondary_tx_filter(double rfbw);
    double _calibrate_rx_TIAs(double rfbw);
    void _setup_adc();
    void _calibrate_baseband_dc_offset();
    void _calibrate_rf_dc_offset();
    void _calibrate_rx_quadrature();
    void _tx_quadrature_cal_routine();
    void _calibrate_tx_quadrature();
    void _program_mixer_gm_subtable();
    void _program_gain_table();
    void _setup_gain_control(bool use_agc);
    void _setup_synth(direction_t direction, double vcorate);
    double _tune_bbvco(const double rate);
    void _reprogram_gains();
    double _tune_helper(direction_t direction, const double value);
    double _setup_rates(const double rate);
    double _get_temperature(const double cal_offset, const double timeout = 0.1);
    void _configure_bb_rf_dc_tracking(const bool on);
    void _setup_agc(chain_t chain, gain_mode_t gain_mode);
    void _set_fir_taps(direction_t direction, chain_t chain, const std::vector<boost::int16_t>& taps);
    std::vector<boost::int16_t> _get_fir_taps(direction_t direction, chain_t chain);
    size_t _get_num_fir_taps(direction_t direction);
    size_t _get_fir_dec_int(direction_t direction);
    filter_info_base::sptr _get_filter_lp_tia_sec(direction_t direction);
    filter_info_base::sptr _get_filter_lp_bb(direction_t direction);
    filter_info_base::sptr _get_filter_dec_int_3(direction_t direction);
    filter_info_base::sptr _get_filter_hb_3(direction_t direction);
    filter_info_base::sptr _get_filter_hb_2(direction_t direction);
    filter_info_base::sptr _get_filter_hb_1(direction_t direction);
    filter_info_base::sptr _get_filter_fir(direction_t direction, chain_t chain);
    void _set_filter_fir(direction_t direction, chain_t channel, filter_info_base::sptr filter);
    void _set_filter_lp_bb(direction_t direction, filter_info_base::sptr filter);
    void _set_filter_lp_tia_sec(direction_t direction, filter_info_base::sptr filter);

private:    //Members
    typedef struct {
        boost::uint8_t vcodivs;
        boost::uint8_t inputsel;
        boost::uint8_t rxfilt;
        boost::uint8_t txfilt;
        boost::uint8_t bbpll;
        boost::uint8_t bbftune_config;
        boost::uint8_t bbftune_mode;
    } chip_regs_t;

    struct filter_query_helper
    {
        filter_query_helper(
                boost::function<filter_info_base::sptr (direction_t, chain_t)> p_get,
                boost::function<void (direction_t, chain_t, filter_info_base::sptr)> p_set
                ) : get(p_get), set(p_set) {  }

        filter_query_helper(){ }

        boost::function<filter_info_base::sptr (direction_t, chain_t)> get;
        boost::function<void (direction_t, chain_t, filter_info_base::sptr)> set;
    };

    std::map<std::string, filter_query_helper> _rx_filters;
    std::map<std::string, filter_query_helper> _tx_filters;

    //Interfaces
    ad9361_params::sptr _client_params;
    ad9361_io::sptr     _io_iface;
    //Intermediate state
    double              _rx_freq, _tx_freq, _req_rx_freq, _req_tx_freq;
    double              _last_calibration_freq;
    double              _baseband_bw, _bbpll_freq, _adcclock_freq;
    double              _rx_analog_bw, _tx_analog_bw, _rx_bb_lp_bw, _tx_bb_lp_bw;
    double              _rx_tia_lp_bw, _tx_sec_lp_bw;
    double              _req_clock_rate, _req_coreclk;
    boost::uint16_t     _rx_bbf_tunediv;
    boost::uint8_t      _curr_gain_table;
    double              _rx1_gain, _rx2_gain, _tx1_gain, _tx2_gain;
    boost::int32_t      _tfir_factor;
    boost::int32_t      _rfir_factor;
    gain_mode_t         _rx1_agc_mode, _rx2_agc_mode;
    bool                _rx1_agc_enable, _rx2_agc_enable;
    //Register soft-copies
    chip_regs_t         _regs;
    //Synchronization
    boost::recursive_mutex  _mutex;
    bool _use_dc_offset_correction;
    bool _use_iq_balance_correction;
};

}}  //namespace

#endif /* INCLUDED_AD9361_DEVICE_H */