3 * Copyright 2007 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 <mb_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;
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)),
58 d_cs = define_port("cs", "usrp-rx-cs", true, mb_port::EXTERNAL);
62 int interp = 32; // 32 -> 4MS/s
63 double sample_rate = 128e6 / interp;
64 d_nco.set_freq(2*M_PI * freq/sample_rate);
66 //d_disk_write = true;
69 d_ofile.open("raw_rx.dat",std::ios::binary|std::ios::out);
74 usrp_rx_stub::~usrp_rx_stub()
81 usrp_rx_stub::initial_transition()
87 usrp_rx_stub::handle_message(mb_message_sptr msg)
89 pmt_t event = msg->signal();
90 pmt_t port_id = msg->port_id();
91 pmt_t data = msg->data();
93 // Theoretically only have 1 message to ever expect, but
94 // want to make sure its at least what we want
95 if(pmt_eq(port_id, d_cs->port_symbol())) {
98 std::cout << "[USRP_RX_STUB] Starting...\n";
100 if(pmt_eqv(event, s_cmd_usrp_rx_start_reading))
101 read_and_respond(data);
106 usrp_rx_stub::read_and_respond(pmt_t data)
109 while(!usrp_rx_stop) {
111 long nsamples_this_frame = d_samples_per_frame;
113 size_t nshorts = 2 * nsamples_this_frame; // 16-bit I & Q
115 long n_bytes = nshorts*2;
116 pmt_t uvec = pmt_make_s16vector(nshorts, 0);
118 int16_t *samples = pmt_s16vector_writeable_elements(uvec, ignore);
120 // fill in the complex sinusoid
122 for (int i = 0; i < nsamples_this_frame; i++){
126 d_nco.sincos(&s, 1, d_amplitude);
127 // write 16-bit i & q
128 samples[2*i] = (int16_t) s.real();
129 samples[2*i+1] = (int16_t) s.imag();
132 gr_complex s(d_amplitude, d_amplitude);
134 // write 16-bit i & q
135 samples[2*i] = (int16_t) s.real();
136 samples[2*i+1] = (int16_t) s.imag();
141 d_ofile.write((const char *)samples, n_bytes);
143 pmt_t v_pkt = pmt_make_u8vector(sizeof(transport_pkt), 0);
145 (transport_pkt *) pmt_u8vector_writeable_elements(v_pkt, ignore);
147 pkt->set_header(0, channel, 0, n_bytes);
148 pkt->set_timestamp(0xffffffff);
149 memcpy(pkt->payload(), samples, n_bytes);
151 d_cs->send(s_response_usrp_rx_read, pmt_list3(PMT_NIL, PMT_T, v_pkt));
153 // Now lets check the shared CS queue between the TX and RX stub. Each
154 // element in a queue is a list where the first element is an invocation
155 // handle and the second element is a PMT u8 vect representation of the
156 // CS packet response which can just be passed transparently.
157 while(!d_cs_queue.empty()) {
159 pmt_t cs_pkt = d_cs_queue.front();
162 pmt_t invocation_handle = pmt_nth(0, cs_pkt);
163 pmt_t v_pkt = pmt_nth(1, cs_pkt);
165 d_cs->send(s_response_usrp_rx_read,
166 pmt_list3(invocation_handle,
168 v_pkt)); // Take the front CS pkt
172 std::cout << "[USRP_RX_STUB] Received CS response from TX stub\n";
177 usrp_rx_stop = false;
180 std::cout << "[USRP_RX_STUB] Got fake RX stop\n";
184 REGISTER_MBLOCK_CLASS(usrp_rx_stub);