int uhd_device::check_rx_md_err(uhd::rx_metadata_t &md, ssize_t num_smpls) { uhd::time_spec_t ts; if (!num_smpls) { LOG(ERR) << str_code(md); switch (md.error_code) { case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT: LOG(ALERT) << "UHD: Receive timed out"; return ERROR_UNRECOVERABLE; case uhd::rx_metadata_t::ERROR_CODE_OVERFLOW: case uhd::rx_metadata_t::ERROR_CODE_LATE_COMMAND: case uhd::rx_metadata_t::ERROR_CODE_BROKEN_CHAIN: case uhd::rx_metadata_t::ERROR_CODE_BAD_PACKET: default: return ERROR_UNHANDLED; } } // Missing timestamp if (!md.has_time_spec) { LOG(ALERT) << "UHD: Received packet missing timestamp"; return ERROR_UNRECOVERABLE; } ts = md.time_spec; // Monotonicity check if (ts < prev_ts) { LOG(ALERT) << "UHD: Loss of monotonic time"; LOG(ALERT) << "Current time: " << ts.get_real_secs() << ", " << "Previous time: " << prev_ts.get_real_secs(); return ERROR_TIMING; } else { prev_ts = ts; } return 0; }
int UHD_SAFE_MAIN(int argc, char *argv[]){ uhd::set_thread_priority_safe(); std::string args; //Set up program options po::options_description desc("Allowed options"); desc.add_options() ("help", "help message") ("args", po::value<std::string>(&args)->default_value(""), "Specify a single USRP.") ; po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); //Print the help message if (vm.count("help")){ std::cout << boost::format("Query GPSDO Sensors %s") % desc << std::endl; return ~0; } //Create a USRP device std::cout << boost::format("\nCreating the USRP device with: %s...\n") % args; uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args); std::cout << boost::format("Using Device: %s\n") % usrp->get_pp_string(); //Helpful notes std::cout << boost::format("**************************************Helpful Notes on Clock/PPS Selection**************************************\n"); std::cout << boost::format("As you can see, the default 10 MHz Reference and 1 PPS signals are now from the GPSDO.\n"); std::cout << boost::format("If you would like to use the internal reference(TCXO) in other applications, you must configure that explicitly.\n"); std::cout << boost::format("You can no longer select the external SMAs for 10 MHz or 1 PPS signaling.\n"); std::cout << boost::format("****************************************************************************************************************\n"); //Verify GPS sensors are present (i.e. EEPROM has been burnt) std::vector<std::string> sensor_names = usrp->get_mboard_sensor_names(0); if(std::find(sensor_names.begin(), sensor_names.end(), "gps_locked") == sensor_names.end()){ std::cout << boost::format("\ngps_locked sensor not found. This could mean that you have not installed the GPSDO correctly.\n\n"); std::cout << boost::format("Visit this page if the problem persists:\n"); std::cout << boost::format("http://files.ettus.com/uhd_docs/manual/html/gpsdo.html\n\n"); exit(1); } //Check for GPS lock uhd::sensor_value_t gps_locked = usrp->get_mboard_sensor("gps_locked",0); std::cout << boost::format("%s\n") % gps_locked.to_pp_string(); if(gps_locked.to_pp_string().find("unlocked") != std::string::npos) { std::cout << boost::format("GPS does not have lock. Wait a few minutes and try again.\n"); std::cout << boost::format("NMEA strings and device time may not be accurate until lock is achieved.\n\n"); } else std::cout << boost::format("GPS Locked\n"); //Check for 10 MHz lock if(std::find(sensor_names.begin(), sensor_names.end(), "ref_locked") != sensor_names.end()){ uhd::sensor_value_t gps_locked = usrp->get_mboard_sensor("ref_locked",0); std::cout << boost::format("%s\n") % gps_locked.to_pp_string(); if(gps_locked.to_pp_string().find("unlocked") != std::string::npos){ std::cout << boost::format("USRP NOT Locked to GPSDO 10 MHz Reference.\n"); std::cout << boost::format("Double check installation instructions: https://www.ettus.com/content/files/gpsdo-kit_2.pdf\n\n"); } else std::cout << boost::format("USRP Locked to GPSDO 10 MHz Reference.\n"); } else std::cout << boost::format("ref_locked sensor not present on this board.\n"); //Check PPS and compare UHD device time to GPS time uhd::sensor_value_t gps_time = usrp->get_mboard_sensor("gps_time"); const uhd::time_spec_t last_pps_time = usrp->get_time_last_pps(); if(int(boost::math::iround(last_pps_time.get_real_secs())) == gps_time.to_int()) { std::cout << boost::format("GPS and UHD Device time are aligned.\n"); } else { std::cout << boost::format("\nGPS and UHD Device time are NOT aligned. Try re-running the program. Double check 1 PPS connection from GPSDO.\n\n"); } //print NMEA strings std::cout << boost::format("Printing available NMEA strings:\n"); uhd::sensor_value_t gga_string = usrp->get_mboard_sensor("gps_gpgga"); uhd::sensor_value_t rmc_string = usrp->get_mboard_sensor("gps_gprmc"); std::cout << boost::format("%s\n%s\n%s\n") % gga_string.to_pp_string() % rmc_string.to_pp_string() % gps_time.to_pp_string(); std::cout << boost::format("UHD Device time: %.0f seconds\n") % boost::math::iround(last_pps_time.get_real_secs()); //finished std::cout << boost::format("\nDone!\n\n"); return 0; }
TIMESTAMP convert_time(uhd::time_spec_t ts, double rate) { TIMESTAMP ticks = ts.get_full_secs() * rate; return ts.get_tick_count(rate) + ticks; }
template<typename samp_type> void recv_to_file( uhd::usrp::multi_usrp::sptr usrp, const uhd::io_type_t &io_type, std::ofstream &outfile, size_t samps_per_buff, uhd::time_spec_t send_time ){ uhd::rx_metadata_t md; std::vector<samp_type> buff(samps_per_buff); //a packet has 362 samples send_time = send_time + uhd::time_spec_t(1.0); uhd::time_spec_t front_time, end_time; int index; size_t num_rx_samps = usrp->get_device()->recv( &buff.front(), buff.size(), md, io_type, // uhd::device::RECV_MODE_FULL_BUFF uhd::device::RECV_MODE_ONE_PACKET ); if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) return; 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)); } front_time = md.time_spec; end_time = md.time_spec + uhd::time_spec_t((double)(num_rx_samps-1)*decimation/100.0e6); //outfile.write((const char*)&buff.front(), num_rx_samps*sizeof(samp_type)); if((send_time-front_time).get_real_secs() < 0 && (end_time-send_time).get_real_secs() >=0 ){ outfile.write((const char*)&buff.front(), num_rx_samps*sizeof(samp_type)); }else{ if( (send_time-front_time).get_real_secs() >=0 && (end_time-send_time).get_real_secs() >=0 ){ index = (send_time-front_time).get_frac_secs()/((double)decimation/100.0e6); outfile.write((const char*)&buff.at(index), (num_rx_samps-index)*sizeof(samp_type)); printf("start to save at %f with index %d, send_time %f \n",front_time.get_real_secs(),index,send_time.get_real_secs()); printf("timestamp %f tick %ld\n",(front_time+uhd::time_spec_t((double)index*(double)decimation/100.0e6)).get_real_secs(), (front_time+uhd::time_spec_t((double)index*(double)decimation/100.0e6)).get_tick_count(100e6)); } } }
int UHD_SAFE_MAIN(int argc, char *argv[]){ uhd::set_thread_priority_safe(); //variables to be set by po std::string args; //setup the program options po::options_description desc("Allowed options"); desc.add_options() ("help", "help message") ("args", po::value<std::string>(&args)->default_value(""), "single uhd device address args") ; po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); //print the help message if (vm.count("help")){ std::cout << boost::format("UHD Test Timed Commands %s") % desc << std::endl; return ~0; } //create a usrp device std::cout << std::endl; std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl; uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args); std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl; //check if timed commands are supported std::cout << std::endl; std::cout << "Testing support for timed commands on this hardware... " << std::flush; try{ usrp->set_command_time(uhd::time_spec_t(0.0)); usrp->clear_command_time(); } catch (const std::exception &e){ std::cout << "fail" << std::endl; std::cerr << "Got exception: " << e.what() << std::endl; std::cerr << "Timed commands are not supported on this hardware." << std::endl; return ~0; } std::cout << "pass" << std::endl; //readback time really fast, time diff is small std::cout << std::endl; std::cout << "Perform fast readback of registers:" << std::endl; uhd::time_spec_t total_time; for (size_t i = 0; i < 100; i++){ const uhd::time_spec_t t0 = usrp->get_time_now(); const uhd::time_spec_t t1 = usrp->get_time_now(); total_time += (t1-t0); } std::cout << boost::format( "Difference between paired reads: %f us" ) % (total_time.get_real_secs()/100*1e6) << std::endl; //use a timed command to start a stream at a specific time //this is not the right way start streaming at time x, //but it should approximate it within control RTT/2 //setup streaming std::cout << std::endl; std::cout << "About to start streaming using timed command:" << std::endl; //create a receive streamer uhd::stream_args_t stream_args("fc32"); //complex floats uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args); uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE); stream_cmd.num_samps = 100; stream_cmd.stream_now = true; const uhd::time_spec_t stream_time = usrp->get_time_now() + uhd::time_spec_t(0.1); usrp->set_command_time(stream_time); rx_stream->issue_stream_cmd(stream_cmd); usrp->clear_command_time(); //meta-data will be filled in by recv() uhd::rx_metadata_t md; //allocate buffer to receive with samples std::vector<std::complex<float> > buff(stream_cmd.num_samps); const size_t num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md, 1.0); 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)); } std::cout << boost::format( "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; std::cout << boost::format( "Stream time was: %u full secs, %f frac secs" ) % stream_time.get_full_secs() % stream_time.get_frac_secs() << std::endl; std::cout << boost::format( "Difference between stream time and first packet: %f us" ) % ((md.time_spec-stream_time).get_real_secs()/100*1e6) << std::endl; //finished std::cout << std::endl << "Done!" << std::endl << std::endl; return EXIT_SUCCESS; }
int SampleBuffer::read(void *buf, int len, uhd::time_spec_t ts) { return read(buf, len, ts.to_ticks(clock_rate)); }
int SampleBuffer::avail_smpls(uhd::time_spec_t ts) const { return avail_smpls(ts.to_ticks(clock_rate)); }