| 
									
										
										
										
											2020-10-02 22:45:28 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2023-11-19 05:24:06 +01:00
										 |  |  | // Copyright (C) 2020, 2022-2023 Jon Beniston, M7RCE <jon@beniston.com>          //
 | 
					
						
							|  |  |  | // Copyright (C) 2020, 2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com>         //
 | 
					
						
							| 
									
										
										
										
											2020-10-02 22:45:28 +01:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is free software; you can redistribute it and/or modify          //
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by          //
 | 
					
						
							|  |  |  | // the Free Software Foundation as version 3 of the License, or                  //
 | 
					
						
							|  |  |  | // (at your option) any later version.                                           //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,               //
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of                //
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  //
 | 
					
						
							|  |  |  | // GNU General Public License V3 for more details.                               //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License             //
 | 
					
						
							|  |  |  | // along with this program. If not, see <http://www.gnu.org/licenses/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <cstdio>
 | 
					
						
							|  |  |  | #include <cstring>
 | 
					
						
							|  |  |  | #include <cmath>
 | 
					
						
							|  |  |  | #include <regex>
 | 
					
						
							| 
									
										
										
										
											2020-10-25 19:58:32 +00:00
										 |  |  | #include <thread>
 | 
					
						
							| 
									
										
										
										
											2020-10-02 22:45:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <uhd/types/device_addr.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "deviceusrpparam.h"
 | 
					
						
							|  |  |  | #include "deviceusrp.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DeviceUSRP::enumOriginDevices(const QString& hardwareId, PluginInterface::OriginDevices& originDevices) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     try | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         uhd::device_addr_t hint; // Discover all devices
 | 
					
						
							|  |  |  |         uhd::device_addrs_t dev_addrs = uhd::device::find(hint); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (dev_addrs.size() <= 0) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             qDebug("DeviceUSRP::enumOriginDevices: Could not find any USRP device"); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for(unsigned i = 0; i != dev_addrs.size(); i++) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             QString id = QString::fromStdString(dev_addrs[i].to_string()); | 
					
						
							| 
									
										
										
										
											2022-05-14 11:20:37 +02:00
										 |  |  |             QString name = QString::fromStdString(dev_addrs[i].get("name", "N/A")); | 
					
						
							|  |  |  |             QString serial = QString::fromStdString(dev_addrs[i].get("serial", "N/A")); | 
					
						
							| 
									
										
										
										
											2023-01-15 19:45:44 +00:00
										 |  |  |             QString product = QString::fromStdString(dev_addrs[i].get("product", "N/A")); | 
					
						
							| 
									
										
										
										
											2020-10-02 22:45:28 +01:00
										 |  |  |             QString displayedName(QString("%1[%2:$1] %3").arg(name).arg(i).arg(serial)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             qDebug() << "DeviceUSRP::enumOriginDevices: found USRP device " << displayedName; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-15 19:45:44 +00:00
										 |  |  |             // Opening some devices can be a little slow, so use hardcoded number of channels,
 | 
					
						
							|  |  |  |             // for known devices
 | 
					
						
							|  |  |  |             static const QMap<QString, int> channelMap{{"B200", 1}, {"B205", 1}, {"B210", 2}}; | 
					
						
							|  |  |  |             if (channelMap.contains(product)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 originDevices.append(PluginInterface::OriginDevice( | 
					
						
							|  |  |  |                         displayedName, | 
					
						
							|  |  |  |                         hardwareId, | 
					
						
							|  |  |  |                         id, | 
					
						
							|  |  |  |                         (int)i, | 
					
						
							|  |  |  |                         channelMap[product], | 
					
						
							|  |  |  |                         channelMap[product] | 
					
						
							|  |  |  |                     )); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 DeviceUSRPParams usrpParams; | 
					
						
							|  |  |  |                 usrpParams.open(id, true); | 
					
						
							|  |  |  |                 usrpParams.close(); | 
					
						
							| 
									
										
										
										
											2020-10-02 22:45:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-15 19:45:44 +00:00
										 |  |  |                 originDevices.append(PluginInterface::OriginDevice( | 
					
						
							|  |  |  |                         displayedName, | 
					
						
							|  |  |  |                         hardwareId, | 
					
						
							|  |  |  |                         id, | 
					
						
							|  |  |  |                         (int)i, | 
					
						
							|  |  |  |                         usrpParams.m_nbRxChannels, | 
					
						
							|  |  |  |                         usrpParams.m_nbTxChannels | 
					
						
							|  |  |  |                     )); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-10-02 22:45:28 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (const std::exception& e) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         qDebug() << "DeviceUSRP::enumOriginDevices: exception: " << e.what(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-10-25 11:57:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void DeviceUSRP::waitForLock(uhd::usrp::multi_usrp::sptr usrp, const QString& clockSource, int channel) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int tries; | 
					
						
							|  |  |  |     const int maxTries = 100; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Wait for Ref lock
 | 
					
						
							|  |  |  |     std::vector<std::string> sensor_names; | 
					
						
							|  |  |  |     sensor_names = usrp->get_tx_sensor_names(channel); | 
					
						
							|  |  |  |     if (clockSource == "external") | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (std::find(sensor_names.begin(), sensor_names.end(), "ref_locked") != sensor_names.end()) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             for (tries = 0; !usrp->get_mboard_sensor("ref_locked", 0).to_bool() && (tries < maxTries); tries++) | 
					
						
							|  |  |  |                 std::this_thread::sleep_for(std::chrono::milliseconds(10)); | 
					
						
							|  |  |  |             if (tries == maxTries) | 
					
						
							|  |  |  |                 qCritical("USRPInput::acquireChannel: Failed to lock ref"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // Wait for LO lock
 | 
					
						
							|  |  |  |     if (std::find(sensor_names.begin(), sensor_names.end(), "lo_locked") != sensor_names.end()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         for (tries = 0; !usrp->get_tx_sensor("lo_locked", channel).to_bool() && (tries < maxTries); tries++) | 
					
						
							|  |  |  |             std::this_thread::sleep_for(std::chrono::milliseconds(10)); | 
					
						
							|  |  |  |         if (tries == maxTries) | 
					
						
							|  |  |  |             qCritical("USRPInput::acquireChannel: Failed to lock LO"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |