diff options
| -rw-r--r-- | host/docs/usrp1.rst | 1 | ||||
| -rw-r--r-- | host/examples/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | host/examples/latency_test.cpp | 208 | ||||
| -rw-r--r-- | host/include/uhd/types/time_spec.hpp | 7 | ||||
| -rw-r--r-- | host/lib/types/CMakeLists.txt | 56 | ||||
| -rw-r--r-- | host/lib/types/time_spec.cpp | 70 | ||||
| -rw-r--r-- | host/lib/usrp/usrp1/io_impl.cpp | 16 | ||||
| -rw-r--r-- | host/lib/usrp/usrp1/soft_time_ctrl.cpp | 37 | ||||
| -rw-r--r-- | host/lib/usrp/usrp1/usrp1_impl.hpp | 1 | ||||
| -rw-r--r-- | host/tests/time_spec_test.cpp | 19 | ||||
| -rwxr-xr-x | host/utils/usrp_n2xx_net_burner.py | 37 | 
11 files changed, 411 insertions, 42 deletions
diff --git a/host/docs/usrp1.rst b/host/docs/usrp1.rst index 0a8224850..4a239444a 100644 --- a/host/docs/usrp1.rst +++ b/host/docs/usrp1.rst @@ -78,6 +78,7 @@ List of emulated features  * Setting the current device time  * Getting the current device time  * Transmitting at a specific time +* Transmitting a specific number of samples  * Receiving at a specific time  * Receiving a specific number of samples  * End of burst flags for transmit/receive diff --git a/host/examples/CMakeLists.txt b/host/examples/CMakeLists.txt index ac131b217..47152a88a 100644 --- a/host/examples/CMakeLists.txt +++ b/host/examples/CMakeLists.txt @@ -27,6 +27,7 @@ SET(example_sources      test_pps_input.cpp      tx_timed_samples.cpp      tx_waveforms.cpp +    latency_test.cpp  )  #for each source: build an executable and install diff --git a/host/examples/latency_test.cpp b/host/examples/latency_test.cpp new file mode 100644 index 000000000..340e87fb7 --- /dev/null +++ b/host/examples/latency_test.cpp @@ -0,0 +1,208 @@ +// +// Copyright 2010-2011 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program.  If not, see <http://www.gnu.org/licenses/>. +// + +#include <uhd/utils/thread_priority.hpp> +#include <uhd/utils/safe_main.hpp> +#include <uhd/usrp/single_usrp.hpp> +#include <boost/program_options.hpp> +#include <boost/format.hpp> +#include <iostream> +#include <complex> + +namespace po = boost::program_options; + +int UHD_SAFE_MAIN(int argc, char *argv[]){ +    uhd::set_thread_priority_safe(); + +    //variables to be set by po +    std::string args; +    double wait_time; +    size_t total_num_samps; +    size_t samps_per_packet; +    double rate, freq; +    float ampl; +    double delta_t; +    size_t pkts; + +    //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") +        ("wait", po::value<double>(&wait_time)->default_value(0.1), "wait time between test cycles") +        ("rtt", po::value<double>(&delta_t)->default_value(0.001), "Round-Trip time to test") +        ("nsamps", po::value<size_t>(&total_num_samps)->default_value(100), "total number of samples to transmit") +        ("spp", po::value<size_t>(&samps_per_packet)->default_value(1000), "number of samples per packet") +        ("pkts", po::value<size_t>(&pkts)->default_value(1000), "number of packets") +        ("rate", po::value<double>(&rate)->default_value(100e6/4), "rate of outgoing samples") +        ("freq", po::value<double>(&freq)->default_value(0), "rf center frequency in Hz") +        ("ampl", po::value<float>(&l)->default_value(float(0.3)), "amplitude of each sample") +        ("dilv", "specify to disable inner-loop verbose") +    ; +    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("Latency Test %s") % desc << std::endl; +        return ~0; +    } + +    bool verbose = vm.count("dilv") == 0; + +    //create a usrp device +    std::cout << std::endl; +    //std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl; +    uhd::usrp::single_usrp::sptr sdev = uhd::usrp::single_usrp::make(args); +    uhd::device::sptr dev = sdev->get_device(); +    //std::cout << boost::format("Using Device: %s") % sdev->get_pp_string() << std::endl; + +    //set the tx sample rate +    sdev->set_tx_rate(rate); +    std::cout << boost::format("Actual TX Rate: %f Msps...") % (sdev->get_tx_rate()/1e6) << std::endl; + +    //set the rx sample rate +    sdev->set_rx_rate(rate); +    std::cout << boost::format("Actual RX Rate: %f Msps...") % (sdev->get_rx_rate()/1e6) << std::endl; + +    //allocate data to send +    std::vector<std::complex<float> > txbuff(samps_per_packet, std::complex<float>(ampl, ampl)); + +    //allocate receive buffer +    std::vector<std::complex<float> > rxbuff(samps_per_packet, std::complex<float>(ampl, ampl)); + + +    int time_error = 0; +    int ack = 0; +    int underflow = 0; +    int other = 0; +    // ************************************************** +    if(verbose) +      std::cout << boost::format("Setting device timestamp to 0...") << std::endl; +    sdev->set_time_now(uhd::time_spec_t(0.0)); + +    //setup streaming +    std::cout << std::endl; +    if(verbose) +      std::cout << boost::format( +        "Begin receiving %u samples, %f seconds in the future..." +    ) % total_num_samps % wait_time << std::endl; +     +    uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE); +    stream_cmd.num_samps = total_num_samps; +    stream_cmd.stream_now = false; + +    uhd::tx_metadata_t tx_md; + +    size_t num_packets = (total_num_samps+samps_per_packet-1)/samps_per_packet; + + +    uhd::time_spec_t cur_time; +    sdev->set_time_now(uhd::time_spec_t(cur_time)); + +    for(int j=0;j<pkts;j++) { +      int err = 0; +      //sleep(0.3); +      cur_time = sdev->get_time_now() + uhd::time_spec_t(0.01); +      stream_cmd.time_spec = uhd::time_spec_t(cur_time); +      sdev->issue_stream_cmd(stream_cmd); +      size_t num_acc_samps = 0; +      uhd::rx_metadata_t rx_md; +      while(num_acc_samps < total_num_samps){ +        std::vector<std::complex<float> > rxbuff(dev->get_max_recv_samps_per_packet()); +        size_t num_rx_samps = dev->recv( +					&rxbuff.front(), rxbuff.size(), rx_md, +					uhd::io_type_t::COMPLEX_FLOAT32, +					uhd::device::RECV_MODE_ONE_PACKET +					); +	 +        switch(rx_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; +	  err = 1; +	  goto done_loop; +        default: +	  std::cout << boost::format("Got error code 0x%x, exiting loop...") % rx_md.error_code << std::endl; +	  err = 1; +	  goto done_loop; +        } +	 +        if(verbose) std::cout << boost::format("Got packet: %u samples, %u full secs, %f frac secs")  +	  % num_rx_samps % rx_md.time_spec.get_full_secs() % rx_md.time_spec.get_frac_secs() << std::endl; +	 +        num_acc_samps += num_rx_samps; +      } done_loop: +       +      if(verbose) std::cout << "Done receiving samps" << std::endl; +      if(err) +	continue; +      for (size_t i = 0; i < num_packets; i++){ +        //setup the metadata flags per fragment +        tx_md.start_of_burst = (i == 0);              //only first packet has SOB +        tx_md.end_of_burst   = (i == num_packets-1);  //only last packet has EOB +        tx_md.has_time_spec  = (i == 0);              //only first packet has time +	 +        size_t samps_to_send = std::min(total_num_samps - samps_per_packet*i, samps_per_packet); +	 +        //send the entire packet (driver fragments internally) +	//tx_md.time_spec = rx_md.time_spec+uhd::time_spec_t(delta_t); +	tx_md.time_spec = cur_time + uhd::time_spec_t(delta_t); +        size_t num_tx_samps = dev->send +	  (&txbuff.front(), samps_to_send, tx_md, +	   uhd::io_type_t::COMPLEX_FLOAT32, +	   uhd::device::SEND_MODE_FULL_BUFF, +	   //send will backup into the host this many seconds before sending: +	   0.1 //timeout (delay before transmit + padding) +	   ); +        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; +      } +       +      uhd::async_metadata_t async_md; +      if (not dev->recv_async_msg(async_md)){ +	std::cout << boost::format +	  ("failed:\n    Async message recv timed out.\n") << std::endl; +      } else { +	switch(async_md.event_code){ +	case uhd::async_metadata_t::EVENT_CODE_TIME_ERROR : +	  time_error++; +	  break; +	case uhd::async_metadata_t::EVENT_CODE_BURST_ACK : +	  ack++; +	  break; +	case uhd::async_metadata_t::EVENT_CODE_UNDERFLOW : +	  underflow++; +	  break; +	default: +	  std::cout << boost::format +	    ("failed:\n    Got unexpected event code 0x%x.\n")  +	    % async_md.event_code << std::endl; +	  other++; +	  break; +	}      +      } +    } +    std::cout << boost::format("ACK %d, UNDERFLOW %d, TIME_ERR %d, other %d") % ack  +      % underflow % time_error % other << std::endl; +    return 0; +} diff --git a/host/include/uhd/types/time_spec.hpp b/host/include/uhd/types/time_spec.hpp index 57d002d48..2046fbd3f 100644 --- a/host/include/uhd/types/time_spec.hpp +++ b/host/include/uhd/types/time_spec.hpp @@ -40,6 +40,13 @@ namespace uhd{      public:          /*! +         * Get the system time in time_spec_t format. +         * Uses the highest precision clock available. +         * \return the system time as a time_spec_t +         */ +        static time_spec_t get_system_time(void); + +        /*!           * Create a time_spec_t from a real-valued seconds count.           * \param secs the real-valued seconds count (default = 0)           */ diff --git a/host/lib/types/CMakeLists.txt b/host/lib/types/CMakeLists.txt index dfb7cf903..ad625111e 100644 --- a/host/lib/types/CMakeLists.txt +++ b/host/lib/types/CMakeLists.txt @@ -16,6 +16,62 @@  #  ######################################################################## +# Setup defines for high resolution timing +######################################################################## +MESSAGE(STATUS "") +MESSAGE(STATUS "Configuring high resolution timing...") +INCLUDE(CheckCXXSourceCompiles) + +SET(CMAKE_REQUIRED_LIBRARIES -lrt) +CHECK_CXX_SOURCE_COMPILES(" +    #include <ctime> +    int main(){ +        timespec ts; +        return clock_gettime(CLOCK_MONOTONIC, &ts); +    } +    " HAVE_CLOCK_GETTIME +) +UNSET(CMAKE_REQUIRED_LIBRARIES) + +INCLUDE(CheckCXXSourceCompiles) +CHECK_CXX_SOURCE_COMPILES(" +    #include <mach/mach_time.h> +    int main(){ +        mach_timebase_info_data_t info; +        mach_timebase_info(&info); +        mach_absolute_time(); +        return 0; +    } +    " HAVE_MACH_ABSOLUTE_TIME +) + +CHECK_CXX_SOURCE_COMPILES(" +    #include <Windows.h> +    int main(){ +        LARGE_INTEGER value; +        QueryPerformanceCounter(&value); +        QueryPerformanceFrequency(&value); +        return 0; +    } +    " HAVE_QUERY_PERFORMANCE_COUNTER +) + +IF(HAVE_CLOCK_GETTIME) +    MESSAGE(STATUS "  High resolution timing supported through clock_gettime.") +    ADD_DEFINITIONS(-DTIME_SPEC_USE_CLOCK_GETTIME) +    SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lrt") +ELSEIF(HAVE_MACH_ABSOLUTE_TIME) +    MESSAGE(STATUS "  High resolution timing supported through mach_absolute_time.") +    ADD_DEFINITIONS(-DTIME_SPEC_USE_MACH_ABSOLUTE_TIME) +ELSEIF(HAVE_QUERY_PERFORMANCE_COUNTER) +    MESSAGE(STATUS "  High resolution timing supported through QueryPerformanceCounter.") +    ADD_DEFINITIONS(-DTIME_SPEC_USE_QUERY_PERFORMANCE_COUNTER) +ELSE() +    MESSAGE(STATUS "  High resolution timing supported though microsec_clock.") +    ADD_DEFINITIONS(-DTIME_SPEC_USE_MICROSEC_CLOCK) +ENDIF() + +########################################################################  # This file included, use CMake directory variables  ########################################################################  LIBUHD_APPEND_SOURCES( diff --git a/host/lib/types/time_spec.cpp b/host/lib/types/time_spec.cpp index f39625a11..ece3b92f3 100644 --- a/host/lib/types/time_spec.cpp +++ b/host/lib/types/time_spec.cpp @@ -19,6 +19,70 @@  #include <boost/math/special_functions/round.hpp>  using namespace uhd; + +/*********************************************************************** + * Time spec system time + **********************************************************************/ + +/*! + * Creates a time spec from system counts: + * TODO make part of API as a static factory function + * The counts type is 64 bits and will overflow the ticks type of long. + * Therefore, divmod the counts into seconds + sub-second counts first. + */ +#include <inttypes.h> //imaxdiv, intmax_t +static UHD_INLINE time_spec_t time_spec_t_from_counts(intmax_t counts, intmax_t freq){ +    imaxdiv_t divres = imaxdiv(counts, freq); +    return time_spec_t(time_t(divres.quot), double(divres.rem)/freq); +} + +#ifdef TIME_SPEC_USE_CLOCK_GETTIME +#include <time.h> +time_spec_t time_spec_t::get_system_time(void){ +    timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); +    return time_spec_t(ts.tv_sec, ts.tv_nsec, 1e9); +} +#endif /* TIME_SPEC_USE_CLOCK_GETTIME */ + + +#ifdef TIME_SPEC_USE_MACH_ABSOLUTE_TIME +#include <mach/mach_time.h> +time_spec_t time_spec_t::get_system_time(void){ +    mach_timebase_info_data_t info; mach_timebase_info(&info); +    intmax_t nanosecs = mach_absolute_time()*info.numer/info.denom; +    return time_spec_t_from_counts(nanosecs, intmax_t(1e9)); +} +#endif /* TIME_SPEC_USE_MACH_ABSOLUTE_TIME */ + + +#ifdef TIME_SPEC_USE_QUERY_PERFORMANCE_COUNTER +#include <Windows.h> +time_spec_t time_spec_t::get_system_time(void){ +    LARGE_INTEGER counts, freq; +    QueryPerformanceCounter(&counts); +    QueryPerformanceFrequency(&freq); +    return time_spec_t_from_counts(counts.QuadPart, freq.QuadPart); +} +#endif /* TIME_SPEC_USE_QUERY_PERFORMANCE_COUNTER */ + + +#ifdef TIME_SPEC_USE_MICROSEC_CLOCK +#include <boost/date_time/posix_time/posix_time.hpp> +namespace pt = boost::posix_time; +time_spec_t time_spec_t::get_system_time(void){ +    pt::ptime time_now = pt::microsec_clock::universal_time(); +    pt::time_duration time_dur = time_now - pt::from_time_t(0); +    return time_spec_t( +        time_t(time_dur.total_seconds()), +        long(time_dur.fractional_seconds()), +        double(pt::time_duration::ticks_per_second()) +    ); +} +#endif /* TIME_SPEC_USE_MICROSEC_CLOCK */ + +/*********************************************************************** + * Time spec constructors + **********************************************************************/  time_spec_t::time_spec_t(double secs):      _full_secs(0),      _frac_secs(secs) @@ -40,6 +104,9 @@ time_spec_t::time_spec_t(time_t full_secs, long tick_count, double tick_rate):      /* NOP */  } +/*********************************************************************** + * Time spec accessors + **********************************************************************/  long time_spec_t::get_tick_count(double tick_rate) const{      return boost::math::iround(this->get_frac_secs()*tick_rate);  } @@ -58,6 +125,9 @@ double time_spec_t::get_frac_secs(void) const{      return std::fmod(this->_frac_secs, 1.0);  } +/*********************************************************************** + * Time spec math overloads + **********************************************************************/  time_spec_t &time_spec_t::operator+=(const time_spec_t &rhs){      this->_full_secs += rhs.get_full_secs();      this->_frac_secs += rhs.get_frac_secs(); diff --git a/host/lib/usrp/usrp1/io_impl.cpp b/host/lib/usrp/usrp1/io_impl.cpp index 9fa1b4f72..52a7c6650 100644 --- a/host/lib/usrp/usrp1/io_impl.cpp +++ b/host/lib/usrp/usrp1/io_impl.cpp @@ -91,7 +91,6 @@ struct usrp1_impl::io_impl{      void commit_send_buff(offset_send_buffer::sptr, offset_send_buffer::sptr, size_t);      void flush_send_buff(void);      bool get_send_buffs(vrt_packet_handler::managed_send_buffs_t &, double); -    bool transmitting_enb;  };  /*! @@ -133,6 +132,9 @@ void usrp1_impl::io_impl::flush_send_buff(void){      //calculate the number of bytes to alignment      size_t bytes_to_pad = (-1*curr_buff->offset)%alignment_padding; +    //send at least alignment_padding to guarantee zeros are sent +    if (bytes_to_pad == 0) bytes_to_pad = alignment_padding; +      //get the buffer, clear, and commit (really current buffer)      vrt_packet_handler::managed_send_buffs_t buffs(1);      if (this->get_send_buffs(buffs, 0.1)){ @@ -190,7 +192,7 @@ void usrp1_impl::io_init(void){      );      rx_stream_on_off(false); -    tx_stream_on_off(false); +    _io_impl->flush_send_buff();  }  void usrp1_impl::rx_stream_on_off(bool enb){ @@ -201,13 +203,6 @@ void usrp1_impl::rx_stream_on_off(bool enb){      }  } -void usrp1_impl::tx_stream_on_off(bool enb){ -    if (not enb) _io_impl->flush_send_buff(); -    _codec_ctrls[DBOARD_SLOT_A]->enable_tx_digital(enb); -    _codec_ctrls[DBOARD_SLOT_B]->enable_tx_digital(enb); -    _io_impl->transmitting_enb = enb; -} -  /***********************************************************************   * Data send + helper functions   **********************************************************************/ @@ -232,7 +227,6 @@ size_t usrp1_impl::send(      send_mode_t send_mode, double timeout  ){      if (_soft_time_ctrl->send_pre(metadata, timeout)) return num_samps; -    if (not _io_impl->transmitting_enb) tx_stream_on_off(true);      size_t num_samps_sent = vrt_packet_handler::send(          _io_impl->packet_handler_send_state,       //last state of the send handler @@ -250,7 +244,7 @@ size_t usrp1_impl::send(      //handle eob flag (commit the buffer, disable the DACs)      //check num samps sent to avoid flush on incomplete/timeout      if (metadata.end_of_burst and num_samps_sent == num_samps){ -        this->tx_stream_on_off(false); +        _io_impl->flush_send_buff();      }      //handle the polling for underflow conditions diff --git a/host/lib/usrp/usrp1/soft_time_ctrl.cpp b/host/lib/usrp/usrp1/soft_time_ctrl.cpp index c91ecc7ed..246df93eb 100644 --- a/host/lib/usrp/usrp1/soft_time_ctrl.cpp +++ b/host/lib/usrp/usrp1/soft_time_ctrl.cpp @@ -18,7 +18,8 @@  #include "soft_time_ctrl.hpp"  #include <uhd/transport/bounded_buffer.hpp>  #include <boost/any.hpp> -#include <boost/thread.hpp> +#include <boost/thread/thread.hpp> +#include <boost/thread/condition_variable.hpp>  #include <boost/date_time/posix_time/posix_time.hpp>  #include <iostream> @@ -27,30 +28,7 @@ using namespace uhd::usrp;  using namespace uhd::transport;  namespace pt = boost::posix_time; -static const time_spec_t TWIDDLE(0.0015); - -/*********************************************************************** - * Utility helper functions - **********************************************************************/ - -//TODO put these in time_spec_t (maybe useful) - -static const double time_dur_tps = double(pt::time_duration::ticks_per_second()); - -time_spec_t time_dur_to_time_spec(const pt::time_duration &time_dur){ -    return time_spec_t( -        time_dur.total_seconds(), -        long(time_dur.fractional_seconds()), -        time_dur_tps -    ); -} - -pt::time_duration time_spec_to_time_dur(const time_spec_t &time_spec){ -    return pt::time_duration( -        0, 0, long(time_spec.get_full_secs()), -        time_spec.get_tick_count(time_dur_tps) -    ); -} +static const time_spec_t TWIDDLE(0.0011);  /***********************************************************************   * Soft time control implementation @@ -84,7 +62,7 @@ public:       ******************************************************************/      void set_time(const time_spec_t &time){          boost::mutex::scoped_lock lock(_update_mutex); -        _time_offset = boost::get_system_time() - time_spec_to_time_dur(time); +        _time_offset = time_spec_t::get_system_time() - time;      }      time_spec_t get_time(void){ @@ -94,7 +72,7 @@ public:      UHD_INLINE time_spec_t time_now(void){          //internal get time without scoped lock -        return time_dur_to_time_spec(boost::get_system_time() - _time_offset); +        return time_spec_t::get_system_time() - _time_offset;      }      UHD_INLINE void sleep_until_time( @@ -102,7 +80,8 @@ public:      ){          boost::condition_variable cond;          //use a condition variable to unlock, sleep, lock -        cond.timed_wait(lock, _time_offset + time_spec_to_time_dur(time)); +        double seconds_to_sleep = (time - time_now()).get_real_secs(); +        cond.timed_wait(lock, pt::microseconds(long(seconds_to_sleep*1e6)));      }      /******************************************************************* @@ -211,7 +190,7 @@ private:      boost::mutex _update_mutex;      size_t _nsamps_remaining;      stream_cmd_t::stream_mode_t _stream_mode; -    pt::ptime _time_offset; +    time_spec_t _time_offset;      bounded_buffer<boost::any>::sptr _cmd_queue;      const cb_fcn_type _stream_on_off;      boost::thread_group _thread_group; diff --git a/host/lib/usrp/usrp1/usrp1_impl.hpp b/host/lib/usrp/usrp1/usrp1_impl.hpp index 057725394..28199ebe3 100644 --- a/host/lib/usrp/usrp1/usrp1_impl.hpp +++ b/host/lib/usrp/usrp1/usrp1_impl.hpp @@ -125,7 +125,6 @@ private:      UHD_PIMPL_DECL(io_impl) _io_impl;      void io_init(void);      void rx_stream_on_off(bool); -    void tx_stream_on_off(bool);      void handle_overrun(size_t);      //underrun and overrun poll intervals diff --git a/host/tests/time_spec_test.cpp b/host/tests/time_spec_test.cpp index 5ad782160..070392f93 100644 --- a/host/tests/time_spec_test.cpp +++ b/host/tests/time_spec_test.cpp @@ -18,6 +18,7 @@  #include <boost/test/unit_test.hpp>  #include <uhd/types/time_spec.hpp>  #include <boost/foreach.hpp> +#include <boost/thread.hpp> //sleep  #include <iostream>  BOOST_AUTO_TEST_CASE(test_time_spec_compare){ @@ -59,3 +60,21 @@ BOOST_AUTO_TEST_CASE(test_time_spec_parts){      BOOST_CHECK_CLOSE(uhd::time_spec_t(-1.1).get_frac_secs(), -0.1, 0.001);      BOOST_CHECK_EQUAL(uhd::time_spec_t(-1.1).get_tick_count(100), -10);  } + +BOOST_AUTO_TEST_CASE(test_time_spec_get_system_time){ +    std::cout << "Testing time specification get system time..." << std::endl; + +    //Not really checking for high resolution timing here, +    //just need to check that system time is minimally working. + +    uhd::time_spec_t start = uhd::time_spec_t::get_system_time(); +    boost::this_thread::sleep(boost::posix_time::milliseconds(500)); +    uhd::time_spec_t stop = uhd::time_spec_t::get_system_time(); + +    uhd::time_spec_t diff = stop - start; +    std::cout << "start: " << start.get_real_secs() << std::endl; +    std::cout << "stop: " << stop.get_real_secs() << std::endl; +    std::cout << "diff: " << diff.get_real_secs() << std::endl; +    BOOST_CHECK(diff.get_real_secs() > 0); //assert positive +    BOOST_CHECK(diff.get_real_secs() < 1.0); //assert under 1s +} diff --git a/host/utils/usrp_n2xx_net_burner.py b/host/utils/usrp_n2xx_net_burner.py index 21327e0af..f52a2cbc1 100755 --- a/host/utils/usrp_n2xx_net_burner.py +++ b/host/utils/usrp_n2xx_net_burner.py @@ -18,6 +18,7 @@  # TODO: make it autodetect UHD devices  # TODO: you should probably watch sequence numbers +# TODO: validate images in 1) size and 2) header content so you can't write a Justin Bieber MP3 to Flash  import optparse  import math @@ -35,7 +36,7 @@ UDP_MAX_XFER_BYTES = 1024  UDP_TIMEOUT = 3  UDP_POLL_INTERVAL = 0.10 #in seconds -USRP2_FW_PROTO_VERSION = 7 +USRP2_FW_PROTO_VERSION = 7 #should be unused after r6  #from bootloader_utils.h @@ -144,6 +145,22 @@ def get_flash_info(ip):    return (memory_size_bytes, sector_size_bytes) +   +def is_valid_fpga_image(fpga_image): +    for i in range(0,63): +      if ord(fpga_image[i]) == 0xFF: +        continue +      if ord(fpga_image[i]) == 0xAA and ord(fpga_image[i+1]) == 0x99: +        return 1 + +    return 0 +     +def is_valid_fw_image(fw_image): +    for i in range(0,4): +      if ord(fw_image[i]) != 0x0B: +          return 0; + +    return 1  def burn_fw(ip, fw, fpga, reset, safe):    init_update(ip) @@ -159,6 +176,15 @@ def burn_fw(ip, fw, fpga, reset, safe):      fpga_file = open(fpga, 'rb')      fpga_image = fpga_file.read() +     +    if len(fpga_image) > FPGA_IMAGE_SIZE_BYTES: +        print "Error: FPGA image file too large." +        return 0 +     +    if not is_valid_fpga_image(fpga_image): +        print "Error: Invalid FPGA image file." +        return 0 +          erase_image(ip, image_location, FPGA_IMAGE_SIZE_BYTES)      write_image(ip, fpga_image, image_location)      verify_image(ip, fpga_image, image_location) @@ -171,6 +197,15 @@ def burn_fw(ip, fw, fpga, reset, safe):      fw_file = open(fw, 'rb')      fw_image = fw_file.read() +     +    if len(fw_image) > FW_IMAGE_SIZE_BYTES: +        print "Error: Firmware image file too large." +        return 0 +     +    if not is_valid_fw_image(fw_image): +        print "Error: Invalid firmware image file." +        return 0     +          erase_image(ip, image_location, FW_IMAGE_SIZE_BYTES)      write_image(ip, fw_image, image_location)      verify_image(ip, fw_image, image_location)  | 
