Imported Upstream version 3.2.2
[debian/gnuradio] / gr-usrp / src / usrp_source_s.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_s.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 = 1;
33
34 usrp_source_s_sptr
35 usrp_make_source_s (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_s_sptr (new usrp_source_s (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_s::usrp_source_s (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_s",
70                        gr_make_io_signature (1, 1, sizeof (short)),
71                        which_board, decim_rate, nchan, mux, mode,
72                        fusb_block_size,
73                        fusb_nblocks,
74                        fpga_filename, firmware_filename)
75 {
76 }
77
78 usrp_source_s::~usrp_source_s ()
79 {
80   // NOP
81 }
82
83 int
84 usrp_source_s::ninput_bytes_reqd_for_noutput_items (int noutput_items)
85 {
86   return noutput_items * NBASIC_SAMPLES_PER_ITEM * sizeof_basic_sample();
87 }
88
89 /*
90  * Convert interleaved 8 or 16-bit I & Q from usrp buffer into a single
91  * short output stream.
92  */
93 void
94 usrp_source_s::copy_from_usrp_buffer (gr_vector_void_star &output_items,
95                                        int output_index,
96                                        int output_items_available,
97                                        int &output_items_produced,
98                                        const void *usrp_buffer,
99                                        int usrp_buffer_length,
100                                        int &bytes_read)
101 {
102   short *out = &((short *) output_items[0])[output_index];
103   unsigned sbs = sizeof_basic_sample();
104   unsigned nusrp_bytes_per_item = NBASIC_SAMPLES_PER_ITEM * sbs;
105
106   int nitems = std::min (output_items_available,
107                          (int)(usrp_buffer_length / nusrp_bytes_per_item));
108
109   signed char *s8 = (signed char *) usrp_buffer;
110   short *s16 = (short *) usrp_buffer;
111
112   switch(sbs){
113   case 1:
114     for (int i = 0; i < nitems; i++){
115       out[i] = s8[i] << 8;
116     }
117     break;
118
119   case 2:
120     for (int i = 0; i < nitems; i++){
121       out[i] = usrp_to_host_short(s16[i]);
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 }