uhd_io_cpp.cpp 12.1 KB
Newer Older
M
manjaro 已提交
1 2 3 4 5
/*
 * Copyright 2015 Ettus Research LLC
 * Copyright 2018 Ettus Research, a National Instruments Company
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
M
manjaro 已提交
6
 * 丁劲犇修改 2021
M
manjaro 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 */

#include <uhd/types/tune_request.hpp>
#include <uhd/usrp/multi_usrp.hpp>
#include <uhd/utils/safe_main.hpp>
#include <uhd/utils/thread.hpp>
#include <chrono>
#include <complex>
#include <csignal>
#include <fstream>
#include <iostream>
#include <thread>
#include <vector>
using uhd::tune_request_t;
using uhd::tx_streamer;
using uhd::usrp::multi_usrp;
using uhd::rx_streamer;
using std::thread;
using std::cerr;
using std::endl;
M
manjaro 已提交
27 28 29
using std::string;
using std::vector;
using std::shared_ptr;
M
manjaro 已提交
30 31 32 33 34 35

static bool stop_signal_called = false;
void sigint_handler(int code){
	(void)code;
	stop_signal_called = true;
}
M
manjaro 已提交
36 37 38 39
/*!
 * \brief The tag_channelOptions struct
 * 通道配置参数
 */
M
manjaro 已提交
40
struct tag_channelOptions{
M
manjaro 已提交
41 42 43 44 45
	string type = "sc16";		//样点类型,为上位机上类型: fc64, fc32, or sc16
	string subdev="";			//通道,A:A, A:B两个通道,对应B210左侧、右侧接口.一般默认配置,用信道号channels选取接口。
	string ant = "TX/RX";		//天线选取,B210 有 TX/RX 或者 RX2两个
	string wirefmt;			//内部类型 (sc8 or sc16),是片上处理的类型
	vector<size_t> channels {0};//通道号,可以设置0,1之类的。默认subdev时,0=A:A,1=A:B,subdev被修改,则采取修改后的编号
M
manjaro 已提交
46 47 48 49 50 51 52 53 54
	size_t spb	=	10000;			//缓冲大小,太小会丢包,太大会超时
	double rate = 61.44e6/2;		//采样率,单位Hz
	double freq = 1.0e9;			//射频频率,单位Hz
	double gain = 55;				//射频增益,单位dB
	double bw   = rate;				//滤波带宽,默认为采样窗口
	double lo_offset = 0;			//LO偏移,单位 Hz (缺省)
	bool   int_n_mod = false;		//int-n 模式(本例不配置)
	bool   docheck = true;			//在开始前执行检查
	double setup_time = 1.0;		//rx配置检查时间,可选。
M
manjaro 已提交
55
};
M
manjaro 已提交
56
//通道检查函数
M
manjaro 已提交
57 58
bool check_tx_status(const string & ref, multi_usrp::sptr usrp,const tag_channelOptions & op);
bool check_rx_status(const string & ref, multi_usrp::sptr usrp,const tag_channelOptions & op);
M
manjaro 已提交
59

M
manjaro 已提交
60 61 62 63
/*!
 * 范例吞吐函数,使用环形队列保持跟随收发
 */
