Skip to content

Commit d126cdb

Browse files
tom-vanborneoa
authored andcommitted
drivers/cmsis_dap: fix misleading error selecting not compiled backend
If one of CMSIS-DAP backends was not compiled (due to missing library or configure --disable-cmsis-dap) and Tcl config explicitly selected it, a misleading message "invalid backend argument to cmsis-dap backend <backend>" was printed. Create dummy backends in struct cmsis_dap_backend to replace a not built backend. Check for NULL open backend method to distinguish the backend is dummy. Rework 'cmsis-dap backend' command to honour dummy backend. While on it print more helpful error messages. Change-Id: I8f12aeaaecf19302032870bc232e5135c1d935e7 Signed-off-by: Tomas Vanek <[email protected]> Reviewed-on: https://review.openocd.org/c/openocd/+/8760 Tested-by: jenkins Reviewed-by: Antonio Borneo <[email protected]>
1 parent accbeae commit d126cdb

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

src/jtag/drivers/cmsis_dap.c

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,22 @@
3939
#include "cmsis_dap.h"
4040
#include "libusb_helper.h"
4141

42-
static const struct cmsis_dap_backend *const cmsis_dap_backends[] = {
43-
#if BUILD_CMSIS_DAP_USB == 1
44-
&cmsis_dap_usb_backend,
42+
/* Create a dummy backend for 'backend' command if real one does not build */
43+
#if BUILD_CMSIS_DAP_USB == 0
44+
const struct cmsis_dap_backend cmsis_dap_usb_backend = {
45+
.name = "usb_bulk",
46+
};
4547
#endif
4648

47-
#if BUILD_CMSIS_DAP_HID == 1
48-
&cmsis_dap_hid_backend,
49+
#if BUILD_CMSIS_DAP_HID == 0
50+
const struct cmsis_dap_backend cmsis_dap_hid_backend = {
51+
.name = "hid"
52+
};
4953
#endif
54+
55+
static const struct cmsis_dap_backend *const cmsis_dap_backends[] = {
56+
&cmsis_dap_usb_backend,
57+
&cmsis_dap_hid_backend,
5058
};
5159

5260
/* USB Config */
@@ -261,26 +269,32 @@ static int cmsis_dap_open(void)
261269
return ERROR_FAIL;
262270
}
263271

272+
int retval = ERROR_FAIL;
264273
if (cmsis_dap_backend >= 0) {
265274
/* Use forced backend */
266275
backend = cmsis_dap_backends[cmsis_dap_backend];
267-
if (backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, adapter_get_required_serial()) != ERROR_OK)
268-
backend = NULL;
276+
if (backend->open)
277+
retval = backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, adapter_get_required_serial());
278+
else
279+
LOG_ERROR("Requested CMSIS-DAP backend is disabled by configure");
280+
269281
} else {
270282
/* Try all backends */
271283
for (unsigned int i = 0; i < ARRAY_SIZE(cmsis_dap_backends); i++) {
272284
backend = cmsis_dap_backends[i];
273-
if (backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, adapter_get_required_serial()) == ERROR_OK)
285+
if (!backend->open)
286+
continue;
287+
288+
retval = backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, adapter_get_required_serial());
289+
if (retval == ERROR_OK)
274290
break;
275-
else
276-
backend = NULL;
277291
}
278292
}
279293

280-
if (!backend) {
294+
if (retval != ERROR_OK) {
281295
LOG_ERROR("unable to find a matching CMSIS-DAP device");
282296
free(dap);
283-
return ERROR_FAIL;
297+
return retval;
284298
}
285299

286300
dap->backend = backend;
@@ -293,7 +307,8 @@ static int cmsis_dap_open(void)
293307
static void cmsis_dap_close(struct cmsis_dap *dap)
294308
{
295309
if (dap->backend) {
296-
dap->backend->close(dap);
310+
if (dap->backend->close)
311+
dap->backend->close(dap);
297312
dap->backend = NULL;
298313
}
299314

@@ -2192,22 +2207,27 @@ COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command)
21922207

21932208
COMMAND_HANDLER(cmsis_dap_handle_backend_command)
21942209
{
2195-
if (CMD_ARGC == 1) {
2196-
if (strcmp(CMD_ARGV[0], "auto") == 0) {
2197-
cmsis_dap_backend = -1; /* autoselect */
2198-
} else {
2199-
for (unsigned int i = 0; i < ARRAY_SIZE(cmsis_dap_backends); i++) {
2200-
if (strcasecmp(cmsis_dap_backends[i]->name, CMD_ARGV[0]) == 0) {
2210+
if (CMD_ARGC != 1)
2211+
return ERROR_COMMAND_SYNTAX_ERROR;
2212+
2213+
if (strcmp(CMD_ARGV[0], "auto") == 0) {
2214+
cmsis_dap_backend = -1; /* autoselect */
2215+
} else {
2216+
for (unsigned int i = 0; i < ARRAY_SIZE(cmsis_dap_backends); i++) {
2217+
if (strcasecmp(cmsis_dap_backends[i]->name, CMD_ARGV[0]) == 0) {
2218+
if (cmsis_dap_backends[i]->open) {
22012219
cmsis_dap_backend = i;
22022220
return ERROR_OK;
22032221
}
2204-
}
22052222

2206-
command_print(CMD, "invalid backend argument to cmsis-dap backend <backend>");
2207-
return ERROR_COMMAND_ARGUMENT_INVALID;
2223+
command_print(CMD, "Requested cmsis-dap backend %s is disabled by configure",
2224+
cmsis_dap_backends[i]->name);
2225+
return ERROR_NOT_IMPLEMENTED;
2226+
}
22082227
}
2209-
} else {
2210-
return ERROR_COMMAND_SYNTAX_ERROR;
2228+
2229+
command_print(CMD, "invalid argument %s to cmsis-dap backend", CMD_ARGV[0]);
2230+
return ERROR_COMMAND_ARGUMENT_INVALID;
22112231
}
22122232

22132233
return ERROR_OK;

0 commit comments

Comments
 (0)