diff options
Diffstat (limited to 'host/lib/usrp/b200')
-rw-r--r-- | host/lib/usrp/b200/CMakeLists.txt | 1 | ||||
-rw-r--r-- | host/lib/usrp/b200/b200_impl.cpp | 8 | ||||
-rw-r--r-- | host/lib/usrp/b200/b200_impl.hpp | 14 | ||||
-rw-r--r-- | host/lib/usrp/b200/b200_io_impl.cpp | 2 | ||||
-rw-r--r-- | host/lib/usrp/b200/b200_radio_ctrl_core.cpp | 349 | ||||
-rw-r--r-- | host/lib/usrp/b200/b200_radio_ctrl_core.hpp | 48 |
6 files changed, 410 insertions, 12 deletions
diff --git a/host/lib/usrp/b200/CMakeLists.txt b/host/lib/usrp/b200/CMakeLists.txt index 9ac732c02..3dd26774b 100644 --- a/host/lib/usrp/b200/CMakeLists.txt +++ b/host/lib/usrp/b200/CMakeLists.txt @@ -21,5 +21,6 @@ if(ENABLE_B200) ${CMAKE_CURRENT_SOURCE_DIR}/b200_uart.cpp ${CMAKE_CURRENT_SOURCE_DIR}/b200_cores.cpp ${CMAKE_CURRENT_SOURCE_DIR}/b200_mb_eeprom.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/b200_radio_ctrl_core.cpp ) endif(ENABLE_B200) diff --git a/host/lib/usrp/b200/b200_impl.cpp b/host/lib/usrp/b200/b200_impl.cpp index 65d009e61..2e2ed5a98 100644 --- a/host/lib/usrp/b200/b200_impl.cpp +++ b/host/lib/usrp/b200/b200_impl.cpp @@ -501,7 +501,7 @@ b200_impl::b200_impl( //////////////////////////////////////////////////////////////////// // Local control endpoint //////////////////////////////////////////////////////////////////// - _local_ctrl = radio_ctrl_core_3000::make(false /*lilE*/, + _local_ctrl = b200_radio_ctrl_core::make(false /*lilE*/, _ctrl_transport, zero_copy_if::sptr() /*null*/, B200_LOCAL_CTRL_SID); @@ -868,16 +868,16 @@ void b200_impl::setup_radio(const size_t dspno) //////////////////////////////////////////////////////////////////// // radio control //////////////////////////////////////////////////////////////////// - perif.ctrl = radio_ctrl_core_3000::make( + perif.ctrl = b200_radio_ctrl_core::make( false /*lilE*/, _ctrl_transport, zero_copy_if::sptr() /*null*/, sid); perif.ctrl->hold_task(_async_task); _async_task_data->radio_ctrl[dspno] = perif.ctrl; // weak _tree->access<time_spec_t>(mb_path / "time" / "cmd") .add_coerced_subscriber(std::bind( - &radio_ctrl_core_3000::set_time, perif.ctrl, std::placeholders::_1)); + &b200_radio_ctrl_core::set_time, perif.ctrl, std::placeholders::_1)); _tree->access<double>(mb_path / "tick_rate") .add_coerced_subscriber(std::bind( - &radio_ctrl_core_3000::set_tick_rate, perif.ctrl, std::placeholders::_1)); + &b200_radio_ctrl_core::set_tick_rate, perif.ctrl, std::placeholders::_1)); this->register_loopback_self_test(perif.ctrl); //////////////////////////////////////////////////////////////////// diff --git a/host/lib/usrp/b200/b200_impl.hpp b/host/lib/usrp/b200/b200_impl.hpp index 44205f03a..b280bcc87 100644 --- a/host/lib/usrp/b200/b200_impl.hpp +++ b/host/lib/usrp/b200/b200_impl.hpp @@ -10,6 +10,7 @@ #include "b200_cores.hpp" #include "b200_iface.hpp" +#include "b200_radio_ctrl_core.hpp" #include "b200_uart.hpp" #include <uhd/device.hpp> #include <uhd/property_tree.hpp> @@ -26,20 +27,19 @@ #include <uhdlib/usrp/common/ad9361_ctrl.hpp> #include <uhdlib/usrp/common/ad936x_manager.hpp> #include <uhdlib/usrp/common/adf4001_ctrl.hpp> +#include <uhdlib/usrp/common/pwr_cal_mgr.hpp> #include <uhdlib/usrp/common/recv_packet_demuxer_3000.hpp> #include <uhdlib/usrp/cores/gpio_atr_3000.hpp> -#include <uhdlib/usrp/cores/radio_ctrl_core_3000.hpp> #include <uhdlib/usrp/cores/rx_dsp_core_3000.hpp> #include <uhdlib/usrp/cores/rx_vita_core_3000.hpp> #include <uhdlib/usrp/cores/time_core_3000.hpp> #include <uhdlib/usrp/cores/tx_dsp_core_3000.hpp> #include <uhdlib/usrp/cores/tx_vita_core_3000.hpp> #include <uhdlib/usrp/cores/user_settings_core_3000.hpp> -#include <uhdlib/usrp/common/pwr_cal_mgr.hpp> +#include <unordered_map> #include <boost/assign.hpp> #include <memory> #include <mutex> -#include <unordered_map> static const uint8_t B200_FW_COMPAT_NUM_MAJOR = 8; static const uint8_t B200_FW_COMPAT_NUM_MINOR = 0; @@ -143,7 +143,7 @@ private: // controllers b200_iface::sptr _iface; - radio_ctrl_core_3000::sptr _local_ctrl; + b200_radio_ctrl_core::sptr _local_ctrl; uhd::usrp::ad9361_ctrl::sptr _codec_ctrl; uhd::usrp::ad936x_manager::sptr _codec_mgr; b200_local_spi_core::sptr _spi_iface; @@ -166,8 +166,8 @@ private: struct AsyncTaskData { std::shared_ptr<async_md_type> async_md; - std::weak_ptr<radio_ctrl_core_3000> local_ctrl; - std::weak_ptr<radio_ctrl_core_3000> radio_ctrl[2]; + std::weak_ptr<b200_radio_ctrl_core> local_ctrl; + std::weak_ptr<b200_radio_ctrl_core> radio_ctrl[2]; b200_uart::sptr gpsdo_uart; }; std::shared_ptr<AsyncTaskData> _async_task_data; @@ -193,7 +193,7 @@ private: // perifs in the radio core struct radio_perifs_t { - radio_ctrl_core_3000::sptr ctrl; + b200_radio_ctrl_core::sptr ctrl; uhd::usrp::gpio_atr::gpio_atr_3000::sptr atr; uhd::usrp::gpio_atr::gpio_atr_3000::sptr fp_gpio; time_core_3000::sptr time64; diff --git a/host/lib/usrp/b200/b200_io_impl.cpp b/host/lib/usrp/b200/b200_io_impl.cpp index a20709497..2433ab2db 100644 --- a/host/lib/usrp/b200/b200_io_impl.cpp +++ b/host/lib/usrp/b200/b200_io_impl.cpp @@ -327,7 +327,7 @@ boost::optional<uhd::msg_task::msg_type_t> b200_impl::handle_async_task( case B200_RESP0_MSG_SID: case B200_RESP1_MSG_SID: case B200_LOCAL_RESP_SID: { - radio_ctrl_core_3000::sptr ctrl; + b200_radio_ctrl_core::sptr ctrl; if (sid == B200_RESP0_MSG_SID) ctrl = data->radio_ctrl[0].lock(); if (sid == B200_RESP1_MSG_SID) diff --git a/host/lib/usrp/b200/b200_radio_ctrl_core.cpp b/host/lib/usrp/b200/b200_radio_ctrl_core.cpp new file mode 100644 index 000000000..e885bf1b6 --- /dev/null +++ b/host/lib/usrp/b200/b200_radio_ctrl_core.cpp @@ -0,0 +1,349 @@ +// +// Copyright 2012-2015 Ettus Research LLC +// Copyright 2018 Ettus Research, a National Instruments Company +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include "b200_radio_ctrl_core.hpp" +#include <uhd/exception.hpp> +#include <uhd/transport/bounded_buffer.hpp> +#include <uhd/transport/vrt_if_packet.hpp> +#include <uhd/utils/byteswap.hpp> +#include <uhd/utils/log.hpp> +#include <uhd/utils/safe_call.hpp> +#include <uhdlib/usrp/common/async_packet_handler.hpp> +#include <boost/format.hpp> +#include <boost/thread/mutex.hpp> +#include <boost/thread/thread.hpp> +#include <functional> +#include <queue> + +using namespace uhd; +using namespace uhd::usrp; +using namespace uhd::transport; + +static const double ACK_TIMEOUT = 2.0; // supposed to be worst case practical timeout +static const double MASSIVE_TIMEOUT = 10.0; // for when we wait on a timed command +static const size_t SR_READBACK = 32; + +b200_radio_ctrl_core::~b200_radio_ctrl_core(void) +{ + /* NOP */ +} + +class b200_radio_ctrl_core_impl : public b200_radio_ctrl_core +{ +public: + b200_radio_ctrl_core_impl(const bool big_endian, + uhd::transport::zero_copy_if::sptr ctrl_xport, + uhd::transport::zero_copy_if::sptr resp_xport, + const uint32_t sid, + const std::string& name) + : _link_type(vrt::if_packet_info_t::LINK_TYPE_CHDR) + , _packet_type(vrt::if_packet_info_t::PACKET_TYPE_CONTEXT) + , _bige(big_endian) + , _ctrl_xport(ctrl_xport) + , _resp_xport(resp_xport) + , _sid(sid) + , _name(name) + , _seq_out(0) + , _timeout(ACK_TIMEOUT) + , _resp_queue(128 /*max response msgs*/) + , _resp_queue_size(_resp_xport ? _resp_xport->get_num_recv_frames() : 3) + { + if (resp_xport) { + while (resp_xport->get_recv_buff(0.0)) { + } // flush + } + this->set_time(uhd::time_spec_t(0.0)); + this->set_tick_rate(1.0); // something possible but bogus + } + + ~b200_radio_ctrl_core_impl(void) override + { + _timeout = ACK_TIMEOUT; // reset timeout to something small + UHD_SAFE_CALL( + this->peek32(0); // dummy peek with the purpose of ack'ing all packets + _async_task.reset(); // now its ok to release the task + ) + } + + /******************************************************************* + * Peek and poke 32 bit implementation + ******************************************************************/ + void poke32(const wb_addr_type addr, const uint32_t data) override + { + boost::mutex::scoped_lock lock(_mutex); + this->send_pkt(addr / 4, data); + this->wait_for_ack(false); + } + + uint32_t peek32(const wb_addr_type addr) override + { + boost::mutex::scoped_lock lock(_mutex); + this->send_pkt(SR_READBACK, addr / 8); + const uint64_t res = this->wait_for_ack(true); + const uint32_t lo = uint32_t(res & 0xffffffff); + const uint32_t hi = uint32_t(res >> 32); + return ((addr / 4) & 0x1) ? hi : lo; + } + + uint64_t peek64(const wb_addr_type addr) override + { + boost::mutex::scoped_lock lock(_mutex); + this->send_pkt(SR_READBACK, addr / 8); + return this->wait_for_ack(true); + } + + /******************************************************************* + * Update methods for time + ******************************************************************/ + void set_time(const uhd::time_spec_t& time) override + { + boost::mutex::scoped_lock lock(_mutex); + _time = time; + _use_time = _time != uhd::time_spec_t(0.0); + if (_use_time) + _timeout = MASSIVE_TIMEOUT; // permanently sets larger timeout + } + + uhd::time_spec_t get_time(void) override + { + boost::mutex::scoped_lock lock(_mutex); + return _time; + } + + void set_tick_rate(const double rate) override + { + boost::mutex::scoped_lock lock(_mutex); + _tick_rate = rate; + } + +private: + // This is the buffer type for messages in radio control core. + struct resp_buff_type + { + uint32_t data[8]; + }; + + /******************************************************************* + * Primary control and interaction private methods + ******************************************************************/ + UHD_INLINE void send_pkt(const uint32_t addr, const uint32_t data = 0) + { + managed_send_buffer::sptr buff = _ctrl_xport->get_send_buff(0.0); + if (not buff) { + throw uhd::runtime_error("fifo ctrl timed out getting a send buffer"); + } + uint32_t* pkt = buff->cast<uint32_t*>(); + + // load packet info + vrt::if_packet_info_t packet_info; + packet_info.link_type = _link_type; + packet_info.packet_type = _packet_type; + packet_info.num_payload_words32 = 2; + packet_info.num_payload_bytes = + packet_info.num_payload_words32 * sizeof(uint32_t); + packet_info.packet_count = _seq_out; + packet_info.tsf = _time.to_ticks(_tick_rate); + packet_info.sob = false; + packet_info.eob = false; + packet_info.sid = _sid; + packet_info.has_sid = true; + packet_info.has_cid = false; + packet_info.has_tsi = false; + packet_info.has_tsf = _use_time; + packet_info.has_tlr = false; + + // load header + if (_bige) + vrt::if_hdr_pack_be(pkt, packet_info); + else + vrt::if_hdr_pack_le(pkt, packet_info); + + // load payload + pkt[packet_info.num_header_words32 + 0] = (_bige) ? uhd::htonx(addr) + : uhd::htowx(addr); + pkt[packet_info.num_header_words32 + 1] = (_bige) ? uhd::htonx(data) + : uhd::htowx(data); + // UHD_LOGGER_INFO("radio_ctrl") << boost::format("0x%08x, 0x%08x\n") % addr % + // data; send the buffer over the interface + _outstanding_seqs.push(_seq_out); + buff->commit(sizeof(uint32_t) * (packet_info.num_packet_words32)); + + _seq_out++; // inc seq for next call + } + + UHD_INLINE uint64_t wait_for_ack(const bool readback) + { + while (readback or (_outstanding_seqs.size() >= _resp_queue_size)) { + // get seq to ack from outstanding packets list + UHD_ASSERT_THROW(not _outstanding_seqs.empty()); + const size_t seq_to_ack = _outstanding_seqs.front(); + _outstanding_seqs.pop(); + + // parse the packet + vrt::if_packet_info_t packet_info; + resp_buff_type resp_buff; + memset(&resp_buff, 0x00, sizeof(resp_buff)); + uint32_t const* pkt = NULL; + managed_recv_buffer::sptr buff; + + // get buffer from response endpoint - or die in timeout + if (_resp_xport) { + buff = _resp_xport->get_recv_buff(_timeout); + try { + UHD_ASSERT_THROW(bool(buff)); + UHD_ASSERT_THROW(buff->size() > 0); + } catch (const std::exception& ex) { + throw uhd::io_error( + str(boost::format("Radio ctrl (%s) no response packet - %s") + % _name % ex.what())); + } + pkt = buff->cast<const uint32_t*>(); + packet_info.num_packet_words32 = buff->size() / sizeof(uint32_t); + } + + // get buffer from response endpoint - or die in timeout + else { + /* + * Couldn't get message with haste. + * Now check both possible queues for messages. + * Messages should come in on _resp_queue, + * but could end up in dump_queue. + * If we don't get a message --> Die in timeout. + */ + double accum_timeout = 0.0; + const double short_timeout = 0.005; // == 5ms + while ( + not((_resp_queue.pop_with_haste(resp_buff)) + || (check_dump_queue(resp_buff)) + || (_resp_queue.pop_with_timed_wait(resp_buff, short_timeout)))) { + /* + * If a message couldn't be received within a given timeout + * --> throw AssertionError! + */ + accum_timeout += short_timeout; + UHD_ASSERT_THROW(accum_timeout < _timeout); + } + + pkt = resp_buff.data; + packet_info.num_packet_words32 = sizeof(resp_buff) / sizeof(uint32_t); + } + + // parse the buffer + try { + packet_info.link_type = _link_type; + if (_bige) + vrt::if_hdr_unpack_be(pkt, packet_info); + else + vrt::if_hdr_unpack_le(pkt, packet_info); + } catch (const std::exception& ex) { + UHD_LOGGER_ERROR("radio_ctrl") + << "Radio ctrl bad VITA packet: " << ex.what(); + if (buff) { + UHD_VAR(buff->size()); + } else { + UHD_LOGGER_INFO("radio_ctrl") << "buff is NULL"; + } + UHD_LOGGER_INFO("radio_ctrl") << std::hex << pkt[0] << std::dec; + UHD_LOGGER_INFO("radio_ctrl") << std::hex << pkt[1] << std::dec; + UHD_LOGGER_INFO("radio_ctrl") << std::hex << pkt[2] << std::dec; + UHD_LOGGER_INFO("radio_ctrl") << std::hex << pkt[3] << std::dec; + } + + // check the buffer + try { + UHD_ASSERT_THROW(packet_info.has_sid); + UHD_ASSERT_THROW( + packet_info.sid == uint32_t((_sid >> 16) | (_sid << 16))); + UHD_ASSERT_THROW(packet_info.packet_count == (seq_to_ack & 0xfff)); + UHD_ASSERT_THROW(packet_info.num_payload_words32 == 2); + UHD_ASSERT_THROW(packet_info.packet_type == _packet_type); + } catch (const std::exception& ex) { + throw uhd::io_error( + str(boost::format("Radio ctrl (%s) packet parse error - %s") % _name + % ex.what())); + } + + // return the readback value + if (readback and _outstanding_seqs.empty()) { + const uint64_t hi = + (_bige) ? uhd::ntohx(pkt[packet_info.num_header_words32 + 0]) + : uhd::wtohx(pkt[packet_info.num_header_words32 + 0]); + const uint64_t lo = + (_bige) ? uhd::ntohx(pkt[packet_info.num_header_words32 + 1]) + : uhd::wtohx(pkt[packet_info.num_header_words32 + 1]); + return ((hi << 32) | lo); + } + } + + return 0; + } + + /* + * If ctrl_core waits for a message that didn't arrive it can search for it in the + * dump queue. This actually happens during shutdown. handle_async_task can't access + * radio_ctrl_cores queue anymore thus it returns the corresponding message. msg_task + * class implements a dump_queue to store such messages. With check_dump_queue we can + * check if a message we are waiting for got stranded there. If a message got stuck we + * get it here and push it onto our own message_queue. + */ + bool check_dump_queue(resp_buff_type& b) + { + const size_t min_buff_size = + 8; // Same value as in b200_io_impl->handle_async_task + uint32_t recv_sid = (((_sid) << 16) | ((_sid) >> 16)); + uhd::msg_task::msg_payload_t msg; + do { + msg = _async_task->get_msg_from_dump_queue(recv_sid); + } while (msg.size() < min_buff_size && !msg.empty()); + + if (msg.size() >= min_buff_size) { + memcpy(b.data, &msg.front(), std::min(msg.size(), sizeof(b.data))); + return true; + } + return false; + } + + void push_response(const uint32_t* buff) override + { + resp_buff_type resp_buff; + std::memcpy(resp_buff.data, buff, sizeof(resp_buff)); + _resp_queue.push_with_haste(resp_buff); + } + + void hold_task(uhd::msg_task::sptr task) override + { + _async_task = task; + } + + const vrt::if_packet_info_t::link_type_t _link_type; + const vrt::if_packet_info_t::packet_type_t _packet_type; + const bool _bige; + const uhd::transport::zero_copy_if::sptr _ctrl_xport; + const uhd::transport::zero_copy_if::sptr _resp_xport; + uhd::msg_task::sptr _async_task; + const uint32_t _sid; + const std::string _name; + boost::mutex _mutex; + size_t _seq_out; + uhd::time_spec_t _time; + bool _use_time; + double _tick_rate; + double _timeout; + std::queue<size_t> _outstanding_seqs; + bounded_buffer<resp_buff_type> _resp_queue; + const size_t _resp_queue_size; +}; + +b200_radio_ctrl_core::sptr b200_radio_ctrl_core::make(const bool big_endian, + zero_copy_if::sptr ctrl_xport, + zero_copy_if::sptr resp_xport, + const uint32_t sid, + const std::string& name) +{ + return sptr( + new b200_radio_ctrl_core_impl(big_endian, ctrl_xport, resp_xport, sid, name)); +} diff --git a/host/lib/usrp/b200/b200_radio_ctrl_core.hpp b/host/lib/usrp/b200/b200_radio_ctrl_core.hpp new file mode 100644 index 000000000..06f2c9fbf --- /dev/null +++ b/host/lib/usrp/b200/b200_radio_ctrl_core.hpp @@ -0,0 +1,48 @@ +// +// Copyright 2012-2015 Ettus Research LLC +// Copyright 2018 Ettus Research, a National Instruments Company +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#pragma once + +#include <uhd/transport/zero_copy.hpp> +#include <uhd/types/time_spec.hpp> +#include <uhd/types/wb_iface.hpp> +#include <uhd/utils/msg_task.hpp> +#include <memory> +#include <string> + +/*! + * Provide access to peek, poke for the radio ctrl module + */ +class b200_radio_ctrl_core : public uhd::timed_wb_iface +{ +public: + typedef std::shared_ptr<b200_radio_ctrl_core> sptr; + + ~b200_radio_ctrl_core(void) override = 0; + + //! Make a new control object + static sptr make(const bool big_endian, + uhd::transport::zero_copy_if::sptr ctrl_xport, + uhd::transport::zero_copy_if::sptr resp_xport, + const uint32_t sid, + const std::string& name = "0"); + + //! Hold a ref to a task thats feeding push response + virtual void hold_task(uhd::msg_task::sptr task) = 0; + + //! Push a response externall (resp_xport is NULL) + virtual void push_response(const uint32_t* buff) = 0; + + //! Set the command time that will activate + void set_time(const uhd::time_spec_t& time) override = 0; + + //! Get the command time that will activate + uhd::time_spec_t get_time(void) override = 0; + + //! Set the tick rate (converting time into ticks) + virtual void set_tick_rate(const double rate) = 0; +}; |