diff options
Diffstat (limited to 'usrp2/host/lib/usrp2_impl.cc')
-rw-r--r-- | usrp2/host/lib/usrp2_impl.cc | 127 |
1 files changed, 91 insertions, 36 deletions
diff --git a/usrp2/host/lib/usrp2_impl.cc b/usrp2/host/lib/usrp2_impl.cc index 1ecfd7348..3d0304324 100644 --- a/usrp2/host/lib/usrp2_impl.cc +++ b/usrp2/host/lib/usrp2_impl.cc @@ -133,7 +133,7 @@ namespace usrp2 { d_bg_running(false), d_rx_seqno(-1), d_tx_seqno(0), d_next_rid(0), d_num_rx_frames(0), d_num_rx_missing(0), d_num_rx_overruns(0), d_num_rx_bytes(0), d_num_enqueued(0), d_enqueued_mutex(), d_bg_pending_cond(&d_enqueued_mutex), - d_channel_rings(NCHANS), d_tx_interp(0), d_rx_decim(0) + d_channel_rings(NCHANS), d_tx_interp(0), d_rx_decim(0), d_dont_enqueue(true) { if (!d_eth_buf->open(ifc, htons(U2_ETHERTYPE))) throw std::runtime_error("Unable to register USRP2 protocol"); @@ -298,22 +298,35 @@ namespace usrp2 { cmd->eop.len = sizeof(cmd->eop); } + + bool + usrp2::impl::transmit_cmd(void *cmd_, size_t len_) + { + const void *cmd = cmd_; + int len = len_; + unsigned char tmp[64]; + + if (len_ < 64){ // pad to minimum ethernet frame size + memset(tmp, 0, sizeof(tmp)); + memcpy(tmp, cmd_, len_); + cmd = tmp; + len = sizeof(tmp); + } + + return d_eth_buf->tx_frame(cmd, len) == eth_buffer::EB_OK; + } + bool - usrp2::impl::transmit_cmd(void *cmd, size_t len, pending_reply *p, double secs) + usrp2::impl::transmit_cmd_and_wait(void *cmd, size_t len, pending_reply *p, double secs) { - if (p) - d_pending_replies[p->rid()] = p; + d_pending_replies[p->rid()] = p; - // Transmit command - if (d_eth_buf->tx_frame(cmd, len) != eth_buffer::EB_OK) { + if (!transmit_cmd(cmd, len)){ d_pending_replies[p->rid()] = 0; return false; } - int res = 1; - if (p) - res = p->wait(secs); - + int res = p->wait_for_completion(secs); d_pending_replies[p->rid()] = 0; return res == 1; } @@ -373,6 +386,10 @@ namespace usrp2 { return handle_control_packet(base, len); } else { // data packets + + if (d_dont_enqueue) // toss packet + return data_handler::RELEASE; + return handle_data_packet(base, len); } @@ -404,7 +421,7 @@ namespace usrp2 { // Copy reply into caller's buffer memcpy(rp->buffer(), p, std::min(oplen, buflen)); - rp->signal(); + rp->notify_completion(); d_pending_replies[rid] = 0; return data_handler::RELEASE; } @@ -485,7 +502,7 @@ namespace usrp2 { cmd.op.gain = htons(u2_double_to_fxpt_gain(gain)); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -512,7 +529,7 @@ namespace usrp2 { cmd.eop.len = sizeof(cmd.eop); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -532,7 +549,7 @@ namespace usrp2 { cmd.op.freq_lo = htonl(u2_fxpt_freq_lo(fxpt)); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -569,7 +586,7 @@ namespace usrp2 { cmd.op.decim = htonl(decimation_factor); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -589,7 +606,7 @@ namespace usrp2 { cmd.op.scale_iq = htonl(((scale_i & 0xffff) << 16) | (scale_q & 0xffff)); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -634,14 +651,18 @@ namespace usrp2 { cmd.eop.opcode = OP_EOP; cmd.eop.len = sizeof(cmd.eop); + d_dont_enqueue = false; bool success = false; pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - success = transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT); + success = transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT); success = success && (ntohx(reply.ok) == 1); if (success) d_channel_rings[channel] = ring_sptr(new ring(d_eth_buf->max_frames())); + else + d_dont_enqueue = true; + //fprintf(stderr, "usrp2::start_rx_streaming: success = %d\n", success); return success; } } @@ -661,6 +682,9 @@ namespace usrp2 { return false; } + d_dont_enqueue = true; // no new samples + flush_rx_samples(channel); // dump any we may already have + op_stop_rx_cmd cmd; op_generic_t reply; @@ -677,9 +701,10 @@ namespace usrp2 { bool success = false; pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - success = transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT); + success = transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT); success = success && (ntohx(reply.ok) == 1); d_channel_rings[channel].reset(); + //fprintf(stderr, "usrp2::stop_rx_streaming: success = %d\n", success); return success; } } @@ -732,6 +757,36 @@ namespace usrp2 { return true; } + bool + usrp2::impl::flush_rx_samples(unsigned int channel) + { + if (channel > MAX_CHAN) { + std::cerr << "usrp2: invalid channel (" << channel + << " )" << std::endl; + return false; + } + + if (channel > 0) { + std::cerr << "usrp2: channel " << channel + << " not implemented" << std::endl; + return false; + } + + ring_sptr rp = d_channel_rings[channel]; + if (!rp){ + return false; + } + + // Iterate through frames and drop them + void *p; + size_t frame_len_in_bytes; + while (rp->dequeue(&p, &frame_len_in_bytes)) { + d_eth_buf->release_frame(p); + dec_enqueued(); + } + return true; + } + // ---------------------------------------------------------------- // Transmit // ---------------------------------------------------------------- @@ -747,7 +802,7 @@ namespace usrp2 { cmd.op.gain = htons(u2_double_to_fxpt_gain(gain)); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -774,7 +829,7 @@ namespace usrp2 { cmd.eop.len = sizeof(cmd.eop); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -794,7 +849,7 @@ namespace usrp2 { cmd.op.freq_lo = htonl(u2_fxpt_freq_lo(fxpt)); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -831,7 +886,7 @@ namespace usrp2 { cmd.op.interp = htonl(interpolation_factor); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -882,7 +937,7 @@ namespace usrp2 { cmd.op.scale_iq = htonl(((scale_i & 0xffff) << 16) | (scale_q & 0xffff)); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -1010,7 +1065,7 @@ namespace usrp2 { cmd.eop.len = sizeof(cmd.eop); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; return ntohx(reply.ok) == 1; @@ -1069,7 +1124,7 @@ namespace usrp2 { return false; pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, 4*DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, 4*DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -1108,7 +1163,7 @@ namespace usrp2 { cmd.eop.len = sizeof(cmd.eop); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -1135,7 +1190,7 @@ namespace usrp2 { cmd.eop.len = sizeof(cmd.eop); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; return ntohx(reply.ok) == 1; @@ -1157,7 +1212,7 @@ namespace usrp2 { cmd.eop.len = sizeof(cmd.eop); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; return ntohx(reply.ok) == 1; @@ -1197,7 +1252,7 @@ namespace usrp2 { reply = (op_generic_t *)malloc(rlen+bytes); pending_reply p(cmd.op.rid, reply, rlen+bytes); - if (transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) { + if (transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) { uint32_t nwords = (reply->len-rlen)/sizeof(uint32_t); uint32_t *data = (uint32_t *)(reply+rlen/wlen); for (unsigned int i = 0; i < nwords; i++) @@ -1264,7 +1319,7 @@ namespace usrp2 { bool ok = false; op_generic_t reply; pending_reply p(cmd->op.rid, &reply, sizeof(reply)); - if (transmit_cmd(cmd, l, &p, DEF_CMD_TIMEOUT)) + if (transmit_cmd_and_wait(cmd, l, &p, DEF_CMD_TIMEOUT)) ok = (ntohx(reply.ok) == 1); free(cmd); @@ -1286,7 +1341,7 @@ namespace usrp2 { cmd.eop.len = sizeof(cmd.eop); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -1315,7 +1370,7 @@ namespace usrp2 { cmd.eop.len = sizeof(cmd.eop); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -1348,7 +1403,7 @@ namespace usrp2 { cmd.eop.len = sizeof(cmd.eop); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -1377,7 +1432,7 @@ namespace usrp2 { cmd.eop.len = sizeof(cmd.eop); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -1406,7 +1461,7 @@ namespace usrp2 { cmd.eop.len = sizeof(cmd.eop); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); @@ -1443,7 +1498,7 @@ namespace usrp2 { cmd.eop.len = sizeof(cmd.eop); pending_reply p(cmd.op.rid, &reply, sizeof(reply)); - if (!transmit_cmd(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) + if (!transmit_cmd_and_wait(&cmd, sizeof(cmd), &p, DEF_CMD_TIMEOUT)) return false; bool success = (ntohx(reply.ok) == 1); |