Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / codec_ctrl.cpp @ 0d5cef73

History | View | Annotate | Download (14.9 KB)

1
//
2
// Copyright 2010-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
#include "codec_ctrl.hpp"
19
#include "ad9862_regs.hpp"
20
#include <uhd/utils/log.hpp>
21
#include <uhd/utils/safe_call.hpp>
22
#include <uhd/types/dict.hpp>
23
#include <uhd/exception.hpp>
24
#include <uhd/utils/algorithm.hpp>
25
#include <uhd/utils/byteswap.hpp>
26
#include <boost/cstdint.hpp>
27
#include <boost/format.hpp>
28
#include <boost/tuple/tuple.hpp>
29
#include <boost/math/special_functions/round.hpp>
30
#include <boost/math/special_functions/sign.hpp>
31
#include <boost/assign/list_of.hpp>
32
#include <iomanip>
33

    
34
using namespace uhd;
35

    
36
const gain_range_t usrp1_codec_ctrl::tx_pga_gain_range(-20, 0, double(0.1));
37
const gain_range_t usrp1_codec_ctrl::rx_pga_gain_range(0, 20, 1);
38

    
39
/***********************************************************************
40
 * Codec Control Implementation
41
 **********************************************************************/
42
class usrp1_codec_ctrl_impl : public usrp1_codec_ctrl {
43
public:
44
    //structors
45
    usrp1_codec_ctrl_impl(spi_iface::sptr iface, int spi_slave);
46
    ~usrp1_codec_ctrl_impl(void);
47

    
48
    //aux adc and dac control
49
    double read_aux_adc(aux_adc_t which);
50
    void write_aux_dac(aux_dac_t which, double volts);
51

    
52
    //duc control
53
    void set_duc_freq(double freq, double);
54
    void enable_tx_digital(bool enb);
55

    
56
    //pga gain control
57
    void set_tx_pga_gain(double);
58
    double get_tx_pga_gain(void);
59
    void set_rx_pga_gain(double, char);
60
    double get_rx_pga_gain(char);
61
    
62
    //rx adc buffer control
63
    void bypass_adc_buffers(bool bypass);
64

    
65
private:
66
    spi_iface::sptr _iface;
67
    int _spi_slave;
68
    ad9862_regs_t _ad9862_regs;
69
    void send_reg(boost::uint8_t addr);
70
    void recv_reg(boost::uint8_t addr);
71

    
72
    double coarse_tune(double codec_rate, double freq);
73
    double fine_tune(double codec_rate, double freq);
74
};
75

    
76
/***********************************************************************
77
 * Codec Control Structors
78
 **********************************************************************/
