Merged r11377:11390 from jcorgan/usrp-headers in to trunk.
[debian/gnuradio] / usrp / limbo / inband / usrp_rx_stub.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008 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 along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <usrp_rx_stub.h>
27
28 #include <iostream>
29 #include <vector>
30 #include <usb.h>
31 #include <mblock/class_registry.h>
32 #include <usrp_inband_usb_packet.h>
33 #include <fpga_regs_common.h>
34 #include "usrp_standard.h"
35 #include <stdio.h>
36 #include <string.h>
37 #include <ui_nco.h>
38 #include <fstream>
39
40 #include <symbols_usrp_rx_cs.h>
41
42 typedef usrp_inband_usb_packet transport_pkt;
43
44 static const bool verbose = false;
45
46 bool usrp_rx_stop_stub;
47
48 // Used for the fake control packet response code to send the responses back up
49 // the RX.  The TX stub dumps responses in to this queue.
50 std::queue<pmt_t> d_cs_queue;
51
52 usrp_rx_stub::usrp_rx_stub(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg)
53   : mb_mblock(rt, instance_name, user_arg),
54     d_samples_per_frame((long)(126)),
55     d_decim_rx(128),
56     d_amplitude(16384),
57     d_disk_write(false)
58 {
59
60   // Information about the rates are passed all the way from the app in the form
61   // of a dictionary.  We use this to read the RX decimation rate and compute
62   // the approximate number of MS/s as a form of flow control for the stub.
63   pmt_t usrp_dict = user_arg;
64
65   if (pmt_is_dict(usrp_dict)) {
66     // Read the RX decimation rate
67     if(pmt_t decim_rx = pmt_dict_ref(usrp_dict, 
68                                       pmt_intern("decim-rx"), 
69                                       PMT_NIL)) {
70       if(!pmt_eqv(decim_rx, PMT_NIL)) 
71         d_decim_rx = pmt_to_long(decim_rx);
72     }
73   }
74
75   d_cs = define_port("cs", "usrp-rx-cs", true, mb_port::EXTERNAL);
76   
77   // initialize NCO
78   double freq = 100e3;
79   int interp = 32;                          // 32 -> 4MS/s
80   double sample_rate = 64e6 / interp;   
81   d_nco.set_freq(2*M_PI * freq/sample_rate);
82
83   //d_disk_write = true;
84   
85   if(d_disk_write)
86     d_ofile.open("raw_rx.dat",std::ios::binary|std::ios::out);
87   
88   usrp_rx_stop_stub = false;
89 }
90
91 usrp_rx_stub::~usrp_rx_stub() 
92 {
93   if(d_disk_write)
94     d_ofile.close();
95 }
96
97 void 
98 usrp_rx_stub::initial_transition()
99 {
100 }
101
102 void
103 usrp_rx_stub::handle_message(mb_message_sptr msg)
104 {
105   pmt_t event = msg->signal();
106   pmt_t port_id = msg->port_id();
107   pmt_t data = msg->data(); 
108
109   if (pmt_eq(msg->signal(), s_timeout)
110       && !pmt_eq(msg->data(), s_done)) {
111   
112     if(!usrp_rx_stop_stub) 
113       read_and_respond();
114     else {  // requested to stop
115       cancel_timeout(msg->metadata());
116       usrp_rx_stop_stub=false;
117       if(verbose)
118         std::cout << "[USRP_RX_STUB] Stopping RX stub\n";
119     }
120
121   }
122
123   // Theoretically only have 1 message to ever expect, but
124   // want to make sure its at least what we want
125   if(pmt_eq(port_id, d_cs->port_symbol())
126       && pmt_eqv(event, s_cmd_usrp_rx_start_reading)) {
127
128     if(verbose)
129       std::cout << "[USRP_RX_STUB] Starting with decim @ " 
130                 << d_decim_rx << std::endl;
131     
132       start_packet_timer();
133   }
134 }
135
136 // Setup a periodic timer which will drive packet generation
137 void
138 usrp_rx_stub::start_packet_timer()
139 {
140   d_t0 = mb_time::time();   // current time
141
142   // Calculate the inter-packet arrival time.  
143   double samples_per_sec = (64.0/(double)d_decim_rx)*1000000.0;
144   double frames_per_sec = samples_per_sec / (double)d_samples_per_frame;
145   double frame_rate = 1.0 / frames_per_sec;
146
147   if(verbose) {
148     std::cout << "[USRP_RX_STUB] Scheduling periodic packet generator\n";
149     std::cout << "\tsamples_per_sec: " << samples_per_sec << std::endl;
150     std::cout << "\tframes_per_sec: " << frames_per_sec << std::endl;
151     std::cout << "\tframe_rate: " << frame_rate << std::endl;
152   }
153
154   schedule_periodic_timeout(d_t0 + frame_rate, mb_time(frame_rate), PMT_T);
155 }
156
157 void
158 usrp_rx_stub::read_and_respond()
159 {
160
161   long nsamples_this_frame = d_samples_per_frame;
162
163   size_t nshorts = 2 * nsamples_this_frame;     // 16-bit I & Q
164   long channel = 0;
165   long n_bytes = nshorts*2;
166   pmt_t uvec = pmt_make_s16vector(nshorts, 0);
167   size_t ignore;
168   int16_t *samples = pmt_s16vector_writable_elements(uvec, ignore);
169
170   // fill in the complex sinusoid
171
172   for (int i = 0; i < nsamples_this_frame; i++){
173
174     if (1){
175       gr_complex s;
176       d_nco.sincos(&s, 1, d_amplitude);
177       // write 16-bit i & q
178       samples[2*i] =   (int16_t) s.real();
179       samples[2*i+1] = (int16_t) s.imag();
180     }
181     else {
182       gr_complex s(d_amplitude, d_amplitude);
183
184       // write 16-bit i & q
185       samples[2*i] =   (int16_t) s.real();
186       samples[2*i+1] = (int16_t) s.imag();
187     }
188   }
189   
190   if(d_disk_write)
191     d_ofile.write((const char *)samples, n_bytes);
192
193   pmt_t v_pkt = pmt_make_u8vector(sizeof(transport_pkt), 0);
194   transport_pkt *pkt =
195     (transport_pkt *) pmt_u8vector_writable_elements(v_pkt, ignore);
196
197   pkt->set_header(0, channel, 0, n_bytes);
198   pkt->set_timestamp(0xffffffff);
199   memcpy(pkt->payload(), samples, n_bytes);
200   
201   d_cs->send(s_response_usrp_rx_read, pmt_list3(PMT_NIL, PMT_T, v_pkt));
202
203   // Now lets check the shared CS queue between the TX and RX stub.  Each
204   // element in a queue is a list where the first element is an invocation
205   // handle and the second element is a PMT u8 vect representation of the
206   // CS packet response which can just be passed transparently.
207   while(!d_cs_queue.empty()) {
208     
209     pmt_t cs_pkt = d_cs_queue.front();
210     d_cs_queue.pop();
211
212     pmt_t invocation_handle = pmt_nth(0, cs_pkt);
213     pmt_t v_pkt = pmt_nth(1, cs_pkt);
214
215     d_cs->send(s_response_usrp_rx_read,   
216                pmt_list3(invocation_handle, 
217                          PMT_T, 
218                          v_pkt));  // Take the front CS pkt
219
220     
221     if(verbose)
222       std::cout << "[USRP_RX_STUB] Received CS response from TX stub\n";
223   }
224
225 }
226
227 REGISTER_MBLOCK_CLASS(usrp_rx_stub);