-
Notifications
You must be signed in to change notification settings - Fork 3
/
excalibur.c
330 lines (314 loc) · 9.2 KB
/
excalibur.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
// Copyright (c) <2012> <Leif Asbrink>
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
#include "osnum.h"
#include "globdef.h"
#include <string.h>
#include <fcntl.h>
#include "uidef.h"
#include "thrdef.h"
#include "sdrdef.h"
#include "fft1def.h"
#include "screendef.h"
#include "vernr.h"
#if OSNUM == OSNUM_WINDOWS
#include "wscreen.h"
#endif
#if OSNUM == OSNUM_LINUX
#include "lscreen.h"
#endif
void open_excalibur(void);
void close_excalibur(void);
void select_excalibur_mode(int *line);
void start_excalibur_read(void);
void lir_excalibur_read(void);
void set_excalibur_frequency(void);
void set_excalibur_att(void);
void excalibur_stop(void);
int load_excalibur_library(char *serial_numbers);
char *excalibur_parfil_name="par_excalibur";
char *excalibur_parm_text[MAX_EXCALIBUR_PARM]={"Rate no", //0
"Sampl. rate", //1
"Sampl. clk adjust", //2
"MW filter maxfreq", //3
"Dither", //4
"Check"}; //5
void excalibur_input(void)
{
double rt_factor;
int i, errcod;
int rxin_local_workload_reset;
char s[80];
double dt1, read_start_time, total_reads;
int timing_loop_counter,timing_loop_counter_max,initial_skip_flag;
float t1;
int local_nco_counter, local_att_counter;
#if OSNUM == OSNUM_LINUX
clear_thread_times(THREAD_EXCALIBUR_INPUT);
#endif
local_nco_counter=sdr_nco_counter;
local_att_counter=sdr_att_counter;
errcod=0;
dt1=current_time();
while(sdr == -1)
{
open_excalibur();
lir_sleep(30000);
if(sdr == -1)
{
sprintf(s,"Waiting %.2f", current_time()-dt1);
lir_text(0,screen_last_line,s);
lir_refresh_screen();
if(kill_all_flag)goto excalibur_init_error_exit;
}
}
// ****************************************************
// We have a connection to the Excaliobur.
i=read_sdrpar(excalibur_parfil_name, MAX_EXCALIBUR_PARM,
excalibur_parm_text, (int*)((void*)&excalib));
if(i != 0)
{
errcod=1337;
goto excalibur_error_exit;
}
if( excalib.check != EXCALIBUR_PAR_VERNR ||
excalib.rate_no < 0 ||
abs(excalib.clock_adjust) > 10000 ||
excalib.rate_no >= no_of_excalibur_rates)goto excalibur_error_exit;
rt_factor=EXCALIBUR_SAMPLING_CLOCK/excalib.sampling_rate;
adjusted_sdr_clock=EXCALIBUR_SAMPLING_CLOCK+excalib.clock_adjust;
t1=ui.rx_ad_speed-adjusted_sdr_clock/rt_factor;
if(fabs(t1/ui.rx_ad_speed) > 0.0001)
{
errcod=1338;
goto excalibur_error_exit;
}
ui.rx_ad_speed=adjusted_sdr_clock/rt_factor;
fft1_block_timing();
if(thread_command_flag[THREAD_SCREEN]!=THRFLAG_NOT_ACTIVE)
{
while(thread_status_flag[THREAD_SCREEN]!=THRFLAG_ACTIVE &&
thread_status_flag[THREAD_SCREEN]!=THRFLAG_IDLE &&
thread_status_flag[THREAD_SCREEN]!=THRFLAG_SEM_WAIT)
{
if(thread_command_flag[THREAD_EXCALIBUR_INPUT] ==
THRFLAG_KILL)goto excalibur_error_exit;
lir_sleep(10000);
}
}
start_excalibur_read();
set_excalibur_frequency();
set_excalibur_att();
thread_status_flag[THREAD_EXCALIBUR_INPUT]=THRFLAG_ACTIVE;
#include "timing_setup.c"
lir_status=LIR_OK;
while(thread_command_flag[THREAD_EXCALIBUR_INPUT] == THRFLAG_ACTIVE)
{
#include "input_speed.c"
if(local_nco_counter != sdr_nco_counter)
{
local_nco_counter=sdr_nco_counter;
set_excalibur_frequency();
}
if(local_att_counter != sdr_att_counter)
{
local_att_counter=sdr_att_counter;
set_excalibur_att();
}
lir_excalibur_read();
if(kill_all_flag)goto excalibur_exit;
finish_rx_read();
if(kill_all_flag) goto excalibur_exit;
}
excalibur_exit:;
excalibur_stop();
excalibur_error_exit:;
if(errcod != 0)lirerr(errcod);
close_excalibur();
excalibur_init_error_exit:;
thread_status_flag[THREAD_EXCALIBUR_INPUT]=THRFLAG_RETURNED;
while(thread_command_flag[THREAD_EXCALIBUR_INPUT] != THRFLAG_NOT_ACTIVE)
{
lir_sleep(1000);
}
}
int display_excalibur_parm_info(int *line)
{
char s[80];
int errcod;
errcod=read_sdrpar(excalibur_parfil_name, MAX_EXCALIBUR_PARM,
excalibur_parm_text, (int*)((void*)&excalib));
if(errcod == 0)
{
settextcolor(7);
sprintf(s,"rate number = %i ", excalib.rate_no);
if(excalib.sampling_rate >=1000000)
{
sprintf(s,"rate number = %i (%8.3f MHz)",
excalib.rate_no, 0.000001*excalib.sampling_rate);
}
else
{
sprintf(s,"rate number = %i (%8.3f kHz)",
excalib.rate_no, 0.001*excalib.sampling_rate);
}
lir_text(24,line[0],s);
SNDLOG"\n%s",s);
line[0]+=1;
sprintf(s,"clock adjustment = %i", excalib.clock_adjust);
lir_text(24,line[0],s);
SNDLOG"\n%s",s);
line[0]+=1;
sprintf(s,"MW filter maxfreq = %i", excalib.mw_filter_maxfreq);
lir_text(24,line[0],s);
SNDLOG"\n%s",s);
line[0]+=1;
sprintf(s,"dither = %i", excalib.dither);
lir_text(24,line[0],s);
SNDLOG"\n%s\n",s);
}
return (errcod);
}
void init_excalibur(void)
{
char *serial_numbers[MAX_EXCALIBUR_DEVICES][9];
FILE *excalibur_file, *excalibur_sernum_file;
double rt_factor;
int count, i, line;
int *sdr_pi;
char *looking;
char s[2000];
looking="Looking for an Excalibur on the USB port.";
settextcolor(12);
lir_text(10,1,looking);
SNDLOG"\n%s",looking);
lir_text(10,2,"Reset CPU, USB and SDR hardware if system hangs here.");
lir_refresh_screen();
settextcolor(14);
count=load_excalibur_library((char*)serial_numbers);
settextcolor(7);
if(count==0)
{
sprintf(s,"Did not find any Excalibur hardware.");
SNDLOG"\n%s\n",s);
lir_text(10,8,s);
lir_text(12,9,press_any_key);
await_processed_keyboard();
return;
}
clear_lines(1,2);
lir_refresh_screen();
line=7;
if(count == 1)
{
lir_text(0,line,"There is only one Excalibur on the system.");
line++;
sprintf(s,"The serial number is %s and it is auto-selected.",
(char*)serial_numbers[0]);
lir_text(0,line,s);
line++;
i=0;
}
else
{
for(i=0; i<count; i++)
{
sprintf(s,"%02d serial=%s",i,(char*)serial_numbers[i]);
lir_text(0,line,s);
line++;
}
line++;
lir_text(0,line,"Select the unit to use by its line number.");
i=lir_get_integer(44,line,2,0,count);
if(kill_all_flag)return;
}
excalibur_sernum_file=fopen(excalibur_sernum_filename,"w");
if(excalibur_sernum_file == NULL)
{
lirerr(281667);
return;
}
fprintf(excalibur_sernum_file,"%s",(char*)serial_numbers[i]);
fclose(excalibur_sernum_file);
open_excalibur();
if(kill_all_flag)return;
settextcolor(7);
if(sdr != -1)
{
clear_lines(1,2);
lir_refresh_screen();
line=10;
select_excalibur_mode(&line);
if(kill_all_flag)goto excalibur_errexit;
if(ui.operator_skil == OPERATOR_SKIL_NEWCOMER)
{
excalib.clock_adjust=0;
}
else
{
sprintf(s,"Set sampling clock shift (Hz))");
lir_text(5,line,s);
excalib.clock_adjust=lir_get_integer(37, line, 5,-10000,10000);
if(kill_all_flag)goto excalibur_errexit;
line++;
}
rt_factor=EXCALIBUR_SAMPLING_CLOCK/excalib.sampling_rate;
adjusted_sdr_clock=EXCALIBUR_SAMPLING_CLOCK+excalib.clock_adjust;
ui.rx_ad_speed=adjusted_sdr_clock/rt_factor;
if(ui.operator_skil == OPERATOR_SKIL_NEWCOMER)
{
excalib.mw_filter_maxfreq=1800000;
excalib.dither=1;
}
else
{
lir_text(5,line,"Max freq (Hz) without MW filter ?");
excalib. mw_filter_maxfreq=lir_get_integer(37, line, 7, 0, 50000000);
if(kill_all_flag)goto excalibur_errexit;
line++;
lir_text(5,line,"Enable dither ? (0/1)");
excalib.dither=lir_get_integer(27, line, 1, 0, 1);
if(kill_all_flag)goto excalibur_errexit;
line++;
}
excalib.check=EXCALIBUR_PAR_VERNR;
excalibur_file=fopen(excalibur_parfil_name,"w");
if(excalibur_file == NULL)
{
excalibur_errexit:;
close_excalibur();
clear_screen();
ui.rx_addev_no=UNDEFINED_DEVICE_CODE;
return;
}
sdr_pi=(int*)(&excalib);
for(i=0; i<MAX_EXCALIBUR_PARM; i++)
{
fprintf(excalibur_file,"%s [%d]\n",excalibur_parm_text[i],sdr_pi[i]);
}
parfile_end(excalibur_file);
close_excalibur();
}
else
{
SNDLOG"\nopen_excalibur failed.\n");
}
}