Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: scan return type + swdp naming phase out #1568

Merged
merged 6 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static bool cmd_version(target_s *t, int argc, const char **argv);
static bool cmd_help(target_s *t, int argc, const char **argv);

static bool cmd_jtag_scan(target_s *target, int argc, const char **argv);
static bool cmd_swdp_scan(target_s *t, int argc, const char **argv);
static bool cmd_swd_scan(target_s *t, int argc, const char **argv);
static bool cmd_auto_scan(target_s *t, int argc, const char **argv);
static bool cmd_frequency(target_s *t, int argc, const char **argv);
static bool cmd_targets(target_s *t, int argc, const char **argv);
Expand Down Expand Up @@ -85,7 +85,8 @@ 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"},
{"swdp_scan", cmd_swdp_scan, "Scan SW-DP for devices: [TARGET_ID]"},
{"swd_scan", cmd_swd_scan, "Scan SWD interface for devices: [TARGET_ID]"},
dragonmux marked this conversation as resolved.
Show resolved Hide resolved
{"swdp_scan", cmd_swd_scan, "Deprecated: use swd_scan instead"},
{"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"},
Expand Down Expand Up @@ -212,13 +213,13 @@ static bool cmd_jtag_scan(target_s *target, int argc, const char **argv)
if (connect_assert_nrst)
platform_nrst_set_val(true); /* will be deasserted after attach */

uint32_t devs = 0;
bool scan_result = false;
volatile exception_s e;
TRY_CATCH (e, EXCEPTION_ALL) {
#if PC_HOSTED == 1
devs = bmda_jtag_scan();
scan_result = bmda_jtag_scan();
#else
devs = jtag_scan();
scan_result = jtag_scan();
#endif
}
switch (e.type) {
Expand All @@ -230,7 +231,7 @@ static bool cmd_jtag_scan(target_s *target, int argc, const char **argv)
break;
}

