2012-03-01 00:28:56 +00:00
|
|
|
//
|
|
|
|
|
// Copyright 2012 Ettus Research LLC
|
2018-02-19 23:30:32 +00:00
|
|
|
// Copyright 2018 Ettus Research, a National Instruments Company
|
2012-03-01 00:28:56 +00:00
|
|
|
//
|
2018-02-19 23:30:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2012-03-01 00:28:56 +00:00
|
|
|
//
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
#include "usrp2_fifo_ctrl.hpp"
|
2012-03-01 23:59:40 +00:00
|
|
|
#include "usrp2_regs.hpp"
|
2012-03-01 00:28:56 +00:00
|
|
|
#include <uhd/exception.hpp>
|
2020-03-02 23:25:13 +00:00
|
|
|
#include <uhd/transport/vrt_if_packet.hpp>
|
2017-02-08 00:37:25 +00:00
|
|
|
#include <uhd/utils/log.hpp>
|
2012-03-07 02:51:31 +00:00
|
|
|
#include <uhd/utils/safe_call.hpp>
|
2012-03-01 00:28:56 +00:00
|
|
|
#include <boost/asio.hpp> //htonl
|
2012-03-02 03:58:19 +00:00
|
|
|
#include <boost/format.hpp>
|
2020-03-02 23:25:13 +00:00
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
|
#include <boost/thread/thread.hpp>
|
2012-03-01 00:28:56 +00:00
|
|
|
|
2012-03-07 00:32:58 +00:00
|
|
|
using namespace uhd;
|
2012-03-01 00:28:56 +00:00
|
|
|
using namespace uhd::transport;
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
static const size_t POKE32_CMD = (1 << 8);
|
|
|
|
|
static const size_t PEEK32_CMD = 0;
|
2012-03-01 00:28:56 +00:00
|
|
|
static const double ACK_TIMEOUT = 0.5;
|
2020-03-02 23:25:13 +00:00
|
|
|
static const double MASSIVE_TIMEOUT = 10.0; // for when we wait on a timed command
|
2016-10-31 21:30:52 +00:00
|
|
|
static const uint32_t MAX_SEQS_OUT = 63;
|
2012-03-01 00:28:56 +00:00
|
|
|
|
2012-03-07 00:32:58 +00:00
|
|
|
#define SPI_DIV SR_SPI_CORE + 0
|
|
|
|
|
#define SPI_CTRL SR_SPI_CORE + 1
|
|
|
|
|
#define SPI_DATA SR_SPI_CORE + 2
|
|
|
|
|
#define SPI_READBACK 0
|
|
|
|
|
// spi clock rate = master_clock/(div+1)/2 (10MHz in this case)
|
|
|
|
|
#define SPI_DIVIDER 4
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
class usrp2_fifo_ctrl_impl : public usrp2_fifo_ctrl
|
|
|
|
|
{
|
2012-03-01 00:28:56 +00:00
|
|
|
public:
|
2020-03-02 23:25:13 +00:00
|
|
|
usrp2_fifo_ctrl_impl(zero_copy_if::sptr xport)
|
|
|
|
|
: _xport(xport), _seq_out(0), _seq_ack(0), _timeout(ACK_TIMEOUT)
|
2012-03-01 00:28:56 +00:00
|
|
|
{
|
2020-03-02 23:25:13 +00:00
|
|
|
while (_xport->get_recv_buff(0.0)) {
|
|
|
|
|
} // flush
|
2012-03-02 02:35:12 +00:00
|
|
|
this->set_time(uhd::time_spec_t(0.0));
|
2020-03-02 23:25:13 +00:00
|
|
|
this->set_tick_rate(1.0); // something possible but bogus
|
2012-03-07 00:32:58 +00:00
|
|
|
this->init_spi();
|
2012-03-01 00:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
~usrp2_fifo_ctrl_impl(void)
|
|
|
|
|
{
|
|
|
|
|
_timeout = ACK_TIMEOUT; // reset timeout to something small
|
2012-03-07 02:51:31 +00:00
|
|
|
UHD_SAFE_CALL(
|
2020-03-02 23:25:13 +00:00
|
|
|
this->peek32(0); // dummy peek with the purpose of ack'ing all packets
|
2012-03-07 02:51:31 +00:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-13 20:27:48 +00:00
|
|
|
/*******************************************************************
|
|
|
|
|
* Peek and poke 32 bit implementation
|
|
|
|
|
******************************************************************/
|
2020-03-02 23:25:13 +00:00
|
|
|
void poke32(const wb_addr_type addr, const uint32_t data)
|
|
|
|
|
{
|
2012-03-01 00:28:56 +00:00
|
|
|
boost::mutex::scoped_lock lock(_mutex);
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
this->send_pkt((addr - SETTING_REGS_BASE) / 4, data, POKE32_CMD);
|
2012-03-01 00:28:56 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
this->wait_for_ack(_seq_out - MAX_SEQS_OUT);
|
2012-03-01 00:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
uint32_t peek32(const wb_addr_type addr)
|
|
|
|
|
{
|
2012-03-01 00:28:56 +00:00
|
|
|
boost::mutex::scoped_lock lock(_mutex);
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
this->send_pkt((addr - READBACK_BASE) / 4, 0, PEEK32_CMD);
|
2012-03-01 00:28:56 +00:00
|
|
|
|
2012-03-13 20:27:48 +00:00
|
|
|
return this->wait_for_ack(_seq_out);
|
2012-03-01 00:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-13 20:27:48 +00:00
|
|
|
/*******************************************************************
|
|
|
|
|
* Peek and poke 16 bit not implemented
|
|
|
|
|
******************************************************************/
|
2020-03-02 23:25:13 +00:00
|
|
|
void poke16(const wb_addr_type, const uint16_t)
|
|
|
|
|
{
|
2012-03-01 00:28:56 +00:00
|
|
|
throw uhd::not_implemented_error("poke16 not implemented in fifo ctrl module");
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
uint16_t peek16(const wb_addr_type)
|
|
|
|
|
{
|
2012-03-01 00:28:56 +00:00
|
|
|
throw uhd::not_implemented_error("peek16 not implemented in fifo ctrl module");
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-13 20:27:48 +00:00
|
|
|
/*******************************************************************
|
|
|
|
|
* FIFO controlled SPI implementation
|
|
|
|
|
******************************************************************/
|
2020-03-02 23:25:13 +00:00
|
|
|
void init_spi(void)
|
|
|
|
|
{
|
2012-03-07 00:32:58 +00:00
|
|
|
boost::mutex::scoped_lock lock(_mutex);
|
|
|
|
|
|
2012-03-08 03:15:10 +00:00
|
|
|
this->send_pkt(SPI_DIV, SPI_DIVIDER, POKE32_CMD);
|
2020-03-02 23:25:13 +00:00
|
|
|
this->wait_for_ack(_seq_out - MAX_SEQS_OUT);
|
2012-03-07 00:32:58 +00:00
|
|
|
|
|
|
|
|
_ctrl_word_cache = 0; // force update first time around
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
uint32_t transact_spi(int which_slave,
|
|
|
|
|
const spi_config_t& config,
|
2016-10-31 21:30:52 +00:00
|
|
|
uint32_t data,
|
2012-03-07 00:32:58 +00:00
|
|
|
size_t num_bits,
|
2020-03-02 23:25:13 +00:00
|
|
|
bool readback)
|
|
|
|
|
{
|
2012-03-07 00:32:58 +00:00
|
|
|
boost::mutex::scoped_lock lock(_mutex);
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// load control word
|
2016-10-31 21:30:52 +00:00
|
|
|
uint32_t ctrl_word = 0;
|
2012-03-07 00:32:58 +00:00
|
|
|
ctrl_word |= ((which_slave & 0xffffff) << 0);
|
|
|
|
|
ctrl_word |= ((num_bits & 0x3ff) << 24);
|
2020-03-02 23:25:13 +00:00
|
|
|
if (config.mosi_edge == spi_config_t::EDGE_FALL)
|
|
|
|
|
ctrl_word |= (1 << 31);
|
|
|
|
|
if (config.miso_edge == spi_config_t::EDGE_RISE)
|
|
|
|
|
ctrl_word |= (1 << 30);
|
2012-03-07 00:32:58 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// load data word (must be in upper bits)
|
2016-10-31 21:30:52 +00:00
|
|
|
const uint32_t data_out = data << (32 - num_bits);
|
2012-03-07 00:32:58 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// conditionally send control word
|
|
|
|
|
if (_ctrl_word_cache != ctrl_word) {
|
2012-03-08 03:15:10 +00:00
|
|
|
this->send_pkt(SPI_CTRL, ctrl_word, POKE32_CMD);
|
2020-03-02 23:25:13 +00:00
|
|
|
this->wait_for_ack(_seq_out - MAX_SEQS_OUT);
|
2012-03-07 00:32:58 +00:00
|
|
|
_ctrl_word_cache = ctrl_word;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// send data word
|
2012-03-08 03:15:10 +00:00
|
|
|
this->send_pkt(SPI_DATA, data_out, POKE32_CMD);
|
2020-03-02 23:25:13 +00:00
|
|
|
this->wait_for_ack(_seq_out - MAX_SEQS_OUT);
|
2012-03-07 00:32:58 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// conditional readback
|
|
|
|
|
if (readback) {
|
2012-03-08 03:15:10 +00:00
|
|
|
this->send_pkt(SPI_READBACK, 0, PEEK32_CMD);
|
2012-03-13 20:27:48 +00:00
|
|
|
return this->wait_for_ack(_seq_out);
|
2012-03-07 00:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-13 20:27:48 +00:00
|
|
|
/*******************************************************************
|
|
|
|
|
* Update methods for time
|
|
|
|
|
******************************************************************/
|
2020-03-02 23:25:13 +00:00
|
|
|
void set_time(const uhd::time_spec_t& time)
|
|
|
|
|
{
|
2012-03-02 02:35:12 +00:00
|
|
|
boost::mutex::scoped_lock lock(_mutex);
|
2020-03-02 23:25:13 +00:00
|
|
|
_time = time;
|
2012-03-02 02:35:12 +00:00
|
|
|
_use_time = _time != uhd::time_spec_t(0.0);
|
2020-03-02 23:25:13 +00:00
|
|
|
if (_use_time)
|
|
|
|
|
_timeout = MASSIVE_TIMEOUT; // permanently sets larger timeout
|
2012-03-02 02:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-02 17:51:27 +00:00
|
|
|
uhd::time_spec_t get_time()
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(_mutex);
|
|
|
|
|
return _time;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
void set_tick_rate(const double rate)
|
|
|
|
|
{
|
2012-03-02 02:35:12 +00:00
|
|
|
boost::mutex::scoped_lock lock(_mutex);
|
|
|
|
|
_tick_rate = rate;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 00:28:56 +00:00
|
|
|
private:
|
2012-03-13 20:27:48 +00:00
|
|
|
/*******************************************************************
|
|
|
|
|
* Primary control and interaction private methods
|
|
|
|
|
******************************************************************/
|
2020-03-02 23:25:13 +00:00
|
|
|
UHD_INLINE void send_pkt(wb_addr_type addr, uint32_t data, int cmd)
|
|
|
|
|
{
|
2012-03-13 20:27:48 +00:00
|
|
|
managed_send_buffer::sptr buff = _xport->get_send_buff(0.0);
|
2020-03-02 23:25:13 +00:00
|
|
|
if (not buff) {
|
2012-03-13 20:27:48 +00:00
|
|
|
throw uhd::runtime_error("fifo ctrl timed out getting a send buffer");
|
|
|
|
|
}
|
2020-03-02 23:25:13 +00:00
|
|
|
uint32_t* trans = buff->cast<uint32_t*>();
|
|
|
|
|
trans[0] = htonl(++_seq_out);
|
|
|
|
|
uint32_t* pkt = trans + 1;
|
2012-03-13 20:27:48 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// load packet info
|
2012-03-13 20:27:48 +00:00
|
|
|
vrt::if_packet_info_t packet_info;
|
2020-03-02 23:25:13 +00:00
|
|
|
packet_info.packet_type = vrt::if_packet_info_t::PACKET_TYPE_CONTEXT;
|
2012-03-13 20:27:48 +00:00
|
|
|
packet_info.num_payload_words32 = 2;
|
2020-03-02 23:25:13 +00:00
|
|
|
packet_info.num_payload_bytes =
|
|
|
|
|
packet_info.num_payload_words32 * sizeof(uint32_t);
|
2012-03-13 20:27:48 +00:00
|
|
|
packet_info.packet_count = _seq_out;
|
2020-03-02 23:25:13 +00:00
|
|
|
packet_info.tsf = _time.to_ticks(_tick_rate);
|
|
|
|
|
packet_info.sob = false;
|
|
|
|
|
packet_info.eob = false;
|
|
|
|
|
packet_info.has_sid = false;
|
|
|
|
|
packet_info.has_cid = false;
|
|
|
|
|
packet_info.has_tsi = false;
|
|
|
|
|
packet_info.has_tsf = _use_time;
|
|
|
|
|
packet_info.has_tlr = false;
|
|
|
|
|
|
|
|
|
|
// load header
|
2012-03-13 20:27:48 +00:00
|
|
|
vrt::if_hdr_pack_be(pkt, packet_info);
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// load payload
|
|
|
|
|
const uint32_t ctrl_word = (addr & 0xff) | cmd | (_seq_out << 16);
|
|
|
|
|
pkt[packet_info.num_header_words32 + 0] = htonl(ctrl_word);
|
|
|
|
|
pkt[packet_info.num_header_words32 + 1] = htonl(data);
|
2012-03-13 20:27:48 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// send the buffer over the interface
|
|
|
|
|
buff->commit(sizeof(uint32_t) * (packet_info.num_packet_words32 + 1));
|
2012-03-13 20:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
UHD_INLINE bool wraparound_lt16(const int16_t i0, const int16_t i1)
|
|
|
|
|
{
|
|
|
|
|
if (((i0 ^ i1) & 0x8000) == 0) // same sign bits
|
2016-10-31 21:30:52 +00:00
|
|
|
return uint16_t(i0) < uint16_t(i1);
|
|
|
|
|
return int16_t(i1 - i0) > 0;
|
2012-03-13 20:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
UHD_INLINE uint32_t wait_for_ack(const uint16_t seq_to_ack)
|
|
|
|
|
{
|
|
|
|
|
while (wraparound_lt16(_seq_ack, seq_to_ack)) {
|
2012-03-13 20:27:48 +00:00
|
|
|
managed_recv_buffer::sptr buff = _xport->get_recv_buff(_timeout);
|
2020-03-02 23:25:13 +00:00
|
|
|
if (not buff) {
|
2012-03-13 20:27:48 +00:00
|
|
|
throw uhd::runtime_error("fifo ctrl timed out looking for acks");
|
|
|
|
|
}
|
2020-03-02 23:25:13 +00:00
|
|
|
const uint32_t* pkt = buff->cast<const uint32_t*>();
|
2012-03-13 20:27:48 +00:00
|
|
|
vrt::if_packet_info_t packet_info;
|
2020-03-02 23:25:13 +00:00
|
|
|
packet_info.num_packet_words32 = buff->size() / sizeof(uint32_t);
|
2012-03-13 20:27:48 +00:00
|
|
|
vrt::if_hdr_unpack_be(pkt, packet_info);
|
2020-03-02 23:25:13 +00:00
|
|
|
_seq_ack = ntohl(pkt[packet_info.num_header_words32 + 0]) >> 16;
|
|
|
|
|
if (_seq_ack == seq_to_ack) {
|
|
|
|
|
return ntohl(pkt[packet_info.num_header_words32 + 1]);
|
2012-03-13 20:27:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 00:28:56 +00:00
|
|
|
zero_copy_if::sptr _xport;
|
|
|
|
|
boost::mutex _mutex;
|
2016-10-31 21:30:52 +00:00
|
|
|
uint16_t _seq_out;
|
|
|
|
|
uint16_t _seq_ack;
|
2012-03-02 02:35:12 +00:00
|
|
|
uhd::time_spec_t _time;
|
|
|
|
|
bool _use_time;
|
|
|
|
|
double _tick_rate;
|
2012-03-02 03:58:19 +00:00
|
|
|
double _timeout;
|
2016-10-31 21:30:52 +00:00
|
|
|
uint32_t _ctrl_word_cache;
|
2012-03-01 00:28:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
usrp2_fifo_ctrl::sptr usrp2_fifo_ctrl::make(zero_copy_if::sptr xport)
|
|
|
|
|
{
|
2012-03-01 00:28:56 +00:00
|
|
|
return sptr(new usrp2_fifo_ctrl_impl(xport));
|
|
|
|
|
}
|