Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / dsp_utils.cpp @ 2c8a7c7d

History | View | Annotate | Download (5.53 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 <uhd/usrp/dsp_utils.hpp>
19
#include <uhd/types/dict.hpp>
20
#include <uhd/utils/assert.hpp>
21
#include <boost/assign/list_of.hpp>
22
#include <boost/tuple/tuple.hpp>
23
#include <boost/math/special_functions/round.hpp>
24
#include <cmath>
25

    
26
using namespace uhd;
27
using namespace uhd::usrp;
28

    
29
template <class T> T ceil_log2(T num){
30
    return std::ceil(std::log(num)/std::log(T(2)));
31
}
32

    
33
/*!
34
 *     3                   2                   1                   0
35
 *   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
36
 *  +-------------------------------+-------+-------+-------+-------+
37
 *  |                                               | DDC0Q | DDC0I |
38
 *  +-------------------------------+-------+-------+-------+-------+
39
 */
40
boost::uint32_t dsp_type1::calc_rx_mux_word(subdev_conn_t subdev_conn){
41
    switch(subdev_conn){
42
    case SUBDEV_CONN_COMPLEX_IQ: return (0x1 << 4) | (0x0 << 0); //DDC0Q=ADC0Q, DDC0I=ADC0I
43
    case SUBDEV_CONN_COMPLEX_QI: return (0x0 << 4) | (0x1 << 0); //DDC0Q=ADC0I, DDC0I=ADC0Q
44
    case SUBDEV_CONN_REAL_I:     return (0xf << 4) | (0x0 << 0); //DDC0Q=ZERO,  DDC0I=ADC0I
45
    case SUBDEV_CONN_REAL_Q:     return (0x1 << 4) | (0xf << 0); //DDC0Q=ADC0Q, DDC0I=ZERO
46
    default:                     UHD_THROW_INVALID_CODE_PATH();
47
    }
48
}
49

    
50
/*!
51
 *     3                   2                   1                   0
52
 *   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
53
 *  +-------------------------------+-------+-------+-------+-------+
54
 *  |                                               | DAC0Q | DAC0I |
55
 *  +-------------------------------+-------+-------+-------+-------+
56
 */
57
boost::uint32_t dsp_type1::calc_tx_mux_word(subdev_conn_t subdev_conn){
58
    switch(subdev_conn){
59
    case SUBDEV_CONN_COMPLEX_IQ: return (0x1 << 4) | (0x0 << 0); //DAC0Q=DUC0Q, DAC0I=DUC0I
60
    case SUBDEV_CONN_COMPLEX_QI: return (0x0 << 4) | (0x1 << 0); //DAC0Q=DUC0I, DAC0I=DUC0Q
61
    case SUBDEV_CONN_REAL_I:     return (0xf << 4) | (0x0 << 0); //DAC0Q=ZERO,  DAC0I=DUC0I
62
    case SUBDEV_CONN_REAL_Q:     return (0x0 << 4) | (0xf << 0); //DAC0Q=DUC0I, DAC0I=ZERO
63
    default:                     UHD_THROW_INVALID_CODE_PATH();
64
    }
65
}
66

    
67
boost::uint32_t dsp_type1::calc_cordic_word_and_update(
68
    double &freq,
69
    double codec_rate
70
){
71
    UHD_ASSERT_THROW(std::abs(freq) <= codec_rate/2.0);
72
    static const double scale_factor = std::pow(2.0, 32);
73

    
74
    //calculate the freq register word (signed)
75
    boost::int32_t freq_word = boost::int32_t(boost::math::round((freq / codec_rate) * scale_factor));
76

    
77
    //update the actual frequency
78
    freq = (double(freq_word) / scale_factor) * codec_rate;
79

    
80
    return boost::uint32_t(freq_word);
81
}
82

    
83
boost::uint32_t dsp_type1::calc_cic_filter_word(unsigned rate){
84
    int hb0 = 0, hb1 = 0;
85
    if (not (rate & 0x1)){
86
        hb0 = 1;
87
        rate /= 2;
88
    }
89
    if (not (rate & 0x1)){
90
        hb1 = 1;
91
        rate /= 2;
92
    }
93
    return (hb1 << 9) | (hb0 << 8) | (rate & 0xff);
94
}
95

    
96
boost::uint32_t dsp_type1::calc_iq_scale_word(
97
    boost::int16_t i, boost::int16_t q
98
){
99
    return (boost::uint32_t(i) << 16) | (boost::uint32_t(q) << 0);
100
}
101

    
102
boost::uint32_t dsp_type1::calc_iq_scale_word(unsigned rate){
103
    // Calculate CIC interpolation (i.e., without halfband interpolators)
104
    unsigned tmp_rate = calc_cic_filter_word(rate) & 0xff;
105

    
106
    // Calculate closest multiplier constant to reverse gain absent scale multipliers
107
    double rate_cubed = std::pow(double(tmp_rate), 3);
108
    boost::int16_t scale = boost::math::iround((4096*std::pow(2, ceil_log2(rate_cubed)))/(1.65*rate_cubed));
109
    return calc_iq_scale_word(scale, scale);
110
}
111

    
112
boost::uint32_t dsp_type1::calc_stream_cmd_word(
113
    const stream_cmd_t &stream_cmd, size_t num_samps_continuous
114
){
115
    UHD_ASSERT_THROW(stream_cmd.num_samps <= 0x3fffffff);
116

    
117
    //setup the mode to instruction flags
118
    typedef boost::tuple<bool, bool, bool> inst_t;
119
    static const uhd::dict<stream_cmd_t::stream_mode_t, inst_t> mode_to_inst = boost::assign::map_list_of
120
                                                            //reload, chain, samps
121
        (stream_cmd_t::STREAM_MODE_START_CONTINUOUS,   inst_t(true,  true,  false))
122
        (stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS,    inst_t(false, false, false))
123
        (stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE, inst_t(false, false, true))
124
        (stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_MORE, inst_t(false, true,  true))
125
    ;
126

    
127
    //setup the instruction flag values
128
    bool inst_reload, inst_chain, inst_samps;
129
    boost::tie(inst_reload, inst_chain, inst_samps) = mode_to_inst[stream_cmd.stream_mode];
130

    
131
    //calculate the word from flags and length
132
    boost::uint32_t word = 0;
133
    word |= boost::uint32_t((stream_cmd.stream_now)? 1 : 0) << 31;
134
    word |= boost::uint32_t((inst_chain)?            1 : 0) << 30;
135
    word |= boost::uint32_t((inst_reload)?           1 : 0) << 29;
136
    word |= (inst_samps)? stream_cmd.num_samps : ((inst_chain)? num_samps_continuous : 1);
137
    return word;
138
}