Statistics
| Branch: | Tag: | Revision:

root / usrp / host / lib / inband / usrp_tx_stub.cc @ b644e266

History | View | Annotate | Download (10.5 kB)

1
/* -*- c++ -*- */
2
/*
3
 * Copyright 2007 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 <iostream>
27
#include <vector>
28
#include <usb.h>
29
#include <mb_class_registry.h>
30
#include <usrp_tx_stub.h>
31
#include <usrp_inband_usb_packet.h>
32
#include <fpga_regs_common.h>
33
#include "usrp_standard.h"
34
#include <stdio.h>
35
#include <fstream>
36
#include <usrp_rx_stub.h>
37
38
#include <symbols_usrp_tx_cs.h>
39
40
typedef usrp_inband_usb_packet transport_pkt;
41
42
static const bool verbose = false;
43
44
usrp_tx_stub::usrp_tx_stub(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg)
45
  : mb_mblock(rt, instance_name, user_arg),
46
    d_disk_write(false)
47
{
48
  d_cs = define_port("cs", "usrp-tx-cs", true, mb_port::EXTERNAL);
49
  
50
  //d_disk_write=true;
51
  
52
  if(d_disk_write) {
53
    d_ofile.open("tx_stub_data.dat",std::ios::binary|std::ios::out);
54
    d_cs_ofile.open("tx_stub_cs.dat",std::ios::binary|std::ios::out);
55
  }
56
}
57
58
usrp_tx_stub::~usrp_tx_stub() 
59
{
60
  if(d_disk_write) {
61
    d_ofile.close();
62
    d_cs_ofile.close();
63
  }
64
}
65
66
void 
67
usrp_tx_stub::initial_transition()
68
{
69
  
70
}
71
72
void
73
usrp_tx_stub::handle_message(mb_message_sptr msg)
74
{
75
  pmt_t event = msg->signal();
76
  pmt_t port_id = msg->port_id();
77
  pmt_t data = msg->data(); 
78
79
  // Theoretically only have 1 message to ever expect, but
80
  // want to make sure its at least what we want
81
  if(pmt_eq(port_id, d_cs->port_symbol())) {
82
    
83
    if(pmt_eqv(event, s_cmd_usrp_tx_write)) 
84
      write(data);
85
  }
86
}
87
88
void
89
usrp_tx_stub::write(pmt_t data)
90
{
91
  pmt_t invocation_handle = pmt_nth(0, data);
92
  pmt_t channel = pmt_nth(1, data);
93
  pmt_t v_packets = pmt_nth(2, data);
94
  d_utx = boost::any_cast<usrp_standard_tx *>(pmt_any_ref(pmt_nth(3, data)));
95
96
  size_t n_bytes;
97
98
  transport_pkt *pkts = (transport_pkt *) pmt_u8vector_writeable_elements(v_packets, n_bytes);
99
  long n_packets = static_cast<long>(std::ceil(n_bytes / (double)transport_pkt::max_pkt_size()));
100
  
101
  // Parse the packets looking for C/S packets and dump them to a disk if
102
  // necessary
103
  for(long i=0; i<n_packets; i++) {
104
105
    if(d_disk_write) {
106
      if(pkts[i].chan() == 0x1f)
107
        d_cs_ofile.write((const char *)&pkts[i], transport_pkt::max_pkt_size());
108
      else
109
        d_ofile.write((const char *)&pkts[i], transport_pkt::max_pkt_size());
110
111
      d_cs_ofile.flush();
112
      d_ofile.flush();
113
    }
114
115
    if(pkts[i].chan() == 0x1f)
116
      parse_cs(invocation_handle, pkts[i]);
117
  }
118
119
  d_cs->send(s_response_usrp_tx_write,
120
       pmt_list3(invocation_handle, PMT_T, channel));
121
122
  return;
123
}
124
125
void
126
usrp_tx_stub::parse_cs(pmt_t invocation_handle, transport_pkt pkt)
127
{
128
  
129
  long payload_len = pkt.payload_len();
130
  long curr_payload = 0;
131
      
132
  size_t ignore;
133
134
  // There is the possibility that the responses for a single USB packet full of
135
  // CS packets will not fit back in a single USB packet, considering some
136
  // responses are greater than their commands (read registers).
137
 new_packet:
138
  pmt_t v_pkt = pmt_make_u8vector(sizeof(transport_pkt), 0);
139
  
140
  transport_pkt *q_pkt =
141
    (transport_pkt *) pmt_u8vector_writeable_elements(v_pkt, ignore);
142
      
143
  q_pkt->set_header(0, 0x1f, 0, 0);
144
  q_pkt->set_timestamp(0xffffffff);
145
146
  // We dispatch based on the control packet type, however we can extract the
147
  // opcode and the length immediately which is consistent in all responses.
148
  //
149
  // Since each control packet can have multiple responses, we keep reading the
150
  // lengths of each subpacket until we reach the payload length.  
151
  while(curr_payload < payload_len) {
152
153
    pmt_t sub_packet = pkt.read_subpacket(curr_payload);
154
    pmt_t op_symbol = pmt_nth(0, sub_packet);
155
156
    int len = pkt.cs_len(curr_payload);
157
      
158
    if(verbose)
159
      std::cout << "[USRP_TX_STUB] Parsing subpacket " 
160
                << op_symbol << " ... length " << len << std::endl;
161
162
    //----------------- PING FIXED ------------------//
163
    if(pmt_eq(op_symbol, s_op_ping_fixed)) {
164
165
      long rid = pmt_to_long(pmt_nth(1, sub_packet));
166
      long pingval = pmt_to_long(pmt_nth(2, sub_packet));
167
168
      // Generate a reply and put it in the queue for the RX stub to read
169
      if(!q_pkt->cs_ping_reply(rid, pingval))
170
        goto new_packet;
171
172
      if(verbose)
173
        std::cout << "[USRP_TX_STUB] Generated ping response "
174
                  << "("
175
                  << "RID: " << rid << ", "
176
                  << "VAL: " << pingval 
177
                  << ")\n";
178
    }
179
180
    //----------------- READ REG ------------------//
181
    if(pmt_eq(op_symbol, s_op_read_reg)) {
182
183
      long rid = pmt_to_long(pmt_nth(1, sub_packet));
184
      long reg_num = pmt_to_long(pmt_nth(2, sub_packet));
185
      long reg_val = 0xdeef;
186
187
      // Generate a reply and put it in the queue for the RX stub to read
188
      if(!q_pkt->cs_read_reg_reply(rid, reg_num, reg_val))
189
        goto new_packet;
190
191
      if(verbose)
192
        std::cout << "[USRP_TX_STUB] Generated read register response "
193
                  << "("
194
                  << "RID: " << rid << ", "
195
                  << "REG: " << reg_num << ", "
196
                  << "VAL: " << reg_val
197
                  << ")\n";
198
    }
199
    
200
    //----------------- DELAY ------------------//
201
    if(pmt_eq(op_symbol, s_op_delay)) {
202
203
      long ticks = pmt_to_long(pmt_nth(1, sub_packet));
204
205
      if(verbose)
206
        std::cout << "[USRP_TX_STUB] Received delay command "
207
                  << "("
208
                  << "Ticks: " << ticks
209
                  << ")\n";
210
    }
211
212
    //----------------- WRITE REG ------------------//
213
    if(pmt_eq(op_symbol, s_op_write_reg)) {
214
215
      pmt_t reg_num = pmt_nth(1, sub_packet);
216
      pmt_t reg_val = pmt_nth(2, sub_packet);
217
218
      if(verbose)
219
        std::cout << "[USRP_TX_STUB] Received write register command "
220
                  << "("
221
                  << "RegNum: " << reg_num << ", "
222
                  << "Val: " << reg_val
223
                  << ")\n";
224
    }
225
    
226
    //----------------- WRITE REG MASK ---------------//
227
    if(pmt_eq(op_symbol, s_op_write_reg_masked)) {
228
229
      pmt_t reg_num = pmt_nth(1, sub_packet);
230
      pmt_t reg_val = pmt_nth(2, sub_packet);
231
      pmt_t mask    = pmt_nth(3, sub_packet);
232
233
      if(verbose)
234
        std::cout << "[USRP_TX_STUB] Received write register command "
235
                  << "("
236
                  << "RegNum: " << reg_num << ", "
237
                  << "Val: " << reg_val << ", "
238
                  << "Mask: " << mask
239
                  << ")\n";
240
    }
241
242
    //---------------- I2C WRITE ------------------//
243
    if(pmt_eq(op_symbol, s_op_i2c_write)) {
244
      pmt_t i2c_addr  = pmt_nth(1, sub_packet);
245
      pmt_t i2c_data  = pmt_nth(2, sub_packet);
246
247
      if(verbose)
248
        std::cout << "[USRP_TX_STUB] Received i2c write command "
249
                  << "("
250
                  << "Addr: " << i2c_addr << ", "
251
                  << "Data: " << i2c_data
252
                  << ")\n";
253
    }
254
255
    //---------------- I2C READ ------------------//
256
    if(pmt_eq(op_symbol, s_op_i2c_read)) {
257
      long rid       = pmt_to_long(pmt_nth(1, sub_packet));
258
      long i2c_addr  = pmt_to_long(pmt_nth(2, sub_packet));
259
      long i2c_bytes = pmt_to_long(pmt_nth(3, sub_packet));
260
261
      // Create data to place as a response, filled with 0xff
262
      size_t ignore;
263
      pmt_t i2c_data = pmt_make_u8vector(i2c_bytes, 0xff);
264
      uint8_t *w_data = (uint8_t *) pmt_u8vector_writeable_elements(i2c_data, ignore);
265
266
      // Generate a reply and put it in the queue for the RX stub to read
267
      if(!q_pkt->cs_i2c_read_reply(rid, i2c_addr, w_data, i2c_bytes))
268
        goto new_packet;
269
270
      if(verbose)
271
        std::cout << "[USRP_TX_STUB] Received i2c read "
272
                  << "("
273
                  << "RID: " << rid << ", "
274
                  << "Addr: " << i2c_addr << ", "
275
                  << "Bytes: " << i2c_bytes
276
                  << ")\n";
277
    }
278
    
279
    //---------------- SPI WRITE ------------------//
280
    if(pmt_eq(op_symbol, s_op_spi_write)) {
281
      long enables  = pmt_to_long(pmt_nth(1, sub_packet));
282
      long format   = pmt_to_long(pmt_nth(2, sub_packet));
283
      long opt      = pmt_to_long(pmt_nth(3, sub_packet));
284
      pmt_t data    = pmt_nth(4, sub_packet);
285
286
      if(verbose)
287
        std::cout << "[USRP_TX_STUB] Received spi write command "
288
                  << "("
289
                  << "Enables: " << enables << ", "
290
                  << "Format: " << format << ", "
291
                  << "Options: " << opt << ", "
292
                  << "Data: " << data
293
                  << ")\n";
294
    }
295
296
    //---------------- SPI READ ------------------//
297
    if(pmt_eq(op_symbol, s_op_spi_read)) {
298
      long rid      = pmt_to_long(pmt_nth(1, sub_packet));
299
      long enables  = pmt_to_long(pmt_nth(2, sub_packet));
300
      long format   = pmt_to_long(pmt_nth(3, sub_packet));
301
      long opt      = pmt_to_long(pmt_nth(4, sub_packet));
302
      long n_bytes  = pmt_to_long(pmt_nth(5, sub_packet));
303
304
      // Create data to place as a fake response
305
      size_t ignore;
306
      pmt_t spi_data = pmt_make_u8vector(n_bytes, 0xff);
307
      uint8_t *w_data = (uint8_t *) pmt_u8vector_writeable_elements(spi_data, ignore);
308
309
      // Generate a reply and put it in the queue for the RX stub to read
310
      if(!q_pkt->cs_spi_read_reply(rid, w_data, n_bytes))
311
        goto new_packet;
312
313
      if(verbose)
314
        std::cout << "[USRP_TX_STUB] Received spi read command "
315
                  << "("
316
                  << "RID: " << rid << ", "
317
                  << "Enables: " << enables << ", "
318
                  << "Format: " << format << ", "
319
                  << "Options: " << opt << ", "
320
                  << "Bytes: " << n_bytes
321
                  << ")\n";
322
      
323
    }
324
325
    // Each subpacket has an unaccounted for 2 bytes which is the opcode
326
    // and the length field
327
    curr_payload += len + 2;
328
329
    // All subpackets are 32-bit aligned
330
    int align_offset = 4 - (curr_payload % 4);
331
332
    if(align_offset != 4)
333
      curr_payload += align_offset;
334
335
  }
336
337
  // If the packet has data in the payload, it needs queued
338
  if(q_pkt->payload_len() > 0)
339
    d_cs_queue.push(pmt_list2(invocation_handle, v_pkt));
340
341
  return;
342
}
343
344
REGISTER_MBLOCK_CLASS(usrp_tx_stub);