template <class FMT = short>
M
manjaro 已提交
64 65 66 67 68 69
void do_io(
		const tag_channelOptions & oprx,
		const tag_channelOptions & optx,
		rx_streamer::sptr rx,
		tx_streamer::sptr tx)
{
M
manjaro 已提交
70 71 72 73 74
	if (oprx.channels.size()>1 || optx.channels.size()>1)
	{
		cerr << "multi channels IO is not suitable for this simple demo."<<endl;
		return;
	}
M
manjaro 已提交
75
	//初始化队列
M
manjaro 已提交
76 77
	vector< shared_ptr< FMT > > vec_buffer;
	vector< int > vec_buffersz;
M
manjaro 已提交
78 79
	const int bufsz = 65536;
	for (int i=0;i<bufsz;++i)
M
manjaro 已提交
80
	{
M
manjaro 已提交
81
		vec_buffer.push_back(shared_ptr< FMT > (new FMT[oprx.spb * 2]));
M
manjaro 已提交
82 83 84
		vec_buffersz.push_back(0);
	}
	//收发计数
M
manjaro 已提交
85
	long long rx_count = 0, tx_count = 0;
M
manjaro 已提交
86
	//接收线程
M
manjaro 已提交
87 88 89 90 91 92 93 94 95
	auto thcall_rx = [&]()->void{
		uhd::rx_metadata_t md_rx;
		uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS);
		stream_cmd.num_samps  = size_t(oprx.spb);
		stream_cmd.stream_now = true;
		stream_cmd.time_spec  = uhd::time_spec_t();
		rx->issue_stream_cmd(stream_cmd);
		while (!stop_signal_called)
		{
M
manjaro 已提交
96
			vec_buffersz[rx_count % bufsz] = rx->recv((void *)(vec_buffer[rx_count % bufsz].get()),oprx.spb,md_rx,0.1,false);
M
manjaro 已提交
97 98
			//md_rx可以读取时戳
			//auto tm_first = md_rx.time_spec;
M
manjaro 已提交
99 100 101 102 103 104 105 106 107
			++rx_count;
			if (md_rx.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT)
				fputs("Time out.",stderr);
			else if (md_rx.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW)
				fputs("Over flow",stderr);
			else if (md_rx.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE)
				cerr << "Receiver error: "<< md_rx.strerror() << endl ;
		}
	};
M
manjaro 已提交
108
	//发射线程
M
manjaro 已提交
109 110 111 112
	auto thcall_tx = [&]()->void{
		uhd::tx_metadata_t md_tx;
		md_tx.end_of_burst   = false;
		md_tx.start_of_burst = false;
M
manjaro 已提交
113
		long long last_tx = -1;
M
manjaro 已提交
114 115
		while (!stop_signal_called)
		{
M
manjaro 已提交
116 117 118 119 120 121 122
			//等待一会数据,以便TX可以保持跟随。也可以不设置,这样开始时会争夺。
			if (rx_count<100)
				continue;
			if (last_tx==tx_count)
				cerr<<"Blocked!"<<endl;
			const size_t samples_sent = tx->send((void *)(vec_buffer[tx_count % bufsz].get()),vec_buffersz[tx_count % bufsz],md_tx,0.1);
			last_tx = tx_count;
M
manjaro 已提交
123 124 125 126 127 128 129
			if (tx_count<rx_count)
				++tx_count;
			if (samples_sent != optx.spb)
				cerr<<"The tx_stream timed out sending " << optx.spb << " samples ("  << samples_sent << " sent)." << endl;
		}
	};

M
manjaro 已提交
130
	//启动线程
M
manjaro 已提交
131
	thread rx_thread(thcall_rx);
M
manjaro 已提交
132
	thread tx_thread(thcall_tx);
M
manjaro 已提交
133
	cerr<<"Press ^C to Stop."<<endl;
M
manjaro 已提交
134
	//主线程不断打印状态
M
manjaro 已提交
135 136 137 138 139
	while (!stop_signal_called)
	{
		cerr<<"RX" << rx_count<< " TX " << tx_count<<"\r";
		std::this_thread::sleep_for(std::chrono::milliseconds(400));
	}
M
manjaro 已提交
140
	//退出
M
manjaro 已提交
141 142 143 144 145 146 147 148
	rx_thread.join();
	tx_thread.join();

}


