forked from ttrftech/NanoVNA
-
Notifications
You must be signed in to change notification settings - Fork 54
/
main.c
3425 lines (3171 loc) · 105 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
/*
*
* 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 "nanovna.h"
#include <chprintf.h>
#include <string.h>
#include <math.h>
freq_t frequencyStart;
freq_t frequencyStop;
int32_t frequencyExtra;
/*
* Shell settings
*/
// If need run shell as thread (use more amount of memory fore stack), after
// enable this need reduce spi_buffer size, by default shell run in main thread
#ifdef TINYSA4
#define VNA_SHELL_THREAD
#endif
static BaseSequentialStream *shell_stream;
threads_queue_t shell_thread;
// Shell new line
#define VNA_SHELL_NEWLINE_STR "\r\n"
// Shell command promt
#define VNA_SHELL_PROMPT_STR "ch> "
// Shell max arguments
#define VNA_SHELL_MAX_ARGUMENTS 4
// Shell max command line size
#define VNA_SHELL_MAX_LENGTH 48
// Shell command functions prototypes
typedef void (*vna_shellcmd_t)(int argc, char *argv[]);
#define VNA_SHELL_FUNCTION(command_name) \
static void command_name(int argc, char *argv[])
// Shell command line buffer, args, nargs, and function ptr
char shell_line[VNA_SHELL_MAX_LENGTH];
char *shell_args[VNA_SHELL_MAX_ARGUMENTS + 1];
uint16_t shell_nargs;
static volatile vna_shellcmd_t shell_function = 0;
//#define ENABLED_DUMP
// Allow get threads debug info
#define ENABLE_THREADS_COMMAND
// Enable vbat_offset command, allow change battery voltage correction in config
#define ENABLE_VBAT_OFFSET_COMMAND
// Info about NanoVNA, need fore soft
#define ENABLE_INFO_COMMAND
// Enable color command, allow change config color for traces, grid, menu
#define ENABLE_COLOR_COMMAND
#ifdef __USE_SERIAL_CONSOLE__
#define ENABLE_USART_COMMAND
#endif
#ifdef __USE_SD_CARD__
#ifdef __DISABLE_HOT_INSERT__
uint16_t sd_card_inserted_at_boot = false;
#endif
// Enable SD card console command
#define ENABLE_SD_CARD_CMD
#endif
void update_frequencies(void);
static void set_frequencies(freq_t start, freq_t stop, uint16_t points);
static bool sweep(bool break_on_operation);
static long_t my_atoi(const char *p);
uint8_t sweep_mode = SWEEP_ENABLE;
uint16_t sweep_once_count = 1;
uint16_t redraw_request = 0; // contains REDRAW_XXX flags
// Version text, displayed in Config->Version menu, also send by info command
const char * const info_about[]={
BOARD_NAME,
"2019-2024 Copyright @Erik Kaashoek",
"2016-2020 Copyright @edy555",
"SW licensed under GPL. See: https://github.com/erikkaashoek/tinySA",
"Version: " VERSION,
"Build Time: " __DATE__ " - " __TIME__,
"Kernel: " CH_KERNEL_VERSION,
"Compiler: " PORT_COMPILER_NAME,
"Architecture: " PORT_ARCHITECTURE_NAME " Core Variant: " PORT_CORE_VARIANT_NAME,
"Port Info: " PORT_INFO,
"Platform: " PLATFORM_NAME,
0 // sentinel
};
bool dirty = true;
int32_t scan_after_dirty = 0;
uint8_t completed = false;
uint8_t enable_after_complete = 0;
#ifdef TINYSA4
freq_t ULTRA_MAX_FREQ; // Start of harmonic mode
freq_t MAX_LO_FREQ;
freq_t MAX_ABOVE_IF_FREQ; // Range to use for below IF
freq_t MIN_BELOW_IF_FREQ; // Range to use for below IF
freq_t ULTRA_THRESHOLD;
freq_t NORMAL_MAX_FREQ;
int max2871;
#endif
void clear_backup(void) {
uint32_t *f = &backup; // Clear backup when no valid config data
int i = USED_BACKUP_SIZE;
while (i--)
*f++ = 0;
}
#ifdef __USE_SD_CARD__
systime_t last_auto_save = 0;
#endif
#ifdef TINYSA4
static THD_WORKING_AREA(waThread1, 1224);
systime_t restart_set_time = 0;
systime_t restart_interval = 0;
#else
static THD_WORKING_AREA(waThread1, 768);
bool has_esd = false;
#endif
static THD_FUNCTION(Thread1, arg)
{
(void)arg;
chRegSetThreadName("sweep");
// Init UI and plot grid
area_height = AREA_HEIGHT_NORMAL;
ui_init();
//Initialize graph plotting
plot_init();
#ifdef __SD_CARD_LOAD__
sd_card_load_config("autoload.ini");
#endif
//#ifndef TINYSA4
// ui_process();
//#endif
while (1) {
// START_PROFILE
if (sweep_mode&(SWEEP_ENABLE|SWEEP_ONCE)) {
backup_t b;
b.data.frequency0 = setting.frequency0;
b.data.frequency1 = setting.frequency1;
if (setting.auto_attenuation)
b.data.attenuation = 0;
else
b.data.attenuation = setting.attenuate_x2+1;
b.data.external_gain = setting.external_gain*2;
if (setting.auto_reflevel || setting.unit != U_DBM)
b.data.reflevel = 0;
else
b.data.reflevel = setting.reflevel + 140;
if (setting.rbw_x10 == 0)
b.data.RBW = 0;
else {
#ifdef TINYSA4
b.data.RBW = SI4463_rbw_selected+1;
#else
b.data.RBW = SI4432_rbw_selected+1;
#endif
}
#ifdef TINYSA4
b.data.harmonic = setting.harmonic;
#endif
b.data.mode = setting.mode;
b.data.checksum = 0;
uint8_t *c = (uint8_t *)&b;
int ci = sizeof(backup_t)-1; // Exclude checksum
uint8_t checksum = 0x55;
while (ci--) {
checksum ^= *c++;
}
b.data.checksum = checksum;
uint32_t *f = (uint32_t *)&b;
uint32_t *t = &backup;
int i = USED_BACKUP_SIZE;
while (i--)
*t++ = *f++;
#ifdef __LISTEN__
if (setting.listen && markers[active_marker].enabled == M_ENABLED) {
perform(false, 0, getFrequency(markers[active_marker].index), false);
SI4432_Listen(MODE_SELECT(setting.mode));
} else
#endif
{
completed = sweep(true);
#ifdef __USE_SD_CARD__
if (setting.trigger_auto_save && (last_auto_save == 0 || chVTGetSystemTimeX() - last_auto_save > S2ST(30)) ) { // once every 30 seconds max
uint16_t old_mode = config._mode;
config._mode |= _MODE_AUTO_FILENAME;
save_csv(1+(2<<0)); // frequencies + trace 1
last_auto_save = chVTGetSystemTimeX();
config._mode = old_mode;
}
#endif
#ifdef __USE_RTC__
if (restart_set_time) {
if ( chVTGetSystemTimeX() - restart_set_time > restart_interval) {
chThdSleepMilliseconds(200);
NVIC_SystemReset();
}
}
#endif
if (sweep_once_count>1) {
sweep_once_count--;
} else
sweep_mode&=~SWEEP_ONCE;
}
} else if (sweep_mode & SWEEP_SELFTEST) {
// call from lowest level to save stack space
selftest(setting.test);
completed = true;
// sweep_mode = SWEEP_ENABLE;
#ifdef __SINGLE_LETTER__
} else if (sweep_mode & SWEEP_REMOTE) {
sweep_remote();
#endif
#ifdef __CALIBRATE__
} else if (sweep_mode & SWEEP_CALIBRATE) {
// call from lowest level to save stack space
calibrate();
sweep_mode = SWEEP_ENABLE;
#endif
#ifdef TINYSA4
} else if (sweep_mode & SWEEP_CALIBRATE_HARMONIC) {
// call from lowest level to save stack space
calibrate_harmonic();
sweep_mode = SWEEP_ENABLE;
#endif
} else {
// if (setting.mode != -1)
__WFI();
}
// STOP_PROFILE
// Run Shell command in sweep thread
if (shell_function) {
operation_requested = OP_NONE; // otherwise commands will be aborted
do {
shell_function(shell_nargs - 1, &shell_args[1]);
shell_function = 0;
if (operation_requested == OP_NONE) // Don't prompt if aborted
shell_printf(VNA_SHELL_PROMPT_STR);
// Resume shell thread
if (!abort_enabled) osalThreadDequeueNextI(&shell_thread, MSG_OK);
} while (shell_function);
if (dirty) {
if (MODE_OUTPUT(setting.mode))
draw_menu(); // update screen if in output mode and dirty
else
redraw_request |= REDRAW_CAL_STATUS | REDRAW_AREA | REDRAW_FREQUENCY;
}
}
// START_PROFILE
// Process UI inputs
if (!(sweep_mode & SWEEP_SELFTEST)) {
sweep_mode|= SWEEP_UI_MODE;
ui_process();
sweep_mode&=~SWEEP_UI_MODE;
}
// Process collected data, calculate trace coordinates and plot only if scan
// completed
if (completed) {
// Enable traces at sweep complete for redraw
if (enable_after_complete){
TRACE_ENABLE(enable_after_complete);
enable_after_complete = 0;
}
// START_PROFILE;
// Prepare draw graphics, cache all lines, mark screen cells for redraw
plot_into_index(measured);
redraw_request |= REDRAW_CELLS | REDRAW_BATTERY;
// STOP_PROFILE;
if (uistat.marker_tracking) {
int i = marker_search_max(active_marker);
if (i != -1 && active_marker != MARKER_INVALID) {
set_marker_index(active_marker, i);
redraw_request |= REDRAW_MARKER;
}
}
}
// plot trace and other indications as raster
draw_all(completed); // flush markmap only if scan completed to prevent
// remaining traces
#ifdef __WATCHDOG__
wdgReset(&WDGD1);
#endif
// STOP_PROFILE
}
}
#pragma GCC push_options
#pragma GCC optimize ("Os")
void enableTracesAtComplete(uint8_t mask){
// Disable this traces
TRACE_DISABLE(mask);
enable_after_complete|=mask;
redraw_request|=REDRAW_AREA;
}
int
is_paused(void)
{
return !(sweep_mode & (SWEEP_ENABLE|SWEEP_ONCE));
}
static inline void
pause_sweep(void)
{
sweep_mode &= ~SWEEP_ENABLE;
}
static inline void
resume_sweep(void)
{
sweep_mode |= SWEEP_ENABLE;
}
void
resume_once(uint16_t c)
{
sweep_once_count = c;
sweep_mode |= SWEEP_ONCE;
}
void
toggle_sweep(void)
{
sweep_mode ^= SWEEP_ENABLE;
}
// Shell commands output
int shell_printf(const char *fmt, ...)
{
if (shell_stream == NULL) return 0;
va_list ap;
int formatted_bytes = 0;
va_start(ap, fmt);
formatted_bytes = chvprintf(shell_stream, fmt, ap);
va_end(ap);
return formatted_bytes;
}
// Shell commands output
int usage_printf(const char *fmt, ...)
{
if (shell_stream == NULL) return 0;
va_list ap;
int formatted_bytes = 0;
va_start(ap, fmt);
shell_printf("usage: ");
formatted_bytes += chvprintf(shell_stream, fmt, ap);
va_end(ap);
return formatted_bytes;
}
#ifdef __USE_SERIAL_CONSOLE__
// Serial Shell commands output
int shell_serial_printf(const char *fmt, ...)
{
va_list ap;
int formatted_bytes;
va_start(ap, fmt);
formatted_bytes = chvprintf((BaseSequentialStream *)&SD1, fmt, ap);
va_end(ap);
return formatted_bytes;
}
#endif
//
// Function used for search substring v in list
// Example need search parameter "center" in "start|stop|center|span|cw" getStringIndex return 2
// If not found return -1
// Used for easy parse command arguments
static int get_str_index(const char *v, const char *list)
{
int i = 0;
while (1) {
const char *p = v;
while (1) {
char c = *list;
if (c == '|') c = 0;
if (c == *p++) {
// Found, return index
if (c == 0) return i;
list++; // Compare next symbol
continue;
}
break; // Not equal, break
}
// Set new substring ptr
while (1) {
// End of string, not found
if (*list == 0) return -1;
if (*list++ == '|') break;
}
i++;
}
return -1;
}
#ifdef __USE_RTC__
VNA_SHELL_FUNCTION(cmd_restart)
{
(void)argc;
(void)argv;
if (argc == 1) {
restart_interval = S2ST(my_atoi(argv[0]));
if (restart_interval) {
restart_set_time = chVTGetSystemTimeX();
if (restart_set_time == 0)
restart_set_time = 1;
} else
restart_set_time = 0;
}
}
#endif
VNA_SHELL_FUNCTION(cmd_pause)
{
(void)argc;
(void)argv;
pause_sweep();
draw_cal_status();
}
VNA_SHELL_FUNCTION(cmd_status)
{
(void)argc;
(void)argv;
if (is_paused())
shell_printf("Paused\r\n");
else
shell_printf("Resumed\r\n");
}
VNA_SHELL_FUNCTION(cmd_resume)
{
(void)argc;
(void)argv;
uint16_t c = 0;
// restore frequencies array and cal
// if (dirty)
update_frequencies();
if (argc == 1) {
c = my_atoi(argv[0]);
resume_once(c) ;
} else
resume_sweep();
}
VNA_SHELL_FUNCTION(cmd_wait)
{
(void)argc;
(void)argv;
if (argc == 1 && argv[0][0] == '?') {
usage_printf("wait {seconds}\r\n");
return;
}
break_execute = false;
drawMessageBox("Info", "Waiting", 0) ;
if (argc == 1) {
uint32_t t = my_atoi(argv[0]);
while (t > 0 && !break_execute) {
chThdSleepMilliseconds(1000);
t -= 1;
}
} else {
pause_sweep();
resume_once(1) ;
while (!is_paused() && !break_execute) {
chThdSleepMilliseconds(100);
}
}
redraw_request|= REDRAW_AREA|REDRAW_FREQUENCY;
draw_all(true);
}
VNA_SHELL_FUNCTION(cmd_repeat)
{
(void)argc;
(void)argv;
uint16_t c = 0;
if (argc == 1) {
c = my_atoi(argv[0]);
set_repeat(c);
} else
set_repeat(1);
}
VNA_SHELL_FUNCTION(cmd_reset)
{
(void)argc;
(void)argv;
#ifndef TINYSA4
if (argc == 1) {
if (get_str_index(argv[0], "dfu") == 0) {
shell_printf("Performing reset to DFU mode\r\n");
enter_dfu();
return;
}
}
#endif
shell_printf("Performing reset\r\n");
rccEnableWWDG(FALSE);
WWDG->CFR = 0x60;
WWDG->CR = 0xff;
/* wait forever */
while (1)
;
}
int set_frequency(freq_t freq)
{
(void) freq;
return 1;
}
// Use macro, std isdigit more big
#define _isdigit(c) (c >= '0' && c <= '9')
// Rewrite universal standart str to value functions to more compact
//
// Convert string to int32
static long_t my_atoi(const char *p)
{
long_t value = 0;
uint32_t c;
bool neg = false;
if (*p == '-') {neg = true; p++;}
if (*p == '+') p++;
while ((c = *p++ - '0') < 10)
value = value * 10 + c;
switch (*(--p)) {
case 'k': value *= 1000; break;
case 'M': value *= 1000000; break;
case 'G': value *= 1000000000; break;
}
return neg ? -value : value;
}
// Convert string to uint32
// 0x - for hex radix
// 0o - for oct radix
// 0b - for bin radix
// default dec radix
freq_t my_atoui(const char *p)
{
int d = 1;
freq_t value = 0, radix = 10, c;
if (*p == '+') p++;
if (*p == '0') {
switch (p[1]) {
case 'x': radix = 16; break;
case 'o': radix = 8; break;
case 'b': radix = 2; break;
default: goto calculate;
}
p+=2;
}
calculate:
while (1) {
c = *p++;
if (c == '.') { d = 0; continue; }
c = c - '0';
if (c >= 'A' - '0') c = (c&(~0x20)) - ('A' - '0') + 10;
if (c >= radix) break;
if (value < (~(freq_t)0)/radix) {
if (d<=0) d--;
value = value * radix + c;
}
}
if (d == 1)
d = 0;
switch (*(--p)) {
case 'k': d += 3; break;
case 'M': d += 6; break;
case 'G': d += 9; break;
}
while (d < 0) {
value /= radix;
d++;
}
while (d-->0)
value *= radix;
return value;
}
float
my_atof(const char *p)
{
int neg = FALSE;
if (*p == '-')
neg = TRUE;
if (*p == '-' || *p == '+')
p++;
float x = my_atoi(p);
while (_isdigit((int)*p))
p++;
if (*p == 'k' || *p == 'M' || *p == 'G')
p++;
if (*p == '.' || *p == ',') {
float d = 1.0;
p++;
while (_isdigit((int)*p)) {
d /= 10;
x += d * (*p - '0');
p++;
}
}
if (*p == 'e' || *p == 'E') {
p++;
int exp = my_atoi(p);
while (exp > 0) {
x *= 10;
exp--;
}
while (exp < 0) {
x /= 10;
exp++;
}
}
switch (*p) {
case 'k': x *= 1e+3; break;
case 'M': x *= 1e+6; break;
case 'G': x *= 1e+9; break;
case 'm': x /= 1e+3; break;
case 'u': x /= 1e+6; break;
case 'n': x /= 1e+9; break;
case 'p': x /= 1e+12; break;
}
if (neg)
x = -x;
return x;
}
VNA_SHELL_FUNCTION(cmd_freq)
{
if (argc != 1 || argv[0][0] == '?') {
goto usage;
}
freq_t freq = my_atoui(argv[0]);
pause_sweep();
set_frequency(freq);
return;
usage:
usage_printf("freq {frequency(Hz)}\r\n");
}
#ifdef __USE_RTC__
VNA_SHELL_FUNCTION(cmd_time)
{
(void)argc;
(void)argv;
uint32_t dt_buf[2];
dt_buf[0] = rtc_get_tr_bcd(); // TR should be read first for sync
dt_buf[1] = rtc_get_dr_bcd(); // DR should be read second
static const uint8_t idx_to_time[] = {6,5,4,2, 1, 0};
static const char time_cmd[] = "y|m|d|h|min|sec";
// 0 1 2 4 5 6
// time[] ={sec, min, hr, 0, day, month, year, 0}
uint8_t *time = (uint8_t*)dt_buf;
if (argc == 3 && get_str_index(argv[0], "b") == 0){
rtc_set_time(my_atoui(argv[1]), my_atoui(argv[2]));
return;
}
if (argc!=2 || argv[0][0] == '?') goto usage;
int idx = get_str_index(argv[0], time_cmd);
uint32_t val = my_atoui(argv[1]);
if (idx < 0 || val > 99)
goto usage;
// Write byte value in struct
time[idx_to_time[idx]] = ((val/10)<<4)|(val%10); // value in bcd format
rtc_set_time(dt_buf[1], dt_buf[0]);
return;
usage:
usage_printf("time {[%s] 0-99} or {b 0xYYMMDD 0xHHMMSS}\r\n"\
"20%02x/%02x/%02x %02x:%02x:%02x\r\n", time_cmd, time[6], time[5], time[4], time[2], time[1], time[0]);
}
#endif
VNA_SHELL_FUNCTION(cmd_dac)
{
uint32_t value;
if (argc != 1 || argv[0][0] == '?') {
usage_printf("dac {value(0-4095)}\r\n"\
"current value: %d\r\n", config.dac_value);
return;
}
value = my_atoui(argv[0]) & 0xFFF;
config.dac_value = value;
DAC->DHR12R2 = value;
}
VNA_SHELL_FUNCTION(cmd_saveconfig)
{
(void)argc;
(void)argv;
config_save();
shell_printf("Config saved.\r\n");
}
VNA_SHELL_FUNCTION(cmd_clearconfig)
{
if (argc != 1 || argv[0][0] == '?') {
usage_printf("clearconfig {protection key}\r\n");
return;
}
if (get_str_index(argv[0], "1234") != 0) {
shell_printf("Key unmatched.\r\n");
return;
}
clear_all_config_prop_data();
shell_printf("Config and all cal data cleared.\r\n"\
"Do reset manually to take effect. Then do touch cal and save.\r\n");
}
#ifdef __AUDIO__
static struct {
int16_t rms[2];
int16_t ave[2];
int callback_count;
#if 1
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 uint8_t wait_count = 0;
volatile uint8_t accumerate_count = 0;
#endif
#ifdef __VNA__
const int8_t bandwidth_accumerate_count[] = {
1, // 1kHz
3, // 300Hz
10, // 100Hz
33, // 30Hz
100 // 10Hz
};
float measured[2][POINTS_COUNT][2];
#endif
measurement_t measured;
#ifdef __AUDIO__
#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
#ifdef __AUDIO__
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 > 1) {
--wait_count;
} else if (wait_count > 0) {
if (accumerate_count > 0) {
#ifndef TINYSA4
// dsp_process(p, n);
#endif
accumerate_count--;
}
#ifdef ENABLED_DUMP
duplicate_buffer_to_dump(p);
#endif
}
#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 = {
NULL, // TX Buffer
rx_buffer, // RX Buffer
AUDIO_BUFFER_LEN * 2,
NULL, // tx callback
i2s_end_callback, // rx callback
0, // i2scfgr
2 // i2spr
};
#endif
#endif
#define MAX_DATA 2
VNA_SHELL_FUNCTION(cmd_data)
{
int i;
int sel = 0;
if (argc != 1 || argv[0][0] == '?')
goto usage;
sel = my_atoi(argv[0]);
if (sel >= 0 && sel <= MAX_DATA) {
static const uint8_t sel_conv[]={TRACE_TEMP, TRACE_STORED, TRACE_ACTUAL};
float *data = measured[sel_conv[sel]];
for (i = 0; i < sweep_points; i++)
shell_printf("%e\r\n", value(data[i]));
return;
}
usage:
usage_printf("data [0-2]\r\n");
}
#ifdef ENABLED_DUMP
VNA_SHELL_FUNCTION(cmd_dump)
{
int i, j;
int len;
if (argc == 1)
dump_selection = my_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++) {
shell_printf("%04x ", 0xffff & (int)dump_buffer[i]);
}
shell_printf("\r\n");
}
}
#endif
uint8_t in_menu_command;
VNA_SHELL_FUNCTION(cmd_menu)
{
ui_mode_normal();
menu_current_level = 0;
if (argc == 0) {
return;
}
in_menu_command = true;
if (argc >= 1)
menu_invoke(my_atoi(argv[0])-1);
if (argc >= 2)
menu_invoke(my_atoi(argv[1])-1);
if (argc >= 3)
menu_invoke(my_atoi(argv[2])-1);
if (argc >= 4)
menu_invoke(my_atoi(argv[3])-1);
in_menu_command = false;
}
uint8_t remote_text = false;
VNA_SHELL_FUNCTION(cmd_text)
{
if (argc!= 1)
return;
char *p = argv[0];
char *t = kp_buf;
while (*p) {
*t++ = *p++;
}
*t = 0;
uistat.value = my_atof(kp_buf);
uistat.freq_value = my_atoui(kp_buf);
set_numeric_value();
remote_text = true;
ui_mode_normal();
}
VNA_SHELL_FUNCTION(cmd_remark)
{
(void) argc;
(void) argv;
}
#ifdef __REMOTE_DESKTOP__
uint8_t remote_mouse_down = false;
uint8_t auto_capture = false;
void send_region(remote_region_t *rd, uint8_t * buf, uint16_t size)
{
if (SDU1.config->usbp->state == USB_ACTIVE) {
streamWrite(shell_stream, (void*) rd, sizeof(remote_region_t));
streamWrite(shell_stream, (void*) buf, size);
streamWrite(shell_stream, (void*)"ch> ", 4);
}
else
auto_capture = false;
}
VNA_SHELL_FUNCTION(cmd_refresh)
{
// read pixel count at one time (PART*2 bytes required for read buffer)
int m = generic_option_cmd("refresh", "off|on", argc, argv[0]);
if (m>=0) {
auto_capture = m;
}
}
VNA_SHELL_FUNCTION(cmd_touch)
{
if (argc != 2) return;
touch_set(my_atoi(argv[0]), my_atoi(argv[1]));
remote_mouse_down = 1;
handle_touch_interrupt();
}
VNA_SHELL_FUNCTION(cmd_release)
{
if (argc == 2)
touch_set(my_atoi(argv[0]), my_atoi(argv[1]));
remote_mouse_down = 2;
handle_touch_interrupt();
}
#endif
VNA_SHELL_FUNCTION(cmd_capture)
{
// read pixel count at one time (PART*2 bytes required for read buffer)
(void)argc;
(void)argv;
int y;
#ifdef TINYSA4
#if SPI_BUFFER_SIZE < (2*LCD_WIDTH)
#error "Low size of spi_buffer for cmd_capture"
#endif
#else
#if SPI_BUFFER_SIZE < (3*LCD_WIDTH + 1)
#error "Low size of spi_buffer for cmd_capture"
#endif
#endif
// read 2 row pixel time (read buffer limit by 2/3 + 1 from spi_buffer size)
for (y = 0; y < LCD_HEIGHT; y += 2) {
// use uint16_t spi_buffer[2048] (defined in ili9341) for read buffer
uint8_t *buf = (uint8_t *)spi_buffer;
ili9341_read_memory(0, y, LCD_WIDTH, 2, spi_buffer);
streamWrite(shell_stream, (void*)buf, 2 * LCD_WIDTH * sizeof(uint16_t));
}
}
#ifdef ENABLE_SD_CARD_CMD
#ifndef __USE_SD_CARD__
#error "Need enable SD card support __USE_SD_CARD__ in nanovna.h, for use ENABLE_SD_CARD_CMD"
#endif