Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / codec_ctrl.cpp @ 1e1e7037

History | View | Annotate | Download (14.9 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 "usrp_commands.h"
20
#include "clock_ctrl.hpp"
21
#include "ad9862_regs.hpp"
22
#include <uhd/types/dict.hpp>
23
#include <uhd/utils/assert.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/assign/list_of.hpp>
31
#include <iostream>
32
#include <iomanip>
33

    
34
using namespace uhd;
35

    
36
static const bool codec_debug = false;
37

    
38
const gain_range_t usrp1_codec_ctrl::tx_pga_gain_range(-20, 0, float(0.1));
39
const gain_range_t usrp1_codec_ctrl::rx_pga_gain_range(0, 20, 1);
40

    
41
/***********************************************************************
42
 * Codec Control Implementation
43
 **********************************************************************/
44
class usrp1_codec_ctrl_impl : public usrp1_codec_ctrl {
45
public:
46
    //structors
47
    usrp1_codec_ctrl_impl(usrp1_iface::sptr iface,
48
                          usrp1_clock_ctrl::sptr clock,
49
                          int spi_slave);
50
    ~usrp1_codec_ctrl_impl(void);
51

    
52
    //aux adc and dac control
53
    float read_aux_adc(aux_adc_t which);
54
    void write_aux_dac(aux_dac_t which, float volts);
55

    
56
    //duc control
57
    void set_duc_freq(double freq);
58

    
59
    //pga gain control
60
    void set_tx_pga_gain(float);
61
    float get_tx_pga_gain(void);
62
    void set_rx_pga_gain(float, char);
63
    float get_rx_pga_gain(char);
64

    
65
private:
66
    usrp1_iface::sptr _iface;
67
    usrp1_clock_ctrl::sptr _clock_ctrl;
68
    int _spi_slave;
69
    ad9862_regs_t _ad9862_regs;
70
    aux_adc_t _last_aux_adc_a, _last_aux_adc_b;
71
    void send_reg(boost::uint8_t addr);
72
    void recv_reg(boost::uint8_t addr);
73

    
74
    double coarse_tune(double codec_rate, double freq);
75
    double fine_tune(double codec_rate, double freq);
76
};
77

    
78
/***********************************************************************
79
 * Codec Control Structors
80
 **********************************************************************/
81
usrp1_codec_ctrl_impl::usrp1_codec_ctrl_impl(usrp1_iface::sptr iface,
82
                                             usrp1_clock_ctrl::sptr clock,
83
                                             int spi_slave)
84
{
85
    _iface = iface;
86
    _clock_ctrl = clock;
87
    _spi_slave = spi_slave;
88

    
89
    //soft reset
90
    _ad9862_regs.soft_reset = 1;
91
    this->send_reg(0);
92

    
93
    //initialize the codec register settings
94
    _ad9862_regs.sdio_bidir = ad9862_regs_t::SDIO_BIDIR_SDIO_SDO;
95
    _ad9862_regs.lsb_first = ad9862_regs_t::LSB_FIRST_MSB;
96
    _ad9862_regs.soft_reset = 0;
97

    
98
    //setup rx side of codec
99
    _ad9862_regs.byp_buffer_a = 1;
100
    _ad9862_regs.byp_buffer_b = 1;
101
    _ad9862_regs.buffer_a_pd = 1;
102
    _ad9862_regs.buffer_b_pd = 1;
103
    _ad9862_regs.rx_pga_a = 0;
104
    _ad9862_regs.rx_pga_b = 0;
105
    _ad9862_regs.rx_twos_comp = 1;
106
    _ad9862_regs.rx_hilbert = ad9862_regs_t::RX_HILBERT_DIS;
107

    
108
    //setup tx side of codec
109
    _ad9862_regs.two_data_paths = ad9862_regs_t::TWO_DATA_PATHS_BOTH;
110
    _ad9862_regs.interleaved = ad9862_regs_t::INTERLEAVED_INTERLEAVED;
111
    _ad9862_regs.tx_pga_gain = 199;
112
    _ad9862_regs.tx_hilbert = ad9862_regs_t::TX_HILBERT_DIS;
113
    _ad9862_regs.interp = ad9862_regs_t::INTERP_4;
114
    _ad9862_regs.tx_twos_comp = 1;
115
    _ad9862_regs.fine_mode = ad9862_regs_t::FINE_MODE_NCO;
116
    _ad9862_regs.coarse_mod = ad9862_regs_t::COARSE_MOD_BYPASS;
117
    _ad9862_regs.dac_a_coarse_gain = 0x3;
118
    _ad9862_regs.dac_b_coarse_gain = 0x3;
119

    
120
    //setup the dll
121
    _ad9862_regs.input_clk_ctrl = ad9862_regs_t::INPUT_CLK_CTRL_EXTERNAL;
122
    _ad9862_regs.dll_mult = ad9862_regs_t::DLL_MULT_2;
123
    _ad9862_regs.dll_mode = ad9862_regs_t::DLL_MODE_FAST;
124

    
125
    //setup clockout
126
    _ad9862_regs.clkout2_div_factor = ad9862_regs_t::CLKOUT2_DIV_FACTOR_2;
127

    
128
    //write the register settings to the codec
129
    for (uint8_t addr = 0; addr <= 25; addr++) {
130
        this->send_reg(addr);
131
    }
132

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

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

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

    
154
/***********************************************************************
155
 * Codec Control Gain Control Methods
156
 **********************************************************************/
157
static const int mtpgw = 255; //maximum tx pga gain word
158

    
159
void usrp1_codec_ctrl_impl::set_tx_pga_gain(float gain){
160
    int gain_word = int(mtpgw*(gain - tx_pga_gain_range.min)/(tx_pga_gain_range.max - tx_pga_gain_range.min));
161
    _ad9862_regs.tx_pga_gain = std::clip(gain_word, 0, mtpgw);
162
    this->send_reg(16);
163
}
164

    
165
float usrp1_codec_ctrl_impl::get_tx_pga_gain(void){
166
    return (_ad9862_regs.tx_pga_gain*(tx_pga_gain_range.max - tx_pga_gain_range.min)/mtpgw) + tx_pga_gain_range.min;
167
}
168

    
169
static const int mrpgw = 0x14; //maximum rx pga gain word
170

    
171
void usrp1_codec_ctrl_impl::set_rx_pga_gain(float gain, char which){
172
    int gain_word = int(mrpgw*(gain - rx_pga_gain_range.min)/(rx_pga_gain_range.max - rx_pga_gain_range.min));
173
    gain_word = std::clip(gain_word, 0, mrpgw);
174
    switch(which){
175
    case 'A':
176
        _ad9862_regs.rx_pga_a = gain_word;
177
        this->send_reg(2);
178
        return;
179
    case 'B':
180
        _ad9862_regs.rx_pga_b = gain_word;
181
        this->send_reg(3);
182
        return;
183
    default: UHD_THROW_INVALID_CODE_PATH();
184
    }
185
}
186

    
187
float usrp1_codec_ctrl_impl::get_rx_pga_gain(char which){
188
    int gain_word;
189
    switch(which){
190
    case 'A': gain_word = _ad9862_regs.rx_pga_a; break;
191
    case 'B': gain_word = _ad9862_regs.rx_pga_b; break;
192
    default: UHD_THROW_INVALID_CODE_PATH();
193
    }
194
    return (gain_word*(rx_pga_gain_range.max - rx_pga_gain_range.min)/mrpgw) + rx_pga_gain_range.min;
195
}
196

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

    
205
float usrp1_codec_ctrl_impl::read_aux_adc(aux_adc_t which)
206
{
207
    //check to see if the switch needs to be set
208
    bool write_switch = false;
209
    switch(which) {
210

    
211
    case AUX_ADC_A1:
212
    case AUX_ADC_A2:
213
        if (which != _last_aux_adc_a) {
214
            _ad9862_regs.select_a = (which == AUX_ADC_A1)?
215
                ad9862_regs_t::SELECT_A_AUX_ADC1: ad9862_regs_t::SELECT_A_AUX_ADC2;
216
            _last_aux_adc_a = which;
217
            write_switch = true;
218
        }
219
        break;
220

    
221
    case AUX_ADC_B1:
222
    case AUX_ADC_B2:
223
        if (which != _last_aux_adc_b) {
224
            _ad9862_regs.select_b = (which == AUX_ADC_B1)?
225
                ad9862_regs_t::SELECT_B_AUX_ADC1: ad9862_regs_t::SELECT_B_AUX_ADC2;
226
            _last_aux_adc_b = which;
227
            write_switch = true;
228
        }
229
        break;
230

    
231
    }
232

    
233
    //write the switch if it changed
234
    if(write_switch) this->send_reg(34);
235

    
236
    //map aux adcs to register values to read
237
    static const uhd::dict<aux_adc_t, boost::uint8_t> aux_dac_to_addr = boost::assign::map_list_of
238
        (AUX_ADC_A2, 26) (AUX_ADC_A1, 28)
239
        (AUX_ADC_B2, 30) (AUX_ADC_B1, 32)
240
    ;
241

    
242
    //read the value
243
    this->recv_reg(aux_dac_to_addr[which]+0);
244
    this->recv_reg(aux_dac_to_addr[which]+1);
245

    
246
    //return the value scaled to volts
247
    switch(which) {
248
    case AUX_ADC_A1: return aux_adc_to_volts(_ad9862_regs.aux_adc_a1_9_2, _ad9862_regs.aux_adc_a1_1_0);
249
    case AUX_ADC_A2: return aux_adc_to_volts(_ad9862_regs.aux_adc_a2_9_2, _ad9862_regs.aux_adc_a2_1_0);
250
    case AUX_ADC_B1: return aux_adc_to_volts(_ad9862_regs.aux_adc_b1_9_2, _ad9862_regs.aux_adc_b1_1_0);
251
    case AUX_ADC_B2: return aux_adc_to_volts(_ad9862_regs.aux_adc_b2_9_2, _ad9862_regs.aux_adc_b2_1_0);
252
    }
253
    UHD_ASSERT_THROW(false);
254
}
255

    
256
/***********************************************************************
257
 * Codec Control AUX DAC Methods
258
 **********************************************************************/
259
void usrp1_codec_ctrl_impl::write_aux_dac(aux_dac_t which, float volts)
260
{
261
    //special case for aux dac d (aka sigma delta word)
262
    if (which == AUX_DAC_D) {
263
        boost::uint16_t dac_word = std::clip(boost::math::iround(volts*0xfff/3.3), 0, 0xfff);
264
        _ad9862_regs.sig_delt_11_4 = boost::uint8_t(dac_word >> 4);
265
        _ad9862_regs.sig_delt_3_0 = boost::uint8_t(dac_word & 0xf);
266
        this->send_reg(42);
267
        this->send_reg(43);
268
        return;
269
    }
270

    
271
    //calculate the dac word for aux dac a, b, c
272
    boost::uint8_t dac_word = std::clip(boost::math::iround(volts*0xff/3.3), 0, 0xff);
273

    
274
    //setup a lookup table for the aux dac params (reg ref, reg addr)
275
    typedef boost::tuple<boost::uint8_t*, boost::uint8_t> dac_params_t;
276
    uhd::dict<aux_dac_t, dac_params_t> aux_dac_to_params = boost::assign::map_list_of
277
        (AUX_DAC_A, dac_params_t(&_ad9862_regs.aux_dac_a, 36))
278
        (AUX_DAC_B, dac_params_t(&_ad9862_regs.aux_dac_b, 37))
279
        (AUX_DAC_C, dac_params_t(&_ad9862_regs.aux_dac_c, 38))
280
    ;
281

    
282
    //set the aux dac register
283
    UHD_ASSERT_THROW(aux_dac_to_params.has_key(which));
284
    boost::uint8_t *reg_ref, reg_addr;
285
    boost::tie(reg_ref, reg_addr) = aux_dac_to_params[which];
286
    *reg_ref = dac_word;
287
    this->send_reg(reg_addr);
288
}
289

    
290
/***********************************************************************
291
 * Codec Control SPI Methods
292
 **********************************************************************/
293
void usrp1_codec_ctrl_impl::send_reg(boost::uint8_t addr)
294
{
295
    boost::uint32_t reg = _ad9862_regs.get_write_reg(addr);
296

    
297
    if (codec_debug) {
298
        std::cout.fill('0');
299
        std::cout << "codec control write reg: 0x";
300
        std::cout << std::setw(8) << std::hex << reg << std::endl;
301
    }
302
    _iface->transact_spi(_spi_slave,
303
                         spi_config_t::EDGE_RISE, reg, 16, false);
304
}
305

    
306
void usrp1_codec_ctrl_impl::recv_reg(boost::uint8_t addr)
307
{
308
    boost::uint32_t reg = _ad9862_regs.get_read_reg(addr);
309

    
310
    if (codec_debug) {
311
        std::cout.fill('0');
312
        std::cout << "codec control read reg: 0x";
313
        std::cout << std::setw(8) << std::hex << reg << std::endl;
314
    }
315

    
316
    boost::uint32_t ret = _iface->transact_spi(_spi_slave,
317
                                        spi_config_t::EDGE_RISE, reg, 16, true);
318

    
319
    if (codec_debug) {
320
        std::cout.fill('0');
321
        std::cout << "codec control read ret: 0x";
322
        std::cout << std::setw(8) << std::hex << ret << std::endl;
323
    }
324

    
325
    _ad9862_regs.set_reg(addr, boost::uint16_t(ret));
326
}
327

    
328
/***********************************************************************
329
 * DUC tuning 
330
 **********************************************************************/
331
double usrp1_codec_ctrl_impl::coarse_tune(double codec_rate, double freq)
332
{
333
    double coarse_freq;
334

    
335
    double coarse_freq_1 = codec_rate / 8;
336
    double coarse_freq_2 = codec_rate / 4;
337
    double coarse_limit_1 = coarse_freq_1 / 2;
338
    double coarse_limit_2 = (coarse_freq_1 + coarse_freq_2) / 2;
339
    double max_freq = coarse_freq_2 + .09375 * codec_rate;
340
 
341
    if (freq < -max_freq) {
342
        return false;
343
    }
344
    else if (freq < -coarse_limit_2) {
345
        _ad9862_regs.neg_coarse_tune = ad9862_regs_t::NEG_COARSE_TUNE_NEG_SHIFT;
346
        _ad9862_regs.coarse_mod = ad9862_regs_t::COARSE_MOD_FDAC_4;
347
        coarse_freq = -coarse_freq_2;
348
    }
349
    else if (freq < -coarse_limit_1) {
350
        _ad9862_regs.neg_coarse_tune = ad9862_regs_t::NEG_COARSE_TUNE_NEG_SHIFT;
351
        _ad9862_regs.coarse_mod = ad9862_regs_t::COARSE_MOD_FDAC_8;
352
        coarse_freq = -coarse_freq_1;
353
    }
354
    else if (freq < coarse_limit_1) {
355
        _ad9862_regs.coarse_mod = ad9862_regs_t::COARSE_MOD_BYPASS;
356
        coarse_freq = 0; 
357
    }
358
    else if (freq < coarse_limit_2) {
359
        _ad9862_regs.neg_coarse_tune = ad9862_regs_t::NEG_COARSE_TUNE_POS_SHIFT;
360
        _ad9862_regs.coarse_mod = ad9862_regs_t::COARSE_MOD_FDAC_8;
361
        coarse_freq = coarse_freq_1;
362
    }
363
    else if (freq <= max_freq) {
364
        _ad9862_regs.neg_coarse_tune = ad9862_regs_t::NEG_COARSE_TUNE_POS_SHIFT;
365
        _ad9862_regs.coarse_mod = ad9862_regs_t::COARSE_MOD_FDAC_4;
366
        coarse_freq = coarse_freq_2;
367
    }
368
    else {
369
        return 0; 
370
    }
371

    
372
    return coarse_freq;
373
}
374

    
375
double usrp1_codec_ctrl_impl::fine_tune(double codec_rate, double target_freq)
376
{
377
    static const double scale_factor = std::pow(2.0, 24);
378

    
379
    boost::uint32_t freq_word = boost::uint32_t(
380
        boost::math::round(abs((target_freq / codec_rate) * scale_factor)));
381

    
382
    double actual_freq = freq_word * codec_rate / scale_factor;
383

    
384
    if (target_freq < 0) {
385
        _ad9862_regs.neg_fine_tune = ad9862_regs_t::NEG_FINE_TUNE_NEG_SHIFT;
386
        actual_freq = -actual_freq; 
387
    }
388
    else {
389
        _ad9862_regs.neg_fine_tune = ad9862_regs_t::NEG_FINE_TUNE_POS_SHIFT;
390
    } 
391

    
392
    _ad9862_regs.fine_mode = ad9862_regs_t::FINE_MODE_NCO;
393
    _ad9862_regs.ftw_23_16 = (freq_word >> 16) & 0xff;
394
    _ad9862_regs.ftw_15_8  = (freq_word >>  8) & 0xff;
395
    _ad9862_regs.ftw_7_0   = (freq_word >>  0) & 0xff;
396

    
397
    return actual_freq;
398
}
399

    
400
void usrp1_codec_ctrl_impl::set_duc_freq(double freq)
401
{
402
    double codec_rate = _clock_ctrl->get_master_clock_freq() * 2;
403
    double coarse_freq = coarse_tune(codec_rate, freq);
404
    double fine_freq = fine_tune(codec_rate / 4, freq - coarse_freq);
405

    
406
    if (codec_debug) {
407
        std::cout << "ad9862 tuning result:" << std::endl;
408
        std::cout << "   requested:   " << freq << std::endl;
409
        std::cout << "   actual:      " << coarse_freq + fine_freq << std::endl;
410
        std::cout << "   coarse freq: " << coarse_freq << std::endl;
411
        std::cout << "   fine freq:   " << fine_freq << std::endl;
412
        std::cout << "   codec rate:  " << codec_rate << std::endl;
413
    }    
414

    
415
    this->send_reg(20);
416
    this->send_reg(21);
417
    this->send_reg(22);
418
    this->send_reg(23);
419
}
420

    
421
/***********************************************************************
422
 * Codec Control Make
423
 **********************************************************************/
424
usrp1_codec_ctrl::sptr usrp1_codec_ctrl::make(usrp1_iface::sptr iface,
425
                                              usrp1_clock_ctrl::sptr clock,
426
                                              int spi_slave)
427
{
428
    return sptr(new usrp1_codec_ctrl_impl(iface, clock, spi_slave));
429
}