if (devs == 0) {
if (!scan_result) {
platform_target_clk_output_enable(false);
platform_nrst_set_val(false);
gdb_out("JTAG device scan failed!\n");
Expand All @@ -243,7 +244,7 @@ static bool cmd_jtag_scan(target_s *target, int argc, const char **argv)
return true;
}

bool cmd_swdp_scan(target_s *t, int argc, const char **argv)
bool cmd_swd_scan(target_s *t, int argc, const char **argv)
{
(void)t;
volatile uint32_t targetid = 0;
Expand All @@ -255,13 +256,13 @@ bool cmd_swdp_scan(target_s *t, int argc, const char **argv)
if (connect_assert_nrst)
platform_nrst_set_val(true); /* will be deasserted after attach */

uint32_t devs = 0;
bool scan_result = false;
volatile exception_s e;
TRY_CATCH (e, EXCEPTION_ALL) {
#if PC_HOSTED == 1
devs = bmp_swd_scan(targetid);
scan_result = bmda_swd_scan(targetid);
#else
devs = adiv5_swdp_scan(targetid);
scan_result = adiv5_swd_scan(targetid);
#endif
}
switch (e.type) {
Expand All @@ -273,7 +274,7 @@ bool cmd_swdp_scan(target_s *t, int argc, const char **argv)
break;
}

if (devs == 0) {
if (!scan_result) {
platform_target_clk_output_enable(false);
platform_nrst_set_val(false);
gdb_out("SW-DP scan failed!\n");
Expand All @@ -297,24 +298,24 @@ bool cmd_auto_scan(target_s *t, int argc, const char **argv)
if (connect_assert_nrst)
platform_nrst_set_val(true); /* will be deasserted after attach */

uint32_t devs = 0;
bool scan_result = false;
volatile exception_s e;
TRY_CATCH (e, EXCEPTION_ALL) {
#if PC_HOSTED == 1
devs = bmda_jtag_scan();
scan_result = bmda_jtag_scan();
#else
devs = jtag_scan();
scan_result = jtag_scan();
#endif
if (devs > 0)
if (scan_result)
break;
gdb_out("JTAG scan found no devices, trying SWD!\n");

#if PC_HOSTED == 1
devs = bmp_swd_scan(0);
scan_result = bmda_swd_scan(0);
#else
devs = adiv5_swdp_scan(0);
scan_result = adiv5_swd_scan(0);
#endif
if (devs > 0)
if (scan_result)
break;

gdb_out("SW-DP scan found no devices.\n");
Expand All @@ -328,7 +329,7 @@ bool cmd_auto_scan(target_s *t, int argc, const char **argv)
break;
}

if (devs == 0) {
if (!scan_result) {
platform_target_clk_output_enable(false);
platform_nrst_set_val(false);
gdb_out("auto scan failed!\n");
Expand Down
8 changes: 4 additions & 4 deletions src/include/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ typedef uint32_t target_addr_t;
typedef struct target_controller target_controller_s;

#if PC_HOSTED == 1
uint32_t bmp_swd_scan(uint32_t targetid);
uint32_t bmda_jtag_scan(void);
bool bmda_swd_scan(uint32_t targetid);
bool bmda_jtag_scan(void);
#endif
uint32_t adiv5_swdp_scan(uint32_t targetid);
uint32_t jtag_scan(void);
bool adiv5_swd_scan(uint32_t targetid);
bool jtag_scan(void);

size_t target_foreach(void (*callback)(size_t index, target_s *target, void *context), void *context);
void target_list_free(void);
Expand Down
27 changes: 11 additions & 16 deletions src/platforms/hosted/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,21 +418,19 @@ static void display_target(size_t idx, target_s *target, void *context)
core_name ? core_name : "");
}

uint32_t scan_for_targets(const bmda_cli_options_s *const opt)
bool scan_for_targets(const bmda_cli_options_s *const opt)
{
if (opt->opt_scanmode == BMP_SCAN_JTAG)
return bmda_jtag_scan();
if (opt->opt_scanmode == BMP_SCAN_SWD)
return bmp_swd_scan(opt->opt_targetid);
uint32_t num_targets = bmda_jtag_scan();
if (num_targets)
return num_targets;
return bmda_swd_scan(opt->opt_targetid);
if (bmda_jtag_scan())
return true;
DEBUG_WARN("JTAG scan found no devices, trying SWD.\n");
num_targets = bmp_swd_scan(opt->opt_targetid);
if (num_targets)
return num_targets;
if (bmda_swd_scan(opt->opt_targetid))
return true;
DEBUG_ERROR("SW-DP scan failed!\n");
return 0U;
return false;
}

int cl_execute(bmda_cli_options_s *opt)
Expand All @@ -451,22 +449,19 @@ int cl_execute(bmda_cli_options_s *opt)
DEBUG_INFO("Running in Test Mode\n");
DEBUG_INFO("Target voltage: %s Volt\n", platform_target_voltage());

int res = 0;
uint32_t num_targets = scan_for_targets(opt);

if (!num_targets) {
if (!scan_for_targets(opt)) {
DEBUG_ERROR("No target found\n");
return -1;
}
num_targets = target_foreach(display_target, &num_targets);

const size_t num_targets = target_foreach(display_target, NULL);
if (opt->opt_target_dev > num_targets) {
DEBUG_ERROR(
"Given target number %" PRIu32 " not available max %" PRIu32 "\n", opt->opt_target_dev, num_targets);
DEBUG_ERROR("Given target number %" PRIu32 " not available max %zu\n", opt->opt_target_dev, num_targets);
return -1;
}
target_s *t = target_attach_n(opt->opt_target_dev, &cl_controller);

int res = 0;
int read_file = -1;
if (!t) {
DEBUG_ERROR("Can not attach to target %d\n", opt->opt_target_dev);
Expand Down
11 changes: 5 additions & 6 deletions src/platforms/hosted/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void platform_init(int argc, char **argv)
}
}

uint32_t bmp_swd_scan(uint32_t targetid)
bool bmda_swd_scan(const uint32_t targetid)
{
info.is_jtag = false;
platform_max_frequency_set(cl_opts.opt_max_swj_frequency);
Expand All @@ -170,16 +170,15 @@ uint32_t bmp_swd_scan(uint32_t targetid)
case BMP_TYPE_FTDI:
case BMP_TYPE_CMSIS_DAP:
case BMP_TYPE_JLINK:
return adiv5_swdp_scan(targetid);
break;
return adiv5_swd_scan(targetid);

#if HOSTED_BMP_ONLY == 0
case BMP_TYPE_STLINK_V2:
return stlink_swd_scan();
#endif

default:
return 0;
return false;
}
}

Expand Down Expand Up @@ -217,7 +216,7 @@ void bmda_add_jtag_dev(const uint32_t dev_index, const jtag_dev_s *const jtag_de
remote_add_jtag_dev(dev_index, jtag_dev);
}

uint32_t bmda_jtag_scan(void)
bool bmda_jtag_scan(void)
{
info.is_jtag = true;

Expand All @@ -236,7 +235,7 @@ uint32_t bmda_jtag_scan(void)
#endif

default:
return 0;
return false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/platforms/hosted/stlinkv2.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ uint32_t stlink_raw_access(adiv5_debug_port_s *dp, uint8_t rnw, uint16_t addr, u
}

if (result == STLINK_ERROR_FAIL)
raise_exception(EXCEPTION_ERROR, "SWDP invalid ACK");
raise_exception(EXCEPTION_ERROR, "SWD invalid ACK");
return response;
}

Expand Down
4 changes: 2 additions & 2 deletions src/platforms/hosted/stlinkv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
#define STLINK_ERROR_WAIT 1

bool stlink_init(void);
uint32_t stlink_swd_scan(void);
uint32_t stlink_jtag_scan(void);
bool stlink_swd_scan(void);
bool stlink_jtag_scan(void);
int stlink_hwversion(void);
const char *stlink_target_voltage(void);
void stlink_nrst_set_val(bool assert);
Expand Down
6 changes: 3 additions & 3 deletions src/platforms/hosted/stlinkv2_jtag.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
static int stlink_enter_debug_jtag(void);
static size_t stlink_read_idcodes(uint32_t *idcodes);

uint32_t stlink_jtag_scan(void)
bool stlink_jtag_scan(void)
{
uint32_t idcodes[STLINK_JTAG_MAX_DEVS];
target_list_free();

jtag_dev_count = 0;
memset(jtag_devs, 0, sizeof(jtag_devs));
if (stlink_enter_debug_jtag())
return 0;
return false;
jtag_dev_count = stlink_read_idcodes(idcodes);
/* Check for known devices and handle accordingly */
for (uint32_t i = 0; i < jtag_dev_count; ++i)
Expand All @@ -51,7 +51,7 @@ uint32_t stlink_jtag_scan(void)
}
}
}
return jtag_dev_count;
return jtag_dev_count > 0;
}