79
usrp1_codec_ctrl_impl::usrp1_codec_ctrl_impl(spi_iface::sptr iface, int spi_slave){
80
    _iface = iface;
81
    _spi_slave = spi_slave;
82

    
83
    //soft reset
84
    _ad9862_regs.soft_reset = 1;
85
    this->send_reg(0);
86

    
87
    //initialize the codec register settings
88
    _ad9862_regs.sdio_bidir = ad9862_regs_t::SDIO_BIDIR_SDIO_SDO;
89
    _ad9862_regs.lsb_first = ad9862_regs_t::LSB_FIRST_MSB;
90
    _ad9862_regs.soft_reset = 0;
91

    
92
    //setup rx side of codec
93
    _ad9862_regs.byp_buffer_a = 1;
94
    _ad9862_regs.byp_buffer_b = 1;
95
    _ad9862_regs.buffer_a_pd = 1;
96
    _ad9862_regs.buffer_b_pd = 1;
97
    _ad9862_regs.rx_pga_a = 0;
98
    _ad9862_regs.rx_pga_b = 0;
99
    _ad9862_regs.rx_twos_comp = 1;
100
    _ad9862_regs.rx_hilbert = ad9862_regs_t::RX_HILBERT_DIS;
101

    
102
    //setup tx side of codec
103
    _ad9862_regs.two_data_paths = ad9862_regs_t::TWO_DATA_PATHS_BOTH;
104
    _ad9862_regs.interleaved = ad9862_regs_t::INTERLEAVED_INTERLEAVED;
105
    _ad9862_regs.tx_pga_gain = 199;
106
    _ad9862_regs.tx_hilbert = ad9862_regs_t::TX_HILBERT_DIS;
107
    _ad9862_regs.interp = ad9862_regs_t::INTERP_4;
108
    _ad9862_regs.tx_twos_comp = 1;
109
    _ad9862_regs.fine_mode = ad9862_regs_t::FINE_MODE_NCO;
110
    _ad9862_regs.coarse_mod = ad9862_regs_t::COARSE_MOD_BYPASS;
111
    _ad9862_regs.dac_a_coarse_gain = 0x3;
112
    _ad9862_regs.dac_b_coarse_gain = 0x3;
113

    
114
    //setup the dll
115
    _ad9862_regs.input_clk_ctrl = ad9862_regs_t::INPUT_CLK_CTRL_EXTERNAL;
116
    _ad9862_regs.dll_mult = ad9862_regs_t::DLL_MULT_2;
117
    _ad9862_regs.dll_mode = ad9862_regs_t::DLL_MODE_FAST;
118

    
119
    //setup clockout
120
    _ad9862_regs.clkout2_div_factor = ad9862_regs_t::CLKOUT2_DIV_FACTOR_2;
121

    
122
    //write the register settings to the codec
123
    for (boost::uint8_t addr = 0; addr <= 25; addr++) {
124
        this->send_reg(addr);
125
    }
126

    
127
    //always start conversions for aux ADC
128
    _ad9862_regs.start_a = 1;
129
    _ad9862_regs.start_b = 1;
130

    
131
    //aux adc clock
132
    _ad9862_regs.clk_4 = ad9862_regs_t::CLK_4_1_4;
133
    this->send_reg(34);
134
}
135

    
136
usrp1_codec_ctrl_impl::~usrp1_codec_ctrl_impl(void){UHD_SAFE_CALL(
137
    //set aux dacs to zero
138
    this->write_aux_dac(AUX_DAC_A, 0);
139
    this->write_aux_dac(AUX_DAC_B, 0);
140
    this->write_aux_dac(AUX_DAC_C, 0);
141
    this->write_aux_dac(AUX_DAC_D, 0);
142

    
143
    //power down
144
    _ad9862_regs.all_rx_pd = 1;
145
    this->send_reg(1);
146
    _ad9862_regs.tx_digital_pd = 1;
147
    _ad9862_regs.tx_analog_pd = ad9862_regs_t::TX_ANALOG_PD_BOTH;
148
    this->send_reg(8);
149
)}
150

    
151
/***********************************************************************
152
 * Codec Control Gain Control Methods
153
 **********************************************************************/
154
static const int mtpgw = 255; //maximum tx pga gain word
155

    
156
void usrp1_codec_ctrl_impl::set_tx_pga_gain(double gain){
157
    int gain_word = int(mtpgw*(gain - tx_pga_gain_range.start())/(tx_pga_gain_range.stop() - tx_pga_gain_range.start()));
158
    _ad9862_regs.tx_pga_gain = uhd::clip(gain_word, 0, mtpgw);
159
    this->send_reg(16);
160
}
161

    
162
double usrp1_codec_ctrl_impl::get_tx_pga_gain(void){
163
    return (_ad9862_regs.tx_pga_gain*(tx_pga_gain_range.stop() - tx_pga_gain_range.start())/mtpgw) + tx_pga_gain_range.start();
164
}
165

    
166
static const int mrpgw = 0x14; //maximum rx pga gain word
167

    
168
void usrp1_codec_ctrl_impl::set_rx_pga_gain(double gain, char which){
169
    int gain_word = int(mrpgw*(gain - rx_pga_gain_range.start())/(rx_pga_gain_range.stop() - rx_pga_gain_range.start()));
170
    gain_word = uhd::clip(gain_word, 0, mrpgw);
171
    switch(which){
172
    case 'A':
173
        _ad9862_regs.rx_pga_a = gain_word;
174
        this->send_reg(2);
175
        return;
176
    case 'B':
177
        _ad9862_regs.rx_pga_b = gain_word;
178
        this->send_reg(3);
179
        return;
180
    default: UHD_THROW_INVALID_CODE_PATH();
181
    }
182
}
183

    
184
double usrp1_codec_ctrl_impl::get_rx_pga_gain(char which){
185
    int gain_word;
186
    switch(which){
187
    case 'A': gain_word = _ad9862_regs.rx_pga_a; break;
188
    case 'B': gain_word = _ad9862_regs.rx_pga_b; break;
189
    default: UHD_THROW_INVALID_CODE_PATH();
190
    }
191
    return (gain_word*(rx_pga_gain_range.stop() - rx_pga_gain_range.start())/mrpgw) + rx_pga_gain_range.start();
192
}
193

    
194
/***********************************************************************
195
 * Codec Control AUX ADC Methods
196
 **********************************************************************/
