switch source package format to 3.0 quilt
[debian/gnuradio] / usrp2 / host / lib / usrp2_impl.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,2009,2010 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef INCLUDED_USRP2_IMPL_H
20 #define INCLUDED_USRP2_IMPL_H
21
22 #include <usrp2/usrp2.h>
23 #include <usrp2/data_handler.h>
24 #include <usrp2_eth_packet.h>
25 #include <gruel/thread.h>
26 #include <boost/scoped_ptr.hpp>
27 #include "control.h"
28 #include "ring.h"
29 #include <string>
30
31 #define MAX_SUBPKT_LEN 252
32
33 namespace usrp2 {
34
35   class eth_buffer;
36   class pktfilter;
37   class usrp2_thread;
38   class usrp2_tune_result;
39   class pending_reply;
40   class ring;
41
42   //! High-level d'board info
43   struct db_info {
44     int         dbid;
45     double      freq_min;               // Hz
46     double      freq_max;               // Hz
47     double      gain_min;               // dB
48     double      gain_max;               // dB
49     double      gain_step_size;         // dB
50
51     db_info() : dbid(-1), freq_min(0), freq_max(0),
52                 gain_min(0), gain_max(0), gain_step_size(0) {}
53   };
54
55   class usrp2::impl : private data_handler
56   {
57     static const size_t NRIDS = 256;
58     static const size_t NCHANS = 32;
59
60     eth_buffer    *d_eth_buf;
61     std::string    d_interface_name;
62     pktfilter     *d_pf;
63     std::string    d_addr;       // FIXME: use u2_mac_addr_t instead
64
65     boost::thread_group d_rx_tg;
66     volatile bool  d_bg_running; // TODO: multistate if needed
67
68     int            d_rx_seqno;
69     int            d_tx_seqno;
70     int            d_next_rid;
71     unsigned int   d_num_rx_frames;
72     unsigned int   d_num_rx_missing;
73     unsigned int   d_num_rx_overruns;
74     unsigned int   d_num_rx_bytes;
75
76     unsigned int   d_num_enqueued;
77     gruel::mutex   d_enqueued_mutex;
78     gruel::condition_variable d_bg_pending_cond;
79
80     // all pending_replies are stack allocated, thus no possibility of leaking these
81     pending_reply *d_pending_replies[NRIDS]; // indexed by 8-bit reply id
82
83     std::vector<ring_sptr>   d_channel_rings; // indexed by 5-bit channel number
84     gruel::mutex   d_channel_rings_mutex;
85
86     db_info        d_tx_db_info;
87     db_info        d_rx_db_info;
88
89     int            d_tx_interp;         // shadow tx interp
90     int            d_rx_decim;          // shadow rx decim
91
92     bool           d_dont_enqueue;
93
94     void inc_enqueued() {
95       gruel::scoped_lock l(d_enqueued_mutex);
96       d_num_enqueued++;
97     }
98
99     void dec_enqueued() {
100       gruel::scoped_lock l(d_enqueued_mutex);
101       if (--d_num_enqueued == 0)
102         d_bg_pending_cond.notify_one();
103     }
104
105     static bool parse_mac_addr(const std::string &s, u2_mac_addr_t *p);
106     void init_et_hdrs(u2_eth_packet_t *p, const std::string &dst);
107     void init_etf_hdrs(u2_eth_packet_t *p, const std::string &dst,
108                        int word0_flags, int chan, uint32_t timestamp);
109     void start_bg();
110     void stop_bg();
111     void init_config_rx_v2_cmd(op_config_rx_v2_cmd *cmd);
112     void init_config_tx_v2_cmd(op_config_tx_v2_cmd *cmd);
113     bool transmit_cmd_and_wait(void *cmd, size_t len, pending_reply *p, double secs=0.0);
114     bool transmit_cmd(void *cmd, size_t len);
115     virtual data_handler::result operator()(const void *base, size_t len);
116     data_handler::result handle_control_packet(const void *base, size_t len);
117     data_handler::result handle_data_packet(const void *base, size_t len);
118     bool dboard_info();
119     bool reset_db();
120
121   public:
122     impl(const std::string &ifc, props *p, size_t rx_bufsize);
123     ~impl();
124
125     std::string mac_addr() const { return d_addr; } // FIXME: convert from u2_mac_addr_t
126     std::string interface_name() const { return d_interface_name; }
127
128     // Rx
129
130     bool set_rx_antenna(int ant);
131     bool set_rx_gain(double gain);
132     double rx_gain_min() { return d_rx_db_info.gain_min; }
133     double rx_gain_max() { return d_rx_db_info.gain_max; }
134     double rx_gain_db_per_step() { return d_rx_db_info.gain_step_size; }
135     bool set_rx_lo_offset(double frequency);
136     bool set_rx_center_freq(double frequency, tune_result *result);
137     double rx_freq_min() { return d_rx_db_info.freq_min; }
138     double rx_freq_max() { return d_rx_db_info.freq_max; }
139     bool set_rx_decim(int decimation_factor);
140     int rx_decim() { return d_rx_decim; }
141     bool set_rx_scale_iq(int scale_i, int scale_q);
142     bool set_gpio_ddr(int bank, uint16_t value, uint16_t mask);
143     bool set_gpio_sels(int bank, std::string src);
144     bool enable_gpio_streaming(int bank, int enable);
145     bool write_gpio(int bank, uint16_t value, uint16_t mask);
146     bool read_gpio(int bank, uint16_t *value);
147     bool start_rx_streaming(unsigned int channel, unsigned int items_per_frame);
148     bool start_rx_streaming_at(unsigned int channel, unsigned int items_per_frame, unsigned int time);
149     bool sync_and_start_rx_streaming_at(unsigned int channel, unsigned int items_per_frame, unsigned int time);
150     bool rx_samples(unsigned int channel, rx_sample_handler *handler);
151     bool flush_rx_samples(unsigned int channel);
152     bool stop_rx_streaming(unsigned int channel);
153     unsigned int rx_overruns() const { return d_num_rx_overruns; }
154     unsigned int rx_missing() const { return d_num_rx_missing; }
155
156     // Tx
157
158     bool set_tx_antenna(int ant);
159     bool set_tx_gain(double gain);
160     double tx_gain_min() { return d_tx_db_info.gain_min; }
161     double tx_gain_max() { return d_tx_db_info.gain_max; }
162     double tx_gain_db_per_step() { return d_tx_db_info.gain_step_size; }
163     bool set_tx_lo_offset(double frequency);
164     bool set_tx_center_freq(double frequency, tune_result *result);
165     double tx_freq_min() { return d_tx_db_info.freq_min; }
166     double tx_freq_max() { return d_tx_db_info.freq_max; }
167     bool set_tx_interp(int interpolation_factor);
168     int tx_interp() { return d_tx_interp; }
169     void default_tx_scale_iq(int interpolation_factor, int *scale_i, int *scale_q);
170     bool set_tx_scale_iq(int scale_i, int scale_q);
171
172     bool tx_32fc(unsigned int channel,
173                  const std::complex<float> *samples,
174                  size_t nsamples,
175                  const tx_metadata *metadata);
176
177     bool tx_16sc(unsigned int channel,
178                  const std::complex<int16_t> *samples,
179                  size_t nsamples,
180                  const tx_metadata *metadata);
181
182     bool tx_raw(unsigned int channel,
183                 const uint32_t *items,
184                 size_t nitems,
185                 const tx_metadata *metadata);
186
187     // misc
188
189     bool config_mimo(int flags);
190     bool fpga_master_clock_freq(long *freq);
191     bool adc_rate(long *rate);
192     bool dac_rate(long *rate);
193     bool tx_daughterboard_id(int *dbid);
194     bool rx_daughterboard_id(int *dbid);
195
196     // low level
197
198     bool burn_mac_addr(const std::string &new_addr);
199     bool sync_to_pps();
200     bool sync_every_pps(bool enable);
201     std::vector<uint32_t> peek32(uint32_t addr, uint32_t words);
202     bool poke32(uint32_t addr, const std::vector<uint32_t> &data);
203
204     // Receive thread, need to be public for boost::bind
205     void bg_loop();
206   };
207
208 } // namespace usrp2
209
210 #endif /* INCLUDED_USRP2_IMPL_H */