diff --git a/tools/cmake/UploadMethodManager.cmake b/tools/cmake/UploadMethodManager.cmake index 0f6a2b2e356..d4288ba8ae5 100644 --- a/tools/cmake/UploadMethodManager.cmake +++ b/tools/cmake/UploadMethodManager.cmake @@ -146,4 +146,10 @@ function(mbed_generate_upload_target target) else() gen_upload_target(${target} ${CMAKE_CURRENT_BINARY_DIR}/$.hex) endif() + + # Make sure building the upload target causes the target to be built first + if(TARGET flash-${target}) + add_dependencies(flash-${target} ${target}) + endif() + endfunction() \ No newline at end of file diff --git a/tools/cmake/upload_methods/FindLinkServer.cmake b/tools/cmake/upload_methods/FindLinkServer.cmake index 5bbf90527ab..bb08b929b0c 100644 --- a/tools/cmake/upload_methods/FindLinkServer.cmake +++ b/tools/cmake/upload_methods/FindLinkServer.cmake @@ -7,6 +7,7 @@ # This module defines: # LinkServer - Whether the reqested tools were found. # LinkServer_PATH - full path to the LinkServer command line tool. +# LinkServer_VERSION - version number of LinkServer # Check for LinkServer install folders on Windows if("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows") @@ -26,6 +27,16 @@ find_program(LinkServer_PATH HINTS ${LINKSERVER_HINTS} ) -find_package_handle_standard_args(LinkServer REQUIRED_VARS LinkServer_PATH) +if(EXISTS "${LinkServer_PATH}") + # Detect version + execute_process(COMMAND ${LinkServer_PATH} --version + OUTPUT_VARIABLE LinkServer_VERSION_OUTPUT) + + # The output looks like "LinkServer v1.2.45 [Build 45] [2023-07-25 09:54:50]", so use a regex to grab the version + string(REGEX REPLACE "LinkServer v([0-9]+\\.[0-9]+\\.[0-9]+).*" "\\1" LinkServer_VERSION ${LinkServer_VERSION_OUTPUT}) +endif() + + +find_package_handle_standard_args(LinkServer REQUIRED_VARS LinkServer_PATH VERSION_VAR LinkServer_VERSION) diff --git a/tools/cmake/upload_methods/UploadMethodARDUINO_BOSSAC.cmake b/tools/cmake/upload_methods/UploadMethodARDUINO_BOSSAC.cmake index ca9a5c2a639..526d6975b5a 100644 --- a/tools/cmake/upload_methods/UploadMethodARDUINO_BOSSAC.cmake +++ b/tools/cmake/upload_methods/UploadMethodARDUINO_BOSSAC.cmake @@ -28,6 +28,4 @@ function(gen_upload_target TARGET_NAME BINARY_FILE) --write ${BINARY_FILE} --reset) - add_dependencies(flash-${TARGET_NAME} ${TARGET_NAME}) - endfunction(gen_upload_target) diff --git a/tools/cmake/upload_methods/UploadMethodJLINK.cmake b/tools/cmake/upload_methods/UploadMethodJLINK.cmake index 19b06a21b6b..11ae4c78046 100644 --- a/tools/cmake/upload_methods/UploadMethodJLINK.cmake +++ b/tools/cmake/upload_methods/UploadMethodJLINK.cmake @@ -71,9 +71,6 @@ exit -ExitOnError -CommandFile ${COMMAND_FILE_PATH}) - - add_dependencies(flash-${TARGET_NAME} ${TARGET_NAME}) - endfunction(gen_upload_target) ### Commands to run the debug server. diff --git a/tools/cmake/upload_methods/UploadMethodLINKSERVER.cmake b/tools/cmake/upload_methods/UploadMethodLINKSERVER.cmake index edc46a76040..5ee41ed2765 100644 --- a/tools/cmake/upload_methods/UploadMethodLINKSERVER.cmake +++ b/tools/cmake/upload_methods/UploadMethodLINKSERVER.cmake @@ -23,6 +23,12 @@ endif() find_package(LinkServer) set(UPLOAD_LINKSERVER_FOUND ${LinkServer_FOUND}) +if(LinkServer_FOUND) + if(${LinkServer_VERSION} VERSION_LESS 1.5.30 AND "${MBED_OUTPUT_EXT}" STREQUAL "hex") + message(FATAL_ERROR "LinkServer <1.5.30 does not support flashing hex files! Please upgrade LinkServer and then clean and rebuild the project.") + endif() +endif() + ### Function to generate upload target function(gen_upload_target TARGET_NAME BINARY_FILE) @@ -37,8 +43,6 @@ function(gen_upload_target TARGET_NAME BINARY_FILE) --addr ${MBED_UPLOAD_BASE_ADDR} ${BINARY_FILE}) - add_dependencies(flash-${TARGET_NAME} ${TARGET_NAME}) - endfunction(gen_upload_target) ### Commands to run the debug server. diff --git a/tools/cmake/upload_methods/UploadMethodMBED.cmake b/tools/cmake/upload_methods/UploadMethodMBED.cmake index 654a6c26705..e1e11ca2c3a 100644 --- a/tools/cmake/upload_methods/UploadMethodMBED.cmake +++ b/tools/cmake/upload_methods/UploadMethodMBED.cmake @@ -25,6 +25,4 @@ function(gen_upload_target TARGET_NAME BINARY_FILE) ${mbed-os_SOURCE_DIR}/tools/python VERBATIM) - add_dependencies(flash-${TARGET_NAME} ${TARGET_NAME}) - endfunction(gen_upload_target) diff --git a/tools/cmake/upload_methods/UploadMethodOPENOCD.cmake b/tools/cmake/upload_methods/UploadMethodOPENOCD.cmake index 34f6fed9fdc..61d2b09adaa 100644 --- a/tools/cmake/upload_methods/UploadMethodOPENOCD.cmake +++ b/tools/cmake/upload_methods/UploadMethodOPENOCD.cmake @@ -41,7 +41,6 @@ endif() function(gen_upload_target TARGET_NAME BINARY_FILE) - # unlike other upload methods, OpenOCD uses the elf file add_custom_target(flash-${TARGET_NAME} COMMENT "Flashing ${TARGET_NAME} with OpenOCD..." COMMAND ${OpenOCD} @@ -51,7 +50,6 @@ function(gen_upload_target TARGET_NAME BINARY_FILE) -c "program ${BINARY_FILE} ${MBED_UPLOAD_BASE_ADDR} reset exit" VERBATIM) - add_dependencies(flash-${TARGET_NAME} ${TARGET_NAME}) endfunction(gen_upload_target) ### Commands to run the debug server. diff --git a/tools/cmake/upload_methods/UploadMethodPICOTOOL.cmake b/tools/cmake/upload_methods/UploadMethodPICOTOOL.cmake index 340ae768676..aad1ed715d0 100644 --- a/tools/cmake/upload_methods/UploadMethodPICOTOOL.cmake +++ b/tools/cmake/upload_methods/UploadMethodPICOTOOL.cmake @@ -29,12 +29,16 @@ function(gen_upload_target TARGET_NAME BINARY_FILE) list(APPEND PICOTOOL_TARGET_ARGS --address ${PICOTOOL_TARGET_ADDRESS}) endif() + if("${MBED_OUTPUT_EXT}" STREQUAL "hex") + message(FATAL_ERROR "Bin file output must be enabled to use picotool. Set MBED_OUTPUT_EXT to empty string or to 'bin' in your top level CMakeLists.txt!") + endif() + add_custom_target(flash-${TARGET_NAME} COMMAND ${Picotool} load + --verify --execute - $) - - add_dependencies(flash-${TARGET_NAME} ${TARGET_NAME}) + --offset ${MBED_UPLOAD_BASE_ADDR} + ${CMAKE_CURRENT_BINARY_DIR}/$.bin) endfunction(gen_upload_target) diff --git a/tools/cmake/upload_methods/UploadMethodPYOCD.cmake b/tools/cmake/upload_methods/UploadMethodPYOCD.cmake index 848b54bdd69..ad6d947a349 100644 --- a/tools/cmake/upload_methods/UploadMethodPYOCD.cmake +++ b/tools/cmake/upload_methods/UploadMethodPYOCD.cmake @@ -34,7 +34,6 @@ function(gen_upload_target TARGET_NAME BINARY_FILE) --base-address ${MBED_UPLOAD_BASE_ADDR} ${BINARY_FILE}) - add_dependencies(flash-${TARGET_NAME} ${TARGET_NAME}) endfunction(gen_upload_target) ### Commands to run the debug server. diff --git a/tools/cmake/upload_methods/UploadMethodSTLINK.cmake b/tools/cmake/upload_methods/UploadMethodSTLINK.cmake index a7a40117d88..872af59ae66 100644 --- a/tools/cmake/upload_methods/UploadMethodSTLINK.cmake +++ b/tools/cmake/upload_methods/UploadMethodSTLINK.cmake @@ -32,7 +32,6 @@ function(gen_upload_target TARGET_NAME BINARY_FILE) ${STLINK_ARGS} write ${BINARY_FILE} ${MBED_UPLOAD_BASE_ADDR}) - add_dependencies(flash-${TARGET_NAME} ${TARGET_NAME}) endfunction(gen_upload_target) ### Commands to run the debug server. diff --git a/tools/cmake/upload_methods/UploadMethodSTM32CUBE.cmake b/tools/cmake/upload_methods/UploadMethodSTM32CUBE.cmake index f6229fe8776..04561abdb67 100644 --- a/tools/cmake/upload_methods/UploadMethodSTM32CUBE.cmake +++ b/tools/cmake/upload_methods/UploadMethodSTM32CUBE.cmake @@ -36,8 +36,6 @@ function(gen_upload_target TARGET_NAME BINARY_FILE) -w ${BINARY_FILE} ${MBED_UPLOAD_BASE_ADDR} -rst) - add_dependencies(flash-${TARGET_NAME} ${TARGET_NAME}) - endfunction(gen_upload_target) ### Commands to run the debug server.