diff --git a/targets/upload_method_cfg/K64F.cmake b/targets/upload_method_cfg/K64F.cmake index e91e120f336..8723b494155 100644 --- a/targets/upload_method_cfg/K64F.cmake +++ b/targets/upload_method_cfg/K64F.cmake @@ -3,11 +3,10 @@ # include app.cmake and where you add mbed os as a subdirectory. # # Notes: -# 1. PyOCD did not actually work in my testing as of Apr 2024, though this device is supposed to be supported -# 2. Be sure to update the DAPLink firmware on the board via these instructions: https://os.mbed.com/blog/entry/DAPLink-bootloader-update/ -# 3. OpenOCD 0.12 flashes this device perfectly and can enter a debug session, but cannot hit breakpoints -# 4. LinkServer can both flash and debug, so it's the recommended upload method for this device. -# 5. LinkServer does appear to have a bug where it doesn't map the peripheral registers as valid memory, so you can't +# 1. Be sure to update the DAPLink firmware on the board via these instructions: https://os.mbed.com/blog/entry/DAPLink-bootloader-update/ +# 2. OpenOCD 0.12 flashes this device perfectly and can enter a debug session, but cannot hit breakpoints +# 3. LinkServer can both flash and debug, so it's the recommended upload method for this device. +# 4. LinkServer does appear to have a bug where it doesn't map the peripheral registers as valid memory, so you can't # inspect them. I was able to work around this by inserting a block like this into /devices/FRDM-K64F.json: # # "name": "MK64FN1M0xxx12", diff --git a/tools/cmake/UploadMethodManager.cmake b/tools/cmake/UploadMethodManager.cmake index 4e92db5d3b7..839c52814eb 100644 --- a/tools/cmake/UploadMethodManager.cmake +++ b/tools/cmake/UploadMethodManager.cmake @@ -2,34 +2,71 @@ # SPDX-License-Identifier: Apache-2.0 # ---------------------------------------------- -# Load the upload method that the user selects +# Common upload method options # This variable should have been set in app.cmake or by the upload method cfg file, sanity check here if(NOT DEFINED UPLOAD_METHOD_DEFAULT) message(FATAL_ERROR "UPLOAD_METHOD_DEFAULT not found.") endif() +## Upload method set(UPLOAD_METHOD "${UPLOAD_METHOD_DEFAULT}" CACHE STRING "Method for uploading programs to the mbed") -# use a higher numbered port to allow use without root on Linux/Mac -set(GDB_PORT 23331 CACHE STRING "Port that the GDB server will be started on.") - -# Upload methods must be uppercase, guard against the user making a mistake (since Windows will allow opening -# an include file with the wrong case, the error message gets confusing) +# Upload methods must be uppercase, guard against the user making a mistake (since Windows and Mac will allow including +# an include file with the wrong case, the error that happens later gets confusing) string(TOUPPER "${UPLOAD_METHOD}" UPLOAD_METHOD_UCASE) if(NOT "${UPLOAD_METHOD_UCASE}" STREQUAL "${UPLOAD_METHOD}") message(WARNING "UPLOAD_METHOD value should be uppercase. It has been automatically changed to \"${UPLOAD_METHOD_UCASE}\".") set(UPLOAD_METHOD "${UPLOAD_METHOD_UCASE}" CACHE STRING "" FORCE) endif() -# Load the upload method. This is expected to set the following variables: -# UPLOAD_${UPLOAD_METHOD}_FOUND - True iff the dependencies for this upload method were found -# UPLOAD_SUPPORTS_DEBUG - True iff this upload method supports debugging -# UPLOAD_GDBSERVER_DEBUG_COMMAND - Command to start a new GDB server -# UPLOAD_WANTS_EXTENDED_REMOTE - True iff GDB should use "target extended-remote" to connect to the GDB server -# UPLOAD_LAUNCH_COMMANDS - List of GDB commands to run after launching GDB. -# UPLOAD_RESTART_COMMANDS - List of GDB commands to run when the "restart chip" function is used. +## GDB port +# use a higher numbered port to allow use without root on Linux/Mac +set(MBED_GDB_PORT 23331 CACHE STRING "Port that the GDB server will be started on.") + +## Upload tool serial number +set(MBED_UPLOAD_SERIAL_NUMBER "" CACHE STRING "Serial number of the Mbed board or the programming tool, for upload methods that select by serial number.") + +# Handle legacy per-upload-method aliases for the upload serial number +foreach(LEGACY_VAR_NAME JLINK_USB_SERIAL_NUMBER LINKSERVER_PROBE_SN MBED_TARGET_UID OPENOCD_ADAPTER_SERIAL PYOCD_PROBE_UID STLINK_SERIAL_ARGUMENT STM32CUBE_PROBE_SN) + if(DEFINED ${LEGACY_VAR_NAME}) + if(NOT "${${LEGACY_VAR_NAME}}" STREQUAL "") + message(WARNING "${LEGACY_VAR_NAME} is deprecated, set the MBED_UPLOAD_SERIAL_NUMBER variable instead. MBED_UPLOAD_SERIAL_NUMBER will be set to the value of ${LEGACY_VAR_NAME}.") + set(MBED_UPLOAD_SERIAL_NUMBER ${${LEGACY_VAR_NAME}} CACHE STRING "" FORCE) + endif() + endif() +endforeach() + +## Upload base address +if(NOT DEFINED MBED_UPLOAD_BASE_ADDR OR "${MBED_UPLOAD_BASE_ADDR}" STREQUAL "") + set(BASE_ADDR_DESCRIPTION "Base address for uploading code, i.e. the memory address where the first byte of the bin/hex file will get loaded to (with 0x prefix). Generally should point to the start of the desired flash bank and defaults to the configured start of the primary ROM bank (MBED_CONFIGURED_ROM_START).") + if(MBED_CONFIG_DEFINITIONS MATCHES "MBED_CONFIGURED_ROM_START=(0x[0-9a-zA-Z]+)") + set(MBED_UPLOAD_BASE_ADDR ${CMAKE_MATCH_1} CACHE STRING ${BASE_ADDR_DESCRIPTION}) + else() + if(NOT ${UPLOAD_METHOD} STREQUAL "NONE") + message(FATAL_ERROR "Since no ROM banks have been defined, you need to set the MBED_UPLOAD_BASE_ADDR option so we know where to upload code. NOTE: If you upgraded from an old version of Mbed CE and are getting this error, delete and reconfigure your CMake build directory.") + endif() + endif() +endif() + +# ---------------------------------------------- +# Load the upload method. +# Upload methods are expected to refer to the following variables: +# - MBED_UPLOAD_SERIAL_NUMBER - USB serial number of the mbed board or of the programmer +# - MBED_UPLOAD_BASE_ADDR - Base address of the flash where the bin file will be updated +# +# Upload methods are expected to set the following variables: +# - UPLOAD_${UPLOAD_METHOD}_FOUND - True iff the dependencies for this upload method were found +# - UPLOAD_SUPPORTS_DEBUG - True iff this upload method supports debugging +# - UPLOAD_GDBSERVER_DEBUG_COMMAND - Command to start a new GDB server +# - UPLOAD_WANTS_EXTENDED_REMOTE - True iff GDB should use "target extended-remote" to connect to the GDB server +# - UPLOAD_LAUNCH_COMMANDS - List of GDB commands to run after launching GDB. +# - UPLOAD_RESTART_COMMANDS - List of GDB commands to run when the "restart chip" function is used. # See here for more info: https://github.com/mbed-ce/mbed-os/wiki/Debugger-Commands-and-State-in-Upload-Methods +# +# WARNING: Upload method files are included during the add_subdirectory(mbed-os) call in the top-level CMakeLists.txt. This means that +# if you declare variables in an upload method script, you have to save them as CACHE INTERNAL (or PARENT_SCOPE) so that they are accessible +# to code running outside of the mbed-os subdirectory. include(UploadMethod${UPLOAD_METHOD}) if(NOT "${UPLOAD_${UPLOAD_METHOD}_FOUND}") diff --git a/tools/cmake/upload_methods/UploadMethodJLINK.cmake b/tools/cmake/upload_methods/UploadMethodJLINK.cmake index f18c56d4875..cdfa86ccf68 100644 --- a/tools/cmake/upload_methods/UploadMethodJLINK.cmake +++ b/tools/cmake/upload_methods/UploadMethodJLINK.cmake @@ -7,7 +7,6 @@ # JLINK_CLOCK_SPEED - Speed to run the J-Link at, in KHz. # JLINK_CPU_NAME - Name of CPU to pass to J-Link # This method has the following options: -# JLINK_USB_SERIAL_NUMBER - Use a J-Link connected over USB with the specified serial number. # JLINK_NETWORK_ADDRESS - Use a J-Link connected over the network with the given [:port] # JLINK_NO_GUI - If set to true, suppress GUI dialog boxes from the J-Link software. # @@ -19,14 +18,13 @@ find_package(JLINK) set(UPLOAD_JLINK_FOUND ${JLINK_FOUND}) ### Setup options -set(JLINK_USB_SERIAL_NUMBER "" CACHE STRING "Use a J-Link connected over USB with the specified serial number.") set(JLINK_NETWORK_ADDRESS "" CACHE STRING "Use a J-Link connected over the network with the given [:port]") # Figure out -select option. See here: https://wiki.segger.com/J-Link_GDB_Server#-select -if((NOT "${JLINK_USB_SERIAL_NUMBER}" STREQUAL "") AND (NOT "${JLINK_NETWORK_ADDRESS}" STREQUAL "")) - message(FATAL_ERROR "Cannot use both JLINK_USB_SERIAL_NUMBER and JLINK_NETWORK_ADDRESS at the same time!") -elseif(NOT "${JLINK_USB_SERIAL_NUMBER}" STREQUAL "") - set(JLINK_SELECT_ARG -Select usb=${JLINK_USB_SERIAL_NUMBER} CACHE INTERNAL "" FORCE) +if((NOT "${MBED_UPLOAD_SERIAL_NUMBER}" STREQUAL "") AND (NOT "${JLINK_NETWORK_ADDRESS}" STREQUAL "")) + message(FATAL_ERROR "Cannot use both MBED_UPLOAD_SERIAL_NUMBER and JLINK_NETWORK_ADDRESS at the same time!") +elseif(NOT "${MBED_UPLOAD_SERIAL_NUMBER}" STREQUAL "") + set(JLINK_SELECT_ARG -Select usb=${MBED_UPLOAD_SERIAL_NUMBER} CACHE INTERNAL "" FORCE) elseif(NOT "${JLINK_NETWORK_ADDRESS}" STREQUAL "") set(JLINK_SELECT_ARG -Select ip=${JLINK_NETWORK_ADDRESS} CACHE INTERNAL "" FORCE) else() @@ -51,8 +49,11 @@ function(gen_upload_target TARGET_NAME BINARY_FILE) # create command file for j-link set(COMMAND_FILE_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/flash-${TARGET_NAME}.jlink) + + # Note: loadfile currently only honors the base address for .bin files. For hex files it uses the offset read + # from the hex file. Unsure if that will be an issue or not... file(GENERATE OUTPUT ${COMMAND_FILE_PATH} CONTENT -"loadfile ${BINARY_FILE} +"loadfile ${BINARY_FILE} ${MBED_UPLOAD_BASE_ADDR} r go exit diff --git a/tools/cmake/upload_methods/UploadMethodLINKSERVER.cmake b/tools/cmake/upload_methods/UploadMethodLINKSERVER.cmake index 751c17032d2..2f28b90c968 100644 --- a/tools/cmake/upload_methods/UploadMethodLINKSERVER.cmake +++ b/tools/cmake/upload_methods/UploadMethodLINKSERVER.cmake @@ -4,18 +4,15 @@ ### NXP LinkServer Upload Method # This method needs the following parameters: # LINKSERVER_DEVICE - Chip name and board to connect to, separated by a colon. -# LINKSERVER_PROBE_SN - Serial number, or serial number substring, of the debug probe to connect to. If blank, will connect to any probe. set(UPLOAD_SUPPORTS_DEBUG TRUE) ### Handle options -set(LINKSERVER_PROBE_SN "" CACHE STRING "Serial number, or serial number substring, of the debug probe to connect to. If blank, will connect to any probe.") - -if("${LINKSERVER_PROBE_SN}" STREQUAL "") +if("${MBED_UPLOAD_SERIAL_NUMBER}" STREQUAL "") # This argument causes Redlink to connect to the first available debug probe set(LINKSERVER_PROBE_ARGS "" CACHE INTERNAL "" FORCE) else() - set(LINKSERVER_PROBE_ARGS --probe ${LINKSERVER_PROBE_SN} CACHE INTERNAL "" FORCE) + set(LINKSERVER_PROBE_ARGS --probe ${MBED_UPLOAD_SERIAL_NUMBER} CACHE INTERNAL "" FORCE) endif() if("${LINKSERVER_DEVICE}" STREQUAL "") @@ -37,7 +34,8 @@ function(gen_upload_target TARGET_NAME BINARY_FILE) ${LINKSERVER_PROBE_ARGS} ${LINKSERVER_DEVICE} load - $) + --addr ${MBED_UPLOAD_BASE_ADDR} + ${BINARY_FILE}) add_dependencies(flash-${TARGET_NAME} ${TARGET_NAME}) diff --git a/tools/cmake/upload_methods/UploadMethodMBED.cmake b/tools/cmake/upload_methods/UploadMethodMBED.cmake index 00bcbd774ec..654a6c26705 100644 --- a/tools/cmake/upload_methods/UploadMethodMBED.cmake +++ b/tools/cmake/upload_methods/UploadMethodMBED.cmake @@ -2,7 +2,6 @@ # This method needs the following parameters: # MBED_RESET_BAUDRATE - Serial baudrate to connect to the target at when resetting it. # This method creates the following options: -# MBED_TARGET_UID - Probe UID to pass to commands. You can get the UIDs from `python -m pyocd list`. set(UPLOAD_SUPPORTS_DEBUG FALSE) @@ -13,8 +12,6 @@ if(NOT DEFINED MBED_RESET_BAUDRATE) set(MBED_RESET_BAUDRATE 9600) endif() -set(MBED_TARGET_UID "" CACHE STRING "UID of mbed target to upload to if there are multiple connected. You can get the UIDs from `python -m pyocd list`") - ### Function to generate upload target function(gen_upload_target TARGET_NAME BINARY_FILE) @@ -23,7 +20,7 @@ function(gen_upload_target TARGET_NAME BINARY_FILE) ${BINARY_FILE} ${MBED_TARGET} ${MBED_RESET_BAUDRATE} - ${MBED_TARGET_UID} + ${MBED_UPLOAD_SERIAL_NUMBER} WORKING_DIRECTORY ${mbed-os_SOURCE_DIR}/tools/python VERBATIM) diff --git a/tools/cmake/upload_methods/UploadMethodOPENOCD.cmake b/tools/cmake/upload_methods/UploadMethodOPENOCD.cmake index 15f9edf1b63..db7733ec653 100644 --- a/tools/cmake/upload_methods/UploadMethodOPENOCD.cmake +++ b/tools/cmake/upload_methods/UploadMethodOPENOCD.cmake @@ -4,8 +4,9 @@ ### 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. +# OPENOCD_LOAD_ADDRESS - Address where the bin or hex file will be loaded to memory, e.g. 0x8000000. If not set, will default to +# the configured address of the first ROM bank (MBED_CONFIGURED_ROM_START) # 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. # OPENOCD_VERSION_RANGE - Acceptable version range of OpenOCD. This may be a single version, in which case it is treated as # a minimum, or a versionMin..." + -w ${BINARY_FILE} ${MBED_UPLOAD_BASE_ADDR} -rst) add_dependencies(flash-${TARGET_NAME} ${TARGET_NAME})