1 set(OPENOCD "" CACHE STRING "path to openocd (default: search for it)")
2 set(OPENOCD_CFG "" CACHE STRING "path to openocd configuration (default: generate automatically)")
3 set(OPENOCD_INTERFACE "" CACHE STRING "openocd interface name (default: automatic depending on target)")
6 set(OPENOCD_PATH ${OPENOCD})
8 find_program(OPENOCD_FOUND_PATH NAMES openocd openocd.exe)
9 if (NOT OPENOCD_FOUND_PATH)
10 message(STATUS "Could not find openocd, debugging won't be available")
12 set(OPENOCD_PATH ${OPENOCD_FOUND_PATH})
17 # Retrieve version number as a sanity check
18 execute_process(COMMAND ${OPENOCD_PATH} -v
20 ERROR_VARIABLE OPENOCD_HELP
21 RESULT_VARIABLE OPENOCD_RESULT)
23 string(REPLACE "\n" ";" OPENOCD_HELP_LINES ${OPENOCD_HELP})
24 list(GET OPENOCD_HELP_LINES 0 OPENOCD_FIRST_HELP_LINE)
25 string(REPLACE "\r" "" OPENOCD_HELP_LINE ${OPENOCD_FIRST_HELP_LINE})
26 if (NOT OPENOCD_RESULT EQUAL 0)
27 # User provided an incorrect path
28 message(FATAL_ERROR "error executing ${OPENOCD_PATH} (${OPENOCD_RESULT})")
30 message(STATUS "using openocd: ${OPENOCD_HELP_LINE}")
31 add_custom_target(openocd ${OPENOCD_PATH} -f ${OPENOCD_CFG}
32 COMMENT "Run openocd using OPENOCD_CFG=(${OPENOCD_CFG}) as configuration"
37 function(setup_openocd target_exe target_name)
39 set(openocd_interface ${OPENOCD_INTERFACE})
41 get_property(openocd_interface TARGET ${target_exe} PROPERTY OPENOCD_DEFAULT_INTERFACE)
43 get_property(openocd_target TARGET ${target_exe} PROPERTY OPENOCD_TARGET)
44 if(OPENOCD_CFG OR (openocd_target AND openocd_interface))
45 set(openocd_run_target "openocd_${target_name}")
46 if (OPENOCD_CFG AND NOT OPENOCD_CFG STREQUAL "")
47 get_filename_component(openocd_cfg_path ${OPENOCD_CFG}
49 BASE_DIR ${CMAKE_BINARY_DIR})
51 set(openocd_cfg_path ${CMAKE_BINARY_DIR}/openocd/${target_name}.cfg)
53 OUTPUT ${openocd_cfg_path}
54 COMMENT "Generating openocd configuration for ${openocd_target} via ${openocd_interface}"
55 COMMAND ${CMAKE_COMMAND} -P ${MAIN_DIR}/cmake/openocd_cfg.cmake
56 ${openocd_target} ${openocd_interface} ${openocd_cfg_path}
60 # Target for openocd configuration
61 set(openocd_cfg_target "openocd_cfg_${target_name}")
62 add_custom_target(${openocd_cfg_target} DEPENDS ${openocd_cfg_path})
63 exclude_from_all(${openocd_cfg_target})
65 # Target for running openocd
66 add_custom_target(${openocd_run_target} ${OPENOCD_PATH} -f ${openocd_cfg_path}
67 COMMENT "Running openocd for target ${target_name} via ${openocd_interface}"
68 DEPENDS ${openocd_cfg_path}
71 exclude_from_all(${openocd_run_target})
72 # Target for flashing via openocd
73 set(openocd_flash_target "openocd_flash_${target_name}")
74 add_custom_target(${openocd_flash_target} ${CMAKE_COMMAND} -E env
75 OPENOCD_CMD=${OPENOCD_PATH}
76 ${MAIN_UTILS_DIR}/openocd_flash.py -f
77 ${openocd_cfg_path} $<TARGET_FILE:${target_exe}>
79 COMMENT "Flashing ${target_name} with openocd"
80 DEPENDS ${openocd_cfg_path} ${target_exe}
82 exclude_from_all(${openocd_flash_target})