3 * Copyright 2007,2008 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
26 #include <usrp_rx_stub.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"
40 #include <symbols_usrp_rx_cs.h>
42 typedef usrp_inband_usb_packet transport_pkt;
44 static const bool verbose = false;
46 bool usrp_rx_stop_stub;
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;
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)),
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;
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"),
70 if(!pmt_eqv(decim_rx, PMT_NIL))
71 d_decim_rx = pmt_to_long(decim_rx);
75 d_cs = define_port("cs", "usrp-rx-cs", true, mb_port::EXTERNAL);
79 int interp = 32; // 32 -> 4MS/s
80 double sample_rate = 64e6 / interp;
81 d_nco.set_freq(2*M_PI * freq/sample_rate);
83 //d_disk_write = true;
86 d_ofile.open("raw_rx.dat",std::ios::binary|std::ios::out);
88 usrp_rx_stop_stub = false;
91 usrp_rx_stub::~usrp_rx_stub()
98 usrp_rx_stub::initial_transition()
103 usrp_rx_stub::handle_message(mb_message_sptr msg)
105 pmt_t event = msg->signal();
106 pmt_t port_id = msg->port_id();
107 pmt_t data = msg->data();
109 if (pmt_eq(msg->signal(), s_timeout)
110 && !pmt_eq(msg->data(), s_done)) {
112 if(!usrp_rx_stop_stub)
114 else { // requested to stop
115 cancel_timeout(msg->metadata());
116 usrp_rx_stop_stub=false;
118 std::cout << "[USRP_RX_STUB] Stopping RX stub\n";
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)) {
129 std::cout << "[USRP_RX_STUB] Starting with decim @ "
130 << d_decim_rx << std::endl;
132 start_packet_timer();
136 // Setup a periodic timer which will drive packet generation
138 usrp_rx_stub::start_packet_timer()
140 d_t0 = mb_time::time(); // current time
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;
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;
154 schedule_periodic_timeout(d_t0 + frame_rate, mb_time(frame_rate), PMT_T);
158 usrp_rx_stub::read_and_respond()
161 long nsamples_this_frame = d_samples_per_frame;
163 size_t nshorts = 2 * nsamples_this_frame; // 16-bit I & Q
165 long n_bytes = nshorts*2;
166 pmt_t uvec = pmt_make_s16vector(nshorts, 0);
168 int16_t *samples = pmt_s16vector_writable_elements(uvec, ignore);
170 // fill in the complex sinusoid
172 for (int i = 0; i < nsamples_this_frame; i++){
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();
182 gr_complex s(d_amplitude, d_amplitude);
184 // write 16-bit i & q
185 samples[2*i] = (int16_t) s.real();
186 samples[2*i+1] = (int16_t) s.imag();
191 d_ofile.write((const char *)samples, n_bytes);
193 pmt_t v_pkt = pmt_make_u8vector(sizeof(transport_pkt), 0);
195 (transport_pkt *) pmt_u8vector_writable_elements(v_pkt, ignore);
197 pkt->set_header(0, channel, 0, n_bytes);
198 pkt->set_timestamp(0xffffffff);
199 memcpy(pkt->payload(), samples, n_bytes);
201 d_cs->send(s_response_usrp_rx_read, pmt_list3(PMT_NIL, PMT_T, v_pkt));
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()) {
209 pmt_t cs_pkt = d_cs_queue.front();
212 pmt_t invocation_handle = pmt_nth(0, cs_pkt);
213 pmt_t v_pkt = pmt_nth(1, cs_pkt);
215 d_cs->send(s_response_usrp_rx_read,
216 pmt_list3(invocation_handle,
218 v_pkt)); // Take the front CS pkt
222 std::cout << "[USRP_RX_STUB] Received CS response from TX stub\n";
227 REGISTER_MBLOCK_CLASS(usrp_rx_stub);