summaryrefslogtreecommitdiff
path: root/usrp2/host
diff options
context:
space:
mode:
Diffstat (limited to 'usrp2/host')
-rw-r--r--usrp2/host/lib/control.cc20
-rw-r--r--usrp2/host/lib/control.h11
-rw-r--r--usrp2/host/lib/usrp2_impl.cc127
-rw-r--r--usrp2/host/lib/usrp2_impl.h6
4 files changed, 117 insertions, 47 deletions
diff --git a/usrp2/host/lib/control.cc b/usrp2/host/lib/control.cc
index 4b8597c60..bb71f79c2 100644
--- a/usrp2/host/lib/control.cc
+++ b/usrp2/host/lib/control.cc
@@ -30,26 +30,34 @@
namespace usrp2 {
pending_reply::pending_reply(unsigned int rid, void *buffer, size_t len)
- : d_rid(rid), d_mutex(), d_cond(&d_mutex), d_buffer(buffer), d_len(len)
+ : d_rid(rid), d_buffer(buffer), d_len(len), d_mutex(), d_cond(&d_mutex),
+ d_complete(false)
{
}
pending_reply::~pending_reply()
{
- signal(); // Needed?
+ notify_completion(); // Needed?
}
int
- pending_reply::wait(double secs)
+ pending_reply::wait_for_completion(double secs)
{
- omni_mutex_lock l(d_mutex);
omni_time abs_timeout = omni_time::time(omni_time(secs));
- return d_cond.timedwait(abs_timeout.d_secs, abs_timeout.d_nsecs);
+ omni_mutex_lock l(d_mutex);
+ while (!d_complete){
+ int r = d_cond.timedwait(abs_timeout.d_secs, abs_timeout.d_nsecs);
+ if (r == 0) // timed out
+ return 0;
+ }
+ return 1;
}
void
- pending_reply::signal()
+ pending_reply::notify_completion()
{
+ omni_mutex_lock l(d_mutex);
+ d_complete = true;
d_cond.signal();
}
diff --git a/usrp2/host/lib/control.h b/usrp2/host/lib/control.h
index 9adc1618f..8769e4522 100644
--- a/usrp2/host/lib/control.h
+++ b/usrp2/host/lib/control.h
@@ -118,10 +118,13 @@ namespace usrp2 {
{
private:
unsigned int d_rid;
- omni_mutex d_mutex;
- omni_condition d_cond;
void *d_buffer;
size_t d_len;
+
+ // d_mutex is used with d_cond and also protects d_complete
+ omni_mutex d_mutex;
+ omni_condition d_cond;
+ bool d_complete;
public:
/*!
@@ -140,12 +143,12 @@ namespace usrp2 {
* Returns: 1 = ok, reply packet in buffer
* 0 = timeout
*/
- int wait(double secs);
+ int wait_for_completion(double secs);
/*!
* Allows creating thread to resume after copying reply into buffer
*/
- void signal();
+ void notify_completion();
/*!
* Retrieve pending reply ID
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);
diff --git a/usrp2/host/lib/usrp2_impl.h b/usrp2/host/lib/usrp2_impl.h
index ec96f3a70..ed71a6ba3 100644
--- a/usrp2/host/lib/usrp2_impl.h
+++ b/usrp2/host/lib/usrp2_impl.h
@@ -87,6 +87,8 @@ namespace usrp2 {
int d_tx_interp; // shadow tx interp
int d_rx_decim; // shadow rx decim
+ bool d_dont_enqueue;
+
void inc_enqueued() {
omni_mutex_lock l(d_enqueued_mutex);
d_num_enqueued++;
@@ -105,7 +107,8 @@ namespace usrp2 {
void stop_bg();
void init_config_rx_v2_cmd(op_config_rx_v2_cmd *cmd);
void init_config_tx_v2_cmd(op_config_tx_v2_cmd *cmd);
- bool transmit_cmd(void *cmd, size_t len, pending_reply *p, double secs=0.0);
+ bool transmit_cmd_and_wait(void *cmd, size_t len, pending_reply *p, double secs=0.0);
+ bool transmit_cmd(void *cmd, size_t len);
virtual data_handler::result operator()(const void *base, size_t len);
data_handler::result handle_control_packet(const void *base, size_t len);
data_handler::result handle_data_packet(const void *base, size_t len);
@@ -141,6 +144,7 @@ namespace usrp2 {
bool read_gpio(int bank, uint16_t *value);
bool start_rx_streaming(unsigned int channel, unsigned int items_per_frame);
bool rx_samples(unsigned int channel, rx_sample_handler *handler);
+ bool flush_rx_samples(unsigned int channel);
bool stop_rx_streaming(unsigned int channel);
unsigned int rx_overruns() const { return d_num_rx_overruns; }
unsigned int rx_missing() const { return d_num_rx_missing; }