-
Notifications
You must be signed in to change notification settings - Fork 47
/
sdr_hackrf.c
281 lines (241 loc) · 9.91 KB
/
sdr_hackrf.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* multi-sdr-gps-sim generates a IQ data stream on-the-fly to simulate a
* GPS L1 baseband signal using a SDR platform like HackRF or ADLAM-Pluto.
*
* This file is part of the Github project at
* https://github.com/mictronics/multi-sdr-gps-sim.git
*
* Copyright © 2021 Mictronics
* Distributed under the MIT License.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
/* for PRIX64 */
#include <inttypes.h>
#include <hackrf.h>
#include "gui.h"
#include "fifo.h"
#include "sdr.h"
#include "sdr_hackrf.h"
static hackrf_device_list_t *list;
static hackrf_device* device;
static const int gui_y_offset = 4;
static const int gui_x_offset = 2;
int sdr_hackrf_init(simulator_t *simulator) {
int result = HACKRF_SUCCESS;
uint8_t board_id = BOARD_ID_INVALID;
char version[255 + 1];
uint16_t usb_version;
read_partid_serialno_t read_partid_serialno;
uint8_t operacakes[8];
double sample_rate_gps_hz;
uint32_t baseband_filter_bw_hackrf_hz = 0;
uint64_t freq_gps_hz;
int y = gui_y_offset;
// HackRF wants 8 bit signed samples
if (simulator->sample_size == SC16) {
gui_status_wprintw(YELLOW, "16 bit sample size requested. Reset to 8 bit with HackRF.\n");
}
simulator->sample_size = SC08;
result = hackrf_init();
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1;
}
list = hackrf_device_list();
if (list->devicecount < 1) {
gui_status_wprintw(RED, "No HackRF boards found.\n");
return -1;
}
if (list->devicecount > 1) {
gui_mvwprintw(TRACK, y++, gui_x_offset, "Found %d HackRF devices. Using index 0.", list->devicecount);
} else {
gui_mvwprintw(TRACK, y++, gui_x_offset, "Found HackRF device.");
}
if (list->serial_numbers[0]) {
gui_mvwprintw(TRACK, y++, gui_x_offset, "Serial number: %s", list->serial_numbers[0]);
}
device = NULL;
result = hackrf_device_list_open(list, 0, &device);
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1;
}
result = hackrf_board_id_read(device, &board_id);
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_board_id_read() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1;
}
gui_mvwprintw(TRACK, y++, gui_x_offset, "Board ID Number: %d (%s)", board_id, hackrf_board_id_name(board_id));
result = hackrf_version_string_read(device, &version[0], 255);
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_version_string_read() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1;
}
result = hackrf_usb_api_version_read(device, &usb_version);
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_usb_api_version_read() failed: %s (%d)\n",
hackrf_error_name(result), result);
return -1;
}
gui_mvwprintw(TRACK, y++, gui_x_offset, "Firmware Version: %s (API:%x.%02x)", version, (usb_version >> 8)&0xFF, usb_version & 0xFF);
result = hackrf_board_partid_serialno_read(device, &read_partid_serialno);
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_board_partid_serialno_read() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1;
}
gui_mvwprintw(TRACK, y++, gui_x_offset, "Part ID Number: 0x%08x 0x%08x",
read_partid_serialno.part_id[0],
read_partid_serialno.part_id[1]);
result = hackrf_get_operacake_boards(device, &operacakes[0]);
if ((result != HACKRF_SUCCESS) && (result != HACKRF_ERROR_USB_API_VERSION)) {
gui_status_wprintw(RED, "hackrf_get_operacake_boards() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1;
}
if (result == HACKRF_SUCCESS) {
for (int j = 0; j < 8; j++) {
if (operacakes[j] == 0)
break;
gui_mvwprintw(TRACK, y++, gui_x_offset, "Operacake found, address: 0x%02x", operacakes[j]);
}
}
#ifdef HACKRF_ISSUE_609_IS_FIXED
uint32_t cpld_crc = 0;
result = hackrf_cpld_checksum(device, &cpld_crc);
if ((result != HACKRF_SUCCESS) && (result != HACKRF_ERROR_USB_API_VERSION)) {
gui_status_wprintw(RED, "hackrf_cpld_checksum() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1;
}
if (result == HACKRF_SUCCESS) {
gui_mvwprintw(TRACK, y++, 60, "CPLD checksum: 0x%08x\n", cpld_crc);
}
#endif /* HACKRF_ISSUE_609_IS_FIXED */
sample_rate_gps_hz = TX_SAMPLERATE;
freq_gps_hz = TX_FREQUENCY;
// Change the freq and sample rate to correct the crystal clock error.
// sample_rate_gps_hz = (uint32_t) ((double) sample_rate_gps_hz * (10000000 - simulator->ppb) / 10000000 + 0.5);
freq_gps_hz = freq_gps_hz * (10000000 - simulator->ppb) / 10000000;
/* Compute default value depending on sample rate */
baseband_filter_bw_hackrf_hz = hackrf_compute_baseband_filter_bw(TX_BW);
if (baseband_filter_bw_hackrf_hz > BASEBAND_FILTER_BW_MAX) {
gui_mvwprintw(TRACK, y++, gui_x_offset, "Baseband filter BW must be less or equal to %u Hz/%.03f MHz",
BASEBAND_FILTER_BW_MAX, (float) (BASEBAND_FILTER_BW_MAX / FREQ_ONE_MHZ));
return -1;
}
if (baseband_filter_bw_hackrf_hz < BASEBAND_FILTER_BW_MIN) {
gui_mvwprintw(TRACK, y++, 60, "Baseband filter BW must be greater or equal to %u Hz/%.03f MHz",
BASEBAND_FILTER_BW_MIN, (float) (BASEBAND_FILTER_BW_MIN / FREQ_ONE_MHZ));
return -1;
}
// Disable antenna bias tee.
result = hackrf_set_antenna_enable(device, 0);
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_set_antenna_enable() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1;
}
gui_mvwprintw(TRACK, y++, gui_x_offset, "Sample rate (%.0lf Hz/%.03f MHz)", sample_rate_gps_hz, ((float) sample_rate_gps_hz / (float) FREQ_ONE_MHZ));
result = hackrf_set_sample_rate(device, sample_rate_gps_hz);
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_sample_rate_set() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1;
}
gui_mvwprintw(TRACK, y++, gui_x_offset, "Baseband filter bandwidth (%d Hz/%.03f MHz)",
baseband_filter_bw_hackrf_hz, ((float) baseband_filter_bw_hackrf_hz / (float) FREQ_ONE_MHZ));
result = hackrf_set_baseband_filter_bandwidth(device, baseband_filter_bw_hackrf_hz);
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_baseband_filter_bandwidth_set() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1;
}
gui_mvwprintw(TRACK, y++, gui_x_offset, "Freq (%" PRIu64 " Hz/%.03f MHz)", freq_gps_hz, ((double) freq_gps_hz / (double) FREQ_ONE_MHZ));
result = hackrf_set_freq(device, freq_gps_hz);
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_set_freq() failed: %s (%d)", hackrf_error_name(result), result);
return -1;
}
if (simulator->enable_tx_amp) {
gui_mvwprintw(TRACK, y++, gui_x_offset, "Amplifier enabled");
result = hackrf_set_amp_enable(device, 1);
} else {
result = hackrf_set_amp_enable(device, 0);
}
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_set_amp_enable() failed: %s (%d)", hackrf_error_name(result), result);
return -1;
}
if (simulator->tx_gain < TX_IF_GAIN_MIN) {
simulator->tx_gain = TX_IF_GAIN_MIN;
} else if (simulator->tx_gain > TX_IF_GAIN_MAX) {
simulator->tx_gain = TX_IF_GAIN_MAX;
}
gui_mvwprintw(TRACK, y++, gui_x_offset, "TX IF gain: %idB", simulator->tx_gain);
result = hackrf_set_txvga_gain(device, simulator->tx_gain);
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_set_txvga_gain() failed: %s (%d)", hackrf_error_name(result), result);
return -1;
}
result = hackrf_set_hw_sync_mode(device, 0);
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_set_hw_sync_mode() failed: %s (%d)", hackrf_error_name(result), result);
return -1;
}
if (!fifo_create(NUM_FIFO_BUFFERS, HACKRF_TRANSFER_BUFFER_SIZE, sizeof (signed char))) {
gui_status_wprintw(RED, "Error creating TX fifo!");
return -1;
}
return 0;
}
void sdr_hackrf_close(void) {
fifo_halt();
fifo_destroy();
if (device != NULL) {
hackrf_stop_tx(device);
hackrf_set_amp_enable(device, 0);
hackrf_set_txvga_gain(device, 0);
hackrf_close(device);
}
hackrf_device_list_free(list);
hackrf_exit();
}
static int sdr_tx_callback(hackrf_transfer *transfer) {
// Get a fifo block
struct iq_buf *iq = fifo_dequeue();
if (iq != NULL && iq->data8 != NULL) {
// Fifo has transfer block size
memcpy(transfer->buffer, iq->data8, transfer->valid_length);
// Release and free up used block
fifo_release(iq);
return 0;
}
return -1;
}
int sdr_hackrf_run(void) {
if (device == NULL) {
gui_status_wprintw(RED, "HackRF device is NULL\n");
return -1;
}
fifo_wait_full();
int result = hackrf_start_tx(device, sdr_tx_callback, NULL);
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_start_tx() failed: %s (%d)", hackrf_error_name(result), result);
return -1;
}
return 0;
}
int sdr_hackrf_set_gain(const int gain) {
int g = gain;
if (g < TX_IF_GAIN_MIN) {
g = TX_IF_GAIN_MIN;
} else if (g > TX_IF_GAIN_MAX) {
g = TX_IF_GAIN_MAX;
}
int result = hackrf_set_txvga_gain(device, g);
if (result != HACKRF_SUCCESS) {
gui_status_wprintw(RED, "hackrf_set_txvga_gain() failed: %s (%d)", hackrf_error_name(result), result);
}
return g;
}