197
static double aux_adc_to_volts(boost::uint8_t high, boost::uint8_t low)
198
{
199
    return double(((boost::uint16_t(high) << 2) | low)*3.3)/0x3ff;
200
}
201

    
202
double usrp1_codec_ctrl_impl::read_aux_adc(aux_adc_t which){
203
    switch(which){
204
    case AUX_ADC_A1:
205
        _ad9862_regs.select_a = ad9862_regs_t::SELECT_A_AUX_ADC1;
206
        this->send_reg(34); //start conversion and select mux
207
        this->recv_reg(28); //read the value (2 bytes, 2 reads)
208
        this->recv_reg(29);
209
        return aux_adc_to_volts(_ad9862_regs.aux_adc_a1_9_2, _ad9862_regs.aux_adc_a1_1_0);
210

    
211
    case AUX_ADC_A2:
212
        _ad9862_regs.select_a = ad9862_regs_t::SELECT_A_AUX_ADC2;
213
        this->send_reg(34); //start conversion and select mux
214
        this->recv_reg(26); //read the value (2 bytes, 2 reads)
215
        this->recv_reg(27);
216
        return aux_adc_to_volts(_ad9862_regs.aux_adc_a2_9_2, _ad9862_regs.aux_adc_a2_1_0);
217

    
218
    case AUX_ADC_B1:
219
        _ad9862_regs.select_b = ad9862_regs_t::SELECT_B_AUX_ADC1;
220
        this->send_reg(34); //start conversion and select mux
221
        this->recv_reg(32); //read the value (2 bytes, 2 reads)
222
        this->recv_reg(33);
223
        return aux_adc_to_volts(_ad9862_regs.aux_adc_b1_9_2, _ad9862_regs.aux_adc_b1_1_0);
224

    
225
    case AUX_ADC_B2:
226
        _ad9862_regs.select_b = ad9862_regs_t::SELECT_B_AUX_ADC2;
227
        this->send_reg(34); //start conversion and select mux
228
        this->recv_reg(30); //read the value (2 bytes, 2 reads)
229
        this->recv_reg(31);
230
        return aux_adc_to_volts(_ad9862_regs.aux_adc_b2_9_2, _ad9862_regs.aux_adc_b2_1_0);
231
    }
232
    UHD_THROW_INVALID_CODE_PATH();
233
}
234

    
235
/***********************************************************************
236
 * Codec Control AUX DAC Methods
237
 **********************************************************************/
