-
Notifications
You must be signed in to change notification settings - Fork 4
/
hpsdr.cc
1486 lines (1333 loc) · 35.4 KB
/
hpsdr.cc
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Talk over the ethernet to an OpenHPSDR radio
// using Protocol 2.
//
// Tested with a blue Apache Labs ANAN-7000dle.
//
// Robert Morris, AB1HL.
//
#include "hpsdr.h"
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <sys/time.h>
#include <sys/select.h>
#include <vector>
#include <thread>
#include <sndfile.h>
#include <ifaddrs.h>
#include <net/if.h>
#include "fft.h"
#include "util.h"
//
// protocol 2 packet formats from:
// openHPSDR Ethernet Protocol V3.8,
// March 2019, Phil Harman VK6PH
//
//
// the sequence:
// host sends discovery packet to broadcast:1024.
// radio replies, revealing its IP address.
// host sends setup packets (but radio doesn't reply):
// general registers -> port 1024
// ddc registers -> port 1025
// duc registers -> port 1026
// high priority -> port 1027 (with "run" bit set)
// radio sends:
// high priority status from 1025
// mic in from 1026
// wideband from 1027
// DDC0 I&Q rom 1035
//
// host must send a Command & Control packet (C&C) every 100 ms.
//
#define ANGELIA_BOARD 3
#define ORION2_BOARD 5
// append a 32-bit value.
// most significant byte first.
static void
push32(std::vector<char> &v, int x)
{
v.push_back((x >> 24) & 0xff);
v.push_back((x >> 16) & 0xff);
v.push_back((x >> 8) & 0xff);
v.push_back((x >> 0) & 0xff);
}
// x may be negative.
static void
push24(std::vector<char> &v, int x)
{
if(x > 0)
assert(x < (1<<23));
if(x < 0)
assert(x >= -(1<<23));
v.push_back((x >> 16) & 0xff);
v.push_back((x >> 8) & 0xff);
v.push_back((x >> 0) & 0xff);
}
static void
push16(std::vector<char> &v, int x)
{
v.push_back((x >> 8) & 0xff);
v.push_back((x >> 0) & 0xff);
}
static void
push8(std::vector<char> &v, int x)
{
v.push_back((x >> 0) & 0xff);
}
static unsigned int
r32(const std::vector<char> &v, int off)
{
unsigned int x = 0;
x |= (v[off+0] & 0xff) << 24;
x |= (v[off+1] & 0xff) << 16;
x |= (v[off+2] & 0xff) << 8;
x |= (v[off+3] & 0xff) << 0;
return x;
}
//
// signed! for I/Q samples.
//
static int
r24(const std::vector<char> &v, int off)
{
unsigned int x = 0;
x |= (v[off+0] & 0xff) << 16;
x |= (v[off+1] & 0xff) << 8;
x |= (v[off+2] & 0xff) << 0;
if(x & (1 << 23))
x |= 0xff000000;
return x;
}
static unsigned int
r16(const std::vector<char> &v, int off)
{
unsigned int x = 0;
x |= (v[off+0] & 0xff) << 8;
x |= (v[off+1] & 0xff) << 0;
return x;
}
HPSDR *
HPSDR::open()
{
static HPSDR *sdr = 0;
if(sdr == 0){
sdr = new HPSDR;
std::thread th( [ ] () { sdr->loop(); } );
th.detach();
}
return sdr;
}
HPSDR::HPSDR()
{
state_ = None;
s_ = -1;
last_discover_ = 0;
last_general_ = 0;
last_high_ = 0;
last_swr_print_ = 0;
run_ = 0;
board_type_ = -1;
do_dither_ = 1;
do_random_ = 1;
do_lpf_ = 1;
do_hpf_ = 1;
do_attenuate_ = 0;
do_ext1_ = 0;
do_both_ = 0;
step_[0] = 0;
step_[1] = 0;
last_clipped_[0] = 0;
last_clipped_[1] = 0;
last_unclipped_[0] = 0;
last_unclipped_[1] = 0;
tx_active_ = 0;
tx_seq_ = 0;
tx_ptt_ = 0;
tx_power_ = 1;
tx_hz_ = 0;
tx_pa_ = 0; // enable power amplifier?
tx_drive_ = 0;
tx_drive_override_ = -1;
for(int i = 0; i < NDDC; i++){
ddc_[i].active_ = 0;
ddc_[i].hz_ = 0;
ddc_[i].sdr_rate_ = 0;
ddc_[i].rate_ = 0;
ddc_[i].count_ = 0;
ddc_[i].expect_seq_ = 0;
}
check_config_files();
}
//
// allocate an unused DDC.
//
int
HPSDR::allocate_unit(int rate)
{
for(int unit = 0; unit < NDDC; unit++){
if(ddc_[unit].hz_ == 0){
activate(unit, rate);
ddc_[unit].active_ = 1;
configuration_changed_ = 1;
return unit;
}
}
return -1;
}
void
HPSDR::set_freq(int unit, int hz)
{
assert(unit >= 0 && unit < NDDC && ddc_[unit].active_);
ddc_[unit].hz_ = hz;
}
void
HPSDR::activate(int unit, int rate)
{
assert(unit >= 0 && unit < NDDC);
ddc_[unit].sdr_rate_ = 48000;
if(rate > 0)
ddc_[unit].rate_ = rate;
else
ddc_[unit].rate_ = ddc_[unit].sdr_rate_;
assert(ddc_[unit].rate_ <= ddc_[unit].sdr_rate_);
assert((ddc_[unit].sdr_rate_ % ddc_[unit].rate_) == 0);
ddc_[unit].count_ = 0;
if(ddc_[unit].rate_ < ddc_[unit].sdr_rate_){
// Liquid DSP filter + resampler to convert sdr_rate_ to rate_.
int h_len = estimate_req_filter_len(0.01, 60.0);
float h[h_len];
double cutoff = (ddc_[unit].rate_ / (double) ddc_[unit].sdr_rate_) / 2.0;
#if 1
float bands[4] = {
0.0, (float)(cutoff * 0.9), // pass-band
(float)cutoff, 0.5 }; // stop-band
float des[2] = { 1.0, 0.0 }; // desired response
float weights[2] = { 1.0, 1.0 };
liquid_firdespm_wtype wtype[2] = {
LIQUID_FIRDESPM_EXPWEIGHT,
LIQUID_FIRDESPM_FLATWEIGHT,
};
liquid_firdespm_btype btype = LIQUID_FIRDESPM_BANDPASS;
firdespm_run(h_len, 2, bands, des, weights, wtype, btype, h);
#else
cutoff *= 0.9;
liquid_firdes_kaiser(h_len, cutoff, 60.0, 0.0, h);
#endif
ddc_[unit].filter_ = firfilt_crcf_create(h, h_len);
}
}
HPSDR::~HPSDR()
{
if(s_ >= 0)
close(s_);
}
void
HPSDR::list()
{
int s = open_socket();
discover(s); // send a discovery broadcast packet.
for(int i = 0; i < 20 && wait_socket(s, 100); i++){
struct sockaddr_in from;
socklen_t fromlen = sizeof(from);
char buf[2048];
int cc = recvfrom(s, buf, sizeof(buf), 0, (sockaddr *) &from, &fromlen);
if(cc < 0){
perror("hpsdr: recvfrom");
exit(1);
}
if(cc == 60 && (buf[4] == 0x02 || buf[4] == 0x03)){
// 0x02 idle, 0x03 busy
const char *types[] = {
"Atlas", "Hermes", "Hermes2", "Angelia", "Orion",
"OrionMkII", "HermesLite" };
int type = buf[11] & 0xff;
int protocol = buf[12] & 0xff;
int firmware = buf[13] & 0xff;
int ddcs = buf[20] & 0xff; // number of DDCs
printf("HPSDR %s %s %s protocol=%d.%d firmware=%d.%d ddcs=%d\n",
inet_ntoa(from.sin_addr),
(type >= 0 && type < 7) ? types[type] : "???",
buf[4] == 0x02 ? "idle" : "busy",
protocol / 10,
protocol % 10,
firmware / 10,
firmware % 10,
ddcs);
}
}
close(s);
}
std::vector<char>
HPSDR::make_discovery_packet()
{
std::vector<char> v;
push32(v, 0); // Seq #
v.push_back(0x02); // Command - Discovery
while(v.size() < 60)
v.push_back(0);
return v;
}
int
HPSDR::open_socket()
{
int s = socket(AF_INET, SOCK_DGRAM, 0);
if(s < 0){
perror("hpsdr: socket");
exit(1);
}
int yes = 1;
if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes)) < 0){
perror("hpsdr: SO_BROADCAST");
}
int aaa = 0;
socklen_t aaalen = sizeof(aaa);
getsockopt(s, SOL_SOCKET, SO_RCVBUF, &aaa, &aaalen);
int bbb = 1024 * 1024;
if(bbb > aaa){
if(setsockopt(s, SOL_SOCKET, SO_RCVBUF, &bbb, sizeof(bbb)) < 0){
perror("hpsdr: SO_RCVBUF");
}
// int ccc = 0;
// socklen_t ccclen = sizeof(ccc);
// getsockopt(s, SOL_SOCKET, SO_RCVBUF, &ccc, &ccclen);
// fprintf(stderr, "RCVBUF %d %d %d\n", aaa, bbb, ccc);
}
return s;
}
//
// send out a broadcast discover packet on each network interface.
//
void
HPSDR::discover(int s)
{
struct ifaddrs *ifap;
if(getifaddrs(&ifap) < 0){
perror("hpsdr: getifaddrs");
exit(1);
}
for(struct ifaddrs *ifa = ifap; ifa; ifa = ifa->ifa_next){
if(ifa->ifa_broadaddr == 0)
continue;
if((ifa->ifa_flags & IFF_UP) == 0)
continue;
if((ifa->ifa_flags & IFF_BROADCAST) == 0)
continue;
if(ifa->ifa_broadaddr->sa_family != AF_INET)
continue;
sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
// despite what the protocol 2 document says, it seems like
// you have to send to the broadcast address, not to
// a specific IP address.
sin.sin_addr = ((sockaddr_in *)(ifa->ifa_broadaddr))->sin_addr;
// port 1024 is the discovery port
sin.sin_port = htons(1024);
std::vector<char> pkt = make_discovery_packet();
if(sendto(s, pkt.data(), pkt.size(), 0, (sockaddr *) &sin, sizeof(sin)) < 0){
perror("hpsdr: sendto");
}
}
freeifaddrs(ifap);
}
//
// if file exists, read a number from it and put it in flag.
// otherwise do nothing.
//
void
check_file(const char *file, int &flag)
{
FILE *fp = fopen(file, "r");
if(fp == 0)
return;
int xxx = 0;
if(fscanf(fp, "%d", &xxx) == 1){
if(flag != xxx)
fprintf(stderr, "%s changed from %d to %d\n", file, flag, xxx);
flag = xxx;
}
fclose(fp);
}
void
check_file(const char *file, volatile int &flag)
{
int z = flag;
check_file(file, z);
flag = z;
}
void
HPSDR::check_config_files()
{
check_file("hpsdr-dither", do_dither_);
check_file("hpsdr-random", do_random_);
check_file("hpsdr-lpf", do_lpf_);
check_file("hpsdr-hpf", do_hpf_);
check_file("hpsdr-attenuate", do_attenuate_);
check_file("hpsdr-ext1", do_ext1_);
check_file("hpsdr-pa", tx_pa_);
check_file("hpsdr-power", tx_power_);
check_file("hpsdr-drive", tx_drive_override_);
check_file("hpsdr-both", do_both_);
}
//
// run timers.
//
void
HPSDR::tick()
{
if(state_ == None){
s_ = open_socket();
state_ = Discovering;
run_ = 1;
}
if(state_ == Discovering && now() > last_discover_ + 5){
discover(s_);
last_discover_ = now();
}
if(state_ == Running && (configuration_changed_ || now() > last_general_ + 5)){
configuration_changed_ = 0;
if(run_ && (int) ddc_[0].input_.size() > 100*ddc_[0].rate_){
fprintf(stderr, "HPSDR::tick too much input\n");
ddc_[0].input_.clear();
}
check_config_files();
send_general();
send_ddc();
send_duc();
last_general_ = now();
}
if(state_ == Running && now() > last_high_ + 0.2){
// send "high priority" packets frequently, to keep
// the watchdog timer from expiring, and to change
// frequency promptly if need be.
send_high();
last_high_ = now();
}
}
//
// wait up to ms for input on s_,
// return 1 if s_ is readable, 0 otherwise.
//
int
HPSDR::wait_socket(int s, int ms)
{
fd_set readfds, writefds, exceptfds;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
FD_SET(s, &readfds);
timeval tv;
tv.tv_sec = ms / 1000;
tv.tv_usec = (ms % 1000) * 1000;
int n = select(s + 1, &readfds, &writefds, &exceptfds, &tv);
if(n > 0 && FD_ISSET(s, &readfds)){
return 1;
} else {
return 0;
}
}
void
HPSDR::process()
{
struct sockaddr_in from;
socklen_t fromlen = sizeof(from);
char buf[2048];
int cc = recvfrom(s_, buf, sizeof(buf), 0, (sockaddr *) &from, &fromlen);
if(cc < 0){
perror("hpsdr: recvfrom");
exit(1);
}
process_packet(std::vector<char>(buf, buf+cc), from);
}
void
HPSDR::process_discovery_response(const std::vector<char> &buf,
const sockaddr_in &from)
{
radio_sin_ = from;
board_type_ = buf[11] & 0xff; // 3 = Angelia, 5 = Orion Mk II
}
//
// send general setup packet.
//
void
HPSDR::send_general()
{
std::vector<char> v;
push32(v, 0); // 0: SEQ
push8(v, 0); // 4: Command
push16(v, 1025); // 5: DDC port
push16(v, 1026); // 7: DUC port
push16(v, 1027); // 9: high priority from PC port
push16(v, 1025); // 11: high priority to PC port
push16(v, 1028); // 13: DDC audio port
push16(v, 1029); // 15: DUC0 I&Q base port
push16(v, 1035); // 17: DDC0 port (DDCx up to ... + 79)
push16(v, 1026); // 19: Mic samples port
push16(v, 1027); // 21: wideband ADC0 port
push8(v, 0); // 23: wideband enable
push16(v, 512); // 24: wideband samples per packet
push8(v, 16); // 26: wideband sample size
push8(v, 70); // 27: wideband update rate, ms per frame
push8(v, 32); // 28: wideband packets per frame
push16(v, 0); // 29: memory mapped from PC port
push16(v, 0); // 31: memory mapped to PC port
push16(v, 0); // 33: reserved
push16(v, 0); // 35: reserved
push8(v, 1 << 3); // 37: bit[3]=1 means frequency set as phase word
push8(v, 1); // 38: enable hardware reset / watchdog timer
push8(v, 0); // 39: big-endian, I&Q 3-byte format
for(int i = 40; i < 56; i++)
push8(v, 0);
push8(v, 0); // 56: Atlas/Mercury DDC config -- ignored?
push8(v, 0); // 57: ref clock source -- ignored?
push8(v, tx_pa_); // 58: enable PA
int alexes = 1;
if(do_both_)
alexes |= 2; // enable alex1
push8(v, alexes); // 59: enable Alex filter boards, one bit each
assert(v.size() == 60);
assert(ntohs(radio_sin_.sin_port) == 1024);
if(sendto(s_, v.data(), v.size(), 0, (sockaddr *) &radio_sin_,
sizeof(radio_sin_)) < 0){
perror("hpsdr: sendto");
}
}
//
// send DDC setup packet to port 1025.
//
void
HPSDR::send_ddc()
{
std::vector<char> v;
push32(v, 0); // 0: seq
push8(v, 2); // 4: two ADCs on the ANAN-100D
push8(v, do_dither_?3:0); // 5: activate ADC dither, bitmap.
push8(v, do_random_?3:0); // 6: activate ADC random, bitmap.
unsigned int active = 0;
for(int i = 0; i < NDDC && i < 8; i++){
if(ddc_[i].active_)
active |= (1 << i);
}
push8(v, active); // 7: activate DDCx, bitmap.
for(int i = 8; i < 17; i++)
push8(v, 0); // don't activate DDCs 8..79
for(int unit = 0; unit < 80; unit++){
int adc = (unit > 0 && do_both_) ? 1 : 0;
push8(v, adc); // which ADC does DDCx talk to?
int rate = unit < NDDC ? ddc_[unit].sdr_rate_ / 1000 : 48;
push16(v, rate); // DDCx sampling rate; 48 is 48000
push16(v, 0); // reserved
push8(v, 24); // DDCx I&Q sample size
}
for(int i = 497; i < 1443; i++)
push8(v, 0);
push8(v, 0); // 1443: not used
assert(v.size() == 1444);
sockaddr_in sin = radio_sin_;
sin.sin_port = htons(1025);
if(sendto(s_, v.data(), v.size(), 0, (sockaddr *) &sin,
sizeof(sin)) < 0){
perror("hpsdr: sendto");
}
}
//
// send DUC setup packet to port 1026.
//
void
HPSDR::send_duc()
{
std::vector<char> v;
push32(v, 0); // 0: seq
push8(v, 1); // 4: # of DACs
push8(v, 0); // 5: 0 means not CW
push8(v, 0); // 6: CW sidetone level
push16(v, 0);// 7: CW sidetone hz
push8(v, 0); // 9: CW keyer speed WPM
push8(v, 50); // 10: CW weight
push16(v, 0); // 11: CW hang time, ms
push8(v, 0); // 13: RF delay, ms
push16(v, 192); // 14: DUC sample rate
push8(v, 24); // 16: 16 bits/sample
for(int i = 17; i <= 49; i++)
push8(v, 0); // reserved
push8(v, 0); // 50: mic control
push8(v, 0); // 51: line in gain
for(int i = 52; i <= 58; i++)
push8(v, 0); // reserved
push8(v, 0); // 59: reserved
assert(v.size() == 60);
sockaddr_in sin = radio_sin_;
sin.sin_port = htons(1026);
if(sendto(s_, v.data(), v.size(), 0, (sockaddr *) &sin,
sizeof(sin)) < 0){
perror("hpsdr: sendto");
}
}
//
// compute 32-bit "phase word" corresponding to
// a specified frequency.
//
// phase_word[31:0] = 2^32 * frequency(Hz)/DSP clock frequency (Hz)
//
// DSP clock frequency is dependent on the Board type and is either
// specified in Appendix A or, if hardware specific, then as part of
// the Discovery response as specified in Appendix B.
//
unsigned int
HPSDR::ph(int hz)
{
double x = hz / 122880000.0;
x *= 4.0 * 1024.0 * 1024.0 * 1024.0; // 2^32
return x;
}
//
// choose Alex pre-selector control bits
// based on min and max receive frequency,
// and on whether we're transmitting.
//
unsigned int
HPSDR::make_alex0()
{
double lo = -1;
double hi = -1;
if(tx_active_){
lo = tx_hz_;
hi = tx_hz_;
} else if(do_both_){
// just DDC 0 on ADC 0
hi = lo = ddc_[0].hz_;
} else {
// lowest and highest MHz we want to receive?
// in case multiple DDCs.
for(int unit = 0; unit < NDDC; unit++){
int hz = ddc_[unit].hz_;
if(hz > 0){
if(lo < 0 || hz < lo){
lo = hz;
}
if(hi < 0 || hz > hi){
hi = hz;
}
}
}
}
lo /= 1000000.0;
hi /= 1000000.0;
unsigned int alex0 = 0;
//
// TX side.
//
alex0 |= 1 << 24; // Ant 1
if(do_lpf_){
// low-pass filter (shared with TX).
if(hi < 2.0){
alex0 |= 1 << 23; // 160M
} else if(hi < 4.0){
alex0 |= 1 << 22; // 80M
} else if(hi < 7.3){
alex0 |= 1 << 21; // 60/40M
} else if(hi < 14.350){
alex0 |= 1 << 20; // 30/20M
} else if(hi < 21.450){
alex0 |= 1 << 31; // 17/15M
} else if(hi < 30.0){
alex0 |= 1 << 30; // 12/10M
} else {
alex0 |= 1 << 29; // 6M/bypass
}
} else {
alex0 |= 1 << 29; // 6M/Bypass LPF
}
//
// RX side.
//
if(tx_active_){
// do nothing -- T/R switch will disconnect the receivers.
} else {
if(do_ext1_){
alex0 |= 1 << 9; // Ext 1
if(board_type_ == ANGELIA_BOARD){
alex0 |= 1 << 11; // ByPass (disconnect Ant 1 from RX)
}
if(board_type_ == ORION2_BOARD){
alex0 |= 1 << 14; // Rx Master in Sel.
}
}
if(board_type_ == ANGELIA_BOARD){
int att = do_attenuate_;
if(att >= 20){
alex0 |= 1 << 13; // 20 dB Alex attenuator.
att -= 20;
}
if(att >= 10){
alex0 |= 1 << 14; // 10 dB Alex attenuator.
att -= 10;
}
}
if(board_type_ == ANGELIA_BOARD){
if(do_hpf_){
// high-pass filter.
if(lo >= 50.0){
alex0 |= 1 << 3; // 6M with LNA
} else if(lo >= 20.0){
alex0 |= 1 << 2;
} else if(lo >= 13){
alex0 |= 1 << 1;
} else if(lo >= 9.5){
alex0 |= 1 << 4;
} else if(lo >= 6.5){
alex0 |= 1 << 5;
} else if(lo >= 1.5){
alex0 |= 1 << 6;
} else {
alex0 |= 1 << 12; // HF Bypass (maybe doesn't work?)
}
} else {
alex0 |= 1 << 6; // 1.5 MHz HPF
}
}
if(board_type_ == ORION2_BOARD){
if(do_hpf_){
// band-pass filters
if(lo >= 1.5 && hi < 2.1){
alex0 |= 1 << 6;
} else if(lo >= 2.1 && hi < 5.5){
alex0 |= 1 << 5;
} else if(lo >= 5.5 && hi < 10.99){
alex0 |= 1 << 4;
} else if(lo >= 11.0 && hi < 21){
alex0 |= 1 << 1;
} else if(lo >= 21 && hi < 27){
alex0 |= 1 << 2;
} else if(lo >= 27 && hi < 61.4){
alex0 |= 1 << 3;
} else {
alex0 |= 1 << 12; // Bypass
}
} else {
alex0 |= 1 << 12; // Bypass
}
}
}
return alex0;
}
//
// 2nd Alex board, connected to ADC 1.
// only used if do_both_.
// and in that case, units 1, 2, and 3
// use ADC1 and Alex1.
// Alex1 has only RX bandpass filters (no TX filters).
//
unsigned int
HPSDR::make_alex1()
{
if(board_type_ != ORION2_BOARD){
// ANAN-100D has no Alex 1.
return 0;
}
if(tx_active_ || do_both_ == 0){
// RX2 ground
return 1 << 8;
}
// lowest and highest MHz we want to receive?
// in case multiple DDCs.
double lo = -1;
double hi = -1;
for(int unit = 1; unit < NDDC; unit++){
int hz = ddc_[unit].hz_;
if(hz > 0){
if(lo < 0 || hz < lo){
lo = hz;
}
if(hi < 0 || hz > hi){
hi = hz;
}
}
}
lo /= 1000000.0;
hi /= 1000000.0;
unsigned int alex1 = 0;
if(do_hpf_){
// band-pass filters
if(lo >= 1.5 && hi < 2.1){
alex1 |= 1 << 6;
} else if(lo >= 2.1 && hi < 5.5){
alex1 |= 1 << 5;
} else if(lo >= 5.5 && hi < 10.99){
alex1 |= 1 << 4;
} else if(lo >= 11.0 && hi < 21){
alex1 |= 1 << 1;
} else if(lo >= 21 && hi < 27){
alex1 |= 1 << 2;
} else if(lo >= 27 && hi < 61.4){
alex1 |= 1 << 3;
} else {
alex1 |= 1 << 12; // Bypass
}
} else {
alex1 |= 1 << 12; // Bypass
}
return alex1;
}
//
// band selector bits on OC0..OC3 on the DB9 connector
// on the back of the ANAN-7000dle.
//
unsigned int
HPSDR::make_band()
{
int bits = 0;
double mhz = tx_hz_ / 1000000.0;
if(mhz < 1.8){
// ???
} else if(mhz < 2.0){
bits = 1; // 160M
} else if(mhz < 4.0){
bits = 2; // 80M
} else if(mhz < 7.3){
bits = 3; // 40M
} else if(mhz < 10.2){
bits = 4; // 30M
} else if(mhz < 14.350){
bits = 5; // 20M
} else if(mhz < 19){
bits = 6; // 17M
} else if(mhz < 22){
bits = 7; // 15M
} else if(mhz < 25){
bits = 8; // 12M
} else if(mhz < 30){
bits = 9; // 10M
} else if(mhz < 55){
bits = 10; // 6M
}
bits = 3; // XXX
bits <<= 1;
return bits;
}
//
// give a desired PA power output, return a good drive
// level to put in the high-priority packets.
//
// I don't know how to do this correctly. All I know is
// the drive level required to get about 25 watts on
// my ANAN-100D.
//
// the drive-vs-power relationship is not linear,
// but I don't know what it is.
//
int
HPSDR::watts2drive(int hz, int watts)
{
if(watts > 30)
watts = 30;
int drive = 10; // pretty low.
double mhz = hz / 1000000.0;
if(board_type_ == ORION2_BOARD){
// use Blue ANAN-7000DLE measurements at 5 watts
if(mhz >= 1.5 && mhz <= 2){
drive = 23 * watts / 5.0;
} else if(mhz >= 3.5 && mhz <= 4){
drive = 21 * watts / 5.0;
} else if(mhz >= 7 && mhz <= 8){
drive = 25 * watts / 5.0;
} else if(mhz >= 10 && mhz <= 11){
drive = 26 * watts / 5.0;
} else if(mhz >= 14 && mhz <= 15){
drive = 30 * watts / 5.0;
} else if(mhz >= 18 && mhz <= 19){
drive = 24 * watts / 5.0;
} else if(mhz >= 21 && mhz <= 22){
drive = 27 * watts / 5.0;
} else if(mhz >= 24 && mhz <= 25){
drive = 30 * watts / 5.0;
} else if(mhz >= 28 && mhz <= 30){
drive = 37 * watts / 5.0;
} else if(mhz >= 50 && mhz <= 54){
drive = 37 * watts / 5.0;
} else {
static int once = 0;
if(once == 0){
fprintf(stderr, "HPSDR::watts2drive(%d, %d) : %d hz not recognized\n",
hz, watts, hz);
once = 1;
}
}
}
if(board_type_ == ANGELIA_BOARD){
if(watts <= 10){
// use ANAN-100D measurements at 5 watts
if(mhz >= 1.5 && mhz <= 2){
drive = 16 * watts / 5.0;
} else if(mhz >= 3.5 && mhz <= 4){
drive = 17 * watts / 5.0;
} else if(mhz >= 7 && mhz <= 8){
drive = 20 * watts / 5.0;
} else if(mhz >= 10 && mhz <= 11){
drive = 21 * watts / 5.0;
} else if(mhz >= 14 && mhz <= 15){
drive = 26 * watts / 5.0;
} else if(mhz >= 18 && mhz <= 19){
drive = 26 * watts / 5.0;
} else if(mhz >= 21 && mhz <= 22){
drive = 30 * watts / 5.0;
} else if(mhz >= 24 && mhz <= 25){
drive = 25 * watts / 5.0;
} else if(mhz >= 28 && mhz <= 30){
drive = 31 * watts / 5.0;
} else if(mhz >= 50 && mhz <= 54){
drive = 55 * watts / 5.0;
} else {
static int once = 0;
if(once == 0){
fprintf(stderr, "HPSDR::watts2drive(%d, %d) : %d hz not recognized\n",
hz, watts, hz);
once = 1;
}
}
} else {
// use ANAN-100D measurements at 25 watts
if(mhz >= 1.5 && mhz <= 2){
drive = 27 * watts / 25.0;
} else if(mhz >= 3.5 && mhz <= 4){
drive = 30 * watts / 25.0;
} else if(mhz >= 7 && mhz <= 8){
drive = 33 * watts / 25.0;
} else if(mhz >= 10 && mhz <= 11){
drive = 35 * watts / 25.0;
} else if(mhz >= 14 && mhz <= 15){