int UHD_SAFE_MAIN(int /*argc*/, char* /*argv*/[])
{
M
manjaro 已提交
149
	//1.创建一个设备,可以不填写,则使用第一台设备。
M
manjaro 已提交
150
	string args ("");
M
manjaro 已提交
151
	//2.设置时钟参考 (internal, external, mimo)
M
manjaro 已提交
152
	string ref = "internal";
M
manjaro 已提交
153 154
	cerr << "Creating the usrp device with: "  << args  <<"..."<< endl;
	multi_usrp::sptr usrp = multi_usrp::make(args);
M
manjaro 已提交
155
	usrp->set_clock_source(ref,multi_usrp::ALL_MBOARDS);
M
manjaro 已提交
156

M
manjaro 已提交
157
	//3.配置发射
M
manjaro 已提交
158
	tag_channelOptions tx_op;
M
manjaro 已提交
159 160 161
	tx_op.freq = 89e6;
	tx_op.bw = 400e3;
	tx_op.gain=70;
M
manjaro 已提交
162 163
	tx_op.channels[0] = 1;
	//3.1子设备配置(默认),一般不动它。
M
manjaro 已提交
164
	if (tx_op.subdev.size())
M
manjaro 已提交
165
		usrp->set_tx_subdev_spec(tx_op.subdev, multi_usrp::ALL_MBOARDS);
M
manjaro 已提交
166
	cerr << "TX Using Device: " << usrp->get_pp_string() << endl;
M
manjaro 已提交
167
	//3.2速率配置
M
manjaro 已提交
168
	cerr << "Setting TX Rate: " <<  (tx_op.rate / 1e6) << "Msps..." << endl;
M
manjaro 已提交
169 170
	usrp->set_tx_rate(tx_op.rate, multi_usrp::ALL_CHANS);
	cerr << "Actual TX Rate: " << usrp->get_tx_rate(tx_op.channels[0]) / 1e6 << "Msps..." << endl;
M
manjaro 已提交
171
	//3.3中心频率配置
M
manjaro 已提交
172 173 174 175 176
	cerr << "Setting TX Freq: "  << (tx_op.freq / 1e6) <<"MHz..." << endl;
	cerr << "Setting TX LO Offset: " << (tx_op.lo_offset / 1e6) << "MHz..." <<endl;
	tune_request_t tune_request_tx = tune_request_t(tx_op.freq, tx_op.lo_offset);
	if (tx_op.int_n_mod)
		tune_request_tx.args = uhd::device_addr_t("mode_n=integer");
M
manjaro 已提交
177 178
	usrp->set_tx_freq(tune_request_tx,tx_op.channels[0]);
	cerr << "Actual TX Freq: " <<  (usrp->get_tx_freq(tx_op.channels[0]) / 1e6) << "MHz..." << endl;
M
manjaro 已提交
179
	//3.4增益配置
M
manjaro 已提交
180
	cerr << "Setting TX Gain: " << tx_op.gain <<" dB..." << endl;
M
manjaro 已提交
181 182
	usrp->set_tx_gain(tx_op.gain,tx_op.channels[0]);
	cerr << "Actual TX Gain: " << usrp->get_tx_gain(tx_op.channels[0]) << " dB..." << endl;
M
manjaro 已提交
183
	//3.5模拟前端滤波器带宽配置
M
manjaro 已提交
184
	cerr << "Setting TX Bandwidth: " << (tx_op.bw / 1e6) << "MHz..."  << endl;
M
manjaro 已提交
185 186
	usrp->set_tx_bandwidth(tx_op.bw,tx_op.channels[0]);
	cerr << "Actual TX Bandwidth: " << usrp->get_tx_bandwidth(tx_op.channels[0]) / 1e6 << "MHz..." << endl;
M
manjaro 已提交
187
	//3.6指定天线
M
manjaro 已提交
188
	if (tx_op.ant.size())
M
manjaro 已提交
189
		usrp->set_tx_antenna(tx_op.ant,tx_op.channels[0]);
M
manjaro 已提交
190

M
manjaro 已提交
191
	//4.配置接收
M
manjaro 已提交
192 193
	tag_channelOptions rx_op;
	rx_op.ant = "RX2";
M
manjaro 已提交
194 195 196
	rx_op.bw = 400e3;
	rx_op.freq = 107.7e6;
	rx_op.gain = 50;
M
manjaro 已提交
197
	rx_op.channels[0] = 0;
M
manjaro 已提交
198
	//4.1 子设备
M
manjaro 已提交
199
	if (rx_op.subdev.size())
M
manjaro 已提交
200
		usrp->set_rx_subdev_spec(rx_op.subdev,multi_usrp::ALL_MBOARDS);
M
manjaro 已提交
201
	cerr << "RX Using Device: " << usrp->get_pp_string() << endl;
M
manjaro 已提交
202
	//4.2 采样率
M
manjaro 已提交
203
	cerr << "Setting RX Rate: " <<  (rx_op.rate / 1e6) << "Msps..." << endl;
M
manjaro 已提交
204 205
	usrp->set_rx_rate(rx_op.rate,multi_usrp::ALL_CHANS);
	cerr << "Actual RX Rate: " << usrp->get_rx_rate(rx_op.channels[0]) / 1e6 << "Msps..." << endl;
M
manjaro 已提交
206
	//4.3 中心频率
M
manjaro 已提交
207 208 209 210 211
	cerr << "Setting RX Freq: "  << (rx_op.freq / 1e6) <<"MHz..." << endl;
	cerr << "Setting RX LO Offset: " << (rx_op.lo_offset / 1e6) << "MHz..." <<endl;
	tune_request_t tune_request_rx = tune_request_t(rx_op.freq, rx_op.lo_offset);
	if (rx_op.int_n_mod)
		tune_request_rx.args = uhd::device_addr_t("mode_n=integer");
M
manjaro 已提交
212 213
	usrp->set_rx_freq(tune_request_rx,rx_op.channels[0]);
	cerr << "Actual RX Freq: " <<  (usrp->get_rx_freq(rx_op.channels[0]) / 1e6) << "MHz..." << endl;
M
manjaro 已提交
214
	//4.4 增益
M
manjaro 已提交
215
	cerr << "Setting RX Gain: " << rx_op.gain <<" dB..." << endl;
M
manjaro 已提交
216 217
	usrp->set_rx_gain(rx_op.gain,rx_op.channels[0]);
	cerr << "Actual RX Gain: " << usrp->get_rx_gain(rx_op.channels[0]) << " dB..." << endl;
M
manjaro 已提交
218
	//4.5 前端模拟滤波带宽
M
manjaro 已提交
219
	cerr << "Setting RX Bandwidth: " << (rx_op.bw / 1e6) << "MHz..."  << endl;
M
manjaro 已提交
220 221
	usrp->set_rx_bandwidth(rx_op.bw,rx_op.channels[0]);
	cerr << "Actual RX Bandwidth: " << usrp->get_rx_bandwidth(rx_op.channels[0]) / 1e6 << "MHz..." << endl;
M
manjaro 已提交
222
	//4.6 选择天线
M
manjaro 已提交
223
	if (rx_op.ant.size())
M
manjaro 已提交
224
		usrp->set_rx_antenna(rx_op.ant,rx_op.channels[0]);
M
manjaro 已提交
225

M
manjaro 已提交
226
	//5 检查状态
M
manjaro 已提交
227
	if (tx_op.docheck)	check_tx_status(ref,usrp,tx_op);
M
manjaro 已提交
228 229 230 231
	if (rx_op.docheck)	check_rx_status(ref,usrp,rx_op);

	//6.创建流对象实例
	//6.1 发射
M
manjaro 已提交
232
	uhd::stream_args_t stream_args_tx(tx_op.type, tx_op.wirefmt);
M
manjaro 已提交
233
	stream_args_tx.channels = tx_op.channels;
M
manjaro 已提交
234
	tx_streamer::sptr tx_stream = usrp->get_tx_stream(stream_args_tx);
M
manjaro 已提交
235
	// 6.2 接收
M
manjaro 已提交
236
	uhd::stream_args_t stream_args_rx(rx_op.type, rx_op.wirefmt);
M
manjaro 已提交
237
	stream_args_rx.channels             = rx_op.channels;
M
manjaro 已提交
238 239
	rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args_rx);