238
void usrp1_codec_ctrl_impl::write_aux_dac(aux_dac_t which, double volts)
239
{
240
    //special case for aux dac d (aka sigma delta word)
241
    if (which == AUX_DAC_D) {
242
        boost::uint16_t dac_word = uhd::clip(boost::math::iround(volts*0xfff/3.3), 0, 0xfff);
243
        _ad9862_regs.sig_delt_11_4 = boost::uint8_t(dac_word >> 4);
244
        _ad9862_regs.sig_delt_3_0 = boost::uint8_t(dac_word & 0xf);
245
        this->send_reg(42);
246
        this->send_reg(43);
247
        return;
248
    }
249

    
250
    //calculate the dac word for aux dac a, b, c
251
    boost::uint8_t dac_word = uhd::clip(boost::math::iround(volts*0xff/3.3), 0, 0xff);
252

    
253
    //setup a lookup table for the aux dac params (reg ref, reg addr)
254
    typedef boost::tuple<boost::uint8_t*, boost::uint8_t> dac_params_t;
255
    uhd::dict<aux_dac_t, dac_params_t> aux_dac_to_params = boost::assign::map_list_of
256
        (AUX_DAC_A, dac_params_t(&_ad9862_regs.aux_dac_a, 36))
257
        (AUX_DAC_B, dac_params_t(&_ad9862_regs.aux_dac_b, 37))
258
        (AUX_DAC_C, dac_params_t(&_ad9862_regs.aux_dac_c, 38))
259
    ;
260

    
261
    //set the aux dac register
262
    UHD_ASSERT_THROW(aux_dac_to_params.has_key(which));
263
    boost::uint8_t *reg_ref, reg_addr;
264
    boost::tie(reg_ref, reg_addr) = aux_dac_to_params[which];
265
    *reg_ref = dac_word;
266
    this->send_reg(reg_addr);
267
}
268

    
269
/***********************************************************************
270
 * Codec Control SPI Methods
271
 **********************************************************************/
272
void usrp1_codec_ctrl_impl::send_reg(boost::uint8_t addr)
273
{
274
    boost::uint32_t reg = _ad9862_regs.get_write_reg(addr);
275

    
276
    UHD_LOGV(often)
277
        << "codec control write reg: 0x"
278
        << std::setw(8) << std::hex << reg << std::endl
279
    ;
280
    _iface->write_spi(_spi_slave,
281
                         spi_config_t::EDGE_RISE, reg, 16);
282
}
283

    
284
void usrp1_codec_ctrl_impl::recv_reg(boost::uint8_t addr)
285
{
286
    boost::uint32_t reg = _ad9862_regs.get_read_reg(addr);
287

    
288
    UHD_LOGV(often)
289
        << "codec control read reg: 0x"
290
        << std::setw(8) << std::hex << reg << std::endl
291
    ;
292

    
293
    boost::uint32_t ret = _iface->read_spi(_spi_slave,
294
                                        spi_config_t::EDGE_RISE, reg, 16);
295

    
296
    UHD_LOGV(often)
297
        << "codec control read ret: 0x"
298
        << std::setw(8) << std::hex << ret << std::endl
299
    ;
300

    
301
    _ad9862_regs.set_reg(addr, boost::uint16_t(ret));
302
}
303

    
304
/***********************************************************************
305
 * DUC tuning 
306
 **********************************************************************/
