Skip to content

Commit

Permalink
command: refactor auto scan command with a for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
perigoso authored and Rafael Silva committed Feb 6, 2025
1 parent 25671eb commit 54ebb95
Showing 1 changed file with 38 additions and 33 deletions.
71 changes: 38 additions & 33 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
#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);

Expand Down Expand Up @@ -379,44 +384,44 @@ bool cmd_auto_scan(target_s *target, int argc, const char **argv)
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
if (!scan_result) {
gdb_out("JTAG scan found no devices, trying SWD!\n");

static const scan_command_s scan_commands[] =
#if CONFIG_BMDA == 1
scan_result = bmda_swd_scan();
#else
scan_result = adiv5_swd_scan();
#endif
if (!scan_result) {
gdb_out("SWD scan found no devices.\n");
/* clang-format off */
{
{bmda_jtag_scan, "JTAG"},
{bmda_swd_scan, "SWD"},
#if defined(CONFIG_RVSWD) && defined(PLATFORM_HAS_RVSWD)
#if CONFIG_BMDA == 1
scan_result = bmda_rvswd_scan();
#else
scan_result = false;
{bmda_rvswd_scan, "RVSWD"},
#endif
if (!scan_result)
gdb_out("RVSWD scan found no devices.\n");
};
#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 scan. Is target stuck in WFI?\n");
break;
case EXCEPTION_ERROR:
gdb_outf("Exception: %s\n", exception_frame.msg);
break;
default:
break;
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) {
Expand Down

0 comments on commit 54ebb95

Please sign in to comment.