Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / dsp_impl.cpp @ 43e5480d

History | View | Annotate | Download (6.1 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 "usrp1_impl.hpp"
19
#include "fpga_regs_standard.h"
20
#include <uhd/usrp/dsp_utils.hpp>
21
#include <uhd/usrp/dsp_props.hpp>
22
#include <boost/bind.hpp>
23
#include <boost/format.hpp>
24
#include <iostream>
25
#include <cmath>
26

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

    
30
/***********************************************************************
31
 * RX DDC Initialization
32
 **********************************************************************/
33
void usrp1_impl::rx_dsp_init(void)
34
{
35
    _rx_dsp_proxy = wax_obj_proxy::make(
36
        boost::bind(&usrp1_impl::rx_dsp_get, this, _1, _2),
37
        boost::bind(&usrp1_impl::rx_dsp_set, this, _1, _2));
38

    
39
    rx_dsp_set(DSP_PROP_HOST_RATE, _clock_ctrl->get_master_clock_freq() / 16);
40
}
41

    
42
/***********************************************************************
43
 * RX DDC Get
44
 **********************************************************************/
45
void usrp1_impl::rx_dsp_get(const wax::obj &key, wax::obj &val)
46
{
47
    switch(key.as<dsp_prop_t>()){
48
    case DSP_PROP_NAME:
49
        val = std::string("usrp1 ddc0");
50
        return;
51

    
52
    case DSP_PROP_OTHERS:
53
        val = prop_names_t();
54
        return;
55

    
56
    case DSP_PROP_FREQ_SHIFT:
57
        val = _rx_dsp_freq;
58
        return;
59

    
60
    case DSP_PROP_CODEC_RATE:
61
        val = _clock_ctrl->get_master_clock_freq();
62
        return;
63

    
64
    case DSP_PROP_HOST_RATE:
65
        val = _clock_ctrl->get_master_clock_freq()/_rx_dsp_decim;
66
        return;
67

    
68
    default: UHD_THROW_PROP_GET_ERROR();
69
    }
70

    
71
}
72

    
73
/***********************************************************************
74
 * RX DDC Set
75
 **********************************************************************/
76
unsigned int compute_freq_word(double master, double target)
77
{
78
    static const int NBITS = 14;
79
    int   v = (int) rint (target / master * pow(2.0, 32.0));
80
 
81
    if (0)
82
      v = (v >> (32 - NBITS)) << (32 - NBITS);    // keep only top NBITS
83
 
84
    double actual_freq = v * master / pow(2.0, 32.0);
85
 
86
    if (0) std::cerr << boost::format(
87
        "compute_freq_control_word_fpga: target = %g  actual = %g  delta = %g\n"
88
    ) % target % actual_freq % (actual_freq - target);
89
 
90
    return (unsigned int) v;
91
}
92

    
93
void usrp1_impl::rx_dsp_set(const wax::obj &key, const wax::obj &val)
94
{
95
    switch(key.as<dsp_prop_t>()) {
96
    case DSP_PROP_FREQ_SHIFT: {
97
            double new_freq = val.as<double>();
98
            boost::uint32_t hw_freq_word = compute_freq_word(
99
                              _clock_ctrl->get_master_clock_freq(), new_freq);
100
            _iface->poke32(FR_RX_FREQ_0, hw_freq_word);
101
            _tx_dsp_freq = new_freq;
102
            return;
103
        }
104
    case DSP_PROP_HOST_RATE: {
105
            //FIXME: Stop and resume streaming during set?
106
            unsigned int rate =
107
                    _clock_ctrl->get_master_clock_freq() / val.as<double>();
108

    
109
            if ((rate & 0x01) || (rate < 4) || (rate > 256)) {
110
                std::cerr << "Decimation must be even and between 4 and 256"
111
                          << std::endl;
112
                return;
113
            }
114

    
115
            _rx_dsp_decim = rate;
116
            _iface->poke32(FR_DECIM_RATE, _rx_dsp_decim/2 - 1);
117
        }
118
        return;
119

    
120
    default: UHD_THROW_PROP_SET_ERROR();
121
    }
122

    
123
}
124

    
125
/***********************************************************************
126
 * TX DUC Initialization
127
 **********************************************************************/
128
void usrp1_impl::tx_dsp_init(void)
129
{
130
    _tx_dsp_proxy = wax_obj_proxy::make(
131
                          boost::bind(&usrp1_impl::tx_dsp_get, this, _1, _2),
132
                          boost::bind(&usrp1_impl::tx_dsp_set, this, _1, _2));
133

    
134
    //initial config and update
135
    tx_dsp_set(DSP_PROP_HOST_RATE, _clock_ctrl->get_master_clock_freq() * 2 / 16);
136
}
137

    
138
/***********************************************************************
139
 * TX DUC Get
140
 **********************************************************************/
141
void usrp1_impl::tx_dsp_get(const wax::obj &key, wax::obj &val)
142
{
143
    switch(key.as<dsp_prop_t>()) {
144
    case DSP_PROP_NAME:
145
        val = std::string("usrp1 duc0");
146
        return;
147

    
148
    case DSP_PROP_OTHERS:
149
        val = prop_names_t(); //empty
150
        return;
151

    
152
    case DSP_PROP_FREQ_SHIFT:
153
        val = _tx_dsp_freq;
154
        return;
155

    
156
    case DSP_PROP_CODEC_RATE:
157
        val = _clock_ctrl->get_master_clock_freq() * 2;
158
        return;
159

    
160
    case DSP_PROP_HOST_RATE:
161
        val = _clock_ctrl->get_master_clock_freq() * 2 / _tx_dsp_interp;
162
        return;
163

    
164
    default: UHD_THROW_PROP_GET_ERROR();
165
    }
166

    
167
}
168

    
169
/***********************************************************************
170
 * TX DUC Set
171
 **********************************************************************/
172
void usrp1_impl::tx_dsp_set(const wax::obj &key, const wax::obj &val)
173
{
174
    switch(key.as<dsp_prop_t>()) {
175

    
176
    case DSP_PROP_FREQ_SHIFT: {
177
            double new_freq = val.as<double>();
178
            _codec_ctrls[DBOARD_SLOT_A]->set_duc_freq(new_freq);
179
            _tx_dsp_freq = new_freq;
180
            return;
181
        }
182

    
183
    //TODO freq prop secondary: DBOARD_SLOT_B codec...
184

    
185
    case DSP_PROP_HOST_RATE: {
186
            unsigned int rate =
187
                    _clock_ctrl->get_master_clock_freq() * 2 / val.as<double>();
188

    
189
            if ((rate & 0x01) || (rate < 8) || (rate > 512)) {
190
                std::cerr << "Interpolation rate must be even and between 8 and 512"
191
                          << std::endl;
192
                return;
193
            }
194

    
195
            _tx_dsp_interp = rate;
196
            _iface->poke32(FR_INTERP_RATE, _tx_dsp_interp / 4 - 1);
197
            return;
198
        }
199
    default: UHD_THROW_PROP_SET_ERROR();
200
    }
201

    
202
}