blob: 07fc44945667258d37098ab7333146547b4a04bf [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
Eric2e6c3622023-05-02 14:34:49 +020068template <typename T>
69struct uhd_hw {
Ericb7253c62022-11-28 19:21:08 +010070 uhd::usrp::multi_usrp::sptr dev;
71 uhd::rx_streamer::sptr rx_stream;
72 uhd::tx_streamer::sptr tx_stream;
73 blade_sample_type *one_pkt_buf;
74 std::vector<blade_sample_type *> pkt_ptrs;
75 size_t rx_spp;
76 double rxticks;
77 const unsigned int rxFullScale, txFullScale;
78 const int rxtxdelay;
79 float rxgain, txgain;
80 static std::atomic<bool> stop_me_flag;
81
82 virtual ~uhd_hw()
83 {
84 delete[] one_pkt_buf;
85 }
Eric097a16e2023-03-02 17:46:49 +010086 uhd_hw() : rxFullScale(32767), txFullScale(32767 * 0.3), rxtxdelay(-67)
Ericb7253c62022-11-28 19:21:08 +010087 {
88 }
89
90 void close_device()
91 {
92 }
93
94 bool tuneTx(double freq, size_t chan = 0)
95 {
96 msleep(25);
97 dev->set_tx_freq(freq, chan);
98 msleep(25);
99 return true;
100 };
101 bool tuneRx(double freq, size_t chan = 0)
102 {
103 msleep(25);
104 dev->set_rx_freq(freq, chan);
105 msleep(25);
106 return true;
107 };
108 bool tuneRxOffset(double offset, size_t chan = 0)
109 {
110 return true;
111 };
112
113 double setRxGain(double dB, size_t chan = 0)
114 {
115 rxgain = dB;
116 msleep(25);
117 dev->set_rx_gain(dB, chan);
118 msleep(25);
119 return dB;
120 };
121 double setTxGain(double dB, size_t chan = 0)
122 {
123 txgain = dB;
124 msleep(25);
125 dev->set_tx_gain(dB, chan);
126 msleep(25);
127 return dB;
128 };
129 int setPowerAttenuation(int atten, size_t chan = 0)
130 {
131 return atten;
132 };
133
134 int init_device(bh_fn_t rxh, bh_fn_t txh)
135 {
136 auto const lock_delay_ms = 500;
Eric Wild10b4e312023-01-11 18:53:48 +0100137 auto clock_lock_attempts = 15; // x lock_delay_ms
Ericb7253c62022-11-28 19:21:08 +0100138 auto const mcr = 26e6;
139 auto const rate = (1625e3 / 6) * 4;
140 auto const ref = "external";
141 auto const gain = 35;
142 auto const freq = 931.4e6; // 936.8e6
143 auto bw = 0.5e6;
144 auto const channel = 0;
145 // aligned to blade: 1020 samples per transfer
146 std::string args = { "recv_frame_size=4092,send_frame_size=4092" };
147
148 dev = uhd::usrp::multi_usrp::make(args);
149 std::cout << "Using Device: " << dev->get_pp_string() << std::endl;
150 dev->set_clock_source(ref);
151 dev->set_master_clock_rate(mcr);
152 dev->set_rx_rate(rate, channel);
153 dev->set_tx_rate(rate, channel);
154 uhd::tune_request_t tune_request(freq, 0);
155 dev->set_rx_freq(tune_request, channel);
156 dev->set_rx_gain(gain, channel);
157 dev->set_tx_gain(60, channel);
158 dev->set_rx_bandwidth(bw, channel);
159 dev->set_tx_bandwidth(bw, channel);
160
161 while (!(dev->get_rx_sensor("lo_locked", channel).to_bool() &&
Eric Wild10b4e312023-01-11 18:53:48 +0100162 dev->get_mboard_sensor("ref_locked").to_bool()) &&
163 clock_lock_attempts > 0) {
164 std::cerr << "clock source lock attempts remaining: " << clock_lock_attempts << ".."
165 << std::endl;
Ericb7253c62022-11-28 19:21:08 +0100166 std::this_thread::sleep_for(std::chrono::milliseconds(lock_delay_ms));
Eric Wild10b4e312023-01-11 18:53:48 +0100167 clock_lock_attempts--;
168 }
169
170 if (clock_lock_attempts <= 0) {
171 std::cerr << "Error locking clock, gpsdo missing? quitting.." << std::endl;
172 return -1;
173 }
Ericb7253c62022-11-28 19:21:08 +0100174
175 uhd::stream_args_t stream_args("sc16", "sc16");
176 rx_stream = dev->get_rx_stream(stream_args);
177 uhd::stream_args_t stream_args2("sc16", "sc16");
178 tx_stream = dev->get_tx_stream(stream_args2);
179
180 rx_spp = rx_stream->get_max_num_samps();
181 rxticks = dev->get_rx_rate();
182 assert(rxticks == dev->get_tx_rate());
183 one_pkt_buf = new blade_sample_type[rx_spp];
184 pkt_ptrs = { 1, &one_pkt_buf[0] };
185 return 0;
186 }
187
Ericb3157b92023-05-23 11:32:34 +0200188 void actually_enable_streams()
189 {
190 // nop: stream cmd in handler
191 }
192
Ericb7253c62022-11-28 19:21:08 +0100193 void *rx_cb(bh_fn_t burst_handler)
194 {
195 void *ret = nullptr;
196 static int to_skip = 0;
197
198 uhd::rx_metadata_t md;
199 auto num_rx_samps = rx_stream->recv(pkt_ptrs.front(), rx_spp, md, 1.0, true);
200
201 if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) {
202 std::cerr << boost::format("Timeout while streaming") << std::endl;
203 exit(0);
204 }
205 if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW) {
206 std::cerr << boost::format("Got an overflow indication\n") << std::endl;
207 exit(0);
208 }
209 if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE) {
210 std::cerr << str(boost::format("Receiver error: %s") % md.strerror());
211 exit(0);
212 }
213
214 dev_buf_t rcd = { rxticks, num_rx_samps, &md, &one_pkt_buf[0] };
215
216 if (to_skip < 120) // prevents weird overflows on startup
217 to_skip++;
218 else {
219 burst_handler(&rcd);
220 }
221
222 return ret;
223 }
224
225 auto get_rx_burst_handler_fn(bh_fn_t burst_handler)
226 {
227 auto fn = [this, burst_handler] {
228 pthread_setname_np(pthread_self(), "rxrun");
229
230 uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS);
231 stream_cmd.stream_now = true;
232 stream_cmd.time_spec = uhd::time_spec_t();
233 rx_stream->issue_stream_cmd(stream_cmd);
234
235 while (!stop_me_flag) {
236 rx_cb(burst_handler);
237 }
238 };
239 return fn;
240 }
241 auto get_tx_burst_handler_fn(bh_fn_t burst_handler)
242 {
243 auto fn = [] {
244 // dummy
245 };
246 return fn;
247 }
248 void submit_burst_ts(blade_sample_type *buffer, int len, uint64_t ts)
249 {
250 uhd::tx_metadata_t m = {};
251 m.end_of_burst = true;
252 m.start_of_burst = true;
253 m.has_time_spec = true;
254 m.time_spec = m.time_spec.from_ticks(ts + rxtxdelay, rxticks); // uhd specific b210 delay!
255 std::vector<void *> ptrs(1, buffer);
256
257 tx_stream->send(ptrs, len, m, 1.0);
258#ifdef DBGXX
259 uhd::async_metadata_t async_md;
260 bool tx_ack = false;
261 while (!tx_ack && tx_stream->recv_async_msg(async_md)) {
262 tx_ack = (async_md.event_code == uhd::async_metadata_t::EVENT_CODE_BURST_ACK);
263 }
264 std::cout << (tx_ack ? "yay" : "nay") << " " << async_md.time_spec.to_ticks(rxticks) << std::endl;
265#endif
266 }
267};