This repository was archived by the owner on Mar 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 844
Expand file tree
/
Copy pathcommand.c
More file actions
881 lines (806 loc) · 26.5 KB
/
Copy pathcommand.c
File metadata and controls
881 lines (806 loc) · 26.5 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
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
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2011 Black Sphere Technologies Ltd.
* Written by Gareth McMullin <[email protected]>
* Copyright (C) 2021 Uwe Bonnes ([email protected])
* Copyright (C) 2023-2025 1BitSquared <[email protected]>
* Modified by Rachel Mant <[email protected]>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* This file implements a basic command interpreter for GDB 'monitor' commands.
*/
#include "general.h"
#include "platform.h"
#include "exception.h"
#include "command.h"
#include "gdb_packet.h"
#include "target.h"
#include "target_internal.h"
#include "morse.h"
#include "version.h"
#include "jtagtap.h"
#if CONFIG_BMDA == 0
#include "jtag_scan.h"
#endif
#ifdef ENABLE_RTT
#include "rtt.h"
#include "hex_utils.h"
#endif
#ifdef PLATFORM_HAS_TRACESWO
#include "serialno.h"
#include "swo.h"
#include "usb.h"
#endif
typedef struct scan_command {
bool (*scan)(void);
const char *name;
} scan_command_s;
static bool cmd_version(target_s *target, int argc, const char **argv);
static bool cmd_help(target_s *target, int argc, const char **argv);
static bool cmd_jtag_scan(target_s *target, int argc, const char **argv);
static bool cmd_swd_scan(target_s *target, int argc, const char **argv);
#if defined(CONFIG_RVSWD) && defined(PLATFORM_HAS_RVSWD)
static bool cmd_rvswd_scan(target_s *target, int argc, const char **argv);
#endif
static bool cmd_onboard_flash_scan(target_s *target, int argc, const char **argv);
static bool cmd_auto_scan(target_s *target, int argc, const char **argv);
static bool cmd_frequency(target_s *target, int argc, const char **argv);
static bool cmd_targets(target_s *target, int argc, const char **argv);
static bool cmd_morse(target_s *target, int argc, const char **argv);
static bool cmd_halt_timeout(target_s *target, int argc, const char **argv);
static bool cmd_connect_reset(target_s *target, int argc, const char **argv);
static bool cmd_reset(target_s *target, int argc, const char **argv);
static bool cmd_tdi_low_reset(target_s *target, int argc, const char **argv);
#ifdef PLATFORM_HAS_POWER_SWITCH
static bool cmd_target_power(target_s *target, int argc, const char **argv);
#endif
#ifdef PLATFORM_HAS_BATTERY
static bool cmd_target_battery(target_s *t, int argc, const char **argv);
#endif
#ifdef PLATFORM_HAS_WIFI
static bool cmd_wifi(target_s *t, int argc, const char **argv);
#endif
#ifdef PLATFORM_HAS_TRACESWO
static bool cmd_swo(target_s *target, int argc, const char **argv);
#endif
static bool cmd_heapinfo(target_s *target, int argc, const char **argv);
#ifdef ENABLE_RTT
static bool cmd_rtt(target_s *target, int argc, const char **argv);
#endif
#if defined(PLATFORM_HAS_DEBUG) && CONFIG_BMDA == 0
static bool cmd_debug_bmp(target_s *target, int argc, const char **argv);
#endif
#if CONFIG_BMDA == 1
static bool cmd_shutdown_bmda(target_s *target, int argc, const char **argv);
#endif
#ifdef _MSC_VER
#define strtok_r strtok_s
#endif
const command_s cmd_list[] = {
{"version", cmd_version, "Display firmware version info"},
{"help", cmd_help, "Display help for monitor commands"},
{"jtag_scan", cmd_jtag_scan, "Scan JTAG chain for devices"},
{"swd_scan", cmd_swd_scan, "Scan SWD interface for devices: [TARGET_ID]"},
{"swdp_scan", cmd_swd_scan, "Deprecated: use swd_scan instead"},
#if defined(CONFIG_RVSWD) && defined(PLATFORM_HAS_RVSWD)
{"rvswd_scan", cmd_rvswd_scan, "Scan RVSWD for devices"},
#endif
{"spi_scan", cmd_onboard_flash_scan, "Scan for on-board SPI Flash devices"},
{"auto_scan", cmd_auto_scan, "Automatically scan all chain types for devices"},
{"frequency", cmd_frequency, "set minimum high and low times: [FREQ]"},
{"targets", cmd_targets, "Display list of available targets"},
{"morse", cmd_morse, "Display morse error message"},
{"halt_timeout", cmd_halt_timeout, "Timeout to wait until Cortex-M is halted: [TIMEOUT, default 2000ms]"},
{"connect_rst", cmd_connect_reset, "Configure connect under reset: [enable|disable]"},
{"reset", cmd_reset, "Pulse the nRST line - disconnects target: [PULSE_LEN, default 0ms]"},
{"tdi_low_reset", cmd_tdi_low_reset,
"Pulse nRST with TDI set low to attempt to wake certain targets up (eg LPC82x)"},
#ifdef PLATFORM_HAS_POWER_SWITCH
{"tpwr", cmd_target_power, "Supplies power to the target: [enable|disable]"},
#endif
#ifdef PLATFORM_HAS_BATTERY
{"battery", cmd_target_battery, "Reads the battery state"},
#endif
#ifdef PLATFORM_HAS_WIFI
{"wifi", cmd_wifi, "Show/Set Wi-Fi connection [AP name,passphrase]"},
#endif
#ifdef ENABLE_RTT
{"rtt", cmd_rtt,
"[enable|disable|status|channel [0..15 ...]|ident [STR]|cblock|ram [RAM_START RAM_END]|poll [MAXMS MINMS "
"MAXERR]]"},
#endif
#ifdef PLATFORM_HAS_TRACESWO
#if SWO_ENCODING == 1
{"swo", cmd_swo, "Start SWO capture, Manchester mode: <enable|disable> [decode [CHANNEL_NR ...]]"},
#elif SWO_ENCODING == 2
{"swo", cmd_swo, "Start SWO capture, UART mode: <enable|disable> [BAUDRATE] [decode [CHANNEL_NR ...]]"},
#elif SWO_ENCODING == 3
{"swo", cmd_swo, "Start SWO capture: <enable|disable> [manchester|uart] [BAUDRATE] [decode [CHANNEL_NR ...]]"},
#endif
{"traceswo", cmd_swo, "Deprecated: use swo instead"},
#endif
{"heapinfo", cmd_heapinfo, "Set semihosting heapinfo: HEAP_BASE HEAP_LIMIT STACK_BASE STACK_LIMIT"},
#if defined(PLATFORM_HAS_DEBUG) && CONFIG_BMDA == 0
{"debug_bmp", cmd_debug_bmp, "Output BMP \"debug\" strings to the second vcom: [enable|disable]"},
#endif
#if CONFIG_BMDA == 1
{"shutdown_bmda", cmd_shutdown_bmda, "Tell the BMDA server to shut down when the GDB connection closes"},
#endif
{NULL, NULL, NULL},
};
#ifdef PLATFORM_HAS_CUSTOM_COMMANDS
extern const command_s platform_cmd_list[];
#endif
bool connect_assert_nrst;
#if defined(PLATFORM_HAS_DEBUG) && CONFIG_BMDA == 0
bool debug_bmp;
#endif
unsigned cortexm_wait_timeout = 2000; /* Timeout to wait for Cortex to react on halt command. */
int command_process(target_s *const target, char *const cmd_buffer)
{
/* Initial estimate for argc */
size_t argc = 1;
for (size_t i = 0; i < strlen(cmd_buffer); ++i) {
if (cmd_buffer[i] == ' ' || cmd_buffer[i] == '\t')
++argc;
}
/* This needs replacing with something more sensible.
* It should be pinging -Wvla among other things, and it failing is straight-up UB
*/
const char **const argv = alloca(sizeof(const char *) * argc);
/* Tokenize cmd_buffer to find argv */
argc = 0;
/* Reentrant strtok needs a state pointer to the unprocessed part */
char *token_state = NULL;
for (const char *part = strtok_r(cmd_buffer, " \t", &token_state); part; part = strtok_r(NULL, " \t", &token_state))
argv[argc++] = part;
/* Look for match and call handler */
for (const command_s *cmd = cmd_list; cmd->cmd; ++cmd) {
/* Accept a partial match as GDB does.
* So 'mon ver' will match 'monitor version'
*/
if ((argc == 0) || !strncmp(argv[0], cmd->cmd, strlen(argv[0])))
return !cmd->handler(target, argc, argv);
}
#ifdef PLATFORM_HAS_CUSTOM_COMMANDS
for (const command_s *cmd = platform_cmd_list; cmd->cmd; ++cmd) {
if (!strncmp(argv[0], cmd->cmd, strlen(argv[0])))
return !cmd->handler(target, argc, argv);
}
#endif
if (!target)
return -1;
return target_command(target, argc, argv);
}
bool cmd_version(target_s *target, int argc, const char **argv)
{
(void)target;
(void)argc;
(void)argv;
#if CONFIG_BMDA == 1
gdb_out("Black Magic Debug App " FIRMWARE_VERSION "\n");
bmda_display_probe();
#else
#ifndef PLATFORM_IDENT_DYNAMIC
gdb_out(BOARD_IDENT);
#else
gdb_outf(BOARD_IDENT, platform_ident());
#endif
gdb_outf(", Hardware Version %d\n", platform_hwversion());
#endif
gdb_out("Copyright (C) 2010-2025 Black Magic Debug Project\n");
gdb_out("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n\n");
return true;
}
bool cmd_help(target_s *target, int argc, const char **argv)
{
(void)argc;
(void)argv;
if (!target || target->tc->destroy_callback) {
gdb_out("General commands:\n");
for (const command_s *cmd = cmd_list; cmd->cmd; cmd++)
gdb_outf("\t%s -- %s\n", cmd->cmd, cmd->help);
#ifdef PLATFORM_HAS_CUSTOM_COMMANDS
gdb_out("Platform commands:\n");
for (const command_s *cmd = platform_cmd_list; cmd->cmd; ++cmd)
gdb_outf("\t%s -- %s\n", cmd->cmd, cmd->help);
#endif
if (!target)
return true;
}
target_command_help(target);
return true;
}
static bool cmd_jtag_scan(target_s *target, int argc, const char **argv)
{
(void)target;
(void)argc;
(void)argv;
gdb_outf("Target voltage: %s\n", platform_target_voltage());
if (connect_assert_nrst)
platform_nrst_set_val(true); /* will be deasserted after attach */
bool scan_result = false;
TRY (EXCEPTION_ALL) {
#if CONFIG_BMDA == 1
scan_result = bmda_jtag_scan();
#else
scan_result = jtag_scan();
#endif
}
CATCH () {
case EXCEPTION_TIMEOUT:
gdb_outf("Timeout during scan. Is target stuck in WFI?\n");
break;
case EXCEPTION_ERROR:
gdb_outf("Exception: %s\n", exception_frame.msg);
break;
default:
break;
}
if (!scan_result) {
platform_target_clk_output_enable(false);
platform_nrst_set_val(false);
gdb_out("JTAG device scan failed!\n");
return false;
}
cmd_targets(NULL, 0, NULL);
platform_target_clk_output_enable(false);
morse(NULL, false);
return true;
}
bool cmd_swd_scan(target_s *target, int argc, const char **argv)
{
(void)target;
volatile uint32_t targetid = 0;
if (argc > 1)
targetid = strtoul(argv[1], NULL, 0);
gdb_outf("Target voltage: %s\n", platform_target_voltage());
if (connect_assert_nrst)
platform_nrst_set_val(true); /* will be deasserted after attach */
bool scan_result = false;
TRY (EXCEPTION_ALL) {
#if CONFIG_BMDA == 1
scan_result = bmda_swd_scan_targetid(targetid);
#else
scan_result = adiv5_swd_scan_targetid(targetid);
#endif
}
CATCH () {
case EXCEPTION_TIMEOUT:
gdb_outf("Timeout during scan. Is target stuck in WFI?\n");
break;
case EXCEPTION_ERROR:
gdb_outf("Exception: %s\n", exception_frame.msg);
break;
default:
break;
}
if (!scan_result) {
platform_target_clk_output_enable(false);
platform_nrst_set_val(false);
gdb_out("SWD scan failed!\n");
return false;
}
cmd_targets(NULL, 0, NULL);
platform_target_clk_output_enable(false);
morse(NULL, false);
return true;
}
bool cmd_onboard_flash_scan(target_s *target, int argc, const char **argv)
{
(void)target;
(void)argc;
(void)argv;
/* Attempt a SPI bus scan for Flash */
if (!onboard_flash_scan()) {
gdb_out("On-board Flash scan failed!\n");
return false;
}
/* If that succeeded, register global target commands and clear morse code state */
cmd_targets(NULL, 0, NULL);
morse(NULL, false);
return true;
}
#if defined(CONFIG_RVSWD) && defined(PLATFORM_HAS_RVSWD)
bool cmd_rvswd_scan(target_s *target, int argc, const char **argv)
{
(void)target;
(void)argc;
(void)argv;
if (platform_target_voltage())
gdb_outf("Target voltage: %s\n", platform_target_voltage());
if (connect_assert_nrst)
platform_nrst_set_val(true); /* will be deasserted after attach */
bool scan_result = false;
TRY (EXCEPTION_ALL) {
#if CONFIG_BMDA == 1
scan_result = bmda_rvswd_scan();
#else
scan_result = false;
#endif
}
CATCH () {
case EXCEPTION_TIMEOUT:
gdb_outf("Timeout during scan. Is target stuck in WFI?\n");
break;
case EXCEPTION_ERROR:
gdb_outf("Exception: %s\n", exception_frame.msg);
break;
default:
break;
}
if (!scan_result) {
platform_target_clk_output_enable(false);
platform_nrst_set_val(false);
gdb_out("RVSWD scan failed!\n");
return false;
}
cmd_targets(NULL, 0, NULL);
platform_target_clk_output_enable(false);
morse(NULL, false);
return true;
}
#endif
bool cmd_auto_scan(target_s *target, int argc, const char **argv)
{
(void)target;
(void)argc;
(void)argv;
gdb_outf("Target voltage: %s\n", platform_target_voltage());
if (connect_assert_nrst)
platform_nrst_set_val(true); /* will be deasserted after attach */
static const scan_command_s scan_commands[] =
#if CONFIG_BMDA == 1
/* clang-format off */
{
{bmda_jtag_scan, "JTAG"},
{bmda_swd_scan, "SWD"},
#if defined(CONFIG_RVSWD) && defined(PLATFORM_HAS_RVSWD)
{bmda_rvswd_scan, "RVSWD"},
#endif
};
#else
{
{jtag_scan, "JTAG"},
{adiv5_swd_scan, "SWD"},
};
/* clang-format on */
#endif
bool scan_result = false;
for (size_t i = 0; i < ARRAY_LENGTH(scan_commands); i++) {
TRY (EXCEPTION_ALL) {
scan_result = scan_commands[i].scan();
}
CATCH () {
case EXCEPTION_TIMEOUT:
gdb_outf("Timeout during %s scan. Is target stuck in WFI?\n", scan_commands[i].name);
break;
case EXCEPTION_ERROR:
gdb_outf("%s exception: %s\n", scan_commands[i].name, exception_frame.msg);
break;
default:
break;
}
if (scan_result)
break;
gdb_outf("%s scan found no devices.\n", scan_commands[i].name);
}
if (!scan_result) {
platform_target_clk_output_enable(false);
platform_nrst_set_val(false);
gdb_out("auto scan failed!\n");
return false;
}
cmd_targets(NULL, 0, NULL);
platform_target_clk_output_enable(false);
morse(NULL, false);
return true;
}
bool cmd_frequency(target_s *target, int argc, const char **argv)
{
(void)target;
if (argc == 2) {
char *multiplier = NULL;
uint32_t frequency = strtoul(argv[1], &multiplier, 10);
if (!multiplier) {
gdb_outf("Frequency must be an integral value possibly followed by 'k' or 'M'");
return false;
}
switch (*multiplier) {
case 'k':
frequency *= 1000U;
break;
case 'M':
frequency *= 1000U * 1000U;
break;
default:
break;
}
platform_max_frequency_set(frequency);
}
const uint32_t freq = platform_max_frequency_get();
if (freq == FREQ_FIXED)
gdb_outf("Debug iface frequency is fixed.\n");
else
gdb_outf("Debug iface frequency set to %" PRIu32 "Hz\n", freq);
return true;
}
static void display_target(size_t idx, target_s *target, void *context)
{
(void)context;
const char attached = target->attached ? '*' : ' ';
const char *const core_name = target->core;
if (!strcmp(target->driver, "ARM Cortex-M"))
gdb_outf("***%2u %c Unknown %s Designer 0x%x Part ID 0x%x %s\n", (unsigned)idx, attached, target->driver,
target->designer_code, target->part_id, core_name ? core_name : "");
else
gdb_outf("%2u %3c %s %s\n", (unsigned)idx, attached, target->driver, core_name ? core_name : "");
}
bool cmd_targets(target_s *target, int argc, const char **argv)
{
(void)target;
(void)argc;
(void)argv;
gdb_out("Available Targets:\n");
gdb_out("No. Att Driver\n");
if (!target_foreach(display_target, NULL)) {
gdb_out("No usable targets found.\n");
return false;
}
return true;
}
bool cmd_morse(target_s *target, int argc, const char **argv)
{
(void)target;
(void)argc;
(void)argv;
if (morse_msg) {
gdb_outf("%s\n", morse_msg);
DEBUG_WARN("%s\n", morse_msg);
} else
gdb_out("No message\n");
return true;
}
bool parse_enable_or_disable(const char *value, bool *out)
{
const size_t value_len = strlen(value);
if (value_len && !strncmp(value, "enable", value_len))
*out = true;
else if (value_len && !strncmp(value, "disable", value_len))
*out = false;
else {
gdb_out("'enable' or 'disable' argument must be provided\n");
return false;
}
return true;
}
static bool cmd_connect_reset(target_s *target, int argc, const char **argv)
{
(void)target;
bool print_status = false;
if (argc == 1)
print_status = true;
else if (argc == 2) {
if (parse_enable_or_disable(argv[1], &connect_assert_nrst))
print_status = true;
} else
gdb_out("Unrecognized command format\n");
if (print_status) {
gdb_outf("Assert nRST during connect: %s\n", connect_assert_nrst ? "enabled" : "disabled");
}
return true;
}
static bool cmd_halt_timeout(target_s *target, int argc, const char **argv)
{
(void)target;
if (argc > 1)
cortexm_wait_timeout = strtoul(argv[1], NULL, 0);
gdb_outf("Cortex-M timeout to wait for device halts: %u\n", cortexm_wait_timeout);
return true;
}
static bool cmd_reset(target_s *target, int argc, const char **argv)
{
(void)target;
uint32_t pulse_len_ms = 0;
if (argc > 1)
pulse_len_ms = strtoul(argv[1], NULL, 0);
target_list_free();
platform_nrst_set_val(true);
platform_delay(pulse_len_ms);
platform_nrst_set_val(false);
return true;
}
static bool cmd_tdi_low_reset(target_s *target, int argc, const char **argv)
{
(void)target;
(void)argc;
(void)argv;
if (!jtag_proc.jtagtap_next)
return false;
jtag_proc.jtagtap_next(true, false);
cmd_reset(NULL, 0, NULL);
return true;
}
#ifdef PLATFORM_HAS_POWER_SWITCH
static bool cmd_target_power(target_s *target, int argc, const char **argv)
{
(void)target;
if (argc == 1)
gdb_outf("Target Power: %s\n", platform_target_get_power() ? "enabled" : "disabled");
else if (argc == 2) {
bool want_enable = false;
if (parse_enable_or_disable(argv[1], &want_enable)) {
if (want_enable && !platform_target_get_power() &&
platform_target_voltage_sense() > POWER_CONFLICT_THRESHOLD) {
/* want to enable target power, but VREF > 0.5V sensed -> cancel */
gdb_outf("Target already powered (%s)\n", platform_target_voltage());
} else {
if (!platform_target_set_power(want_enable))
DEBUG_ERROR("%s target power failed\n", want_enable ? "Enabling" : "Disabling");
gdb_outf("%s target power\n", want_enable ? "Enabling" : "Disabling");
}
}
} else
gdb_outf("Unrecognized command format\n");
return true;
}
#endif
#ifdef PLATFORM_HAS_BATTERY
static bool cmd_target_battery(target_s *t, int argc, const char **argv)
{
(void)t;
(void)argc;
(void)argv;
gdb_out(platform_battery_voltage());
return true;
}
#endif
#ifdef PLATFORM_HAS_WIFI
static bool cmd_wifi(target_s *t, int argc, const char **argv)
{
(void)t;
gdb_out(platform_wifi_state(argc, argv));
return true;
}
#endif
#ifdef ENABLE_RTT
static const char *on_or_off(const bool value)
{
return value ? "on" : "off";
}
static bool cmd_rtt(target_s *target, int argc, const char **argv)
{
const size_t command_len = argc > 1 ? strlen(argv[1]) : 0;
if (argc == 1 || (argc == 2 && strncmp(argv[1], "enabled", command_len) == 0)) {
rtt_enabled = true;
rtt_found = false;
memset(rtt_channel, 0, sizeof(rtt_channel));
} else if (argc == 2 && strncmp(argv[1], "disabled", command_len) == 0) {
rtt_enabled = false;
rtt_found = false;
} else if (argc == 2 && strncmp(argv[1], "status", command_len) == 0) {
gdb_outf("rtt: %s found: %s ident: ", on_or_off(rtt_enabled), rtt_found ? "yes" : "no");
if (rtt_ident[0] == '\0')
gdb_out("off");
else
gdb_outf("\"%s\"", rtt_ident);
gdb_outf(" halt: %s", on_or_off(target_mem_access_needs_halt(target)));
gdb_out(" channels: ");
if (rtt_auto_channel)
gdb_out("auto ");
for (size_t i = 0; i < MAX_RTT_CHAN; i++) {
if (rtt_channel_enabled[i])
gdb_outf("%" PRIu32 " ", (uint32_t)i);
}
if (rtt_flag_ram)
gdb_outf("ram: 0x%08" PRIx32 " 0x%08" PRIx32, rtt_ram_start, rtt_ram_end);
gdb_outf("\nmax poll ms: %" PRIu32 " min poll ms: %" PRIu32 " max errs: %" PRIu32 "\n", rtt_max_poll_ms,
rtt_min_poll_ms, rtt_max_poll_errs);
} else if (argc >= 2 && strncmp(argv[1], "channel", command_len) == 0) {
/* mon rtt channel switches to auto rtt channel selection
mon rtt channel number... selects channels given */
for (size_t i = 0; i < MAX_RTT_CHAN; i++)
rtt_channel_enabled[i] = false;
if (argc == 2)
rtt_auto_channel = true;
else {
rtt_auto_channel = false;
for (size_t i = 2; i < (size_t)argc; ++i) {
const uint32_t channel = strtoul(argv[i], NULL, 0);
if (channel < MAX_RTT_CHAN)
rtt_channel_enabled[channel] = true;
}
}
} else if (argc == 2 && strncmp(argv[1], "ident", command_len) == 0)
rtt_ident[0] = '\0';
else if (argc == 2 && strncmp(argv[1], "poll", command_len) == 0)
gdb_outf("%" PRIu32 " %" PRIu32 " %" PRIu32 "\n", rtt_max_poll_ms, rtt_min_poll_ms, rtt_max_poll_errs);
else if (argc == 2 && strncmp(argv[1], "cblock", command_len) == 0) {
gdb_outf("cbaddr: 0x%08" PRIx32 "\n", rtt_cbaddr);
gdb_out("ch ena i/o buffer@ size head tail flag\n");
for (uint32_t i = 0; i < rtt_num_up_chan + rtt_num_down_chan; ++i) {
gdb_outf("%2" PRIu32 " %c %s 0x%08" PRIx32 " %6" PRIu32 " %6" PRIu32 " %6" PRIu32 " %4" PRIu32 "\n", i,
rtt_channel_enabled[i] ? 'y' : 'n', i < rtt_num_up_chan ? "out" : "in ", rtt_channel[i].buf_addr,
rtt_channel[i].buf_size, rtt_channel[i].head, rtt_channel[i].tail, rtt_channel[i].flag);
}
} else if (argc == 3 && strncmp(argv[1], "ident", command_len) == 0) {
strncpy(rtt_ident, argv[2], sizeof(rtt_ident));
rtt_ident[sizeof(rtt_ident) - 1U] = '\0';
for (size_t i = 0; i < sizeof(rtt_ident); i++) {
if (rtt_ident[i] == '_')
rtt_ident[i] = ' ';
}
} else if (argc == 2 && strncmp(argv[1], "ram", command_len) == 0)
rtt_flag_ram = false;
else if (argc == 4 && strncmp(argv[1], "ram", command_len) == 0) {
if (read_hex32(argv[2], NULL, &rtt_ram_start, READ_HEX_NO_FOLLOW) &&
read_hex32(argv[3], NULL, &rtt_ram_end, READ_HEX_NO_FOLLOW)) {
rtt_flag_ram = rtt_ram_end > rtt_ram_start;
if (!rtt_flag_ram)
gdb_out("address?\n");
}
} else if (argc == 5 && strncmp(argv[1], "poll", command_len) == 0) {
/* set polling params */
rtt_max_poll_ms = strtoul(argv[2], NULL, 0);
rtt_min_poll_ms = strtoul(argv[3], NULL, 0);
rtt_max_poll_errs = strtoul(argv[4], NULL, 0);
} else
gdb_out("what?\n");
return true;
}
#endif
#ifdef PLATFORM_HAS_TRACESWO
static bool cmd_swo_enable(int argc, const char **argv)
{
/* Set up which mode we're going to default to */
#if SWO_ENCODING == 1
const swo_coding_e capture_mode = swo_manchester;
#elif SWO_ENCODING == 2
const swo_coding_e capture_mode = swo_nrz_uart;
#elif SWO_ENCODING == 3
swo_coding_e capture_mode = swo_none;
#endif
/* Set up the presumed baudrate for the stream */
uint32_t baudrate = SWO_DEFAULT_BAUD;
/*
* Before we can enable SWO data recovery, potentially with decoding,
* start with the assumption ITM decoding is off
*/
uint32_t itm_stream_mask = 0U;
uint8_t decode_arg = 1U;
#if SWO_ENCODING == 3
/* Next, determine which decoding mode to use */
if (argc > decode_arg) {
const size_t arg_length = strlen(argv[decode_arg]);
if (!strncmp(argv[decode_arg], "manchester", arg_length))
capture_mode = swo_manchester;
if (!strncmp(argv[decode_arg], "uart", arg_length))
capture_mode = swo_nrz_uart;
}
/* If a mode was given, make sure the rest of the parser skips the mode verb */
if (capture_mode != swo_none)
++decode_arg;
/* Otherwise set a default mode up */
else
capture_mode = swo_nrz_uart;
#endif
#if SWO_ENCODING == 2 || SWO_ENCODING == 3
/* Handle the optional baud rate argument if present */
if (capture_mode == swo_nrz_uart && argc > decode_arg && argv[decode_arg][0] >= '0' && argv[decode_arg][0] <= '9') {
baudrate = strtoul(argv[decode_arg], NULL, 0);
if (baudrate == 0U)
baudrate = SWO_DEFAULT_BAUD;
++decode_arg;
}
#endif
/* Check if `decode` has been given and if it has, enable ITM decoding */
if (argc > decode_arg && !strncmp(argv[decode_arg], "decode", strlen(argv[decode_arg]))) {
/* Check if there are specific ITM streams to enable and build a bitmask of them */
if (argc > decode_arg + 1) {
/* For each of the specified streams */
for (size_t i = decode_arg + 1U; i < (size_t)argc; ++i) {
/* Figure out which the next one is */
const uint32_t stream = strtoul(argv[i], NULL, 0);
/* If it's a valid ITM stream number, set it in the mask */
if (stream < 32U)
itm_stream_mask |= 1U << stream;
}
} else
/* Decode all ITM streams if non given */
itm_stream_mask = 0xffffffffU;
}
/* Now enable SWO data recovery */
swo_init(capture_mode, baudrate, itm_stream_mask);
/* And show the user what we've done - first the channel mask from MSb to LSb */
gdb_outf("Channel mask: ");
for (size_t i = 0; i < 32U; ++i) {
const char bit = '0' + ((itm_stream_mask >> (31U - i)) & 1U);
gdb_outf("%c", bit);
}
gdb_outf("\n");
/* Then the connection information for programs that are scraping BMD's output to know what to connect to */
gdb_outf("Trace enabled for BMP serial %s, USB EP %u\n", serial_no, SWO_ENDPOINT);
return true;
}
static bool cmd_swo_disable(void)
{
swo_deinit(true);
gdb_out("Trace disabled\n");
return true;
}
static bool cmd_swo(target_s *target, int argc, const char **argv)
{
(void)target;
bool enable_swo = false;
if (argc >= 2 && !parse_enable_or_disable(argv[1], &enable_swo)) {
gdb_out("Usage: traceswo <enable|disable> [2000000] [decode [0 1 3 31]]\n");
return false;
}
if (enable_swo)
return cmd_swo_enable(argc - 1, argv + 1);
return cmd_swo_disable();
}
#endif
#if defined(PLATFORM_HAS_DEBUG) && CONFIG_BMDA == 0
static bool cmd_debug_bmp(target_s *target, int argc, const char **argv)
{
(void)target;
if (argc == 2 && !parse_enable_or_disable(argv[1], &debug_bmp))
return false;
if (argc > 2) {
gdb_outf("usage: monitor debug [enable|disable]\n");
return false;
}
gdb_outf("Debug mode is %s\n", debug_bmp ? "enabled" : "disabled");
return true;
}
#endif
#if CONFIG_BMDA == 1
static bool cmd_shutdown_bmda(target_s *target, int argc, const char **argv)
{
(void)target;
(void)argc;
(void)argv;
shutdown_bmda = true;
return true;
}
#endif
/*
* Heapinfo allows passing up to four uint32_t from host to target.
* Heapinfo can be used to quickly test a system with different heap and stack values, to see how much heap and stack is needed.
* - User sets up values for heap and stack using "mon heapinfo"
* - When the target boots, crt0.S does a heapinfo semihosting call to get these values for heap and stack.
* - If the target system crashes, increase heap or stack
* See newlib/libc/sys/arm/crt0.S "Issue Angel SWI to read stack info"
*/
static bool cmd_heapinfo(target_s *target, int argc, const char **argv)
{
if (target == NULL)
gdb_out("not attached\n");
else if (argc == 5) {
target_addr_t heap_base = strtoul(argv[1], NULL, 16);
target_addr_t heap_limit = strtoul(argv[2], NULL, 16);
target_addr_t stack_base = strtoul(argv[3], NULL, 16);
target_addr_t stack_limit = strtoul(argv[4], NULL, 16);
gdb_outf("heap_base: %08" PRIx32 " heap_limit: %08" PRIx32 " stack_base: %08" PRIx32 " stack_limit: "
"%08" PRIx32 "\n",
heap_base, heap_limit, stack_base, stack_limit);
target_set_heapinfo(target, heap_base, heap_limit, stack_base, stack_limit);
} else
gdb_outf("%s\n", "Set semihosting heapinfo: HEAP_BASE HEAP_LIMIT STACK_BASE STACK_LIMIT");
return true;
}