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.
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"
36 #include <usrp_rx_stub.h>
38 #include <symbols_usrp_tx_cs.h>
40 typedef usrp_inband_usb_packet transport_pkt;
42 static const bool verbose = false;
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),
48 d_cs = define_port("cs", "usrp-tx-cs", true, mb_port::EXTERNAL);
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);
58 usrp_tx_stub::~usrp_tx_stub()
67 usrp_tx_stub::initial_transition()
73 usrp_tx_stub::handle_message(mb_message_sptr msg)
75 pmt_t event = msg->signal();
76 pmt_t port_id = msg->port_id();
77 pmt_t data = msg->data();
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())) {
83 if(pmt_eqv(event, s_cmd_usrp_tx_write))
89 usrp_tx_stub::write(pmt_t data)
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)));
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()));
101 // Parse the packets looking for C/S packets and dump them to a disk if
103 for(long i=0; i<n_packets; i++) {
106 if(pkts[i].chan() == CONTROL_CHAN)
107 d_cs_ofile.write((const char *)&pkts[i], transport_pkt::max_pkt_size());
109 d_ofile.write((const char *)&pkts[i], transport_pkt::max_pkt_size());
115 if(pkts[i].chan() == CONTROL_CHAN)
116 parse_cs(invocation_handle, pkts[i]);
119 d_cs->send(s_response_usrp_tx_write,
120 pmt_list3(invocation_handle, PMT_T, channel));
126 usrp_tx_stub::parse_cs(pmt_t invocation_handle, transport_pkt pkt)
129 long payload_len = pkt.payload_len();
130 long curr_payload = 0;
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).
138 pmt_t v_pkt = pmt_make_u8vector(sizeof(transport_pkt), 0);
140 transport_pkt *q_pkt =
141 (transport_pkt *) pmt_u8vector_writeable_elements(v_pkt, ignore);
143 q_pkt->set_header(0, CONTROL_CHAN, 0, 0);
144 q_pkt->set_timestamp(0xffffffff);
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.
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) {
153 pmt_t sub_packet = pkt.read_subpacket(curr_payload);
154 pmt_t op_symbol = pmt_nth(0, sub_packet);
156 int len = pkt.cs_len(curr_payload);
159 std::cout << "[USRP_TX_STUB] Parsing subpacket "
160 << op_symbol << " ... length " << len << std::endl;
162 //----------------- PING FIXED ------------------//
163 if(pmt_eq(op_symbol, s_op_ping_fixed)) {
165 long rid = pmt_to_long(pmt_nth(1, sub_packet));
166 long pingval = pmt_to_long(pmt_nth(2, sub_packet));
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))
173 std::cout << "[USRP_TX_STUB] Generated ping response "
175 << "RID: " << rid << ", "
176 << "VAL: " << pingval
180 //----------------- READ REG ------------------//
181 if(pmt_eq(op_symbol, s_op_read_reg)) {
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;
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))
192 std::cout << "[USRP_TX_STUB] Generated read register response "
194 << "RID: " << rid << ", "
195 << "REG: " << reg_num << ", "
196 << "VAL: " << reg_val
200 //----------------- DELAY ------------------//
201 if(pmt_eq(op_symbol, s_op_delay)) {
203 long ticks = pmt_to_long(pmt_nth(1, sub_packet));
206 std::cout << "[USRP_TX_STUB] Received delay command "
208 << "Ticks: " << ticks
212 //----------------- WRITE REG ------------------//
213 if(pmt_eq(op_symbol, s_op_write_reg)) {
215 pmt_t reg_num = pmt_nth(1, sub_packet);
216 pmt_t reg_val = pmt_nth(2, sub_packet);
219 std::cout << "[USRP_TX_STUB] Received write register command "
221 << "RegNum: " << reg_num << ", "
222 << "Val: " << reg_val
226 //----------------- WRITE REG MASK ---------------//
227 if(pmt_eq(op_symbol, s_op_write_reg_masked)) {
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);
234 std::cout << "[USRP_TX_STUB] Received write register command "
236 << "RegNum: " << reg_num << ", "
237 << "Val: " << reg_val << ", "
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);
248 std::cout << "[USRP_TX_STUB] Received i2c write command "
250 << "Addr: " << i2c_addr << ", "
251 << "Data: " << i2c_data
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));
261 // Create data to place as a response, filled with 0xff
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);
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))
271 std::cout << "[USRP_TX_STUB] Received i2c read "
273 << "RID: " << rid << ", "
274 << "Addr: " << i2c_addr << ", "
275 << "Bytes: " << i2c_bytes
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);
287 std::cout << "[USRP_TX_STUB] Received spi write command "
289 << "Enables: " << enables << ", "
290 << "Format: " << format << ", "
291 << "Options: " << opt << ", "
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));
304 // Create data to place as a fake response
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);
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))
314 std::cout << "[USRP_TX_STUB] Received spi read command "
316 << "RID: " << rid << ", "
317 << "Enables: " << enables << ", "
318 << "Format: " << format << ", "
319 << "Options: " << opt << ", "
320 << "Bytes: " << n_bytes
325 // Each subpacket has an unaccounted for 2 bytes which is the opcode
326 // and the length field
327 curr_payload += len + 2;
329 // All subpackets are 32-bit aligned
330 int align_offset = 4 - (curr_payload % 4);
332 if(align_offset != 4)
333 curr_payload += align_offset;
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));
344 REGISTER_MBLOCK_CLASS(usrp_tx_stub);