| 1 | /**************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Copyright 2005,2006 Virginia Polytechnic Institute and State University |
|---|
| 4 | |
|---|
| 5 | This file is part of the OSSIE USRP_UHD Device. |
|---|
| 6 | |
|---|
| 7 | OSSIE USRP_UHD Device is free software; you can redistribute it and/or modify |
|---|
| 8 | it under the terms of the GNU General Public License as published by |
|---|
| 9 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | OSSIE USRP_UHD Device is distributed in the hope that it will be useful, |
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | GNU General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | You should have received a copy of the GNU General Public License |
|---|
| 18 | along with OSSIE USRP_UHD Device; if not, write to the Free Software |
|---|
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | ****************************************************************************/ |
|---|
| 23 | |
|---|
| 24 | #include <byteswap.h> |
|---|
| 25 | #include <iostream> |
|---|
| 26 | #include <vector> |
|---|
| 27 | #include <complex> |
|---|
| 28 | #include <boost/program_options.hpp> |
|---|
| 29 | #include <boost/format.hpp> |
|---|
| 30 | #include <boost/thread.hpp> |
|---|
| 31 | |
|---|
| 32 | #include "ossie/cf.h" |
|---|
| 33 | #include "ossie/PortTypes.h" |
|---|
| 34 | |
|---|
| 35 | #include "ossie/ossieSupport.h" |
|---|
| 36 | #include "ossie/debug.h" |
|---|
| 37 | |
|---|
| 38 | #include "USRP_UHD.h" |
|---|
| 39 | |
|---|
| 40 | USRP_UHD_i::USRP_UHD_i(char *id, char *label, char *profile) : |
|---|
| 41 | Device_impl(id, label, profile), |
|---|
| 42 | set_rx_packet_count(-1), |
|---|
| 43 | rx_packet_count(1024), |
|---|
| 44 | rx_packet_size(8192), |
|---|
| 45 | rx_data_size(2), |
|---|
| 46 | number_of_channels(1), |
|---|
| 47 | complex(true), |
|---|
| 48 | rx_overruns(0), |
|---|
| 49 | rx_active(false), |
|---|
| 50 | tx_active(false), |
|---|
| 51 | full_duplex(true), |
|---|
| 52 | did_rx(false) |
|---|
| 53 | { |
|---|
| 54 | // Create USRP_UHD Control ports for TX and RX |
|---|
| 55 | rx_control_port = new USRP_UHD_RX_Control_i(this, "UHD_RX_Control", "DomainName1"); |
|---|
| 56 | tx_control_port = new USRP_UHD_TX_Control_i(this, "UHD_TX_Control", "DomainName1"); |
|---|
| 57 | |
|---|
| 58 | // Create the ports for TX data |
|---|
| 59 | for(int i=0;i<MAX_TX_CHANNEL;i++) |
|---|
| 60 | tx_data_ports[i] = new standardInterfaces_i::complexShort_p(boost::str(boost::format("UHD_TX_Data_%d") % (i+1)).c_str(), "DomainName1"); |
|---|
| 61 | |
|---|
| 62 | // Create the ports for RX Data |
|---|
| 63 | for(int i=0;i<MAX_RX_CHANNEL;i++) |
|---|
| 64 | rx_data_ports[i] = new standardInterfaces_i::complexShort_u(boost::str(boost::format("UHD_RX_Data_%d") % (i+1)).c_str(), "DomainName1"); |
|---|
| 65 | |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | void USRP_UHD_i::start() throw (CF::Resource::StartError, CORBA::SystemException) |
|---|
| 69 | { |
|---|
| 70 | DEBUG(3, USRP_UHD, "Start USRP_UHD called") |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | void USRP_UHD_i::stop() throw (CF::Resource::StopError, CORBA::SystemException) |
|---|
| 74 | { |
|---|
| 75 | DEBUG(3, USRP_UHD, "Stop USRP_UHD called") |
|---|
| 76 | (rx_active)?std::cout<<"Active": std::cout<<"NOT Active"; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | CORBA::Object_ptr USRP_UHD_i::getPort(const char* portName) |
|---|
| 80 | throw(CF::PortSupplier::UnknownPort, CORBA::SystemException) |
|---|
| 81 | { |
|---|
| 82 | DEBUG(3, USRP_UHD, "USRP_UHD getPort called with : " << portName) |
|---|
| 83 | |
|---|
| 84 | CORBA::Object_var p; |
|---|
| 85 | |
|---|
| 86 | if (!CORBA::is_nil(p = rx_control_port->getPort(portName))) |
|---|
| 87 | return p._retn(); |
|---|
| 88 | else if (!CORBA::is_nil(p = tx_control_port->getPort(portName))) |
|---|
| 89 | return p._retn(); |
|---|
| 90 | else { |
|---|
| 91 | for(int i=0;i<MAX_RX_CHANNEL;i++) |
|---|
| 92 | if (!CORBA::is_nil(p = rx_data_ports[i]->getPort(portName))) |
|---|
| 93 | return p._retn(); |
|---|
| 94 | |
|---|
| 95 | for(int i=0;i<MAX_TX_CHANNEL;i++) |
|---|
| 96 | if (!CORBA::is_nil(p = tx_data_ports[i]->getPort(portName))) |
|---|
| 97 | return p._retn(); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | std::cerr << "Couldn't find port " << portName << "Throwing exception" << std::endl; |
|---|
| 101 | // throw CF::PortSupplier::UnknownPort(); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | void USRP_UHD_i::initialize() throw (CF::LifeCycle::InitializeError, CORBA::SystemException) |
|---|
| 105 | { |
|---|
| 106 | DEBUG(3, USRP_UHD, "USRP_UHD Initialize called") |
|---|
| 107 | std::string args(""); |
|---|
| 108 | DEBUG(1, USRP_UHD, "Creating the usrp device with: "<< args) |
|---|
| 109 | sdev = uhd::usrp::multi_usrp::make(args); |
|---|
| 110 | dev = sdev->get_device();//uhd::device::make(args); |
|---|
| 111 | probe_usrp(); |
|---|
| 112 | rx_control_port->set_number_of_channels(1); |
|---|
| 113 | tx_control_port->set_number_of_channels(1); |
|---|
| 114 | sdev->set_rx_antenna("RX2"); |
|---|
| 115 | sdev->set_rx_antenna("TX/RX"); |
|---|
| 116 | DEBUG(1, USRP_UHD, "Using Device: "<< sdev->get_pp_string()) |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | void USRP_UHD_i::configure(const CF::Properties &configProperties) |
|---|
| 120 | throw (CORBA::SystemException, CF::PropertySet::InvalidConfiguration, |
|---|
| 121 | CF::PropertySet::PartialConfiguration) |
|---|
| 122 | { |
|---|
| 123 | |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | void USRP_UHD_i::query(CF::Properties &configProperties) |
|---|
| 127 | throw (CORBA::SystemException, CF::UnknownProperties) |
|---|
| 128 | { |
|---|
| 129 | |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | void USRP_UHD_i::releaseObject() |
|---|
| 134 | throw (CF::LifeCycle::ReleaseError, CORBA::SystemException) |
|---|
| 135 | { |
|---|
| 136 | |
|---|
| 137 | DEBUG(3, USRP_UHD, "USRP_UHD releaseObject called") |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | void USRP_UHD_i::rx_error_helper(uhd::rx_metadata_t::error_code_t error) |
|---|
| 141 | { |
|---|
| 142 | switch(error){ |
|---|
| 143 | case uhd::rx_metadata_t::ERROR_CODE_NONE: |
|---|
| 144 | break; |
|---|
| 145 | case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT: |
|---|
| 146 | DEBUG(2, USRP_UHD, "Got timeout before all samples received, possible packet loss") |
|---|
| 147 | break; |
|---|
| 148 | case uhd::rx_metadata_t::ERROR_CODE_LATE_COMMAND: |
|---|
| 149 | DEBUG(2, USRP_UHD, "UHD::ERROR_CODE_LATE_COMMAND") |
|---|
| 150 | break; |
|---|
| 151 | case uhd::rx_metadata_t::ERROR_CODE_BROKEN_CHAIN: |
|---|
| 152 | DEBUG(2, USRP_UHD, "UHD::ERROR_CODE_BROKEN_CHAIN") |
|---|
| 153 | break; |
|---|
| 154 | case uhd::rx_metadata_t::ERROR_CODE_OVERFLOW: |
|---|
| 155 | //DEBUG(2, USRP_UHD, "UHD::ERROR_CODE_OVERFLOW") |
|---|
| 156 | break; |
|---|
| 157 | case uhd::rx_metadata_t::ERROR_CODE_ALIGNMENT: |
|---|
| 158 | DEBUG(2, USRP_UHD, "UHD::ERROR_CODE_ALIGNMENT") |
|---|
| 159 | break; |
|---|
| 160 | case uhd::rx_metadata_t::ERROR_CODE_BAD_PACKET: |
|---|
| 161 | DEBUG(2, USRP_UHD, "UHD::ERROR_CODE_BAD_PACKET") |
|---|
| 162 | break; |
|---|
| 163 | default: |
|---|
| 164 | DEBUG(3, USRP_UHD, "Got Error Code 0x"<<error) |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | void USRP_UHD_i::rx_data_process() |
|---|
| 169 | { |
|---|
| 170 | |
|---|
| 171 | ///\TODO assumes one channel operation. Set full_deplex to false to prevent Rx buffering during Tx |
|---|
| 172 | ///Full duplex mode is still broken. we still cannot transmit and receive simultaneously using WBX board |
|---|
| 173 | |
|---|
| 174 | //tells the device to stream samples indefinitely |
|---|
| 175 | uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS); |
|---|
| 176 | |
|---|
| 177 | //Use with STREAM_MODE_NUM_SAMPS_AND_DONE/_MORE |
|---|
| 178 | //stream_cmd.num_samps = 65536; |
|---|
| 179 | //stream_cmd.time_spec = uhd::time_spec_t((time_t)0); |
|---|
| 180 | |
|---|
| 181 | //When true, the device will begin streaming ASAP |
|---|
| 182 | stream_cmd.stream_now = true; |
|---|
| 183 | |
|---|
| 184 | sdev->issue_stream_cmd(stream_cmd); |
|---|
| 185 | |
|---|
| 186 | uhd::stream_args_t stream_args("sc16"); //complex floats |
|---|
| 187 | stream_args.args["noclear"] = "1"; |
|---|
| 188 | for (size_t chan = 0; chan < rx_channels; chan++) |
|---|
| 189 | stream_args.channels.push_back(chan); //linear mapping |
|---|
| 190 | uhd::rx_streamer::sptr rx_stream = sdev->get_rx_stream(stream_args); |
|---|
| 191 | DEBUG(3, USRP_UHD, "Rx Channels = " << rx_channels) |
|---|
| 192 | PortTypes::ShortSequence I; |
|---|
| 193 | PortTypes::ShortSequence Q; |
|---|
| 194 | |
|---|
| 195 | ///\TODO Make sure rx_packet_size is >= than USRP_UHD_MIN_RX_SAMPLES!!!!! |
|---|
| 196 | std::vector<std::vector< std::complex<short> > > rx_buff( |
|---|
| 197 | rx_channels, std::vector< std::complex<short> >(rx_packet_size) |
|---|
| 198 | ); |
|---|
| 199 | I.length(rx_packet_size); |
|---|
| 200 | Q.length(rx_packet_size); |
|---|
| 201 | |
|---|
| 202 | std::vector<std::complex<short> *> rx_buff_ptrs; |
|---|
| 203 | for (size_t i = 0; i < rx_buff.size(); i++) |
|---|
| 204 | rx_buff_ptrs.push_back(&rx_buff[i].front()); |
|---|
| 205 | |
|---|
| 206 | DEBUG(3, USRP_UHD, "RX packet count " << rx_packet_count) |
|---|
| 207 | |
|---|
| 208 | while (rx_active && ((rx_packet_count == -1) || (rx_packet_count > 0))) { |
|---|
| 209 | uhd::rx_metadata_t md; |
|---|
| 210 | //if we are running half duplex mode we will need to tell start streaming at the beginning of the loop |
|---|
| 211 | if (full_duplex == false && did_rx == false) { |
|---|
| 212 | uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS); |
|---|
| 213 | sdev->issue_stream_cmd(stream_cmd); |
|---|
| 214 | did_rx = true; |
|---|
| 215 | } |
|---|
| 216 | //DEBUG(9, USRP_UHD, "issue cmd") |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | //TODO get rid of num_rx_samps variable |
|---|
| 220 | size_t num_rx_samps = rx_stream->recv( rx_buff_ptrs, rx_packet_size, md); |
|---|
| 221 | //DEBUG(9, USRP_UHD, "recv data") |
|---|
| 222 | |
|---|
| 223 | //Handle UHD error codes |
|---|
| 224 | rx_error_helper(md.error_code); |
|---|
| 225 | |
|---|
| 226 | for(int j=0;j<rx_channels;j++){ |
|---|
| 227 | |
|---|
| 228 | for (unsigned int i = 0; i < num_rx_samps; i++) { |
|---|
| 229 | I[i] = (CORBA::Short) rx_buff[j][i].real(); |
|---|
| 230 | Q[i] = (CORBA::Short) rx_buff[j][i].imag(); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | //DEBUG(9, USRP_UHD, "before push packet") |
|---|
| 234 | rx_data_ports[j]->pushPacket(I, Q); |
|---|
| 235 | //DEBUG(9, USRP_UHD, "after push packet") |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | if (rx_packet_count != -1) |
|---|
| 241 | --rx_packet_count; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | DEBUG(3, USRP_UHD, "Exiting rx_data_process thread") |
|---|
| 245 | //tells the device to stop streaming samples |
|---|
| 246 | stream_cmd = uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS; |
|---|
| 247 | sdev->issue_stream_cmd(stream_cmd); |
|---|
| 248 | rx_active = false; |
|---|
| 249 | rx_thread->exit(); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | // USRP_UHD Expects data in little endian format |
|---|
| 255 | static inline short convertToLE(short s) |
|---|
| 256 | { |
|---|
| 257 | #ifdef __powerpc__ |
|---|
| 258 | s = bswap_16(s); |
|---|
| 259 | #endif |
|---|
| 260 | |
|---|
| 261 | return s; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | void USRP_UHD_i::tx_data_process() |
|---|
| 265 | { |
|---|
| 266 | PortTypes::ShortSequence *I_in, *Q_in; |
|---|
| 267 | |
|---|
| 268 | const unsigned int tx_buf_len(USRP_UHD_MAX_TX_SAMPLES); |
|---|
| 269 | |
|---|
| 270 | uhd::stream_args_t stream_args("sc16"); //complex short |
|---|
| 271 | stream_args.args["noclear"] = "1"; |
|---|
| 272 | for (size_t chan = 0; chan < tx_channels; chan++) |
|---|
| 273 | stream_args.channels.push_back(chan); //linear mapping |
|---|
| 274 | uhd::tx_streamer::sptr tx_stream = sdev->get_tx_stream(stream_args); |
|---|
| 275 | |
|---|
| 276 | uhd::tx_metadata_t md3; |
|---|
| 277 | |
|---|
| 278 | //Set start and end of burst flags to help with packet fragmentation if needed |
|---|
| 279 | //Host will send ACK packets when an EOB flag is received |
|---|
| 280 | //SOB true -> first packet in sequence gets SOB flag |
|---|
| 281 | //EOB true -> last packet in sequence gets EOB flag |
|---|
| 282 | md3.start_of_burst = false; |
|---|
| 283 | md3.end_of_burst = false; |
|---|
| 284 | |
|---|
| 285 | //We can send packets in the future if needed |
|---|
| 286 | //md.has_time_spec = 0; //no time |
|---|
| 287 | //md.time_spec = uhd::time_spec_t((time_t)0 ); |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | DEBUG(3, USRP_UHD, "Starting tx_data_process thread.") |
|---|
| 291 | |
|---|
| 292 | tx_active = true; // Cleared to stop TX in USRP_UHD_TX_Control->stop() |
|---|
| 293 | size_t samps_to_send = 0; |
|---|
| 294 | std::vector<std::complex<short> *> tx_buff_ptrs; |
|---|
| 295 | DEBUG(3, USRP_UHD, "Max Number of Sample="<<tx_stream->get_max_num_samps()) |
|---|
| 296 | while (tx_active) { |
|---|
| 297 | |
|---|
| 298 | for(int i=0;i<tx_channels;i++){ |
|---|
| 299 | |
|---|
| 300 | tx_data_ports[i]->getData(I_in, Q_in); |
|---|
| 301 | samps_to_send = I_in->length(); |
|---|
| 302 | |
|---|
| 303 | std::vector<std::complex<short> > tx_buff(samps_to_send); |
|---|
| 304 | |
|---|
| 305 | for (unsigned int j = 0; j < samps_to_send; ++j) { |
|---|
| 306 | tx_buff[j] = std::complex<short int> (convertToLE((*I_in)[j]),convertToLE((*Q_in)[j])); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | tx_buff_ptrs.push_back(&tx_buff.front()); |
|---|
| 310 | tx_data_ports[i]->bufferEmptied(); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | //send the entire packet (driver fragments internally) |
|---|
| 314 | size_t num_tx_samps = tx_stream->send(tx_buff_ptrs, samps_to_send, md3); |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | } |
|---|
| 319 | DEBUG(3, USRP_UHD, "Exiting tx_data_process thread.") |
|---|
| 320 | tx_thread->exit(); |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | void print_tree(const uhd::fs_path &path, uhd::property_tree::sptr tree){ |
|---|
| 324 | std::cout << path << std::endl; |
|---|
| 325 | BOOST_FOREACH(const std::string &name, tree->list(path)){ |
|---|
| 326 | print_tree(path / name, tree); |
|---|
| 327 | } |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | void USRP_UHD_i::probe_usrp() |
|---|
| 332 | { |
|---|
| 333 | DEBUG(3, USRP_UHD, "Probing daughter board of USRP..") |
|---|
| 334 | uhd::property_tree::sptr tree = dev->get_tree(); |
|---|
| 335 | uhd::fs_path db_path, mb_path = "/mboards"; |
|---|
| 336 | mb_count = tree->list("/mboards").size(); |
|---|
| 337 | std::cout<<" The number of MB : "<<mb_count<<std::endl; |
|---|
| 338 | for(int i=0;i<mb_count;i++){ |
|---|
| 339 | mb_path = "/mboards"; |
|---|
| 340 | mb_path = mb_path / tree->list("/mboards")[i]; |
|---|
| 341 | db_count = tree->list( mb_path / "dboards").size(); |
|---|
| 342 | std::cout<<" The number of DB slot: "<<db_count<<std::endl; |
|---|
| 343 | for(int j=0;j<db_count;j++){ |
|---|
| 344 | db_path = mb_path / "dboards" / tree->list( mb_path / "dboards")[j]; |
|---|
| 345 | uhd::usrp::dboard_eeprom_t prom = tree->access<uhd::usrp::dboard_eeprom_t>(db_path / "tx_eeprom").get(); |
|---|
| 346 | std::cout<<" TxDB ID : "<<prom.id.to_string()<<std::endl; |
|---|
| 347 | |
|---|
| 348 | prom = tree->access<uhd::usrp::dboard_eeprom_t>(db_path / "rx_eeprom").get(); |
|---|
| 349 | std::cout<<" RxDB ID : "<<prom.id.to_string()<<std::endl; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | } |
|---|