static int stlink_enter_debug_jtag(void)
Expand Down
9 changes: 5 additions & 4 deletions src/platforms/hosted/stlinkv2_swd.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "jtag_devs.h"
#include "target_internal.h"

uint32_t stlink_swd_scan(void)
bool stlink_swd_scan(void)
{
target_list_free();

Expand All @@ -38,12 +38,12 @@ uint32_t stlink_swd_scan(void)
uint8_t data[2];
stlink_send_recv_retry(&command, sizeof(command), data, sizeof(data));
if (stlink_usb_error_check(data, true))
return 0;
return false;

adiv5_debug_port_s *dp = calloc(1, sizeof(*dp));
if (!dp) { /* calloc failed: heap exhaustion */
DEBUG_ERROR("calloc: failed in %s\n", __func__);
return 0;
return false;
}

dp->dp_read = firmware_swdp_read;
Expand All @@ -53,5 +53,6 @@ uint32_t stlink_swd_scan(void)

adiv5_dp_error(dp);
adiv5_dp_init(dp, 0);
return target_list ? 1U : 0U;

return target_list != NULL;
}
10 changes: 5 additions & 5 deletions src/target/adiv5_swd.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ static uint32_t firmware_dp_low_read(const uint16_t addr)
return res == SWDP_ACK_OK ? data : 0;
}

uint32_t adiv5_swdp_scan(const uint32_t targetid)
bool adiv5_swd_scan(const uint32_t targetid)
{
/* Free the device list if any */
target_list_free();

adiv5_debug_port_s *dp = calloc(1, sizeof(*dp));
if (!dp) { /* calloc failed: heap exhaustion */
DEBUG_ERROR("calloc: failed in %s\n", __func__);
return 0;
return false;
}

dp->dp_low_write = firmware_dp_low_write;
Expand All @@ -163,7 +163,7 @@ uint32_t adiv5_swdp_scan(const uint32_t targetid)
#else
if (!bmda_swd_dp_init(dp)) {
free(dp);
return 0;
return false;
}
#endif

Expand Down Expand Up @@ -212,7 +212,7 @@ uint32_t adiv5_swdp_scan(const uint32_t targetid)
/* Give up */
DEBUG_ERROR("No usable DP found\n");
free(dp);
return 0;
return false;
}

/* DP must have the version field set so adiv5_dp_read() does protocol recovery correctly */
Expand Down Expand Up @@ -247,7 +247,7 @@ uint32_t adiv5_swdp_scan(const uint32_t targetid)
adiv5_dp_init(dp, 0);
}

return target_list ? 1U : 0U;
return target_list != NULL;
}

/*
Expand Down
Loading