Skip to content

Commit 54ebb95

Browse files
perigosoRafael Silva
authored andcommitted
command: refactor auto scan command with a for loop
1 parent 25671eb commit 54ebb95

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed

src/command.c

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
#include "usb.h"
5252
#endif
5353

54+
typedef struct scan_command {
55+
bool (*scan)(void);
56+
const char *name;
57+
} scan_command_s;
58+
5459
static bool cmd_version(target_s *target, int argc, const char **argv);
5560
static bool cmd_help(target_s *target, int argc, const char **argv);
5661

@@ -379,44 +384,44 @@ bool cmd_auto_scan(target_s *target, int argc, const char **argv)
379384
if (connect_assert_nrst)
380385
platform_nrst_set_val(true); /* will be deasserted after attach */
381386

382-
bool scan_result = false;
383-
TRY (EXCEPTION_ALL) {
384-
#if CONFIG_BMDA == 1
385-
scan_result = bmda_jtag_scan();
386-
#else
387-
scan_result = jtag_scan();
388-
#endif
389-
if (!scan_result) {
390-
gdb_out("JTAG scan found no devices, trying SWD!\n");
391-
387+
static const scan_command_s scan_commands[] =
392388
#if CONFIG_BMDA == 1
393-
scan_result = bmda_swd_scan();
394-
#else
395-
scan_result = adiv5_swd_scan();
396-
#endif
397-
if (!scan_result) {
398-
gdb_out("SWD scan found no devices.\n");
389+
/* clang-format off */
390+
{
391+
{bmda_jtag_scan, "JTAG"},
392+
{bmda_swd_scan, "SWD"},
399393
#if defined(CONFIG_RVSWD) && defined(PLATFORM_HAS_RVSWD)
400-
#if CONFIG_BMDA == 1
401-
scan_result = bmda_rvswd_scan();
402-
#else
403-
scan_result = false;
394+
{bmda_rvswd_scan, "RVSWD"},
404395
#endif
405-
if (!scan_result)
406-
gdb_out("RVSWD scan found no devices.\n");
396+
};
397+
#else
398+
{
399+
{jtag_scan, "JTAG"},
400+
{adiv5_swd_scan, "SWD"},
401+
};
402+
/* clang-format on */
407403
#endif
408-
}
404+
405+
bool scan_result = false;
406+
for (size_t i = 0; i < ARRAY_LENGTH(scan_commands); i++) {
407+
TRY (EXCEPTION_ALL) {
408+
scan_result = scan_commands[i].scan();
409409
}
410-
}
411-
CATCH () {
412-
case EXCEPTION_TIMEOUT:
413-
gdb_outf("Timeout during scan. Is target stuck in WFI?\n");
414-
break;
415-
case EXCEPTION_ERROR:
416-
gdb_outf("Exception: %s\n", exception_frame.msg);
417-
break;
418-
default:
419-
break;
410+
CATCH () {
411+
case EXCEPTION_TIMEOUT:
412+
gdb_outf("Timeout during %s scan. Is target stuck in WFI?\n", scan_commands[i].name);
413+
break;
414+
case EXCEPTION_ERROR:
415+
gdb_outf("%s exception: %s\n", scan_commands[i].name, exception_frame.msg);
416+
break;
417+
default:
418+
break;
419+
}
420+
421+
if (scan_result)
422+
break;
423+
424+
gdb_outf("%s scan found no devices.\n", scan_commands[i].name);
420425
}
421426

422427
if (!scan_result) {

0 commit comments

Comments
 (0)