Renamed identifiers for consistency: s/complex_float/32fc/ s/complex_16/16sc/.
[debian/gnuradio] / usrp2 / host / apps / tx_samples.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008 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 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 #include <usrp2/usrp2.h>
23 #include <usrp2/strtod_si.h>
24 #include <iostream>
25 #include <complex>
26 #include <getopt.h>
27 #include <gruel/realtime.h>
28 #include <signal.h>
29 #include <string.h>
30 #include <stdexcept>
31
32
33 typedef std::complex<float> fcomplex;
34
35 static volatile bool signaled = false;
36
37 static void 
38 sig_handler(int sig)
39 {
40   signaled = true;
41 }
42
43 static void
44 install_sig_handler(int signum,
45                     void (*new_handler)(int))
46 {
47   struct sigaction new_action;
48   memset (&new_action, 0, sizeof (new_action));
49
50   new_action.sa_handler = new_handler;
51   sigemptyset (&new_action.sa_mask);
52   new_action.sa_flags = 0;
53
54   if (sigaction (signum, &new_action, 0) < 0){
55     perror ("sigaction (install new)");
56     throw std::runtime_error ("sigaction");
57   }
58 }
59
60
61 static const char *
62 prettify_progname(const char *progname)         // that's probably almost a word ;)
63 {
64   const char *p = strrchr(progname, '/');       // drop leading directory path
65   if (p)
66     p++;
67
68   if (strncmp(p, "lt-", 3) == 0)                // drop lt- libtool prefix
69     p += 3;
70
71   return p;
72 }
73
74 static void
75 usage(const char *progname)
76 {
77   fprintf(stderr, "Usage: %s [options]\n\n", prettify_progname(progname));
78   fprintf(stderr, "Options:\n");
79   fprintf(stderr, "  -h                   show this message and exit\n");
80   fprintf(stderr, "  -e ETH_INTERFACE     specify ethernet interface [default=eth0]\n");
81   fprintf(stderr, "  -m MAC_ADDR          mac address of USRP2 HH:HH [default=first one found]\n");
82   fprintf(stderr, "  -I INPUT_FILE        set input filename [default=stdin]\n");
83   fprintf(stderr, "  -r                   repeat.  When EOF of input file is reached, seek to beginning\n");
84   fprintf(stderr, "  -f FREQ              set frequency to FREQ [default=0]\n");
85   fprintf(stderr, "  -i INTERP            set interpolation rate to INTERP [default=32]\n");
86   fprintf(stderr, "  -g gain              set tx gain\n");
87   fprintf(stderr, "  -S SCALE             fpga scaling factor for I & Q [default=256]\n");
88 }
89
90 #define GAIN_NOT_SET (-1000)
91 #define MAX_SAMPLES (371)
92
93 int
94 main(int argc, char **argv)
95 {
96   const char *interface = "eth0";
97   const char *input_filename = 0;
98   bool repeat = false;
99   const char *mac_addr_str = "";
100   double freq = 0;
101   int32_t interp = 32;
102   int32_t samples_per_frame = MAX_SAMPLES;
103   int32_t scale = 3000;
104   double gain = GAIN_NOT_SET;
105   
106   int    ch;
107   double tmp;
108
109
110   while ((ch = getopt(argc, argv, "he:m:I:rf:i:S:F:g:")) != EOF){
111     switch (ch){
112
113     case 'e':
114       interface = optarg;
115       break;
116       
117     case 'm':
118       mac_addr_str = optarg;
119 #if 0
120       if (!usrp2_basic::parse_mac_addr(optarg, &mac_addr)){
121         std::cerr << "invalid mac addr: " << optarg << std::endl;
122         usage(argv[0]);
123         return 1;
124       }
125 #endif
126       break;
127
128     case 'I':
129       input_filename = optarg;
130       break;
131       
132     case 'r':
133       repeat = true;
134       break;
135       
136     case 'f':
137       if (!strtod_si(optarg, &freq)){
138         std::cerr << "invalid number: " << optarg << std::endl;
139         usage(argv[0]);
140         return 1;
141       }
142       break;
143
144     case 'F':
145       samples_per_frame = strtol(optarg, 0, 0);
146       break;
147
148     case 'i':
149       interp = strtol(optarg, 0, 0);
150       break;
151
152     case 'S':
153       if (!strtod_si(optarg, &tmp)){
154         std::cerr << "invalid number: " << optarg << std::endl;
155         usage(argv[0]);
156         return 1;
157       }
158       scale = static_cast<int32_t>(tmp);
159       break;
160       
161     case 'h':
162     default:
163       usage(argv[0]);
164       return 1;
165     }
166   }
167
168   
169   if (argc - optind != 0){
170     usage(argv[0]);
171     return 1;
172   }
173   
174   if (samples_per_frame < 9 || samples_per_frame > MAX_SAMPLES){
175     std::cerr << prettify_progname(argv[0])
176               << ": samples_per_frame is out of range.  "
177               << "Must be in [9, " << MAX_SAMPLES << "].\n";
178     usage(argv[0]);
179     return 1;
180   }
181
182
183   FILE *fp = 0;
184   if (input_filename == 0)
185     fp = stdin;
186   else {
187     fp = fopen(input_filename, "rb");
188     if (fp == 0){
189       perror(input_filename);
190       return 1;
191     }
192   }
193
194   install_sig_handler(SIGINT, sig_handler);
195
196
197   gruel::rt_status_t rt = gruel::enable_realtime_scheduling();
198   if (rt != gruel::RT_OK)
199     std::cerr << "Failed to enable realtime scheduling" << std::endl;
200
201
202   usrp2::usrp2::sptr u2 = usrp2::usrp2::make(interface, mac_addr_str);
203   
204   if (gain != GAIN_NOT_SET){
205     if (!u2->set_tx_gain(gain)){
206       std::cerr << "set_tx_gain failed\n";
207       return 1;
208     }
209   }
210
211   usrp2::tune_result tr;
212   if (!u2->set_tx_center_freq(freq, &tr)){
213     fprintf(stderr, "set_tx_center_freq(%g) failed\n", freq);
214     return 1;
215   }
216
217   printf("Daughterboard configuration:\n");
218   printf("  baseband_freq=%f\n", tr.baseband_freq);
219   printf("       duc_freq=%f\n", tr.dxc_freq);
220   printf("  residual_freq=%f\n", tr.residual_freq);
221   printf("       inverted=%s\n\n", tr.spectrum_inverted ? "yes" : "no");
222
223   if (!u2->set_tx_interp(interp)){
224     fprintf(stderr, "set_tx_interp(%d) failed\n", interp);
225     return 1;
226   }
227
228   if (!u2->set_tx_scale_iq(scale, scale)){
229     std::cerr << "set_tx_scale_iq failed\n";
230     return 1;
231   }
232
233
234   usrp2::tx_metadata    md;
235   md.timestamp = -1;
236   md.start_of_burst = 1;
237   md.send_now = 1;
238
239   while (!signaled){
240
241     std::complex<int16_t> samples[MAX_SAMPLES];
242
243     int r = fread(samples, sizeof(uint32_t), samples_per_frame, fp);
244
245     // fprintf(stderr, "fread -> %d\n", r);
246     
247     if (r == 0){
248       if (!repeat)
249         break;
250       if (fseek(fp, 0, SEEK_SET) == -1)
251         break;
252     }
253
254     // FIXME if r < 9, pad to 9 for minimum packet size constraint
255
256     if (!u2->tx_16sc(0, samples, r, &md)){
257       fprintf(stderr, "tx_complex_int16 failed\n");
258       break;
259     }
260   }
261
262   return 0;
263 }