3 * Copyright 2008 Free Software Foundation, Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <usrp2/usrp2.h>
24 #include <usrp2/tune_result.h>
25 #include <usrp2/copiers.h>
26 #include <gruel/inet.h>
27 #include <usrp2_types.h>
28 #include "usrp2_impl.h"
29 #include "usrp2_thread.h"
30 #include "eth_buffer.h"
31 #include "pktfilter.h"
41 #define USRP2_IMPL_DEBUG 0
43 #define DEBUG_LOG(x) ::write(2, x, 1)
48 static const int DEFAULT_RX_SCALE = 1024;
49 static const int DEFAULT_TX_SCALE = 3000;
53 static const double DEF_CMD_TIMEOUT = 0.1;
56 opcode_to_string(int opcode)
59 case OP_EOP: return "OP_EOP";
60 case OP_ID: return "OP_ID";
61 case OP_ID_REPLY: return "OP_ID_REPLY";
62 case OP_BURN_MAC_ADDR: return "OP_BURN_MAC_ADDR";
63 case OP_READ_TIME: return "OP_READ_TIME";
64 case OP_READ_TIME_REPLY: return "OP_READ_TIME_REPLY";
65 case OP_CONFIG_RX_V2: return "OP_CONFIG_RX_V2";
66 case OP_CONFIG_RX_REPLY_V2: return "OP_CONFIG_RX_REPLY_V2";
67 case OP_CONFIG_TX_V2: return "OP_CONFIG_TX_V2";
68 case OP_CONFIG_TX_REPLY_V2: return "OP_CONFIG_TX_REPLY_V2";
69 case OP_START_RX_STREAMING: return "OP_START_RX_STREAMING";
70 case OP_STOP_RX: return "OP_STOP_RX";
71 case OP_CONFIG_MIMO: return "OP_CONFIG_MIMO";
72 case OP_DBOARD_INFO: return "OP_DBOARD_INFO";
73 case OP_DBOARD_INFO_REPLY: return "OP_DBOARD_INFO_REPLY";
74 case OP_SYNC_TO_PPS: return "OP_SYNC_TO_PPS";
75 case OP_PEEK: return "OP_PEEK";
76 case OP_PEEK_REPLY: return "OP_PEEK_REPLY";
80 snprintf(buf, sizeof(buf), "<unknown opcode: %d>", opcode);
87 * \param p points to fixed header
88 * \param payload_len_in_bytes is length of the fixed hdr and the payload
89 * \param[out] items is set to point to the first uint32 item in the payload
90 * \param[out] nitems is set to the number of uint32 items in the payload
91 * \param[out] md is filled in with the parsed metadata from the frame.
94 parse_rx_metadata(void *p, size_t payload_len_in_bytes,
95 uint32_t **items, size_t *nitems_in_uint32s, rx_metadata *md)
97 if (payload_len_in_bytes < sizeof(u2_fixed_hdr_t)) // invalid format
100 // FIXME deal with the fact that (p % 4) == 2
101 //assert((((uintptr_t) p) % 4) == 0); // must be 4-byte aligned
103 u2_fixed_hdr_t *fh = static_cast<u2_fixed_hdr_t *>(p);
105 // FIXME unaligned loads!
106 md->word0 = u2p_word0(fh);
107 md->timestamp = u2p_timestamp(fh);
109 // FIXME when we've got more info
110 // md->start_of_burst = (md->word0 & XXX) != 0;
111 // md->end_of_burst = (md->word0 & XXX) != 0;
112 // md->rx_overrun = (md->word0 & XXX) != 0;
113 md->start_of_burst = 0;
114 md->end_of_burst = 0;
117 *items = (uint32_t *)(&fh[1]);
118 size_t nbytes = payload_len_in_bytes - sizeof(u2_fixed_hdr_t);
119 assert((nbytes % sizeof(uint32_t)) == 0);
120 *nitems_in_uint32s = nbytes / sizeof(uint32_t);
126 usrp2::impl::impl(const std::string &ifc, props *p)
127 : d_eth_buf(new eth_buffer()), d_pf(0), d_bg_thread(0), d_bg_running(false),
128 d_rx_seqno(-1), d_tx_seqno(0), d_next_rid(0),
129 d_num_rx_frames(0), d_num_rx_missing(0), d_num_rx_overruns(0), d_num_rx_bytes(0),
130 d_num_enqueued(0), d_enqueued_mutex(), d_bg_pending_cond(&d_enqueued_mutex),
131 d_channel_rings(NCHANS), d_tx_interp(0), d_rx_decim(0)
133 if (!d_eth_buf->open(ifc, htons(U2_ETHERTYPE)))
134 throw std::runtime_error("Unable to register USRP2 protocol");
136 d_pf = pktfilter::make_ethertype_inbound(U2_ETHERTYPE, d_eth_buf->mac());
137 if (!d_pf || !d_eth_buf->attach_pktfilter(d_pf))
138 throw std::runtime_error("Unable to attach packet filter.");
142 if (USRP2_IMPL_DEBUG)
143 std::cerr << "usrp2 constructor: using USRP2 at " << d_addr << std::endl;
145 memset(d_pending_replies, 0, sizeof(d_pending_replies));
147 d_bg_thread = new usrp2_thread(this);
148 d_bg_thread->start();
150 if (!dboard_info()) // we're hosed
151 throw std::runtime_error("Unable to retrieve daughterboard info");
156 tx_daughterboard_id(&dbid);
157 fprintf(stderr, "Tx dboard 0x%x\n", dbid);
158 fprintf(stderr, " freq_min = %g\n", tx_freq_min());
159 fprintf(stderr, " freq_max = %g\n", tx_freq_max());
160 fprintf(stderr, " gain_min = %g\n", tx_gain_min());
161 fprintf(stderr, " gain_max = %g\n", tx_gain_max());
162 fprintf(stderr, " gain_db_per_step = %g\n", tx_gain_db_per_step());
164 rx_daughterboard_id(&dbid);
165 fprintf(stderr, "Rx dboard 0x%x\n", dbid);
166 fprintf(stderr, " freq_min = %g\n", rx_freq_min());
167 fprintf(stderr, " freq_max = %g\n", rx_freq_max());
168 fprintf(stderr, " gain_min = %g\n", rx_gain_min());
169 fprintf(stderr, " gain_max = %g\n", rx_gain_max());
170 fprintf(stderr, " gain_db_per_step = %g\n", rx_gain_db_per_step());
173 // default gains to mid point
174 if (!set_tx_gain((tx_gain_min() + tx_gain_max()) / 2))
175 std::cerr << "usrp2::ctor set_tx_gain failed\n";
177 if (!set_rx_gain((rx_gain_min() + rx_gain_max()) / 2))
178 std::cerr << "usrp2::ctor set_rx_gain failed\n";
180 // default interp and decim
181 if (!set_tx_interp(12))
182 std::cerr << "usrp2::ctor set_tx_interp failed\n";
184 if (!set_rx_decim(12))
185 std::cerr << "usrp2::ctor set_rx_decim failed\n";
187 // set workable defaults for scaling
188 if (!set_rx_scale_iq(DEFAULT_RX_SCALE, DEFAULT_RX_SCALE))
189 std::cerr << "usrp2::ctor set_rx_scale_iq failed\n";
191 if (!set_tx_scale_iq(DEFAULT_TX_SCALE, DEFAULT_TX_SCALE))
192 std::cerr << "usrp2::ctor set_tx_scale_iq failed\n";
198 d_bg_thread = 0; // thread class deletes itself
203 if (USRP2_IMPL_DEBUG) {
204 std::cerr << std::endl
205 << "usrp2 destructor: received " << d_num_rx_frames
206 << " frames, with " << d_num_rx_missing << " lost ("
207 << (d_num_rx_frames == 0 ? 0 : (int)(100.0*d_num_rx_missing/d_num_rx_frames))
208 << "%), totaling " << d_num_rx_bytes
209 << " bytes" << std::endl;
214 usrp2::impl::parse_mac_addr(const std::string &s, u2_mac_addr_t *p)
216 p->addr[0] = 0x00; // Matt's IAB
228 return sscanf(s.c_str(), "%hhx:%hhx", &p->addr[4], &p->addr[5]) == 2;
231 return sscanf(s.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
232 &p->addr[0], &p->addr[1], &p->addr[2],
233 &p->addr[3], &p->addr[4], &p->addr[5]) == 6;
240 usrp2::impl::init_et_hdrs(u2_eth_packet_t *p, const std::string &dst)
242 p->ehdr.ethertype = htons(U2_ETHERTYPE);
243 parse_mac_addr(dst, &p->ehdr.dst);
244 memcpy(&p->ehdr.src, d_eth_buf->mac(), 6);
245 p->thdr.flags = 0; // FIXME transport header values?
246 p->thdr.seqno = d_tx_seqno++;
251 usrp2::impl::init_etf_hdrs(u2_eth_packet_t *p, const std::string &dst,
252 int word0_flags, int chan, uint32_t timestamp)
254 init_et_hdrs(p, dst);
255 u2p_set_word0(&p->fixed, word0_flags, chan);
256 u2p_set_timestamp(&p->fixed, timestamp);
258 if (chan == CONTROL_CHAN) { // no sequence numbers, back it out
265 usrp2::impl::init_config_rx_v2_cmd(op_config_rx_v2_cmd *cmd)
267 memset(cmd, 0, sizeof(*cmd));
268 init_etf_hdrs(&cmd->h, d_addr, 0, CONTROL_CHAN, -1);
269 cmd->op.opcode = OP_CONFIG_RX_V2;
270 cmd->op.len = sizeof(cmd->op);
271 cmd->op.rid = d_next_rid++;
272 cmd->eop.opcode = OP_EOP;
273 cmd->eop.len = sizeof(cmd->eop);
277 usrp2::impl::init_config_tx_v2_cmd(op_config_tx_v2_cmd *cmd)
279 memset(cmd, 0, sizeof(*cmd));
280 init_etf_hdrs(&cmd->h, d_addr, 0, CONTROL_CHAN, -1);
281 cmd->op.opcode = OP_CONFIG_TX_V2;
282 cmd->op.len = sizeof(cmd->op);
283 cmd->op.rid = d_next_rid++;
284 cmd->eop.opcode = OP_EOP;
285 cmd->eop.len = sizeof(cmd->eop);
289 usrp2::impl::transmit_cmd(void *cmd, size_t len, pending_reply *p, double secs)
292 d_pending_replies[p->rid()] = p;
295 if (d_eth_buf->tx_frame(cmd, len) != eth_buffer::EB_OK) {
296 d_pending_replies[p->rid()] = 0;
304 d_pending_replies[p->rid()] = 0;
308 // ----------------------------------------------------------------
309 // Background loop: received packet demuxing
310 // ----------------------------------------------------------------
313 usrp2::impl::stop_bg()
315 d_bg_running = false;
316 d_bg_pending_cond.signal();
319 d_bg_thread->join(&dummy_status);
323 usrp2::impl::bg_loop()
326 while(d_bg_running) {
328 // Receive available frames from ethernet buffer. Handler will
329 // process control frames, enqueue data packets in channel
330 // rings, and signal blocked API threads
331 int res = d_eth_buf->rx_frames(this, 100); // FIXME magic timeout
332 if (res == eth_buffer::EB_ERROR)
335 // Wait for user API thread(s) to process all enqueued packets.
336 // The channel ring thread that decrements d_num_enqueued to zero
337 // will signal this thread to continue.
339 omni_mutex_lock l(d_enqueued_mutex);
340 while(d_num_enqueued > 0 && d_bg_running)
341 d_bg_pending_cond.wait();
344 d_bg_running = false;
348 // passed to eth_buffer::rx_frames
351 usrp2::impl::operator()(const void *base, size_t len)
353 u2_eth_samples_t *pkt = (u2_eth_samples_t *)base;
355 // FIXME unaligned load!
356 int chan = u2p_chan(&pkt->hdrs.fixed);
358 if (chan == CONTROL_CHAN) { // control packets
360 return handle_control_packet(base, len);
362 else { // data packets
363 return handle_data_packet(base, len);
370 usrp2::impl::handle_control_packet(const void *base, size_t len)
372 // point to beginning of payload (subpackets)
373 unsigned char *p = (unsigned char *)base + sizeof(u2_eth_packet_t);
375 // FIXME (p % 4) == 2. Not good. Must watch for unaligned loads.
377 // FIXME iterate over payload, handling more than a single subpacket.
380 unsigned int oplen = p[1];
381 unsigned int rid = p[2];
383 pending_reply *rp = d_pending_replies[rid];
385 unsigned int buflen = rp->len();
386 if (oplen != buflen) {
387 std::cerr << "usrp2: mismatched command reply length (expected: "
388 << buflen << " got: " << oplen << "). "
389 << "op = " << opcode_to_string(opcode) << std::endl;
392 // Copy reply into caller's buffer
393 memcpy(rp->buffer(), p, std::min(oplen, buflen));
395 d_pending_replies[rid] = 0;
396 return data_handler::RELEASE;
399 // TODO: handle unsolicited, USRP2 initiated, or late replies
401 return data_handler::RELEASE;
405 usrp2::impl::handle_data_packet(const void *base, size_t len)
407 u2_eth_samples_t *pkt = (u2_eth_samples_t *)base;
409 d_num_rx_bytes += len;
411 /* --- FIXME start of fake transport layer handler --- */
413 if (d_rx_seqno != -1) {
414 int expected_seqno = (d_rx_seqno + 1) & 0xFF;
415 int seqno = pkt->hdrs.thdr.seqno;
417 if (seqno != expected_seqno) {
418 ::write(2, "S", 1); // missing sequence number
419 int missing = seqno - expected_seqno;
424 d_num_rx_missing += missing;
428 d_rx_seqno = pkt->hdrs.thdr.seqno;
430 /* --- end of fake transport layer handler --- */
432 // FIXME unaligned load!
433 unsigned int chan = u2p_chan(&pkt->hdrs.fixed);
435 if (!d_channel_rings[chan]) {
437 return data_handler::RELEASE; // discard packet, no channel handler
440 // Strip off ethernet header and transport header and enqueue the rest
442 size_t offset = offsetof(u2_eth_samples_t, hdrs.fixed);
443 if (d_channel_rings[chan]->enqueue(&pkt->hdrs.fixed, len-offset)) {
446 return data_handler::KEEP; // channel ring runner will mark frame done
450 return data_handler::RELEASE; // discard, no room in channel ring
452 return data_handler::RELEASE;
456 // ----------------------------------------------------------------
458 // ----------------------------------------------------------------
461 usrp2::impl::set_rx_gain(double gain)
463 op_config_rx_v2_cmd cmd;
464 op_config_rx_reply_v2_t reply;
466 init_config_rx_v2_cmd(&cmd);
467 cmd.op.valid = htons(CFGV_GAIN);
468 cmd.op.gain = htons(u2_double_to_fxpt_gain(gain));
470 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
471 if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT))
474 bool success = (ntohx(reply.ok) == 1);
479 usrp2::impl::set_rx_center_freq(double frequency, tune_result *result)
481 op_config_rx_v2_cmd cmd;
482 op_config_rx_reply_v2_t reply;
484 init_config_rx_v2_cmd(&cmd);
485 cmd.op.valid = htons(CFGV_FREQ);
486 u2_fxpt_freq_t fxpt = u2_double_to_fxpt_freq(frequency);
487 cmd.op.freq_hi = htonl(u2_fxpt_freq_hi(fxpt));
488 cmd.op.freq_lo = htonl(u2_fxpt_freq_lo(fxpt));
490 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
491 if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT))
494 bool success = (ntohx(reply.ok) == 1);
495 if (result && success) {
496 result->baseband_freq =
497 u2_fxpt_freq_to_double(
498 u2_fxpt_freq_from_hilo(ntohl(reply.baseband_freq_hi),
499 ntohl(reply.baseband_freq_lo)));
502 u2_fxpt_freq_to_double(
503 u2_fxpt_freq_from_hilo(ntohl(reply.ddc_freq_hi),
504 ntohl(reply.ddc_freq_lo)));
506 result->residual_freq =
507 u2_fxpt_freq_to_double(
508 u2_fxpt_freq_from_hilo(ntohl(reply.residual_freq_hi),
509 ntohl(reply.residual_freq_lo)));
511 result->spectrum_inverted = (bool)(ntohx(reply.inverted) == 1);
518 usrp2::impl::set_rx_decim(int decimation_factor)
520 op_config_rx_v2_cmd cmd;
521 op_config_rx_reply_v2_t reply;
523 init_config_rx_v2_cmd(&cmd);
524 cmd.op.valid = htons(CFGV_INTERP_DECIM);
525 cmd.op.decim = htonl(decimation_factor);
527 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
528 if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT))
531 bool success = (ntohx(reply.ok) == 1);
533 d_rx_decim = decimation_factor;
538 usrp2::impl::set_rx_scale_iq(int scale_i, int scale_q)
540 op_config_rx_v2_cmd cmd;
541 op_config_rx_reply_v2_t reply;
543 init_config_rx_v2_cmd(&cmd);
544 cmd.op.valid = htons(CFGV_SCALE_IQ);
545 cmd.op.scale_iq = htonl(((scale_i & 0xffff) << 16) | (scale_q & 0xffff));
547 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
548 if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT))
551 bool success = (ntohx(reply.ok) == 1);
556 usrp2::impl::start_rx_streaming(unsigned int channel, unsigned int items_per_frame)
558 if (channel > MAX_CHAN) {
559 std::cerr << "usrp2: invalid channel number (" << channel
564 if (channel > 0) { // until firmware supports multiple streams
565 std::cerr << "usrp2: channel " << channel
566 << " not implemented" << std::endl;
570 if (d_channel_rings[channel]) {
571 std::cerr << "usrp2: channel " << channel
572 << " already streaming" << std::endl;
576 d_channel_rings[channel] = ring_sptr(new ring(d_eth_buf->max_frames()));
578 if (items_per_frame == 0)
579 items_per_frame = U2_MAX_SAMPLES; // minimize overhead
581 op_start_rx_streaming_cmd cmd;
584 memset(&cmd, 0, sizeof(cmd));
585 init_etf_hdrs(&cmd.h, d_addr, 0, CONTROL_CHAN, -1);
586 cmd.op.opcode = OP_START_RX_STREAMING;
587 cmd.op.len = sizeof(cmd.op);
588 cmd.op.rid = d_next_rid++;
589 cmd.op.items_per_frame = htonl(items_per_frame);
590 cmd.eop.opcode = OP_EOP;
591 cmd.eop.len = sizeof(cmd.eop);
593 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
594 if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT))
597 bool success = (ntohx(reply.ok) == 1);
602 usrp2::impl::stop_rx_streaming(unsigned int channel)
604 if (channel > MAX_CHAN) {
605 std::cerr << "usrp2: invalid channel number (" << channel
610 if (channel > 0) { // until firmware supports multiple streams
611 std::cerr << "usrp2: channel " << channel
612 << " not implemented" << std::endl;
616 #if 0 // don't be overzealous.
617 if (!d_channel_rings[channel]) {
618 std::cerr << "usrp2: channel " << channel
619 << " not streaming" << std::endl;
627 memset(&cmd, 0, sizeof(cmd));
628 init_etf_hdrs(&cmd.h, d_addr, 0, CONTROL_CHAN, -1);
629 cmd.op.opcode = OP_STOP_RX;
630 cmd.op.len = sizeof(cmd.op);
631 cmd.op.rid = d_next_rid++;
632 cmd.eop.opcode = OP_EOP;
633 cmd.eop.len = sizeof(cmd.eop);
635 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
636 if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT))
639 bool success = (ntohx(reply.ok) == 1);
641 d_channel_rings[channel].reset();
648 usrp2::impl::rx_samples(unsigned int channel, rx_sample_handler *handler)
650 if (channel > MAX_CHAN) {
651 std::cerr << "usrp2: invalid channel (" << channel
652 << " )" << std::endl;
657 std::cerr << "usrp2: channel " << channel
658 << " not implemented" << std::endl;
662 ring_sptr rp = d_channel_rings[channel];
664 std::cerr << "usrp2: channel " << channel
665 << " not receiving" << std::endl;
669 // Wait for frames available in channel ring
671 rp->wait_for_not_empty();
674 // Iterate through frames and present to user
676 size_t frame_len_in_bytes;
677 while (rp->dequeue(&p, &frame_len_in_bytes)) {
678 uint32_t *items; // points to beginning of data items
679 size_t nitems_in_uint32s;
681 if (!parse_rx_metadata(p, frame_len_in_bytes, &items, &nitems_in_uint32s, &md))
684 bool want_more = (*handler)(items, nitems_in_uint32s, &md);
685 d_eth_buf->release_frame(p);
695 // ----------------------------------------------------------------
697 // ----------------------------------------------------------------
700 usrp2::impl::set_tx_gain(double gain)
702 op_config_tx_v2_cmd cmd;
703 op_config_tx_reply_v2_t reply;
705 init_config_tx_v2_cmd(&cmd);
706 cmd.op.valid = htons(CFGV_GAIN);
707 cmd.op.gain = htons(u2_double_to_fxpt_gain(gain));
709 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
710 if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT))
713 bool success = (ntohx(reply.ok) == 1);
718 usrp2::impl::set_tx_center_freq(double frequency, tune_result *result)
720 op_config_tx_v2_cmd cmd;
721 op_config_tx_reply_v2_t reply;
723 init_config_tx_v2_cmd(&cmd);
724 cmd.op.valid = htons(CFGV_FREQ);
725 u2_fxpt_freq_t fxpt = u2_double_to_fxpt_freq(frequency);
726 cmd.op.freq_hi = htonl(u2_fxpt_freq_hi(fxpt));
727 cmd.op.freq_lo = htonl(u2_fxpt_freq_lo(fxpt));
729 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
730 if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT))
733 bool success = (ntohx(reply.ok) == 1);
734 if (result && success) {
735 result->baseband_freq =
736 u2_fxpt_freq_to_double(
737 u2_fxpt_freq_from_hilo(ntohl(reply.baseband_freq_hi),
738 ntohl(reply.baseband_freq_lo)));
741 u2_fxpt_freq_to_double(
742 u2_fxpt_freq_from_hilo(ntohl(reply.duc_freq_hi),
743 ntohl(reply.duc_freq_lo)));
745 result->residual_freq =
746 u2_fxpt_freq_to_double(
747 u2_fxpt_freq_from_hilo(ntohl(reply.residual_freq_hi),
748 ntohl(reply.residual_freq_lo)));
750 result->spectrum_inverted = (bool)(ntohx(reply.inverted) == 1);
757 usrp2::impl::set_tx_interp(int interpolation_factor)
759 op_config_tx_v2_cmd cmd;
760 op_config_tx_reply_v2_t reply;
762 init_config_tx_v2_cmd(&cmd);
763 cmd.op.valid = htons(CFGV_INTERP_DECIM);
764 cmd.op.interp = htonl(interpolation_factor);
766 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
767 if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT))
770 bool success = (ntohx(reply.ok) == 1);
772 d_tx_interp = interpolation_factor;
777 usrp2::impl::set_tx_scale_iq(int scale_i, int scale_q)
779 op_config_tx_v2_cmd cmd;
780 op_config_tx_reply_v2_t reply;
782 init_config_tx_v2_cmd(&cmd);
783 cmd.op.valid = htons(CFGV_SCALE_IQ);
784 cmd.op.scale_iq = htonl(((scale_i & 0xffff) << 16) | (scale_q & 0xffff));
786 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
787 if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT))
790 bool success = (ntohx(reply.ok) == 1);
795 usrp2::impl::tx_32fc(unsigned int channel,
796 const std::complex<float> *samples,
798 const tx_metadata *metadata)
800 uint32_t items[nsamples];
801 copy_host_32fc_to_u2_16sc(nsamples, samples, items);
802 return tx_raw(channel, items, nsamples, metadata);
806 usrp2::impl::tx_16sc(unsigned int channel,
807 const std::complex<int16_t> *samples,
809 const tx_metadata *metadata)
811 #ifdef WORDS_BIGENDIAN
813 // Already binary equivalent to 16-bit I/Q on the wire.
814 // No conversion required.
816 assert(sizeof(samples[0]) == sizeof(uint32_t));
817 return tx_raw(channel, (const uint32_t *) samples, nsamples, metadata);
821 uint32_t items[nsamples];
822 copy_host_16sc_to_u2_16sc(nsamples, samples, items);
823 return tx_raw(channel, items, nsamples, metadata);
829 usrp2::impl::tx_raw(unsigned int channel,
830 const uint32_t *items,
832 const tx_metadata *metadata)
837 // FIXME there's the possibility that we send fewer than 9 items in a frame.
838 // That would end up glitching the transmitter, since the ethernet will pad to
839 // 64-bytes total (9 items). We really need some part of the stack to
840 // carry the real length (thdr?).
842 // fragment as necessary then fire away
844 size_t nframes = (nitems + U2_MAX_SAMPLES - 1) / U2_MAX_SAMPLES;
845 size_t last_frame = nframes - 1;
846 u2_eth_packet_t hdrs;
849 for (size_t fn = 0; fn < nframes; fn++){
850 uint32_t timestamp = 0;
854 timestamp = metadata->timestamp;
855 if (metadata->send_now)
856 flags |= U2P_TX_IMMEDIATE;
857 if (metadata->start_of_burst)
858 flags |= U2P_TX_START_OF_BURST;
861 flags |= U2P_TX_IMMEDIATE;
863 if (fn == last_frame){
864 if (metadata->end_of_burst)
865 flags |= U2P_TX_END_OF_BURST;
868 init_etf_hdrs(&hdrs, d_addr, flags, channel, timestamp);
870 size_t i = std::min((size_t) U2_MAX_SAMPLES, nitems - n);
873 iov[0].iov_base = &hdrs;
874 iov[0].iov_len = sizeof(hdrs);
875 iov[1].iov_base = const_cast<uint32_t *>(&items[n]);
876 iov[1].iov_len = i * sizeof(uint32_t);
878 size_t total = iov[0].iov_len + iov[1].iov_len;
880 fprintf(stderr, "usrp2::tx_raw: FIXME: short packet: %zd items (%zd bytes)\n", i, total);
882 if (d_eth_buf->tx_framev(iov, 2) != eth_buffer::EB_OK){
892 // ----------------------------------------------------------------
894 // ----------------------------------------------------------------
897 usrp2::impl::config_mimo(int flags)
899 op_config_mimo_cmd cmd;
902 memset(&cmd, 0, sizeof(cmd));
903 init_etf_hdrs(&cmd.h, d_addr, 0, CONTROL_CHAN, -1);
904 cmd.op.opcode = OP_CONFIG_MIMO;
905 cmd.op.len = sizeof(cmd.op);
906 cmd.op.rid = d_next_rid++;
907 cmd.eop.opcode = OP_EOP;
908 cmd.eop.len = sizeof(cmd.eop);
910 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
911 if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT))
914 return ntohx(reply.ok) == 1;
918 usrp2::impl::fpga_master_clock_freq(long *freq)
920 *freq = 100000000L; // 100 MHz
925 usrp2::impl::adc_rate(long *rate)
927 return fpga_master_clock_freq(rate);
931 usrp2::impl::dac_rate(long *rate)
933 return fpga_master_clock_freq(rate);
937 usrp2::impl::tx_daughterboard_id(int *dbid)
939 *dbid = d_tx_db_info.dbid;
944 usrp2::impl::rx_daughterboard_id(int *dbid)
946 *dbid = d_rx_db_info.dbid;
951 // ----------------------------------------------------------------
952 // low-level commands
953 // ----------------------------------------------------------------
956 usrp2::impl::burn_mac_addr(const std::string &new_addr)
958 op_burn_mac_addr_cmd cmd;
961 memset(&cmd, 0, sizeof(cmd));
962 init_etf_hdrs(&cmd.h, d_addr, 0, CONTROL_CHAN, -1);
963 cmd.op.opcode = OP_BURN_MAC_ADDR;
964 cmd.op.len = sizeof(cmd.op);
965 cmd.op.rid = d_next_rid++;
966 if (!parse_mac_addr(new_addr, &cmd.op.addr))
969 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
970 if (!transmit_cmd(&cmd, sizeof(cmd), &p, 4*DEF_CMD_TIMEOUT))
973 bool success = (ntohx(reply.ok) == 1);
978 fill_dboard_info(db_info *dst, const u2_db_info_t *src)
980 dst->dbid = ntohl(src->dbid);
983 u2_fxpt_freq_to_double(u2_fxpt_freq_from_hilo(ntohl(src->freq_min_hi),
984 ntohl(src->freq_min_lo)));
986 u2_fxpt_freq_to_double(u2_fxpt_freq_from_hilo(ntohl(src->freq_max_hi),
987 ntohl(src->freq_max_lo)));
989 dst->gain_min = u2_fxpt_gain_to_double(ntohs(src->gain_min));
990 dst->gain_max = u2_fxpt_gain_to_double(ntohs(src->gain_max));
991 dst->gain_step_size = u2_fxpt_gain_to_double(ntohs(src->gain_step_size));
995 usrp2::impl::dboard_info()
997 op_dboard_info_cmd cmd;
998 op_dboard_info_reply_t reply;
1000 memset(&cmd, 0, sizeof(cmd));
1001 init_etf_hdrs(&cmd.h, d_addr, 0, CONTROL_CHAN, -1);
1002 cmd.op.opcode = OP_DBOARD_INFO;
1003 cmd.op.len = sizeof(cmd.op);
1004 cmd.op.rid = d_next_rid++;
1005 cmd.eop.opcode = OP_EOP;
1006 cmd.eop.len = sizeof(cmd.eop);
1008 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
1009 if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT))
1012 bool success = (ntohx(reply.ok) == 1);
1014 fill_dboard_info(&d_tx_db_info, &reply.tx_db_info);
1015 fill_dboard_info(&d_rx_db_info, &reply.rx_db_info);
1022 usrp2::impl::sync_to_pps()
1027 memset(&cmd, 0, sizeof(cmd));
1028 init_etf_hdrs(&cmd.h, d_addr, 0, CONTROL_CHAN, -1);
1029 cmd.op.opcode = OP_SYNC_TO_PPS;
1030 cmd.op.len = sizeof(cmd.op);
1031 cmd.op.rid = d_next_rid++;
1032 cmd.eop.opcode = OP_EOP;
1033 cmd.eop.len = sizeof(cmd.eop);
1035 pending_reply p(cmd.op.rid, &reply, sizeof(reply));
1036 if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT))
1039 return ntohx(reply.ok) == 1;
1042 std::vector<uint32_t>
1043 usrp2::impl::peek32(uint32_t addr, uint32_t words)
1045 std::vector<uint32_t> result; // zero sized on error return
1046 // fprintf(stderr, "usrp2::peek: addr=%08X words=%u\n", addr, words);
1048 if (addr % 4 != 0) {
1049 fprintf(stderr, "usrp2::peek: addr (=%08X) must be 32-bit word aligned\n", addr);
1057 op_generic_t *reply;
1059 int wlen = sizeof(uint32_t);
1060 int rlen = sizeof(op_generic_t);
1061 size_t bytes = words*wlen;
1063 memset(&cmd, 0, sizeof(cmd));
1064 init_etf_hdrs(&cmd.h, d_addr, 0, CONTROL_CHAN, -1);
1065 cmd.op.opcode = OP_PEEK;
1066 cmd.op.len = sizeof(cmd.op);
1067 cmd.op.rid = d_next_rid++;
1068 cmd.eop.opcode = OP_EOP;
1069 cmd.eop.len = sizeof(cmd.eop);
1071 cmd.op.addr = htonl(addr);
1072 cmd.op.bytes = htonl(bytes);
1074 reply = (op_generic_t *)malloc(rlen+bytes);
1075 pending_reply p(cmd.op.rid, reply, rlen+bytes);
1076 if (transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) {
1077 uint32_t nwords = (reply->len-rlen)/sizeof(uint32_t);
1078 uint32_t *data = (uint32_t *)(reply+rlen/wlen);
1079 for (unsigned int i = 0; i < nwords; i++)
1080 result.push_back(ntohl(data[i]));
1088 usrp2::impl::poke32(uint32_t addr, const std::vector<uint32_t> &data)
1090 if (addr % 4 != 0) {
1091 fprintf(stderr, "usrp2::poke32: addr (=%08X) must be 32-bit word aligned\n", addr);
1095 int plen = sizeof(op_poke_cmd);
1096 int wlen = sizeof(uint32_t);
1097 int max_words = (MAX_SUBPKT_LEN-plen)/wlen;
1098 int words = data.size();
1100 if (words > max_words) {
1101 fprintf(stderr, "usrp2::poke32: write size (=%u) exceeds maximum of %u words\n",
1106 //fprintf(stderr, "usrp2::poke32: addr=%08X words=%u\n", addr, words);
1114 // Allocate, clear, and initialize command packet
1115 int bytes = words*wlen;
1116 int l = plen+bytes+sizeof(*eop); // op_poke_cmd+data+eop
1117 cmd = (op_poke_cmd *)malloc(l);
1118 //fprintf(stderr, "cmd=%p l=%i\n", cmd, l);
1120 init_etf_hdrs(&cmd->h, d_addr, 0, CONTROL_CHAN, -1);
1121 cmd->op.opcode = OP_POKE;
1122 cmd->op.len = sizeof(cmd->op)+bytes;
1123 cmd->op.rid = d_next_rid++;
1124 cmd->op.addr = htonl(addr);
1126 // Copy data from vector into packet space
1127 uint32_t *dest = (uint32_t *)((uint8_t *)cmd+plen);
1128 for (unsigned int i = 0; i < words; i++) {
1129 //fprintf(stderr, "%03i@%p\n", i, dest);
1130 *dest++ = htonl(data[i]);
1133 // Write end-of-packet subpacket
1134 eop = (op_generic_t *)dest;
1135 eop->opcode = OP_EOP;
1136 eop->len = sizeof(*eop);
1137 //fprintf(stderr, "eop=%p len=%i\n", eop, eop->len);
1139 // Send command to device and retrieve reply
1142 pending_reply p(cmd->op.rid, &reply, sizeof(reply));
1143 if (transmit_cmd(cmd, l, &p, DEF_CMD_TIMEOUT))
1144 ok = (ntohx(reply.ok) == 1);
1150 } // namespace usrp2