Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / usrp1_calc_mux.hpp @ 5a678852

History | View | Annotate | Download (6.64 KB)

1
//
2
// Copyright 2010-2011 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/config.hpp>
19
#include <uhd/exception.hpp>
20
#include <uhd/types/dict.hpp>
21
#include <uhd/utils/algorithm.hpp>
22
#include <boost/assign/list_of.hpp>
23
#include <boost/format.hpp>
24
#include <utility>
25
#include <vector>
26
#include <string>
27

    
28
#ifndef INCLUDED_USRP1_CALC_MUX_HPP
29
#define INCLUDED_USRP1_CALC_MUX_HPP
30

    
31
//db_name, conn_type for the mux calculations below...
32
typedef std::pair<std::string, std::string> mapping_pair_t;
33

    
34
/***********************************************************************
35
 * Calculate the RX mux value:
36
 *    The I and Q mux values are intentionally reversed to flip I and Q
37
 *    to account for the reversal in the type conversion routines.
38
 **********************************************************************/
39
static int calc_rx_mux_pair(int adc_for_i, int adc_for_q){
40
    return (adc_for_i << 0) | (adc_for_q << 2);
41
}
42

    
43
/*!
44
 *    3                   2                   1                   0
45
 *  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
46
 * +-----------------------+-------+-------+-------+-------+-+-----+
47
 * |      must be zero     | Q3| I3| Q2| I2| Q1| I1| Q0| I0|Z| NCH |
48
 * +-----------------------+-------+-------+-------+-------+-+-----+
49
 */
50
static boost::uint32_t calc_rx_mux(const std::vector<mapping_pair_t> &mapping){
51
    //create look-up-table for mapping dboard name and connection type to ADC flags
52
    static const int ADC0 = 0, ADC1 = 1, ADC2 = 2, ADC3 = 3;
53
    static const uhd::dict<std::string, uhd::dict<std::string, int> > name_to_conn_to_flag = boost::assign::map_list_of
54
        ("A", boost::assign::map_list_of
55
            ("IQ", calc_rx_mux_pair(ADC0, ADC1)) //I and Q
56
            ("QI", calc_rx_mux_pair(ADC1, ADC0)) //I and Q
57
            ("I",  calc_rx_mux_pair(ADC0, ADC0)) //I and Q (Q identical but ignored Z=1)
58
            ("Q",  calc_rx_mux_pair(ADC1, ADC1)) //I and Q (Q identical but ignored Z=1)
59
        )
60
        ("B", boost::assign::map_list_of
61
            ("IQ", calc_rx_mux_pair(ADC2, ADC3)) //I and Q
62
            ("QI", calc_rx_mux_pair(ADC3, ADC2)) //I and Q
63
            ("I",  calc_rx_mux_pair(ADC2, ADC2)) //I and Q (Q identical but ignored Z=1)
64
            ("Q",  calc_rx_mux_pair(ADC3, ADC3)) //I and Q (Q identical but ignored Z=1)
65
        )
66
    ;
67

    
68
    //extract the number of channels
69
    const size_t nchan = mapping.size();
70

    
71
    //calculate the channel flags
72
    int channel_flags = 0;
73
    size_t num_reals = 0, num_quads = 0;
74
    BOOST_FOREACH(const mapping_pair_t &pair, uhd::reversed(mapping)){
75
        const std::string name = pair.first, conn = pair.second;
76
        if (conn == "IQ" or conn == "QI") num_quads++;
77
        if (conn == "I" or conn == "Q") num_reals++;
78
        channel_flags = (channel_flags << 4) | name_to_conn_to_flag[name][conn];
79
    }
80

    
81
    //calculate Z:
82
    //    for all real sources: Z = 1
83
    //    for all quadrature sources: Z = 0
84
    //    for mixed sources: warning + Z = 0
85
    int Z = (num_quads > 0)? 0 : 1;
86
    if (num_quads != 0 and num_reals != 0) UHD_MSG(warning) << boost::format(
87
        "Mixing real and quadrature rx subdevices is not supported.\n"
88
        "The Q input to the real source(s) will be non-zero.\n"
89
    );
90

    
91
    //calculate the rx mux value
92
    return ((channel_flags & 0xffff) << 4) | ((Z & 0x1) << 3) | ((nchan & 0x7) << 0);
93
}
94

    
95
/***********************************************************************
96
 * Calculate the TX mux value:
97
 *    The I and Q mux values are intentionally reversed to flip I and Q
98
 *    to account for the reversal in the type conversion routines.
99
 **********************************************************************/
