/*********************************************************************** * transmit_worker function * A function to be used as a boost::thread_group thread for transmitting **********************************************************************/ void transmit_worker( std::vector<std::complex<float> > buff, wave_table_class wave_table, uhd::tx_streamer::sptr tx_streamer, uhd::tx_metadata_t metadata, size_t step, size_t index, int num_channels ){ std::vector<std::complex<float> *> buffs(num_channels, &buff.front()); //send data until the signal handler gets called while(not stop_signal_called){ //fill the buffer with the waveform for (size_t n = 0; n < buff.size(); n++){ buff[n] = wave_table(index += step); } //send the entire contents of the buffer tx_streamer->send(buffs, buff.size(), metadata); metadata.start_of_burst = false; metadata.has_time_spec = false; } //send a mini EOB packet metadata.end_of_burst = true; tx_streamer->send("", 0, metadata); }
/*********************************************************************** * Transmit thread **********************************************************************/ static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, uhd::tx_streamer::sptr tx_stream, const double tx_wave_freq, const double tx_wave_ampl) { uhd::set_thread_priority_safe(); // set max TX gain usrp->set_tx_gain(usrp->get_tx_gain_range().stop()); // setup variables and allocate buffer uhd::tx_metadata_t md; md.has_time_spec = false; std::vector<samp_type> buff(tx_stream->get_max_num_samps() * 10); // values for the wave table lookup size_t index = 0; const double tx_rate = usrp->get_tx_rate(); const size_t step = boost::math::iround(wave_table_len * tx_wave_freq / tx_rate); wave_table table(tx_wave_ampl); // fill buff and send until interrupted while (not boost::this_thread::interruption_requested()) { for (size_t i = 0; i < buff.size(); i++) buff[i] = table(index += step); tx_stream->send(&buff.front(), buff.size(), md); } // send a mini EOB packet md.end_of_burst = true; tx_stream->send("", 0, md); }
/*********************************************************************** * tx_worker function * A function to be used in its own thread for transmitting. Push all * tx values into the USRP buffer as USRP buffer space is available, * but allow other actions to occur concurrently. **********************************************************************/ void tx_worker( unsigned int bufflen, uhd::tx_streamer::sptr tx_stream, uhd::time_spec_t start_time, std::complex<int16_t>* vec_ptr, int end ){ unsigned int acc_samps = 0; uhd::tx_metadata_t md; md.start_of_burst = true; md.has_time_spec = true; md.time_spec = start_time; size_t spb = tx_stream->get_max_num_samps(); if (spb > bufflen) spb = bufflen; while(acc_samps < bufflen-spb){ size_t nsamples = tx_stream->send(vec_ptr, spb, md); vec_ptr += spb; acc_samps += nsamples; //std::cout << acc_samps <<std::endl; md.start_of_burst = false; md.has_time_spec = false; } // Now on the last packet if (end) md.end_of_burst = true; spb = bufflen - acc_samps; size_t nsamples = tx_stream->send(vec_ptr, spb, md); }
/*********************************************************************** * TX Hammer **********************************************************************/ void tx_hammer(uhd::usrp::multi_usrp::sptr usrp, const std::string &tx_cpu, uhd::tx_streamer::sptr tx_stream){ uhd::set_thread_priority_safe(); uhd::tx_metadata_t md; const size_t max_samps_per_packet = tx_stream->get_max_num_samps(); std::vector<char> buff(max_samps_per_packet*uhd::convert::get_bytes_per_item(tx_cpu)); std::vector<void *> buffs; for (size_t ch = 0; ch < tx_stream->get_num_channels(); ch++) buffs.push_back(&buff.front()); //same buffer for each channel //print pre-test summary std::cout << boost::format( "Testing transmit rate %f Msps" ) % (usrp->get_tx_rate()/1e6) << std::endl; //setup variables and allocate buffer std::srand( time(NULL) ); while(not boost::this_thread::interruption_requested()){ size_t total_num_samps = rand() % 100000; size_t num_acc_samps = 0; float timeout = 1; usrp->set_time_now(uhd::time_spec_t(0.0)); while(num_acc_samps < total_num_samps){ //send a single packet num_tx_samps += tx_stream->send(buffs, max_samps_per_packet, md, timeout); num_acc_samps += std::min(total_num_samps-num_acc_samps, tx_stream->get_max_num_samps()); } //send a mini EOB packet md.end_of_burst = true; tx_stream->send("", 0, md); } }
/*********************************************************************** * Benchmark TX Rate **********************************************************************/ void benchmark_tx_rate( uhd::usrp::multi_usrp::sptr usrp, const std::string &tx_cpu, uhd::tx_streamer::sptr tx_stream, std::atomic<bool>& burst_timer_elapsed, const boost::posix_time::ptime &start_time, bool random_nsamps=false ) { uhd::set_thread_priority_safe(); //print pre-test summary std::cout << boost::format( "[%s] Testing transmit rate %f Msps on %u channels" ) % NOW() % (usrp->get_tx_rate()/1e6) % tx_stream->get_num_channels() << std::endl; //setup variables and allocate buffer uhd::tx_metadata_t md; md.time_spec = usrp->get_time_now() + uhd::time_spec_t(INIT_DELAY); md.has_time_spec = (tx_stream->get_num_channels() > 1); const size_t max_samps_per_packet = tx_stream->get_max_num_samps(); std::vector<char> buff(max_samps_per_packet*uhd::convert::get_bytes_per_item(tx_cpu)); std::vector<const void *> buffs; for (size_t ch = 0; ch < tx_stream->get_num_channels(); ch++) buffs.push_back(&buff.front()); //same buffer for each channel md.has_time_spec = (buffs.size() != 1); if (random_nsamps) { std::srand((unsigned int)time(NULL)); while (not burst_timer_elapsed) { size_t total_num_samps = rand() % max_samps_per_packet; size_t num_acc_samps = 0; const float timeout = 1; usrp->set_time_now(uhd::time_spec_t(0.0)); while(num_acc_samps < total_num_samps){ //send a single packet num_tx_samps += tx_stream->send(buffs, max_samps_per_packet, md, timeout)*tx_stream->get_num_channels(); num_acc_samps += std::min(total_num_samps-num_acc_samps, tx_stream->get_max_num_samps()); } } } else { while (not burst_timer_elapsed) { const size_t num_tx_samps_sent_now = tx_stream->send(buffs, max_samps_per_packet, md)*tx_stream->get_num_channels(); num_tx_samps += num_tx_samps_sent_now; if (num_tx_samps_sent_now == 0) { num_timeouts_tx++; if ((num_timeouts_tx % 10000) == 1) { std::cerr << "[" << NOW() << "] Tx timeouts: " << num_timeouts_tx << std::endl; } } md.has_time_spec = false; } } //send a mini EOB packet md.end_of_burst = true; tx_stream->send(buffs, 0, md); }
/*! * Test the underflow message: * Send a start of burst packet with no following end of burst. * We expect to get an underflow(within a burst) async message. */ bool test_underflow_message(uhd::usrp::multi_usrp::sptr, uhd::rx_streamer::sptr, uhd::tx_streamer::sptr tx_stream){ std::cout << "Test underflow message... " << std::flush; uhd::tx_metadata_t md; md.start_of_burst = true; md.end_of_burst = false; md.has_time_spec = false; tx_stream->send("", 0, md); uhd::async_metadata_t async_md; if (not tx_stream->recv_async_msg(async_md, 1)){ std::cout << boost::format( "failed:\n" " Async message recv timed out.\n" ) << std::endl; return false; } switch(async_md.event_code){ case uhd::async_metadata_t::EVENT_CODE_UNDERFLOW: std::cout << boost::format( "success:\n" " Got event code underflow message.\n" ) << std::endl; return true; default: std::cout << boost::format( "failed:\n" " Got unexpected event code 0x%x.\n" ) % async_md.event_code << std::endl; return false; } }
int uhd_device::writeSamples(short *buf, int len, bool *underrun, unsigned long long timestamp,bool isControl) { uhd::tx_metadata_t metadata; metadata.has_time_spec = true; metadata.start_of_burst = false; metadata.end_of_burst = false; metadata.time_spec = convert_time(timestamp, tx_rate); *underrun = false; // No control packets if (isControl) { LOG(ERR) << "Control packets not supported"; return 0; } // Drop a fixed number of packets (magic value) if (!aligned) { drop_cnt++; if (drop_cnt == 1) { LOG(DEBUG) << "Aligning transmitter: stop burst"; *underrun = true; metadata.end_of_burst = true; } else if (drop_cnt < 30) { LOG(DEBUG) << "Aligning transmitter: packet advance"; return len; } else { LOG(DEBUG) << "Aligning transmitter: start burst"; metadata.start_of_burst = true; aligned = true; drop_cnt = 0; } } size_t num_smpls = tx_stream->send(buf, len, metadata); if (num_smpls != (unsigned) len) { LOG(ALERT) << "UHD: Device send timed out"; LOG(ALERT) << "UHD: Version " << uhd::get_version_string(); LOG(ALERT) << "UHD: Unrecoverable error, exiting..."; exit(-1); } return num_smpls; }
/*! * Test the burst ack message: * Send a burst of many samples that will fragment internally. * We expect to get an burst ack async message. */ bool test_burst_ack_message(uhd::usrp::multi_usrp::sptr, uhd::rx_streamer::sptr, uhd::tx_streamer::sptr tx_stream){ std::cout << "Test burst ack message... " << std::flush; uhd::tx_metadata_t md; md.start_of_burst = true; md.end_of_burst = true; md.has_time_spec = false; //3 times max-sps guarantees a SOB, no burst, and EOB packet std::vector<std::complex<float> > buff(tx_stream->get_max_num_samps()*3); tx_stream->send( &buff.front(), buff.size(), md ); uhd::async_metadata_t async_md; if (not tx_stream->recv_async_msg(async_md)){ std::cout << boost::format( "failed:\n" " Async message recv timed out.\n" ) << std::endl; return false; } switch(async_md.event_code){ case uhd::async_metadata_t::EVENT_CODE_BURST_ACK: std::cout << boost::format( "success:\n" " Got event code burst ack message.\n" ) << std::endl; return true; default: std::cout << boost::format( "failed:\n" " Got unexpected event code 0x%x.\n" ) % async_md.event_code << std::endl; return false; } }
/*! * Test the time error message: * Send a burst packet that occurs at a time in the past. * We expect to get a time error async message. */ bool test_time_error_message(uhd::usrp::multi_usrp::sptr usrp, uhd::rx_streamer::sptr, uhd::tx_streamer::sptr tx_stream){ std::cout << "Test time error message... " << std::flush; uhd::tx_metadata_t md; md.start_of_burst = true; md.end_of_burst = true; md.has_time_spec = true; md.time_spec = uhd::time_spec_t(100.0); //send at 100s usrp->set_time_now(uhd::time_spec_t(200.0)); //time at 200s tx_stream->send("", 0, md); uhd::async_metadata_t async_md; if (not tx_stream->recv_async_msg(async_md)){ std::cout << boost::format( "failed:\n" " Async message recv timed out.\n" ) << std::endl; return false; } switch(async_md.event_code){ case uhd::async_metadata_t::EVENT_CODE_TIME_ERROR: std::cout << boost::format( "success:\n" " Got event code time error message.\n" ) << std::endl; return true; default: std::cout << boost::format( "failed:\n" " Got unexpected event code 0x%x.\n" ) % async_md.event_code << std::endl; return false; } }
void thread_startTx(){ num_tx_samps = tx_stream->send(&small_tx_buff.front(), small_tx_buff.size(), tx_md); }