Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / dsp_impl.cpp @ 9cb9e7d5

History | View | Annotate | Download (5.8 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 "../dsp_utils.hpp"
21
#include <uhd/usrp/dsp_props.hpp>
22
#include <boost/bind.hpp>
23
#include <iostream>
24
#include <cstdio>
25

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

    
29
/***********************************************************************
30
 * RX DDC Initialization
31
 **********************************************************************/
32
void usrp1_impl::rx_ddc_init(void)
33
{
34
    _rx_ddc_proxy = wax_obj_proxy::make(
35
        boost::bind(&usrp1_impl::rx_ddc_get, this, _1, _2),
36
        boost::bind(&usrp1_impl::rx_ddc_set, this, _1, _2));
37

    
38
    rx_ddc_set(DSP_PROP_HOST_RATE, double(64e6/10));
39
}
40

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

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

    
55
    case DSP_PROP_FREQ_SHIFT:
56
        val = _ddc_freq;
57
        return;
58

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

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

    
67
    default: UHD_THROW_PROP_GET_ERROR();
68
    }
69

    
70
}
71

    
72
/***********************************************************************
73
 * RX DDC Set
74
 **********************************************************************/
75
unsigned int compute_freq_word(double master, double target)
76
{
77
    static const int NBITS = 14;
78
    int   v = (int) rint (target / master * pow(2.0, 32.0));
79
 
80
    if (0)
81
      v = (v >> (32 - NBITS)) << (32 - NBITS);    // keep only top NBITS
82
 
83
    double actual_freq = v * master / pow(2.0, 32.0);
84
 
85
    if (0)
86
      fprintf (stderr,
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_ddc_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
            _iface->poke32(FR_RX_FREQ_0, compute_freq_word(64e6, new_freq));
99
            _ddc_freq = new_freq;
100
            return;
101
        }
102
    case DSP_PROP_HOST_RATE: {
103
            //FIXME: Stop and resume streaming during set?
104
            unsigned int rate =
105
                    _clock_ctrl->get_master_clock_freq() / val.as<double>();
106

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

    
113
            _ddc_decim = rate;
114
            _iface->poke32(FR_DECIM_RATE, _ddc_decim/2 - 1);
115
        }
116
        return;
117

    
118
    default: UHD_THROW_PROP_SET_ERROR();
119
    }
120

    
121
}
122

    
123
/***********************************************************************
124
 * TX DUC Initialization
125
 **********************************************************************/
126
void usrp1_impl::tx_duc_init(void)
127
{
128
    _tx_duc_proxy = wax_obj_proxy::make(
129
                          boost::bind(&usrp1_impl::tx_duc_get, this, _1, _2),
130
                          boost::bind(&usrp1_impl::tx_duc_set, this, _1, _2));
131

    
132
    //initial config and update
133
    tx_duc_set(DSP_PROP_HOST_RATE, double(64e6/10));
134
}
135

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

    
146
    case DSP_PROP_OTHERS:
147
        val = prop_names_t(); //empty
148
        return;
149

    
150
    case DSP_PROP_FREQ_SHIFT:
151
        val = _duc_freq;
152
        return;
153

    
154
    case DSP_PROP_CODEC_RATE:
155
        val = MASTER_CLOCK_RATE;
156
        return;
157

    
158
    case DSP_PROP_HOST_RATE:
159
        val = _clock_ctrl->get_master_clock_freq() * 2 / _duc_interp;
160
        return;
161

    
162
    default: UHD_THROW_PROP_GET_ERROR();
163
    }
164

    
165
}
166

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

    
174
    case DSP_PROP_FREQ_SHIFT: {
175
            double new_freq = val.as<double>();
176
            _codec_ctrl->set_duc_freq(new_freq);
177
            _duc_freq = new_freq;
178
            return;
179
        }
180
    case DSP_PROP_HOST_RATE: {
181
            unsigned int rate =
182
                    _clock_ctrl->get_master_clock_freq() * 2 / val.as<double>();
183

    
184
            if ((rate & 0x01) || (rate < 8) || (rate > 512)) {
185
                std::cerr << "Interpolation rate must be even and between 8 and 512"
186
                          << std::endl;
187
                return;
188
            }
189

    
190
            _duc_interp = rate;
191
            _iface->poke32(FR_INTERP_RATE, _duc_interp / 4 - 1);
192
            return;
193
        }
194
    default: UHD_THROW_PROP_SET_ERROR();
195
    }
196

    
197
}