100
static int calc_tx_mux_pair(int chn_for_i, int chn_for_q){
101
    return (chn_for_i << 0) | (chn_for_q << 4);
102
}
103

    
104
/*!
105
 *    3                   2                   1                   0
106
 *  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
107
 * +-----------------------+-------+-------+-------+-------+-+-----+
108
 * |                       | DAC1Q | DAC1I | DAC0Q | DAC0I |0| NCH |
109
 * +-----------------------------------------------+-------+-+-----+
110
 */
111
static boost::uint32_t calc_tx_mux(const std::vector<mapping_pair_t> &mapping){
112
    //create look-up-table for mapping channel number and connection type to flags
113
    static const int ENB = 1 << 3, CHAN_I0 = 0, CHAN_Q0 = 1, CHAN_I1 = 2, CHAN_Q1 = 3;
114
    static const uhd::dict<size_t, uhd::dict<std::string, int> > chan_to_conn_to_flag = boost::assign::map_list_of
115
        (0, boost::assign::map_list_of
116
            ("IQ", calc_tx_mux_pair(CHAN_I0 | ENB, CHAN_Q0 | ENB))
117
            ("QI", calc_tx_mux_pair(CHAN_Q0 | ENB, CHAN_I0 | ENB))
118
            ("I",  calc_tx_mux_pair(CHAN_I0 | ENB, 0            ))
119
            ("Q",  calc_tx_mux_pair(0,             CHAN_I0 | ENB))
120
        )
121
        (1, boost::assign::map_list_of
122
            ("IQ", calc_tx_mux_pair(CHAN_I1 | ENB, CHAN_Q1 | ENB))
123
            ("QI", calc_tx_mux_pair(CHAN_Q1 | ENB, CHAN_I1 | ENB))
124
            ("I",  calc_tx_mux_pair(CHAN_I1 | ENB, 0            ))
125
            ("Q",  calc_tx_mux_pair(0,             CHAN_I1 | ENB))
126
        )
127
    ;
128

    
129
    //extract the number of channels
130
    size_t nchan = mapping.size();
131

    
132
    //calculate the channel flags
133
    int channel_flags = 0, chan = 0;
134
    uhd::dict<std::string, int> slot_to_chan_count = boost::assign::map_list_of("A", 0)("B", 0);
135
    BOOST_FOREACH(const mapping_pair_t &pair, mapping){
136
        const std::string name = pair.first, conn = pair.second;
137

    
138
        //combine the channel flags: shift for slot A vs B
139
        if (name == "A") channel_flags |= chan_to_conn_to_flag[chan][conn] << 0;
140
        if (name == "B") channel_flags |= chan_to_conn_to_flag[chan][conn] << 8;
141

    
142
        //sanity check, only 1 channel per slot
143
        slot_to_chan_count[name]++;
144
        if (slot_to_chan_count[name] > 1) throw uhd::value_error(
145
            "cannot assign dboard slot to multiple channels: " + name
146
        );
147

    
148
        //increment for the next channel
149
        chan++;
150
    }
151

    
152
    //calculate the tx mux value
153
    return ((channel_flags & 0xffff) << 4) | ((nchan & 0x7) << 0);
154
}
155

    
156
#endif /* INCLUDED_USRP1_CALC_MUX_HPP */