diff options
Diffstat (limited to 'host/lib')
-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 |
5 files changed, 139 insertions, 41 deletions
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 |