Imported Upstream version 3.2.2
[debian/gnuradio] / gr-usrp / src / usrp_source_c.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2006 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <usrp_source_c.h>
28 #include <gr_io_signature.h>
29 #include <usrp_standard.h>
30 #include <usrp_bytesex.h>
31
32 static const int NBASIC_SAMPLES_PER_ITEM = 2;   // I & Q
33
34 usrp_source_c_sptr
35 usrp_make_source_c (int which_board,
36                      unsigned int decim_rate,
37                      int nchan,
38                      int mux,
39                      int mode,
40                      int fusb_block_size,
41                      int fusb_nblocks,
42                      const std::string fpga_filename,
43                      const std::string firmware_filename
44                      ) throw (std::runtime_error)
45 {
46   return usrp_source_c_sptr (new usrp_source_c (which_board,
47                                                   decim_rate,
48                                                   nchan,
49                                                   mux,
50                                                   mode,
51                                                   fusb_block_size,
52                                                   fusb_nblocks,
53                                                   fpga_filename,
54                                                   firmware_filename
55                                                   ));
56 }
57
58
59 usrp_source_c::usrp_source_c (int which_board,
60                                 unsigned int decim_rate,
61                                 int nchan,
62                                 int mux,
63                                 int mode,
64                                 int fusb_block_size,
65                                 int fusb_nblocks,
66                                 const std::string fpga_filename,
67                                 const std::string firmware_filename
68                                 ) throw (std::runtime_error)
69   : usrp_source_base ("usrp_source_c",
70                        gr_make_io_signature (1, 1, sizeof (gr_complex)),
71                        which_board, decim_rate, nchan, mux, mode,
72                        fusb_block_size, fusb_nblocks,
73                        fpga_filename, firmware_filename)
74 {
75 }
76
77 usrp_source_c::~usrp_source_c ()
78 {
79   // NOP
80 }
81
82 int
83 usrp_source_c::ninput_bytes_reqd_for_noutput_items (int noutput_items)
84 {
85   return noutput_items * NBASIC_SAMPLES_PER_ITEM * sizeof_basic_sample();
86 }
87
88 /*
89  * Convert interleaved 8 or 16-bit I & Q from usrp buffer into a single
90  * complex output stream.
91  */
92 void
93 usrp_source_c::copy_from_usrp_buffer (gr_vector_void_star &output_items,
94                                        int output_index,
95                                        int output_items_available,
96                                        int &output_items_produced,
97                                        const void *usrp_buffer,
98                                        int usrp_buffer_length,
99                                        int &bytes_read)
100 {
101   gr_complex *out = &((gr_complex *) output_items[0])[output_index];
102   unsigned sbs = sizeof_basic_sample();
103   unsigned nusrp_bytes_per_item = NBASIC_SAMPLES_PER_ITEM * sbs;
104
105   int nitems = std::min (output_items_available,
106                          (int)(usrp_buffer_length / nusrp_bytes_per_item));
107
108   signed char *s8 = (signed char *) usrp_buffer;
109   short *s16 = (short *) usrp_buffer;
110
111   switch (sbs){
112   case 1:
113     for (int i = 0; i < nitems; i++){
114       out[i] = gr_complex ((float)(s8[2*i+0] << 8), (float)(s8[2*i+1] << 8));
115     }
116     break;
117
118   case 2:
119     for (int i = 0; i < nitems; i++){
120       out[i] = gr_complex ((float) usrp_to_host_short(s16[2*i+0]),
121                            (float) usrp_to_host_short(s16[2*i+1]));
122     }
123     break;
124
125   default:
126     assert(0);
127   }
128
129   output_items_produced = nitems;
130   bytes_read = nitems * nusrp_bytes_per_item;
131 }