forked from hugen79/NanoVNA-H
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2320 lines (2078 loc) · 61.1 KB
/
main.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
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
/*
* 2016-2019 Copyright @edy555, licensed under GPL. https://github.com/ttrftech/NanoVNA
* All rights reserved.
*
* This 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 3, or (at your option)
* any later version.
*
* The software 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "ch.h"
#include "hal.h"
#include "usbcfg.h"
#include "si5351.h"
#include "nanovna.h"
#include "fft.h"
#include <chprintf.h>
#include <shell.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define START_MIN 10000
#define STOP_MAX 1500000000
static void apply_error_term_at(int i);
static void apply_edelay_at(int i);
static void cal_interpolate(int s);
static void update_frequencies(void);
static void set_frequencies(uint32_t start, uint32_t stop, int16_t points);
static bool sweep(bool break_on_operation);
mutex_t mutex_sweep;
mutex_t mutex_ili9341;
#define DRIVE_STRENGTH_AUTO (-1)
#define FREQ_HARMONICS (config.harmonic_freq_threshold)
#define IS_HARMONIC_MODE(f) ((f) > FREQ_HARMONICS)
static int32_t frequency_offset = 5000;
static uint32_t frequency = 10000000;
static int8_t drive_strength = SI5351_CLK_DRIVE_STRENGTH_8MA;
int8_t sweep_enabled = TRUE;
static int8_t sweep_once = FALSE;
static int8_t cal_auto_interpolate = TRUE;
uint16_t redraw_request = 0; // contains REDRAW_XXX flags
int16_t vbat = 0;
bool pll_lock_failed;
static THD_WORKING_AREA(waThread1, 640);
static THD_FUNCTION(Thread1, arg)
{
chRegSetThreadName("sweep");
while (1) {
bool completed = false;
if (sweep_enabled || sweep_once) {
chMtxLock(&mutex_sweep);
palClearPad(GPIOC, GPIOC_LED); // disable led and wait for voltage stabilization
chThdSleepMilliseconds(10);
completed = sweep(true);
sweep_once = FALSE;
// enable led
palSetPad(GPIOC, GPIOC_LED);
chMtxUnlock(&mutex_sweep);
} else {
__WFI();
}
chMtxLock(&mutex_sweep);
ui_process();
if (sweep_enabled) {
adc_stop(ADC1);
vbat = adc_vbat_read(ADC1);
touch_start_watchdog();
draw_battery_status();
// if (pll_lock_failed) {
// draw_pll_lock_error();
// }
/* calculate trace coordinates and plot only if scan completed */
if (completed) {
plot_into_index(measured);
redraw_request |= REDRAW_CELLS;
}
}
/* plot trace and other indications as raster */
draw_all(completed); // flush markmap only if scan completed to prevent remaining traces
chMtxUnlock(&mutex_sweep);
}
}
static void pause_sweep(void)
{
sweep_enabled = FALSE;
}
static void resume_sweep(void)
{
sweep_enabled = TRUE;
}
void toggle_sweep(void)
{
sweep_enabled = !sweep_enabled;
}
static float bessel0(float x) {
const float eps = 0.0001;
float ret = 0;
float term = 1;
float m = 0;
while (term > eps * ret) {
ret += term;
++m;
term *= (x*x) / (4*m*m);
}
return ret;
}
static float kaiser_window(float k, float n, float beta) {
if (beta == 0.0) return 1.0;
float r = (2 * k) / (n - 1) - 1;
return bessel0(beta * sqrt(1 - r * r)) / bessel0(beta);
}
static void transform_domain(void)
{
if ((domain_mode & DOMAIN_MODE) != DOMAIN_TIME) return; // nothing to do for freq domain
chMtxLock(&mutex_ili9341); // [protect spi_buffer]
// use spi_buffer as temporary buffer
// and calculate ifft for time domain
float* tmp = (float*)spi_buffer;
uint8_t window_size = POINT_COUNT, offset = 0;
uint8_t is_lowpass = FALSE;
switch (domain_mode & TD_FUNC) {
case TD_FUNC_BANDPASS:
offset = 0;
window_size = POINT_COUNT;
break;
case TD_FUNC_LOWPASS_IMPULSE:
case TD_FUNC_LOWPASS_STEP:
is_lowpass = TRUE;
offset = POINT_COUNT;
window_size = POINT_COUNT * 2;
break;
}
float beta = 0.0;
switch (domain_mode & TD_WINDOW) {
case TD_WINDOW_MINIMUM:
beta = 0.0; // this is rectangular
break;
case TD_WINDOW_NORMAL:
beta = 6.0;
break;
case TD_WINDOW_MAXIMUM:
beta = 13;
break;
}
for (int ch = 0; ch < 2; ch++) {
memcpy(tmp, measured[ch], sizeof(measured[0]));
for (int i = 0; i < POINT_COUNT; i++) {
float w = kaiser_window(i+offset, window_size, beta);
tmp[i*2+0] *= w;
tmp[i*2+1] *= w;
}
#if POINT_COUNT >= FFT_SIZE
#error CHECK ME
#endif
for (int i = POINT_COUNT; i < FFT_SIZE; i++) {
tmp[i*2+0] = 0.0;
tmp[i*2+1] = 0.0;
}
if (is_lowpass) {
for (int i = 1; i < POINT_COUNT; i++) {
tmp[(FFT_SIZE-i)*2+0] = tmp[i*2+0];
tmp[(FFT_SIZE-i)*2+1] = -tmp[i*2+1];
}
}
fft256_inverse((float(*)[2])tmp);
memcpy(measured[ch], tmp, sizeof(measured[0]));
for (int i = 0; i < POINT_COUNT; i++) {
measured[ch][i][0] /= (float)FFT_SIZE;
if (is_lowpass) {
measured[ch][i][1] = 0.0;
} else {
measured[ch][i][1] /= (float)FFT_SIZE;
}
}
if ( (domain_mode & TD_FUNC) == TD_FUNC_LOWPASS_STEP ) {
for (int i = 1; i < POINT_COUNT; i++) {
measured[ch][i][0] += measured[ch][i-1][0];
}
}
}
chMtxUnlock(&mutex_ili9341); // [/protect spi_buffer]
}
static void cmd_pause(BaseSequentialStream *chp, int argc, char *argv[])
{
(void)chp;
(void)argc;
(void)argv;
pause_sweep();
}
static void cmd_resume(BaseSequentialStream *chp, int argc, char *argv[])
{
(void)chp;
(void)argc;
(void)argv;
// restore frequencies array and cal
chMtxLock(&mutex_sweep);
update_frequencies();
if (cal_auto_interpolate && (cal_status & CALSTAT_APPLY))
cal_interpolate(lastsaveid);
chMtxUnlock(&mutex_sweep);
resume_sweep();
}
static void cmd_reset(BaseSequentialStream *chp, int argc, char *argv[])
{
(void)argc;
(void)argv;
if (argc == 1) {
if (strcmp(argv[0], "dfu") == 0) {
chprintf(chp, "Performing reset to DFU mode\r\n");
enter_dfu();
return;
}
}
chprintf(chp, "Performing reset\r\n");
rccEnableWWDG(FALSE);
WWDG->CFR = 0x60;
WWDG->CR = 0xff;
/* wait forever */
while (1)
;
}
# if 0
static const int8_t gain_table[][2] = {
{ 0, 0 }, // 1st: 0 ~ 300MHz
{ 42, 40 }, // 2nd: 300 ~ 600MHz
{ 52, 50 }, // 3rd: 600 ~ 900MHz
{ 80, 78 }, // 4th: 900 ~ 1200MHz
{ 90, 88 }, // 5th: 1200 ~ 1400MHz
{ 95, 93 }, // 6th: 1400MHz ~
};
# endif
//NanoVNA-H REV3.4
static const int8_t gain_table[][2] = {
{ 0, 0 }, // 1st: 0 ~ 300MHz
{ 50, 50 }, // 2nd: 300 ~ 600MHz
{ 55, 55 }, // 3rd: 600 ~ 900MHz
{ 75, 75 }, // 4th: 900 ~ 1200MHz
{ 80, 80 }, // 5th: 1200 ~ 1500MHz
// { 90, 90 }, // 6th: 1500MHz ~1800MHz
// { 95, 95 }, // 7th: 1800MHz ~
};
static int adjust_gain(int newfreq)
{
int delay = 0;
int new_order = (newfreq-1) / FREQ_HARMONICS; //Harmonics are switched after an integer multiple, and then the gain needs to be switched after an integer multiple.
int old_order = (frequency-1) / FREQ_HARMONICS;
if (new_order != old_order) {
tlv320aic3204_set_gain(gain_table[new_order][0], gain_table[new_order][1]);
delay += 10;
}
return delay;
}
static int set_frequency(uint32_t freq)
{
int delay = 0;
if (frequency == freq)
return delay;
delay += adjust_gain(freq);
int8_t ds = drive_strength;
if (ds == DRIVE_STRENGTH_AUTO) {
ds = freq > FREQ_HARMONICS ? SI5351_CLK_DRIVE_STRENGTH_8MA : SI5351_CLK_DRIVE_STRENGTH_2MA;
}
delay += si5351_set_frequency_with_offset(freq, frequency_offset, ds);
frequency = freq;
return delay;
}
static void cmd_offset(BaseSequentialStream *chp, int argc, char *argv[])
{
if (argc != 1) {
chprintf(chp, "usage: offset {frequency offset(Hz)}\r\n");
return;
}
chMtxLock(&mutex_sweep);
frequency_offset = atoi(argv[0]);
set_frequency(frequency);
chMtxUnlock(&mutex_sweep);
}
static void cmd_freq(BaseSequentialStream *chp, int argc, char *argv[])
{
int freq;
if (argc != 1) {
chprintf(chp, "usage: freq {frequency(Hz)}\r\n");
return;
}
pause_sweep();
chMtxLock(&mutex_sweep);
freq = atoi(argv[0]);
set_frequency(freq);
chMtxUnlock(&mutex_sweep);
}
static void cmd_power(BaseSequentialStream *chp, int argc, char *argv[])
{
if (argc != 1) {
chprintf(chp, "usage: power {0-3|-1}\r\n");
return;
}
drive_strength = atoi(argv[0]);
chMtxLock(&mutex_sweep);
set_frequency(frequency);
chMtxUnlock(&mutex_sweep);
}
#ifdef __CMD_TIME__
static void cmd_time(BaseSequentialStream *chp, int argc, char *argv[])
{
RTCDateTime timespec;
(void)argc;
(void)argv;
rtcGetTime(&RTCD1, ×pec);
chprintf(chp, "%d/%d/%d %d\r\n", timespec.year+1980, timespec.month, timespec.day, timespec.millisecond);
}
#endif
#ifdef __DAC__
static void cmd_dac(BaseSequentialStream *chp, int argc, char *argv[])
{
int value;
if (argc != 1) {
chprintf(chp, "usage: dac {value(0-4095)}\r\n");
chprintf(chp, "current value: %d\r\n", config.dac_value);
return;
}
value = atoi(argv[0]);
config.dac_value = value;
dacPutChannelX(&DACD2, 0, value);
}
#endif
static void cmd_threshold(BaseSequentialStream *chp, int argc, char *argv[])
{
int value;
if (argc != 1) {
chprintf(chp, "usage: threshold {frequency in harmonic mode}\r\n");
chprintf(chp, "current: %d\r\n", config.harmonic_freq_threshold);
return;
}
value = atoi(argv[0]);
chMtxLock(&mutex_sweep);
config.harmonic_freq_threshold = value;
chMtxUnlock(&mutex_sweep);
}
static void cmd_saveconfig(BaseSequentialStream *chp, int argc, char *argv[])
{
(void)argc;
(void)argv;
config_save();
chprintf(chp, "Config saved.\r\n");
}
static void cmd_clearconfig(BaseSequentialStream *chp, int argc, char *argv[])
{
if (argc != 1) {
chprintf(chp, "usage: clearconfig {protection key}\r\n");
return;
}
if (strcmp(argv[0], "1234") != 0) {
chprintf(chp, "Key unmatched.\r\n");
return;
}
chMtxLock(&mutex_sweep); // TODO: separate mutex?
clear_all_config_prop_data();
chMtxUnlock(&mutex_sweep);
chprintf(chp, "Config and all cal data cleared.\r\n");
// chprintf(chp, "WARNING: Do reset manually to take effect.\r\n");
}
static struct {
int16_t rms[2];
int16_t ave[2];
int callback_count;
// int32_t last_counter_value;
// int32_t interval_cycles;
// int32_t busy_cycles;
} stat;
static int16_t rx_buffer[AUDIO_BUFFER_LEN * 2];
#ifdef __DUMP_CMD__
int16_t dump_buffer[AUDIO_BUFFER_LEN];
int16_t dump_selection = 0;
#endif
static volatile int16_t wait_count = 0;
float measured[2][POINT_COUNT][2];
static void wait_dsp(int count)
{
wait_count = count;
//reset_dsp_accumerator();
while (wait_count)
__WFI();
}
#ifdef __DUMP_CMD__
static void duplicate_buffer_to_dump(int16_t *p)
{
if (dump_selection == 1)
p = samp_buf;
else if (dump_selection == 2)
p = ref_buf;
memcpy(dump_buffer, p, sizeof dump_buffer);
}
#endif
static void i2s_end_callback(I2SDriver *i2sp, size_t offset, size_t n)
{
#if PORT_SUPPORTS_RT
int32_t cnt_s = port_rt_get_counter_value();
int32_t cnt_e;
#endif
int16_t *p = &rx_buffer[offset];
(void)i2sp;
(void)n;
if (wait_count > 0) {
if (wait_count == 1)
dsp_process(p, n);
#ifdef __DUMP_CMD__
duplicate_buffer_to_dump(p);
#endif
--wait_count;
}
#if PORT_SUPPORTS_RT
cnt_e = port_rt_get_counter_value();
stat.interval_cycles = cnt_s - stat.last_counter_value;
stat.busy_cycles = cnt_e - cnt_s;
stat.last_counter_value = cnt_s;
#endif
stat.callback_count++;
}
static const I2SConfig i2sconfig = {
.tx_buffer = NULL, // TX Buffer
.rx_buffer = rx_buffer, // RX Buffer
.size = AUDIO_BUFFER_LEN * 2,
.tx_end_cb = NULL, // tx callback
.rx_end_cb = i2s_end_callback, // rx callback
.i2scfgr = 0, // i2scfgr
.i2spr = 2 // i2spr
};
static void cmd_data(BaseSequentialStream *chp, int argc, char *argv[])
{
int sel = 0;
if (argc == 1)
sel = atoi(argv[0]);
if (sel < 0 || sel > 6) {
chprintf(chp, "usage: data [array]\r\n");
} else {
if (sel > 1)
sel = sel-2;
chMtxLock(&mutex_sweep);
for (int i = 0; i < sweep_points; i++) {
#ifndef __USE_STDIO__
// WARNING: chprintf doesn't support proper float formatting
chprintf(chp, "%f %f\r\n", measured[sel][i][0], measured[sel][i][1]);
#else
// printf floating point losslessly: float="%.9g", double="%.17g"
char tmpbuf[20];
int leng;
leng = snprintf(tmpbuf, sizeof(tmpbuf), "%.9g", measured[sel][i][0]);
for (int j=0; j < leng; j++) {
streamPut(chp, (uint8_t)tmpbuf[j]);
}
streamPut(chp, (uint8_t)' ');
leng = snprintf(tmpbuf, sizeof(tmpbuf), "%.9g", measured[sel][i][1]);
for (int j=0; j < leng; j++) {
streamPut(chp, (uint8_t)tmpbuf[j]);
}
streamPut(chp, (uint8_t)'\r');
streamPut(chp, (uint8_t)'\n');
#endif // __USE_STDIO__
}
chMtxUnlock(&mutex_sweep);
}
}
#ifdef __DUMP_CMD__
static void cmd_dump(BaseSequentialStream *chp, int argc, char *argv[])
{
int i, j;
int len;
if (argc == 1)
dump_selection = atoi(argv[0]);
wait_dsp(3);
len = AUDIO_BUFFER_LEN;
if (dump_selection == 1 || dump_selection == 2)
len /= 2;
for (i = 0; i < len; ) {
for (j = 0; j < 16; j++, i++) {
chprintf(chp, "%04x ", 0xffff & (int)dump_buffer[i]);
}
chprintf(chp, "\r\n");
}
}
#endif
static void cmd_capture(BaseSequentialStream *chp, int argc, char *argv[])
{
// read pixel count at one time (PART*2 bytes required for read buffer)
#define PART 960
chMtxLock(&mutex_ili9341); // [capture display + spi_buffer]
// use uint16_t spi_buffer[1024] (defined in ili9341) for read buffer
uint16_t *buf = &spi_buffer[0];
int len = 320 * 240;
int i;
ili9341_read_memory(0, 0, 320, 240, PART, buf);
for (i = 0; i < PART; i++) {
streamPut(chp, buf[i] >> 8);
streamPut(chp, buf[i] & 0xff);
}
len -= PART;
while (len > 0) {
ili9341_read_memory_continue(PART, buf);
for (i = 0; i < PART; i++) {
streamPut(chp, buf[i] >> 8);
streamPut(chp, buf[i] & 0xff);
}
len -= PART;
}
chMtxUnlock(&mutex_ili9341); // [/capture display + spi_buffer]
}
//static void cmd_gamma(BaseSequentialStream *chp, int argc, char *argv[])
//{
// float gamma[2];
// (void)argc;
// (void)argv;
//
// pause_sweep();
// chMtxLock(&mutex_sweep);
// wait_dsp(4);
// calculate_gamma(gamma);
// chMtxUnlock(&mutex_sweep);
//
// chprintf(chp, "%d %d\r\n", gamma[0], gamma[1]);
//}
static void (*sample_func)(float *gamma) = calculate_gamma;
static void cmd_sample(BaseSequentialStream *chp, int argc, char *argv[])
{
if (argc == 1) {
if (strcmp(argv[0], "ref") == 0) {
sample_func = fetch_amplitude_ref;
return;
} else if (strcmp(argv[0], "ampl") == 0) {
sample_func = fetch_amplitude;
return;
} else if (strcmp(argv[0], "gamma") == 0) {
sample_func = calculate_gamma;
return;
}
}
chprintf(chp, "usage: sample {gamma|ampl|ref}\r\n");
}
#if 0
int32_t frequency0 = 1000000;
int32_t frequency1 = 300000000;
int16_t sweep_points = POINT_COUNT;
uint32_t frequencies[POINT_COUNT];
uint16_t cal_status;
float cal_data[5][POINT_COUNT][2];
#endif
config_t config = {
.magic = CONFIG_MAGIC,
#ifdef __DAC__
.dac_value = 1922,
#endif
.grid_color = 0x1084,
.menu_normal_color = 0xffff,
.menu_active_color = 0x7777,
.trace_color = { RGBHEX(0xffe31f), RGBHEX(0x00bfe7), RGBHEX(0x1fe300), RGBHEX(0xe7079f) },
.touch_cal = { 370, 540, 154, 191 }, //{ 620, 600, 160, 190 },
.default_loadcal = 0,
.harmonic_freq_threshold = 300000000,
.vbat_offset = 480,
.checksum = 0
};
properties_t current_props = {
.magic = CONFIG_MAGIC,
._frequency0 = 50000, // start = 50kHz
._frequency1 = 900000000, // end = 900MHz
._sweep_points = POINT_COUNT,
._cal_status = 0,
//._frequencies = {},
//._cal_data = {},
._electrical_delay = 0,
._trace = /*[4] */
{/*enable, type, channel, polar, scale, refpos*/
{ 1, TRC_LOGMAG, 0, 0, 1.0, 7.0 },
{ 1, TRC_LOGMAG, 1, 0, 1.0, 7.0 },
{ 1, TRC_SMITH, 0, 1, 1.0, 0.0 },
{ 1, TRC_PHASE, 1, 0, 1.0, 4.0 }
},
._markers = /*[4] */ {
{ 1, 30, 0 }, { 0, 40, 0 }, { 0, 60, 0 }, { 0, 80, 0 }
},
._active_marker = 0,
._domain_mode = 0,
._velocity_factor = 70,
.checksum = 0
};
properties_t *active_props = ¤t_props;
static void ensure_edit_config(void)
{
if (active_props == ¤t_props)
return;
//memcpy(¤t_props, active_props, sizeof(config_t));
active_props = ¤t_props;
// move to uncal state
cal_status = 0;
}
// main loop for measurement
static bool sweep(bool break_on_operation)
{
pll_lock_failed = false;
for (int i = 0; i < sweep_points; i++) {
int delay = set_frequency(frequencies[i]);
delay = delay < 3 ? 3 : delay;
delay = delay > 8 ? 8 : delay;
tlv320aic3204_select(0); // CH0:REFLECT
wait_dsp(delay);
/* calculate reflection coeficient */
(*sample_func)(measured[0][i]);
tlv320aic3204_select(1); // CH1:TRANSMISSION
wait_dsp(delay);
/* calculate transmission coeficient */
(*sample_func)(measured[1][i]);
if (cal_status & CALSTAT_APPLY)
apply_error_term_at(i);
if (electrical_delay != 0)
apply_edelay_at(i);
// back to toplevel to handle ui operation
if (operation_requested && break_on_operation)
return false;
}
transform_domain();
return true;
}
#ifdef __SCANRAW_CMD__
static void measure_gamma_avg(uint8_t channel, uint32_t freq, uint16_t avg_count, float* gamma) {
int delay = set_frequency(freq);
delay = delay < 3 ? 3 : delay;
delay = delay > 8 ? 8 : delay;
tlv320aic3204_select(channel);
wait_dsp(delay);
gamma[0] = 0.0;
gamma[1] = 0.0;
float gamma_acc[2] = { 0, 0 };
for (int j = 0; j < avg_count; j++) {
wait_dsp(1);
/* calculate reflection/transmission coeficient */
(*sample_func)(gamma);
if (avg_count == 1) break;
gamma_acc[0] += gamma[0];
gamma_acc[1] += gamma[1];
}
if (avg_count > 1) {
gamma[0] = gamma_acc[0] / avg_count;
gamma[1] = gamma_acc[1] / avg_count;
}
}
static void cmd_scanraw(BaseSequentialStream *chp, int argc, char *argv[])
{
int32_t chan, freq, step, count, avg_count;
if (argc != 4 && argc != 5) {
chprintf(chp, "usage: scanraw {channel(0|1)} {start(Hz)} {stEp(Hz)} {count} [average]\r\n");
return;
}
chan = atoi(argv[0]);
freq = atoi(argv[1]);
step = atoi(argv[2]);
count = atoi(argv[3]);
avg_count = 1;
if (argc == 5)
avg_count = atoi(argv[4]);
if (chan < 0 || chan > 1) {
chprintf(chp, "error: invalid channel\r\n");
return;
}
if (freq < START_MIN ||
(freq+(uint64_t)step*count) < START_MIN ||
(freq+(uint64_t)step*count) > STOP_MAX) {
chprintf(chp, "error: invalid frequency range\r\n");
return;
}
if (avg_count < 1 || avg_count > 1000) {
chprintf(chp, "error: invalid average\r\n");
return;
}
chMtxLock(&mutex_sweep);
palClearPad(GPIOC, GPIOC_LED); // disable led and wait for voltage stabilization
chThdSleepMilliseconds(10);
for (int i = 0; i < count; i++, freq += step) {
float gamma[2];
measure_gamma_avg(chan, freq, avg_count, gamma);
#ifndef __USE_STDIO__
// WARNING: chprintf doesn't support proper float formatting
chprintf(chp, "%f\t%f\r\n", gamma[0], gamma[1]);
#else
// printf floating point losslessly: float="%.9g", double="%.17g"
char tmpbuf[20];
int leng;
leng = snprintf(tmpbuf, sizeof(tmpbuf), "%.9g", gamma[0]);
for (int j=0; j < leng; j++) {
streamPut(chp, (uint8_t)tmpbuf[j]);
}
streamPut(chp, (uint8_t)'\t');
leng = snprintf(tmpbuf, sizeof(tmpbuf), "%.9g", gamma[1]);
for (int j=0; j < leng; j++) {
streamPut(chp, (uint8_t)tmpbuf[j]);
}
streamPut(chp, (uint8_t)'\r');
streamPut(chp, (uint8_t)'\n');
#endif // __USE_STDIO__
}
chMtxUnlock(&mutex_sweep);
}
#endif //__SCANRAW_CMD__
static void cmd_scan(BaseSequentialStream *chp, int argc, char *argv[])
{
int32_t start, stop;
int16_t points = sweep_points;
if (argc != 2 && argc != 3) {
chprintf(chp, "usage: scan {start(Hz)} {stop(Hz)} [points]\r\n");
return;
}
start = atoi(argv[0]);
stop = atoi(argv[1]);
if (start == 0 || stop == 0 || start > stop) {
chprintf(chp, "frequency range is invalid\r\n");
return;
}
if (argc == 3) {
points = atoi(argv[2]);
if (points <= 0 || points > sweep_points) {
chprintf(chp, "sweep points exceeds range\r\n");
return;
}
}
pause_sweep();
chMtxLock(&mutex_sweep);
set_frequencies(start, stop, points);
if (cal_auto_interpolate && (cal_status & CALSTAT_APPLY))
cal_interpolate(lastsaveid);
sweep_once = TRUE;
chMtxUnlock(&mutex_sweep);
// wait finishing sweep
while (sweep_once)
chThdSleepMilliseconds(10);
}
static void update_marker_index(void)
{
int m;
int i;
for (m = 0; m < MARKER_COUNT; m++) {
if (!markers[m].enabled)
continue;
uint32_t f = markers[m].frequency;
if (f < frequencies[0]) {
markers[m].index = 0;
markers[m].frequency = frequencies[0];
} else if (f >= frequencies[sweep_points-1]) {
markers[m].index = sweep_points-1;
markers[m].frequency = frequencies[sweep_points-1];
} else {
for (i = 0; i < sweep_points-1; i++) {
if (frequencies[i] <= f && f < frequencies[i+1]) {
uint32_t mid = (frequencies[i] + frequencies[i+1])/2;
if (f < mid) {
markers[m].index = i;
} else {
markers[m].index = i + 1;
}
break;
}
}
}
}
}
static void set_frequencies(uint32_t start, uint32_t stop, int16_t points)
{
chMtxLock(&mutex_sweep);
uint32_t i;
uint32_t span = stop - start;
for (i = 0; i < points; i++) {
uint32_t offset = (uint32_t)((i * (uint64_t)span) / (points - 1));
frequencies[i] = start + (uint32_t)offset;
}
// disable at out of sweep range
for (; i < sweep_points; i++)
frequencies[i] = 0;
chMtxUnlock(&mutex_sweep);
}
static void update_frequencies(void)
{
chMtxLock(&mutex_sweep);
uint32_t start, stop;
if (frequency1 > 0) {
start = frequency0;
stop = frequency1;
} else {
int32_t center = frequency0;
int32_t span = -frequency1;
start = center - span/2;
stop = center + span/2;
}
set_frequencies(start, stop, sweep_points);
operation_requested = OP_FREQCHANGE;
update_marker_index();
// set grid layout
update_grid();
chMtxUnlock(&mutex_sweep);
}
static void freq_mode_startstop(void)
{
if (frequency1 <= 0) {
int center = frequency0;
int span = -frequency1;
ensure_edit_config();
frequency0 = center - span/2;
frequency1 = center + span/2;
}
}
static void freq_mode_centerspan(void)
{
if (frequency1 > 0) {
int start = frequency0;
int stop = frequency1;
ensure_edit_config();
frequency0 = (start + stop)/2; // center
frequency1 = -(stop - start); // span
}
}
void set_sweep_frequency(int type, int32_t freq)
{
chMtxLock(&mutex_sweep);
int32_t center;
int32_t span;
int cal_applied = cal_status & CALSTAT_APPLY;
switch (type) {
case ST_START:
ensure_edit_config();
freq_mode_startstop();
if (freq < START_MIN)
freq = START_MIN;
if (freq > STOP_MAX)
freq = STOP_MAX;
frequency0 = freq;
// if start > stop then make start = stop
if (frequency1 < freq)
frequency1 = freq;
update_frequencies();
break;
case ST_STOP:
ensure_edit_config();
freq_mode_startstop();
if (freq > STOP_MAX)
freq = STOP_MAX;
if (freq < START_MIN)
freq = START_MIN;
frequency1 = freq;
// if start > stop then make start = stop
if (frequency0 > freq)
frequency0 = freq;
update_frequencies();
break;
case ST_CENTER:
ensure_edit_config();
freq_mode_centerspan();
if (freq > STOP_MAX)
freq = STOP_MAX;
if (freq < START_MIN)
freq = START_MIN;
frequency0 = freq;
center = frequency0;
span = -frequency1;
if (center-span/2 < START_MIN) {
span = (center - START_MIN) * 2;
frequency1 = -span;
}
if (center+span/2 > STOP_MAX) {
span = (STOP_MAX - center) * 2;
frequency1 = -span;
}
update_frequencies();
break;