forked from jks-prv/KiwiSDR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
640 lines (547 loc) · 21.7 KB
/
Copy pathmain.cpp
File metadata and controls
640 lines (547 loc) · 21.7 KB
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
/*
--------------------------------------------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Boston, MA 02110-1301, USA.
--------------------------------------------------------------------------------
*/
// Copyright (c) 2014-2026 John Seamons, ZL4VO/KF6VO
#include "types.h"
#include "config.h"
#include "kiwi.h"
#include "rx.h"
#include "clk.h"
#include "misc.h"
#include "str.h"
#include "mem.h"
#include "web.h"
#include "peri.h"
#include "eeprom.h"
#include "spi.h"
#include "gps.h"
#include "gps_fe.h"
#include "rf_attn.h"
#include "coroutines.h"
#include "cfg.h"
#include "net.h"
#include "ext_int.h"
#include "sanitizer.h"
#include "shmem.h" // shmem_init()
#include "debug.h"
#include "fpga.h"
#include "rx_util.h"
#include "rx_waterfall_cmd.h"
#include "net.h"
#include "security.h"
#include "ansi.h"
#include "other.gen.h"
#ifdef EV_MEAS
#warning NB: EV_MEAS is enabled
#endif
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <time.h>
#include <sys/resource.h>
#include <sched.h>
#include <math.h>
#include <stdlib.h>
#include <errno.h>
kiwi_t kiwi;
int version_maj, version_min;
int fpga_id, rx_chans, rx_wb_buf_chans, v_wb_buf_chans, wf_chans, wb_chans,
nrx_bufs, nrx_samps, nrx_samps_wb,
nwf_nxfer, nwf_samps, nwf_tsamps, nwf_nxferL, nwf_sampsL, nwf_tsampsL,
snd_rate, snd_rate_i, wb_rate, rx_decim, rx1_decim, rx2_decim;
int p0=0, p1=0, p2=0, wf_sim, wf_real, wf_time, ev_dump=0, wf_flip, wf_start=1, down,
rx_yield=1000, gps_chans=GPS_MAX_CHANS, wf_max, cfg_no_wf, snr_meas=1,
spi_clkg, spi_speed, spi_mode = -1, spi_no_async, spi_test,
do_gps, do_sdr=1, wf_olap, meas, spi_delay=100, debian_ver, monitors_max, bg,
print_stats, ecpu_cmds, ecpu_tcmds, use_spidev, spidev_maj = -1, spidev_min = -1,
debian_maj, debian_min, test_flag, dx_print, wf_full_rate,
gps_debug, gps_var, gps_lo_gain, gps_cg_gain, use_foptim, is_locked, drm_nreg_chans;
u4_t ov_mask, snd_intr_usec;
bool need_hardware, kiwi_reg_debug, gps_e1b_only, ecpu_stack_check, spi_show_stats,
disable_led_task, is_multi_core, debug_printfs, cmd_debug, gen_debug;
int main_argc;
char **main_argv;
static bool _kiwi_restart;
void kiwi_restart()
{
#ifdef USE_ASAN
// leak detector needs exit while running on main() stack
_kiwi_restart = true;
TaskWakeupF(TID_MAIN, TWF_CANCEL_DEADLINE);
while (1)
TaskSleepSec(1);
#else
kiwi_exit(0);
#endif
}
#ifdef USE_OTHER
void other_task(void *param)
{
void other_main(int argc, char *argv[]);
other_main(main_argc, main_argv);
kiwi_exit(0);
}
#endif
int main(int argc, char *argv[])
{
int i;
int p_gps = 0, gpio_test_pin = 0, drop_root = 0;
eeprom_action_e eeprom_action = EE_NORM;
int fw_sel_override = FW_CONFIGURED;
int fw_test = 0;
int fpga_id_check = 1;
int fpga_sim_fail = 0;
int wb_sel, wb_sel_override = -1;
kiwi.a50 = -1;
version_maj = VERSION_MAJ;
version_min = VERSION_MIN;
#ifdef MULTI_CORE
is_multi_core = true;
#endif
main_argc = argc;
main_argv = argv;
#ifdef DEVSYS
do_sdr = 0;
p_gps = -1;
#else
// enable generation of core file in /tmp
//scall("core_pattern", system("echo /tmp/core-%e-%s-%p-%t > /proc/sys/kernel/core_pattern"));
// use same filename to prevent looping dumps from filling up filesystem
scall("core_pattern", system("echo /tmp/core-%e > /proc/sys/kernel/core_pattern"));
const struct rlimit unlim = { RLIM_INFINITY, RLIM_INFINITY };
scall("setrlimit", setrlimit(RLIMIT_CORE, &unlim));
system("rm -f /tmp/core-kiwi.*-*"); // remove old core files
set_cpu_affinity(0);
#endif
#ifdef PLATFORM_beaglebone_black
kiwi.platform = PLATFORM_BBG_BBB;
#endif
#ifdef PLATFORM_beaglebone_ai
kiwi.platform = PLATFORM_BBAI;
#endif
#ifdef PLATFORM_beaglebone_ai64
kiwi.platform = PLATFORM_BBAI_64;
#endif
#ifdef PLATFORM_beagleY_ai
kiwi.platform = PLATFORM_BYAI;
#endif
#ifdef PLATFORM_raspberrypi
kiwi.platform = PLATFORM_RPI;
#endif
kstr_init();
shmem_init();
printf_init();
system("mkdir -p " DIR_DATA);
#define ARG(s) (strcmp(argv[ai], s) == 0)
#define ARGP() argv[++ai]
#define ARGL(v) if (ai+1 < argc) (v) = strtol(argv[++ai], 0, 0);
for (int ai = 1; ai < argc; ) {
if (strncmp(argv[ai], "-o_", 3) == 0) {
fw_sel_override = FW_OTHER;
ai++;
while (ai < argc && ((argv[ai][0] != '+') && (argv[ai][0] != '-'))) ai++;
continue;
}
if (ARG("-fw")) { ARGL(fw_sel_override); printf("firmware select override: %d\n", fw_sel_override); } else
if (ARG("-fw_test")) { ARGL(fw_test); printf("firmware test: %d\n", fw_test); } else
if (ARG("-fpga_id")) fpga_id_check = 0; else
if (ARG("-fpga_fail")) fpga_sim_fail = 1; else
if (ARG("-a50")) { ARGL(kiwi.a50); printf("a50: %d\n", kiwi.a50); } else
if (ARG("-wb")) { ARGL(wb_sel_override); printf("wideband rate override: %d\n", wb_sel_override); } else
if (ARG("-rdoff")) { ARGL(wf_rd_offset); printf("WF rd_offset: %d\n", wf_rd_offset); } else
if (ARG("-wfsd")) { ARGL(wf_slowdown); printf("WF slowdown: %d\n", wf_slowdown); } else
if (ARG("-kiwi_reg")) kiwi_reg_debug = TRUE; else
if (ARG("-cmd_debug")) cmd_debug = TRUE; else
if (ARG("-bg")) { background_mode = TRUE; bg=1; } else
if (ARG("-log")) { log_foreground_mode = TRUE; } else
if (ARG("-fopt")) use_foptim = 1; else // in EDATA_DEVEL mode use foptim version of files
if (ARG("-down")) down = 1; else
if (ARG("+gps")) p_gps = 1; else
if (ARG("-gps")) p_gps = -1; else
if (ARG("+sdr")) do_sdr = 1; else
if (ARG("-sdr")) do_sdr = 0; else
if (ARG("-root")) { ARGL(drop_root); drop_root = drop_root/10 - 1; printf("drop_root %d\n", drop_root); } else
if (ARG("-d")) gen_debug = true; else
if (ARG("-debug")) debug_printfs = true; else
if (ARG("-gps_debug")) { gps_debug = -1; ARGL(gps_debug); } else
if (ARG("-stats") || ARG("+stats")) { print_stats = STATS_TASK; ARGL(print_stats); } else
if (ARG("-gpio")) { ARGL(gpio_test_pin); } else
if (ARG("-estack")) ecpu_stack_check = true; else
if (ARG("-rsid")) kiwi.RsId = true; else
if (ARG("-v")) {} else // dummy arg so Kiwi version can appear in e.g. htop
if (ARG("-test")) { ARGL(test_flag); printf("test_flag %d(0x%x)\n", test_flag, test_flag); } else
if (ARG("-snrmeas")) snr_meas = 0; else
if (ARG("-wffull")) wf_full_rate = 1; else
if (ARG("-mm")) kiwi.test_marine_mobile = true; else
if (ARG("-dx")) { ARGL(dx_print); printf("dx %d(0x%x)\n", dx_print, dx_print); } else
if (ARG("-led") || ARG("-leds")) disable_led_task = true; else
if (ARG("-gps_e1b")) gps_e1b_only = true; else
if (ARG("-gps_var")) { ARGL(gps_var); printf("gps_var %d\n", gps_var); } else
if (ARG("-e1b_lo_gain")) { ARGL(gps_lo_gain); printf("e1b_lo_gain %d\n", gps_lo_gain); } else
if (ARG("-e1b_cg_gain")) { ARGL(gps_cg_gain); printf("e1b_cg_gain %d\n", gps_cg_gain); } else
if (ARG("-debian")) {} else // dummy arg so Kiwi version can appear in e.g. htop
if (ARG("-nmulti")) { is_multi_core = false; printf("no multi-core\n"); } else
if (ARG("-ctrace")) { ARGL(web_caching_debug); } else
if (ARG("-ext")) kiwi.ext_clk = true; else
if (ARG("-use_spidev")) { ARGL(use_spidev); } else
if (ARG("-spidev")) { ARGL(spidev_maj); ARGL(spidev_min); printf("spidev%d.%d\n", spidev_maj, spidev_min); } else
if (ARG("-eeprom_reset")) eeprom_action = EE_RESET; else
if (ARG("-eeprom_fix")) eeprom_action = EE_FIX; else
if (ARG("-eeprom_test")) eeprom_action = EE_TEST; else
if (ARG("-sim")) wf_sim = 1; else
if (ARG("-real")) wf_real = 1; else
if (ARG("-time")) wf_time = 1; else
if (ARG("-dump") || ARG("+dump")) { ARGL(ev_dump); } else
if (ARG("-flip")) wf_flip = 1; else
if (ARG("-start")) wf_start = 1; else
if (ARG("-wmax")) wf_max = 1; else
if (ARG("-olap")) wf_olap = 1; else
if (ARG("-meas")) meas = 1; else
if (ARG("-clkg")) spi_clkg = 1; else
if (ARG("-spispeed")) { ARGL(spi_speed); } else
if (ARG("-spimode")) { ARGL(spi_mode); } else
if (ARG("-spi")) { ARGL(spi_delay); } else
if (ARG("-spistats")) { spi_show_stats = true; } else
if (ARG("-spiasync")) { spi_no_async = 1; } else
if (ARG("-spitest")) { spi_test = 1; } else
if (ARG("-ch")) { ARGL(gps_chans); } else
if (ARG("-y")) { ARGL(rx_yield); } else
if (ARG("-p0")) { ARGL(p0); printf("-p0 = %d\n", p0); } else
if (ARG("-p1")) { ARGL(p1); printf("-p1 = %d\n", p1); } else
if (ARG("-p2")) { ARGL(p2); printf("-p2 = %d\n", p2); } else
lprintf("unknown arg: \"%s\"\n", argv[ai]);
ai++;
while (ai < argc && ((argv[ai][0] != '+') && (argv[ai][0] != '-'))) ai++;
}
lprintf("KiwiSDR v%d.%d --------------------------------------------------------------------\n",
version_maj, version_min);
lprintf("compiled: %s %s on %s\n", __DATE__, __TIME__, COMPILE_HOST);
#if (defined(DEVSYS) && 0)
printf("%6s %6s %6s %6s\n", toUnits(1234), toUnits(999800, 1), toUnits(999800777, 2), toUnits(1800777666, 3));
printf("______ ______ ______ ______\n");
_exit(0);
#endif
#if (defined(DEVSYS) && 0)
for (int i = 0; i < 0xfffd; i++) kstr_wrap((char *) malloc(1));
printf("kstr_wrap() test WORKED\n");
_exit(0);
#endif
#if (defined(DEVSYS) && 0)
void ale_2g_test();
ale_2g_test();
_exit(0);
#endif
#if (defined(DEVSYS) && 0)
printf("kiwi_strnlen(\"sss\"(n),3): 0:%d 1:%d 2:%d 3:%d 4:%d 5:%d\n", kiwi_strnlen(NULL, 3), kiwi_strnlen("a", 3),
kiwi_strnlen("ab", 3), kiwi_strnlen("abc", 3), kiwi_strnlen("abcd", 3), kiwi_strnlen("abcde", 3));
_exit(0);
#endif
#if 0
for (int i = 0; i <= 255; i++) {
char *s;
asprintf(&s, " %c", i);
real_printf("%s", kiwi_str_clean(s, KCLEAN_DELETE));
free(s);
}
fflush(stdout);
_exit(0);
#endif
#ifdef TEST_IP_PARSE_STRICT
ip_test_parse_strict();
_exit(0);
#endif
#if (defined(DEVSYS))
printf("DEVSYS: nothing to do, exiting..\n");
_exit(0);
#endif
char *reply = read_file_string_reply("/etc/debian_version");
if (reply == NULL) panic("debian_version");
if (sscanf(kstr_sp(reply), "%d.%d", &debian_maj, &debian_min) != 2) panic("debian_version");
kstr_free(reply);
lprintf("/etc/debian_version %d.%d\n", debian_maj, debian_min);
debian_ver = debian_maj;
lprintf("Mongoose %s\n", MG_VERSION);
#if defined(USE_ASAN)
lprintf("### compiled with USE_ASAN\n");
#endif
assert(TRUE != FALSE);
assert(true != false);
assert(TRUE == true);
assert(FALSE == false);
assert(NOT_FOUND != TRUE);
assert(NOT_FOUND != FALSE);
debug_printfs |= kiwi_file_exists(DIR_CFG "/opt.debug")? 1:0;
TaskInit();
dump_init();
misc_init();
cfg_reload();
bool update_admcfg = false;
// on reboot let ntpd and other stuff settle first
kiwi.restart_delay = admcfg_default_int("restart_delay", RESTART_DELAY_30_SEC, &update_admcfg);
if (background_mode && !kiwi_file_exists("/tmp/.kiwi_no_restart_delay")) {
kiwi.restart_delay = CLAMP(kiwi.restart_delay, 0, RESTART_DELAY_MAX);
int delay;
switch (kiwi.restart_delay) {
case 0: delay = 0; break;
case 1: delay = 30; break;
case 2: delay = 45; break;
default: delay = (kiwi.restart_delay - 1) * 30; break;
}
system("touch /tmp/.kiwi_no_restart_delay"); // removed on reboot
if (delay != 0) {
lprintf("power on detected: delaying start %d secs...\n", delay);
sleep(delay);
}
}
clock_init();
if (fw_sel_override != FW_CONFIGURED) {
kiwi.firmware_sel = fw_sel_override;
} else {
kiwi.firmware_sel = admcfg_default_int("firmware_sel", FW_SEL_SDR_RX4_WF4, &update_admcfg);
}
kiwi.firmware_sel = CLAMP_TO(kiwi.firmware_sel, FW_OTHER, N_FW_MAX, 0);
if (wb_sel_override != -1) {
wb_sel = wb_sel_override;
} else {
wb_sel = admcfg_default_int("wb_sel", 0, &update_admcfg);
wb_sel = CLAMP_TO(wb_sel, 0, 6, 0);
const int wb_bw[] = { 72, 108, 144, 192, 204, 240, 300 };
wb_sel = wb_bw[wb_sel];
}
// upgrade mode to RX8_WF3 for public kiwi
if ((kiwi.firmware_sel == FW_SEL_SDR_RX4_WF4 || kiwi.firmware_sel == FW_SEL_SDR_RX8_WF2) && !admcfg_true("onetime_rx83_switch") && admcfg_true("kiwisdr_com_register")) {
kiwi.firmware_sel = FW_SEL_SDR_RX8_WF3_SHARE;
admcfg_update_int("firmware_sel", kiwi.firmware_sel, &update_admcfg);
lprintf("PUBLIC: switching operating mode to rx8.wf3 (see admin mode tab)\n");
admcfg_set_bool("onetime_rx83_switch", true);
printf(RED "switching operating mode to rx8.wf3" NONL);
}
if (kiwi.firmware_sel == FW_SEL_SDR_RX4_WF4) {
fpga_id = FPGA_ID_RX4_WF4;
rx_chans = 4;
wf_chans = 4;
snd_rate = SND_RATE_4CH;
snd_rate_i = SND_RATE_4CH_I;
rx_decim = RX_DECIM_4CH;
rx1_decim = RX1_STD_DECIM;
rx2_decim = RX2_STD_DECIM;
nrx_bufs = RXBUF_SIZE_44 / NRX_SPI;
} else
if (kiwi.firmware_sel == FW_SEL_SDR_RX8_WF2) {
fpga_id = FPGA_ID_RX8_WF2;
rx_chans = 8;
wf_chans = 2;
snd_rate = SND_RATE_8CH;
snd_rate_i = SND_RATE_8CH_I;
rx_decim = RX_DECIM_8CH;
rx1_decim = RX1_STD_DECIM;
rx2_decim = RX2_STD_DECIM;
nrx_bufs = RXBUF_SIZE_82 / NRX_SPI;
} else
if (kiwi.firmware_sel == FW_SEL_SDR_RX8_WF3_SHARE) {
fpga_id = FPGA_ID_RX8_WF3;
rx_chans = 8;
wf_chans = 3;
gps_chans = GPS_RX83_CHANS;
kiwi.wf_share = true;
snd_rate = SND_RATE_8CH;
snd_rate_i = SND_RATE_8CH_I;
rx_decim = RX_DECIM_8CH;
rx1_decim = RX1_STD_DECIM;
rx2_decim = RX2_STD_DECIM;
nrx_bufs = RXBUF_SIZE_83 / NRX_SPI;
} else
if (kiwi.firmware_sel == FW_SEL_SDR_RX3_WF3) {
fpga_id = FPGA_ID_RX3_WF3;
rx_chans = 3;
wf_chans = 3;
snd_rate = SND_RATE_3CH;
snd_rate_i = SND_RATE_3CH_I;
rx_decim = RX_DECIM_3CH;
rx1_decim = RX1_WIDE_DECIM;
rx2_decim = RX2_WIDE_DECIM;
nrx_bufs = RXBUF_SIZE_33 / NRX_SPI;
} else
if (kiwi.firmware_sel == FW_SEL_SDR_RX14_WF0) {
fpga_id = FPGA_ID_RX14_WF0;
rx_chans = 14;
wf_chans = 0;
gps_chans = GPS_RX14_CHANS;
snd_rate = SND_RATE_14CH;
snd_rate_i = SND_RATE_14CH_I;
rx_decim = RX_DECIM_14CH;
rx1_decim = RX1_STD_DECIM;
rx2_decim = RX2_STD_DECIM;
nrx_bufs = RXBUF_SIZE_14 / NRX_SPI;
} else
if (kiwi.firmware_sel == FW_SEL_SDR_WB) {
fpga_id = FPGA_ID_WB;
rx_chans = 1;
wf_chans = 1;
wb_chans = 1;
snd_rate = SND_RATE_WB;
snd_rate_i = SND_RATE_WB_I;
switch (wb_sel) {
case 72: rx1_decim = 926; rx2_decim = 6; break;
case 108: rx1_decim = 617; rx2_decim = 9; break;
case 144: rx1_decim = 463; rx2_decim = 12; break;
case 192: rx1_decim = 347; rx2_decim = 16; break;
case 204: rx1_decim = 327; rx2_decim = 17; break;
case 240: rx1_decim = 278; rx2_decim = 20; break;
case 300: rx1_decim = 222; rx2_decim = 25; break;
default: printf("wb_sel=%d\n", wb_sel); panic("bad wb_sel"); break;
}
v_wb_buf_chans = rx2_decim;
rx_decim = rx1_decim * rx2_decim;
wb_rate = SND_RATE_WB * rx2_decim;
nrx_bufs = RXBUF_SIZE_WB / NRX_SPI;
kiwi.isWB = true;
} else {
fpga_id = FPGA_ID_OTHER;
lprintf("firmware: OTHER\n");
}
// rx_chans
// | rx_wb_buf_chans (USE_WB uses this many equivalent buffer channels)
// | | wb_chans
// | | |
// 44 4 4 0
// 33 3 3 0
// 82 8 8 0
// 83 8
// 14 14 14 0
// wb 1 6 1 72k
// wb 1 16 1 192k
// wb 1 17 1 204k
// wb 1 20 1 240k
// wb 1 25 1 300k
rx_wb_buf_chans = kiwi.isWB? v_wb_buf_chans : rx_chans;
if (fpga_id == FPGA_ID_OTHER) {
kiwi.mode_id = strdup((char *) "other");
} else {
if (kiwi.isWB) {
asprintf(&kiwi.mode_id, "wb.%dk", wb_sel);
lprintf("firmware: wb rate %dk\n", wb_rate/1000);
} else {
asprintf(&kiwi.mode_id, "rx%d.wf%d%s", rx_chans, wf_chans, fw_test? ".test" : "");
lprintf("firmware: %s\n", kiwi.mode_id);
}
cfg_no_wf = cfg_true("no_wf");
lprintf("firmware: rx_chans=%d rx_wb_buf_chans=%d wb_chans=%d wf_chans=%d gps_chans=%d\n",
rx_chans, rx_wb_buf_chans, wb_chans, wf_chans, gps_chans);
nrx_samps = NRX_SAMPS_CHANS(rx_wb_buf_chans);
int nrx_samps_max;
nrx_samps_max = nrx_samps;
snd_intr_usec = 1e6 / ((float) snd_rate/nrx_samps);
lprintf("firmware: RX rx_decim=%d RX1_DECIM=%d RX2_DECIM=%d USE_RX_CICF=%d\n",
rx_decim, rx1_decim, rx2_decim, VAL_USE_RX_CICF);
lprintf("firmware: RX rx_srate=%.3f(%d,%d) wb_srate=%d bufs=%d nsamps=%d nsamps_max=%d intr_usec=%d\n",
ext_update_get_sample_rateHz(ADC_CLK_SYS), snd_rate, snd_rate_i, wb_rate, nrx_bufs, nrx_samps, nrx_samps_max, snd_intr_usec);
check(wf_chans <= MAX_WF_DDC);
check(wb_chans <= MAX_WB_CHANS);
// NB: For wideband the value used by rx_audio_mem_wb.v must be (nrx_samps * v_wb_buf_chans),
// not simply nrx_samps, because the verilog loop counts each DDC avail separately.
// This value is transmitted via spi_set(CmdSetRXNsamps, nrx_samps_wb) in the data_pump.
nrx_samps_wb = kiwi.isWB? (nrx_samps * v_wb_buf_chans) : nrx_samps;
check(nrx_bufs <= MAX_NRX_BUFS);
check(nrx_samps_max <= MAX_NRX_SAMPS);
check(nrx_samps_max < FASTFIR_OUTBUF_SIZE); // see data_pump.h
check(nrx_samps_wb < MAX_WB_SAMPS); // see data_pump.h
if (kiwi.wf_share) {
nwf_nxfer = (WF_NFFT * NIQ / SPIBUF_W);
nwf_samps = ((WF_NFFT-1) / nwf_nxfer);
nwf_tsamps = nwf_nxfer * nwf_samps;
lprintf("firmware: WF nfft=%d xfer=%d samps=%d tsamps=%d\n", WF_NFFT, nwf_nxfer, nwf_samps, nwf_tsamps);
nwf_nxferL = (WF_NFFT_MAX * NIQ / SPIBUF_W);
nwf_sampsL = ((WF_NFFT_MAX-1) / nwf_nxferL);
} else {
nwf_nxferL = (WF_NFFT_MAX * NIQ / SPIBUF_W) + 1;
nwf_sampsL = (WF_NFFT_MAX / nwf_nxferL) + 1;
}
nwf_tsampsL = nwf_nxferL * nwf_sampsL;
lprintf("firmware: WF nfft=%d xfer=%d samps=%d tsamps=%d\n", WF_NFFT_MAX, nwf_nxferL, nwf_sampsL, nwf_tsampsL);
monitors_max = (rx_chans * N_CAMP) + N_QUEUERS;
}
TaskInitCfg();
// force enable_gps true because there is no longer an option switch in the admin interface (now uses acquisition checkboxes)
do_gps = admcfg_default_bool("enable_gps", true, &update_admcfg);
if (!do_gps) {
admcfg_set_bool("enable_gps", true);
do_gps = 1;
}
if (update_admcfg) admcfg_save(); // during init doesn't conflict with admin cfg
if (p_gps != 0) do_gps = (p_gps == 1)? 1:0;
if (down) do_sdr = do_gps = 0;
need_hardware = (do_gps || do_sdr);
// called early, in case another server already running so we can detect the busy socket and bail
web_server_init(WS_INIT_CREATE);
if (need_hardware) {
peri_init();
if (gpio_test_pin) gpio_test(gpio_test_pin);
if (fpga_init(fpga_id_check, fpga_sim_fail)) {
kiwi.hw = true;
eeprom_update(eeprom_action);
kiwi.ext_clk = cfg_true("ext_ADC_clk");
ctrl_clr_set(0xffff, CTRL_EEPROM_WP);
net.dna = fpga_dna();
printf("device DNA %08x|%08x\n", PRINTF_U64_ARG(net.dna));
} else {
lprintf("*** NO KIWI HARDWARE DETECTED\n");
}
}
rx_server_init();
#ifdef USE_SDR
extint_setup();
#endif
web_server_init(WS_INIT_START);
if (kiwi.hw) {
// need to do gps clock switch even if gps is not enabled
gps_fe_init();
printf("switching GPS clock..\n");
kiwi_msleep(100);
ctrl_clr_set(0, CTRL_GPS_CLK_EN);
kiwi_msleep(100);
// switch to ext clock only after GPS clock switch occurs
if (kiwi.ext_clk) {
printf("switching to external ADC clock..\n");
ctrl_clr_set(0, CTRL_OSC_DIS);
kiwi_msleep(100);
}
rf_attn_init();
#ifdef USE_GPS
gps_main(argc, argv, do_gps);
#endif
}
CreateTask(stat_task, NULL, MAIN_PRIORITY);
#ifdef USE_OTHER
if (kiwi.firmware_sel == FW_OTHER) {
CreateTask(other_task, NULL, MAIN_PRIORITY);
}
#endif
// run periodic housekeeping functions
while (TRUE) {
TaskCollect();
TaskCheckStacks(false);
TaskSleepReasonSec("main loop", 10);
if (drop_root) {
static u4_t dropped;
if (dropped == drop_root) drop_root_privileges();
dropped++;
}
if (_kiwi_restart) kiwi_exit(0);
}
}