M
manjaro 已提交
240
	//开始收发循环
M
manjaro 已提交
241 242 243 244 245 246 247 248 249
	do_io<short>(rx_op,tx_op,rx_stream,tx_stream);

	// finished
	cerr << endl << "Done!" << endl << endl;

	return EXIT_SUCCESS;
}


M
manjaro 已提交
250
bool check_tx_status(const string & ref, multi_usrp::sptr usrp,const tag_channelOptions & op)
M
manjaro 已提交
251 252
{
	// Check Ref and LO Lock detect
M
manjaro 已提交
253 254 255 256 257 258 259 260 261 262 263
	vector<string> sensor_names;
	for (size_t c :op.channels)
	{
		sensor_names = usrp->get_tx_sensor_names(c);
		if (std::find(sensor_names.begin(), sensor_names.end(), "lo_locked")
				!= sensor_names.end()) {
			uhd::sensor_value_t lo_locked = usrp->get_tx_sensor("lo_locked",op.channels[0]);
			cerr <<"Checking TX: "<< lo_locked.to_pp_string() << std::endl;
			UHD_ASSERT_THROW(lo_locked.to_bool());
		}

M
manjaro 已提交
264 265 266 267 268
	}
	sensor_names = usrp->get_mboard_sensor_names(0);
	if ((ref == "mimo")
			and (std::find(sensor_names.begin(), sensor_names.end(), "mimo_locked")
				 != sensor_names.end())) {
M
manjaro 已提交
269
		uhd::sensor_value_t mimo_locked = usrp->get_mboard_sensor("mimo_locked",0);
M
manjaro 已提交
270 271 272 273 274 275 276 277 278 279 280 281
		std::cerr << "Checking TX: "<< mimo_locked.to_pp_string()  << std::endl;
		UHD_ASSERT_THROW(mimo_locked.to_bool());
	}
	if ((ref == "external")
			and (std::find(sensor_names.begin(), sensor_names.end(), "ref_locked")
				 != sensor_names.end())) {
		uhd::sensor_value_t ref_locked = usrp->get_mboard_sensor("ref_locked", 0);
		std::cout << "Checking TX: %s ..."<< ref_locked.to_pp_string()  << std::endl;
		UHD_ASSERT_THROW(ref_locked.to_bool());
	}
	return true;
}
M
manjaro 已提交
282 283 284 285 286
typedef std::function<uhd::sensor_value_t(const string&)> get_sensor_fn_t;
bool check_locked_sensor(vector<string> sensor_names,
						 const char* sensor_name,
						 get_sensor_fn_t get_sensor_fn,
						 double setup_time)
