forked from ttrftech/NanoVNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
2160 lines (1914 loc) · 52.7 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
/*
* Copyright (c) 2016-2017, TAKAHASHI Tomohiro (TTRFTECH) [email protected]
* 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"
#if defined(NANOVNA_F303)
#include "adc_F303.h"
#endif
#include <chprintf.h>
#include <shell.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <arm_math.h>
//#define ENABLED_DUMP
static void apply_error_term_at(int i);
static void apply_edelay_at(int i);
static void cal_interpolate(int s);
void update_frequencies(void);
void set_frequencies(uint32_t start, uint32_t stop, int16_t points);
bool sweep(bool break_on_operation);
static MUTEX_DECL(mutex);
#define DRIVE_STRENGTH_AUTO (-1)
#define FREQ_HARMONICS (config.harmonic_freq_threshold)
#define IS_HARMONIC_MODE(f) ((f) > FREQ_HARMONICS)
int32_t frequency_offset = 5000;
uint32_t frequency = 10000000;
int8_t drive_strength = DRIVE_STRENGTH_AUTO;
int8_t sweep_enabled = TRUE;
int8_t sweep_once = FALSE;
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)
{
(void)arg;
chRegSetThreadName("sweep");
while (1) {
bool completed = false;
if (sweep_enabled || sweep_once) {
chMtxLock(&mutex);
completed = sweep(true);
sweep_once = FALSE;
chMtxUnlock(&mutex);
} else {
__WFI();
}
chMtxLock(&mutex);
ui_process();
if (sweep_enabled) {
if (vbat != -1) {
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);
}
}
void
pause_sweep(void)
{
sweep_enabled = FALSE;
}
void
resume_sweep(void)
{
sweep_enabled = TRUE;
}
void
toggle_sweep(void)
{
sweep_enabled = !sweep_enabled;
}
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;
}
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
// use spi_buffer as temporary buffer
// and calculate ifft for time domain
float* tmp = (float*)spi_buffer;
uint16_t window_size = SWEEP_POINTS, offset = 0;
uint8_t is_lowpass = FALSE;
switch (domain_mode & TD_FUNC) {
case TD_FUNC_BANDPASS:
offset = 0;
window_size = SWEEP_POINTS;
break;
case TD_FUNC_LOWPASS_IMPULSE:
case TD_FUNC_LOWPASS_STEP:
is_lowpass = TRUE;
offset = SWEEP_POINTS;
window_size = SWEEP_POINTS*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 < SWEEP_POINTS; i++) {
float w = kaiser_window(i+offset, window_size, beta);
tmp[i*2+0] *= w;
tmp[i*2+1] *= w;
}
for (int i = SWEEP_POINTS; i < FFT_SIZE; i++) {
tmp[i*2+0] = 0.0;
tmp[i*2+1] = 0.0;
}
if (is_lowpass) {
for (int i = 1; i < SWEEP_POINTS; 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 < SWEEP_POINTS; 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 < SWEEP_POINTS; i++) {
measured[ch][i][0] += measured[ch][i-1][0];
}
}
}
}
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
update_frequencies();
if (cal_auto_interpolate && (cal_status & CALSTAT_APPLY))
cal_interpolate(lastsaveid);
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)
;
}
const int8_t gain_table[] = {
0, // 0 ~ 300MHz
40, // 300 ~ 600MHz
50, // 600 ~ 900MHz
75, // 900 ~ 1200MHz
85, // 1200 ~ 1400MHz
95 // 1400MHz ~
};
#define DELAY_GAIN_CHANGE 10
static int
adjust_gain(int newfreq)
{
int delay = 0;
int new_order = newfreq / FREQ_HARMONICS;
int old_order = frequency / FREQ_HARMONICS;
if (new_order != old_order) {
tlv320aic3204_set_gain(gain_table[new_order], gain_table[new_order]);
delay += DELAY_GAIN_CHANGE;
}
return delay;
}
int set_frequency(uint32_t freq)
{
int 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;
}
frequency_offset = atoi(argv[0]);
set_frequency(frequency);
}
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);
freq = atoi(argv[0]);
set_frequency(freq);
chMtxUnlock(&mutex);
}
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]);
set_frequency(frequency);
}
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);
}
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);
}
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]);
config.harmonic_freq_threshold = value;
}
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;
}
clear_all_config_prop_data();
chprintf(chp, "Config and all cal data cleared.\r\n");
chprintf(chp, "Do reset manually to take effect. Then do touch cal and save.\r\n");
}
static struct {
int16_t rms[2];
int16_t ave[2];
int callback_count;
#if 0
int32_t last_counter_value;
int32_t interval_cycles;
int32_t busy_cycles;
#endif
} stat;
int16_t rx_buffer[AUDIO_BUFFER_LEN * 2];
#ifdef ENABLED_DUMP
int16_t dump_buffer[AUDIO_BUFFER_LEN];
int16_t dump_selection = 0;
#endif
volatile int16_t wait_count = 0;
float measured[2][SWEEP_POINTS][2];
static void
wait_dsp(int count)
{
wait_count = count;
//reset_dsp_accumerator();
while (wait_count)
__WFI();
}
#ifdef ENABLED_DUMP
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
void i2s_end_callback(I2SDriver *i2sp, size_t offset, size_t n)
{
// #if PORT_SUPPORTS_RT
#if 0
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 ENABLED_DUMP
duplicate_buffer_to_dump(p);
#endif
--wait_count;
}
//#if PORT_SUPPORTS_RT
#if 0
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 = {
NULL, // TX Buffer
rx_buffer, // RX Buffer
AUDIO_BUFFER_LEN * 2,
NULL, // tx callback
i2s_end_callback, // rx callback
0, // i2scfgr
2 // i2spr
};
static void cmd_data(BaseSequentialStream *chp, int argc, char *argv[])
{
int i;
int sel = 0;
if (argc == 1)
sel = atoi(argv[0]);
if (sel == 0 || sel == 1) {
chMtxLock(&mutex);
for (i = 0; i < sweep_points; i++) {
if (frequencies[i] != 0)
chprintf(chp, "%f %f\r\n", measured[sel][i][0], measured[sel][i][1]);
}
chMtxUnlock(&mutex);
} else if (sel >= 2 && sel < 7) {
chMtxLock(&mutex);
for (i = 0; i < sweep_points; i++) {
if (frequencies[i] != 0)
chprintf(chp, "%f %f\r\n", cal_data[sel-2][i][0], cal_data[sel-2][i][1]);
}
chMtxUnlock(&mutex);
} else {
chprintf(chp, "usage: data [array]\r\n");
}
}
#ifdef ENABLED_DUMP
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 LCD_WIDTH
(void)argc;
(void)argv;
chMtxLock(&mutex);
// use uint16_t spi_buffer[1024] (defined in ili9341) for read buffer
uint16_t *buf = &spi_buffer[0];
int len = LCD_WIDTH * LCD_HEIGHT;
int i;
ili9341_read_memory(0, 0, LCD_WIDTH, LCD_HEIGHT, 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);
}
#if 0
static void cmd_gamma(BaseSequentialStream *chp, int argc, char *argv[])
{
float gamma[2];
(void)argc;
(void)argv;
pause_sweep();
chMtxLock(&mutex);
wait_dsp(4);
calculate_gamma(gamma);
chMtxUnlock(&mutex);
chprintf(chp, "%d %d\r\n", gamma[0], gamma[1]);
}
#endif
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 = SWEEP_POINTS;
uint32_t frequencies[SWEEP_POINTS];
uint16_t cal_status;
float cal_data[5][SWEEP_POINTS][2];
#endif
config_t config = {
.magic = CONFIG_MAGIC,
.dac_value = 1922,
.grid_color = 0x1084,
.menu_normal_color = 0xffff,
.menu_active_color = 0x7777,
.trace_color = { RGB565(0,255,255), RGB565(255,0,40), RGB565(0,0,255), RGB565(50,255,0) },
// .touch_cal = { 693, 605, 124, 171 }, // 2.4 inch LCD panel
.touch_cal = { 338, 522, 153, 192 }, // 2.8 inch LCD panel
.default_loadcal = 0,
.harmonic_freq_threshold = 300000000,
.checksum = 0
};
properties_t current_props = {
/* magic */ CONFIG_MAGIC,
/* frequency0 */ 50000, // start = 50kHz
/* frequency1 */ 900000000, // end = 900MHz
/* sweep_points */ SWEEP_POINTS,
/* 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;
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;
}
#define DELAY_CHANNEL_CHANGE 3
// main loop for measurement
bool sweep(bool break_on_operation)
{
pll_lock_failed = false;
int i;
for (i = 0; i < sweep_points; i++) {
int delay = set_frequency(frequencies[i]);
tlv320aic3204_select(0); // CH0:REFLECT
wait_dsp(delay);
// blink LED while scanning
#if !defined(LED_OFF)
palClearPad(GPIOC, GPIOC_LED);
#endif
/* calculate reflection coeficient */
(*sample_func)(measured[0][i]);
tlv320aic3204_select(1); // CH1:TRANSMISSION
wait_dsp(DELAY_CHANNEL_CHANGE);
/* calculate transmission coeficient */
(*sample_func)(measured[1][i]);
#if !defined(LED_OFF)
// blink LED while scanning
palSetPad(GPIOC, GPIOC_LED);
#endif
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;
}
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: sweep {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);
set_frequencies(start, stop, points);
if (cal_auto_interpolate && (cal_status & CALSTAT_APPLY))
cal_interpolate(lastsaveid);
sweep_once = TRUE;
chMtxUnlock(&mutex);
// wait finishing sweep
while (sweep_once)
chThdSleepMilliseconds(10);
}
static void
update_marker_index(void)
{
int m;
int i;
for (m = 0; m < 4; 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;
}
}
}
}
}
void
set_frequencies(uint32_t start, uint32_t stop, int16_t points)
{
int i;
float span = stop - start;
for (i = 0; i < points; i++) {
float offset = i * span / (float)(points - 1);
frequencies[i] = start + (uint32_t)offset;
}
// disable at out of sweep range
for (; i < sweep_points; i++)
frequencies[i] = 0;
}
void
update_frequencies(void)
{
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();
}
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;
}
}
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
}
}
#define START_MIN 50000
//#define STOP_MAX 900000000
#define STOP_MAX 1500000000
void
set_sweep_frequency(int type, int32_t freq)
{
int cal_applied = cal_status & CALSTAT_APPLY;
// negative value indicate overflow, do nothing
if (freq < 0)
return;
switch (type) {
case ST_START:
freq_mode_startstop();
if (freq < START_MIN)
freq = START_MIN;
if (freq > STOP_MAX)
freq = STOP_MAX;
if (frequency0 != freq) {
ensure_edit_config();
frequency0 = freq;
// if start > stop then make start = stop
if (frequency1 < freq)
frequency1 = freq;
update_frequencies();
}
break;
case ST_STOP:
freq_mode_startstop();
if (freq > STOP_MAX)
freq = STOP_MAX;
if (freq < START_MIN)
freq = START_MIN;
if (frequency1 != freq) {
ensure_edit_config();
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 (frequency0 != freq) {
ensure_edit_config();
frequency0 = freq;
int center = frequency0;
int 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;
case ST_SPAN:
freq_mode_centerspan();
if (frequency1 != -freq) {
ensure_edit_config();
frequency1 = -freq;
int center = frequency0;
int span = -frequency1;
if (center-span/2 < START_MIN) {
center = START_MIN + span/2;
frequency0 = center;
}
if (center+span/2 > STOP_MAX) {
center = STOP_MAX - span/2;
frequency0 = center;
}
update_frequencies();
}
break;
case ST_CW:
freq_mode_centerspan();
if (frequency0 != freq || frequency1 != 0) {
ensure_edit_config();
frequency0 = freq;
frequency1 = 0;
update_frequencies();
}
break;
}
if (cal_auto_interpolate && cal_applied)
cal_interpolate(lastsaveid);
}
uint32_t
get_sweep_frequency(int type)
{
if (frequency1 >= 0) {
switch (type) {
case ST_START: return frequency0;
case ST_STOP: return frequency1;
case ST_CENTER: return (frequency0 + frequency1)/2;
case ST_SPAN: return frequency1 - frequency0;
case ST_CW: return (frequency0 + frequency1)/2;
}
} else {
switch (type) {
case ST_START: return frequency0 + frequency1/2;
case ST_STOP: return frequency0 - frequency1/2;
case ST_CENTER: return frequency0;
case ST_SPAN: return -frequency1;
case ST_CW: return frequency0;
}
}
return 0;
}
static void cmd_sweep(BaseSequentialStream *chp, int argc, char *argv[])
{
if (argc == 0) {
chprintf(chp, "%d %d %d\r\n", frequency0, frequency1, sweep_points);
return;
} else if (argc > 3) {
goto usage;
}
if (argc >= 2) {
if (strcmp(argv[0], "start") == 0) {
int32_t value = atoi(argv[1]);
set_sweep_frequency(ST_START, value);
return;
} else if (strcmp(argv[0], "stop") == 0) {
int32_t value = atoi(argv[1]);
set_sweep_frequency(ST_STOP, value);
return;
} else if (strcmp(argv[0], "center") == 0) {
int32_t value = atoi(argv[1]);
set_sweep_frequency(ST_CENTER, value);
return;
} else if (strcmp(argv[0], "span") == 0) {
int32_t value = atoi(argv[1]);
set_sweep_frequency(ST_SPAN, value);
return;
} else if (strcmp(argv[0], "cw") == 0) {
int32_t value = atoi(argv[1]);
set_sweep_frequency(ST_CW, value);
return;
}
}
if (argc >= 1) {
int32_t value = atoi(argv[0]);
if (value == 0)
goto usage;
set_sweep_frequency(ST_START, value);