1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
//
// Copyright 2013 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
#include "x300_fw_common.h"
#include "x300_regs.hpp"
#include <uhd/exception.hpp>
#include <uhd/transport/nirio/niriok_proxy.h>
#include <uhd/transport/nirio/status.h>
#include <uhd/transport/udp_simple.hpp>
#include <uhd/types/wb_iface.hpp>
#include <uhd/utils/byteswap.hpp>
#include <uhd/utils/log.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/format.hpp>
#include <chrono>
#include <mutex>
#include <thread>
using namespace uhd;
using namespace uhd::niusrprio;
class x300_ctrl_iface : public wb_iface
{
public:
enum { num_retries = 3 };
x300_ctrl_iface(bool enable_errors = true) : errors(enable_errors)
{
/* NOP */
}
void flush(void)
{
std::lock_guard<std::mutex> lock(reg_access);
__flush();
}
void poke32(const wb_addr_type addr, const uint32_t data) override
{
for (size_t i = 1; i <= num_retries; i++) {
std::lock_guard<std::mutex> lock(reg_access);
try {
return this->__poke32(addr, data);
} catch (const uhd::io_error& ex) {
std::string error_msg =
str(boost::format("%s: x300 fw communication failure #%u\n%s")
% __loc_info() % i % ex.what());
if (errors)
UHD_LOGGER_ERROR("X300") << error_msg;
if (i == num_retries)
throw uhd::io_error(error_msg);
}
}
}
uint32_t peek32(const wb_addr_type addr) override
{
for (size_t i = 1; i <= num_retries; i++) {
std::lock_guard<std::mutex> lock(reg_access);
try {
uint32_t data = this->__peek32(addr);
return data;
} catch (const uhd::io_error& ex) {
std::string error_msg =
str(boost::format("%s: x300 fw communication failure #%u\n%s")
% __loc_info() % i % ex.what());
if (errors)
UHD_LOGGER_ERROR("X300") << error_msg;
if (i == num_retries)
throw uhd::io_error(error_msg);
}
}
return 0;
}
protected:
bool errors;
virtual void __poke32(const wb_addr_type addr, const uint32_t data) = 0;
virtual uint32_t __peek32(const wb_addr_type addr) = 0;
virtual void __flush() = 0;
virtual std::string __loc_info() = 0;
std::mutex reg_access;
};
//-----------------------------------------------------
// Ethernet impl
//-----------------------------------------------------
class x300_ctrl_iface_enet : public x300_ctrl_iface
{
public:
x300_ctrl_iface_enet(uhd::transport::udp_simple::sptr udp, bool enable_errors = true)
: x300_ctrl_iface(enable_errors), udp(udp), seq(0)
{
try {
this->peek32(0);
} catch (...) {
}
}
protected:
void __poke32(const wb_addr_type addr, const uint32_t data) override
{
// load request struct
x300_fw_comms_t request = x300_fw_comms_t();
request.flags =
uhd::htonx<uint32_t>(X300_FW_COMMS_FLAGS_ACK | X300_FW_COMMS_FLAGS_POKE32);
request.sequence = uhd::htonx<uint32_t>(seq++);
request.addr = uhd::htonx(addr);
request.data = uhd::htonx(data);
// send request
__flush();
udp->send(boost::asio::buffer(&request, sizeof(request)));
// recv reply
x300_fw_comms_t reply = x300_fw_comms_t();
const size_t nbytes = udp->recv(boost::asio::buffer(&reply, sizeof(reply)), 1.0);
if (nbytes == 0)
throw uhd::io_error("x300 fw poke32 - reply timed out");
// sanity checks
const size_t flags = uhd::ntohx<uint32_t>(reply.flags);
UHD_ASSERT_THROW(nbytes == sizeof(reply));
UHD_ASSERT_THROW(not(flags & X300_FW_COMMS_FLAGS_ERROR));
UHD_ASSERT_THROW(flags & X300_FW_COMMS_FLAGS_POKE32);
UHD_ASSERT_THROW(flags & X300_FW_COMMS_FLAGS_ACK);
UHD_ASSERT_THROW(reply.sequence == request.sequence);
UHD_ASSERT_THROW(reply.addr == request.addr);
UHD_ASSERT_THROW(reply.data == request.data);
}
uint32_t __peek32(const wb_addr_type addr) override
{
// load request struct
x300_fw_comms_t request = x300_fw_comms_t();
request.flags =
uhd::htonx<uint32_t>(X300_FW_COMMS_FLAGS_ACK | X300_FW_COMMS_FLAGS_PEEK32);
request.sequence = uhd::htonx<uint32_t>(seq++);
request.addr = uhd::htonx(addr);
request.data = 0;
// send request
__flush();
udp->send(boost::asio::buffer(&request, sizeof(request)));
// recv reply
x300_fw_comms_t reply = x300_fw_comms_t();
const size_t nbytes = udp->recv(boost::asio::buffer(&reply, sizeof(reply)), 1.0);
if (nbytes == 0)
throw uhd::io_error("x300 fw peek32 - reply timed out");
// sanity checks
const size_t flags = uhd::ntohx<uint32_t>(reply.flags);
UHD_ASSERT_THROW(nbytes == sizeof(reply));
UHD_ASSERT_THROW(not(flags & X300_FW_COMMS_FLAGS_ERROR));
UHD_ASSERT_THROW(flags & X300_FW_COMMS_FLAGS_PEEK32);
UHD_ASSERT_THROW(flags & X300_FW_COMMS_FLAGS_ACK);
UHD_ASSERT_THROW(reply.sequence == request.sequence);
UHD_ASSERT_THROW(reply.addr == request.addr);
// return result!
return uhd::ntohx<uint32_t>(reply.data);
}
void __flush(void) override
{
char buff[X300_FW_COMMS_MTU] = {};
while (udp->recv(boost::asio::buffer(buff), 0.0)) {
} // flush
}
std::string __loc_info(void) override
{
return udp->get_send_addr();
}
private:
uhd::transport::udp_simple::sptr udp;
size_t seq;
};
//-----------------------------------------------------
// PCIe impl
//-----------------------------------------------------
class x300_ctrl_iface_pcie : public x300_ctrl_iface
{
public:
x300_ctrl_iface_pcie(niriok_proxy::sptr drv_proxy, bool enable_errors = true)
: x300_ctrl_iface(enable_errors), _drv_proxy(drv_proxy)
{
nirio_status status = 0;
nirio_status_chain(
_drv_proxy->set_attribute(RIO_ADDRESS_SPACE, BUS_INTERFACE), status);
// Verify that the Ettus FPGA loaded in the device. This may not be true if the
// user is switching to UHD after using LabVIEW FPGA.
uint32_t pcie_fpga_signature = 0;
_drv_proxy->peek(FPGA_PCIE_SIG_REG, pcie_fpga_signature);
if (pcie_fpga_signature != FPGA_X3xx_SIG_VALUE)
throw uhd::io_error(
"cannot create x300_ctrl_iface_pcie. incorrect/no fpga image");
// Also, poll on the ZPU_STATUS bit to ensure all the state machines in the FPGA
// are ready to accept register transaction requests.
uint32_t reg_data = 0xffffffff;
boost::posix_time::ptime start_time =
boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_duration elapsed;
do {
std::this_thread::sleep_for(
std::chrono::microseconds(500)); // Avoid flooding the bus
elapsed = boost::posix_time::microsec_clock::local_time() - start_time;
nirio_status_chain(
_drv_proxy->peek(PCIE_ZPU_STATUS_REG(0), reg_data), status);
} while (nirio_status_not_fatal(status) && (reg_data & PCIE_ZPU_STATUS_SUSPENDED)
&& elapsed.total_milliseconds() < INIT_TIMEOUT_IN_MS);
nirio_status_to_exception(status, "Could not initialize x300_ctrl_iface_pcie.");
try {
this->peek32(0);
} catch (...) {
}
}
protected:
void __poke32(const wb_addr_type addr, const uint32_t data) override
{
nirio_status status = 0;
uint32_t reg_data = 0xffffffff;
boost::posix_time::ptime start_time =
boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_duration elapsed;
nirio_status_chain(_drv_proxy->poke(PCIE_ZPU_DATA_REG(addr), data), status);
if (nirio_status_not_fatal(status)) {
do {
std::this_thread::sleep_for(
std::chrono::microseconds(50)); // Avoid flooding the bus
elapsed = boost::posix_time::microsec_clock::local_time() - start_time;
nirio_status_chain(
_drv_proxy->peek(PCIE_ZPU_STATUS_REG(addr), reg_data), status);
} while (
nirio_status_not_fatal(status)
&& ((reg_data & (PCIE_ZPU_STATUS_BUSY | PCIE_ZPU_STATUS_SUSPENDED)) != 0)
&& elapsed.total_milliseconds() < READ_TIMEOUT_IN_MS);
}
if (nirio_status_fatal(status))
throw uhd::io_error("x300 fw poke32 - hardware IO error");
if (elapsed.total_milliseconds() > READ_TIMEOUT_IN_MS)
throw uhd::io_error("x300 fw poke32 - operation timed out");
}
uint32_t __peek32(const wb_addr_type addr) override
{
nirio_status status = 0;
uint32_t reg_data = 0xffffffff;
boost::posix_time::ptime start_time =
boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_duration elapsed;
nirio_status_chain(
_drv_proxy->poke(PCIE_ZPU_READ_REG(addr), PCIE_ZPU_READ_START), status);
if (nirio_status_not_fatal(status)) {
do {
std::this_thread::sleep_for(
std::chrono::microseconds(50)); // Avoid flooding the bus
elapsed = boost::posix_time::microsec_clock::local_time() - start_time;
nirio_status_chain(
_drv_proxy->peek(PCIE_ZPU_STATUS_REG(addr), reg_data), status);
} while (
nirio_status_not_fatal(status)
&& ((reg_data & (PCIE_ZPU_STATUS_BUSY | PCIE_ZPU_STATUS_SUSPENDED)) != 0)
&& elapsed.total_milliseconds() < READ_TIMEOUT_IN_MS);
}
nirio_status_chain(_drv_proxy->peek(PCIE_ZPU_DATA_REG(addr), reg_data), status);
if (nirio_status_fatal(status))
throw uhd::io_error("x300 fw peek32 - hardware IO error");
if (elapsed.total_milliseconds() > READ_TIMEOUT_IN_MS)
throw uhd::io_error("x300 fw peek32 - operation timed out");
return reg_data;
}
void __flush(void) override
{
__peek32(0);
}
std::string __loc_info(void) override
{
return std::to_string(_drv_proxy->get_interface_num());
}
private:
niriok_proxy::sptr _drv_proxy;
static const uint32_t READ_TIMEOUT_IN_MS = 100;
static const uint32_t INIT_TIMEOUT_IN_MS = 5000;
};
wb_iface::sptr x300_make_ctrl_iface_enet(
uhd::transport::udp_simple::sptr udp, bool enable_errors = true)
{
return wb_iface::sptr(new x300_ctrl_iface_enet(udp, enable_errors));
}
wb_iface::sptr x300_make_ctrl_iface_pcie(
niriok_proxy::sptr drv_proxy, bool enable_errors = true)
{
return wb_iface::sptr(new x300_ctrl_iface_pcie(drv_proxy, enable_errors));
}
|