From 4412cc44591bd8cbb3b779620b745fca87f54957 Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Sun, 16 Oct 2022 00:15:37 -0700 Subject: [PATCH] Add OPENOCD_ADAPTER_SERIAL --- .../upload_methods/UploadMethodOPENOCD.cmake | 31 +++++++++++++++++-- .../upload_methods/UploadMethodPYOCD.cmake | 11 ++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/tools/cmake/upload_methods/UploadMethodOPENOCD.cmake b/tools/cmake/upload_methods/UploadMethodOPENOCD.cmake index 8130885b71f..3b8989662b3 100644 --- a/tools/cmake/upload_methods/UploadMethodOPENOCD.cmake +++ b/tools/cmake/upload_methods/UploadMethodOPENOCD.cmake @@ -4,6 +4,8 @@ ### OpenOCD Upload Method # This method needs the following parameters: # OPENOCD_CHIP_CONFIG_COMMANDS - Specifies all OpenOCD commands needed to configure openocd for your target processor. +# This method creates the following options: +# OPENOCD_ADAPTER_SERIAL - Serial number of the debug adapter to select for OpenOCD. Set to empty to detect any matching adapter. set(UPLOAD_SUPPORTS_DEBUG TRUE) @@ -11,7 +13,28 @@ set(UPLOAD_SUPPORTS_DEBUG TRUE) find_package(OpenOCD) set(UPLOAD_OPENOCD_FOUND ${OpenOCD_FOUND}) +### Setup options +set(OPENOCD_ADAPTER_SERIAL "" CACHE STRING "Serial number of the debug adapter to select for OpenOCD. Set to empty to detect any matching adapter.") + ### Function to generate upload target +set(OPENOCD_ADAPTER_SERIAL_COMMAND "") +if(NOT "${OPENOCD_ADAPTER_SERIAL}" STREQUAL "") + + # Generate script file that tells OpenOCD how to find the correct debug adapter. + file(GENERATE OUTPUT ${CMAKE_BINARY_DIR}/openocd_adapter_config.cfg CONTENT +"# Script to select the correct debug adapter with OpenOCD. +# This file is generated by UploadMethodOPENOCD.cmake. Your edits will be overwritten. +# As of OpenOCD 0.11, it appears that the 'hla' adapter driver (used for stlinks and some others) +# does not support the standard 'adapter serial' command, and instead requires its own 'hla_serial' command. +if { [adapter name] == \"hla\" } { + hla_serial \"${OPENOCD_ADAPTER_SERIAL}\" +} else { + adapter serial \"${OPENOCD_ADAPTER_SERIAL}\" +}") + + set(OPENOCD_ADAPTER_SERIAL_COMMAND -f ${CMAKE_BINARY_DIR}/openocd_adapter_config.cfg) +endif() + function(gen_upload_target TARGET_NAME BIN_FILE) # unlike other upload methods, OpenOCD uses the elf file @@ -19,7 +42,9 @@ function(gen_upload_target TARGET_NAME BIN_FILE) COMMENT "Flashing ${TARGET_NAME} with OpenOCD..." COMMAND ${OpenOCD} ${OPENOCD_CHIP_CONFIG_COMMANDS} - -c "program $ reset exit") + ${OPENOCD_ADAPTER_SERIAL_COMMAND} + -c "program $ reset exit" + VERBATIM) add_dependencies(flash-${TARGET_NAME} ${TARGET_NAME}) endfunction(gen_upload_target) @@ -30,5 +55,7 @@ add_custom_target(gdbserver COMMAND ${OpenOCD} ${OPENOCD_CHIP_CONFIG_COMMANDS} + ${OPENOCD_ADAPTER_SERIAL_COMMAND} -c "gdb_port ${GDB_PORT}" - USES_TERMINAL) + USES_TERMINAL + VERBATIM) diff --git a/tools/cmake/upload_methods/UploadMethodPYOCD.cmake b/tools/cmake/upload_methods/UploadMethodPYOCD.cmake index f7126fdb1fa..da855a06464 100644 --- a/tools/cmake/upload_methods/UploadMethodPYOCD.cmake +++ b/tools/cmake/upload_methods/UploadMethodPYOCD.cmake @@ -6,7 +6,7 @@ # PYOCD_TARGET_NAME - Name of your processor as passed to the -t option of pyOCD. This is usually the full or partial model number. # PYOCD_CLOCK_SPEED - Clock speed of the JTAG or SWD connection. Default is in Hz, but can use k and M suffixes for MHz and GHz # This method creates the following options: -# PYOCD_PROBE_UID - Probe UID to pass to pyOCD commands. You can get the UIDs from `python -m pyocd list`. +# PYOCD_PROBE_UID - Probe UID to pass to pyOCD commands. You can get the UIDs from `python -m pyocd list`. Set to empty to detect any probe. set(UPLOAD_SUPPORTS_DEBUG TRUE) @@ -15,12 +15,13 @@ include(CheckPythonPackage) check_python_package(pyocd HAVE_PYOCD) set(UPLOAD_PYOCD_FOUND ${HAVE_PYOCD}) +### Setup options +set(PYOCD_PROBE_UID "" CACHE STRING "Probe UID to pass to pyOCD commands. You can get the UIDs from `python -m pyocd list`. Set to empty to detect any probe.") + ### Function to generate upload target set(PYOCD_PROBE_ARGS "") -if(DEFINED PYOCD_PROBE_UID) - if(NOT "${PYOCD_PROBE_UID}" STREQUAL "") - set(PYOCD_PROBE_ARGS --probe ${PYOCD_PROBE_UID}) - endif() +if(NOT "${PYOCD_PROBE_UID}" STREQUAL "") + set(PYOCD_PROBE_ARGS --probe ${PYOCD_PROBE_UID}) endif() function(gen_upload_target TARGET_NAME BIN_FILE)