307
double usrp1_codec_ctrl_impl::coarse_tune(double codec_rate, double freq)
308
{
309
    double coarse_freq;
310

    
311
    double coarse_freq_1 = codec_rate / 8;
312
    double coarse_freq_2 = codec_rate / 4;
313
    double coarse_limit_1 = coarse_freq_1 / 2;
314
    double coarse_limit_2 = (coarse_freq_1 + coarse_freq_2) / 2;
315
    double max_freq = coarse_freq_2 + .09375 * codec_rate;
316
 
317
    if (freq < -max_freq) {
318
        return false;
319
    }
320
    else if (freq < -coarse_limit_2) {
321
        _ad9862_regs.neg_coarse_tune = ad9862_regs_t::NEG_COARSE_TUNE_NEG_SHIFT;
322
        _ad9862_regs.coarse_mod = ad9862_regs_t::COARSE_MOD_FDAC_4;
323
        coarse_freq = -coarse_freq_2;
324
    }
325
    else if (freq < -coarse_limit_1) {
326
        _ad9862_regs.neg_coarse_tune = ad9862_regs_t::NEG_COARSE_TUNE_NEG_SHIFT;
327
        _ad9862_regs.coarse_mod = ad9862_regs_t::COARSE_MOD_FDAC_8;
328
        coarse_freq = -coarse_freq_1;
329
    }
330
    else if (freq < coarse_limit_1) {
331
        _ad9862_regs.coarse_mod = ad9862_regs_t::COARSE_MOD_BYPASS;
332
        coarse_freq = 0; 
333
    }
334
    else if (freq < coarse_limit_2) {
335
        _ad9862_regs.neg_coarse_tune = ad9862_regs_t::NEG_COARSE_TUNE_POS_SHIFT;
336
        _ad9862_regs.coarse_mod = ad9862_regs_t::COARSE_MOD_FDAC_8;
337
        coarse_freq = coarse_freq_1;
338
    }
339
    else if (freq <= max_freq) {
340
        _ad9862_regs.neg_coarse_tune = ad9862_regs_t::NEG_COARSE_TUNE_POS_SHIFT;
341
        _ad9862_regs.coarse_mod = ad9862_regs_t::COARSE_MOD_FDAC_4;
342
        coarse_freq = coarse_freq_2;
343
    }
344
    else {
345
        return 0; 
346
    }
347

    
348
    return coarse_freq;
349
}
350

    
351
double usrp1_codec_ctrl_impl::fine_tune(double codec_rate, double target_freq)
352
{
353
    static const double scale_factor = std::pow(2.0, 24);
354

    
355
    boost::uint32_t freq_word = boost::uint32_t(
356
        boost::math::round(abs((target_freq / codec_rate) * scale_factor)));
357

    
358
    double actual_freq = freq_word * codec_rate / scale_factor;
359

    
360
    if (target_freq < 0) {
361
        _ad9862_regs.neg_fine_tune = ad9862_regs_t::NEG_FINE_TUNE_NEG_SHIFT;
362
        actual_freq = -actual_freq; 
363
    }
364
    else {
365
        _ad9862_regs.neg_fine_tune = ad9862_regs_t::NEG_FINE_TUNE_POS_SHIFT;
366
    } 
367

    
368
    _ad9862_regs.fine_mode = ad9862_regs_t::FINE_MODE_NCO;
369
    _ad9862_regs.ftw_23_16 = (freq_word >> 16) & 0xff;
370
    _ad9862_regs.ftw_15_8  = (freq_word >>  8) & 0xff;
371
    _ad9862_regs.ftw_7_0   = (freq_word >>  0) & 0xff;
372

    
373
    return actual_freq;
374
}
375

    
376
void usrp1_codec_ctrl_impl::set_duc_freq(double freq, double rate)
377
{
378
    double codec_rate = rate * 2;
379

    
380
    //correct for outside of rate (wrap around)
381
    freq = std::fmod(freq, rate);
382
    if (std::abs(freq) > rate/2.0)
383
        freq -= boost::math::sign(freq)*rate;
384

    
385
    double coarse_freq = coarse_tune(codec_rate, freq);
386
    double fine_freq = fine_tune(codec_rate / 4, freq - coarse_freq);
387

    
388
    UHD_LOG
389
        << "ad9862 tuning result:" << std::endl
390
        << "   requested:   " << freq << std::endl
391
        << "   actual:      " << coarse_freq + fine_freq << std::endl
392
        << "   coarse freq: " << coarse_freq << std::endl
393
        << "   fine freq:   " << fine_freq << std::endl
394
        << "   codec rate:  " << codec_rate << std::endl
395
    ;
396

    
397
    this->send_reg(20);
398
    this->send_reg(21);
399
    this->send_reg(22);
400
    this->send_reg(23);
401
}
402

    
403
void usrp1_codec_ctrl_impl::enable_tx_digital(bool enb){
404
    _ad9862_regs.tx_digital_pd = (enb)? 0 : 1;
405
    this->send_reg(8);
406
}
407

    
408
/***********************************************************************
409
 * Codec Control ADC buffer bypass
410
 * Disable this for AC-coupled daughterboards (TVRX)
411
 * By default it is initialized TRUE.
412
 **********************************************************************/
413
void usrp1_codec_ctrl_impl::bypass_adc_buffers(bool bypass) {
414
    _ad9862_regs.byp_buffer_a = bypass;
415
    _ad9862_regs.byp_buffer_b = bypass;
416
    this->send_reg(2);
417
}
418

    
419
/***********************************************************************
420
 * Codec Control Make
421
 **********************************************************************/
422
usrp1_codec_ctrl::sptr usrp1_codec_ctrl::make(spi_iface::sptr iface,
423
                                              int spi_slave)
424
{
425
    return sptr(new usrp1_codec_ctrl_impl(iface, spi_slave));
426
}