From cadafd4c3e67203aea80b99bd3839fc84ff7c17f Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Fri, 11 Mar 2011 16:22:20 -0800 Subject: uhd: cleanup/tweaks on timed samples examples --- host/examples/latency_test.cpp | 6 ++-- host/examples/rx_timed_samples.cpp | 49 ++++++++++++++++---------------- host/examples/tx_timed_samples.cpp | 58 +++++++++++++++++++++++--------------- 3 files changed, 63 insertions(+), 50 deletions(-) diff --git a/host/examples/latency_test.cpp b/host/examples/latency_test.cpp index be44aad70..85ac73567 100644 --- a/host/examples/latency_test.cpp +++ b/host/examples/latency_test.cpp @@ -150,9 +150,9 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ break; default: - std::cout << boost::format - ("failed:\n Got unexpected event code 0x%x.\n") - % async_md.event_code << std::endl; + std::cerr << boost::format( + "failed:\n Got unexpected event code 0x%x.\n" + ) % async_md.event_code << std::endl; other++; break; } diff --git a/host/examples/rx_timed_samples.cpp b/host/examples/rx_timed_samples.cpp index 630b4a7a9..046b9d367 100644 --- a/host/examples/rx_timed_samples.cpp +++ b/host/examples/rx_timed_samples.cpp @@ -39,8 +39,8 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ desc.add_options() ("help", "help message") ("args", po::value(&args)->default_value(""), "single uhd device address args") - ("secs", po::value(&seconds_in_future)->default_value(3), "number of seconds in the future to receive") - ("nsamps", po::value(&total_num_samps)->default_value(1000), "total number of samples to receive") + ("secs", po::value(&seconds_in_future)->default_value(1.5), "number of seconds in the future to receive") + ("nsamps", po::value(&total_num_samps)->default_value(10000), "total number of samples to receive") ("clock", po::value(&clock), "master clock frequency in Hz") ("rate", po::value(&rate)->default_value(100e6/16), "rate of incoming samples") ("freq", po::value(&freq)->default_value(0), "rf center frequency in Hz") @@ -94,44 +94,43 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ stream_cmd.time_spec = uhd::time_spec_t(seconds_in_future); usrp->issue_stream_cmd(stream_cmd); - //allocate buffer to receive + //meta-data will be filled in by recv() + uhd::rx_metadata_t md; + + //allocate buffer to receive with samples std::vector > buff(usrp->get_device()->get_max_recv_samps_per_packet()); - //loop until total number of samples reached + //the first call to recv() will block this many seconds before receiving + double timeout = seconds_in_future + 0.1; //timeout (delay before receive + padding) + size_t num_acc_samps = 0; //number of accumulated samples while(num_acc_samps < total_num_samps){ - uhd::rx_metadata_t md; + //receive a single packet size_t num_rx_samps = usrp->get_device()->recv( &buff.front(), buff.size(), md, uhd::io_type_t::COMPLEX_FLOAT32, - uhd::device::RECV_MODE_ONE_PACKET + uhd::device::RECV_MODE_ONE_PACKET, timeout ); - //handle the error codes - switch(md.error_code){ - case uhd::rx_metadata_t::ERROR_CODE_NONE: - break; - - case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT: - if (num_acc_samps == 0) continue; - std::cout << boost::format( - "Got timeout before all samples received, possible packet loss, exiting loop..." - ) << std::endl; - goto done_loop; - - default: - std::cout << boost::format( - "Got error code 0x%x, exiting loop..." - ) % md.error_code << std::endl; - goto done_loop; + //use a small timeout for subsequent packets + timeout = 0.1; + + //handle the error code + if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) break; + if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE){ + throw std::runtime_error(str(boost::format( + "Unexpected error code 0x%x" + ) % md.error_code)); } if(verbose) std::cout << boost::format( - "Got packet: %u samples, %u full secs, %f frac secs" + "Received packet: %u samples, %u full secs, %f frac secs" ) % num_rx_samps % md.time_spec.get_full_secs() % md.time_spec.get_frac_secs() << std::endl; num_acc_samps += num_rx_samps; - } done_loop: + } + + if (num_acc_samps < total_num_samps) std::cerr << "Receive timeout before all samples received..." << std::endl; //finished std::cout << std::endl << "Done!" << std::endl << std::endl; diff --git a/host/examples/tx_timed_samples.cpp b/host/examples/tx_timed_samples.cpp index f10d7e4ea..25a2c0e43 100644 --- a/host/examples/tx_timed_samples.cpp +++ b/host/examples/tx_timed_samples.cpp @@ -33,7 +33,6 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ std::string args; double seconds_in_future; size_t total_num_samps; - size_t samps_per_packet; double rate, freq; float ampl; @@ -42,9 +41,8 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ desc.add_options() ("help", "help message") ("args", po::value(&args)->default_value(""), "single uhd device address args") - ("secs", po::value(&seconds_in_future)->default_value(3), "number of seconds in the future to transmit") - ("nsamps", po::value(&total_num_samps)->default_value(1000), "total number of samples to transmit") - ("spp", po::value(&samps_per_packet)->default_value(1000), "number of samples per packet") + ("secs", po::value(&seconds_in_future)->default_value(1.5), "number of seconds in the future to transmit") + ("nsamps", po::value(&total_num_samps)->default_value(10000), "total number of samples to transmit") ("rate", po::value(&rate)->default_value(100e6/16), "rate of outgoing samples") ("freq", po::value(&freq)->default_value(0), "rf center frequency in Hz") ("ampl", po::value(&l)->default_value(float(0.3)), "amplitude of each sample") @@ -81,37 +79,53 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ std::cout << boost::format("Setting device timestamp to 0...") << std::endl; usrp->set_time_now(uhd::time_spec_t(0.0)); - //allocate data to send - std::vector > buff(samps_per_packet, std::complex(ampl, ampl)); + //allocate buffer with data to send + std::vector > buff(usrp->get_device()->get_max_send_samps_per_packet(), std::complex(ampl, ampl)); + //setup metadata for the first packet uhd::tx_metadata_t md; + md.start_of_burst = false; + md.end_of_burst = false; + md.has_time_spec = true; md.time_spec = uhd::time_spec_t(seconds_in_future); - //send the data in multiple packets - size_t num_packets = (total_num_samps+samps_per_packet-1)/samps_per_packet; - for (size_t i = 0; i < num_packets; i++){ + //the first call to send() will block this many seconds before sending: + double timeout = seconds_in_future + 0.1; //timeout (delay before transmit + padding) - //setup the metadata flags per fragment - md.start_of_burst = (i == 0); //only first packet has SOB - md.end_of_burst = (i == num_packets-1); //only last packet has EOB - md.has_time_spec = (i == 0); //only first packet has time + size_t num_acc_samps = 0; //number of accumulated samples + while(num_acc_samps < total_num_samps){ + size_t samps_to_send = std::min(total_num_samps - num_acc_samps, buff.size()); - size_t samps_to_send = std::min(total_num_samps - samps_per_packet*i, samps_per_packet); + //ensure the the last packet has EOB set + md.end_of_burst = samps_to_send <= buff.size(); - //send the entire packet (driver fragments internally) + //send a single packet size_t num_tx_samps = usrp->get_device()->send( &buff.front(), samps_to_send, md, uhd::io_type_t::COMPLEX_FLOAT32, - uhd::device::SEND_MODE_FULL_BUFF, - //send will backup into the host this many seconds before sending: - seconds_in_future + 0.1 //timeout (delay before transmit + padding) + uhd::device::SEND_MODE_ONE_PACKET, timeout ); - if (num_tx_samps < samps_to_send) std::cout << "Send timeout..." << std::endl; - if(verbose) std::cout << std::endl << boost::format("Sent %d samples") % num_tx_samps << std::endl; + + //use a small timeout for subsequent packets + timeout = 0.1; + + //do not use time spec for subsequent packets + md.has_time_spec = false; + + if (num_tx_samps < samps_to_send) std::cerr << "Send timeout..." << std::endl; + if(verbose) std::cout << boost::format("Sent packet: %u samples") % num_tx_samps << std::endl; + + num_acc_samps += num_tx_samps; } - //ensure that the buffers have flushed out to the device before deconstruction - boost::this_thread::sleep(boost::posix_time::seconds(1)); + std::cout << std::endl << "Waiting for async burst ACK... " << std::flush; + uhd::async_metadata_t async_md; + bool got_async_burst_ack = false; + //loop through all messages for the ACK packet (may have underflow messages in queue) + while (not got_async_burst_ack and usrp->get_device()->recv_async_msg(async_md, seconds_in_future)){ + got_async_burst_ack = (async_md.event_code == uhd::async_metadata_t::EVENT_CODE_BURST_ACK); + } + std::cout << (got_async_burst_ack? "success" : "fail") << std::endl; //finished std::cout << std::endl << "Done!" << std::endl << std::endl; -- cgit v1.2.3