forked from g0orx/pihpsdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio.c
659 lines (597 loc) · 18 KB
/
radio.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/* Copyright (C)
* 2015 - John Melton, G0ORX/N6LYT
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <semaphore.h>
#include <math.h>
#include "mode.h"
#include "radio.h"
#include "channel.h"
#include "agc.h"
#include "band.h"
#include "discovered.h"
#include "property.h"
#include "new_protocol.h"
#ifdef LIMESDR
#include "lime_protocol.h"
#endif
#include "wdsp.h"
#ifdef FREEDV
#include "freedv.h"
#endif
#define min(x,y) (x<y?x:y)
#define max(x,y) (x<y?y:x)
char property_path[128];
sem_t property_sem;
int atlas_penelope=0;
int atlas_clock_source_10mhz=0;
int atlas_clock_source_128mhz=0;
int atlas_config=0;
int atlas_mic_source=0;
int classE=0;
int tx_out_of_band=0;
int sample_rate=48000;
int filter_board=ALEX;
int pa=PA_ENABLED;
int apollo_tuner=0;
int updates_per_second=10;
int display_panadapter=1;
int panadapter_high=-60;
int panadapter_low=-160;
int display_filled=1;
int display_detector_mode=DETECTOR_MODE_AVERAGE;
int display_average_mode=AVERAGE_MODE_LOG_RECURSIVE;
double display_average_time=120.0;
int display_waterfall=1;
int waterfall_high=-100;
int waterfall_low=-150;
int waterfall_automatic=1;
int display_sliders=1;
int display_toolbar=1;
int toolbar_simulate_buttons=1;
double volume=0.2;
double mic_gain=1.5;
int rx_dither=0;
int rx_random=0;
int rx_preamp=0;
int mic_linein=0;
int mic_boost=0;
int mic_bias_enabled=0;
int mic_ptt_enabled=0;
int mic_ptt_tip_bias_ring=0;
int agc=AGC_MEDIUM;
double agc_gain=60.0;
int nr_none=1;
int nr=0;
int nr2=0;
int nb=0;
int nb2=0;
int anf=0;
int snb=0;
int nr_agc=0; // 0=pre AGC 1=post AGC
int nr2_gain_method=2; // 0=Linear 1=Log 2=gamma
int nr2_npe_method=0; // 0=OSMS 1=MMSE
int nr2_ae=1; // 0=disable 1=enable
int cwPitch=600;
int tune_drive=6;
int drive=60;
int receivers=2;
int adc[2]={0,1};
int locked=0;
int step=100;
int lt2208Dither = 0;
int lt2208Random = 0;
int attenuation = 0; // 0dB
//unsigned long alex_rx_antenna=0;
//unsigned long alex_tx_antenna=0;
//unsigned long alex_attenuation=0;
int cw_keys_reversed=0; // 0=disabled 1=enabled
int cw_keyer_speed=12; // 1-60 WPM
int cw_keyer_mode=KEYER_STRAIGHT;
int cw_keyer_weight=30; // 0-100
int cw_keyer_spacing=0; // 0=on 1=off
int cw_keyer_internal=1; // 0=external 1=internal
int cw_keyer_sidetone_volume=127; // 0-127
int cw_keyer_ptt_delay=20; // 0-255ms
int cw_keyer_hang_time=300; // ms
int cw_keyer_sidetone_frequency=400; // Hz
int cw_breakin=1; // 0=disabled 1=enabled
int vfo_encoder_divisor=25;
int protocol;
int device;
int ozy_software_version;
int mercury_software_version;
int penelope_software_version;
int ptt;
int dot;
int dash;
int adc_overload;
int pll_locked;
unsigned int exciter_power;
unsigned int alex_forward_power;
unsigned int alex_reverse_power;
unsigned int AIN3;
unsigned int AIN4;
unsigned int AIN6;
unsigned int IO1;
unsigned int IO2;
unsigned int IO3;
int supply_volts;
int mox;
int tune;
long long ddsFrequency=14250000;
long long ddsOffset=0;
unsigned char OCtune=0;
int OCfull_tune_time=2800; // ms
int OCmemory_tune_time=550; // ms
long long tune_timeout;
#ifdef FREEDV
char freedv_tx_text_data[64];
#endif
void init_radio() {
int rc;
rc=sem_init(&property_sem, 0, 0);
if(rc!=0) {
fprintf(stderr,"init_radio: sem_init failed for property_sem: %d\n", rc);
exit(-1);
}
sem_post(&property_sem);
}
void setSampleRate(int rate) {
sample_rate=rate;
}
int getSampleRate() {
return sample_rate;
}
void setMox(int state) {
fprintf(stderr,"setMox: protocol=%d\n", protocol);
if(mox!=state) {
mox=state;
if(protocol==NEW_PROTOCOL) {
schedule_high_priority(3);
}
if(mox) {
#ifdef FREEDV
if(mode==modeFREEDV) {
freedv_reset_tx_text_index();
}
#endif
SetChannelState(CHANNEL_RX0,0,1);
SetChannelState(CHANNEL_TX,1,0);
} else {
SetChannelState(CHANNEL_TX,0,1);
SetChannelState(CHANNEL_RX0,1,0);
}
}
}
int getMox() {
return mox;
}
void setTune(int state) {
fprintf(stderr,"setTune: protocol=%d\n", protocol);
if(tune!=state) {
tune=state;
if(tune) {
if(OCmemory_tune_time!=0) {
struct timeval te;
gettimeofday(&te,NULL);
tune_timeout=(te.tv_sec*1000LL+te.tv_usec/1000)+(long long)OCmemory_tune_time;
}
}
if(protocol==NEW_PROTOCOL) {
schedule_high_priority(4);
schedule_general();
}
if(tune) {
SetChannelState(CHANNEL_RX0,0,1);
SetChannelState(CHANNEL_TX,1,0);
} else {
SetChannelState(CHANNEL_TX,0,1);
SetChannelState(CHANNEL_RX0,1,0);
}
}
}
int getTune() {
return tune;
}
int isTransmitting() {
return ptt!=0 || mox!=0 || tune!=0;
}
void setFrequency(long long f) {
BANDSTACK_ENTRY* entry=bandstack_entry_get_current();
if(entry->frequencyA!=f) {
switch(protocol) {
case NEW_PROTOCOL:
case ORIGINAL_PROTOCOL:
entry->frequencyA=f;
break;
#ifdef LIMESDR
case LIMESDR_PROTOCOL:
{
long long minf=entry->frequencyA-(long long)(sample_rate/2);
long long maxf=entry->frequencyA+(long long)(sample_rate/2);
if(f<minf) f=minf;
if(f>maxf) f=maxf;
ddsOffset=f-entry->frequencyA;
fprintf(stderr,"radio.c: setFrequency: ddsOffset=%lld\n",ddsOffset);
wdsp_set_offset(ddsOffset);
return;
}
break;
#endif
}
}
//fprintf(stderr,"setFrequency: protocol=%d f=%lld\n", protocol, f);
ddsFrequency=f;
switch(protocol) {
case NEW_PROTOCOL:
schedule_high_priority(5);
break;
case ORIGINAL_PROTOCOL:
schedule_frequency_changed();
break;
#ifdef LIMESDR
case LIMESDR_PROTOCOL:
lime_protocol_set_frequency(f);
ddsOffset=0;
wdsp_set_offset(ddsOffset);
break;
#endif
}
}
long long getFrequency() {
return ddsFrequency;
}
double getDrive() {
return (double)drive/255.0;
}
void setDrive(double value) {
drive=(int)(value*255.0);
if(protocol==NEW_PROTOCOL) {
schedule_high_priority(6);
}
}
double getTuneDrive() {
return (double)tune_drive/255.0;
}
void setTuneDrive(double value) {
fprintf(stderr,"setTuneDrive: protocol=%d\n", protocol);
tune_drive=(int)(value*255.0);
if(protocol==NEW_PROTOCOL) {
schedule_high_priority(7);
}
}
void set_attenuation(int value) {
//attenuation=value;
switch(protocol) {
case NEW_PROTOCOL:
schedule_high_priority(8);
break;
#ifdef LIMESDR
case LIMESDR_PROTOCOL:
lime_protocol_set_attenuation(value);
break;
#endif
}
}
int get_attenuation() {
return attenuation;
}
void set_alex_rx_antenna(int v) {
//alex_rx_antenna=v;
if(protocol==NEW_PROTOCOL) {
schedule_high_priority(1);
}
#ifdef LIMESDR
if(protocol==LIMESDR_PROTOCOL) {
lime_protocol_set_antenna(v);;
}
#endif
}
void set_alex_tx_antenna(int v) {
//alex_tx_antenna=v;
if(protocol==NEW_PROTOCOL) {
schedule_high_priority(2);
}
}
void set_alex_attenuation(int v) {
//alex_attenuation=v;
if(protocol==NEW_PROTOCOL) {
schedule_high_priority(0);
}
}
void radioRestoreState() {
char *value;
sem_wait(&property_sem);
loadProperties(property_path);
value=getProperty("atlas_penelope");
if(value) atlas_penelope=atoi(value);
value=getProperty("tx_out_of_band");
if(value) tx_out_of_band=atoi(value);
value=getProperty("sample_rate");
if(value) sample_rate=atoi(value);
value=getProperty("filter_board");
if(value) filter_board=atoi(value);
value=getProperty("apollo_tuner");
if(value) apollo_tuner=atoi(value);
value=getProperty("pa");
if(value) pa=atoi(value);
value=getProperty("updates_per_second");
if(value) updates_per_second=atoi(value);
value=getProperty("display_panadapter");
if(value) display_panadapter=atoi(value);
value=getProperty("display_detector_mode");
if(value) display_detector_mode=atoi(value);
value=getProperty("display_average_mode");
if(value) display_average_mode=atoi(value);
value=getProperty("display_average_time");
if(value) display_average_time=atof(value);
value=getProperty("panadapter_high");
if(value) panadapter_high=atoi(value);
value=getProperty("panadapter_low");
if(value) panadapter_low=atoi(value);
value=getProperty("display_waterfall");
if(value) display_waterfall=atoi(value);
value=getProperty("display_sliders");
if(value) display_sliders=atoi(value);
value=getProperty("display_toolbar");
if(value) display_toolbar=atoi(value);
value=getProperty("toolbar_simulate_buttons");
if(value) toolbar_simulate_buttons=atoi(value);
value=getProperty("waterfall_high");
if(value) waterfall_high=atoi(value);
value=getProperty("waterfall_low");
if(value) waterfall_low=atoi(value);
value=getProperty("waterfall_automatic");
if(value) waterfall_automatic=atoi(value);
value=getProperty("volume");
if(value) volume=atof(value);
value=getProperty("drive");
if(value) drive=atoi(value);
value=getProperty("tune_drive");
if(value) tune_drive=atoi(value);
value=getProperty("mic_gain");
if(value) mic_gain=atof(value);
value=getProperty("mic_boost");
if(value) mic_boost=atof(value);
value=getProperty("mic_linein");
if(value) mic_linein=atof(value);
value=getProperty("mic_ptt_enabled");
if(value) mic_ptt_enabled=atof(value);
value=getProperty("mic_bias_enabled");
if(value) mic_bias_enabled=atof(value);
value=getProperty("mic_ptt_tip_bias_ring");
if(value) mic_ptt_tip_bias_ring=atof(value);
value=getProperty("nr_none");
if(value) nr_none=atoi(value);
value=getProperty("nr");
if(value) nr=atoi(value);
value=getProperty("nr2");
if(value) nr2=atoi(value);
value=getProperty("nb");
if(value) nb=atoi(value);
value=getProperty("nb2");
if(value) nb2=atoi(value);
value=getProperty("anf");
if(value) anf=atoi(value);
value=getProperty("snb");
if(value) snb=atoi(value);
value=getProperty("nr_agc");
if(value) nr_agc=atoi(value);
value=getProperty("nr2_gain_method");
if(value) nr2_gain_method=atoi(value);
value=getProperty("nr2_npe_method");
if(value) nr2_npe_method=atoi(value);
value=getProperty("nr2_ae");
if(value) nr2_ae=atoi(value);
value=getProperty("agc");
if(value) agc=atoi(value);
value=getProperty("agc_gain");
if(value) agc_gain=atof(value);
value=getProperty("step");
if(value) step=atoi(value);
value=getProperty("cw_keys_reversed");
if(value) cw_keys_reversed=atoi(value);
value=getProperty("cw_keyer_speed");
if(value) cw_keyer_speed=atoi(value);
value=getProperty("cw_keyer_mode");
if(value) cw_keyer_mode=atoi(value);
value=getProperty("cw_keyer_weight");
if(value) cw_keyer_weight=atoi(value);
value=getProperty("cw_keyer_spacing");
if(value) cw_keyer_spacing=atoi(value);
value=getProperty("cw_keyer_internal");
if(value) cw_keyer_internal=atoi(value);
value=getProperty("cw_keyer_sidetone_volume");
if(value) cw_keyer_sidetone_volume=atoi(value);
value=getProperty("cw_keyer_ptt_delay");
if(value) cw_keyer_ptt_delay=atoi(value);
value=getProperty("cw_keyer_hang_time");
if(value) cw_keyer_hang_time=atoi(value);
value=getProperty("cw_keyer_sidetone_frequency");
if(value) cw_keyer_sidetone_frequency=atoi(value);
value=getProperty("cw_breakin");
if(value) cw_breakin=atoi(value);
value=getProperty("vfo_encoder_divisor");
if(value) vfo_encoder_divisor=atoi(value);
value=getProperty("OCtune");
if(value) OCtune=atoi(value);
value=getProperty("OCfull_tune_time");
if(value) OCfull_tune_time=atoi(value);
value=getProperty("OCmemory_tune_time");
if(value) OCmemory_tune_time=atoi(value);
value=getProperty("attenuation");
if(value) attenuation=atoi(value);
value=getProperty("rx_dither");
if(value) rx_dither=atoi(value);
value=getProperty("rx_random");
if(value) rx_random=atoi(value);
value=getProperty("rx_preamp");
if(value) rx_preamp=atoi(value);
#ifdef FREEDV
value=getProperty("freedv_tx_text_data");
if(value) strcpy(freedv_tx_text_data,value);
#endif
bandRestoreState();
sem_post(&property_sem);
}
void radioSaveState() {
char value[80];
sem_wait(&property_sem);
sprintf(value,"%d",atlas_penelope);
setProperty("atlas_penelope",value);
sprintf(value,"%d",sample_rate);
setProperty("sample_rate",value);
sprintf(value,"%d",filter_board);
setProperty("filter_board",value);
sprintf(value,"%d",apollo_tuner);
setProperty("apollo_tuner",value);
sprintf(value,"%d",pa);
setProperty("pa",value);
sprintf(value,"%d",updates_per_second);
setProperty("updates_per_second",value);
sprintf(value,"%d",display_panadapter);
setProperty("display_panadapter",value);
sprintf(value,"%d",display_detector_mode);
setProperty("display_detector_mode",value);
sprintf(value,"%d",display_average_mode);
setProperty("display_average_mode",value);
sprintf(value,"%f",display_average_time);
setProperty("display_average_time",value);
sprintf(value,"%d",panadapter_high);
setProperty("panadapter_high",value);
sprintf(value,"%d",panadapter_low);
setProperty("panadapter_low",value);
sprintf(value,"%d",display_waterfall);
setProperty("display_waterfall",value);
sprintf(value,"%d",display_sliders);
setProperty("display_sliders",value);
sprintf(value,"%d",display_toolbar);
setProperty("display_toolbar",value);
sprintf(value,"%d",toolbar_simulate_buttons);
setProperty("toolbar_simulate_buttons",value);
sprintf(value,"%d",waterfall_high);
setProperty("waterfall_high",value);
sprintf(value,"%d",waterfall_low);
setProperty("waterfall_low",value);
sprintf(value,"%d",waterfall_automatic);
setProperty("waterfall_automatic",value);
sprintf(value,"%f",volume);
setProperty("volume",value);
sprintf(value,"%f",mic_gain);
setProperty("mic_gain",value);
sprintf(value,"%d",drive);
setProperty("drive",value);
sprintf(value,"%d",tune_drive);
setProperty("tune_drive",value);
sprintf(value,"%d",mic_boost);
setProperty("mic_boost",value);
sprintf(value,"%d",mic_linein);
setProperty("mic_linein",value);
sprintf(value,"%d",mic_ptt_enabled);
setProperty("mic_ptt_enabled",value);
sprintf(value,"%d",mic_bias_enabled);
setProperty("mic_bias_enabled",value);
sprintf(value,"%d",mic_ptt_tip_bias_ring);
setProperty("mic_ptt_tip_bias_ring",value);
sprintf(value,"%d",nr_none);
setProperty("nr_none",value);
sprintf(value,"%d",nr);
setProperty("nr",value);
sprintf(value,"%d",nr2);
setProperty("nr2",value);
sprintf(value,"%d",nb);
setProperty("nb",value);
sprintf(value,"%d",nb2);
setProperty("nb2",value);
sprintf(value,"%d",anf);
setProperty("anf",value);
sprintf(value,"%d",snb);
setProperty("snb",value);
sprintf(value,"%d",nr_agc);
setProperty("nr_agc",value);
sprintf(value,"%d",nr2_gain_method);
setProperty("nr2_gain_method",value);
sprintf(value,"%d",nr2_npe_method);
setProperty("nr2_npe_method",value);
sprintf(value,"%d",nr2_ae);
setProperty("nr2_ae",value);
sprintf(value,"%d",agc);
setProperty("agc",value);
sprintf(value,"%f",agc_gain);
setProperty("agc_gain",value);
sprintf(value,"%d",step);
setProperty("step",value);
sprintf(value,"%d",cw_keys_reversed);
setProperty("cw_keys_reversed",value);
sprintf(value,"%d",cw_keyer_speed);
setProperty("cw_keyer_speed",value);
sprintf(value,"%d",cw_keyer_mode);
setProperty("cw_keyer_mode",value);
sprintf(value,"%d",cw_keyer_weight);
setProperty("cw_keyer_weight",value);
sprintf(value,"%d",cw_keyer_spacing);
setProperty("cw_keyer_spacing",value);
sprintf(value,"%d",cw_keyer_internal);
setProperty("cw_keyer_internal",value);
sprintf(value,"%d",cw_keyer_sidetone_volume);
setProperty("cw_keyer_sidetone_volume",value);
sprintf(value,"%d",cw_keyer_ptt_delay);
setProperty("cw_keyer_ptt_delay",value);
sprintf(value,"%d",cw_keyer_hang_time);
setProperty("cw_keyer_hang_time",value);
sprintf(value,"%d",cw_keyer_sidetone_frequency);
setProperty("cw_keyer_sidetone_frequency",value);
sprintf(value,"%d",cw_breakin);
setProperty("cw_breakin",value);
sprintf(value,"%d",vfo_encoder_divisor);
setProperty("vfo_encoder_divisor",value);
sprintf(value,"%d",OCtune);
setProperty("OCtune",value);
sprintf(value,"%d",OCfull_tune_time);
setProperty("OCfull_tune_time",value);
sprintf(value,"%d",OCmemory_tune_time);
setProperty("OCmemory_tune_time",value);
sprintf(value,"%d",attenuation);
setProperty("attenuation",value);
sprintf(value,"%d",rx_dither);
setProperty("rx_dither",value);
sprintf(value,"%d",rx_random);
setProperty("rx_random",value);
sprintf(value,"%d",rx_preamp);
setProperty("rx_preamp",value);
#ifdef FREEDV
if(strlen(freedv_tx_text_data)>0) {
setProperty("freedv_tx_text_data",freedv_tx_text_data);
}
#endif
bandSaveState();
saveProperties(property_path);
sem_post(&property_sem);
}
void calculate_display_average() {
double display_avb;
int display_average;
double t=0.001*display_average_time;
display_avb = exp(-1.0 / ((double)updates_per_second * t));
display_average = max(2, (int)min(60, (double)updates_per_second * t));
SetDisplayAvBackmult(CHANNEL_RX0, 0, display_avb);
SetDisplayNumAverage(CHANNEL_RX0, 0, display_average);
}