M
manjaro 已提交
287 288
{
	if (std::find(sensor_names.begin(), sensor_names.end(), sensor_name)
M
manjaro 已提交
289
			== sensor_names.end())
M
manjaro 已提交
290 291 292
		return false;

	auto setup_timeout = std::chrono::steady_clock::now()
M
manjaro 已提交
293
			+ std::chrono::milliseconds(int64_t(setup_time * 1000));
M
manjaro 已提交
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
	bool lock_detected = false;

	std::cerr << "Checking RX Waiting for: " << sensor_name;
	std::cerr.flush();

	while (true) {
		if (lock_detected and (std::chrono::steady_clock::now() > setup_timeout)) {
			std::cerr << " locked." << std::endl;
			break;
		}
		if (get_sensor_fn(sensor_name).to_bool()) {
			std::cerr << "+";
			std::cerr.flush();
			lock_detected = true;
		} else {
			if (std::chrono::steady_clock::now() > setup_timeout) {
				std::cerr << std::endl;
				std::cerr << "timed out waiting for consecutive locks on sensor : "<<sensor_name;
				return false;
			}
			std::cerr.flush();
		}
		std::this_thread::sleep_for(std::chrono::milliseconds(100));
	}
	std::cout << std::endl;
	return true;
}

M
manjaro 已提交
322
bool check_rx_status(const string & ref, multi_usrp::sptr usrp,const tag_channelOptions & op)
M
manjaro 已提交
323 324
{
	// check Ref and LO Lock detect
M
manjaro 已提交
325 326 327 328 329 330 331 332 333
	for (size_t c :op.channels)
	{
		check_locked_sensor(usrp->get_rx_sensor_names(c),
							"lo_locked",
							[usrp, op, c](const string& sensor_name) {
			return usrp->get_rx_sensor(sensor_name, c);
		},
		op.setup_time);
	}
M
manjaro 已提交
334 335 336
	if (ref == "mimo") {
		check_locked_sensor(usrp->get_mboard_sensor_names(0),
							"mimo_locked",
M
manjaro 已提交
337
							[usrp](const string& sensor_name) {
M
manjaro 已提交
338 339 340 341 342 343 344
			return usrp->get_mboard_sensor(sensor_name);
		},
		op.setup_time);
	}
	if (ref == "external") {
		check_locked_sensor(usrp->get_mboard_sensor_names(0),
							"ref_locked",
M
manjaro 已提交
345
							[usrp](const string& sensor_name) {
M
manjaro 已提交
346 347 348 349 350 351
			return usrp->get_mboard_sensor(sensor_name);
		},
		op.setup_time);
	}
	return true;
}