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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/* -*- 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 <iostream>
#include <vector>
#include <usb.h>
#include <mb_class_registry.h>
#include <usrp_tx_stub.h>
#include <usrp_inband_usb_packet.h>
#include <fpga_regs_common.h>
#include "usrp_standard.h"
#include <stdio.h>
#include <fstream>
#include <usrp_rx_stub.h>
#include <symbols_usrp_tx_cs.h>
typedef usrp_inband_usb_packet transport_pkt;
static const bool verbose = false;
usrp_tx_stub::usrp_tx_stub(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg)
: mb_mblock(rt, instance_name, user_arg),
d_disk_write(false)
{
d_cs = define_port("cs", "usrp-tx-cs", true, mb_port::EXTERNAL);
//d_disk_write=true;
if(d_disk_write) {
d_ofile.open("tx_stub_data.dat",std::ios::binary|std::ios::out);
d_cs_ofile.open("tx_stub_cs.dat",std::ios::binary|std::ios::out);
}
}
usrp_tx_stub::~usrp_tx_stub()
{
if(d_disk_write) {
d_ofile.close();
d_cs_ofile.close();
}
}
void
usrp_tx_stub::initial_transition()
{
}
void
usrp_tx_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(pmt_eqv(event, s_cmd_usrp_tx_write))
write(data);
}
}
void
usrp_tx_stub::write(pmt_t data)
{
pmt_t invocation_handle = pmt_nth(0, data);
pmt_t channel = pmt_nth(1, data);
pmt_t v_packets = pmt_nth(2, data);
d_utx = boost::any_cast<usrp_standard_tx *>(pmt_any_ref(pmt_nth(3, data)));
size_t n_bytes;
transport_pkt *pkts = (transport_pkt *) pmt_u8vector_writeable_elements(v_packets, n_bytes);
long n_packets = static_cast<long>(std::ceil(n_bytes / (double)transport_pkt::max_pkt_size()));
// Parse the packets looking for C/S packets and dump them to a disk if
// necessary
for(long i=0; i<n_packets; i++) {
if(d_disk_write) {
if(pkts[i].chan() == CONTROL_CHAN)
d_cs_ofile.write((const char *)&pkts[i], transport_pkt::max_pkt_size());
else
d_ofile.write((const char *)&pkts[i], transport_pkt::max_pkt_size());
d_cs_ofile.flush();
d_ofile.flush();
}
if(pkts[i].chan() == CONTROL_CHAN)
parse_cs(invocation_handle, pkts[i]);
}
d_cs->send(s_response_usrp_tx_write,
pmt_list3(invocation_handle, PMT_T, channel));
return;
}
void
usrp_tx_stub::parse_cs(pmt_t invocation_handle, transport_pkt pkt)
{
long payload_len = pkt.payload_len();
long curr_payload = 0;
size_t ignore;
// There is the possibility that the responses for a single USB packet full of
// CS packets will not fit back in a single USB packet, considering some
// responses are greater than their commands (read registers).
new_packet:
pmt_t v_pkt = pmt_make_u8vector(sizeof(transport_pkt), 0);
transport_pkt *q_pkt =
(transport_pkt *) pmt_u8vector_writeable_elements(v_pkt, ignore);
q_pkt->set_header(0, CONTROL_CHAN, 0, 0);
q_pkt->set_timestamp(0xffffffff);
// We dispatch based on the control packet type, however we can extract the
// opcode and the length immediately which is consistent in all responses.
//
// Since each control packet can have multiple responses, we keep reading the
// lengths of each subpacket until we reach the payload length.
while(curr_payload < payload_len) {
pmt_t sub_packet = pkt.read_subpacket(curr_payload);
pmt_t op_symbol = pmt_nth(0, sub_packet);
int len = pkt.cs_len(curr_payload);
if(verbose)
std::cout << "[USRP_TX_STUB] Parsing subpacket "
<< op_symbol << " ... length " << len << std::endl;
//----------------- PING FIXED ------------------//
if(pmt_eq(op_symbol, s_op_ping_fixed)) {
long rid = pmt_to_long(pmt_nth(1, sub_packet));
long pingval = pmt_to_long(pmt_nth(2, sub_packet));
// Generate a reply and put it in the queue for the RX stub to read
if(!q_pkt->cs_ping_reply(rid, pingval))
goto new_packet;
if(verbose)
std::cout << "[USRP_TX_STUB] Generated ping response "
<< "("
<< "RID: " << rid << ", "
<< "VAL: " << pingval
<< ")\n";
}
//----------------- READ REG ------------------//
if(pmt_eq(op_symbol, s_op_read_reg)) {
long rid = pmt_to_long(pmt_nth(1, sub_packet));
long reg_num = pmt_to_long(pmt_nth(2, sub_packet));
long reg_val = 0xdeef;
// Generate a reply and put it in the queue for the RX stub to read
if(!q_pkt->cs_read_reg_reply(rid, reg_num, reg_val))
goto new_packet;
if(verbose)
std::cout << "[USRP_TX_STUB] Generated read register response "
<< "("
<< "RID: " << rid << ", "
<< "REG: " << reg_num << ", "
<< "VAL: " << reg_val
<< ")\n";
}
//----------------- DELAY ------------------//
if(pmt_eq(op_symbol, s_op_delay)) {
long ticks = pmt_to_long(pmt_nth(1, sub_packet));
if(verbose)
std::cout << "[USRP_TX_STUB] Received delay command "
<< "("
<< "Ticks: " << ticks
<< ")\n";
}
//----------------- WRITE REG ------------------//
if(pmt_eq(op_symbol, s_op_write_reg)) {
pmt_t reg_num = pmt_nth(1, sub_packet);
pmt_t reg_val = pmt_nth(2, sub_packet);
if(verbose)
std::cout << "[USRP_TX_STUB] Received write register command "
<< "("
<< "RegNum: " << reg_num << ", "
<< "Val: " << reg_val
<< ")\n";
}
//----------------- WRITE REG MASK ---------------//
if(pmt_eq(op_symbol, s_op_write_reg_masked)) {
pmt_t reg_num = pmt_nth(1, sub_packet);
pmt_t reg_val = pmt_nth(2, sub_packet);
pmt_t mask = pmt_nth(3, sub_packet);
if(verbose)
std::cout << "[USRP_TX_STUB] Received write register command "
<< "("
<< "RegNum: " << reg_num << ", "
<< "Val: " << reg_val << ", "
<< "Mask: " << mask
<< ")\n";
}
//---------------- I2C WRITE ------------------//
if(pmt_eq(op_symbol, s_op_i2c_write)) {
pmt_t i2c_addr = pmt_nth(1, sub_packet);
pmt_t i2c_data = pmt_nth(2, sub_packet);
if(verbose)
std::cout << "[USRP_TX_STUB] Received i2c write command "
<< "("
<< "Addr: " << i2c_addr << ", "
<< "Data: " << i2c_data
<< ")\n";
}
//---------------- I2C READ ------------------//
if(pmt_eq(op_symbol, s_op_i2c_read)) {
long rid = pmt_to_long(pmt_nth(1, sub_packet));
long i2c_addr = pmt_to_long(pmt_nth(2, sub_packet));
long i2c_bytes = pmt_to_long(pmt_nth(3, sub_packet));
// Create data to place as a response, filled with 0xff
size_t ignore;
pmt_t i2c_data = pmt_make_u8vector(i2c_bytes, 0xff);
uint8_t *w_data = (uint8_t *) pmt_u8vector_writeable_elements(i2c_data, ignore);
// Generate a reply and put it in the queue for the RX stub to read
if(!q_pkt->cs_i2c_read_reply(rid, i2c_addr, w_data, i2c_bytes))
goto new_packet;
if(verbose)
std::cout << "[USRP_TX_STUB] Received i2c read "
<< "("
<< "RID: " << rid << ", "
<< "Addr: " << i2c_addr << ", "
<< "Bytes: " << i2c_bytes
<< ")\n";
}
//---------------- SPI WRITE ------------------//
if(pmt_eq(op_symbol, s_op_spi_write)) {
long enables = pmt_to_long(pmt_nth(1, sub_packet));
long format = pmt_to_long(pmt_nth(2, sub_packet));
long opt = pmt_to_long(pmt_nth(3, sub_packet));
pmt_t data = pmt_nth(4, sub_packet);
if(verbose)
std::cout << "[USRP_TX_STUB] Received spi write command "
<< "("
<< "Enables: " << enables << ", "
<< "Format: " << format << ", "
<< "Options: " << opt << ", "
<< "Data: " << data
<< ")\n";
}
//---------------- SPI READ ------------------//
if(pmt_eq(op_symbol, s_op_spi_read)) {
long rid = pmt_to_long(pmt_nth(1, sub_packet));
long enables = pmt_to_long(pmt_nth(2, sub_packet));
long format = pmt_to_long(pmt_nth(3, sub_packet));
long opt = pmt_to_long(pmt_nth(4, sub_packet));
long n_bytes = pmt_to_long(pmt_nth(5, sub_packet));
// Create data to place as a fake response
size_t ignore;
pmt_t spi_data = pmt_make_u8vector(n_bytes, 0xff);
uint8_t *w_data = (uint8_t *) pmt_u8vector_writeable_elements(spi_data, ignore);
// Generate a reply and put it in the queue for the RX stub to read
if(!q_pkt->cs_spi_read_reply(rid, w_data, n_bytes))
goto new_packet;
if(verbose)
std::cout << "[USRP_TX_STUB] Received spi read command "
<< "("
<< "RID: " << rid << ", "
<< "Enables: " << enables << ", "
<< "Format: " << format << ", "
<< "Options: " << opt << ", "
<< "Bytes: " << n_bytes
<< ")\n";
}
// Each subpacket has an unaccounted for 2 bytes which is the opcode
// and the length field
curr_payload += len + 2;
// All subpackets are 32-bit aligned
int align_offset = 4 - (curr_payload % 4);
if(align_offset != 4)
curr_payload += align_offset;
}
// If the packet has data in the payload, it needs queued
if(q_pkt->payload_len() > 0)
d_cs_queue.push(pmt_list2(invocation_handle, v_pkt));
return;
}
REGISTER_MBLOCK_CLASS(usrp_tx_stub);
|