diff options
-rw-r--r-- | host/docs/usrp1.rst | 1 | ||||
-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 |
9 files changed, 202 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/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) |