blob: 8295a54104640b182809b490762420c60598866f [file] [log] [blame]
Ericb7253c62022-11-28 19:21:08 +01001#pragma once
2/*
3 * (C) 2022 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Eric Wild <ewild@sysmocom.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <uhd/version.hpp>
24#include <uhd/usrp/multi_usrp.hpp>
25#include <uhd/types/metadata.hpp>
26#include <complex>
27#include <cstring>
28#include <iostream>
29#include <thread>
30
31#include <Timeval.h>
32#include <vector>
33
34using blade_sample_type = std::complex<int16_t>;
35const int SAMPLE_SCALE_FACTOR = 1;
36
37struct uhd_buf_wrap {
38 double rxticks;
39 size_t num_samps;
40 uhd::rx_metadata_t *md;
41 blade_sample_type *buf;
42 auto actual_samples_per_buffer()
43 {
44 return num_samps;
45 }
46 long get_first_ts()
47 {
48 return md->time_spec.to_ticks(rxticks);
49 }
50 int readall(blade_sample_type *outaddr)
51 {
52 memcpy(outaddr, buf, num_samps * sizeof(blade_sample_type));
53 return num_samps;
54 }
55 int read_n(blade_sample_type *outaddr, int start, int num)
56 {
57 assert(start >= 0);
58 auto to_read = std::min((int)num_samps - start, num);
59 assert(to_read >= 0);
60 memcpy(outaddr, buf + start, to_read * sizeof(blade_sample_type));
61 return to_read;
62 }
63};
64
65using dev_buf_t = uhd_buf_wrap;
66using bh_fn_t = std::function<int(dev_buf_t *)>;
67
68template <typename T> struct uhd_hw {
69 uhd::usrp::multi_usrp::sptr dev;
70 uhd::rx_streamer::sptr rx_stream;
71 uhd::tx_streamer::sptr tx_stream;
72 blade_sample_type *one_pkt_buf;
73 std::vector<blade_sample_type *> pkt_ptrs;
74 size_t rx_spp;
75 double rxticks;
76 const unsigned int rxFullScale, txFullScale;
77 const int rxtxdelay;
78 float rxgain, txgain;
79 static std::atomic<bool> stop_me_flag;
80
81 virtual ~uhd_hw()
82 {
83 delete[] one_pkt_buf;
84 }
Eric097a16e2023-03-02 17:46:49 +010085 uhd_hw() : rxFullScale(32767), txFullScale(32767 * 0.3), rxtxdelay(-67)
Ericb7253c62022-11-28 19:21:08 +010086 {
87 }
88
89 void close_device()
90 {
91 }
92
93 bool tuneTx(double freq, size_t chan = 0)
94 {
95 msleep(25);
96 dev->set_tx_freq(freq, chan);
97 msleep(25);
98 return true;
99 };
100 bool tuneRx(double freq, size_t chan = 0)
101 {
102 msleep(25);
103 dev->set_rx_freq(freq, chan);
104 msleep(25);
105 return true;
106 };
107 bool tuneRxOffset(double offset, size_t chan = 0)
108 {
109 return true;
110 };
111
112 double setRxGain(double dB, size_t chan = 0)
113 {
114 rxgain = dB;
115 msleep(25);
116 dev->set_rx_gain(dB, chan);
117 msleep(25);
118 return dB;
119 };
120 double setTxGain(double dB, size_t chan = 0)
121 {
122 txgain = dB;
123 msleep(25);
124 dev->set_tx_gain(dB, chan);
125 msleep(25);
126 return dB;
127 };
128 int setPowerAttenuation(int atten, size_t chan = 0)
129 {
130 return atten;
131 };
132
133 int init_device(bh_fn_t rxh, bh_fn_t txh)
134 {
135 auto const lock_delay_ms = 500;
Eric Wild10b4e312023-01-11 18:53:48 +0100136 auto clock_lock_attempts = 15; // x lock_delay_ms
Ericb7253c62022-11-28 19:21:08 +0100137 auto const mcr = 26e6;
138 auto const rate = (1625e3 / 6) * 4;
139 auto const ref = "external";
140 auto const gain = 35;
141 auto const freq = 931.4e6; // 936.8e6
142 auto bw = 0.5e6;
143 auto const channel = 0;
144 // aligned to blade: 1020 samples per transfer
145 std::string args = { "recv_frame_size=4092,send_frame_size=4092" };
146
147 dev = uhd::usrp::multi_usrp::make(args);
148 std::cout << "Using Device: " << dev->get_pp_string() << std::endl;
149 dev->set_clock_source(ref);
150 dev->set_master_clock_rate(mcr);
151 dev->set_rx_rate(rate, channel);
152 dev->set_tx_rate(rate, channel);
153 uhd::tune_request_t tune_request(freq, 0);
154 dev->set_rx_freq(tune_request, channel);
155 dev->set_rx_gain(gain, channel);
156 dev->set_tx_gain(60, channel);
157 dev->set_rx_bandwidth(bw, channel);
158 dev->set_tx_bandwidth(bw, channel);
159
160 while (!(dev->get_rx_sensor("lo_locked", channel).to_bool() &&
Eric Wild10b4e312023-01-11 18:53:48 +0100161 dev->get_mboard_sensor("ref_locked").to_bool()) &&
162 clock_lock_attempts > 0) {
163 std::cerr << "clock source lock attempts remaining: " << clock_lock_attempts << ".."
164 << std::endl;
Ericb7253c62022-11-28 19:21:08 +0100165 std::this_thread::sleep_for(std::chrono::milliseconds(lock_delay_ms));
Eric Wild10b4e312023-01-11 18:53:48 +0100166 clock_lock_attempts--;
167 }
168
169 if (clock_lock_attempts <= 0) {
170 std::cerr << "Error locking clock, gpsdo missing? quitting.." << std::endl;
171 return -1;
172 }
Ericb7253c62022-11-28 19:21:08 +0100173
174 uhd::stream_args_t stream_args("sc16", "sc16");
175 rx_stream = dev->get_rx_stream(stream_args);
176 uhd::stream_args_t stream_args2("sc16", "sc16");
177 tx_stream = dev->get_tx_stream(stream_args2);
178
179 rx_spp = rx_stream->get_max_num_samps();
180 rxticks = dev->get_rx_rate();
181 assert(rxticks == dev->get_tx_rate());
182 one_pkt_buf = new blade_sample_type[rx_spp];
183 pkt_ptrs = { 1, &one_pkt_buf[0] };
184 return 0;
185 }
186
187 void *rx_cb(bh_fn_t burst_handler)
188 {
189 void *ret = nullptr;
190 static int to_skip = 0;
191
192 uhd::rx_metadata_t md;
193 auto num_rx_samps = rx_stream->recv(pkt_ptrs.front(), rx_spp, md, 1.0, true);
194
195 if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) {
196 std::cerr << boost::format("Timeout while streaming") << std::endl;
197 exit(0);
198 }
199 if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW) {
200 std::cerr << boost::format("Got an overflow indication\n") << std::endl;
201 exit(0);
202 }
203 if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE) {
204 std::cerr << str(boost::format("Receiver error: %s") % md.strerror());
205 exit(0);
206 }
207
208 dev_buf_t rcd = { rxticks, num_rx_samps, &md, &one_pkt_buf[0] };
209
210 if (to_skip < 120) // prevents weird overflows on startup
211 to_skip++;
212 else {
213 burst_handler(&rcd);
214 }
215
216 return ret;
217 }
218
219 auto get_rx_burst_handler_fn(bh_fn_t burst_handler)
220 {
221 auto fn = [this, burst_handler] {
222 pthread_setname_np(pthread_self(), "rxrun");
223
224 uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS);
225 stream_cmd.stream_now = true;
226 stream_cmd.time_spec = uhd::time_spec_t();
227 rx_stream->issue_stream_cmd(stream_cmd);
228
229 while (!stop_me_flag) {
230 rx_cb(burst_handler);
231 }
232 };
233 return fn;
234 }
235 auto get_tx_burst_handler_fn(bh_fn_t burst_handler)
236 {
237 auto fn = [] {
238 // dummy
239 };
240 return fn;
241 }
242 void submit_burst_ts(blade_sample_type *buffer, int len, uint64_t ts)
243 {
244 uhd::tx_metadata_t m = {};
245 m.end_of_burst = true;
246 m.start_of_burst = true;
247 m.has_time_spec = true;
248 m.time_spec = m.time_spec.from_ticks(ts + rxtxdelay, rxticks); // uhd specific b210 delay!
249 std::vector<void *> ptrs(1, buffer);
250
251 tx_stream->send(ptrs, len, m, 1.0);
252#ifdef DBGXX
253 uhd::async_metadata_t async_md;
254 bool tx_ack = false;
255 while (!tx_ack && tx_stream->recv_async_msg(async_md)) {
256 tx_ack = (async_md.event_code == uhd::async_metadata_t::EVENT_CODE_BURST_ACK);
257 }
258 std::cout << (tx_ack ? "yay" : "nay") << " " << async_md.time_spec.to_ticks(rxticks) << std::endl;
259#endif
260 }
261};