-
Notifications
You must be signed in to change notification settings - Fork 0
/
perseustest.c
359 lines (305 loc) · 9.77 KB
/
perseustest.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
282
283
284
285
286
287
288
289
290
291
292
293
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// perseus-sdr library test
// Copyright (c) 2010 Nicolangelo Palermo / IV3NWV
// This file is part of the Perseus SDR Library
//
// The Perseus SDR Library is free software; you can redistribute
// it and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either version
// 2.1 of the License, or (at your option) any later version.
// The Perseus SDR Library 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 Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with the Perseus SDR Library;
// if not, see <http://www.gnu.org/licenses/>.
// Creation date: 10 Jan 2010
// Version: 0.1
// Author: Nicolangelo Palermo / IV3NWV
// (nicopal at microtelecom dot it)
// ------------------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#if defined _WIN32
#define sleep(x) Sleep((x)*1000)
#else
#include <unistd.h>
#endif
#include "perseus-sdr.h"
#if defined SVN_REVISION
const char *svn_revision = SVN_REVISION;
#else
const char *svn_revision ="UNKNOWN";
#endif
/* TODO:
- Verify that control functions are thread safe (no mutex used yet)
*/
void print_usage ()
{
fprintf (stderr, "Usage:\n");
fprintf (stderr, "-s ............ sample rate (");
{
int buf[BUFSIZ];
if (perseus_get_sampling_rates (0, buf, sizeof(buf)/sizeof(buf[0])) < 0) {
printf("get sampling rates error: %s\n", perseus_errorstr());
} else {
int i = 0;
while (buf[i]) {
fprintf(stderr, " %d", buf[i]);
i++;
}
}
fprintf (stderr, ")\n");
}
fprintf (stderr, "-n ............ number of buffers\n");
fprintf (stderr, "-b ............ buffer size in bytes\n");
fprintf (stderr, "-d ............ debug level (0..9, -1 no debug)\n");
fprintf (stderr, "-a ............ don't test attenuators\n");
fprintf (stderr, "-t ............ acquisition test duration in seconds (default 10)\n");
fprintf (stderr, "-h ............ this help\n");
}
// The function that will be called by the perseus library when a
// data buffer from the Perseus input data endpoint is available
static int user_data_callback(void *buf, int buf_size, void *extra);
int main(int argc, char **argv)
{
int k, num_perseus;
perseus_descr *descr;
eeprom_prodid prodid;
FILE *fout;
int i;
int sr = 95000;
int nb = 6;
int bs = 1024;
int dbg_lvl = 3;
int no_ta = 0;
int test_time = 10;
for (i=0; i < argc; ++i) {
dbgprintf (3,"%d: %s\n", i, argv[i]);
if (!strcmp(argv[i],"-s") && (i+1)<argc) {
++i;
if(sscanf(argv[i], "%d", &sr) == 1) ; else sr = 95000;
}
if (!strcmp(argv[i],"-n") && (i+1)<argc) {
++i;
if(sscanf(argv[i], "%d", &nb) == 1) ; else nb = 6;
}
if (!strcmp(argv[i],"-b") && (i+1)<argc) {
++i;
if(sscanf(argv[i], "%d", &bs) == 1) ; else bs = 1024;
}
if (!strcmp(argv[i],"-d") && (i+1)<argc) {
++i;
if(sscanf(argv[i], "%d", &dbg_lvl) == 1) ; else dbg_lvl = 3;
}
if (!strcmp(argv[i],"-a")) {
no_ta = 1;
}
if (!strcmp(argv[i],"-t") && (i+1)<argc) {
++i;
if(sscanf(argv[i], "%d", &test_time) == 1) ; else test_time = 10;
}
if (!strcmp(argv[i],"-h")) {
print_usage ();
exit (255);
}
}
// Set debug info dumped to stderr to the maximum verbose level
perseus_set_debug(dbg_lvl);
printf ("Revision: %s\n", svn_revision);
printf ("SAMPLE RATE: %d\n", sr);
printf ("NBUF: %d BUF SIZE: %d TOTAL BUFFER LENGTH: %d\n", nb, bs, nb*bs);
// Check how many Perseus receivers are connected to the system
num_perseus = perseus_init();
printf("%d Perseus receivers found\n",num_perseus);
if (num_perseus==0) {
printf("No Perseus receivers detected\n");
perseus_exit();
return 0;
}
// Open the first one...
if ((descr=perseus_open(0))==NULL) {
printf("error: %s\n", perseus_errorstr());
return 255;
}
// Download the standard firmware to the unit
printf("Downloading firmware...\n");
if (perseus_firmware_download(descr,NULL)<0) {
printf("firmware download error: %s", perseus_errorstr());
return 255;
}
// Dump some information about the receiver (S/N and HW rev)
if (descr->is_preserie == TRUE)
printf("The device is a preserie unit");
else
if (perseus_get_product_id(descr,&prodid)<0)
printf("get product id error: %s", perseus_errorstr());
else
printf("Receiver S/N: %05d-%02hX%02hX-%02hX%02hX-%02hX%02hX - HW Release:%hd.%hd\n",
(uint16_t) prodid.sn,
(uint16_t) prodid.signature[5],
(uint16_t) prodid.signature[4],
(uint16_t) prodid.signature[3],
(uint16_t) prodid.signature[2],
(uint16_t) prodid.signature[1],
(uint16_t) prodid.signature[0],
(uint16_t) prodid.hwrel,
(uint16_t) prodid.hwver);
// Printing all sampling rates available .....
{
int buf[BUFSIZ];
if (perseus_get_sampling_rates (descr, buf, sizeof(buf)/sizeof(buf[0])) < 0) {
printf("get sampling rates error: %s\n", perseus_errorstr());
goto main_cleanup;
} else {
int i = 0;
while (buf[i]) {
printf("#%d: sample rate: %d\n", i, buf[i]);
i++;
}
}
}
// Configure the receiver for 2 MS/s operations
printf("Configuring FPGA...\n");
if (perseus_set_sampling_rate(descr, sr) < 0) { // specify the sampling rate value in Samples/second
//if (perseus_set_sampling_rate_n(descr, 0)<0) // specify the sampling rate value as ordinal in the vector
printf("fpga configuration error: %s\n", perseus_errorstr());
goto main_cleanup;
}
// Printing all attenuator values available .....
{
int buf[BUFSIZ];
if (perseus_get_attenuator_values (descr, buf, sizeof(buf)/sizeof(buf[0])) < 0) {
printf("get attenuator values error: %s\n", perseus_errorstr());
} else {
int i = 0;
while (buf[i] != -1) {
printf("#%d: att val in dB: %d\n", i, buf[i]);
i++;
}
}
}
// Cycle attenuator leds on the receiver front panel
// just to see if they indicate what they shoud
// perseus_set_attenuator(descr, PERSEUS_ATT_0DB);
// sleep(1);
// perseus_set_attenuator(descr, PERSEUS_ATT_10DB);
// sleep(1);
// perseus_set_attenuator(descr, PERSEUS_ATT_20DB);
// sleep(1);
// perseus_set_attenuator(descr, PERSEUS_ATT_30DB);
// sleep(1);
// perseus_set_attenuator(descr, PERSEUS_ATT_0DB);
// sleep(1);
//
// perseus_set_attenuator_in_db(descr, 0);
// sleep(1);
// perseus_set_attenuator_in_db(descr, 10);
// sleep(1);
// perseus_set_attenuator_in_db(descr, 20);
// sleep(1);
// perseus_set_attenuator_in_db(descr, 30);
// sleep(1);
// perseus_set_attenuator_in_db(descr, 0);
// sleep(1);
perseus_set_attenuator_in_db(descr, 33); // Bad value !!!
if (no_ta == 0) {
perseus_set_attenuator_n(descr, 0);
sleep(1);
perseus_set_attenuator_n(descr, 1);
sleep(1);
perseus_set_attenuator_n(descr, 2);
sleep(1);
perseus_set_attenuator_n(descr, 3);
sleep(1);
perseus_set_attenuator_n(descr, 0);
sleep(1);
}
// Enable ADC Dither, Disable ADC Preamp
perseus_set_adc(descr, TRUE, FALSE);
// Do the same cycling test with the WB front panel led.
// Enable preselection filters (WB_MODE Off)
perseus_set_ddc_center_freq(descr, 7000000.000, 1);
sleep(1);
// Disable preselection filters (WB_MODE On)
perseus_set_ddc_center_freq(descr, 7000000.000, 0);
sleep(1);
// Re-enable preselection filters (WB_MODE Off)
perseus_set_ddc_center_freq(descr, 7000000.000, 1);
// We open a file for binary output.
// IQ samples will be saved in binary format to this file
fout = fopen("perseusdata","wb");
if (!fout) {
printf("Can't open output file\n");
perseus_close(descr);
perseus_exit();
}
// We start the acquisition passing our callback and its params
// (in this case the fout file handle we just open)
printf("Starting async data acquisition... \n");
if (perseus_start_async_input(descr,nb*bs,user_data_callback,fout)<0) {
printf("start async input error: %s\n", perseus_errorstr());
goto main_cleanup;
}
fprintf(stderr, "Collecting input samples... ");
// We wait a 10 s time interval.
// The user data callback we supplied to perseus_start_async_input
// is being called meanwhile.
for (k=0;k<test_time;k++) {
fprintf(stderr, ".");
sleep(1);
}
fprintf(stderr, "\ndone\n");
// We stop the acquisition...
printf("Stopping async data acquisition...\n");
perseus_stop_async_input(descr);
// We can safely close the output file handle here. Acquisition has stopped.
fclose(fout);
main_cleanup:
// And we can finally quit the test application
printf("Quitting...\n");
perseus_close(descr);
perseus_exit();
printf("Bye\n");
return 1;
}
typedef union {
struct {
int32_t i;
int32_t q;
} __attribute__((__packed__)) iq;
struct {
uint8_t i1;
uint8_t i2;
uint8_t i3;
uint8_t i4;
uint8_t q1;
uint8_t q2;
uint8_t q3;
uint8_t q4;
} __attribute__((__packed__)) ;
} iq_sample;
int user_data_callback(void *buf, int buf_size, void *extra)
{
// The buffer received contains 24-bit IQ samples (6 bytes per sample)
// Here as a demonstration we save the received IQ samples as 32 bit
// (msb aligned) integer IQ samples.
// At the maximum sample rate (2 MS/s) the hard disk should be capable
// of writing data at a rate of at least 16 MB/s (almost 1 GB/min!)
uint8_t *samplebuf = (uint8_t*)buf;
FILE *fout = (FILE*)extra;
int nSamples = buf_size/6;
int k;
iq_sample s;
s.i1 = s.q1 = 0;
for (k=0;k<nSamples;k++) {
s.i2 = *samplebuf++;
s.i3 = *samplebuf++;
s.i4 = *samplebuf++;
s.q2 = *samplebuf++;
s.q3 = *samplebuf++;
s.q4 = *samplebuf++;
fwrite(&s.iq, 1, sizeof(iq_sample), fout);
}
return 0;
}