1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/* -*- c++ -*- */
/*
* Copyright 2007 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <usrp_rx_stub.h>
#include <iostream>
#include <vector>
#include <usb.h>
#include <mb_class_registry.h>
#include <usrp_inband_usb_packet.h>
#include <fpga_regs_common.h>
#include "usrp_standard.h"
#include <stdio.h>
#include <string.h>
#include <ui_nco.h>
#include <fstream>
#include <symbols_usrp_rx_cs.h>
typedef usrp_inband_usb_packet transport_pkt;
static const bool verbose = false;
bool usrp_rx_stop;
// Used for the fake control packet response code to send the responses back up
// the RX. The TX stub dumps responses in to this queue.
std::queue<pmt_t> d_cs_queue;
usrp_rx_stub::usrp_rx_stub(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg)
: mb_mblock(rt, instance_name, user_arg),
d_samples_per_frame((long)(126)),
d_amplitude(16384),
d_disk_write(false)
{
d_cs = define_port("cs", "usrp-rx-cs", true, mb_port::EXTERNAL);
// initialize NCO
double freq = 100e3;
int interp = 32; // 32 -> 4MS/s
double sample_rate = 128e6 / interp;
d_nco.set_freq(2*M_PI * freq/sample_rate);
//d_disk_write = true;
if(d_disk_write)
d_ofile.open("raw_rx.dat",std::ios::binary|std::ios::out);
usrp_rx_stop = false;
}
usrp_rx_stub::~usrp_rx_stub()
{
if(d_disk_write)
d_ofile.close();
}
void
usrp_rx_stub::initial_transition()
{
}
void
usrp_rx_stub::handle_message(mb_message_sptr msg)
{
pmt_t event = msg->signal();
pmt_t port_id = msg->port_id();
pmt_t data = msg->data();
// Theoretically only have 1 message to ever expect, but
// want to make sure its at least what we want
if(pmt_eq(port_id, d_cs->port_symbol())) {
if(verbose)
std::cout << "[USRP_RX_STUB] Starting...\n";
if(pmt_eqv(event, s_cmd_usrp_rx_start_reading))
read_and_respond(data);
}
}
void
usrp_rx_stub::read_and_respond(pmt_t data)
{
while(!usrp_rx_stop) {
long nsamples_this_frame = d_samples_per_frame;
size_t nshorts = 2 * nsamples_this_frame; // 16-bit I & Q
long channel = 0;
long n_bytes = nshorts*2;
pmt_t uvec = pmt_make_s16vector(nshorts, 0);
size_t ignore;
int16_t *samples = pmt_s16vector_writeable_elements(uvec, ignore);
// fill in the complex sinusoid
for (int i = 0; i < nsamples_this_frame; i++){
if (1){
gr_complex s;
d_nco.sincos(&s, 1, d_amplitude);
// write 16-bit i & q
samples[2*i] = (int16_t) s.real();
samples[2*i+1] = (int16_t) s.imag();
}
else {
gr_complex s(d_amplitude, d_amplitude);
// write 16-bit i & q
samples[2*i] = (int16_t) s.real();
samples[2*i+1] = (int16_t) s.imag();
}
}
if(d_disk_write)
d_ofile.write((const char *)samples, n_bytes);
pmt_t v_pkt = pmt_make_u8vector(sizeof(transport_pkt), 0);
transport_pkt *pkt =
(transport_pkt *) pmt_u8vector_writeable_elements(v_pkt, ignore);
pkt->set_header(0, channel, 0, n_bytes);
pkt->set_timestamp(0xffffffff);
memcpy(pkt->payload(), samples, n_bytes);
d_cs->send(s_response_usrp_rx_read, pmt_list3(PMT_NIL, PMT_T, v_pkt));
// Now lets check the shared CS queue between the TX and RX stub. Each
// element in a queue is a list where the first element is an invocation
// handle and the second element is a PMT u8 vect representation of the
// CS packet response which can just be passed transparently.
while(!d_cs_queue.empty()) {
pmt_t cs_pkt = d_cs_queue.front();
d_cs_queue.pop();
pmt_t invocation_handle = pmt_nth(0, cs_pkt);
pmt_t v_pkt = pmt_nth(1, cs_pkt);
d_cs->send(s_response_usrp_rx_read,
pmt_list3(invocation_handle,
PMT_T,
v_pkt)); // Take the front CS pkt
if(verbose)
std::cout << "[USRP_RX_STUB] Received CS response from TX stub\n";
}
}
usrp_rx_stop = false;
if(verbose)
std::cout << "[USRP_RX_STUB] Got fake RX stop\n";
}
REGISTER_MBLOCK_CLASS(usrp_rx_stub);
|