forked from Mictronics/readsb-protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdr_plutosdr.c
256 lines (213 loc) · 8.4 KB
/
sdr_plutosdr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Part of readsb, a Mode-S/ADSB/TIS message decoder.
//
// sdr_pluto.c: PlutoSDR support
//
// Copyright (c) 2020 Michael Wolf <[email protected]>
//
// This file 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, either version 3 of the License, or
// any later version.
//
// This file 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 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 <iio.h>
#include <ad9361.h>
#include "readsb.h"
#include "sdr_plutosdr.h"
static struct {
input_format_t input_format;
int dev_index;
struct iio_channel *rx0_i;
struct iio_channel *rx0_q;
struct iio_buffer *rxbuf;
struct iio_context *ctx;
struct iio_device *dev;
int16_t *readbuf;
iq_convert_fn converter;
struct converter_state *converter_state;
char *uri;
char *network;
} PLUTOSDR;
void plutosdrInitConfig() {
PLUTOSDR.readbuf = NULL;
PLUTOSDR.converter = NULL;
PLUTOSDR.converter_state = NULL;
PLUTOSDR.uri = NULL;
PLUTOSDR.network = NULL;
}
bool plutosdrHandleOption(int argc, char *argv) {
switch (argc) {
case OptPlutoUri:
PLUTOSDR.uri = strdup(argv);
break;
case OptPlutoNetwork:
PLUTOSDR.network = strdup(argv);
break;
}
return true;
}
bool plutosdrOpen() {
PLUTOSDR.network = strdup("pluto.local");
PLUTOSDR.ctx = iio_create_default_context();
if (PLUTOSDR.ctx == NULL && PLUTOSDR.uri != NULL) {
PLUTOSDR.ctx = iio_create_context_from_uri(PLUTOSDR.uri);
} else if (PLUTOSDR.ctx == NULL) {
PLUTOSDR.ctx = iio_create_network_context(PLUTOSDR.network);
}
if (PLUTOSDR.ctx == NULL) {
char buf[1024];
iio_strerror(errno, buf, sizeof (buf));
fprintf(stderr, "plutosdr: Failed creating IIO context: %s\n", buf);
return false;
}
struct iio_scan_context *ctx;
struct iio_context_info **info;
ctx = iio_create_scan_context(NULL, 0);
if (ctx) {
int info_count = iio_scan_context_get_info_list(ctx, &info);
if (info_count > 0) {
fprintf(stderr, "plutosdr: %s\n", iio_context_info_get_description(info[0]));
iio_context_info_list_free(info);
}
iio_scan_context_destroy(ctx);
}
int device_count = iio_context_get_devices_count(PLUTOSDR.ctx);
if (!device_count) {
fprintf(stderr, "plutosdr: No supported PLUTOSDR devices found.\n");
plutosdrClose();
}
fprintf(stderr, "plutosdr: Context has %d device(s).\n", device_count);
PLUTOSDR.dev = iio_context_find_device(PLUTOSDR.ctx, "cf-ad9361-lpc");
if (PLUTOSDR.dev == NULL) {
fprintf(stderr, "plutosdr: Error opening the PLUTOSDR device: %s\n", strerror(errno));
plutosdrClose();
}
struct iio_channel* phy_chn = iio_device_find_channel(iio_context_find_device(PLUTOSDR.ctx, "ad9361-phy"), "voltage0", false);
iio_channel_attr_write(phy_chn, "rf_port_select", "A_BALANCED");
iio_channel_attr_write_longlong(phy_chn, "rf_bandwidth", (long long) 1750000);
iio_channel_attr_write_longlong(phy_chn, "sampling_frequency", (long long) Modes.sample_rate);
if (Modes.gain == MODES_AUTO_GAIN) {
iio_channel_attr_write(phy_chn, "gain_control_mode", "slow_attack");
} else {
// We use 10th of dB here, max is 77dB up to 1300MHz
if (Modes.gain > 770)
Modes.gain = 770;
iio_channel_attr_write(phy_chn, "gain_control_mode", "manual");
iio_channel_attr_write_longlong(phy_chn, "hardwaregain", Modes.gain / 10);
}
iio_channel_attr_write_bool(
iio_device_find_channel(iio_context_find_device(PLUTOSDR.ctx, "ad9361-phy"), "altvoltage1", true)
, "powerdown", true); // Turn OFF TX LO
iio_channel_attr_write_longlong(
iio_device_find_channel(iio_context_find_device(PLUTOSDR.ctx, "ad9361-phy"), "altvoltage0", true)
, "frequency", (long long) Modes.freq); // Set RX LO frequency
PLUTOSDR.rx0_i = iio_device_find_channel(PLUTOSDR.dev, "voltage0", false);
if (!PLUTOSDR.rx0_i)
PLUTOSDR.rx0_i = iio_device_find_channel(PLUTOSDR.dev, "altvoltage0", false);
PLUTOSDR.rx0_q = iio_device_find_channel(PLUTOSDR.dev, "voltage1", false);
if (!PLUTOSDR.rx0_q)
PLUTOSDR.rx0_q = iio_device_find_channel(PLUTOSDR.dev, "altvoltage1", false);
ad9361_set_bb_rate(iio_context_find_device(PLUTOSDR.ctx, "ad9361-phy"), Modes.sample_rate);
iio_channel_enable(PLUTOSDR.rx0_i);
iio_channel_enable(PLUTOSDR.rx0_q);
PLUTOSDR.rxbuf = iio_device_create_buffer(PLUTOSDR.dev, MODES_MAG_BUF_SAMPLES, false);
if (!PLUTOSDR.rxbuf) {
perror("plutosdr: Could not create RX buffer");
}
if (!(PLUTOSDR.readbuf = malloc(MODES_RTL_BUF_SIZE * 4))) {
fprintf(stderr, "plutosdr: Failed to allocate read buffer\n");
plutosdrClose();
return false;
}
PLUTOSDR.converter = init_converter(INPUT_SC16,
Modes.sample_rate,
Modes.dc_filter,
&PLUTOSDR.converter_state);
if (!PLUTOSDR.converter) {
fprintf(stderr, "plutosdr: Can't initialize sample converter\n");
plutosdrClose();
return false;
}
return true;
}
static void plutosdrCallback(int16_t *buf, uint32_t len) {
static unsigned dropped = 0;
static uint64_t sampleCounter = 0;
sdrMonitor();
unsigned samples_read = len / 2; // Drops any trailing odd sample, not much else we can do there
if (!samples_read)
return; // that wasn't useful
struct mag_buf *outbuf = fifo_acquire(0 /* don't wait */);
if (!outbuf) {
// FIFO is full. Drop this block.
dropped += samples_read;
sampleCounter += samples_read;
return;
}
outbuf->flags = 0;
if (dropped) {
// We previously dropped some samples due to no buffers being available
outbuf->flags |= MAGBUF_DISCONTINUOUS;
outbuf->dropped = dropped;
}
dropped = 0;
outbuf->sampleTimestamp = sampleCounter * 12e6 / Modes.sample_rate;
sampleCounter += samples_read;
uint64_t block_duration = 1e3 * samples_read / Modes.sample_rate;
outbuf->sysTimestamp = mstime() - block_duration;
// Convert the new data
unsigned to_convert = samples_read;
if (to_convert + outbuf->overlap > outbuf->totalLength) {
// how did that happen?
to_convert = outbuf->totalLength - outbuf->overlap;
dropped = samples_read - to_convert;
}
PLUTOSDR.converter(buf, &outbuf->data[outbuf->overlap], to_convert, PLUTOSDR.converter_state, &outbuf->mean_level, &outbuf->mean_power);
outbuf->validLength = outbuf->overlap + to_convert;
// Push to the demodulation thread
fifo_enqueue(outbuf);
}
void plutosdrRun() {
void *p_dat, *p_end;
ptrdiff_t p_inc;
if (!PLUTOSDR.dev) {
return;
}
while (!Modes.exit) {
int16_t *p = PLUTOSDR.readbuf;
uint32_t len = (uint32_t) iio_buffer_refill(PLUTOSDR.rxbuf) / 2;
p_inc = iio_buffer_step(PLUTOSDR.rxbuf);
p_end = iio_buffer_end(PLUTOSDR.rxbuf);
p_dat = iio_buffer_first(PLUTOSDR.rxbuf, PLUTOSDR.rx0_i);
for (p_dat = iio_buffer_first(PLUTOSDR.rxbuf, PLUTOSDR.rx0_i); p_dat < p_end; p_dat += p_inc) {
*p++ = ((int16_t*) p_dat)[0]; // Real (I)
*p++ = ((int16_t*) p_dat)[1]; // Imag (Q)
}
plutosdrCallback(PLUTOSDR.readbuf, len);
}
}
void plutosdrClose() {
if (PLUTOSDR.readbuf) {
free(PLUTOSDR.readbuf);
}
if (PLUTOSDR.rxbuf) {
iio_buffer_destroy(PLUTOSDR.rxbuf);
}
if (PLUTOSDR.rx0_i) {
iio_channel_disable(PLUTOSDR.rx0_i);
}
if (PLUTOSDR.rx0_q) {
iio_channel_disable(PLUTOSDR.rx0_q);
}
if (PLUTOSDR.ctx) {
iio_context_destroy(PLUTOSDR.ctx);
}
free(PLUTOSDR.network);
free(PLUTOSDR.uri);
}