root / host / lib / usrp / e100 / io_impl.cpp @ eb083300
History | View | Annotate | Download (14.3 KB)
| 1 |
//
|
|---|---|
| 2 |
// Copyright 2010-2012 Ettus Research LLC
|
| 3 |
//
|
| 4 |
// This program is free software: you can redistribute it and/or modify
|
| 5 |
// it under the terms of the GNU General Public License as published by
|
| 6 |
// the Free Software Foundation, either version 3 of the License, or
|
| 7 |
// (at your option) any later version.
|
| 8 |
//
|
| 9 |
// This program is distributed in the hope that it will be useful,
|
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
// GNU General Public License for more details.
|
| 13 |
//
|
| 14 |
// You should have received a copy of the GNU General Public License
|
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 16 |
//
|
| 17 |
|
| 18 |
#include "recv_packet_demuxer.hpp" |
| 19 |
#include "validate_subdev_spec.hpp" |
| 20 |
#include "async_packet_handler.hpp" |
| 21 |
#include "../../transport/super_recv_packet_handler.hpp" |
| 22 |
#include "../../transport/super_send_packet_handler.hpp" |
| 23 |
#include <linux/usrp_e.h> //ioctl structures and constants |
| 24 |
#include "e100_impl.hpp" |
| 25 |
#include "e100_regs.hpp" |
| 26 |
#include <uhd/utils/msg.hpp> |
| 27 |
#include <uhd/utils/log.hpp> |
| 28 |
#include <uhd/utils/tasks.hpp> |
| 29 |
#include <uhd/utils/thread_priority.hpp> |
| 30 |
#include <uhd/transport/bounded_buffer.hpp> |
| 31 |
#include <boost/bind.hpp> |
| 32 |
#include <boost/format.hpp> |
| 33 |
#include <boost/bind.hpp> |
| 34 |
#include <boost/thread/thread.hpp> |
| 35 |
#include <poll.h> //poll |
| 36 |
#include <fcntl.h> //open, close |
| 37 |
#include <sstream> |
| 38 |
#include <fstream> |
| 39 |
#include <boost/make_shared.hpp> |
| 40 |
|
| 41 |
using namespace uhd; |
| 42 |
using namespace uhd::usrp; |
| 43 |
using namespace uhd::transport; |
| 44 |
|
| 45 |
static const size_t vrt_send_header_offset_words32 = 1; |
| 46 |
|
| 47 |
/***********************************************************************
|
| 48 |
* io impl details (internal to this file)
|
| 49 |
* - pirate crew of 1
|
| 50 |
* - bounded buffer
|
| 51 |
* - thread loop
|
| 52 |
* - vrt packet handler states
|
| 53 |
**********************************************************************/
|
| 54 |
struct e100_impl::io_impl{
|
| 55 |
io_impl(void):
|
| 56 |
false_alarm(0), async_msg_fifo(1000/*messages deep*/) |
| 57 |
{ /* NOP */ }
|
| 58 |
|
| 59 |
double tick_rate; //set by update tick rate method |
| 60 |
e100_ctrl::sptr iface; //so handle irq can peek and poke
|
| 61 |
void handle_irq(void); |
| 62 |
size_t false_alarm; |
| 63 |
//The data transport is listed first so that it is deconstructed last,
|
| 64 |
//which is after the states and booty which may hold managed buffers.
|
| 65 |
recv_packet_demuxer::sptr demuxer; |
| 66 |
|
| 67 |
//a pirate's life is the life for me!
|
| 68 |
void recv_pirate_loop(
|
| 69 |
spi_iface::sptr //keep a sptr to iface which shares gpio147
|
| 70 |
){
|
| 71 |
//open the GPIO and set it up for an IRQ
|
| 72 |
std::ofstream edge_file("/sys/class/gpio/gpio147/edge");
|
| 73 |
edge_file << "rising" << std::endl << std::flush;
|
| 74 |
edge_file.close(); |
| 75 |
int fd = ::open("/sys/class/gpio/gpio147/value", O_RDONLY); |
| 76 |
if (fd < 0) UHD_MSG(error) << "Unable to open GPIO for IRQ\n"; |
| 77 |
|
| 78 |
while (not boost::this_thread::interruption_requested()){ |
| 79 |
pollfd pfd; |
| 80 |
pfd.fd = fd; |
| 81 |
pfd.events = POLLPRI | POLLERR; |
| 82 |
ssize_t ret = ::poll(&pfd, 1, 100/*ms*/); |
| 83 |
if (ret > 0) this->handle_irq(); |
| 84 |
} |
| 85 |
|
| 86 |
//cleanup before thread exit
|
| 87 |
::close(fd); |
| 88 |
} |
| 89 |
bounded_buffer<async_metadata_t> async_msg_fifo; |
| 90 |
task::sptr pirate_task; |
| 91 |
}; |
| 92 |
|
| 93 |
void e100_impl::io_impl::handle_irq(void){ |
| 94 |
//check the status of the async msg buffer
|
| 95 |
const boost::uint32_t status = iface->peek32(E100_REG_RB_ERR_STATUS);
|
| 96 |
if ((status & 0x3) == 0){ //not done or error |
| 97 |
//This could be a false-alarm because spi readback is mixed in.
|
| 98 |
//So we just sleep for a bit rather than interrupt continuously.
|
| 99 |
if (false_alarm++ > 3) boost::this_thread::sleep(boost::posix_time::milliseconds(1)); |
| 100 |
return;
|
| 101 |
} |
| 102 |
false_alarm = 0; //its a real message, reset the count... |
| 103 |
//std::cout << boost::format("status: 0x%x") % status << std::endl;
|
| 104 |
|
| 105 |
//load the data struct and call the ioctl
|
| 106 |
usrp_e_ctl32 data; |
| 107 |
data.offset = E100_REG_ERR_BUFF; |
| 108 |
data.count = status >> 16;
|
| 109 |
iface->ioctl(USRP_E_READ_CTL32, &data); |
| 110 |
//for (size_t i = 0; i < data.count; i++){
|
| 111 |
//data.buf[i] = iface->peek32(E100_REG_ERR_BUFF + i*sizeof(boost::uint32_t));
|
| 112 |
//std::cout << boost::format(" buff[%u] = 0x%08x\n") % i % data.buf[i];
|
| 113 |
//}
|
| 114 |
|
| 115 |
//unpack the vrt header and process below...
|
| 116 |
vrt::if_packet_info_t if_packet_info; |
| 117 |
if_packet_info.num_packet_words32 = data.count; |
| 118 |
try{vrt::if_hdr_unpack_le(data.buf, if_packet_info);}
|
| 119 |
catch(const std::exception &e){ |
| 120 |
UHD_MSG(error) << "Error unpacking vrt header:\n" << e.what() << std::endl;
|
| 121 |
goto prepare;
|
| 122 |
} |
| 123 |
|
| 124 |
//handle a tx async report message
|
| 125 |
if (if_packet_info.sid == E100_TX_ASYNC_SID and if_packet_info.packet_type != vrt::if_packet_info_t::PACKET_TYPE_DATA){ |
| 126 |
|
| 127 |
//fill in the async metadata
|
| 128 |
async_metadata_t metadata; |
| 129 |
load_metadata_from_buff(uhd::wtohx<boost::uint32_t>, metadata, if_packet_info, data.buf, tick_rate); |
| 130 |
|
| 131 |
//push the message onto the queue
|
| 132 |
async_msg_fifo.push_with_pop_on_full(metadata); |
| 133 |
|
| 134 |
//print some fastpath messages
|
| 135 |
standard_async_msg_prints(metadata); |
| 136 |
} |
| 137 |
|
| 138 |
//prepare for the next round
|
| 139 |
prepare:
|
| 140 |
iface->poke32(E100_REG_SR_ERR_CTRL, 1 << 0); //clear |
| 141 |
while ((iface->peek32(E100_REG_RB_ERR_STATUS) & (1 << 2)) == 0){} //wait for idle |
| 142 |
iface->poke32(E100_REG_SR_ERR_CTRL, 1 << 1); //start |
| 143 |
} |
| 144 |
|
| 145 |
/***********************************************************************
|
| 146 |
* Helper Functions
|
| 147 |
**********************************************************************/
|
| 148 |
void e100_impl::io_init(void){ |
| 149 |
|
| 150 |
//create new io impl
|
| 151 |
_io_impl = UHD_PIMPL_MAKE(io_impl, ()); |
| 152 |
_io_impl->demuxer = recv_packet_demuxer::make(_data_transport, _rx_dsps.size(), E100_RX_SID_BASE); |
| 153 |
_io_impl->iface = _fpga_ctrl; |
| 154 |
|
| 155 |
//clear fifo state machines
|
| 156 |
_fpga_ctrl->poke32(E100_REG_CLEAR_FIFO, 0);
|
| 157 |
|
| 158 |
//allocate streamer weak ptrs containers
|
| 159 |
_rx_streamers.resize(_rx_dsps.size()); |
| 160 |
_tx_streamers.resize(1/*known to be 1 dsp*/); |
| 161 |
|
| 162 |
//prepare the async msg buffer for incoming messages
|
| 163 |
_fpga_ctrl->poke32(E100_REG_SR_ERR_CTRL, 1 << 0); //clear |
| 164 |
while ((_fpga_ctrl->peek32(E100_REG_RB_ERR_STATUS) & (1 << 2)) == 0){} //wait for idle |
| 165 |
_fpga_ctrl->poke32(E100_REG_SR_ERR_CTRL, 1 << 1); //start |
| 166 |
|
| 167 |
//spawn a pirate, yarrr!
|
| 168 |
_io_impl->pirate_task = task::make(boost::bind( |
| 169 |
&e100_impl::io_impl::recv_pirate_loop, _io_impl.get(), _aux_spi_iface |
| 170 |
)); |
| 171 |
} |
| 172 |
|
| 173 |
void e100_impl::update_tick_rate(const double rate){ |
| 174 |
_io_impl->tick_rate = rate; |
| 175 |
|
| 176 |
//update the tick rate on all existing streamers -> thread safe
|
| 177 |
for (size_t i = 0; i < _rx_streamers.size(); i++){ |
| 178 |
boost::shared_ptr<sph::recv_packet_streamer> my_streamer = |
| 179 |
boost::dynamic_pointer_cast<sph::recv_packet_streamer>(_rx_streamers[i].lock()); |
| 180 |
if (my_streamer.get() == NULL) continue; |
| 181 |
my_streamer->set_tick_rate(rate); |
| 182 |
} |
| 183 |
for (size_t i = 0; i < _tx_streamers.size(); i++){ |
| 184 |
boost::shared_ptr<sph::send_packet_streamer> my_streamer = |
| 185 |
boost::dynamic_pointer_cast<sph::send_packet_streamer>(_tx_streamers[i].lock()); |
| 186 |
if (my_streamer.get() == NULL) continue; |
| 187 |
my_streamer->set_tick_rate(rate); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
void e100_impl::update_rx_samp_rate(const size_t dspno, const double rate){ |
| 192 |
boost::shared_ptr<sph::recv_packet_streamer> my_streamer = |
| 193 |
boost::dynamic_pointer_cast<sph::recv_packet_streamer>(_rx_streamers[dspno].lock()); |
| 194 |
if (my_streamer.get() == NULL) return; |
| 195 |
|
| 196 |
my_streamer->set_samp_rate(rate); |
| 197 |
const double adj = _rx_dsps[dspno]->get_scaling_adjustment(); |
| 198 |
my_streamer->set_scale_factor(adj); |
| 199 |
} |
| 200 |
|
| 201 |
void e100_impl::update_tx_samp_rate(const size_t dspno, const double rate){ |
| 202 |
boost::shared_ptr<sph::send_packet_streamer> my_streamer = |
| 203 |
boost::dynamic_pointer_cast<sph::send_packet_streamer>(_tx_streamers[dspno].lock()); |
| 204 |
if (my_streamer.get() == NULL) return; |
| 205 |
|
| 206 |
my_streamer->set_samp_rate(rate); |
| 207 |
const double adj = _tx_dsp->get_scaling_adjustment(); |
| 208 |
my_streamer->set_scale_factor(adj); |
| 209 |
} |
| 210 |
|
| 211 |
void e100_impl::update_rates(void){ |
| 212 |
const fs_path mb_path = "/mboards/0"; |
| 213 |
_tree->access<double>(mb_path / "tick_rate").update(); |
| 214 |
|
| 215 |
//and now that the tick rate is set, init the host rates to something
|
| 216 |
BOOST_FOREACH(const std::string &name, _tree->list(mb_path / "rx_dsps")){ |
| 217 |
_tree->access<double>(mb_path / "rx_dsps" / name / "rate" / "value").update(); |
| 218 |
} |
| 219 |
BOOST_FOREACH(const std::string &name, _tree->list(mb_path / "tx_dsps")){ |
| 220 |
_tree->access<double>(mb_path / "tx_dsps" / name / "rate" / "value").update(); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
void e100_impl::update_rx_subdev_spec(const uhd::usrp::subdev_spec_t &spec){ |
| 225 |
fs_path root = "/mboards/0/dboards";
|
| 226 |
|
| 227 |
//sanity checking
|
| 228 |
validate_subdev_spec(_tree, spec, "rx");
|
| 229 |
|
| 230 |
//setup mux for this spec
|
| 231 |
bool fe_swapped = false; |
| 232 |
for (size_t i = 0; i < spec.size(); i++){ |
| 233 |
const std::string conn = _tree->access<std::string>(root / spec[i].db_name / "rx_frontends" / spec[i].sd_name / "connection").get(); |
| 234 |
if (i == 0 and (conn == "QI" or conn == "Q")) fe_swapped = true; |
| 235 |
_rx_dsps[i]->set_mux(conn, fe_swapped); |
| 236 |
} |
| 237 |
_rx_fe->set_mux(fe_swapped); |
| 238 |
} |
| 239 |
|
| 240 |
void e100_impl::update_tx_subdev_spec(const uhd::usrp::subdev_spec_t &spec){ |
| 241 |
fs_path root = "/mboards/0/dboards";
|
| 242 |
|
| 243 |
//sanity checking
|
| 244 |
validate_subdev_spec(_tree, spec, "tx");
|
| 245 |
|
| 246 |
//set the mux for this spec
|
| 247 |
const std::string conn = _tree->access<std::string>(root / spec[0].db_name / "tx_frontends" / spec[0].sd_name / "connection").get(); |
| 248 |
_tx_fe->set_mux(conn); |
| 249 |
} |
| 250 |
|
| 251 |
/***********************************************************************
|
| 252 |
* Async Recv
|
| 253 |
**********************************************************************/
|
| 254 |
bool e100_impl::recv_async_msg(
|
| 255 |
async_metadata_t &async_metadata, double timeout
|
| 256 |
){
|
| 257 |
boost::this_thread::disable_interruption di; //disable because the wait can throw
|
| 258 |
return _io_impl->async_msg_fifo.pop_with_timed_wait(async_metadata, timeout);
|
| 259 |
} |
| 260 |
|
| 261 |
/***********************************************************************
|
| 262 |
* Receive streamer
|
| 263 |
**********************************************************************/
|
| 264 |
rx_streamer::sptr e100_impl::get_rx_stream(const uhd::stream_args_t &args_){
|
| 265 |
stream_args_t args = args_; |
| 266 |
|
| 267 |
//setup defaults for unspecified values
|
| 268 |
args.otw_format = args.otw_format.empty()? "sc16" : args.otw_format;
|
| 269 |
args.channels = args.channels.empty()? std::vector<size_t>(1, 0) : args.channels; |
| 270 |
|
| 271 |
//calculate packet size
|
| 272 |
static const size_t hdr_size = 0 |
| 273 |
+ vrt::max_if_hdr_words32*sizeof(boost::uint32_t)
|
| 274 |
+ sizeof(vrt::if_packet_info_t().tlr) //forced to have trailer |
| 275 |
- sizeof(vrt::if_packet_info_t().cid) //no class id ever used |
| 276 |
- sizeof(vrt::if_packet_info_t().tsi) //no int time ever used |
| 277 |
; |
| 278 |
const size_t bpp = _data_transport->get_recv_frame_size() - hdr_size;
|
| 279 |
const size_t bpi = convert::get_bytes_per_item(args.otw_format);
|
| 280 |
const size_t spp = unsigned(args.args.cast<double>("spp", bpp/bpi)); |
| 281 |
|
| 282 |
//make the new streamer given the samples per packet
|
| 283 |
boost::shared_ptr<sph::recv_packet_streamer> my_streamer = boost::make_shared<sph::recv_packet_streamer>(spp); |
| 284 |
|
| 285 |
//init some streamer stuff
|
| 286 |
my_streamer->resize(args.channels.size()); |
| 287 |
my_streamer->set_vrt_unpacker(&vrt::if_hdr_unpack_le); |
| 288 |
|
| 289 |
//set the converter
|
| 290 |
uhd::convert::id_type id; |
| 291 |
id.input_format = args.otw_format + "_item32_le";
|
| 292 |
id.num_inputs = 1;
|
| 293 |
id.output_format = args.cpu_format; |
| 294 |
id.num_outputs = 1;
|
| 295 |
my_streamer->set_converter(id); |
| 296 |
|
| 297 |
//bind callbacks for the handler
|
| 298 |
for (size_t chan_i = 0; chan_i < args.channels.size(); chan_i++){ |
| 299 |
const size_t dsp = args.channels[chan_i];
|
| 300 |
_rx_dsps[dsp]->set_nsamps_per_packet(spp); //seems to be a good place to set this
|
| 301 |
_rx_dsps[dsp]->setup(args); |
| 302 |
my_streamer->set_xport_chan_get_buff(chan_i, boost::bind( |
| 303 |
&recv_packet_demuxer::get_recv_buff, _io_impl->demuxer, dsp, _1 |
| 304 |
), true /*flush*/); |
| 305 |
my_streamer->set_overflow_handler(chan_i, boost::bind( |
| 306 |
&rx_dsp_core_200::handle_overflow, _rx_dsps[dsp] |
| 307 |
)); |
| 308 |
_rx_streamers[dsp] = my_streamer; //store weak pointer
|
| 309 |
} |
| 310 |
|
| 311 |
//sets all tick and samp rates on this streamer
|
| 312 |
this->update_rates();
|
| 313 |
|
| 314 |
return my_streamer;
|
| 315 |
} |
| 316 |
|
| 317 |
/***********************************************************************
|
| 318 |
* Transmit streamer
|
| 319 |
**********************************************************************/
|
| 320 |
tx_streamer::sptr e100_impl::get_tx_stream(const uhd::stream_args_t &args_){
|
| 321 |
stream_args_t args = args_; |
| 322 |
|
| 323 |
//setup defaults for unspecified values
|
| 324 |
args.otw_format = args.otw_format.empty()? "sc16" : args.otw_format;
|
| 325 |
args.channels = args.channels.empty()? std::vector<size_t>(1, 0) : args.channels; |
| 326 |
|
| 327 |
//calculate packet size
|
| 328 |
static const size_t hdr_size = 0 |
| 329 |
+ vrt_send_header_offset_words32*sizeof(boost::uint32_t)
|
| 330 |
+ vrt::max_if_hdr_words32*sizeof(boost::uint32_t)
|
| 331 |
+ sizeof(vrt::if_packet_info_t().tlr) //forced to have trailer |
| 332 |
- sizeof(vrt::if_packet_info_t().sid) //no stream id ever used |
| 333 |
- sizeof(vrt::if_packet_info_t().cid) //no class id ever used |
| 334 |
- sizeof(vrt::if_packet_info_t().tsi) //no int time ever used |
| 335 |
; |
| 336 |
static const size_t bpp = _data_transport->get_send_frame_size() - hdr_size; |
| 337 |
const size_t spp = bpp/convert::get_bytes_per_item(args.otw_format);
|
| 338 |
|
| 339 |
//make the new streamer given the samples per packet
|
| 340 |
boost::shared_ptr<sph::send_packet_streamer> my_streamer = boost::make_shared<sph::send_packet_streamer>(spp); |
| 341 |
|
| 342 |
//init some streamer stuff
|
| 343 |
my_streamer->resize(args.channels.size()); |
| 344 |
my_streamer->set_vrt_packer(&vrt::if_hdr_pack_le, vrt_send_header_offset_words32); |
| 345 |
|
| 346 |
//set the converter
|
| 347 |
uhd::convert::id_type id; |
| 348 |
id.input_format = args.cpu_format; |
| 349 |
id.num_inputs = 1;
|
| 350 |
id.output_format = args.otw_format + "_item32_le";
|
| 351 |
id.num_outputs = 1;
|
| 352 |
my_streamer->set_converter(id); |
| 353 |
|
| 354 |
//bind callbacks for the handler
|
| 355 |
for (size_t chan_i = 0; chan_i < args.channels.size(); chan_i++){ |
| 356 |
const size_t dsp = args.channels[chan_i];
|
| 357 |
UHD_ASSERT_THROW(dsp == 0); //always 0 |
| 358 |
_tx_dsp->setup(args); |
| 359 |
my_streamer->set_xport_chan_get_buff(chan_i, boost::bind( |
| 360 |
&zero_copy_if::get_send_buff, _data_transport, _1 |
| 361 |
)); |
| 362 |
_tx_streamers[dsp] = my_streamer; //store weak pointer
|
| 363 |
} |
| 364 |
|
| 365 |
//sets all tick and samp rates on this streamer
|
| 366 |
this->update_rates();
|
| 367 |
|
| 368 |
return my_streamer;
|
| 369 |
} |